1: <?php
2: /**
3: * Base class for handling images for listings in the admin panel.
4: *
5: * @package EPL
6: * @subpackage Admin/Classes/EPL_Admin_Images
7: * @copyright Copyright (c) 2025, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.5.16
10: */
11:
12: if ( ! defined( 'ABSPATH' ) ) {
13: exit; // Exit if accessed directly.
14: }
15:
16: /**
17: * EPL_Admin_Images Class
18: *
19: * @since 3.5.16
20: * @since 3.5.18 Added nonce check.
21: */
22: if ( ! class_exists( 'EPL_Admin_Images' ) ) :
23: /**
24: * EPL_Admin_Images Class
25: *
26: * @since 3.5.16
27: * @since 3.5.18 Added nonce check.
28: */
29: class EPL_Admin_Images {
30:
31: /**
32: * Config
33: *
34: * @since 3.5.16
35: * @var string $config Config.
36: */
37: public $config;
38:
39: /**
40: * Static properties to manage all instances.
41: *
42: * @since 3.5.16
43: * @var array $registered_extensions Registered extensions.
44: */
45: private static $registered_extensions = array();
46:
47: /**
48: * Save Hook Actions.
49: *
50: * @since 3.5.16
51: *
52: * @var bool $save_hook_added Save hook added.
53: */
54: private static $save_hook_added = false;
55:
56: /**
57: * Get things started
58: *
59: * @param array $config Config.
60: *
61: * @since 3.5.16
62: */
63: public function __construct( $config = array() ) {
64:
65: $defaults = array(
66: 'extension' => 'slider',
67: 'prefix' => 'epl_slider_',
68: 'title' => __( 'Slider Images', 'easy-property-listings' ),
69: 'button_label' => __( 'Add to Slider', 'easy-property-listings' ),
70: 'order_meta_key' => 'epl_slides_order',
71: );
72:
73: $this->config = wp_parse_args( $config, $defaults );
74:
75: // Register this instance.
76: self::$registered_extensions[ $this->get_config( 'extension' ) ] = $this;
77:
78: // Add the save hook only once for all instances.
79: if ( ! self::$save_hook_added ) {
80: add_action( 'save_post', array( __CLASS__, 'save_all_extensions_data' ) );
81: self::$save_hook_added = true;
82: }
83:
84: // Add individual hooks (non-conflicting ones).
85: $this->add_individual_hooks();
86: }
87:
88: /**
89: * Get config
90: *
91: * @param string $key Config key.
92: *
93: * @since 3.5.16
94: */
95: public function get_config( $key = '' ) {
96:
97: if ( empty( $key ) ) {
98: return $this->config;
99: }
100:
101: if ( isset( $this->config[ $key ] ) ) {
102: return $this->config[ $key ];
103: }
104:
105: return false;
106: }
107:
108: /**
109: * Add individual hooks
110: *
111: * @since 3.5.16
112: */
113: private function add_individual_hooks() {
114:
115: add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
116: add_action( 'wp_ajax_epl_images_save_order', array( $this, 'save_image_order' ) );
117: add_action( 'wp_ajax_epl_images_unattach', array( $this, 'unattach_image' ) );
118: add_action( 'add_attachment', array( $this, 'update_image_order_on_new_attachment' ) );
119: }
120:
121: /**
122: * Get the original attachment order from listing metadata.
123: *
124: * @param object $post Current listing post object.
125: * @param array $all_images Attachment IDs to consider.
126: *
127: * @return array Ordered attachment IDs.
128: *
129: * @since 3.5.24
130: */
131: private function get_original_image_order( $post, $all_images ) {
132:
133: $original_order = array();
134:
135: if ( empty( $all_images ) ) {
136: return $original_order;
137: }
138:
139: $order_meta = get_post_meta( $post->ID, 'property_images_original_order', true );
140:
141: if ( empty( $order_meta ) ) {
142: return $original_order;
143: }
144:
145: if ( is_array( $order_meta ) ) {
146: $order_values = $order_meta;
147: } else {
148: $order_values = explode( ',', $order_meta );
149: }
150:
151: $order_values = array_filter( array_map( 'trim', $order_values ), 'strlen' );
152: if ( empty( $order_values ) ) {
153: return $original_order;
154: }
155:
156: $attachments = get_posts(
157: array(
158: 'post_type' => 'attachment',
159: 'post__in' => $all_images,
160: 'numberposts' => -1,
161: 'fields' => 'ids',
162: 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- bounded by post__in below, admin-only.
163: array(
164: 'key' => '_epl_media_object_id',
165: 'compare' => 'EXISTS',
166: ),
167: ),
168: )
169: );
170:
171: if ( empty( $attachments ) ) {
172: return $original_order;
173: }
174:
175: $attachments_by_media_id = array();
176: foreach ( $attachments as $attachment_id ) {
177: $media_object_id = get_post_meta( $attachment_id, '_epl_media_object_id', true );
178:
179: if ( '' === $media_object_id ) {
180: continue;
181: }
182:
183: $attachments_by_media_id[ (string) $media_object_id ][] = $attachment_id;
184: }
185:
186: foreach ( $order_values as $media_object_id ) {
187: if ( ! isset( $attachments_by_media_id[ $media_object_id ] ) ) {
188: continue;
189: }
190:
191: foreach ( $attachments_by_media_id[ $media_object_id ] as $attachment_id ) {
192: if ( in_array( $attachment_id, $original_order, true ) ) {
193: continue;
194: }
195:
196: $original_order[] = $attachment_id;
197: }
198: }
199:
200: return $original_order;
201: }
202:
203: /**
204: * Static method to handle saving for all registered extensions
205: *
206: * @param string $post_id Post ID.
207: *
208: * @since 3.5.16
209: * @since 3.5.18 Check for post object.
210: */
211: public static function save_all_extensions_data( $post_id ) {
212:
213: // Basic validation.
214: if ( empty( $post_id ) || ! is_numeric( $post_id ) ) {
215: return;
216: }
217:
218: $post = get_post( $post_id );
219:
220: if ( ! $post || ! is_epl_post( $post->post_type ) ) {
221: return;
222: }
223:
224: if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
225: return;
226: }
227:
228: if ( ! current_user_can( 'edit_post', $post_id ) ) {
229: return;
230: }
231:
232: // Process each registered extension.
233: foreach ( self::$registered_extensions as $extension ) {
234: $extension->process_save_data( $post_id );
235: }
236: }
237:
238: /**
239: * Process save data for this specific extension
240: *
241: * @param string $post_id Post ID.
242: *
243: * @since 3.5.16
244: * @since 3.5.18 Added nonce check.
245: */
246: public function process_save_data( $post_id ) {
247:
248: $prefix = $this->get_config( 'prefix' );
249: $enabled_thumbs_key = $prefix . 'enabled_thumbs';
250:
251: // Verify nonce.
252: if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'update-post_' . $post_id ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified here.
253: return;
254: }
255:
256: // Only process if this extension's data exists in POST.
257: if ( ! isset( $_POST[ $enabled_thumbs_key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce already verified above.
258: return;
259: }
260:
261: // Mark selection made.
262: update_post_meta( $post_id, $prefix . 'selection_made', true );
263:
264: // Save enabled thumbnails.
265: if ( ! empty( $_POST[ $enabled_thumbs_key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce already verified above.
266: $enabled_thumbs = array_map( 'absint', wp_unslash( $_POST[ $enabled_thumbs_key ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
267: $enabled_thumbs = array_filter( $enabled_thumbs );
268: update_post_meta( $post_id, $enabled_thumbs_key, $enabled_thumbs );
269: } else {
270: update_post_meta( $post_id, $enabled_thumbs_key, array() );
271: }
272: }
273:
274: /**
275: * Is external link
276: *
277: * @param string $url External link URL.
278: *
279: * @since 3.5.16
280: * @since 3.5.18 Adjusted to use home_url.
281: */
282: public function is_external_link( $url ) {
283:
284: $site = home_url();
285: return strpos( (string) $url, $site ) !== 0;
286: }
287:
288: /**
289: * Add meta box for extension
290: *
291: * @since 2.0
292: */
293: public function add_meta_box() {
294:
295: add_meta_box(
296: 'epl_images_management_' . $this->get_config( 'extension' ),
297: $this->get_config( 'title' ),
298: array( $this, 'epl_images_management_callback' ),
299: epl_all_post_types()
300: );
301: }
302:
303: /**
304: * Attachments Callback for adding, disabling and ordering images.
305: *
306: * @param object $post Global post object.
307: *
308: * @since 3.5.16
309: */
310: public function get_parent_images( $post ) {
311:
312: $args = array(
313: 'post_parent' => $post->ID,
314: 'post_type' => 'attachment',
315: 'numberposts' => -1,
316: 'post_mime_type' => 'image',
317: 'orderby' => 'ID',
318: 'fields' => 'ids',
319: );
320:
321: return get_posts( $args );
322: }
323:
324: /**
325: * Attachments Callback for adding, disabling and ordering images.
326: *
327: * @param object $post Global post object.
328: *
329: * @since 3.5.16
330: * @since 3.5.18 Array search improvements and better checks for attachments.
331: * @since 3.5.24 Support for getting original image order.
332: */
333: public function epl_images_management_callback( $post ) {
334:
335: $parent_attachments = $this->get_parent_images( $post );
336:
337: $args = array(
338: 'post_type' => 'attachment',
339: 'numberposts' => -1,
340: 'post_mime_type' => 'image',
341: 'orderby' => 'post__in',
342: 'post__in' => array(),
343: );
344:
345: $attachments = array();
346: $initial = false;
347:
348: $extension = $this->get_config( 'extension' );
349: $prefix = $this->get_config( 'prefix' );
350:
351: $enabled = get_post_meta( $post->ID, $prefix . 'enabled_thumbs', true );
352: $selection_made = get_post_meta( $post->ID, $prefix . 'selection_made', true );
353:
354: if ( empty( $enabled ) && ! $selection_made ) {
355: $initial = true;
356: $enabled = (array) $enabled;
357: }
358:
359: $enabled = array_filter( (array) $enabled );
360: $enabled = array_map( 'intval', $enabled );
361:
362: $custom_images = array_diff( $enabled, $parent_attachments );
363: $all_images = array_merge( $parent_attachments, $custom_images );
364: $post_not_in = array();
365:
366: // Check if listing has floorplan image.
367: $floorplan = get_post_meta( $post->ID, 'property_floorplan', true );
368: $floorplan_id = '';
369: if ( ! $this->is_external_link( $floorplan ) ) {
370:
371: // If it's not an external image check if it's an attachment.
372: $floorplan_id = attachment_url_to_postid( $floorplan );
373:
374: if ( ! empty( $floorplan_id ) ) {
375: $post_not_in[] = $floorplan_id;
376: }
377: }
378:
379: $floorplan_2 = get_post_meta( $post->ID, 'property_floorplan_2', true );
380: $floorplan_id_2 = '';
381: if ( ! $this->is_external_link( $floorplan_2 ) ) {
382:
383: // If it's not an external image check if it's an attachment.
384: $floorplan_id_2 = attachment_url_to_postid( $floorplan_2 );
385:
386: if ( ! empty( $floorplan_id_2 ) ) {
387: $post_not_in[] = $floorplan_id_2;
388: }
389: }
390:
391: if ( ! empty( $all_images ) ) {
392:
393: $args['post__in'] = $all_images;
394: }
395:
396: if ( ! empty( $post_not_in ) ) {
397:
398: foreach ( $post_not_in as $current_not_in ) {
399:
400: if ( ! empty( $args['post__in'] ) ) {
401: $key = array_search( $current_not_in, $args['post__in'], true );
402: if ( false !== $key ) {
403: unset( $args['post__in'][ $key ] );
404: }
405: }
406: }
407:
408: $args['post__not_in'] = $post_not_in;
409: }
410:
411: $order_meta = get_post_meta( $post->ID, $this->get_config( 'order_meta_key' ), true );
412: $has_manual_order = is_array( $order_meta ) ? ! empty( $order_meta ) : ( '' !== $order_meta );
413:
414: if ( $has_manual_order ) {
415:
416: $ordered_posts = is_array( $order_meta ) ? $order_meta : explode( ',', $order_meta );
417: $ordered_posts = array_map( 'absint', $ordered_posts );
418: $ordered_posts = array_filter( $ordered_posts );
419: $unordered_posts = array_diff( $all_images, $ordered_posts );
420: $all_images = array_merge( $ordered_posts, $unordered_posts );
421: $args['post__in'] = $all_images;
422: $args['orderby'] = 'post__in';
423: } else {
424: $ordered_posts = $this->get_original_image_order( $post, $all_images );
425: if ( ! empty( $ordered_posts ) ) {
426: $unordered_posts = array_diff( $all_images, $ordered_posts );
427: $all_images = array_merge( $ordered_posts, $unordered_posts );
428: $args['post__in'] = $all_images;
429: $args['orderby'] = 'post__in';
430: }
431: }
432:
433: if ( has_post_thumbnail( $post->ID ) ) {
434:
435: $featured_image = get_post_thumbnail_id( $post->ID );
436:
437: if ( ! empty( $args['post__in'] ) ) {
438: $key = array_search( $featured_image, $args['post__in'], true );
439: if ( false !== $key ) {
440: unset( $args['post__in'][ $key ] );
441: }
442: } else {
443: $args['exclude'] = $featured_image;
444: }
445:
446: if ( ! empty( $args['post__in'] ) ) {
447: $attachments = get_posts( $args );
448: }
449: } elseif ( ! empty( $args['post__in'] ) ) {
450: $attachments = get_posts( $args );
451: }
452:
453: if ( empty( $attachments ) ) {
454:
455: unset( $args['post__in'] );
456: $args['orderby'] = 'ID';
457: $args['post_parent'] = $post->ID;
458: }
459:
460: $attachments = get_posts( $args ); ?>
461:
462: <div class="epl-listing-attachments-list-wrap">
463:
464: <?php if ( $attachments ) : ?>
465: <ul
466: data-prefix="<?php echo esc_attr( $prefix ); ?>"
467: data-extension="<?php echo esc_attr( $extension ); ?>"
468: id="epl-<?php echo esc_attr( $extension ); ?>-post-attachments"
469: class="epl-<?php echo esc_attr( $extension ); ?>-post-attachments epl-listing-attachments-list"
470: >
471: <input type="hidden" name="<?php echo esc_attr( $prefix ); ?>enabled_thumbs[]" value="" />
472:
473: <?php foreach ( $attachments as $attachment ) : ?>
474: <?php
475: // Determine checked status.
476: $checked = $initial ? 'checked=checked' : '';
477:
478: if ( ! empty( $enabled ) ) {
479: $checked = in_array( $attachment->ID, $enabled, true ) ? 'checked=checked' : '';
480: }
481:
482: // Get thumbnail details.
483: $thumb_size = epl_get_option( $prefix . 'admin_image_size', 'admin-list-thumb' );
484: $thumb = wp_get_attachment_image_src( $attachment->ID, $thumb_size );
485: ?>
486:
487: <li data-id="<?php echo esc_attr( $attachment->ID ); ?>" class="ui-state-default epl-listing-attachments-list-item <?php echo esc_attr( $prefix ); ?>admin_thumb">
488:
489: <span class="epl-slider-unattach">
490: <div class="epl-radio-switch">
491: <input
492: id="epl-<?php echo esc_attr( $extension ); ?>-cmn-toggle-<?php echo esc_attr( $attachment->ID ); ?>"
493: class="epl-radio-switch-input epl-radio-switch-input--yes-no"
494: name="<?php echo esc_attr( $prefix ); ?>enabled_thumbs[]"
495: <?php echo esc_attr( $checked ); ?>
496: value="<?php echo esc_attr( $attachment->ID ); ?>"
497: type="checkbox"
498: />
499: <label for="epl-<?php echo esc_attr( $extension ); ?>-cmn-toggle-<?php echo esc_attr( $attachment->ID ); ?>"></label>
500: </div>
501: </span>
502:
503: <div class="epl-<?php echo esc_attr( $extension ); ?>-slide-tools epl-listing-attachment-tools">
504: <a target="_blank" href="<?php echo esc_url( admin_url( 'post.php?post=' . $attachment->ID . '&action=edit' ) ); ?>">
505: <span class="epl-<?php echo esc_attr( $extension ); ?>-edit-attach dashicons dashicons-edit"></span>
506: </a>
507: <a href="#" class="epl-slider-<?php echo esc_attr( $extension ); ?>-delete-attach epl-listing-attachment-tools-delete-attach">
508: <span class="dashicons dashicons-trash"></span>
509: </a>
510: </div>
511:
512: <img src="<?php echo esc_url( $thumb[0] ); ?>" alt="<?php esc_attr_e( 'Attachment Thumbnail', 'easy-property-listings' ); ?>" />
513: </li>
514: <?php endforeach; ?>
515:
516: </ul>
517: <div class="epl-clearfix"></div>
518: <?php
519: // Output is HTML markup generated by extension callbacks hooked to epl_image_extension_messages; each callback escapes its own output.
520: echo $this->extension_messages( $post ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
521: ?>
522:
523: <?php else : ?>
524:
525: <div class="update-nag">
526: <?php esc_html_e( 'Add images to the slider using add media button.', 'easy-property-listings' ); ?>
527: </div>
528: <ul id="epl-<?php echo esc_attr( $extension ); ?>-post-attachments" class="epl-<?php echo esc_attr( $extension ); ?>-post-attachments"></ul>
529: <div class="epl-clearfix"></div>
530:
531: <?php endif; ?>
532:
533: <div class="update-nag">
534: <?php esc_html_e( 'Drag to reorder images and use the switch to remove images from the slider. Update the post to save changes.', 'easy-property-listings' ); ?>
535: </div>
536: <input
537: type="button"
538: class="btn button epl-<?php echo esc_attr( $extension ); ?>-upload-btn epl-listing-images-upload-btn"
539: name="<?php echo esc_attr( $prefix ); ?>upload_button"
540: value="<?php echo esc_attr( $this->get_config( 'button_label' ) ); ?>"
541: />
542: </div>
543: <?php
544: }
545:
546: /**
547: * Extension messages
548: *
549: * @param object $post Post object.
550: *
551: * @since 3.5.16
552: */
553: public function extension_messages( $post ) {
554:
555: ob_start();
556: do_action( 'epl_image_extension_messages', $this->config, $post );
557: return ob_get_clean();
558: }
559:
560: /**
561: * Save Image Order
562: *
563: * @since 3.5.16
564: * @since 3.5.18 Nonce added and fixes to checks and sanitisation.
565: */
566: public function save_image_order() {
567:
568: // Verify nonce.
569: check_ajax_referer( 'epl_ajax_nonce', 'nonce' );
570:
571: // Parse and validate post ID.
572: $post_id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0;
573: if ( ! $post_id ) {
574: wp_send_json_error( array( 'message' => 'Missing ID' ), 400 );
575: }
576:
577: // Verify capability for this specific post.
578: if ( ! current_user_can( 'edit_post', $post_id ) ) {
579: wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
580: }
581:
582: // Validate post type is EPL.
583: $post = get_post( $post_id );
584: if ( ! $post || ! is_epl_post( $post->post_type ) ) {
585: wp_send_json_error( array( 'message' => 'Invalid post type' ), 400 );
586: }
587:
588: // Validate extension against registered extensions.
589: $extension = isset( $_POST['extension'] ) ? sanitize_key( wp_unslash( $_POST['extension'] ) ) : 'slider';
590: if ( ! isset( self::$registered_extensions[ $extension ] ) ) {
591: wp_send_json_error( array( 'message' => 'Invalid extension' ), 400 );
592: }
593:
594: $order_meta_key = self::$registered_extensions[ $extension ]->get_config( 'order_meta_key' );
595:
596: // Sanitise order as CSV of integers.
597: $order_raw = isset( $_POST['order'] ) ? wp_unslash( $_POST['order'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitized below.
598: $order_ids = array_filter( array_map( 'absint', explode( ',', (string) $order_raw ) ) );
599: $order = implode( ',', $order_ids );
600:
601: update_post_meta( $post_id, $order_meta_key, $order );
602:
603: wp_send_json_success( array( 'order' => $order ) );
604: }
605:
606: /**
607: * Detach Images
608: *
609: * @since 3.5.16
610: * @since 3.5.18 Nonce added and fixes to checks and sanitisation.
611: */
612: public function unattach_image() {
613:
614: // Verify nonce first, before reading any POST data.
615: check_ajax_referer( 'epl_ajax_nonce', 'nonce' );
616:
617: // Parse and validate required IDs.
618: $post_id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0;
619: $image_id = isset( $_POST['img_id'] ) ? absint( $_POST['img_id'] ) : 0;
620:
621: if ( ! $post_id || ! $image_id ) {
622: wp_send_json_error( array( 'message' => 'Missing data' ), 400 );
623: }
624:
625: // Validate post type is EPL.
626: $post = get_post( $post_id );
627: if ( ! $post || ! is_epl_post( $post->post_type ) ) {
628: wp_send_json_error( array( 'message' => 'Invalid post type' ), 400 );
629: }
630:
631: // Verify capability for this specific post.
632: if ( ! current_user_can( 'edit_post', $post_id ) ) {
633: wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
634: }
635:
636: // Validate prefix against registered extension prefixes.
637: $prefix_input = isset( $_POST['prefix'] ) ? sanitize_key( wp_unslash( $_POST['prefix'] ) ) : '';
638: $allowed_prefixes = array();
639: foreach ( self::$registered_extensions as $ext ) {
640: $allowed_prefixes[] = $ext->get_config( 'prefix' );
641: }
642:
643: if ( ! empty( $prefix_input ) && in_array( $prefix_input, $allowed_prefixes, true ) ) {
644: $prefix = $prefix_input;
645: } else {
646: $prefix = $this->get_config( 'prefix' );
647: }
648:
649: // Get current parent images to check if this is a custom image.
650: $parent_attachments = $this->get_parent_images( $post );
651: $is_custom_image = ! in_array( $image_id, $parent_attachments, true );
652:
653: // Only detach if it's actually a child of this post.
654: if ( ! $is_custom_image ) {
655: wp_update_post(
656: array(
657: 'ID' => $image_id,
658: 'post_parent' => 0,
659: )
660: );
661: }
662:
663: // Remove from enabled thumbs (this handles both custom and parent images).
664: $enabled = get_post_meta( $post_id, $prefix . 'enabled_thumbs', true );
665: if ( ! empty( $enabled ) ) {
666: $key = array_search( $image_id, $enabled, true );
667: if ( false !== $key ) {
668: unset( $enabled[ $key ] );
669: // Re-index array to prevent gaps.
670: $enabled = array_values( $enabled );
671: update_post_meta( $post_id, $prefix . 'enabled_thumbs', $enabled );
672: }
673: }
674:
675: // Remove from slide order.
676: // Validate extension against registered extensions.
677: $extension = isset( $_POST['extension'] ) ? sanitize_key( wp_unslash( $_POST['extension'] ) ) : 'slider';
678: if ( ! isset( self::$registered_extensions[ $extension ] ) ) {
679: wp_send_json_error( array( 'message' => 'Invalid extension' ), 400 );
680: }
681:
682: $order_meta_key = self::$registered_extensions[ $extension ]->get_config( 'order_meta_key' );
683:
684: $order = get_post_meta( $post_id, $order_meta_key, true );
685: if ( ! empty( $order ) ) {
686: $order_array = is_array( $order ) ? $order : explode( ',', $order );
687: $key = array_search( $image_id, $order_array, true );
688: if ( false !== $key ) {
689: unset( $order_array[ $key ] );
690: // Re-index array and update.
691: $order_array = array_values( $order_array );
692: update_post_meta( $post_id, $order_meta_key, implode( ',', $order_array ) );
693: }
694: }
695:
696: wp_send_json_success();
697: }
698:
699: /**
700: * Update slide order on adding new attachment
701: *
702: * @param string $post_ID Post ID.
703: *
704: * @since 3.5.16
705: */
706: public function update_image_order_on_new_attachment( $post_ID ) {
707:
708: $post = get_post( $post_ID );
709: $parent = get_post( $post->post_parent );
710:
711: if ( is_null( $parent ) ) {
712: return;
713: }
714:
715: // Update order for all registered extensions.
716: foreach ( self::$registered_extensions as $extension ) {
717: $order_meta_key = $extension->get_config( 'order_meta_key' );
718: $order = get_post_meta( $parent->ID, $order_meta_key, true );
719:
720: if ( ! empty( $order ) ) {
721: update_post_meta( $parent->ID, $order_meta_key, $order . ',' . $post_ID );
722: }
723: }
724: }
725: }
726: endif;
727: