PluginProbe
Easy Image Gallery / trunk
Easy Image Gallery vtrunk
1.0.4 1.0.5 1.0.6 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 trunk 1.0.1 1.0.2 All 26 releases
easy-image-gallery / includes / metabox.php

metabox.php in Easy Image Gallery trunk, at includes/metabox.php

468 lines 17.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly.
5 }
6
7
8 /**
9 * Add meta boxes to selected post types
10 *
11 * @since 1.0
12 */
13 function easy_image_gallery_add_meta_box() {
14
15 $post_types = easy_image_gallery_allowed_post_types();
16
17 if ( ! $post_types ) {
18 return;
19 }
20
21 foreach ( $post_types as $post_type => $status ) {
22 add_meta_box( 'easy_image_gallery', apply_filters( 'easy_image_gallery_meta_box_title', __( 'Image Gallery', 'easy-image-gallery' ) ), 'easy_image_gallery_metabox', $post_type, apply_filters( 'easy_image_gallery_meta_box_context', 'normal' ), apply_filters( 'easy_image_gallery_meta_box_priority', 'low' ) );
23 }
24
25 }
26 add_action( 'add_meta_boxes', 'easy_image_gallery_add_meta_box' );
27
28
29 /**
30 * Render gallery metabox
31 *
32 * @since 1.0
33 */
34 function easy_image_gallery_metabox() {
35
36 global $post;
37
38 $old_meta_structure = get_post_meta( $post->ID, '_easy_image_gallery' );
39 $new_meta_structure = get_post_meta( $post->ID, '_easy_image_gallery_v2' );
40 ?>
41 <div id="dx-eig-gallery">
42 <div class="repeat">
43 <div class="eig_repeat_container">
44 <div class="buttons">
45 <span class="button button-primary button-large eig-add"><?php esc_html_e( 'Add new gallery', 'easy-image-gallery' ); ?></span>
46 </div>
47 <div class="eig_repeat_body">
48 <?php
49 if ( ! empty( $old_meta_structure ) && empty( $new_meta_structure ) ) {
50 ?>
51 <div class="alert-db-danger">
52 You are currently using an old version of Database structure for this page (or post). Once you update the page (post), you will need to use the <strong>SHORTCODE</strong> in order to display the gallery. Otherwise, you will not be able to see the gallery in the front-end part of the page (post).
53 </div>
54 <?php
55 }
56 ?>
57 <div class="eig-template dx-eig-gallery-row row">
58 <div class="dx-eig-gallery-row-heading move">
59 <input type="text" hidden="" class="row_count" data-count="{{row-count-placeholder}}">
60 <input type="text" hidden="" id="attachment_ids_{{row-count-placeholder}}" name="image_gallery[{{row-count-placeholder}}][DATA]" value="">
61 <span class="name">Gallery</span>
62 <a href="#" class="dx-eig-gallery-add-images button" data-count="{{row-count-placeholder}}"><?php esc_html_e( 'Add images to the gallery', 'easy-image-gallery' ); ?></a>
63 <span class="eig-remove"><img src="<?php echo esc_url( EASY_IMAGE_GALLERY_URL . 'includes/fonts/close.png' ); ?>" alt=""></span>
64 <a href="#" class="button button-primary button-small dx-eig-insert-shortcode">Insert this shortcode in the content</a>
65 <input type="text" class="dx-eig-shortcode" name="image_gallery[{{row-count-placeholder}}][SHORTCODE]" value="" hidden>
66 <input type="text" class="dx-eig-shortcode-show" readonly="" value="">
67 <div class="link-image-to-l">
68 <label for="easy_image_gallery_link_images_{{row-count-placeholder}}">
69 <input type="checkbox" id="easy_image_gallery_link_images_{{row-count-placeholder}}" value="on" name="image_gallery[{{row-count-placeholder}}][OPEN_IMAGES]" checked="checked"/> <?php esc_html_e( 'Link images to larger sizes', 'easy-image-gallery' ); ?>
70 </label>
71 </div>
72 <div class="dx-eig-clear"></div>
73 </div>
74 <div class="dx-eig-gallery-row-content" id="gallery-{{row-count-placeholder}}">
75 <p class="no-images-message"><?php esc_html_e( 'Please add images in this gallery', 'easy-image-gallery' ); ?></p>
76 </div>
77 </div>
78 <?php
79 // START GALLERIES LOOP
80
81 // CHECK FOR OLD DB
82
83 if ( isset( $new_meta_structure ) && $new_meta_structure != null ) {
84 $get_galleries = $new_meta_structure;
85 } else {
86 $get_gallery_attachments = $old_meta_structure;
87 if ( isset( $get_gallery_attachments[0] ) ) {
88 $get_gallery_old_data = explode( ',', $get_gallery_attachments[0] );
89 } else {
90 $get_gallery_old_data = null;
91 }
92
93 $get_open_images = get_post_meta( $post->ID, '_easy_image_gallery_link_images' );
94 if ( isset( $get_open_images ) && ! empty( $get_open_images ) ) {
95 $get_open_images = $get_open_images;
96 } else {
97 $get_open_images = null;
98 }
99
100 $get_galleries = array(
101 array(
102 array(
103 'SHORTCODE' => (string) wp_rand( 100000, 999999999 ),
104 'DATA' => $get_gallery_old_data,
105 'OPEN_IMAGES' => $get_open_images[0],
106 ),
107 ),
108 );
109 }
110
111 $gallery_count = -1;
112 if ( isset( $get_galleries ) && ! empty( $get_galleries ) ) {
113 $existing_shortcodes = array();
114
115 foreach ( $get_galleries[0] as $gallery ) {
116 $gallery_count = $gallery_count + 1;
117
118 $gallery_shortcode = easy_image_gallery_sanitize_gallery_id( $gallery['SHORTCODE'] );
119 if ( '' === $gallery_shortcode || in_array( $gallery_shortcode, $existing_shortcodes, true ) ) {
120 do {
121 $candidate = (string) wp_rand( 100000, 999999999 );
122 } while ( in_array( $candidate, $existing_shortcodes, true ) );
123
124 $gallery_shortcode = $candidate;
125 }
126
127 $existing_shortcodes[] = $gallery_shortcode;
128 $get_attachments = $gallery['DATA'];
129
130 // Convert attachements to string
131 $attachments_string = '';
132 $attachemnnts_count = 0;
133 if ( isset( $get_attachments ) && $get_attachments != null ) {
134 foreach ( $get_attachments as $attachment ) {
135 $attachemnnts_count = $attachemnnts_count + 1;
136
137 if ( $attachemnnts_count == 1 ) {
138 $attachments_string .= $attachment;
139 } else {
140 $attachments_string .= ',' . $attachment;
141 }
142 }
143 } else {
144 $attachments_string = null;
145 }
146 ?>
147 <div class="dx-eig-gallery-row row">
148 <div class="dx-eig-gallery-row-heading move">
149 <input type="text" hidden="" class="row_count" data-count="<?php echo esc_attr( (string) $gallery_count ); ?>">
150 <input type="text" hidden="" id="attachment_ids_<?php echo esc_attr( (string) $gallery_count ); ?>" name="image_gallery[<?php echo esc_attr( (string) $gallery_count ); ?>][DATA]" value="<?php echo esc_attr( (string) ( $attachments_string ?? '' ) ); ?>">
151 <span class="name">Gallery</span>
152 <a href="#" class="dx-eig-gallery-add-images button" data-count="<?php echo esc_attr( (string) $gallery_count ); ?>"><?php esc_html_e( 'Add images to the gallery', 'easy-image-gallery' ); ?></a>
153 <span class="eig-remove"><img src="<?php echo esc_url( EASY_IMAGE_GALLERY_URL . 'includes/fonts/close.png' ); ?>" alt=""></span>
154 <a href="#" class="button button-primary button-small dx-eig-insert-shortcode">Insert this shortcode in the content</a>
155 <input type="text" class="dx-eig-shortcode" name="image_gallery[<?php echo esc_attr( (string) $gallery_count ); ?>][SHORTCODE]" value="<?php echo esc_attr( $gallery_shortcode ); ?>" hidden>
156 <input type="text" class="dx-eig-shortcode-show" readonly="" value="<?php echo esc_attr( '[easy_image_gallery gallery="' . $gallery_shortcode . '"]' ); ?>">
157 <div class="link-image-to-l">
158 <label for="easy_image_gallery_link_images_<?php echo esc_attr( (string) $gallery_count ); ?>">
159 <input type="checkbox" id="easy_image_gallery_link_images_<?php echo esc_attr( (string) $gallery_count ); ?>" value="on" name="image_gallery[<?php echo esc_attr( (string) $gallery_count ); ?>][OPEN_IMAGES]" <?php checked( isset( $gallery['OPEN_IMAGES'] ) ? $gallery['OPEN_IMAGES'] : '', 'on' ); ?> /> <?php esc_html_e( 'Link images to larger sizes', 'easy-image-gallery' ); ?>
160 </label>
161 </div>
162 <div class="dx-eig-clear"></div>
163 </div>
164 <div class="dx-eig-gallery-row-content" id="gallery-<?php echo esc_attr( (string) $gallery_count ); ?>">
165 <?php
166 if ( isset( $get_attachments ) && $get_attachments != null ) {
167 ?>
168 <p class="no-images-message" style="display: none;"><?php esc_html_e( 'Please add images in this gallery', 'easy-image-gallery' ); ?></p>
169 <ul class="gallery_images">
170 <div class="dx-eig-images sortable">
171 <?php
172 foreach ( $get_attachments as $attachemnt ) {
173 echo '<li class="image attachment details" data-attachment_id="' . esc_attr( (string) $attachemnt ) . '" data-gallery="' . esc_attr( (string) $gallery_count ) . '">
174 <div class="attachment-preview">
175 <div class="thumbnail">
176 ' . wp_get_attachment_image( $attachemnt, 'thumbnail' ) . '
177 </div>
178 <a href="#" class="delete_dx_image check" title="Remove Image"><div class="media-modal-icon"></div></a>
179 </div>
180 </li>';
181 }
182 ?>
183 </div>
184 <div class="dx-eig-clear"></div>
185 </ul>
186 <?php
187 } else {
188 echo '<p class="no-images-message">';
189 esc_html_e( 'Please add images in this gallery', 'easy-image-gallery' );
190 echo '</p>';
191 }
192 ?>
193 </div>
194 </div>
195 <?php
196 } // END GALLERIES LOOP
197 }
198 ?>
199 </div>
200 </div>
201 </div>
202 </div>
203
204 <script type="text/javascript">
205 jQuery(function() {
206 jQuery('.repeat').each(function() {
207 jQuery(this).repeatable_fields({
208 wrapper: '.eig_repeat_container',
209 container: '.eig_repeat_body',
210 template: '.eig-template',
211 add: '.eig-add',
212 remove: '.eig-remove',
213 });
214 });
215 });
216 function eig_sortable() {
217 jQuery( function() {
218 jQuery(".sortable").sortable({
219 revert : true,
220 stop : function(event,ui){
221 var gallery = jQuery(this);
222 var gallery_selector = jQuery(this).parent().parent();
223 var get_gallery_id = gallery_selector.find('.image').attr('data-gallery');
224
225 var items = gallery_selector.find('.gallery_images').find('.dx-eig-images').children();
226 var attachments_ids = [];
227 for (i = 0; i < items.length; i++) {
228 var attachment_id = items[i].attributes[1].value;
229 attachments_ids.push(attachment_id);
230 }
231
232
233 if (attachments_ids.length === 0){
234 gallery.find('p.no-images-message').show();
235 }
236
237 jQuery('#attachment_ids_'+get_gallery_id+'').attr('value', attachments_ids);
238 }
239 });
240 } );
241 }
242
243 jQuery(document).on( 'click', '.dx-eig-gallery-add-images', function(e) {
244 var _id = jQuery( this ).attr( 'data-count' );
245 var attachment_ids = null;
246 e.preventDefault();
247
248 var image = wp.media({
249 title: 'Select images for your gallery',
250 multiple: true,
251 }).open();
252
253 image.on( 'select', function() {
254 var selection = image.state().get('selection');
255
256 selection.map( function( attachment ) {
257
258 attachment = attachment.toJSON();
259
260 if ( attachment.id ) {
261 attachment_ids = attachment_ids ? attachment_ids + "," + attachment.id : attachment.id;
262
263 var gallery = jQuery('#gallery-'+ _id +'');
264
265 if (gallery.find('p.no-images-message') && gallery.find('p.no-images-message').css('display') === 'block'){
266 gallery.find('p.no-images-message').css('display', 'none');
267 }
268
269 if (gallery.find('ul.gallery_images').length < 1){
270 gallery.append(
271 '<ul class="gallery_images">'+
272 '<div class="dx-eig-images sortable"></div>'+
273 '<div class="dx-eig-clear"></div>'+
274 '</ul>'
275 );
276
277 eig_sortable();
278 }
279
280 attachment_url = '';
281 if(typeof attachment.sizes.thumbnail !== 'undefined'){
282 attachment_url = attachment.sizes.thumbnail.url;
283 }else{
284 attachment_url = attachment.url;
285 }
286
287 gallery.find('ul.gallery_images .dx-eig-images').append('\
288 <li class="image attachment details" data-attachment_id="' + attachment.id + '" data-gallery="'+_id+'">\
289 <div class="attachment-preview">\
290 <div class="thumbnail">\
291 <img src="' + attachment_url + '" />\
292 </div>\
293 <a href="#" class="delete_dx_image check" title="<?php esc_attr_e( 'Remove image', 'easy-image-gallery' ); ?>"><div class="media-modal-icon"></div></a>\
294 </div>\
295 </li>');
296 }
297
298 } );
299
300 var attachments_selector = jQuery('#attachment_ids_'+ _id +'');
301 var current_image_ids = attachments_selector.attr('value');
302 if (current_image_ids.length > 0){
303 current_image_ids = current_image_ids + ',';
304 }
305 attachments_selector.attr('value',current_image_ids + attachment_ids);
306 });
307 });
308
309 jQuery(document).on( 'click', '.delete_dx_image', function(e) {
310 //Get info
311 var info = jQuery(this).parent().parent();
312 var gallery = info.attr('data-gallery');
313 var gallery_selector = jQuery('#gallery-'+gallery+'');
314
315 //Remove the item
316 info.remove();
317
318 //Save new items
319 var items = gallery_selector.find('.gallery_images').find('.dx-eig-images').children();
320 var attachments_ids = [];
321 for (i = 0; i < items.length; i++) {
322 var attachment_id = items[i].attributes[1].value;
323
324 attachments_ids.push(attachment_id);
325 }
326
327 if (attachments_ids.length === 0){
328 gallery_selector.find('p.no-images-message').show();
329 }
330
331 jQuery('#attachment_ids_'+gallery+'').attr('value', attachments_ids);
332
333 return false;
334 });
335
336 jQuery(document).on( 'click', '.dx-eig-insert-shortcode', function(e) {
337 e.preventDefault();
338
339 var id = jQuery(this).parent().find('.dx-eig-shortcode').val(),
340 shortcode = '[easy_image_gallery gallery="'+ id +'"]';
341
342 // Gutenberg
343 if ( typeof wp.blocks !== 'undefined' ) {
344 var block = wp.blocks.createBlock( 'core/shortcode' );
345 block.attributes.text = shortcode;
346 wp.data.dispatch( 'core/editor' ).insertBlocks( block );
347
348 // Classic Editor
349 } else if ( typeof tinymce !== 'undefined' ) {
350 var editor = tinymce.get('content'),
351 content = editor.getContent();
352
353 content += "\n\n" + shortcode;
354
355 editor.setContent( content );
356 }
357 });
358
359 eig_sortable();
360 </script>
361 <?php
362 }
363
364
365 /**
366 * Save function
367 *
368 * @since 1.0
369 */
370 function easy_image_gallery_save_post( $post_id ) {
371
372 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
373 return;
374 }
375
376 if ( wp_is_post_revision( $post_id ) ) {
377 return;
378 }
379
380 $post_id = (int) $post_id;
381 if ( $post_id <= 0 ) {
382 return;
383 }
384
385 $post_types = easy_image_gallery_allowed_post_types();
386 if ( empty( $post_types ) || ! is_array( $post_types ) ) {
387 return;
388 }
389
390 // Check user permissions (use stored post type, not $_POST — avoids unverified POST reads).
391 $easy_image_gallery_current_post_type = get_post_type( $post_id );
392 if ( $easy_image_gallery_current_post_type && ! array_key_exists( $easy_image_gallery_current_post_type, $post_types ) ) {
393 if ( ! current_user_can( 'edit_page', $post_id ) ) {
394 return;
395 }
396 } elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
397 return;
398 }
399
400 // Require core post update nonce before any $_POST reads (incl. action / gallery fields).
401 if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'update-post_' . $post_id ) ) {
402 return;
403 }
404
405 if ( isset( $_POST['action'] ) && 'inline-save' === $_POST['action'] ) {
406 return;
407 }
408
409 if ( isset( $_POST['image_gallery'] ) && ! empty( $_POST['image_gallery'] ) ) {
410 $galleries = array();
411
412 $easy_image_gallery_post = map_deep( wp_unslash( $_POST['image_gallery'] ), 'sanitize_text_field' );
413
414 $existing_shortcodes = array();
415
416 foreach ( $easy_image_gallery_post as $gallery ) {
417 if ( ! is_array( $gallery ) ) {
418 continue;
419 }
420
421 $gallery_data = isset( $gallery['DATA'] ) ? $gallery['DATA'] : '';
422
423 if ( '' !== $gallery_data ) {
424 $convert_to_arr = explode( ',', $gallery_data );
425 } else {
426 $convert_to_arr = null;
427 }
428
429 $gallery['DATA'] = $convert_to_arr;
430
431 if ( isset( $gallery['SHORTCODE'] ) ) {
432 $gallery['SHORTCODE'] = easy_image_gallery_sanitize_gallery_id( $gallery['SHORTCODE'] );
433
434 if ( '' === $gallery['SHORTCODE'] || in_array( $gallery['SHORTCODE'], $existing_shortcodes, true ) ) {
435 do {
436 $candidate = (string) wp_rand( 100000, 999999999 );
437 } while ( in_array( $candidate, $existing_shortcodes, true ) );
438
439 $gallery['SHORTCODE'] = $candidate;
440 }
441
442 $existing_shortcodes[] = $gallery['SHORTCODE'];
443 }
444
445 $galleries[] = $gallery;
446 }
447
448 update_post_meta( $post_id, '_easy_image_gallery_v2', $galleries );
449 delete_post_meta( $post_id, '_easy_image_gallery' );
450 } elseif ( isset( $_POST['action'] ) && 'editpost' === $_POST['action'] ) {
451 delete_post_meta( $post_id, '_easy_image_gallery_v2' );
452 }
453
454 // Link to larger images (legacy POST key).
455 if ( isset( $_POST['easy_image_gallery_link_images'] ) ) {
456 update_post_meta(
457 $post_id,
458 '_easy_image_gallery_link_images',
459 sanitize_text_field( wp_unslash( $_POST['easy_image_gallery_link_images'] ) )
460 );
461 } else {
462 update_post_meta( $post_id, '_easy_image_gallery_link_images', 'on' );
463 }
464
465 do_action( 'easy_image_gallery_save_post', $post_id );
466 }
467 add_action( 'save_post', 'easy_image_gallery_save_post' );
468