PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 2.1
Responsive Lightbox & Gallery v2.1
2.7.8 trunk 1.0.0 1.0.1 1.0.1.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.0.1 1.4.1 1.4.11 1.4.12 1.4.13 1.4.14 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3.1 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7
responsive-lightbox / includes / class-galleries.php
responsive-lightbox / includes Last commit date
class-fast-image.php 7 years ago class-folders-walker.php 7 years ago class-folders.php 7 years ago class-frontend.php 7 years ago class-galleries.php 7 years ago class-settings.php 7 years ago class-tour.php 7 years ago class-welcome.php 7 years ago class-widgets.php 7 years ago functions.php 7 years ago
class-galleries.php
3617 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Responsive Lightbox Galleries class.
8 *
9 * @class Responsive_Lightbox_Galleries
10 */
11 class Responsive_Lightbox_Galleries {
12
13 public $tabs;
14 public $fields;
15 public $media_item_template;
16 public $tab;
17 public $sizes;
18 public $gallery_args;
19 private $menu_item;
20
21 /**
22 * Constructor.
23 *
24 * @param bool $read_only Whether is it read only mode
25 * @return void
26 */
27 public function __construct( $read_only = false ) {
28 // set instance
29 Responsive_Lightbox()->galleries = $this;
30
31 if ( $read_only )
32 return;
33
34 // actions
35 add_action( 'init', array( $this, 'init' ), 11 );
36 add_action( 'admin_init', array( $this, 'init_admin' ) );
37 add_action( 'edit_form_after_title', array( $this, 'after_title_nav_menu' ) );
38 add_action( 'admin_footer', array( $this, 'modal_gallery_template' ) );
39 add_action( 'media_buttons', array( $this, 'add_gallery_button') );
40 add_action( 'add_meta_boxes_rl_gallery', array( $this, 'add_meta_boxes' ) );
41 add_action( 'save_post_rl_gallery', array( $this, 'save_post' ), 10, 3 );
42 add_action( 'manage_rl_gallery_posts_custom_column', array( $this, 'gallery_columns_content' ), 10, 2 );
43 add_action( 'admin_action_duplicate_gallery', array( $this, 'duplicate_gallery' ) );
44 add_action( 'wp_ajax_rl-get-menu-content', array( $this, 'get_menu_content' ) );
45 add_action( 'wp_ajax_rl-get-preview-content', array( $this, 'get_gallery_preview_content' ) );
46 add_action( 'wp_ajax_rl-post-get-galleries', array( $this, 'post_get_galleries' ) );
47 add_action( 'wp_ajax_rl-post-gallery-preview', array( $this, 'post_gallery_preview' ) );
48 add_action( 'wp_ajax_rl-get-gallery-page-content', array( $this, 'get_gallery_page' ) );
49 add_action( 'wp_ajax_nopriv_rl-get-gallery-page-content', array( $this, 'get_gallery_page' ) );
50 add_action( '_wp_put_post_revision', array( $this, 'save_revision' ) );
51 add_action( 'shutdown', array( $this, 'shutdown_preview' ) );
52
53 // filters
54 add_filter( 'manage_rl_gallery_posts_columns', array( $this, 'gallery_columns' ) );
55 add_filter( 'admin_post_thumbnail_html', array( $this, 'admin_post_thumbnail_html' ), 10, 2 );
56 add_filter( 'post_thumbnail_html', array( $this, 'post_thumbnail_html' ), 10, 5 );
57 add_filter( 'preview_post_link', array( $this, 'preview_post_link' ) );
58 add_filter( 'post_row_actions', array( $this, 'post_row_actions_duplicate' ), 10, 2 );
59
60 if ( ! empty( $_POST['rl_active_tab'] ) )
61 add_filter( 'redirect_post_location', array( $this, 'add_active_tab' ) );
62 }
63
64 /**
65 * Load needed data.
66 *
67 * @return void
68 */
69 public function init() {
70 if ( ! is_admin() )
71 add_filter( 'get_post_metadata', array( $this, 'filter_preview_metadata' ), 10, 4 );
72
73 // register shortcode
74 add_shortcode( 'rl_gallery', array( $this, 'gallery_shortcode' ) );
75
76 // single image template
77 $this->media_item_template = '<li class="rl-gallery-image __IMAGE_STATUS__" data-attachment_id="__IMAGE_ID__"><div class="rl-gallery-inner">__IMAGE__</div><div class="rl-gallery-actions"><a href="#" class="rl-gallery-image-status dashicons dashicons-marker" title="' . __( 'Status', 'responsive-lightbox' ) . '"></a><a href="#" class="rl-gallery-image-edit dashicons dashicons-edit" title="' . __( 'Edit image', 'responsive-lightbox' ) . '"></a><a href="#" class="rl-gallery-image-remove dashicons dashicons-no" title="' . __( 'Remove image', 'responsive-lightbox' ) . '"></a></div></li>';
78
79 // get main instance
80 $rl = Responsive_Lightbox();
81
82 $config_menu_items = apply_filters( 'rl_gallery_types', $rl->gallery_types );
83 $config_menu_items['default'] = __( 'Global', 'responsive-lightbox' );
84
85 $this->tabs = apply_filters(
86 'rl_gallery_tabs',
87 array(
88 'images' => array(
89 'label' => __( 'Images', 'responsive-lightbox' ),
90 'description' => __( 'The settings below adjust the contents of the gallery.', 'responsive-lightbox' ),
91 'menu_items' => array(
92 'media' => __( 'Media Library', 'responsive-lightbox' ),
93 'featured' => __( 'Featured Content', 'responsive-lightbox' )
94 )
95 ),
96 'config' => array(
97 'label' => __( 'Config', 'responsive-lightbox' ),
98 'description' => __( 'The settings below adjust the configuration options for the gallery.', 'responsive-lightbox' ),
99 'menu_items' => $config_menu_items
100 ),
101 'design' => array(
102 'label' => __( 'Design', 'responsive-lightbox' ),
103 'description' => __( 'The settings below adjust the gallery design options.', 'responsive-lightbox' )
104 ),
105 'paging' => array(
106 'label' => __( 'Paging', 'responsive-lightbox' ),
107 'description' => __( 'The settings below adjust the gallery pagination options.', 'responsive-lightbox' )
108 ),
109 'lightbox' => array(
110 'label' => __( 'Lightbox', 'responsive-lightbox' ),
111 'description' => __( 'The settings below adjust the lightbox options.', 'responsive-lightbox' ),
112 ),
113 'misc' => array(
114 'label' => __( 'Misc', 'responsive-lightbox' ),
115 'description' => __( 'The settings below adjust miscellaneous options.', 'responsive-lightbox' ),
116 )
117 )
118 );
119
120 // add folders if active
121 if ( $rl->options['folders']['active'] )
122 $this->tabs['images']['menu_items']['folders'] = __( 'Media Folders', 'responsive-lightbox' );
123
124 // use sizes as keys and values
125 $this->sizes = $this->get_image_sizes();
126 $sizes = array_combine( array_keys( $this->sizes ), array_keys( $this->sizes ) );
127
128 // add default, custom and full image size
129 $sizes['full'] = __( 'Full size', 'responsive-lightbox' );
130 $sizes['global'] = __( 'Global', 'responsive-lightbox' );
131 $sizes['rl_custom_size'] = __( 'Custom size', 'responsive-lightbox' );
132
133 $positions = array(
134 'none' => __( 'None', 'responsive-lightbox' ),
135 'top' => __( 'Top', 'responsive-lightbox' ),
136 'bottom' => __( 'Bottom', 'responsive-lightbox' )
137 );
138
139 $this->fields = apply_filters( 'rl_gallery_tab_fields', array(
140 'images' => array(
141 'media' => array(
142 'attachments' => array(
143 'title' => '',
144 'type' => 'media_library',
145 'default' => array(
146 'ids' => array(),
147 'exclude' => array()
148 )
149 )
150 ),
151 'featured' => array(
152 'attachments' => array(
153 'title' => '',
154 'type' => 'media_preview',
155 'default' => array(
156 'exclude' => array()
157 )
158 ),
159 'number_of_posts' => array(
160 'title' => __( 'Number of Posts', 'responsive-lightbox' ),
161 'type' => 'number',
162 'description' => __( 'Enter the number of posts.', 'responsive-lightbox' ),
163 'default' => 10,
164 'min' => 0
165 ),
166 'orderby' => array(
167 'title' => __( 'Posts Sorting', 'responsive-lightbox' ),
168 'type' => 'select',
169 'description' => __( 'Select the posts sorting.', 'responsive-lightbox' ),
170 'default' => 'date',
171 'options' => array(
172 'id' => __( 'ID', 'responsive-lightbox' ),
173 'author' => __( 'Author', 'responsive-lightbox' ),
174 'title' => __( 'Title', 'responsive-lightbox' ),
175 'name' => __( 'Slug', 'responsive-lightbox' ),
176 'date' => __( 'Date', 'responsive-lightbox' ),
177 'modified' => __( 'Last modified date', 'responsive-lightbox' ),
178 'parent' => __( 'Parent ID', 'responsive-lightbox' ),
179 'rand' => __( 'Random', 'responsive-lightbox' )
180 )
181 ),
182 'order' => array(
183 'title' => __( 'Posts Order', 'responsive-lightbox' ),
184 'type' => 'radio',
185 'description' => __( 'Select the posts order.', 'responsive-lightbox' ),
186 'default' => 'asc',
187 'options' => array(
188 'asc' => __( 'Ascending', 'responsive-lightbox' ),
189 'desc' => __( 'Descending', 'responsive-lightbox' )
190 )
191 ),
192 'offset' => array(
193 'title' => __( 'Posts Offset', 'responsive-lightbox' ),
194 'type' => 'number',
195 'description' => __( 'Enter the posts offset.', 'responsive-lightbox' ),
196 'default' => 0,
197 'min' => 0
198 ),
199 'image_source' => array(
200 'title' => __( 'Image Source', 'responsive-lightbox' ),
201 'type' => 'radio',
202 'description' => __( 'Select the image source.', 'responsive-lightbox' ),
203 'default' => 'thumbnails',
204 'options' => array(
205 'thumbnails' => __( 'Post Thumbnails', 'responsive-lightbox' ),
206 'attached_images' => __( 'Post Attached Images', 'responsive-lightbox' )
207 )
208 ),
209 'images_per_post' => array(
210 'title' => __( 'Images per Post', 'responsive-lightbox' ),
211 'type' => 'number',
212 'description' => __( 'Enter maximum number of images for a post.', 'responsive-lightbox' ),
213 'default' => 1,
214 'min' => 1
215 ),
216 'post_type' => array(
217 'title' => __( 'Post Type', 'responsive-lightbox' ),
218 'type' => 'multiselect',
219 'description' => __( 'Select the post types to query.', 'responsive-lightbox' ),
220 'options' => array(),
221 'default' => array()
222 ),
223 'post_status' => array(
224 'title' => __( 'Post Status', 'responsive-lightbox' ),
225 'type' => 'multiselect',
226 'description' => __( 'Select the post status.', 'responsive-lightbox' ),
227 'options' => array(),
228 'default' => array()
229 ),
230 'post_format' => array(
231 'title' => __( 'Post Format', 'responsive-lightbox' ),
232 'type' => 'multiselect',
233 'description' => __( 'Select the post format.', 'responsive-lightbox' ),
234 'options' => array(),
235 'default' => array()
236 ),
237 'post_term' => array(
238 'title' => __( 'Post Term', 'responsive-lightbox' ),
239 'type' => 'multiselect',
240 'description' => __( 'Select the post taxonomy terms to query.', 'responsive-lightbox' ),
241 'options' => array(),
242 'default' => array()
243 ),
244 'post_author' => array(
245 'title' => __( 'Post Author', 'responsive-lightbox' ),
246 'type' => 'multiselect',
247 'description' => __( 'Select the post author.', 'responsive-lightbox' ),
248 'options' => array(),
249 'default' => array()
250 ),
251 'page_parent' => array(
252 'title' => __( 'Page Parent', 'responsive-lightbox' ),
253 'type' => 'multiselect',
254 'description' => __( 'Select the post parent.', 'responsive-lightbox' ),
255 'options' => array(),
256 'default' => array()
257 ),
258 'page_template' => array(
259 'title' => __( 'Page Template', 'responsive-lightbox' ),
260 'type' => 'multiselect',
261 'description' => __( 'Select the page template.', 'responsive-lightbox' ),
262 'options' => array(),
263 'default' => array()
264 )
265 ),
266 'folders' => array(
267 'attachments' => array(
268 'title' => '',
269 'type' => 'folder_preview',
270 'default' => array(
271 'exclude' => array()
272 )
273 ),
274 'folder' => array(
275 'title' => __( 'Media Folder', 'responsive-lightbox' ),
276 'type' => 'taxonomy',
277 'description' => __( 'Select media folder.', 'responsive-lightbox' ),
278 'default' => array(
279 'id' => 0,
280 'children' => false
281 ),
282 'include_children' => true,
283 'taxonomy' => $rl->options['folders']['media_taxonomy']
284 )
285 )
286 ),
287 'config' => array(),
288 'design' => array(
289 'options' => array(
290 'show_title' => array(
291 'title' => __( 'Thumbnail title', 'responsive-lightbox' ),
292 'type' => 'select',
293 'description' => __( 'Select title for the gallery thumbnails.', 'responsive-lightbox' ),
294 'default' => 'global',
295 'options' => array_merge( array( 'global' => __( 'Global', 'responsive-lightbox' ) ), $rl->settings->image_titles )
296 ),
297 'show_caption' => array(
298 'title' => __( 'Thumbnail caption', 'responsive-lightbox' ),
299 'type' => 'select',
300 'description' => __( 'Select caption for the gallery thumbnails.', 'responsive-lightbox' ),
301 'default' => 'global',
302 'options' => array_merge( array( 'global' => __( 'Global', 'responsive-lightbox' ) ), $rl->settings->image_titles )
303 ),
304 'show_icon' => array(
305 'title' => __( 'Thumbnail icon', 'responsive-lightbox' ),
306 'type' => 'radio',
307 'description' => __( 'Select icon for the gallery thumbnails.', 'responsive-lightbox' ),
308 'default' => '0',
309 'options' => $rl->settings->image_icons
310 ),
311 'hover_effect' => array(
312 'title' => __( 'Hover effect', 'responsive-lightbox' ),
313 'type' => 'select',
314 'description' => __( 'Select thumbnail effect on hover.', 'responsive-lightbox' ),
315 'default' => '0',
316 'options' => array(
317 '0' => __( 'none', 'responsive-lightbox' ),
318 '1' => sprintf( __( 'Effect %s', 'responsive-lightbox' ), 1 ),
319 '2' => sprintf( __( 'Effect %s', 'responsive-lightbox' ), 2 ),
320 '3' => sprintf( __( 'Effect %s', 'responsive-lightbox' ), 3 ),
321 '4' => sprintf( __( 'Effect %s', 'responsive-lightbox' ), 4 ),
322 '5' => sprintf( __( 'Effect %s', 'responsive-lightbox' ), 5 ),
323 '6' => sprintf( __( 'Effect %s', 'responsive-lightbox' ), 6 ),
324 '7' => sprintf( __( 'Effect %s', 'responsive-lightbox' ), 7 ),
325 '8' => sprintf( __( 'Effect %s', 'responsive-lightbox' ), 8 ),
326 '9' => sprintf( __( 'Effect %s', 'responsive-lightbox' ), 9 )
327 )
328 ),
329 'title_color' => array(
330 'title' => __( 'Title Color', 'responsive-lightbox' ),
331 'type' => 'color_picker',
332 'default' => '#ffffff'
333 ),
334 'caption_color' => array(
335 'title' => __( 'Caption Color', 'responsive-lightbox' ),
336 'type' => 'color_picker',
337 'default' => '#cccccc'
338 ),
339 'background_color' => array(
340 'title' => __( 'Background Color', 'responsive-lightbox' ),
341 'type' => 'color_picker',
342 'default' => '#000000'
343 ),
344 'background_opacity' => array(
345 'title' => __( 'Background Opacity', 'responsive-lightbox' ),
346 'type' => 'number',
347 'default' => 80,
348 'step' => 1,
349 'min' => 0,
350 'max' => 100,
351 'append' => '%'
352 ),
353 'border_color' => array(
354 'title' => __( 'Border Color', 'responsive-lightbox' ),
355 'type' => 'color_picker',
356 'default' => '#000000'
357 ),
358 'border_width' => array(
359 'title' => __( 'Border Width', 'responsive-lightbox' ),
360 'type' => 'number',
361 'default' => 0,
362 'step' => 1,
363 'min' => 0,
364 'max' => 100,
365 'append' => 'px'
366 )
367 )
368 ),
369 'paging' => array(
370 'options' => array(
371 'pagination' => array(
372 'title' => __( 'Use pagination', 'responsive-lightbox' ),
373 'type' => 'boolean',
374 'label' => __( 'Enable pagination.', 'responsive-lightbox' ),
375 'default' => false
376 ),
377 'pagination_type' => array(
378 'title' => __( 'Pagination type', 'responsive-lightbox' ),
379 'type' => 'select',
380 'description' => __( 'Select pagination type.', 'responsive-lightbox' ),
381 'default' => 'paged',
382 'options' => array(
383 'paged' => __( 'standard', 'responsive-lightbox' ),
384 'ajax' => __( 'AJAX', 'responsive-lightbox' ),
385 'infinite' => __( 'infinite scroll', 'responsive-lightbox' )
386 )
387 ),
388 'pagination_position' => array(
389 'title' => __( 'Pagination position', 'responsive-lightbox' ),
390 'type' => 'select',
391 'description' => __( 'Select pagination position.', 'responsive-lightbox' ),
392 'default' => 'bottom',
393 'options' => array(
394 'bottom' => __( 'bottom', 'responsive-lightbox' ),
395 'top' => __( 'top', 'responsive-lightbox' ),
396 'both' => __( 'top & bottom', 'responsive-lightbox' )
397 )
398 ),
399 'images_per_page' => array(
400 'title' => __( 'Images per page', 'responsive-lightbox' ),
401 'type' => 'number',
402 'description' => __( 'Number of images per page.', 'responsive-lightbox' ),
403 'default' => get_option( 'posts_per_page', 20 ),
404 'step' => 1,
405 'min' => 0
406 ),
407 'load_more' => array(
408 'title' => __( 'Load More', 'responsive-lightbox' ),
409 'type' => 'radio',
410 'description' => __( 'Select the load more trigger (infinite scroll only).', 'responsive-lightbox' ),
411 'default' => 'automatically',
412 'options' => array(
413 'automatically' => __( 'Automatically', 'responsive-lightbox' ),
414 'manually' => __( 'On click', 'responsive-lightbox' )
415 )
416 )
417 )
418 ),
419 'lightbox' => array(
420 'options' => array(
421 'lightbox_enable' => array(
422 'title' => __( 'Enable Lightbox', 'responsive-lightbox' ),
423 'type' => 'boolean',
424 'label' => __( 'Enable lightbox effect for the gallery.', 'responsive-lightbox' ),
425 'default' => true
426 ),
427 'lightbox_image_size' => array(
428 'title' => __( 'Image Size', 'responsive-lightbox' ),
429 'type' => 'select',
430 'description' => __( 'Select image size for gallery lightbox.', 'responsive-lightbox' ),
431 'default' => 'global',
432 'options' => $sizes
433 ),
434 'lightbox_custom_size' => array(
435 'title' => __( 'Custom size', 'responsive-lightbox' ),
436 'type' => 'multiple',
437 'description' => __( 'Choose the custom image size for gallery lightbox (used if Custom Image size is selected).', 'responsive-lightbox' ),
438 'fields' => array(
439 'lightbox_custom_size_width' => array(
440 'type' => 'number',
441 'append' => __( 'width in px', 'responsive-lightbox' ),
442 'default' => (int) get_option( 'large_size_w' )
443 ),
444 'lightbox_custom_size_height' => array(
445 'type' => 'number',
446 'append' => __( 'height in px', 'responsive-lightbox' ),
447 'default' => (int) get_option( 'large_size_h' )
448 )
449 )
450 ),
451 'lightbox_image_title' => array(
452 'title' => __( 'Image Title', 'responsive-lightbox' ),
453 'type' => 'select',
454 'description' => __( 'Select image title for gallery lightbox.', 'responsive-lightbox' ),
455 'default' => 'global',
456 'options' => array( 'global' => __( 'Global', 'responsive-lightbox' ) ) + $rl->settings->image_titles
457 ),
458 'lightbox_image_caption' => array(
459 'title' => __( 'Image Caption', 'responsive-lightbox' ),
460 'type' => 'select',
461 'description' => __( 'Select image caption for gallery lightbox (used if supported by selected lightbox effect).', 'responsive-lightbox' ),
462 'default' => 'global',
463 'options' => array( 'global' => __( 'Global', 'responsive-lightbox' ) ) + $rl->settings->image_titles
464 )
465 )
466 ),
467 'misc' => array(
468 'options' => array(
469 'gallery_title_position' => array(
470 'title' => __( 'Title Position', 'responsive-lightbox' ),
471 'type' => 'select',
472 'description' => __( 'Select where to display the title.', 'responsive-lightbox' ),
473 'default' => 'none',
474 'options' => $positions
475 ),
476 'gallery_description_position' => array(
477 'title' => __( 'Description Position', 'responsive-lightbox' ),
478 'type' => 'select',
479 'description' => __( 'Select where to display the description.', 'responsive-lightbox' ),
480 'default' => 'none',
481 'options' => $positions
482 ),
483 'gallery_description' => array(
484 'title' => __( 'Gallery Description', 'responsive-lightbox' ),
485 'type' => 'textarea',
486 'description' => __( 'Enter the gallery description (optional).', 'responsive-lightbox' ),
487 'default' => '',
488 'class' => 'large-text'
489 ),
490 'gallery_custom_class' => array(
491 'title' => __( 'Custom Classes', 'responsive-lightbox' ),
492 'type' => 'class',
493 'description' => __( 'Add custom, space saparated CSS classes (optional).', 'responsive-lightbox' ),
494 'default' => '',
495 'class' => 'large-text'
496 )
497 )
498 )
499 ) );
500 }
501
502 /**
503 * Duplicate gallery action in admin.
504 *
505 * @return void
506 */
507 public function duplicate_gallery() {
508 if ( ! ( isset( $_GET['post'] ) || isset( $_POST['post'] ) ) || ! isset( $_REQUEST['action'] ) || ! isset( $_REQUEST['rl_gallery_nonce'] ) || ( isset( $_REQUEST['rl_gallery_nonce'] ) && ! wp_verify_nonce( esc_attr( $_REQUEST['rl_gallery_nonce'] ), 'responsive-lightbox-duplicate-gallery' ) ) )
509 wp_die( __( 'No gallery to duplicate has been supplied!', 'responsive-lightbox' ) );
510
511 // get the original post
512 $post_id = ( isset( $_GET['post'] ) ? (int) $_GET['post'] : (int) $_POST['post'] );
513
514 if ( empty( $post_id ) )
515 wp_die( __( 'No gallery to duplicate has been supplied!', 'responsive-lightbox' ) );
516
517 if ( ! current_user_can( 'edit_post', $post_id ) )
518 wp_die( __( 'You do not have permission to copy this gallery.', 'responsive-lightbox' ) );
519
520 $post = get_post( $post_id );
521
522 // copy the post and insert it
523 if ( isset( $post ) && $post != null ) {
524 $new_id = $this->create_gallery_duplicate( $post );
525
526 // redirect to the post list screen
527 wp_redirect( admin_url( 'edit.php?post_type=' . $post->post_type ) );
528 exit;
529 } else
530 wp_die( esc_attr( __( 'Copy creation failed, could not find original gallery:', 'responsive-lightbox' ) ) . ' ' . htmlspecialchars( $post_id ) );
531 }
532
533 /**
534 * Add duplicate link to gallery listing.
535 *
536 * @param array $actions
537 * @param object $post
538 * @return array
539 */
540 public function post_row_actions_duplicate( $actions, $post ) {
541 global $pagenow;
542
543 if ( $post->post_type !== 'rl_gallery' )
544 return $actions;
545
546 if ( ! current_user_can( 'edit_post', $post->ID ) )
547 return $actions;
548
549 // duplicate link
550 $actions['duplicate_gallery'] = '<a class="duplicate-gallery" title="' . esc_attr__( 'Duplicate this item', 'responsive-lightbox' ) . '" href="' . wp_nonce_url( admin_url( $pagenow . '?post=' . $post->ID . '&action=duplicate_gallery' ), 'responsive-lightbox-duplicate-gallery', 'rl_gallery_nonce' ) . '">' . __( 'Duplicate', 'responsive-lightbox' ) . '</a>';
551
552 return $actions;
553 }
554
555 /**
556 * Create a gallery duplicate.
557 *
558 * @param $post object
559 * @return int
560 */
561 public function create_gallery_duplicate( $post ) {
562 // skip revisions
563 if ( $post->post_type == 'revision' )
564 return;
565
566 $new_post = apply_filters(
567 'rl_duplicate_gallery_args',
568 array(
569 'menu_order' => $post->menu_order,
570 'comment_status' => $post->comment_status,
571 'ping_status' => $post->ping_status,
572 'post_author' => $post->post_author,
573 'post_content' => $post->post_content,
574 'post_excerpt' => $post->post_excerpt,
575 'post_mime_type' => $post->post_mime_type,
576 'post_parent' => $post->post_parent,
577 'post_password' => $post->post_password,
578 'post_status' => $post->post_status,
579 'post_title' => $post->post_title,
580 'post_type' => $post->post_type,
581 'post_date' => current_time( 'mysql' ),
582 'post_date_gmt' => get_gmt_from_date( current_time( 'mysql' ) )
583 ),
584 $post
585 );
586
587 $new_post_id = wp_insert_post( $new_post );
588
589 // if the copy is published or scheduled, we have to set a proper slug
590 if ( $new_post['status'] == 'publish' || $new_post['status'] == 'future' ) {
591 $post_name = wp_unique_post_slug( $post->post_name, $new_post_id, $new_post['status'], $post->post_type, $new_post['post_parent'] );
592
593 $new_post = array();
594 $new_post['ID'] = $new_post_id;
595 $new_post['post_name'] = $post_name;
596
597 // update the post into the database
598 wp_update_post( $new_post );
599 }
600
601 // create metadata for the duplicated gallery
602 $this->create_gallery_duplicate_metadata( $new_post_id, $post );
603
604 // copy taxonomies
605 $this->duplicate_gallery_taxonomies( $new_post_id, $post );
606
607 // action hook for developers
608 do_action( 'rl_after_duplicate_gallery', $new_post_id, $post );
609
610 return $new_post_id;
611 }
612
613 /**
614 * Create a gallery duplicate metadata.
615 *
616 * @param int $new_post_id
617 * @param object $post
618 * @return void
619 */
620 public function create_gallery_duplicate_metadata( $new_post_id, $post ) {
621 if ( empty( $post ) || $post == null )
622 return;
623
624 // meta keys to be copied
625 $meta_keys = apply_filters( 'rl_duplicate_gallery_meta_keys', get_post_custom_keys( $post->ID ) );
626
627 if ( empty( $meta_keys ) )
628 return;
629
630 foreach ( $meta_keys as $meta_key ) {
631 // meta values to be copied
632 $meta_values = apply_filters( 'rl_duplicate_gallery_meta_values', get_post_custom_values( $meta_key, $post->ID ) );
633
634 foreach ( $meta_values as $meta_value ) {
635 $meta_value = maybe_unserialize( $meta_value );
636
637 // add metadata to duplicated post
638 add_post_meta( $new_post_id, $meta_key, $meta_value );
639 }
640 }
641 }
642
643 /**
644 * Copy the taxonomies of a gallery to another gallery.
645 *
646 * @param int $new_post_id
647 * @param object $post
648 * @return void
649 */
650 function duplicate_gallery_taxonomies( $new_post_id, $post ) {
651 global $wpdb;
652
653 if ( isset( $wpdb->terms ) ) {
654 // clear default category
655 wp_set_object_terms( $new_post_id, null, 'category' );
656
657 // get gallery taxonomies
658 $gallery_taxonomies = get_object_taxonomies( $post->post_type );
659
660 if ( ! empty( $gallery_taxonomies ) ) {
661 foreach ( $gallery_taxonomies as $taxonomy ) {
662 $terms = array();
663
664 // get taxonomy terms
665 $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'orderby' => 'term_order' ) );
666
667 if ( ! empty( $post_terms ) ) {
668 foreach ( $post_terms as $term ) {
669 $terms[] = $term->slug;
670 }
671 }
672
673 // copy taxonomy terms
674 wp_set_object_terms( $new_post_id, $terms, $taxonomy );
675 }
676 }
677 }
678 }
679
680 /**
681 * Add a gallery shortcode.
682 *
683 * @param array $args Shortcode arguments
684 * @return function Generated gallery shortcode
685 */
686 public function gallery_shortcode( $args ) {
687 // prepare defaults
688 $defaults = array(
689 'id' => 0
690 );
691
692 // merge defaults with arguments
693 $args = array_merge( $defaults, $args );
694
695 // parse ID
696 $args['id'] = (int) $args['id'];
697
698 // is it gallery?
699 if ( get_post_type( $args['id'] ) !== 'rl_gallery' )
700 return '';
701
702 // get images
703 $images = $this->get_gallery_images( $args['id'], array( 'exclude' => true ) );
704
705 if ( ! $images )
706 return '';
707
708 $attachments = array();
709
710 // build config
711 foreach ( $images as $image ) {
712 if ( ! empty( $image['id'] ) )
713 $attachments[] = $image['id'];
714 }
715
716 // get config data
717 $config = get_post_meta( $args['id'], '_rl_config', true );
718
719 // prepare gallery shortcode parameters
720 $fields = array();
721
722 // get main instance
723 $rl = Responsive_Lightbox();
724
725 if ( ! empty( $config['menu_item'] ) ) {
726 // assign data from db
727 $data = $config[$config['menu_item']];
728
729 foreach ( $rl->frontend->get_default_gallery_fields() as $field_name => $field_args ) {
730 // replace default values
731 if ( array_key_exists( $field_name, $data ) )
732 $fields[$field_name] = $data[$field_name];
733 }
734
735 // is it default gallery type?
736 if ( $config['menu_item'] === 'default' ) {
737 // set new gallery type
738 $gallery_type = $rl->options['settings']['builder_gallery'];
739
740 // assign gallery settings and defaults
741 $gallery_fields = $rl->settings->settings[$gallery_type . '_gallery']['fields'];
742 $gallery_defaults = $rl->options[$gallery_type . '_gallery'];
743 } else {
744 $gallery_type = $config['menu_item'];
745
746 // assign gallery settings and defaults
747 $gallery_fields = $rl->settings->settings[$config['menu_item'] . '_gallery']['fields'];
748 $gallery_defaults = $rl->defaults[$config['menu_item'] . '_gallery'];
749 }
750
751 if ( isset( $gallery_fields, $gallery_defaults ) ) {
752 // run through all fields
753 foreach ( $gallery_fields as $field_name => $field_args ) {
754 if ( $field_args['type'] === 'multiple' ) {
755 foreach ( $field_args['fields'] as $subfield_name => $subfield_args ) {
756 // field exists in db?
757 if ( array_key_exists( $subfield_name, $data ) )
758 $fields[$subfield_name] = $data[$subfield_name];
759 else
760 $fields[$subfield_name] = $gallery_defaults[$subfield_name];
761 }
762 } else {
763 // field exists in db?
764 if ( array_key_exists( $field_name, $data ) )
765 $fields[$field_name] = $data[$field_name];
766 else
767 $fields[$field_name] = $gallery_defaults[$field_name];
768 }
769 }
770 }
771
772 // add gallery type
773 $fields['type'] = $gallery_type;
774 }
775
776 $shortcode = '';
777
778 foreach ( $fields as $arg => $value ) {
779 if ( is_array( $value ) )
780 $shortcode .= ' ' . $arg . '="' . (string) implode( ',', $value ) . '"';
781 else
782 $shortcode .= ' ' . $arg . '="' . (string) $value . '"';
783 }
784
785 // get design data
786 $design = get_post_meta( $args['id'], '_rl_design', true );
787
788 if ( ! empty( $design['menu_item'] ) ) {
789 foreach ( $design[$design['menu_item']] as $arg => $value ) {
790 $shortcode .= ' ' . $arg . '="' . (string) $value . '"';
791 }
792 }
793
794 // get lightbox data
795 $lightbox = get_post_meta( $args['id'], '_rl_lightbox', true );
796
797 if ( ! empty( $lightbox['menu_item'] ) ) {
798 foreach ( $lightbox[$lightbox['menu_item']] as $arg => $value ) {
799 $shortcode .= ' ' . $arg . '="' . (string) $value . '"';
800 }
801 }
802
803 return do_shortcode( '[gallery rl_gallery_id="' . $args['id'] .'" include="' . ( empty( $attachments ) ? '' : implode( ',', $attachments ) ) . '"' . $shortcode . ']' );
804 }
805
806 /**
807 * Add a gallery button.
808 *
809 * @param string $editor_id Editor ID
810 * @return void
811 */
812 public function add_gallery_button( $editor_id ) {
813 if ( get_post_type() === 'rl_gallery' )
814 return;
815
816 // assign singleton instance
817 $rl = Responsive_Lightbox();
818
819 wp_enqueue_script( 'responsive-lightbox-admin-post', RESPONSIVE_LIGHTBOX_URL . '/js/admin-post.js', array( 'jquery' ), $rl->defaults['version'], false );
820
821 wp_localize_script(
822 'responsive-lightbox-admin-post',
823 'rlArgs',
824 array(
825 'nonce' => wp_create_nonce( 'rl-gallery-post' ),
826 'post_id' => get_the_ID()
827 )
828 );
829
830 wp_enqueue_style( 'responsive-lightbox-admin-post', RESPONSIVE_LIGHTBOX_URL . '/css/admin-post.css', array(), $rl->defaults['version'] );
831
832 echo '<button type="button" id="rl-insert-modal-gallery-button" class="button" data-editor="' . $editor_id . '"><span class="wp-media-buttons-icon dashicons dashicons-format-gallery"></span> ' . __( 'Add Gallery', 'responsive-lightbox' ) . '</button>';
833 }
834
835 /**
836 * Modal gallery HTML template.
837 *
838 * @return mixed
839 */
840 public function modal_gallery_template() {
841 global $pagenow;
842
843 // display only for post edit pages
844 if ( ! ( ( $pagenow === 'post.php' || $pagenow === 'post-new.php' ) && get_post_type() !== 'rl_gallery' ) )
845 return;
846
847 $rl = Responsive_Lightbox();
848
849 if ( $rl->options['builder']['categories'] ) {
850 $categories = wp_dropdown_categories(
851 array(
852 'orderby' => 'name',
853 'order' => 'asc',
854 'show_option_none' => '',
855 'show_option_all' => __( 'All categories', 'responsive-lightbox' ),
856 'show_count' => false,
857 'hide_empty' => false,
858 'option_none_value' => '',
859 'hierarchical' => true,
860 'selected' => 0,
861 'taxonomy' => 'rl_category',
862 'hide_if_empty' => false,
863 'echo' => false,
864 'id' => 'rl-media-attachment-categories',
865 'class' => 'attachment-filters',
866 'name' => ''
867 )
868 );
869 }
870
871 echo '
872 <div id="rl-modal-gallery" style="display: none;">
873 <div class="media-modal wp-core-ui">
874 <button type="button" class="media-modal-close"><span class="media-modal-icon"><span class="screen-reader-text">' . __( 'Close', 'responsive-lightbox' ) . '</span></span></button>
875 <div class="media-modal-content">
876 <div class="media-frame mode-select wp-core-ui hide-menu hide-router">
877 <div class="media-frame-title">
878 <h1 class="wrap">' . __( 'Insert Gallery', 'responsive-lightbox' ) . ' <a class="rl-reload-galleries page-title-action" href="#">' . __( 'Reload', 'responsive-lightbox' ). '</a><span class="rl-gallery-reload-spinner spinner"></span></h1>
879 </div>
880 <div class="media-frame-content" data-columns="0">
881 <div class="attachments-browser">
882 <div class="media-toolbar">' . ( $rl->options['builder']['categories'] ? '
883 <div class="media-toolbar-secondary"><label for="rl-media-attachment-categories" class="screen-reader-text">' . esc_html__( 'Filter by category', 'responsive-lightbox' ) . '</label>' . $categories . '</div>' : '' ) . '
884 <div class="media-toolbar-primary search-form">
885 <label for="rl-media-search-input" class="screen-reader-text">' . __( 'Search galleries', 'responsive-lightbox' ) . '</label><input type="search" placeholder="' . esc_attr__( 'Search galleries', 'responsive-lightbox' ) . '" id="rl-media-search-input" class="search">
886 </div>
887 </div>
888 <ul class="attachments rl-galleries-list ui-sortable ui-sortable-disabled">
889 </ul>
890 <div class="media-sidebar visible">
891 <h2>' . __( 'Select A Gallery', 'responsive-lightbox' ) . '</h2>
892 <p>' . __( 'To select a gallery simply click on one of the boxes to the left.', 'responsive-lightbox' ) . '</p>
893 <p>' . __( 'To insert your gallery into the editor, click on the "Insert Gallery" button below.', 'responsive-lightbox' ) . '</p>
894 </div>
895 </div>
896 </div>
897 <div class="media-frame-toolbar">
898 <div class="media-toolbar">
899 <div class="media-toolbar-secondary">
900 <div class="media-selection empty">
901 <div class="selection-info">
902 <span class="rl-gallery-count count">' . sprintf( _n( '%s image', '%s images', 0, 'responsive-lightbox' ), 0 ) . '</span>
903 <a href="" class="button-link rl-edit-gallery-link">' . __( 'Edit gallery', 'responsive-lightbox' ) . '</a>
904 </div>
905 <div class="selection-view">
906 <span class="rl-gallery-images-spinner spinner" style="display: none;"></span>
907 <ul class="attachments rl-attachments-list">
908 </ul>
909 </div>
910 </div>
911 </div>
912 <div class="media-toolbar-primary search-form">
913 <button type="button" class="button media-button button-primary button-large rl-media-button-insert-gallery" disabled="disabled">' . __( 'Insert into post', 'responsive-lightbox') . '</button>
914 <button type="button" class="button media-button button-secondary button-large rl-media-button-cancel-gallery">' . __( 'Cancel', 'responsive-lightbox') . '</button>
915 </div>
916 </div>
917 </div>
918 </div>
919 </div>
920 </div>
921 <div class="media-modal-backdrop"></div>
922 </div>';
923 }
924
925 /**
926 * Render gallery field.
927 *
928 * @param string $field Field name
929 * @param string $tab_id Field tab
930 * @param string $menu_item Field parent
931 * @param array $args Field arguments
932 * @param bool $subfield Is this a subfield
933 * @return string Rendered HTML field
934 */
935 public function render_field( $field, $tab_id, $menu_item, $args, $gallery_id, $subfield = false ) {
936 if ( $subfield ) {
937 $template = '%s%s';
938 $html = '';
939 $subhtml = '';
940 } else {
941 $template = $args['type'] === 'section' ? '<th colspan="2"><h3>%s</h3></th>' : '<th><label for="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '">%s</label></th><td>%s</td>';
942 $html = '<tr class="rl-gallery-field-' . $tab_id . '-' . $menu_item . '-' . $field . ' rl-gallery-field-' . $args['type'] . '" data-field_type="' . $args['type'] . '" data-field_name="' . $field . '">';
943 $subhtml = '';
944 }
945
946 switch ( $args['type'] ) {
947 case 'range':
948 $html .= sprintf(
949 $template,
950 ! empty( $args['title'] ) ? esc_html( $args['title'] ) : '',
951 '<input id="rl_' . $tab_id . '_' . $menu_item . '_' . $field . '" type="range" value="' . $args['value'] . '" name="rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . ']" min="' . ( ! empty( $args['min'] ) ? $args['min'] : 0 ) . '"' . ( ! empty( $args['max'] ) ? ' max="' . $args['max'] . '"' : '' ) . ' step="' . ( ! empty( $args['step'] ) ? $args['step'] : 1 ) . '" oninput="this.form.rl_' . $tab_id . '_' . $menu_item . '_' . $field . '_range.value=this.value" /><output class="rl-gallery-field-output" name="rl_' . $tab_id . '_' . $menu_item . '_' . $field . '_range">' . $args['value'] . '</output>' . ( ! empty( $args['append'] ) ? ' <span>' . esc_html( $args['append'] ) . '</span>' : '' ) . ( ! empty ( $args['description'] ) ? '<p class="description">' . $args['description'] . '</p>' : '' )
952 );
953 break;
954
955 case 'radio':
956 $subhtml = '';
957
958 foreach ( $args['options'] as $key => $label ) {
959 $subhtml .= '<label class="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '" for="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '-' . $key . '"><input id="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '-' . $key . '" type="radio" name="rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . ']" value="' . $key . '" ' . checked( $key, $args['value'], false ) . ' />' . esc_html( $label ) . '</label> ';
960 }
961
962 $html .= sprintf(
963 $template,
964 ! empty( $args['title'] ) ? esc_html( $args['title'] ) : '',
965 $subhtml . ( ! empty ( $args['description'] ) ? '<p class="description">' . $args['description'] . '</p>' : '' )
966 );
967 break;
968
969 case 'number':
970 $html .= sprintf(
971 $template,
972 ! empty( $args['title'] ) ? esc_html( $args['title'] ) : '',
973 '<input id="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '" class="small-text" type="number" value="' . $args['value'] . '" name="rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . ']" min="' . ( ! empty( $args['min'] ) ? $args['min'] : 0 ) . '"' . ( ! empty( $args['max'] ) ? ' max="' . $args['max'] . '"' : '' ) . ' step="' . ( ! empty( $args['step'] ) ? $args['step'] : 1 ) . '" />' . ( ! empty( $args['append'] ) ? ' <span>' . esc_html( $args['append'] ) . '</span>' : '' ) . ( ! empty ( $args['description'] ) ? '<p class="description">' . $args['description'] . '</p>' : '' )
974 );
975 break;
976
977 case 'text':
978 $html .= sprintf(
979 $template,
980 ! empty( $args['title'] ) ? esc_html( $args['title'] ) : '',
981 '<input id="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '"' . ( ! empty( $args['class'] ) ? ' class="' . $args['class'] . '"' : '' ) . ' type="text" value="' . esc_attr( $args['value'] ) . '" name="rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . ']" />' . ( ! empty ( $args['description'] ) ? '<p class="description">' . $args['description'] . '</p>' : '' )
982 );
983 break;
984
985 case 'class':
986 case 'textarea':
987 $html .= sprintf(
988 $template,
989 ! empty( $args['title'] ) ? esc_html( $args['title'] ) : '',
990 '<textarea id="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '"' . ( ! empty( $args['class'] ) ? ' class="' . $args['class'] . '"' : '' ) . ' name="rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . ']">' . esc_textarea( $args['value'] ) . '</textarea>' . ( ! empty ( $args['description'] ) ? '<p class="description">' . $args['description'] . '</p>' : '' )
991 );
992 break;
993
994 case 'select':
995 $subhtml = '<select id="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '" name="rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . ']">';
996
997 foreach ( $args['options'] as $key => $label ) {
998 $subhtml .= '
999 <option value="' . $key . '" ' . selected( $args['value'], $key, false ) . '>' . $label . '</option>';
1000 }
1001
1002 $html .= sprintf(
1003 $template,
1004 ! empty( $args['title'] ) ? esc_html( $args['title'] ) : '',
1005 $subhtml . '</select>' . ( ! empty ( $args['description'] ) ? '<p class="description">' . $args['description'] . '</p>' : '' )
1006 );
1007 break;
1008
1009 case 'taxonomy':
1010 $subhtml = wp_dropdown_categories(
1011 array(
1012 'orderby' => 'name',
1013 'order' => 'asc',
1014 'show_option_none' => __( 'None', 'responsive-lightbox' ),
1015 'show_option_all' => false,
1016 'show_count' => false,
1017 'hide_empty' => false,
1018 'option_none_value' => 0,
1019 'hierarchical' => true,
1020 'selected' => $args['value']['id'],
1021 'taxonomy' => $args['taxonomy'],
1022 'hide_if_empty' => false,
1023 'echo' => false,
1024 'id' => 'rl-' . $tab_id . '-' . $menu_item . '-' . $field,
1025 'name' => 'rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . '][id]'
1026 )
1027 );
1028
1029 if ( isset( $args['include_children'] ) && $args['include_children'] ) {
1030 $subhtml .= '<label class="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '-include-children" for="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '-include-children"><input id="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '-include-children" type="checkbox" name="rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . '][children]" value="true" ' . checked( $args['value']['children'], true, false ) . ' />' . esc_html__( 'Include children.', 'responsive-lightbox' ) . '</label>';
1031 }
1032
1033 $html .= sprintf(
1034 $template,
1035 ! empty( $args['title'] ) ? esc_html( $args['title'] ) : '',
1036 $subhtml . ( ! empty ( $args['description'] ) ? '<p class="description">' . $args['description'] . '</p>' : '' )
1037 );
1038 break;
1039
1040 case 'multiselect':
1041 $subhtml = '<select multiple="multiple" class="select2" id="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '" data-empty="' . ( (int) empty( $args['value'] ) ) . '" data-type="' . $field . '" name="rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . '][]">';
1042
1043 if ( $field === 'post_term' ) {
1044 foreach ( $args['options'] as $taxanomy => $data ) {
1045 $subhtml .= '<optgroup label="' . esc_attr( $data['label'] ) . '">';
1046
1047 foreach ( $data['terms'] as $term_id => $name ) {
1048 $subhtml .= '<option value="' . $term_id . '" ' . selected( in_array( $term_id, $args['value'], false ), true, false ) . '>' . esc_html( $name ) . '</option>';
1049 }
1050
1051 $subhtml .= '</optgroup>';
1052 }
1053 } else {
1054 foreach ( $args['options'] as $key => $label ) {
1055 $subhtml .= '
1056 <option value="' . $key . '" ' . selected( in_array( $key, $args['value'], false ), true, false ) . '>' . $label . '</option>';
1057 }
1058 }
1059
1060 $html .= sprintf(
1061 $template,
1062 ! empty( $args['title'] ) ? esc_html( $args['title'] ) : '',
1063 $subhtml . '</select>' . ( ! empty ( $args['description'] ) ? '<p class="description">' . $args['description'] . '</p>' : '' )
1064 );
1065 break;
1066
1067 case 'boolean':
1068 $html .= sprintf(
1069 $template,
1070 ! empty( $args['title'] ) ? esc_html( $args['title'] ) : '',
1071 '<label class="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '" for="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '"><input id="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '" type="checkbox" name="rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . ']" value="true" ' . checked( $args['value'], true, false ) . ' />' . $args['label'] . '</label>' . ( ! empty ( $args['description'] ) ? '<p class="description">' . $args['description'] . '</p>' : '' )
1072 );
1073 break;
1074
1075 case 'checkbox':
1076 $subhtml = '';
1077
1078 foreach ( $args['options'] as $key => $label ) {
1079 $subhtml .= '<label class="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '-' . $key . '" for="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '-' . $key . '"><input id="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '-' . $key . '" type="checkbox" name="rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . '][' . $key . ']" value="true" ' . checked( in_array( $key, $args['value'], true ), true, false ) . ' />' . $label . '</label><br />';
1080 }
1081
1082 $html .= sprintf(
1083 $template,
1084 ! empty( $args['title'] ) ? esc_html( $args['title'] ) : '',
1085 $subhtml . ( ! empty ( $args['description'] ) ? '<p class="description">' . $args['description'] . '</p>' : '' )
1086 );
1087 break;
1088
1089 case 'multiple':
1090 $subhtml = '';
1091
1092 foreach ( $args['fields'] as $sub_field => $sub_args ) {
1093 $subhtml .= $this->render_field( $sub_field, $tab_id, $menu_item, $sub_args, $gallery_id, true ) . '<br />';
1094 }
1095
1096 $html .= sprintf(
1097 $template,
1098 ! empty( $args['title'] ) ? esc_html( $args['title'] ) : '',
1099 $subhtml . ( ! empty ( $args['description'] ) ? '<p class="description">' . $args['description'] . '</p>' : '' )
1100 );
1101 break;
1102
1103 case 'color_picker':
1104 $html .= sprintf(
1105 $template,
1106 ! empty( $args['title'] ) ? esc_html( $args['title'] ) : '',
1107 '<input id="rl-' . $tab_id . '-' . $menu_item . '-' . $field . '" class="color-picker" type="text" value="' . $args['value'] . '" name="rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . ']" data-default-color="' . $args['default'] . '" />' . ( ! empty ( $args['description'] ) ? '<p class="description">' . $args['description'] . '</p>' : '' )
1108 );
1109 break;
1110
1111 case 'media_library':
1112 $data = get_post_meta( $gallery_id, '_rl_images', true );
1113
1114 // get images
1115 if ( ( ! empty( $data['menu_item'] ) && $data['menu_item'] === 'media' ) || ! ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_POST['action'] ) && $_POST['action'] === 'rl-get-menu-content' ) )
1116 $images = $this->get_gallery_images( $gallery_id );
1117 else
1118 $images = array();
1119
1120 // get media item template
1121 $media_item_template = $this->media_item_template;
1122
1123 $html .= '
1124 <td colspan="2" class="rl-colspan">
1125 <input type="hidden" class="rl-gallery-ids" name="rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . '][ids]" value="' . ( ! empty( $args['value']['ids'] ) ? implode( ',', $args['value']['ids'] ) : '' ) . '">
1126 <a href="#" class="rl-gallery-select button button-secondary">' . __( 'Select images', 'responsive-lightbox' ) . '</a>
1127 <div class="rl-gallery-content">
1128 <ul class="rl-gallery-images rl-gallery-images-media">';
1129
1130 if ( ! empty( $images ) ) {
1131 foreach ( $images as $image ) {
1132 if ( $image['id'] === 0 )
1133 $excluded_item = $image['url'];
1134 else
1135 $excluded_item = $image['id'];
1136
1137 $image_html = $this->get_gallery_image( $image, 'thumbnail' );
1138
1139 // replace ID and URL of an image
1140 $html .= str_replace( '__IMAGE__', '<input type="hidden" class="rl-gallery-exclude" name="rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . '][exclude][]" value="' . ( in_array( $excluded_item, $args['value']['exclude'], true ) ? $excluded_item : '' ) . '" />' . $image_html, str_replace( '__IMAGE_ID__', $image['id'], str_replace( '__IMAGE_STATUS__', in_array( $excluded_item, $args['value']['exclude'], true ) ? 'rl-status-inactive' : 'rl-status-active', $media_item_template ) ) );
1141 }
1142 }
1143
1144 $html .= '
1145 </ul>
1146 </div>
1147 </td>';
1148 break;
1149
1150 case 'media_preview':
1151 case 'folder_preview':
1152 $this->menu_item = $menu_item;
1153
1154 // get images
1155 $images = $this->get_gallery_images( $gallery_id );
1156
1157 // get media item template
1158 $media_item_template = $this->media_item_template;
1159
1160 $html .= '
1161 <td colspan="2" class="rl-colspan">
1162 <div class="rl-gallery-preview-inside">
1163 <a href="#" class="rl-gallery-update-preview button button-secondary">' . __( 'Update preview', 'responsive-lightbox' ) . '</a><span class="spinner" style="display: none;"></span>
1164 </div>
1165 <div class="rl-gallery-content">
1166 <ul class="rl-gallery-images rl-gallery-images-' . ( $args['type'] === 'media_preview' ? 'featured' : 'folders' ) . '">';
1167
1168 if ( ! empty( $images ) ) {
1169 foreach ( $images as $image ) {
1170 if ( empty( $image['id'] ) ) {
1171 $excluded_item = $image['url'];
1172 $image['id'] = 0;
1173 } else
1174 $excluded_item = $image['id'];
1175
1176 $image_html = $this->get_gallery_image( $image, 'thumbnail' );
1177
1178 // replace ID and URL of an image
1179 $html .= str_replace( '__IMAGE__', '<input type="hidden" class="rl-gallery-exclude" name="rl_gallery[' . $tab_id . '][' . $menu_item . '][' . $field . '][exclude][]" value="' . ( in_array( $excluded_item, $args['value']['exclude'], true ) ? $excluded_item : '' ) . '" />' . $image_html, str_replace( '__IMAGE_ID__', $image['id'], str_replace( '__IMAGE_STATUS__', in_array( $excluded_item, $args['value']['exclude'], true ) ? 'rl-status-inactive' : 'rl-status-active', $media_item_template ) ) );
1180 }
1181 }
1182
1183 $html .= '
1184 </ul>
1185 </div>
1186 </td>';
1187 break;
1188
1189 default:
1190 $html .= sprintf(
1191 $template,
1192 ! empty( $args['title'] ) ? esc_html( $args['title'] ) : '',
1193 apply_filters( 'rl_render_gallery_field_' . $args['type'], $subhtml, $field, $tab_id, $menu_item, $args, $subfield ) . ( ! empty ( $args['description'] ) ? '<p class="description">' . $args['description'] . '</p>' : '' )
1194 );
1195 }
1196
1197 if ( ! $subfield )
1198 $html .= '</tr>';
1199
1200 return apply_filters( 'rl_render_gallery_field', $html, $field, $tab_id, $menu_item, $args, $subfield );
1201 }
1202
1203 /**
1204 * Sanitize field based on type. Internal use only.
1205 *
1206 * @param string $field Field name
1207 * @param mixed $value Field value
1208 * @param array $args Field arguments
1209 * @return mixed Sanitized value
1210 */
1211 public function sanitize_field( $field, $value, $args ) {
1212 switch ( $args['type'] ) {
1213 case 'radio':
1214 case 'select':
1215 $value = array_key_exists( $value, $args['options'] ) ? $value : $args['default'];
1216 break;
1217
1218 case 'taxonomy':
1219 if ( is_array( $value ) ) {
1220 if ( isset( $value['id'] ) )
1221 $value['id'] = (int) $value['id'];
1222 else
1223 $value['id'] = 0;
1224
1225 $value['children'] = isset( $value['children'] );
1226 } else
1227 $value = $args['default'];
1228
1229 // get term
1230 $term = get_term( $value['id'], $args['taxonomy'] );
1231
1232 // valid term?
1233 if ( is_a( $term, 'WP_Term' ) )
1234 $value['id'] = $term->term_id;
1235 else
1236 $value['id'] = 0;
1237 break;
1238
1239 case 'multiselect':
1240 if ( is_array( $value ) ) {
1241 // is it post term field?
1242 if ( $field === 'post_term' ) {
1243 $terms = array();
1244
1245 foreach ( $args['options'] as $data ) {
1246 $terms += $data['terms'];
1247 }
1248
1249 $args['options'] = $terms;
1250 }
1251
1252 $values = array();
1253
1254 foreach ( $value as $subvalue ) {
1255 if ( array_key_exists( $subvalue, $args['options'] ) )
1256 $values[] = $subvalue;
1257 }
1258
1259 $value = $values;
1260 } else
1261 $value = $args['default'];
1262 break;
1263
1264 case 'checkbox':
1265 if ( is_array( $value ) && ! empty( $value ) ) {
1266 $sort = array();
1267
1268 foreach ( $value as $sort_key => $bool ) {
1269 if ( array_key_exists( $sort_key, $args['options'] ) )
1270 $sort[] = $sort_key;
1271 }
1272
1273 $value = $sort;
1274 } else
1275 $value = array();
1276 break;
1277
1278 case 'boolean':
1279 $value = $value === 'true';
1280 break;
1281
1282 case 'range':
1283 case 'number':
1284 $value = (int) $value;
1285
1286 // is value lower than?
1287 if ( isset( $args['min'] ) && $value < $args['min'] )
1288 $value = $args['min'];
1289
1290 // is value greater than?
1291 if ( isset( $args['max'] ) && $value > $args['max'] )
1292 $value = $args['max'];
1293 break;
1294
1295 case 'class':
1296 $value = trim( $value );
1297
1298 // more than 1 class?
1299 if ( strpos( $value, ' ' ) !== false ) {
1300 // get unique valid HTML classes
1301 $value = array_unique( array_filter( array_map( 'sanitize_html_class', explode( ' ', $value ) ) ) );
1302
1303 if ( ! empty( $value ) )
1304 $value = implode( ' ', $value );
1305 else
1306 $value = '';
1307 // single class
1308 } else
1309 $value = sanitize_html_class( $value );
1310 break;
1311
1312 case 'text':
1313 $value = trim( sanitize_text_field( $value ) );
1314 break;
1315
1316 case 'textarea':
1317 global $wp_version;
1318
1319 // WP 4.7+
1320 if ( version_compare( $wp_version, '4.7', '>=' ) )
1321 $value = trim( sanitize_textarea_field( $value ) );
1322 // _sanitize_text_fields
1323 else {
1324 $value = wp_check_invalid_utf8( $value );
1325
1326 if ( strpos( $value, '<' ) !== false ) {
1327 $value = wp_pre_kses_less_than( $value );
1328
1329 // this will strip extra whitespace for us.
1330 $value = wp_strip_all_tags( $value, false );
1331
1332 // use html entities in a special case to make sure no later newline stripping stage could lead to a functional tag
1333 $value = str_replace( "<\n", "&lt;\n", $value );
1334 }
1335
1336 $value = trim( $value );
1337 $found = false;
1338
1339 while ( preg_match('/%[a-f0-9]{2}/i', $value, $match ) ) {
1340 $value = str_replace( $match[0], '', $value );
1341 $found = true;
1342 }
1343
1344 // strip out the whitespace that may now exist after removing the octets.
1345 if ( $found )
1346 $value = trim( preg_replace( '/ +/', ' ', $value ) );
1347 }
1348 break;
1349
1350 case 'color_picker':
1351 if ( preg_match( '/^#[a-f0-9]{6}$/i', $value ) !== 1 )
1352 $value = $args['default'];
1353 break;
1354
1355 case 'media_library':
1356 if ( is_array( $value ) ) {
1357 $data = $args['default'];
1358
1359 // check IDs
1360 if ( array_key_exists( 'ids', $value ) ) {
1361 $ids = (string) trim( $value['ids'] );
1362
1363 if ( $ids !== '' )
1364 // get unique and non empty attachment IDs only
1365 $data['ids'] = $this->check_attachments( array_unique( array_filter( array_map( 'intval', explode( ',', $ids ) ) ) ) );
1366 else
1367 $data['ids'] = array();
1368 }
1369
1370 // check excluded items
1371 if ( array_key_exists( 'exclude', $value ) && is_array( $value['exclude'] ) && ! empty( $value['exclude'] ) ) {
1372 $ids = $strings = array();
1373
1374 foreach ( $value['exclude'] as $exclude_item ) {
1375 $item = trim( $exclude_item );
1376
1377 if ( is_numeric( $item ) )
1378 $ids[] = (int) $item;
1379 elseif ( $item !== '' )
1380 $strings[] = $item;
1381 }
1382
1383 if ( ! empty( $ids ) ) {
1384 // get unique and non empty attachment IDs only
1385 $ids = $this->check_attachments( array_unique( array_filter( $ids ) ) );
1386 }
1387
1388 $data['exclude'] = $ids + $strings;
1389 }
1390
1391 $value = $data;
1392 } else
1393 $value = $args['default'];
1394 break;
1395
1396 case 'media_preview':
1397 case 'folder_preview':
1398 if ( is_array( $value ) ) {
1399 $data = $args['default'];
1400
1401 // check excluded items
1402 if ( array_key_exists( 'exclude', $value ) && is_array( $value['exclude'] ) && ! empty( $value['exclude'] ) ) {
1403 $ids = $strings = array();
1404
1405 foreach ( $value['exclude'] as $exclude_item ) {
1406 $item = trim( $exclude_item );
1407
1408 if ( is_numeric( $item ) )
1409 $ids[] = (int) $item;
1410 elseif ( $item !== '' )
1411 $strings[] = $item;
1412 }
1413
1414 if ( ! empty( $ids ) ) {
1415 // get unique and non empty attachment IDs only
1416 $ids = $this->check_attachments( array_unique( array_filter( $ids ) ) );
1417 }
1418
1419 $data['exclude'] = $ids + $strings;
1420 }
1421
1422 $value = $data;
1423 } else
1424 $value = $args['default'];
1425 }
1426
1427 return apply_filters( 'rl_sanitize_gallery_field', $value, $args );
1428 }
1429
1430 /**
1431 * Sanitize set of fields.
1432 *
1433 * @param array $items Fields
1434 * @param array $data POST data
1435 * @param string $tab_id Gallery tab
1436 * @param string $menu_item Gallery menu item
1437 * @return array Sanitize fields
1438 */
1439 public function sanitize_fields( $items, $data, $tab_id, $menu_item ) {
1440 $safedata = array();
1441
1442 foreach ( $items as $field => $item ) {
1443 // skip this field
1444 if ( isset( $item['save'] ) && ! $item['save'] )
1445 continue;
1446
1447 // available field?
1448 if ( isset( $data[$tab_id], $data[$tab_id][$menu_item], $data[$tab_id][$menu_item][$field] ) )
1449 $safedata[$tab_id][$menu_item][$field] = $this->sanitize_field( $field, $data[$tab_id][$menu_item][$field], $item );
1450 // boolean field?
1451 elseif ( $item['type'] === 'boolean' )
1452 $safedata[$tab_id][$menu_item][$field] = false;
1453 // multiple fields?
1454 elseif ( $item['type'] === 'multiple' ) {
1455 foreach ( $item['fields'] as $subfield => $subitem ) {
1456 // available subfield?
1457 if ( isset( $data[$tab_id], $data[$tab_id][$menu_item], $data[$tab_id][$menu_item][$subfield] ) )
1458 $safedata[$tab_id][$menu_item][$subfield] = $this->sanitize_field( $subfield, $data[$tab_id][$menu_item][$subfield], $subitem );
1459 // boolean subfield?
1460 elseif ( $subitem['type'] === 'boolean' )
1461 $safedata[$tab_id][$menu_item][$subfield] = false;
1462 // any other case
1463 else
1464 $safedata[$tab_id][$menu_item][$subfield] = $subitem['default'];
1465 }
1466 // any other case
1467 } else
1468 $safedata[$tab_id][$menu_item][$field] = $item['default'];
1469 }
1470
1471 return $safedata;
1472 }
1473
1474 /**
1475 * Add menu tabs after the post title.
1476 *
1477 * @param object $post Post object
1478 * @return mixed
1479 */
1480 public function after_title_nav_menu( $post ) {
1481 if ( $post->post_type !== 'rl_gallery' )
1482 return;
1483
1484 global $wp_meta_boxes;
1485
1486 $active_tab = ! empty( $_GET['rl_active_tab'] ) && array_key_exists( $_GET['rl_active_tab'], $this->tabs ) ? $_GET['rl_active_tab'] : 'images';
1487
1488 // this will be moved to #postbox-container-2 using js to avoid mobile devices problem
1489 echo '
1490 <h2 class="nav-tab-wrapper">';
1491
1492 foreach ( $this->tabs as $key => $data ) {
1493 echo '
1494 <a id="rl-gallery-tab-' . $key . '" class="rl-gallery-tab nav-tab' . ( $key === $active_tab ? ' nav-tab-active' : '' ) . '" href="#' . $key . '">' . $data['label'] . '</a>';
1495 }
1496
1497 echo '
1498 </h2>';
1499
1500 do_meta_boxes( $post->post_type, 'after_title', $post );
1501
1502 unset( $wp_meta_boxes[$post->post_type]['after_title'] );
1503 }
1504
1505 /**
1506 * Add class to hide metabox.
1507 *
1508 * @param array $classes Classes
1509 * @return array Classes
1510 */
1511 public function hide_metabox( $classes ) {
1512 $classes[] = 'rl-metabox-content';
1513 $classes[] = 'rl-hide-metabox';
1514
1515 return $classes;
1516 }
1517
1518 /**
1519 * Add class to display the metabox.
1520 *
1521 * @param array $classes Classes
1522 * @return array Classes
1523 */
1524 function display_metabox( $classes ) {
1525 $classes[] = 'rl-metabox-content';
1526 $classes[] = 'rl-display-metabox';
1527
1528 return $classes;
1529 }
1530
1531 /**
1532 * Add active tab to post redirect destination URL.
1533 *
1534 * @param string $location Destination URL
1535 * @return string Detination URL with extra arg
1536 */
1537 function add_active_tab( $location ) {
1538 return add_query_arg( 'rl_active_tab', ! empty( $_POST['rl_active_tab'] ) && array_key_exists( $_POST['rl_active_tab'], $this->tabs ) ? $_POST['rl_active_tab'] : 'images', $location );
1539 }
1540
1541 /**
1542 * Add metaboxes.
1543 *
1544 * @return void
1545 */
1546 public function add_meta_boxes() {
1547 $active_tab = ! empty( $_GET['rl_active_tab'] ) && array_key_exists( $_GET['rl_active_tab'], $this->tabs ) ? $_GET['rl_active_tab'] : 'images';
1548
1549 // normal metaboxes
1550 foreach ( $this->tabs as $key => $args ) {
1551 if ( $key === 'images' )
1552 $new_args = $args + array( 'tab_id' => $key, 'active_tab' => $active_tab );
1553 else
1554 $new_args = $args + array( 'tab_id' => $key );
1555
1556 // handle metabox class
1557 if ( $active_tab === $key )
1558 add_filter( 'postbox_classes_rl_gallery_responsive-gallery-' . $key, array( $this, 'display_metabox' ) );
1559 else
1560 add_filter( 'postbox_classes_rl_gallery_responsive-gallery-' . $key, array( $this, 'hide_metabox' ) );
1561
1562 add_meta_box( 'responsive-gallery-' . $key, sprintf( __( 'Gallery %s', 'responsive-lightbox' ), $args['label'] ), array( $this, 'add_metabox' ), 'rl_gallery', 'after_title', 'high', $new_args );
1563 }
1564
1565 // side metaboxes
1566 add_meta_box( 'responsive-gallery-shortcode', __( 'Gallery Code', 'responsive-lightbox' ), array( $this, 'shortcode_metabox' ), 'rl_gallery', 'side', 'core' );
1567 }
1568
1569 /**
1570 * Add single metabox.
1571 *
1572 * @param type $post
1573 * @param array $callback_args
1574 * @return mixed
1575 */
1576 public function add_metabox( $post, $callback_args ) {
1577 $html = $callback_args['args']['tab_id'] === 'images' ? '<input type="hidden" name="rl_active_tab" value="' . $callback_args['args']['active_tab'] . '" />' : '';
1578
1579 // default menu item
1580 $menu_item = 'options';
1581
1582 // get tab data
1583 $data = get_post_meta( $post->ID, '_rl_' . $callback_args['args']['tab_id'], true );
1584
1585 if ( ! is_array( $data ) )
1586 $data = array();
1587
1588 // maybe add description
1589 $html .= ! empty( $callback_args['args']['description'] ) ? '<p class="rl-gallery-tab-description">' . esc_html( $callback_args['args']['description'] ) . '</p>' : '';
1590
1591 // maybe add menu
1592 if ( ! empty( $callback_args['args']['menu_items'] ) ) {
1593 // get selected menu item
1594 $menu_item = ! empty( $data['menu_item'] ) && in_array( $data['menu_item'], array_keys( $callback_args['args']['menu_items'] ) ) ? $data['menu_item'] : key( $callback_args['args']['menu_items'] );
1595
1596 $html .= '
1597 <div class="rl-gallery-tab-menu rl-gallery-tab-menu-' . $callback_args['args']['tab_id'] . '">';
1598
1599 foreach ( $callback_args['args']['menu_items'] as $menu_key => $menu_label ) {
1600 $html .= '
1601 <label><input type="radio" class="rl-gallery-tab-menu-item" name="rl_gallery[' . $callback_args['args']['tab_id'] . '][menu_item]" value="' . $menu_key . '" ' . checked( $menu_item, $menu_key, false ) . ' />' . esc_html( $menu_label ) . ( $callback_args['args']['tab_id'] === 'config' && $menu_key === 'default' ? ' (' . $this->tabs['config']['menu_items'][Responsive_Lightbox()->options['settings']['builder_gallery']] . ')' : '' ) . '</label>';
1602 }
1603
1604 $html .= '
1605 <span class="spinner" style="display: none;"></span>
1606 </div>';
1607 }
1608
1609 $html .= '
1610 <div class="rl-gallery-tab-content rl-gallery-tab-content-' . $callback_args['args']['tab_id'] . '">';
1611
1612 $html .= ! empty( $callback_args['args']['callback'] ) && is_callable( $callback_args['args']['callback'] ) ? call_user_func( $callback_args['args']['callback'], $callback_args['args']['tab_id'], $data, $menu_item, $post->ID ) : $this->get_metabox_content( $callback_args['args']['tab_id'], $data, $menu_item, $post->ID );
1613
1614 $html .= '
1615 </div>';
1616
1617 echo $html;
1618 }
1619
1620 /**
1621 * Get single metabox content.
1622 *
1623 * @param string $tab_id
1624 * @param array $data
1625 * @param string $menu_item
1626 * @param int $gallery_id Gallery ID
1627 * @return string
1628 */
1629 public function get_metabox_content( $tab_id, $data, $menu_item, $gallery_id = 0 ) {
1630 $html = '
1631 <div class="rl-gallery-tab-inside rl-gallery-tab-inside-' . $tab_id . '-' . $menu_item . '">
1632 <table class="form-table">';
1633
1634 switch ( $tab_id ) {
1635 case 'config':
1636 // assign singleton instance
1637 $rl = Responsive_Lightbox();
1638
1639 // get default gallery fields
1640 $default_gallery = $rl->frontend->get_default_gallery_fields();
1641
1642 // assign settings and defaults
1643 $settings = $rl->settings->settings;
1644 $defaults = $rl->defaults;
1645
1646 if ( ! array_key_exists( 'default_gallery', $settings ) )
1647 $settings['default_gallery']['fields'] = $default_gallery;
1648
1649 // assign default values
1650 foreach ( $default_gallery as $field => $field_args ) {
1651 $defaults['default_gallery'][$field] = $field_args['default'];
1652 }
1653
1654 // valid gallery?
1655 if ( array_key_exists( $menu_item . '_gallery', $settings ) && array_key_exists( $menu_item . '_gallery', $defaults ) ) {
1656 if ( $menu_item === 'default' )
1657 $fields = $settings['default_gallery']['fields'];
1658 else {
1659 $fields = $rl->frontend->get_unique_fields( $settings['default_gallery']['fields'], $settings[$menu_item . '_gallery']['fields'] );
1660
1661 // add default gallery default values
1662 foreach ( $default_gallery as $field => $field_args ) {
1663 $defaults[$menu_item . '_gallery'][$field] = $field_args['default'];
1664 }
1665 }
1666
1667 foreach ( $fields as $field => $args ) {
1668 if ( $args['type'] === 'multiple' ) {
1669 $new_args = $args;
1670
1671 foreach ( $args['fields'] as $subfield => $subargs ) {
1672 $new_args['fields'][$subfield] = $subargs + array(
1673 'value' => isset( $data[$menu_item], $data[$menu_item][$subfield] ) ? $data[$menu_item][$subfield] : $defaults[$menu_item . '_gallery'][$subfield],
1674 'default' => $defaults[$menu_item . '_gallery'][$subfield]
1675 );
1676 }
1677 } else {
1678 $new_args = $args + array(
1679 'value' => isset( $data[$menu_item], $data[$menu_item][$field] ) ? $data[$menu_item][$field] : $defaults[$menu_item . '_gallery'][$field],
1680 'default' => $defaults[$menu_item . '_gallery'][$field]
1681 );
1682 }
1683
1684 $html .= $this->render_field( $field, $tab_id, $menu_item, $new_args, $gallery_id );
1685 }
1686 // just in case ajax would fail
1687 } else
1688 $html .= '<p>' . __( 'No data', 'responsive-lightbox' ) . '</p>';
1689 break;
1690
1691 default:
1692 foreach ( $this->fields[$tab_id][$menu_item] as $field => $args ) {
1693 // was this field stored in a database?
1694 if ( isset( $args['save'] ) && ! $args['save'] )
1695 $new_args = $args;
1696 elseif ( $args['type'] === 'multiple' ) {
1697 $new_args = $args;
1698
1699 foreach ( $args['fields'] as $subfield => $subargs ) {
1700 $new_args['fields'][$subfield] = $subargs + array( 'value' => isset( $data[$menu_item], $data[$menu_item][$subfield] ) ? $data[$menu_item][$subfield] : $subargs['default'] );
1701 }
1702 } else
1703 $new_args = $args + array( 'value' => isset( $data[$menu_item], $data[$menu_item][$field] ) ? $data[$menu_item][$field] : $args['default'] );
1704
1705 // media preview?
1706 // if ( $tab_id === 'images' && $menu_item === 'featured' && $field === 'attachments' && $args['type'] === 'media_preview' )
1707 // $new_args['subfields'] = $data['featured'];
1708
1709 $html .= $this->render_field( $field, $tab_id, $menu_item, $new_args, $gallery_id );
1710 }
1711 }
1712
1713 $html .= '
1714 </table>
1715 </div>';
1716
1717 return apply_filters( 'rl_gallery_tab_content', $html, $tab_id, $data, $menu_item );
1718 }
1719
1720 /**
1721 * Get gallery images.
1722 *
1723 * @param int $gallery_id
1724 * @param array $args
1725 * @return array
1726 */
1727 public function get_gallery_images( $gallery_id = 0, $args = array() ) {
1728 $images = array();
1729 $excluded = array();
1730
1731 // assign singleton instance
1732 $rl = Responsive_Lightbox();
1733
1734 // get args
1735 $defaults = array(
1736 'exclude' => false,
1737 'posts_per_page' => -1,
1738 'images_per_page' => 0,
1739 'page' => 1,
1740 'nopaging' => true,
1741 'image_size' => 'large',
1742 'thumbnail_size' => 'thumbnail',
1743 'pagination_type' => 'paged',
1744 'pagination_position' => 'bottom',
1745 'orderby' => 'menu_order',
1746 'order' => 'asc',
1747 'taxonomy' => $rl->options['folders']['media_taxonomy'],
1748 'folder' => array(
1749 'id' => 0,
1750 'children' => null
1751 )
1752 );
1753
1754 // parse arguments
1755 $args = wp_parse_args( apply_filters( 'rl_get_gallery_images_args', $args, $gallery_id ), $defaults );
1756
1757 // sanitize args
1758 $args['exclude'] = (bool) ! empty( $args['exclude'] );
1759 $args['posts_per_page'] = ! empty( $args['posts_per_page'] ) ? (int) $args['posts_per_page'] : -1;
1760 $args['nopaging'] = (bool) ! empty( $args['nopaging'] );
1761
1762 // is it rl_gallery?
1763 if ( $valid_gallery_type = ( get_post_type( $gallery_id ) === 'rl_gallery' ) ) {
1764 $paging = get_post_meta( $gallery_id, '_rl_paging', true );
1765
1766 if ( isset( $paging['menu_item'] ) ) {
1767 $pagination = $paging[$paging['menu_item']];
1768
1769 if ( $pagination['pagination'] ) {
1770 $args['nopaging'] = false;
1771 $args['images_per_page'] = $pagination['images_per_page'];
1772 $args['pagination_type'] = $pagination['pagination_type'];
1773
1774 // infinite type?
1775 if ( $args['pagination_type'] === 'infinite' )
1776 $args['pagination_position'] = 'bottom';
1777 else
1778 $args['pagination_position'] = $pagination['pagination_position'];
1779 } else
1780 $args['nopaging'] = true;
1781 }
1782 }
1783
1784 global $pagenow;
1785
1786 // is it preview?
1787 if ( ( in_array( $pagenow, array( 'post.php', 'post-new.php' ), true ) && $gallery_id ) || ( isset( $_POST['action'] ) && $_POST['action'] === 'rl-get-preview-content' ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_POST['action'] ) && ( $_POST['action'] === 'rl-post-gallery-preview' || $_POST['action'] === 'rl-get-menu-content' ) ) )
1788 $args['images_per_page'] = 0;
1789
1790 if ( isset( $_GET['rl_page'] ) )
1791 $args['page'] = (int) $_GET['rl_page'];
1792 else
1793 $args['page'] = (int) $args['page'];
1794
1795 // is it rl_gallery?
1796 if ( $valid_gallery_type ) {
1797 // get config metadata
1798 $config_meta = get_post_meta( $gallery_id, '_rl_config', true );
1799
1800 // config order
1801 if ( isset( $config_meta['menu_item'] ) ) {
1802 $config = $config_meta[$config_meta['menu_item']];
1803
1804 $args['orderby'] = $config['orderby'];
1805 $args['order'] = $config['order'];
1806 }
1807
1808 // get images metadata
1809 $data = get_post_meta( $gallery_id, '_rl_images', true );
1810
1811 // array?
1812 if ( ! is_array( $data ) )
1813 $data = array();
1814
1815 // get menu item
1816 if ( ! empty( $this->menu_item ) )
1817 $menu_item = $this->menu_item;
1818 elseif ( array_key_exists( 'menu_item', $data ) )
1819 $menu_item = $data['menu_item'];
1820 else
1821 $menu_item = 'media';
1822
1823 // valid data?
1824 if ( ! array_key_exists( $menu_item, $data ) )
1825 $data[$menu_item] = array();
1826
1827 switch ( $menu_item ) {
1828 case 'media':
1829 // get attachment ids
1830 $attachments = ! empty( $data[$menu_item]['attachments']['ids'] ) ? array_map( 'absint', $data[$menu_item]['attachments']['ids'] ) : array();
1831
1832 if ( $attachments ) {
1833 if ( $args['exclude'] )
1834 $excluded = ! empty( $data[$menu_item]['attachments']['exclude'] ) ? $data[$menu_item]['attachments']['exclude'] : array();
1835
1836 foreach ( $attachments as $attachment_id ) {
1837 // get attachment image data
1838 if ( ! in_array( $attachment_id, $excluded ) )
1839 $images[] = $this->get_gallery_image_src( $attachment_id, $args['image_size'], $args['thumbnail_size'] );
1840 }
1841 }
1842 break;
1843
1844 case 'featured':
1845 // copy arguments
1846 $query_args = $args;
1847
1848 // prevent duplicating images order (config tab) with posts order (images tab), query will handle empty strings
1849 if ( array_key_exists( 'post_orderby', $args ) )
1850 $query_args['orderby'] = $args['post_orderby'];
1851 elseif ( array_key_exists( 'orderby', $data[$menu_item] ) )
1852 $query_args['orderby'] = $data[$menu_item]['orderby'];
1853 else
1854 $query_args['orderby'] = '';
1855
1856 if ( array_key_exists( 'post_order', $args ) )
1857 $query_args['order'] = $args['post_order'];
1858 elseif ( array_key_exists( 'order', $data[$menu_item] ) )
1859 $query_args['order'] = $data[$menu_item]['order'];
1860 else
1861 $query_args['order'] = '';
1862
1863 // get attachment ids
1864 $attachments = $this->gallery_query( array_merge( $data[$menu_item], $query_args ) );
1865
1866 if ( $attachments ) {
1867 if ( $args['exclude'] )
1868 $excluded = ! empty( $data[$menu_item]['attachments']['exclude'] ) ? $data[$menu_item]['attachments']['exclude'] : array();
1869
1870 foreach ( $attachments as $attachment_id ) {
1871 // get attachment image data
1872 if ( ! in_array( $attachment_id, $excluded ) )
1873 $images[] = $this->get_gallery_image_src( $attachment_id, $args['image_size'], $args['thumbnail_size'] );
1874 }
1875 }
1876 break;
1877
1878 case 'folders':
1879 if ( ! array_key_exists( 'folder', $data[$menu_item] ) )
1880 $data[$menu_item]['folder'] = $defaults['folder'];
1881
1882 // AJAX requests
1883 if ( is_string( $args['folder']['id'] ) )
1884 $args['folder']['id'] = (int) $args['folder']['id'];
1885
1886 // not empty folder term ID?
1887 if ( ! empty( $args['folder']['id'] ) ) {
1888 // get term
1889 $term = get_term( $args['folder']['id'], $args['taxonomy'] );
1890
1891 // valid term?
1892 if ( is_a( $term, 'WP_Term' ) )
1893 $folder_id = (int) $term->term_id;
1894 else
1895 $folder_id = (int) $data[$menu_item]['folder']['id'];
1896 } else {
1897 if ( isset( $_POST['action'] ) && $_POST['action'] === 'rl-get-preview-content' )
1898 $folder_id = $args['folder']['id'];
1899 else
1900 $folder_id = (int) $data[$menu_item]['folder']['id'];
1901 }
1902
1903 if ( $folder_id > 0 ) {
1904 $include_children = false;
1905
1906 // null means folder was not changed
1907 if ( $args['folder']['children'] === null ) {
1908 if ( array_key_exists( 'children', $data[$menu_item]['folder'] ) && $data[$menu_item]['folder']['children'] === true )
1909 $include_children = true;
1910 // overwritten by args
1911 } else {
1912 if ( is_string( $args['folder']['children'] ) ) {
1913 if ( $args['folder']['children'] === 'true' )
1914 $include_children = true;
1915 } elseif ( is_bool( $args['folder']['children'] ) ) {
1916 if ( $args['folder']['children'] )
1917 $include_children = true;
1918 }
1919 }
1920
1921 $query = new WP_Query(
1922 array(
1923 'post_type' => 'attachment',
1924 'post_status' => 'inherit',
1925 'nopaging' => true,
1926 'posts_per_page' => -1,
1927 'fields' => 'ids',
1928 'tax_query' => array(
1929 array(
1930 'taxonomy' => $args['taxonomy'],
1931 'field' => 'term_id',
1932 'terms' => $folder_id,
1933 'include_children' => $include_children,
1934 'operator' => 'IN'
1935 )
1936 )
1937 )
1938 );
1939
1940 // get attachment IDs
1941 $attachments = $query->get_posts();
1942
1943 // valid attachments?
1944 if ( ! is_wp_error( $attachments ) ) {
1945 // cast IDs to int
1946 $attachments = array_map( 'intval', $attachments );
1947
1948 // make sure to skip duplicates
1949 $attachments = array_unique( $attachments );
1950
1951 if ( $args['exclude'] )
1952 $excluded = ! empty( $data[$menu_item]['attachments']['exclude'] ) ? $data[$menu_item]['attachments']['exclude'] : array();
1953
1954 foreach ( $attachments as $attachment_id ) {
1955 // get attachment image data
1956 if ( ! in_array( $attachment_id, $excluded ) )
1957 $images[] = $this->get_gallery_image_src( $attachment_id, $args['image_size'], $args['thumbnail_size'] );
1958 }
1959 }
1960 }
1961 break;
1962 }
1963 }
1964
1965 // config sort order
1966 switch ( $args['orderby'] ) {
1967 case 'id':
1968 $sort = array();
1969
1970 foreach ( $images as $key => $image ) {
1971 // set sorting value
1972 $sort[$key] = $image['id'];
1973 }
1974
1975 // sort
1976 array_multisort( $sort, $args['order'] === 'asc' ? SORT_ASC : SORT_DESC, SORT_NUMERIC, $images );
1977 break;
1978
1979 case 'title':
1980 $sort = array();
1981
1982 if ( $valid_gallery_type ) {
1983 // get lightbox data
1984 $lightbox_meta = get_post_meta( $gallery_id, '_rl_lightbox', true );
1985
1986 // valid data?
1987 if ( isset( $lightbox_meta['menu_item'] ) )
1988 $title_arg = $lightbox_meta[$lightbox_meta['menu_item']]['lightbox_image_title'];
1989 else
1990 $title_arg = $rl->options['settings']['gallery_image_title'];
1991 } else
1992 $title_arg = $rl->options['settings']['gallery_image_title'];
1993
1994 $images_copy = $images;
1995
1996 foreach ( $images_copy as $key => $image ) {
1997 if ( $title_arg === 'global' )
1998 $images[$key]['title'] = $rl->frontend->get_attachment_title( $image['id'], $rl->options['settings']['gallery_image_title'] );
1999 elseif ( $title_arg === 'default' )
2000 $images[$key]['title'] = '';
2001 else
2002 $images[$key]['title'] = $rl->frontend->get_attachment_title( $image['id'], $title_arg );
2003
2004 // set sorting value
2005 $sort[$key] = function_exists( 'mb_strtolower' ) ? mb_strtolower( $images[$key]['title'] ) : strtolower( $images[$key]['title'] );
2006 }
2007
2008 // sort
2009 array_multisort( $sort, $args['order'] === 'asc' ? SORT_ASC : SORT_DESC, SORT_STRING, $images );
2010 break;
2011
2012 case 'post_date':
2013 $sort = array();
2014
2015 foreach ( $images as $key => $image ) {
2016 // set sorting value
2017 $sort[$key] = $image['date'];
2018 }
2019
2020 // sort
2021 array_multisort( $sort, $args['order'] === 'asc' ? SORT_ASC : SORT_DESC, $images );
2022 break;
2023
2024 case 'menu_order':
2025 // do nothing
2026 break;
2027
2028 case 'rand':
2029 shuffle( $images );
2030 break;
2031 }
2032
2033 $images = apply_filters( 'rl_get_gallery_images_array', $images, $gallery_id, $args );
2034
2035 // save images count
2036 $images_count = count( $images );
2037
2038 if ( metadata_exists( 'post', $gallery_id, '_rl_images_count' ) ) {
2039 $images_count_prev = get_post_meta( $gallery_id, '_rl_images_count', true );
2040
2041 update_post_meta( $gallery_id, '_rl_images_count', $images_count, $images_count_prev );
2042 } else
2043 add_post_meta( $gallery_id, '_rl_images_count', $images_count );
2044
2045 if ( $images && ! $args['nopaging'] && $args['images_per_page'] > 0 ) {
2046 // get part of images
2047 $images = array_slice( $images, ( $args['page'] - 1 ) * $args['images_per_page'], $args['images_per_page'], true );
2048
2049 // pass gallery args
2050 $this->gallery_args = $args;
2051 $this->gallery_args['total'] = (int) ceil( $images_count / $args['images_per_page'] );
2052
2053 // pagination position
2054 if ( $args['pagination_position'] === 'top' )
2055 add_action( 'rl_before_gallery', array( $this, 'do_pagination' ), 10, 2 );
2056 elseif ( $args['pagination_position'] === 'bottom' )
2057 add_action( 'rl_after_gallery', array( $this, 'do_pagination' ), 10, 2 );
2058 else {
2059 add_action( 'rl_before_gallery', array( $this, 'do_pagination' ), 10, 2 );
2060 add_action( 'rl_after_gallery', array( $this, 'do_pagination' ), 10, 2 );
2061 }
2062 }
2063
2064 return apply_filters( 'rl_get_gallery_images', array_values( $images ), $gallery_id, $args );
2065 }
2066
2067 /**
2068 *
2069 */
2070 public function do_pagination( $args, $gallery_id ) {
2071 global $wp;
2072
2073 // get current action
2074 $current_action = current_action();
2075
2076 if ( $current_action === 'rl_before_gallery' )
2077 $class = ' rl-pagination-top';
2078 elseif ( $current_action === 'rl_after_gallery' )
2079 $class = ' rl-pagination-bottom';
2080 else
2081 $class = '';
2082
2083 echo
2084 '<div class="rl-pagination' . $class . '"' . ( $args['pagination_type'] === 'infinite' ? ' data-button="' . $args['load_more'] . '"' : '' ) .'>' .
2085 paginate_links(
2086 array(
2087 'format' => '?rl_page=%#%',
2088 'base' => add_query_arg( array( 'rl_gallery_no' => Responsive_Lightbox()->frontend->gallery_no, 'rl_page' => '%#%' ), $args['pagination_type'] !== 'paged' ? get_permalink( $gallery_id ) : home_url( $wp->request ) ),
2089 'total' => $this->gallery_args['total'],
2090 'current' => $this->gallery_args['page'],
2091 'show_all' => false,
2092 'end_size' => 1,
2093 'mid_size' => 2,
2094 'prev_next' => true,
2095 'prev_text' => __( '&laquo; Previous', 'responsive-lightbox' ),
2096 'next_text' => __( 'Next &raquo;', 'responsive-lightbox' ),
2097 'type' => 'plain',
2098 // 'add_args' => array( 'rl_gallery_no' => Responsive_Lightbox()->frontend->gallery_no ),
2099 'add_args' => '',
2100 'add_fragment' => '',
2101 'before_page_number' => '',
2102 'after_page_number' => ''
2103 )
2104 ) .
2105 '</div>' . ( $args['pagination_type'] === 'infinite' && $args['load_more'] === 'manually' ? '<div class="rl-gallery-button"><button class="rl-button rl-load-more">' . esc_html__( 'Load more', 'responsive-lightbox' ) . '</button></div>' : '' );
2106 }
2107
2108 /**
2109 * Get gallery attachments.
2110 *
2111 * @return string Encoded JSON with HTML content
2112 */
2113 public function get_gallery_page( $args ) {
2114 if ( isset( $_POST['gallery_id'], $_POST['page'], $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'rl_nonce' ) ) {
2115 $_GET['rl_page'] = (int) $_POST['page'];
2116
2117 echo $this->gallery_shortcode( array( 'id' => (int) $_POST['gallery_id'] ) );
2118 }
2119
2120 exit;
2121 }
2122
2123 /**
2124 * Get gallery attachments.
2125 *
2126 * @return string Encoded JSON with HTML content
2127 */
2128 public function post_gallery_preview() {
2129 if ( ! isset( $_POST['post_id'], $_POST['gallery_id'], $_POST['nonce'] ) || ! check_ajax_referer( 'rl-gallery-post', 'nonce', false ) || ! current_user_can( 'edit_post', (int) $_POST['post_id'] ) )
2130 wp_send_json_error();
2131
2132 // parse gallery ID
2133 $gallery_id = (int) $_POST['gallery_id'];
2134
2135 // get gallery data
2136 $data = get_post_meta( $gallery_id, '_rl_images', true );
2137
2138 // prepare data
2139 $attachments = $exclude = array();
2140 $html = '';
2141
2142 $images = $this->get_gallery_images( $gallery_id, array( 'exclude' => true ) );
2143
2144 // count attachments
2145 $images_count = (int) get_post_meta( $gallery_id, '_rl_images_count', true );
2146
2147 if ( ! empty( $images ) ) {
2148 foreach ( $images as $image ) {
2149 $html .= '
2150 <li role="checkbox" aria-label="" aria-checked="false" data-id="' . $image['id'] . '" class="attachment save-ready rl-status-active' . '">
2151 <div class="attachment-preview js--select-attachment type-image landscape">
2152 <div class="thumbnail">
2153 <div class="centered">
2154 <img src="' . $image['thumbnail_url'] . '" draggable="false" alt="" />
2155 </div>
2156 </div>
2157 </div>
2158 </li>';
2159 }
2160 }
2161
2162 // send attachments content
2163 wp_send_json_success(
2164 array(
2165 'attachments' => $html,
2166 'count' => sprintf( _n( '%s image', '%s images', $images_count, 'responsive-lightbox' ), $images_count ),
2167 'edit_url' => current_user_can( 'edit_post', $gallery_id ) ? admin_url( 'post.php?post=' . $gallery_id . '&action=edit' ): ''
2168 )
2169 );
2170 }
2171
2172 /**
2173 * Get all galleries.
2174 *
2175 * @return mixed JSON encoded HTML content
2176 */
2177 public function post_get_galleries() {
2178 if ( ! isset( $_POST['post_id'], $_POST['search'], $_POST['nonce'] ) || ! check_ajax_referer( 'rl-gallery-post', 'nonce', false ) || ! current_user_can( 'edit_post', $post_id = (int) $_POST['post_id'] ) )
2179 wp_send_json_error();
2180
2181 $args = array(
2182 'post_type' => 'rl_gallery',
2183 'post_status' => 'publish',
2184 'nopaging' => true,
2185 'posts_per_page' => -1,
2186 'orderby' => 'title',
2187 'order' => 'ASC',
2188 'suppress_filters' => false,
2189 'no_found_rows' => true,
2190 'cache_results' => false
2191 );
2192
2193 // specific category?
2194 if ( ! empty( $_POST['category'] ) ) {
2195 $args['tax_query'] = array(
2196 array(
2197 'taxonomy' => 'rl_category',
2198 'field' => 'term_id',
2199 'operator' => 'IN',
2200 'include_children' => false,
2201 'terms' => (int) $_POST['category']
2202 )
2203 );
2204 }
2205
2206 $search = wp_unslash( trim( $_POST['search'] ) );
2207
2208 if ( $search !== '' )
2209 $args['s'] = $search;
2210
2211 // get galleries
2212 $query = new WP_Query( $args );
2213
2214 $html = '';
2215
2216 if ( ! empty( $query->posts ) ) {
2217 foreach ( $query->posts as $gallery ) {
2218 // get title
2219 $title = $gallery->post_title !== '' ? $gallery->post_title : __( '(no title)', 'responsive-gallery' );
2220
2221 $html .= '
2222 <li role="checkbox" aria-label="' . esc_attr( $title ) . '" aria-checked="false" data-id="' . $gallery->ID . '" class="attachment save-ready">
2223 <div class="attachment-preview js--select-attachment type-image landscape">
2224 <div class="thumbnail">
2225 <div class="centered">
2226 ' . $this->get_featured_image( $gallery->ID ) . '
2227 </div>
2228 <div class="filename">
2229 <div>' . esc_html( $title ) . '</div>
2230 </div>
2231 </div>
2232 </div>
2233 <button type="button" class="button-link check"><span class="media-modal-icon"></span><span class="screen-reader-text">' . __( 'Deselect', 'responsive-lightbox' ) . '</span></button>
2234 </li>';
2235 }
2236 }
2237
2238 // send galleries content
2239 wp_send_json_success( $html );
2240 }
2241
2242 /**
2243 * Get gallery content based on request.
2244 *
2245 * @return string Encoded JSON with HTML content
2246 */
2247 public function get_menu_content() {
2248 if ( ! isset( $_POST['post_id'], $_POST['tab'], $_POST['menu_item'], $_POST['nonce'] ) || ! check_ajax_referer( 'rl-gallery', 'nonce', false ) || ! current_user_can( 'edit_post', $post_id = (int) $_POST['post_id'] ) || ! array_key_exists( $_POST['tab'], $this->tabs ) )
2249 wp_send_json_error();
2250
2251 // get selected menu item
2252 $menu_item = ! empty( $_POST['menu_item'] ) && in_array( $_POST['menu_item'], array_keys( $this->tabs[$_POST['tab']]['menu_items'] ) ) ? esc_attr( $_POST['menu_item'] ) : key( $this->tabs[$_POST['tab']]['menu_items'] );
2253
2254 // get tab content
2255 wp_send_json_success( $this->get_metabox_content( $_POST['tab'], get_post_meta( $post_id, '_rl_' . $_POST['tab'], true ), $menu_item, $post_id ) );
2256 }
2257
2258 /**
2259 * Get gallery preview content based on request.
2260 *
2261 * @return string Encoded JSON with HTML content
2262 */
2263 public function get_gallery_preview_content() {
2264 // initial checks
2265 if ( ! isset( $_POST['post_id'], $_POST['menu_item'], $_POST['nonce'] ) || ! check_ajax_referer( 'rl-gallery', 'nonce', false ) )
2266 wp_send_json_error();
2267
2268 // cast gallery ID
2269 $post_id = (int) $_POST['post_id'];
2270
2271 // check user privileges
2272 if ( ! current_user_can( 'edit_post', $post_id ) || ! current_user_can( 'upload_files' ) )
2273 wp_send_json_error();
2274
2275 // get query args
2276 $args = ! empty( $_POST['query'] ) ? wp_unslash( $_POST['query'] ) : array();
2277
2278 // check orderby
2279 if ( array_key_exists( 'orderby', $args ) ) {
2280 $args['post_orderby'] = $args['orderby'];
2281
2282 unset( $args['orderby'] );
2283 }
2284
2285 // check order
2286 if ( array_key_exists( 'order', $args ) ) {
2287 $args['post_order'] = $args['order'];
2288
2289 unset( $args['order'] );
2290 }
2291
2292 // check for POST menu item
2293 $menu_item = ! empty( $_POST['menu_item'] ) && is_string( $_POST['menu_item'] ) ? esc_attr( $_POST['menu_item'] ) : '';
2294
2295 // set images menu item
2296 $menu_item = $this->menu_item = ! empty( $menu_item ) && array_key_exists( $menu_item, $this->tabs['images']['menu_items'] ) ? $menu_item : 'media';
2297
2298 // get images
2299 $images = $this->get_gallery_images( $post_id, $args );
2300
2301 // parse excluded images
2302 $excluded = ! empty( $_POST['excluded'] ) ? $_POST['excluded'] : array();
2303
2304 // get excluded images
2305 if ( $excluded )
2306 $excluded = array_unique( array_filter( array_map( 'intval', $excluded ) ) );
2307
2308 // get media item template
2309 $media_item_template = $this->media_item_template;
2310
2311 // build html
2312 $html = '';
2313
2314 if ( ! empty( $images ) ) {
2315 foreach ( $images as $image ) {
2316 $image_html = $this->get_gallery_image( $image, 'thumbnail' );
2317
2318 // replace ID and URL of an image
2319 $html .= str_replace( '__IMAGE__', '<input type="hidden" class="rl-gallery-exclude" name="rl_gallery[images][' . $menu_item . '][attachments][exclude][]" value="' . ( in_array( $image['id'], $excluded, true ) ? $image['id'] : '' ) . '" />' . $image_html, str_replace( '__IMAGE_ID__', $image['id'], str_replace( '__IMAGE_STATUS__', in_array( $image['id'], $excluded, true ) ? 'rl-status-inactive' : 'rl-status-active', $media_item_template ) ) );
2320 }
2321 }
2322
2323 // send JSON encoded HTML
2324 wp_send_json_success( $html );
2325 }
2326
2327 /**
2328 * Get gallery image.
2329 * Based on wp_get_attachment_image() function.
2330 *
2331 * @param array $image
2332 * @param string $size
2333 * @param array attr
2334 * @return array
2335 */
2336 public function get_gallery_image( $image = array(), $size = 'thumbnail', $attr = '' ) {
2337 $html = '';
2338
2339 $image = $this->get_gallery_image_src( $image, $size );
2340
2341 if ( ! empty( $image['url'] ) || ! empty( $image['id'] ) ) {
2342 $size_class = $size;
2343
2344 if ( is_array( $size_class ) )
2345 $size_class = join( 'x', $size_class );
2346
2347 // set parameters
2348 $width = $image['width'];
2349 $height = $image['height'];
2350
2351 $default_attr = array(
2352 'src' => $image['url'],
2353 'class' => 'attachment-' . $size_class . ' size-' . $size_class . ( $height > $width ? ' format-portrait' : ' format-landscape' ),
2354 'alt' => $image['alt']
2355 );
2356
2357 $attr = wp_parse_args( $attr, $default_attr );
2358 $attr = apply_filters( 'rl_get_gallery_image_attributes', $attr, $image, $size );
2359 $attr = array_map( 'esc_attr', $attr );
2360
2361 $html = rtrim( '<img ' . image_hwstring( $width, $height ) );
2362
2363 foreach ( $attr as $name => $value ) {
2364 $html .= ' ' . $name . '="' . $value . '"';
2365 }
2366
2367 $html .= ' />';
2368 }
2369
2370 return apply_filters( 'rl_get_gallery_image', $html, $image, $size );
2371 }
2372
2373 /**
2374 * Get attachment image.
2375 *
2376 * @param int|string|array $image attachment_id, image url or array of image data
2377 * @param string $image_size
2378 * @param string $thumbnail_size
2379 * @return array
2380 */
2381 public function get_gallery_image_src( $image, $image_size = 'large', $thumbnail_size = 'thumbnail' ) {
2382 $imagedata = array();
2383
2384 // attachment id
2385 if ( is_int( $image ) ) {
2386 $image_src = wp_get_attachment_image_src( $image, $image_size, false );
2387
2388 if ( $image && wp_attachment_is_image( $image ) ) {
2389 if ( $thumbnail_size !== $image_size )
2390 $thumbnail_src = wp_get_attachment_image_src( $image, $thumbnail_size, false );
2391 else
2392 $thumbnail_src = $image_src;
2393
2394 $imagedata = array(
2395 'id' => $image,
2396 'link' => '',
2397 'title' => get_the_title( $image ),
2398 'date' => get_the_date( 'Y-m-d H:i:s', $image ),
2399 'caption' => '',
2400 'alt' => '',
2401 'url' => $image_src[0],
2402 'width' => $image_src[1],
2403 'height' => $image_src[2],
2404 'thumbnail_url' => $thumbnail_src[0],
2405 'thumbnail_width' => $thumbnail_src[1],
2406 'thumbnail_height' => $thumbnail_src[2]
2407 );
2408 }
2409 // image url
2410 } elseif ( is_string( $image ) ) {
2411 $imagedata['url'] = $image;
2412
2413 @list( $imagedata['width'], $imagedata['height'] ) = rl_get_image_size_by_url( $imagedata['url'] );
2414
2415 $imagedata = array(
2416 'id' => 0,
2417 'link' => '',
2418 'title' => '',
2419 'caption' => '',
2420 'alt' => '',
2421 'url' => $imagedata['url'],
2422 'width' => $imagedata['width'],
2423 'height' => $imagedata['height'],
2424 'thumbnail_url' => $imagedata['url'],
2425 'thumbnail_width' => $imagedata['width'],
2426 'thumbnail_height' => $imagedata['height']
2427 );
2428 } elseif ( is_array( $image ) ) {
2429 // set width and height from url, if not available
2430 if ( empty( $image['width'] ) || empty( $image['height'] ) )
2431 @list( $image['width'], $image['height'] ) = rl_get_image_size_by_url( $image['url'] );
2432
2433 // set thumbnail data, if not available
2434 if ( empty( $image['thumbnail_url'] ) ) {
2435 $image['thumbnail_url'] = $image['url'];
2436 $image['thumbnail_width'] = $image['width'];
2437 $image['thumbnail_height'] = $image['height'];
2438 } else {
2439 // set thumbnail width and height from url, if not available
2440 if ( empty( $image['thumbnail_width'] ) || empty( $image['thumbnail_height'] ) )
2441 @list( $image['thumbnail_width'], $image['thumbnail_height'] ) = rl_get_image_size_by_url( $image['thumbnail_url'] );
2442 }
2443
2444 /* adjust thumbnail size ?
2445 $image_size_data = $this->get_image_size( $image_size );
2446 $thumbnail_size_data = $this->get_image_size( $thumbnail_size );
2447
2448 if ( $thumbnail_size_data && $image['thumbnail_width'] < $thumbnail_size_data['width'] && $image['thumbnail_height'] < $thumbnail_size_data['height'] ) {
2449 $image['thumbnail_url'] = $image['url'];
2450 $image['thumbnail_width'] = $image['width'];
2451 $image['thumbnail_height'] = $image['height'];
2452 }
2453 */
2454
2455 $imagedata = array(
2456 'id' => ! empty( $image['id'] ) ? (int) $image['id'] : 0,
2457 'link' => ! empty( $image['link'] ) ? esc_url( $image['link'] ) : '',
2458 'title' => ! empty( $image['title'] ) ? esc_html( $image['title'] ) : '',
2459 'caption' => ! empty( $image['caption'] ) ? esc_html( $image['caption'] ) : '',
2460 'alt' => ! empty( $image['alt'] ) ? esc_html( $image['alt'] ) : '',
2461 'url' => ! empty( $image['url'] ) ? esc_url( $image['url'] ) : '',
2462 'width' => ! empty( $image['width'] ) ? (int) $image['width'] : 0,
2463 'height' => ! empty( $image['height'] ) ? (int) $image['height'] : 0,
2464 'thumbnail_url' => ! empty( $image['thumbnail_url'] ) ? esc_url( $image['thumbnail_url'] ) : '',
2465 'thumbnail_width' => ! empty( $image['thumbnail_width'] ) ? (int) $image['thumbnail_width'] : 0,
2466 'thumbnail_height' => ! empty( $image['thumbnail_height'] ) ? (int) $image['thumbnail_height'] : 0,
2467 );
2468 }
2469
2470 return apply_filters( 'rl_get_gallery_image_src', $imagedata, $image, $image_size, $thumbnail_size );
2471 }
2472
2473 /**
2474 * Get gallery featured image.
2475 *
2476 * @param int $gallery_id
2477 * @param $size array
2478 * @param attr array
2479 * @return array
2480 */
2481 public function get_featured_image( $gallery_id, $size = 'thumbnail', $attr = '' ) {
2482 $html = '';
2483 $image = $this->get_featured_image_src( $gallery_id );
2484
2485 if ( $image ) {
2486 $html = $this->get_gallery_image( $image, $size, $attr );
2487 }
2488
2489 return apply_filters( 'rl_get_featured_image', $html, $gallery_id, $size );
2490 }
2491
2492 /**
2493 * Get gallery featured image data.
2494 *
2495 * @param int $gallery_id
2496 * @return array
2497 */
2498 public function get_featured_image_src( $gallery_id ) {
2499 // get featured image data
2500 $featured_image_type = get_post_meta( $gallery_id, '_rl_featured_image_type', true );
2501 $featured_image = get_post_meta( $gallery_id, '_rl_featured_image', true );
2502
2503 switch ( $featured_image_type ) {
2504 case 'url':
2505 $image = esc_url( $featured_image );
2506 break;
2507
2508 case 'id':
2509 $image = (int) $featured_image;
2510 break;
2511
2512 case 'image':
2513 default:
2514 // get gallery images
2515 $images = $this->get_gallery_images( $gallery_id, array( 'exclude' => true ) );
2516
2517 // set image data
2518 if ( $images )
2519 $image = reset( $images );
2520 else
2521 $image = 0;
2522 }
2523
2524 // return only the first image
2525 return apply_filters( 'rl_get_featured_image_src', $image, $gallery_id, $featured_image_type, $featured_image );
2526 }
2527
2528 /**
2529 * Get featured gallery attachments.
2530 *
2531 * @param array $args Query arguments
2532 * @return array Attachment IDs
2533 */
2534 public function gallery_query( $args ) {
2535 $attachments = array();
2536
2537 // get fields
2538 $fields = $this->fields['images']['featured'];
2539
2540 // force these settings
2541 $args['fields'] = 'ids';
2542 $args['tax_query'] = array();
2543 $args['meta_query'] = array();
2544 $args['author__in'] = array();
2545 $args['post_parent__in'] = array();
2546
2547 // get image source
2548 $args['image_source'] = isset( $args['image_source'] ) && array_key_exists( $args['image_source'], $fields['image_source']['options'] ) ? $args['image_source'] : $fields['image_source']['default'];
2549 // get images per post
2550 $args['images_per_post'] = isset( $args['images_per_post'] ) ? absint( $args['images_per_post'] ) : $fields['images_per_post']['default'];
2551
2552 $args['number_of_posts'] = isset( $args['number_of_posts'] ) ? (int) $args['number_of_posts'] : $fields['number_of_posts']['default'];
2553
2554 // get all posts?
2555 if ( $args['number_of_posts'] <= 0 )
2556 $args['number_of_posts'] = -1;
2557
2558 // convert to query arg
2559 $args['posts_per_page'] = $args['number_of_posts'];
2560
2561 $args['order'] = isset( $args['order'] ) && array_key_exists( $args['order'], $fields['order']['options'] ) ? $args['order'] : $fields['order']['default'];
2562 $args['orderby'] = isset( $args['orderby'] ) && array_key_exists( $args['orderby'], $fields['orderby']['options'] ) ? $args['orderby'] : $fields['orderby']['default'];
2563 $args['offset'] = isset( $args['offset'] ) ? absint( $args['offset'] ) : 0;
2564
2565 $tax_queries = array(
2566 'post_format' => array(),
2567 'post_term' => array()
2568 );
2569
2570 $meta_queries = array(
2571 'page_template' => array(),
2572 'image_source' => array()
2573 );
2574
2575 // post type
2576 if ( ! empty( $args['post_type'] ) ) {
2577 // assign post types
2578 $post_types = $args['post_type'];
2579
2580 // clear post types
2581 $args['post_type'] = array();
2582
2583 foreach ( $post_types as $post_type ) {
2584 if ( array_key_exists( $post_type, $fields['post_type']['options'] ) )
2585 $args['post_type'][] = $post_type;
2586 }
2587 } else
2588 $args['post_type'] = 'any';
2589
2590 // post status
2591 if ( ! empty( $args['post_status'] ) ) {
2592 // assign post statuses
2593 $post_statuses = $args['post_status'];
2594
2595 // clear post statuses
2596 $args['post_status'] = array();
2597
2598 foreach ( $post_statuses as $post_status ) {
2599 if ( array_key_exists( $post_status, $fields['post_status']['options'] ) )
2600 $args['post_status'][] = $post_status;
2601 }
2602 }
2603
2604 // post format
2605 if ( ! empty( $args['post_format'] ) ) {
2606 // assign post formats
2607 $post_formats = $args['post_format'];
2608
2609 foreach ( $post_formats as $post_format ) {
2610 if ( array_key_exists( $post_format, $fields['post_format']['options'] ) ) {
2611 // standard format?
2612 if ( $post_format === 'standard' ) {
2613 $tax_queries['post_format'][] = array(
2614 'relation' => 'OR',
2615 array(
2616 'taxonomy' => 'post_format',
2617 'field' => 'slug',
2618 'terms' => array( 'post-format-standard' )
2619 ),
2620 array(
2621 'taxonomy' => 'post_format',
2622 'field' => 'slug',
2623 'operator' => 'NOT EXISTS'
2624 )
2625 );
2626 } else {
2627 $tax_queries['post_format'][] = array(
2628 'taxonomy' => 'post_format',
2629 'field' => 'slug',
2630 'terms' => array( 'post-format-' . $post_format )
2631 );
2632 }
2633 }
2634 }
2635
2636 unset( $args['post_format'] );
2637 }
2638
2639 // page template
2640 if ( ! empty( $args['page_template'] ) ) {
2641 foreach ( $args['page_template'] as $page_template ) {
2642 if ( array_key_exists( $page_template, $fields['page_template']['options'] ) ) {
2643 if ( $page_template === 'default' ) {
2644 $meta_queries['page_template'][] = array(
2645 'relation' => 'OR',
2646 array(
2647 'key' => '_wp_page_template',
2648 'value' => 'default'
2649 ),
2650 array(
2651 'key' => '_wp_page_template',
2652 'value' => ''
2653 ),
2654 array(
2655 'key' => '_wp_page_template',
2656 'compare' => 'NOT EXISTS'
2657 )
2658 );
2659 } else {
2660 $meta_queries['page_template'][] = array(
2661 'key' => '_wp_page_template',
2662 'value' => $page_template
2663 );
2664 }
2665 }
2666 }
2667 }
2668
2669 // post author
2670 if ( ! empty( $args['post_author'] ) ) {
2671 foreach ( $args['post_author'] as $post_author ) {
2672 if ( array_key_exists( $post_author, $fields['post_author']['options'] ) )
2673 $args['author__in'][] = $post_author;
2674 }
2675 }
2676
2677 // page parent
2678 if ( ! empty( $args['page_parent'] ) ) {
2679 foreach ( $args['page_parent'] as $page_parent ) {
2680 if ( array_key_exists( $page_parent, $fields['page_parent']['options'] ) )
2681 $args['post_parent__in'][] = $page_parent;
2682 }
2683 }
2684
2685 // post term
2686 if ( ! empty( $args['post_term'] ) ) {
2687 foreach ( $args['post_term'] as $post_term ) {
2688 if ( array_key_exists( $post_term, $fields['post_term']['options'] ) ) {
2689 $term = get_term( $post_term );
2690
2691 $tax_queries['post_term'][] = array(
2692 'taxonomy' => $term->taxonomy,
2693 'field' => 'term_id',
2694 'terms' => (int) $post_term
2695 );
2696 }
2697 }
2698 }
2699
2700 switch ( $args['image_source'] ) {
2701 case 'thumbnails':
2702 $meta_queries['image_source'][] = array(
2703 'relation' => 'OR',
2704 array(
2705 'key' => '_thumbnail_id',
2706 'compare' => 'EXISTS'
2707 )
2708 );
2709 }
2710
2711 // any tax queries?
2712 if ( ! empty( $tax_queries['post_term'] ) || ! empty( $tax_queries['post_format'] ) ) {
2713 $args['tax_query'] = array( 'relation' => 'AND' );
2714
2715 if ( ! empty( $tax_queries['post_term'] ) )
2716 $args['tax_query'][] = array( 'relation' => 'OR' ) + $tax_queries['post_term'];
2717
2718 if ( ! empty( $tax_queries['post_format'] ) )
2719 $args['tax_query'][] = array( 'relation' => 'OR' ) + $tax_queries['post_format'];
2720 }
2721
2722 // any tax queries?
2723 if ( ! empty( $meta_queries['page_template'] ) || ! empty( $meta_queries['image_source'] ) ) {
2724 $args['meta_query'] = array( 'relation' => 'AND' );
2725
2726 if ( ! empty( $meta_queries['page_template'] ) )
2727 $args['meta_query'][] = array( 'relation' => 'OR', $meta_queries['page_template'] );
2728
2729 if ( ! empty( $meta_queries['image_source'] ) )
2730 $args['meta_query'][] = array( 'relation' => 'OR', $meta_queries['image_source'] );
2731 }
2732
2733 // get posts
2734 $query = new WP_Query( apply_filters( 'rl_gallery_query_args', $args ) );
2735
2736 // get attachments
2737 if ( $query->have_posts() )
2738 $attachments = $this->get_gallery_query_attachments( $query->posts, $args );
2739
2740 return $attachments;
2741 }
2742
2743 /**
2744 * Get query attachments.
2745 *
2746 * @param array $posts Post ids, array or objects
2747 * @param array $args Additional arguments
2748 * @return array
2749 */
2750 public function get_gallery_query_attachments( $posts, $args ) {
2751 $attachments = array();
2752
2753 // any posts?
2754 if ( ! empty( $posts ) ) {
2755 switch ( $args['image_source'] ) {
2756 case 'thumbnails':
2757 foreach ( $posts as $post_id ) {
2758 $attachment_id = (int) get_post_thumbnail_id( $post_id );
2759
2760 if ( ! empty( $attachment_id ) ) {
2761 $attachments[] = $attachment_id;
2762 }
2763 }
2764
2765 array_unique( $attachments );
2766 break;
2767
2768 case 'attached_images':
2769 foreach ( $posts as $post_id ) {
2770 // get attached images, do not use get_attached_media here!
2771 $attachment_ids = (array) get_children(
2772 array(
2773 'post_parent' => $post_id,
2774 'post_status' => 'inherit',
2775 'post_type' => 'attachment',
2776 'post_mime_type' => 'image',
2777 'posts_per_page' => $args['images_per_post'],
2778 'order' => 'ASC',
2779 'orderby' => 'menu_order',
2780 'nopaging' => false,
2781 'page' => 1,
2782 'fields' => 'ids'
2783 )
2784 );
2785
2786 if ( $attachment_ids ) {
2787 foreach ( $attachment_ids as $attachment_id ) {
2788 if ( ! empty( $attachment_id ) ) {
2789 $attachments[] = $attachment_id;
2790 }
2791 }
2792 }
2793 }
2794
2795 array_unique( $attachments );
2796 }
2797 }
2798
2799 return apply_filters( 'rl_get_gallery_query_attachments', $attachments, $posts, $args );
2800 }
2801
2802 /**
2803 * Load featured content query args.
2804 *
2805 * @return void
2806 */
2807 public function init_admin() {
2808 global $pagenow;
2809
2810 // prepare query arguments if needed
2811 if ( ( $pagenow === 'post.php' && ( ( isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) === 'rl_gallery' ) || ( isset( $_POST['post_ID'] ) && get_post_type( $_POST['post_ID'] ) === 'rl_gallery' ) ) ) || ( in_array( $pagenow, array( 'edit.php', 'post-new.php'), true ) && isset( $_GET['post_type'] ) && $_GET['post_type'] === 'rl_gallery' ) || ( $pagenow === 'admin-ajax.php' && isset( $_POST['action'] ) && in_array( $_POST['action'], array( 'rl-get-preview-content', 'rl-post-gallery-preview', 'rl-get-menu-content' ), true ) ) ) {
2812 foreach ( array( 'post_type', 'post_status', 'post_format', 'post_term', 'post_author', 'page_parent', 'page_template' ) as $option ) {
2813 $this->fields['images']['featured'][$option]['options'] = $this->prepare_query_args( $option );
2814 }
2815 }
2816
2817 // add default thumbnail image if needed
2818 if ( Responsive_Lightbox()->options['builder']['gallery_builder'] && $pagenow === 'edit.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] === 'rl_gallery' )
2819 $this->maybe_generate_thumbnail();
2820 }
2821
2822 /**
2823 * Generate post thumbnail replacement.
2824 */
2825 public function maybe_generate_thumbnail() {
2826 // get attachment
2827 $thumbnail_id = get_posts(
2828 array(
2829 'name' => 'responsive-lightbox-thumbnail',
2830 'post_type' => 'attachment',
2831 'post_status' => 'inherit',
2832 'numberposts' => 1,
2833 'fields' => 'ids'
2834 )
2835 );
2836
2837 // no attachment?
2838 if ( empty( $thumbnail_id ) ) {
2839 // get upload directory data
2840 $wp_upload_dir = wp_upload_dir();
2841
2842 // get file path
2843 $filepath = str_replace( '\\', '/', RESPONSIVE_LIGHTBOX_PATH . 'images/responsive-lightbox-thumbnail.png' );
2844
2845 // get file name
2846 $filename = basename( $filepath );
2847
2848 // new filepath in upload dir
2849 $new_filepath = $wp_upload_dir['path'] . '/' . $filename;
2850
2851 // copty file to upload dir
2852 copy( $filepath, $new_filepath );
2853
2854 // get type of file
2855 $filetype = wp_check_filetype( $filename );
2856
2857 // insert attachment
2858 $thumbnail_id = wp_insert_attachment(
2859 array(
2860 'guid' => $wp_upload_dir['url'] . '/' . $filename,
2861 'post_mime_type' => $filetype['type'],
2862 'post_title' => preg_replace( '/\.[^.]+$/', '', $filename ),
2863 'post_content' => '',
2864 'post_parent' => 0,
2865 'post_status' => 'inherit'
2866 ),
2867 $new_filepath,
2868 0
2869 );
2870
2871 // success?
2872 if ( $thumbnail_id ) {
2873 // make sure that this file is included
2874 require_once( ABSPATH . 'wp-admin/includes/image.php' );
2875
2876 // update database with generated metadata for the attachment
2877 wp_update_attachment_metadata( $thumbnail_id, wp_generate_attachment_metadata( $thumbnail_id, $new_filepath ) );
2878 }
2879 } else
2880 $thumbnail_id = $thumbnail_id[0];
2881
2882 return (int) $thumbnail_id;
2883 }
2884
2885 /**
2886 * Prepare values option list.
2887 *
2888 * @param string $type Value type
2889 * @param string $selected Selected value
2890 * @return string Generated HTML
2891 */
2892 public function prepare_query_args( $type = '' ) {
2893 $html = '';
2894
2895 switch( $type ) {
2896 case 'post_type':
2897 $data = $this->get_post_types();
2898 break;
2899
2900 case 'post_status':
2901 $data = $this->get_post_statuses();
2902 break;
2903
2904 case 'post_format':
2905 $data = $this->get_post_formats();
2906 break;
2907
2908 case 'post_term':
2909 $taxonomies = $this->get_taxonomies();
2910 $new_terms = array();
2911
2912 if ( ! empty( $taxonomies ) ) {
2913 foreach ( $taxonomies as $tax_id => $label ) {
2914 $terms = get_terms(
2915 array(
2916 'taxonomy' => $tax_id,
2917 'orderby' => 'name',
2918 'order' => 'ASC',
2919 'hide_empty' => false,
2920 'fields' => 'id=>name'
2921 )
2922 );
2923
2924 if ( ! empty( $terms ) )
2925 $new_terms[$tax_id] = array(
2926 'label' => $label,
2927 'terms' => $terms
2928 );
2929 }
2930 }
2931
2932 $data = $new_terms;
2933 break;
2934
2935 case 'post_author':
2936 $data = $this->get_users();
2937 break;
2938
2939 case 'page_parent':
2940 $parents = array();
2941 $hierarchical = get_post_types(
2942 array(
2943 'public' => true,
2944 'hierarchical' => true
2945 ),
2946 'objects',
2947 'and'
2948 );
2949
2950 if ( ! empty( $hierarchical ) ) {
2951 foreach ( $hierarchical as $post_type => $object ) {
2952 // get top level hierarchical posts
2953 $query = new WP_Query(
2954 array(
2955 'post_type' => $post_type,
2956 'post_status' => 'publish',
2957 'nopaging' => true,
2958 'posts_per_page' => -1,
2959 'orderby' => 'title',
2960 'order' => 'ASC',
2961 'suppress_filters' => false,
2962 'no_found_rows' => true,
2963 'cache_results' => false,
2964 'post_parent' => 0
2965 )
2966 );
2967
2968 if ( ! empty( $query->posts ) ) {
2969 foreach ( $query->posts as $post ) {
2970 $parents[$post->ID] = trim( $post->post_title ) === '' ? __( 'Untitled' ) : $post->post_title;
2971 }
2972 }
2973 }
2974 }
2975
2976 $data = $parents;
2977 break;
2978
2979 case 'page_template':
2980 $data = $this->get_page_templates();
2981 break;
2982
2983 default:
2984 $data = array();
2985 }
2986
2987 return apply_filters( 'rl_galleries_prepare_query_args', $data, $type );
2988 }
2989
2990 /**
2991 * Get public post types.
2992 *
2993 * @return array Post types
2994 */
2995 public function get_post_types() {
2996 $post_types = get_post_types(
2997 array(
2998 'public' => true
2999 ),
3000 'objects',
3001 'and'
3002 );
3003
3004 $data = array();
3005
3006 if ( ! empty( $post_types ) ) {
3007 foreach ( $post_types as $post_type => $cpt ) {
3008 // skip gallery and attachment post types
3009 if ( $post_type === 'rl_gallery' || $post_type === 'attachment' )
3010 continue;
3011
3012 $data[$post_type] = $cpt->labels->singular_name;
3013 }
3014 }
3015
3016 asort( $data );
3017
3018 return $data;
3019 }
3020
3021 /**
3022 * Get post statuses.
3023 *
3024 * @return array Post statuses
3025 */
3026 public function get_post_statuses() {
3027 $post_statuses = get_post_stati();
3028
3029 asort( $post_statuses );
3030
3031 // remove inherit post status
3032 if ( isset( $post_statuses['inherit'] ) )
3033 unset( $post_statuses['inherit'] );
3034
3035 return $post_statuses;
3036 }
3037
3038 /**
3039 * Get post formats.
3040 *
3041 * @return array Post formats
3042 */
3043 public function get_post_formats() {
3044 $post_formats = array(
3045 'aside' => __( 'Aside' ),
3046 'audio' => __( 'Audio' ),
3047 'chat' => __( 'Chat' ),
3048 'gallery' => __( 'Gallery' ),
3049 'link' => __( 'Link' ),
3050 'photo' => __( 'Photo' ),
3051 'quote' => __( 'Quote' ),
3052 'standard' => __( 'Standard' ),
3053 'status' => __( 'Status' ),
3054 'video' => __( 'Video' )
3055 );
3056
3057 asort( $post_formats );
3058
3059 return $post_formats;
3060 }
3061
3062 /**
3063 * Get
3064 */
3065 public function get_taxonomies() {
3066 $taxonomies = get_taxonomies(
3067 array(
3068 'public' => true
3069 ),
3070 'objects',
3071 'and'
3072 );
3073
3074 // remove post format
3075 if ( array_key_exists( 'post_format', $taxonomies ) )
3076 unset( $taxonomies['post_format'] );
3077
3078 $data = array();
3079
3080 if ( ! empty( $taxonomies ) ) {
3081 foreach ( $taxonomies as $tax_id => $taxonomy ) {
3082 $data[$tax_id] = $taxonomy->labels->singular_name;
3083 }
3084 }
3085
3086 asort( $data );
3087
3088 return $data;
3089 }
3090
3091 /**
3092 * Get users.
3093 *
3094 * @return array Users
3095 */
3096 public function get_users() {
3097 $users = get_users(
3098 array(
3099 'fields' => array( 'ID', 'user_login' )
3100 )
3101 );
3102
3103 $data = array();
3104
3105 if ( ! empty( $users ) ) {
3106 foreach ( $users as $user ) {
3107 $data[(int) $user->ID] = $user->user_login;
3108 }
3109 }
3110
3111 asort( $data );
3112
3113 return $data;
3114 }
3115
3116 /**
3117 * Get page templates.
3118 *
3119 * @return array Page templates
3120 */
3121 public function get_page_templates() {
3122 $data = array();
3123 $page_templates = wp_get_theme()->get_page_templates();
3124
3125 if ( ! empty( $page_templates ) ) {
3126 $page_templates = array_flip( $page_templates );
3127
3128 asort( $page_templates );
3129 }
3130
3131 $data = array_merge( array( 'default' => apply_filters( 'default_page_template_title', __( 'Default Template' ) ) ), $page_templates );
3132
3133 return $data;
3134 }
3135
3136 /**
3137 * Save gallery metadata.
3138 *
3139 * @param int $post_id Post ID
3140 * @param object $post Post object
3141 * @param bool $update Whether this is an existing post being updated or not
3142 * @return void
3143 */
3144 public function save_post( $post_id, $post, $update ) {
3145 if ( wp_is_post_revision( $post_id ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! $update || in_array( $post->post_status, array( 'trash', 'auto-draft' ), true ) || ( isset( $_GET['action'] ) && $_GET['action'] === 'untrash' ) || empty( $_POST['rl_gallery'] ) )
3146 return;
3147
3148 $this->save_gallery( wp_unslash( $_POST ), $post_id );
3149 }
3150
3151 /**
3152 * Save gallery preview metadata.
3153 *
3154 * @param array $data Gallery data
3155 * @param int $post_id Post ID
3156 * @param bool $preview Whether is it preview
3157 * @return void
3158 */
3159 public function save_gallery( $post_data, $post_id, $preview = false ) {
3160 $data = $post_data['rl_gallery'];
3161
3162 // prepare sanitized data
3163 $safedata = array();
3164
3165 // sanitize all fields
3166 foreach ( $this->fields as $tab_id => $menu_items ) {
3167 switch ( $tab_id ) {
3168 case 'config':
3169 // add menu item
3170 $menu_item = isset( $data[$tab_id], $data[$tab_id]['menu_item'] ) && array_key_exists( $data[$tab_id]['menu_item'], $this->tabs[$tab_id]['menu_items'] ) ? $data[$tab_id]['menu_item'] : reset( $this->tabs[$tab_id]['menu_items'] );
3171
3172 // get default gallery fields
3173 $default_gallery_fields = Responsive_Lightbox()->frontend->get_default_gallery_fields();
3174
3175 // prepare fields
3176 if ( $menu_item === 'default' )
3177 $items = $default_gallery_fields;
3178 else {
3179 // assign settings and defaults
3180 $fields = Responsive_Lightbox()->settings->settings[$menu_item . '_gallery']['fields'];
3181 $defaults = Responsive_Lightbox()->defaults[$menu_item . '_gallery'];
3182
3183 // make a copy
3184 $fields_copy = $fields;
3185
3186 foreach ( $fields_copy as $field_id => $field ) {
3187 if ( $field['type'] === 'multiple' ) {
3188 foreach ( $field['fields'] as $subfield_id => $subfield ) {
3189 $fields[$field_id]['fields'][$subfield_id]['default'] = $defaults[$subfield_id];
3190 }
3191 } else
3192 $fields[$field_id]['default'] = $defaults[$field_id];
3193 }
3194
3195 $items = Responsive_Lightbox()->frontend->get_unique_fields( $default_gallery_fields, $fields );
3196 }
3197
3198 // sanitize fields
3199 $safedata = $this->sanitize_fields( $items, $data, $tab_id, $menu_item );
3200
3201 // add menu item
3202 $safedata[$tab_id]['menu_item'] = $menu_item;
3203 break;
3204
3205 default:
3206 // add menu item
3207 $menu_item = isset( $data[$tab_id], $data[$tab_id]['menu_item'] ) && array_key_exists( $data[$tab_id]['menu_item'], $this->tabs[$tab_id]['menu_items'] ) ? $data[$tab_id]['menu_item'] : 'options';
3208
3209 // prepare fields
3210 $items = $menu_items[$menu_item];
3211
3212 // sanitize fields
3213 $safedata = $this->sanitize_fields( $items, $data, $tab_id, $menu_item );
3214
3215 // add menu item
3216 $safedata[$tab_id]['menu_item'] = $menu_item;
3217 }
3218
3219 $safedata[$tab_id] = apply_filters( 'rl_gallery_tab_metadata', $safedata[$tab_id], $tab_id );
3220
3221 // preview?
3222 if ( $preview )
3223 update_metadata( 'post', $post_id, '_rl_' . $tab_id, $safedata[$tab_id] );
3224 else
3225 update_post_meta( $post_id, '_rl_' . $tab_id, $safedata[$tab_id] );
3226 }
3227
3228 $featured_image_type = ! empty( $post_data['rl_gallery_featured_image'] ) && in_array( $post_data['rl_gallery_featured_image'], array( 'id', 'url', 'image' ), true ) ? $post_data['rl_gallery_featured_image'] : 'id';
3229
3230 switch ( $featured_image_type ) {
3231 case 'url':
3232 $thumbnail_id = $this->maybe_generate_thumbnail();
3233
3234 $featured_image = isset( $post_data['_rl_thumbnail_url'] ) ? esc_url( $post_data['_rl_thumbnail_url'] ) : '';
3235 break;
3236
3237 case 'image':
3238 $thumbnail_id = $this->maybe_generate_thumbnail();
3239
3240 $featured_image = '';
3241 break;
3242
3243 case 'id':
3244 default:
3245 $featured_image = $thumbnail_id = isset( $post_data['_thumbnail_id'] ) ? (int) $post_data['_thumbnail_id'] : 0;
3246 }
3247
3248 // preview?
3249 if ( $preview ) {
3250 update_metadata( 'post', $post_id, '_rl_featured_image_type', $featured_image_type );
3251 update_metadata( 'post', $post_id, '_rl_featured_image', $featured_image );
3252 update_metadata( 'post', $post_id, '_thumbnail_id', $thumbnail_id );
3253 } else {
3254 // update featured image
3255 update_post_meta( $post_id, '_rl_featured_image_type', $featured_image_type );
3256 update_post_meta( $post_id, '_rl_featured_image', $featured_image );
3257 update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );
3258 }
3259
3260 // update post excerpt
3261 if ( isset( $safedata['misc']['options']['gallery_description'] ) ) {
3262
3263 remove_action( 'save_post_rl_gallery', array( $this, 'save_post' ), 10, 3 );
3264
3265 $postdata = array(
3266 'ID' => $post_id,
3267 'post_excerpt' => wp_kses_post( $safedata['misc']['options']['gallery_description'] ),
3268 );
3269
3270 wp_update_post( $postdata );
3271
3272 add_action( 'save_post_rl_gallery', array( $this, 'save_post' ), 10, 3 );
3273 }
3274 }
3275
3276 /**
3277 * Check attachments IDs.
3278 *
3279 * @param array $attachments Attachment ID's
3280 * @return array Existing image attachment ID's
3281 */
3282 public function check_attachments( $attachments ) {
3283 if ( empty( $attachments ) || ! is_array( $attachments ) )
3284 return array();
3285
3286 $copy = array_map( 'intval', $attachments );
3287
3288 // check attachments
3289 foreach ( $attachments as $key => $attachment_id ) {
3290 // is it an image?
3291 if ( ! wp_attachment_is_image( $attachment_id ) )
3292 unset( $copy[$key] );
3293 }
3294
3295 return $copy;
3296 }
3297
3298 /**
3299 * Display shortcode metabox.
3300 *
3301 * @param object $post Post object
3302 * @return void
3303 */
3304 public function shortcode_metabox( $post ) {
3305 echo '
3306 <p>' . __( 'You can place this gallery anywhere into your posts, pages, custom post types or widgets by using the shortcode below', 'responsive-lightbox' ) . ':</p>
3307 <code class="rl-shortcode">[rl_gallery id=&quot;' . $post->ID . '&quot;]</code>
3308 <p>' . __( 'You can also place this gallery into your template files by using the template tag below', 'responsive-lightbox' ) . ':</p>
3309 <code class="rl-shortcode">if ( function_exists( \'rl_gallery\' ) ) { rl_gallery( \'' . $post->ID . '\' ); }</code>';
3310 }
3311
3312 /**
3313 * Add new event listing columns.
3314 */
3315 public function gallery_columns( $columns ) {
3316 // find title position
3317 $offset = array_search( 'title', array_keys( $columns ) );
3318
3319 // put image column before title
3320 $columns = array_merge(
3321 array_slice( $columns, 0, $offset ),
3322 array(
3323 'image' => __( 'Gallery', 'responsive-lightbox' )
3324 ),
3325 array_slice( $columns, $offset )
3326 );
3327
3328 // put new columns after title
3329 $columns = array_merge(
3330 array_slice( $columns, 0, $offset + 2 ),
3331 array(
3332 'shortcode' => __( 'Shortcode', 'responsive-lightbox' ),
3333 'type' => __( 'Type', 'responsive-lightbox' ),
3334 'source' => __( 'Source', 'responsive-lightbox' )
3335 ),
3336 array_slice( $columns, $offset + 2 )
3337 );
3338
3339 return $columns;
3340 }
3341
3342 /**
3343 * Add new gallery listing columns content.
3344 *
3345 * @return void
3346 */
3347 public function gallery_columns_content( $column_name, $post_id ) {
3348 global $pagenow;
3349
3350 if ( $pagenow === 'edit.php' ) {
3351 switch ( $column_name ) {
3352 case 'image':
3353 // get image data, based on gallery source type
3354 $image = $this->get_featured_image( $post_id, array( 60, 60 ) );
3355 $images_count = (int) get_post_meta( $post_id, '_rl_images_count', true );
3356
3357 // display count
3358 if ( ! empty( $image ) )
3359 echo '<span class="media-icon image-icon">' . $image . '</span><span>' . sprintf( _n( '%s image', '%s images', $images_count, 'responsive-lightbox' ), $images_count ) . '</span>';
3360 else
3361 echo '<span class="media-icon image-icon">' . wp_get_attachment_image( 0, array( 60, 60 ), true, array( 'alt' => '' ) ) . '</span>';
3362 break;
3363
3364 case 'shortcode':
3365 echo '<code>[rl_gallery id="' . $post_id . '"]</code>';
3366 break;
3367
3368 case 'type':
3369 $config = get_post_meta( $post_id, '_rl_config', true );
3370
3371 if ( ! empty( $config['menu_item'] ) && array_key_exists( $config['menu_item'], $this->tabs['config']['menu_items'] ) ) {
3372 echo $this->tabs['config']['menu_items'][$config['menu_item']];
3373
3374 if ( $config['menu_item'] === 'default' )
3375 echo ' (' . $this->tabs['config']['menu_items'][Responsive_Lightbox()->options['settings']['builder_gallery']] . ')';
3376 } else
3377 echo '-';
3378 break;
3379
3380 case 'source':
3381 $images = get_post_meta( $post_id, '_rl_images', true );
3382
3383 if ( ! empty( $images['menu_item'] ) && array_key_exists( $images['menu_item'], $this->tabs['images']['menu_items'] ) )
3384 echo $this->tabs['images']['menu_items'][$images['menu_item']];
3385 else
3386 echo '-';
3387 break;
3388 }
3389 }
3390 }
3391
3392 /**
3393 * Get size information for all currently-registered image sizes.
3394 *
3395 * @global $_wp_additional_image_sizes
3396 * @uses get_intermediate_image_sizes()
3397 * @return array $sizes Data for all currently-registered image sizes.
3398 */
3399 public function get_image_sizes() {
3400 global $_wp_additional_image_sizes;
3401
3402 $sizes = array();
3403
3404 foreach ( get_intermediate_image_sizes() as $_size ) {
3405 if ( in_array( $_size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
3406 $sizes[$_size]['width'] = get_option( "{$_size}_size_w" );
3407 $sizes[$_size]['height'] = get_option( "{$_size}_size_h" );
3408 $sizes[$_size]['crop'] = (bool) get_option( "{$_size}_crop" );
3409 } elseif ( isset( $_wp_additional_image_sizes[$_size] ) ) {
3410 $sizes[$_size] = array(
3411 'width' => $_wp_additional_image_sizes[$_size]['width'],
3412 'height' => $_wp_additional_image_sizes[$_size]['height'],
3413 'crop' => $_wp_additional_image_sizes[$_size]['crop'],
3414 );
3415 }
3416 }
3417
3418 return $sizes;
3419 }
3420
3421 /**
3422 * Get size information for a specific image size.
3423 *
3424 * @uses get_image_sizes()
3425 * @param string $size The image size for which to retrieve data.
3426 * @return bool|array $size Size data about an image size or false if the size doesn't exist.
3427 */
3428 public function get_image_size( $size ) {
3429 if ( isset( $this->sizes[$size] ) )
3430 return $this->sizes[$size];
3431 else
3432 return false;
3433 }
3434
3435 /**
3436 * Filter the admin post thumbnail HTML markup
3437 *
3438 * @param mixed $content
3439 * @param int $post_id
3440 * @return mixed
3441 */
3442 public function admin_post_thumbnail_html( $content, $post_id ) {
3443 if ( get_post_type( $post_id ) === 'rl_gallery' ) {
3444 $value = get_post_meta( $post_id, '_rl_featured_image', true );
3445 $type = get_post_meta( $post_id, '_rl_featured_image_type', true );
3446 $type = ! empty( $type ) && in_array( $type, array( 'image', 'id', 'url' ) ) ? $type : 'image';
3447
3448 if ( ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) && $this->maybe_generate_thumbnail() === (int) get_post_meta( $post_id, '_thumbnail_id', true ) ) {
3449 remove_filter( 'admin_post_thumbnail_html', array( $this, 'admin_post_thumbnail_html' ), 10 );
3450
3451 $content = _wp_post_thumbnail_html( 0, $post_id );
3452 }
3453
3454 $content = '
3455 <div class="rl-gallery-featured-image-options">
3456 <p class="howto">' . __( 'Select gallery featured image source:', 'responsive-lightbox' ) . '</p>
3457 <label for="rl-gallery-featured-image"><input id="rl-gallery-featured-image" type="radio" name="rl_gallery_featured_image" value="image" ' . checked( $type, 'image', false ) . ' />' . __( 'First gallery image', 'responsive-lightbox' ) . '</label><br />
3458 <label for="rl-gallery-featured-id"><input id="rl-gallery-featured-id" type="radio" name="rl_gallery_featured_image" value="id" ' . checked( $type, 'id', false ) . ' />' . __( 'Media Library', 'responsive-lightbox' ) . '</label><br />
3459 <label for="rl-gallery-featured-url"><input id="rl-gallery-featured-url" type="radio" name="rl_gallery_featured_image" value="url" ' . checked( $type, 'url', false ) . ' />' . __( 'Custom URL', 'responsive-lightbox' ) . '</label>
3460 </div>
3461 <div class="rl-gallery-featured-image-select">
3462 <div class="rl-gallery-featured-image-select-id"' . ( $type === 'id' ? '' : ' style="display: none;"') . '>' . $content . '</div>
3463 <div class="rl-gallery-featured-image-select-url"' . ( $type === 'url' ? '' : ' style="display: none;"') . '>
3464 <p><input id="_rl_thumbnail_url" class="large-text" name="_rl_thumbnail_url" value="' . ( $type === 'url' ? esc_url( $value ) : '' ) . '" type="text" /></p>
3465 <p class="howto">' . __( 'Custom featured image URL', 'responsive-lightbox' ) . '</p>
3466 </div>
3467 <div class="rl-gallery-featured-image-select-image"' . ( $type === 'image' ? '' : ' style="display: none;"') . '><p class="howto">' . __( 'Dynamically generated first gallery image', 'responsive-lightbox' ) . '</p></div>
3468 </div>
3469 ';
3470 }
3471
3472 return $content;
3473 }
3474
3475 /**
3476 * Modify the resulting HTML so that the feature image is set as a background property.
3477 *
3478 * @param string $html the HTML image tag.
3479 * @param integer $post_id the post whose featured image is to be printed.
3480 * @param string $post_thumbnail_id the post thumbnail ID.
3481 * @param array|string $size the size of the featured image.
3482 * @param array $attr additional attributes.
3483 * @return string the HTML image tag, with a CSS background property set (when required).
3484 */
3485 public function post_thumbnail_html( $html, $post_id = 0, $post_thumbnail_id = '', $size = false, $attr = array() ) {
3486 if ( get_post_type( $post_id ) === 'rl_gallery' ) {
3487 // break if featured image type is media library
3488 $image_type = get_post_meta( $post_id, '_rl_featured_image_type', true );
3489
3490 if ( ! $image_type || $image_type == 'id' )
3491 return $html;
3492
3493 $image_src = $this->get_gallery_image_src( $this->get_featured_image_src( $post_id ) );
3494 $image_url = $image_src['url'];
3495
3496 // add featured image as background in style tag.
3497 $quote = '"';
3498 $style = "style=${quote}background:url( $image_url ) no-repeat center center;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size: cover;${quote}";
3499
3500 $html = str_replace( 'src=', $style . ' src=', $html );
3501
3502 // fix the alt tag (if possible).
3503 $alt = $image_src['alt'];
3504
3505 if ( isset( $attr['alt'] ) )
3506 $alt = $attr['alt'];
3507
3508 if ( $alt ) {
3509 $html = str_replace( '/(alt=\'[^\']+\'\|alt="[^"]+")/', '', $html );
3510 $html = str_replace( 'src=', ' alt="' . esc_attr( $alt ) . '" src=', $html );
3511 }
3512 }
3513
3514 return $html;
3515 }
3516
3517 /**
3518 * Save the revision meta data.
3519 *
3520 * @param int $revision_id
3521 * @return void
3522 */
3523 public function save_revision( $revision_id ) {
3524 // get revision
3525 $revision = get_post( $revision_id );
3526
3527 // get gallery ID
3528 $post_id = $revision->post_parent;
3529
3530 // is it rl gallery?
3531 if ( get_post_type( $post_id ) !== 'rl_gallery' )
3532 return;
3533
3534 $this->revision_id = $revision_id;
3535
3536 // save revisioned meta data
3537 $this->save_gallery( wp_unslash( $_POST ), $revision_id, true );
3538 }
3539
3540 /**
3541 * Update preview link.
3542 *
3543 * @param string $link Preview link
3544 * @return string
3545 */
3546 public function preview_post_link( $link ) {
3547 // add rl gallery revision ID
3548 if ( property_exists( $this, 'revision_id' ) && ! is_null( $this->revision_id ) ) {
3549 $post_id = wp_get_post_parent_id( $this->revision_id );
3550
3551 // is it valid rl_gallery post?
3552 if ( $post_id && get_post_type( $post_id ) === 'rl_gallery' )
3553 return add_query_arg( 'rl_gallery_revision_id', $this->revision_id, $link );
3554 }
3555
3556 return $link;
3557 }
3558
3559 /**
3560 * Delete gallery revision at shutdown.
3561 *
3562 * @return void
3563 */
3564 public function shutdown_preview() {
3565 // is it a frontend preview?
3566 if ( is_preview() && isset( $_GET['rl_gallery_revision_id'] ) ) {
3567 global $post;
3568
3569 // cast revision ID
3570 $revision_id = (int) $_GET['rl_gallery_revision_id'];
3571
3572 // is it a valid revision?
3573 if ( get_post_type( $post->ID ) === 'rl_gallery' && wp_is_post_revision( $revision_id ) === (int) $post->ID )
3574 wp_delete_post_revision( $revision_id );
3575 }
3576 }
3577
3578 /**
3579 * Filter gallery meta data needed for frontend gallery preview.
3580 *
3581 * @param mixed $value Meta value to filter
3582 * @param int $object_id Object ID
3583 * @param string $meta_key Meta key to filter a value for
3584 * @param bool $single Whether to return a single value
3585 * @return mixed
3586 */
3587 public function filter_preview_metadata( $value, $object_id, $meta_key, $single ) {
3588 // ignore other post types
3589 if ( get_post_type( $object_id ) !== 'rl_gallery' )
3590 return $value;
3591
3592 // get current post
3593 $post = get_post();
3594
3595 // prepare keys
3596 $keys = array( '_rl_featured_image_type', '_rl_featured_image', '_rl_images_count', '_thumbnail_id' );
3597
3598 // add other metakeys
3599 foreach ( array_keys( $this->tabs ) as $key ) {
3600 $keys[] = '_rl_' . $key;
3601 }
3602
3603 // restrict only to specified data
3604 if ( empty( $post ) || (int) $post->ID !== (int) $object_id || ! in_array( $meta_key, $keys, true ) || $post->post_type === 'revision' )
3605 return $value;
3606
3607 // grab the last autosave
3608 $preview = wp_get_post_autosave( $post->ID );
3609
3610 // invalid revision?
3611 if ( ! is_object( $preview ) )
3612 return $value;
3613
3614 // finally replace metadata
3615 return array( get_post_meta( $preview->ID, $meta_key, $single ) );
3616 }
3617 }