forked from gambitph/Titan-Framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-option-upload.php
300 lines (251 loc) · 8.9 KB
/
class-option-upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class TitanFrameworkOptionUpload extends TitanFrameworkOption {
private static $firstLoad = true;
public $defaultSecondarySettings = array(
'size' => 'full', // The size of the image to use in the generated CSS
'placeholder' => '', // show this when blank
);
/**
* Constructor
*
* @return void
* @since 1.5
*/
function __construct( $settings, $owner ) {
parent::__construct( $settings, $owner );
add_filter( 'tf_generate_css_upload_' . $this->getOptionNamespace(), array( $this, 'generateCSS' ), 10, 2 );
}
/**
* Generates CSS for the font, this is used in TitanFrameworkCSS
*
* @param string $css The CSS generated
* @param TitanFrameworkOption $option The current option being processed
* @return string The CSS generated
* @since 1.5
*/
public function generateCSS( $css, $option ) {
if ( $this->settings['id'] != $option->settings['id'] ) {
return $css;
}
$value = $this->getFramework()->getOption( $option->settings['id'] );
if ( empty( $value ) ) {
return $css;
}
if ( is_numeric( $value ) ) {
$size = ! empty( $option->settings['size'] ) ? $option->settings['size'] : 'thumbnail';
$attachment = wp_get_attachment_image_src( $value, $size );
$value = $attachment[0];
}
$css .= "\$" . $option->settings['id'] . ": url(" . $value . ");";
if ( ! empty( $option->settings['css'] ) ) {
// In the css parameter, we accept the term `value` as our current value,
// translate it into the SaSS variable for the current option
$css .= str_replace( 'value', '#{$' . $option->settings['id'] . '}', $option->settings['css'] );
}
return $css;
}
/*
* Display for options and meta
*/
public function display() {
self::createUploaderScript();
$this->echoOptionHeader();
// display the preview image
$value = $this->getValue();
if ( is_numeric( $value ) ) {
// gives us an array with the first element as the src or false on fail
$value = wp_get_attachment_image_src( $value, array( 150, 150 ) );
}
if ( ! is_array( $value ) ) {
$value = $this->getValue();
} else {
$value = $value[0];
}
$previewImage = '';
if ( ! empty( $value ) ) {
$previewImage = "<i class='dashicons dashicons-no-alt remove'></i><img src='" . esc_url( $value ) . "' style='display: none'/>";
}
echo "<div class='thumbnail tf-image-preview'>" . $previewImage . "</div>";
printf("<input name=\"%s\" placeholder=\"%s\" id=\"%s\" type=\"hidden\" value=\"%s\" />",
$this->getID(),
$this->settings['placeholder'],
$this->getID(),
esc_attr( $this->getValue() )
);
$this->echoOptionFooter();
}
/*
* Display for theme customizer
*/
public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) {
$wp_customize->add_control( new TitanFrameworkOptionUploadControl( $wp_customize, $this->getID(), array(
'label' => $this->settings['name'],
'section' => $section->getID(),
'settings' => $this->getID(),
'description' => $this->settings['desc'],
'priority' => $priority,
) ) );
}
public static function createUploaderScript() {
if ( ! self::$firstLoad ) {
return;
}
self::$firstLoad = false;
?>
<script>
jQuery(document).ready(function($){
"use strict";
function tfUploadOptionCenterImage($this) {
// console.log('preview image loaded');
var _preview = $this.parents('.tf-upload').find('.thumbnail');
$this.css({
'marginTop': ( _preview.height() - $this.height() ) / 2,
'marginLeft': ( _preview.width() - $this.width() ) / 2
}).show();
}
// Calculate display offset of preview image on load
$('.tf-upload .thumbnail img').load(function() {
tfUploadOptionCenterImage($(this));
}).each(function(){
// Sometimes the load event might not trigger due to cache
if(this.complete) {
$(this).trigger('load');
};
});
// In the theme customizer, the load event above doesn't work because of the accordion,
// the image's height & width are detected as 0. We bind to the opening of an accordion
// and adjust the image placement from there.
var tfUploadAccordionSections = [];
$('.tf-upload').each(function() {
var $accordion = $(this).parents('.control-section.accordion-section');
if ( $accordion.length > 0 ) {
if ( $.inArray( $accordion, tfUploadAccordionSections ) == -1 ) {
tfUploadAccordionSections.push($accordion);
}
}
});
$.each( tfUploadAccordionSections, function() {
var $title = $(this).find('.accordion-section-title:eq(0)'); // just opening the section
$title.click(function() {
var $accordion = $(this).parents('.control-section.accordion-section');
if ( ! $accordion.is('.open') ) {
$accordion.find('.tf-upload .thumbnail img').each(function() {
var $this = $(this);
setTimeout(function() {
tfUploadOptionCenterImage($this);
}, 1);
});
}
});
});
// remove the image when the remove link is clicked
$('body').on('click', '.tf-upload i.remove', function(event) {
event.preventDefault();
var _input = $(this).parents('.tf-upload').find('input');
var _preview = $(this).parents('.tf-upload').find('div.thumbnail');
_preview.find('img').remove().end().find('i').remove();
_input.val('').trigger('change');
return false;
});
// open the upload media lightbox when the upload button is clicked
$('body').on('click', '.tf-upload .thumbnail, .tf-upload img', function(event) {
event.preventDefault();
// If we have a smaller image, users can click on the thumbnail
if ( $(this).is('.thumbnail') ) {
if ( $(this).parents('.tf-upload').find('img').length != 0 ) {
$(this).parents('.tf-upload').find('img').trigger('click');
return true;
}
}
var _input = $(this).parents('.tf-upload').find('input');
var _preview = $(this).parents('.tf-upload').find('div.thumbnail');
var _remove = $(this).siblings('.tf-upload-image-remove');
// uploader frame properties
var frame = wp.media({
title: '<?php _e( 'Select Image', TF_I18NDOMAIN ) ?>',
multiple: false,
library: { type: 'image' },
button : { text : '<?php _e( 'Use image', TF_I18NDOMAIN ) ?>' }
});
// get the url when done
frame.on('select', function() {
var selection = frame.state().get('selection');
selection.each(function(attachment) {
if ( _input.length > 0 ) {
_input.val(attachment.id);
}
if ( _preview.length > 0 ) {
// remove current preview
if ( _preview.find('img').length > 0 ) {
_preview.find('img').remove();
}
if ( _preview.find('i.remove').length > 0 ) {
_preview.find('i.remove').remove();
}
// Get the preview image
var image = attachment.attributes.sizes.full;
if ( typeof attachment.attributes.sizes.thumbnail != 'undefined' ) {
image = attachment.attributes.sizes.thumbnail;
}
var url = image.url;
var marginTop = ( _preview.height() - image.height ) / 2;
var marginLeft = ( _preview.width() - image.width ) / 2;
$("<img src='" + url + "'/>")
.css('marginTop', marginTop)
.css('marginLeft', marginLeft)
.appendTo(_preview);
$("<i class='dashicons dashicons-no-alt remove'></i>").prependTo(_preview);
}
// we need to trigger a change so that WP would detect that we changed the value
// or else the save button won't be enabled
_input.trigger('change');
_remove.show();
});
frame.off('select');
});
// open the uploader
frame.open();
return false;
});
});
</script>
<?php
}
}
/*
* We create a new control for the theme customizer
*/
add_action( 'customize_register', 'registerTitanFrameworkOptionUploadControl', 1 );
function registerTitanFrameworkOptionUploadControl() {
class TitanFrameworkOptionUploadControl extends WP_Customize_Control {
public $description;
public function render_content() {
TitanFrameworkOptionUpload::createUploaderScript();
$previewImage = '';
$value = $this->value();
if ( is_numeric( $value ) ) {
// gives us an array with the first element as the src or false on fail
$value = wp_get_attachment_image_src( $value, array( 150, 150 ) );
}
if ( ! is_array( $value ) ) {
$value = $this->value();
} else {
$value = $value[0];
}
if ( ! empty( $value ) ) {
$previewImage = "<i class='dashicons dashicons-no-alt remove'></i><img src='" . esc_url( $value ) . "' style='display: none'/>";
}
?>
<div class='tf-upload'>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<div class='thumbnail tf-image-preview'><?php echo $previewImage ?></div>
<input type='hidden' value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); ?>/>
</div>
<?php
if ( ! empty( $this->description ) ) {
echo "<p class='description'>{$this->description}</p>";
}
}
}
}