Plugin Directory

source: simple-page-ordering/trunk/class-simple-page-ordering.php

Last change on this file was 3296580, checked in by 10up, 6 months ago

Update to version 2.7.4 from GitHub

File size: 28.0 KB
Line 
1<?php
2
3namespace SimplePageOrdering;
4
5use stdClass;
6use WP_Error;
7use WP_Post;
8use WP_REST_Response;
9use WP_Query;
10
11// Useful global constants.
12define( 'SIMPLE_PAGE_ORDERING_VERSION', '2.7.4' );
13
14if ( ! class_exists( 'Simple_Page_Ordering' ) ) :
15
16        /**
17         * Simple_Page_Ordering class
18         */
19        class Simple_Page_Ordering {
20
21                /**
22                 * Handles initializing this class and returning the singleton instance after it's been cached.
23                 *
24                 * @return null|Simple_Page_Ordering
25                 */
26                public static function get_instance() {
27                        // Store the instance locally to avoid private static replication
28                        static $instance = null;
29
30                        if ( null === $instance ) {
31                                $instance = new self();
32                                self::add_actions();
33                        }
34
35                        return $instance;
36                }
37
38                /**
39                 * An empty constructor
40                 *
41                 * Purposely do nothing here
42                 */
43                public function __construct() {}
44
45                /**
46                 * Handles registering hooks that initialize this plugin.
47                 */
48                public static function add_actions() {
49                        add_action( 'load-edit.php', array( __CLASS__, 'load_edit_screen' ) );
50                        add_action( 'wp_ajax_simple_page_ordering', array( __CLASS__, 'ajax_simple_page_ordering' ) );
51                        add_action( 'wp_ajax_reset_simple_page_ordering', array( __CLASS__, 'ajax_reset_simple_page_ordering' ) );
52                        add_action( 'plugins_loaded', array( __CLASS__, 'load_textdomain' ) );
53                        add_action( 'rest_api_init', array( __CLASS__, 'rest_api_init' ) );
54
55                        // Custom edit page actions.
56                        add_action( 'post_action_spo-move-under-grandparent', array( __CLASS__, 'handle_move_under_grandparent' ) );
57                        add_action( 'post_action_spo-move-under-sibling', array( __CLASS__, 'handle_move_under_sibling' ) );
58                }
59
60                /**
61                 * Move a post in/up the post parent tree.
62                 *
63                 * This is a custom action on the edit page to modify the post parent
64                 * to be the child it's current grandparent post. If no grandparent
65                 * exists, the post becomes a top level page.
66                 *
67                 * @param int $post_id The post ID.
68                 */
69                public static function handle_move_under_grandparent( $post_id ) {
70                        $post = get_post( $post_id );
71                        if ( ! $post ) {
72                                self::redirect_to_referer();
73                        }
74
75                        check_admin_referer( "simple-page-ordering-nonce-move-{$post->ID}", 'spo_nonce' );
76
77                        if ( ! current_user_can( 'edit_post', $post->ID ) ) {
78                                wp_die( esc_html__( 'You are not allowed to edit this item.', 'simple-page-ordering' ) );
79                        }
80
81                        if ( 0 === $post->post_parent ) {
82                                // Top level. Politely continue without doing anything.
83                                self::redirect_to_referer();
84                        }
85
86                        $ancestors = get_post_ancestors( $post );
87
88                        // If only one ancestor, set to top level page.
89                        if ( 1 === count( $ancestors ) ) {
90                                $parent_id = 0;
91                        } else {
92                                $parent_id = $ancestors[1];
93                        }
94
95                        // Update the post.
96                        wp_update_post(
97                                array(
98                                        'ID'          => $post->ID,
99                                        'post_parent' => $parent_id,
100                                )
101                        );
102
103                        self::redirect_to_referer();
104                }
105
106                /**
107                 * Move a post out/down the post parent tree.
108                 *
109                 * This is a custom action on the edit page to modify the post parent
110                 * to be the child of it's previous sibling post on the current post
111                 * tree.
112                 *
113                 * @param int $post_id The post ID.
114                 */
115                public static function handle_move_under_sibling( $post_id ) {
116                        $post = get_post( $post_id );
117                        if ( ! $post ) {
118                                self::redirect_to_referer();
119                        }
120
121                        check_admin_referer( "simple-page-ordering-nonce-move-{$post->ID}", 'spo_nonce' );
122
123                        if ( ! current_user_can( 'edit_post', $post->ID ) ) {
124                                wp_die( esc_html__( 'You are not allowed to edit this item.', 'simple-page-ordering' ) );
125                        }
126
127                        list( 'top_level_pages' => $top_level_pages, 'children_pages' => $children_pages ) = self::get_walked_pages( $post->post_type );
128
129                        // Get the relevant siblings.
130                        if ( 0 === $post->post_parent ) {
131                                $siblings = $top_level_pages;
132                        } else {
133                                $siblings = $children_pages[ $post->post_parent ];
134                        }
135
136                        // Check if the post being moved is a top level page.
137                        $filtered_siblings = wp_list_filter( $siblings, array( 'ID' => $post->ID ) );
138                        if ( empty( $filtered_siblings ) ) {
139                                // Something went wrong. Do nothing.
140                                self::redirect_to_referer();
141                        }
142
143                        // Find the previous page in the sibling tree
144                        $key = array_key_first( $filtered_siblings );
145                        if ( 0 === $key ) {
146                                // It's the first page. Do nothing.
147                                self::redirect_to_referer();
148                        }
149
150                        $previous_page    = $siblings[ $key - 1 ];
151                        $previous_page_id = $previous_page->ID;
152
153                        // Update the post with the previous page as the parent.
154                        wp_update_post(
155                                array(
156                                        'ID'          => $post->ID,
157                                        'post_parent' => $previous_page_id,
158                                )
159                        );
160
161                        self::redirect_to_referer();
162                }
163
164                /**
165                 * Redirect the user after modifying the post parent.
166                 */
167                public static function redirect_to_referer() {
168                        global $post_type;
169
170                        $send_back = wp_get_referer();
171                        if ( ! $send_back ||
172                                str_contains( $send_back, 'post.php' ) ||
173                                str_contains( $send_back, 'post-new.php' ) ) {
174                                if ( 'attachment' === $post_type ) {
175                                        $send_back = admin_url( 'upload.php' );
176                                } else {
177                                        $send_back = admin_url( 'edit.php' );
178                                        if ( ! empty( $post_type ) ) {
179                                                $send_back = add_query_arg( 'post_type', $post_type, $send_back );
180                                        }
181                                }
182                        } else {
183                                $send_back = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'ids' ), $send_back );
184                        }
185
186                        wp_safe_redirect( $send_back );
187                        exit;
188                }
189
190                /**
191                 * Walk the pages and return top level and children pages.
192                 *
193                 * @param string $post_type Post type to walk.
194                 *
195                 * @return array {
196                 *    @type WP_Post[] $top_level_pages Top level pages.
197                 *    @type WP_Post[] $children_pages  Children pages.
198                 * }
199                 */
200                public static function get_walked_pages( $post_type = 'page' ) {
201                        global $wpdb;
202                        $pages = get_pages(
203                                array(
204                                        'sort_column' => 'menu_order title',
205                                        'post_type'   => $post_type,
206                                )
207                        );
208
209                        $top_level_pages = array();
210                        $children_pages  = array();
211                        $bad_parents     = array();
212
213                        foreach ( $pages as $page ) {
214                                // Catch and repair bad pages.
215                                if ( $page->post_parent === $page->ID ) {
216                                        $page->post_parent = 0;
217                                        $wpdb->update( $wpdb->posts, array( 'post_parent' => 0 ), array( 'ID' => $page->ID ) );
218                                        clean_post_cache( $page );
219                                        $bad_parents[] = $page->ID;
220                                }
221
222                                if ( $page->post_parent > 0 ) {
223                                        $children_pages[ $page->post_parent ][] = $page;
224                                } else {
225                                        $top_level_pages[] = $page;
226                                }
227                        }
228                        // Reprime post cache for bad parents.
229                        _prime_post_caches( $bad_parents, false, false );
230
231                        return array(
232                                'top_level_pages' => $top_level_pages,
233                                'children_pages'  => $children_pages,
234                        );
235                }
236
237                /**
238                 * Loads the plugin textdomain
239                 */
240                public static function load_textdomain() {
241                        load_plugin_textdomain( 'simple-page-ordering', false, dirname( plugin_basename( __FILE__ ) ) . '/localization/' );
242                }
243
244                /**
245                 * Determine whether given post type is sortable or not.
246                 *
247                 * @param string $post_type Post type to check.
248                 *
249                 * @return boolean
250                 */
251                private static function is_post_type_sortable( $post_type = 'post' ) {
252                        $sortable = ( post_type_supports( $post_type, 'page-attributes' ) || is_post_type_hierarchical( $post_type ) );
253
254                        /**
255                         * Change default ordering support for a post type.
256                         *
257                         * @since 2.0.0
258                         *
259                         * @param boolean $sortable Whether this post type is sortable or not.
260                         * @param string  $post_type The post type being checked.
261                         */
262                        return apply_filters( 'simple_page_ordering_is_sortable', $sortable, $post_type );
263                }
264
265                /**
266                 * Load up page ordering scripts for the edit screen
267                 */
268                public static function load_edit_screen() {
269                        $screen    = get_current_screen();
270                        $post_type = $screen->post_type;
271
272                        // is post type sortable?
273                        $sortable = self::is_post_type_sortable( $post_type );
274                        if ( ! $sortable ) {
275                                return;
276                        }
277
278                        // does user have the right to manage these post objects?
279                        if ( ! self::check_edit_others_caps( $post_type ) ) {
280                                return;
281                        }
282
283                        // add view by menu order to views
284                        add_filter(
285                                'views_' . $screen->id,
286                                array(
287                                        __CLASS__,
288                                        'sort_by_order_link',
289                                )
290                        );
291                        add_action( 'pre_get_posts', array( __CLASS__, 'filter_query' ) );
292                        add_action( 'wp', array( __CLASS__, 'wp' ) );
293                        add_action( 'admin_head', array( __CLASS__, 'admin_head' ) );
294                        add_action( 'page_row_actions', array( __CLASS__, 'page_row_actions' ), 10, 2 );
295                }
296
297                /**
298                 * This is to enable pagination.
299                 *
300                 * @param WP_Query $query The WP_Query instance (passed by reference).
301                 */
302                public static function filter_query( $query ) {
303                        if ( ! $query->is_main_query() ) {
304                                return;
305                        }
306
307                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
308                        $is_simple_page_ordering = isset( $_GET['id'] ) ? 'simple-page-ordering' === $_GET['id'] : false;
309
310                        if ( ! $is_simple_page_ordering ) {
311                                return;
312                        }
313
314                        $query->set( 'posts_per_page', -1 );
315                }
316
317                /**
318                 * when we load up our posts query, if we're actually sorting by menu order, initialize sorting scripts
319                 */
320                public static function wp() {
321                        $orderby   = get_query_var( 'orderby' );
322                        $screen    = get_current_screen();
323                        $post_type = $screen->post_type ?? 'post';
324
325                        if ( ( is_string( $orderby ) && 0 === strpos( $orderby, 'menu_order' ) ) || ( isset( $orderby['menu_order'] ) && 'ASC' === $orderby['menu_order'] ) ) {
326
327                                $script_name       = 'dist/js/simple-page-ordering.js';
328                                $script_asset_path = plugin_dir_path( __FILE__ ) . 'dist/js/simple-page-ordering.asset.php';
329                                $script_asset      = file_exists( $script_asset_path )
330                                        ? require $script_asset_path
331                                        : false;
332
333                                if ( false !== $script_asset ) {
334                                        $script_url = plugins_url( $script_name, __FILE__ );
335                                        wp_enqueue_script( 'simple-page-ordering', $script_url, $script_asset['dependencies'], $script_asset['version'], true );
336
337                                        wp_localize_script(
338                                                'simple-page-ordering',
339                                                'simple_page_ordering_localized_data',
340                                                array(
341                                                        '_wpnonce'         => wp_create_nonce( 'simple-page-ordering-nonce' ),
342                                                        /* translators: %1$s is replaced with the post type name */
343                                                        'confirmation_msg' => sprintf( esc_html__( 'Are you sure you want to reset the ordering of the "%1$s" post type?', 'simple-page-ordering' ), $post_type ),
344                                                )
345                                        );
346
347                                        wp_enqueue_style( 'simple-page-ordering', plugins_url( '/dist/css/simple-page-ordering.css', __FILE__ ), [], $script_asset['version'] );
348                                } else {
349                                        add_action(
350                                                'admin_notices',
351                                                function () {
352                                                        ?>
353                                                        <div class="notice notice-warning is-dismissible">
354                                                                <p><?php echo wp_kses_post( __( 'It looks like you are using a development copy of <strong>Simple Page Ordering</strong>. Please run <code>npm i; npm run build</code> to create assets.', 'simple-page-ordering' ) ); ?></p>
355                                                        </div>
356                                                        <?php
357                                                }
358                                        );
359                                }
360                        }
361                }
362
363                /**
364                 * Add page ordering help to the help tab
365                 */
366                public static function admin_head() {
367                        $screen    = get_current_screen();
368                        $post_type = $screen->post_type ?? 'post';
369
370                        $screen->add_help_tab(
371                                array(
372                                        'id'      => 'simple_page_ordering_help_tab',
373                                        'title'   => esc_html__( 'Simple Page Ordering', 'simple-page-ordering' ),
374                                        'content' => sprintf(
375                                                '<p>%s</p><a href="#" id="simple-page-ordering-reset" data-posttype="%s">%s</a>',
376                                                esc_html__( 'To reposition an item, simply drag and drop the row by "clicking and holding" it anywhere (outside of the links and form controls) and moving it to its new position.', 'simple-page-ordering' ),
377                                                esc_attr( get_query_var( 'post_type' ) ),
378                                                /* translators: %1$s is replaced with the post type name */
379                                                sprintf( esc_html__( 'Reset %1$s order', 'simple-page-ordering' ), $post_type )
380                                        ),
381                                )
382                        );
383                }
384
385                /**
386                 * Modify the row actions for hierarchical post types.
387                 *
388                 * This adds the actions to change the parent/child relationships.
389                 *
390                 * @param array   $actions An array of row action links.
391                 * @param WP_Post $post    The post object.
392                 */
393                public static function page_row_actions( $actions, $post ) {
394                        $post = get_post( $post );
395                        if ( ! $post ) {
396                                return $actions;
397                        }
398
399                        if ( ! current_user_can( 'edit_post', $post->ID ) ) {
400                                return $actions;
401                        }
402
403                        list( 'top_level_pages' => $top_level_pages, 'children_pages' => $children_pages ) = self::get_walked_pages( $post->post_type );
404
405                        $edit_link                   = get_edit_post_link( $post->ID, 'raw' );
406                        $move_under_grandparent_link = add_query_arg(
407                                array(
408                                        'action'    => 'spo-move-under-grandparent',
409                                        'spo_nonce' => wp_create_nonce( "simple-page-ordering-nonce-move-{$post->ID}" ),
410                                        'post_type' => $post->post_type,
411                                ),
412                                $edit_link
413                        );
414                        $move_under_sibling_link     = add_query_arg(
415                                array(
416                                        'action'    => 'spo-move-under-sibling',
417                                        'spo_nonce' => wp_create_nonce( "simple-page-ordering-nonce-move-{$post->ID}" ),
418                                        'post_type' => $post->post_type,
419                                ),
420                                $edit_link
421                        );
422
423                        $parent_id = $post->post_parent;
424                        if ( $parent_id ) {
425                                $actions['spo-move-under-grandparent'] = sprintf(
426                                        '<a href="%s">%s</a>',
427                                        esc_url( $move_under_grandparent_link ),
428                                        sprintf(
429                                                /* translators: %s: parent page/post title */
430                                                __( 'Move out from under %s', 'simple-page-ordering' ),
431                                                get_the_title( $parent_id )
432                                        )
433                                );
434                        }
435
436                        // Get the relevant siblings.
437                        if ( 0 === $post->post_parent ) {
438                                $siblings = $top_level_pages;
439                        } else {
440                                $siblings = $children_pages[ $post->post_parent ] ?? [];
441                        }
442
443                        // Assume no sibling.
444                        $sibling = 0;
445                        // Check if the post being moved is a top level page.
446                        $filtered_siblings = wp_list_filter( $siblings, array( 'ID' => $post->ID ) );
447                        if ( ! empty( $filtered_siblings ) ) {
448                                // Find the previous page in the sibling tree
449                                $key = array_key_first( $filtered_siblings );
450                                if ( 0 === $key ) {
451                                        // It's the first page, can't do anything.
452                                        $sibling = 0;
453                                } else {
454                                        $previous_page = $siblings[ $key - 1 ];
455                                        $sibling       = $previous_page->ID;
456                                }
457                        }
458
459                        if ( $sibling ) {
460                                $actions['spo-move-under-sibling'] = sprintf(
461                                        '<a href="%s">%s</a>',
462                                        esc_url( $move_under_sibling_link ),
463                                        sprintf(
464                                                /* translators: %s: sibling page/post title */
465                                                __( 'Move under %s', 'simple-page-ordering' ),
466                                                get_the_title( $sibling )
467                                        )
468                                );
469                        }
470
471                        return $actions;
472                }
473
474                /**
475                 * Page ordering ajax callback
476                 *
477                 * @return void
478                 */
479                public static function ajax_simple_page_ordering() {
480                        // check and make sure we have what we need
481                        if ( empty( $_POST['id'] ) || ( ! isset( $_POST['previd'] ) && ! isset( $_POST['nextid'] ) ) ) {
482                                die( - 1 );
483                        }
484
485                        $nonce = isset( $_POST['_wpnonce'] ) ? sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ) : '';
486
487                        if ( ! wp_verify_nonce( $nonce, 'simple-page-ordering-nonce' ) ) {
488                                die( -1 );
489                        }
490
491                        $post_id  = empty( $_POST['id'] ) ? false : (int) $_POST['id'];
492                        $previd   = empty( $_POST['previd'] ) ? false : (int) $_POST['previd'];
493                        $nextid   = empty( $_POST['nextid'] ) ? false : (int) $_POST['nextid'];
494                        $start    = empty( $_POST['start'] ) ? 1 : (int) $_POST['start'];
495                        $excluded = empty( $_POST['excluded'] ) ? array( $_POST['id'] ) : array_filter( (array) json_decode( $_POST['excluded'] ), 'intval' );
496
497                        // real post?
498                        $post = empty( $post_id ) ? false : get_post( (int) $post_id );
499                        if ( ! $post ) {
500                                die( - 1 );
501                        }
502
503                        // does user have the right to manage these post objects?
504                        if ( ! self::check_edit_others_caps( $post->post_type ) ) {
505                                die( - 1 );
506                        }
507
508                        $result = self::page_ordering( $post_id, $previd, $nextid, $start, $excluded );
509
510                        if ( is_wp_error( $result ) ) {
511                                die( -1 );
512                        }
513
514                        die( wp_json_encode( $result ) );
515                }
516
517                /**
518                 * Page ordering reset ajax callback
519                 *
520                 * @return void
521                 */
522                public static function ajax_reset_simple_page_ordering() {
523                        global $wpdb;
524
525                        $nonce = isset( $_POST['_wpnonce'] ) ? sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ) : '';
526
527                        if ( ! wp_verify_nonce( $nonce, 'simple-page-ordering-nonce' ) ) {
528                                die( -1 );
529                        }
530
531                        // check and make sure we have what we need
532                        $post_type = isset( $_POST['post_type'] ) ? sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) : '';
533
534                        if ( empty( $post_type ) ) {
535                                die( -1 );
536                        }
537
538                        // does user have the right to manage these post objects?
539                        if ( ! self::check_edit_others_caps( $post_type ) ) {
540                                die( -1 );
541                        }
542
543                        // reset the order of all posts of given post type
544                        $wpdb->update( 'wp_posts', array( 'menu_order' => 0 ), array( 'post_type' => $post_type ), array( '%d' ), array( '%s' ) );
545
546                        die( 0 );
547                }
548
549                /**
550                 * Page ordering function
551                 *
552                 * @param int   $post_id  The post ID.
553                 * @param int   $previd   The previous post ID.
554                 * @param int   $nextid   The next post ID.
555                 * @param int   $start    The start index.
556                 * @param array $excluded Array of post IDs.
557                 *
558                 * @return object|WP_Error|"children"
559                 */
560                public static function page_ordering( $post_id, $previd, $nextid, $start, $excluded ) {
561                        // real post?
562                        $post = empty( $post_id ) ? false : get_post( (int) $post_id );
563                        if ( ! $post ) {
564                                return new WP_Error( 'invalid', __( 'Missing mandatory parameters.', 'simple-page-ordering' ) );
565                        }
566
567                        // Badly written plug-in hooks for save post can break things.
568                        if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
569                                error_reporting( 0 ); // phpcs:ignore
570                        }
571
572                        global $wp_version;
573
574                        $previd   = empty( $previd ) ? false : (int) $previd;
575                        $nextid   = empty( $nextid ) ? false : (int) $nextid;
576                        $start    = empty( $start ) ? 1 : (int) $start;
577                        $excluded = empty( $excluded ) ? array( $post_id ) : array_filter( (array) $excluded, 'intval' );
578
579                        $new_pos     = array(); // store new positions for ajax
580                        $return_data = new stdClass();
581
582                        do_action( 'simple_page_ordering_pre_order_posts', $post, $start );
583
584                        // attempt to get the intended parent... if either sibling has a matching parent ID, use that
585                        $parent_id        = $post->post_parent;
586                        $next_post_parent = $nextid ? wp_get_post_parent_id( $nextid ) : false;
587
588                        if ( $previd === $next_post_parent ) {    // if the preceding post is the parent of the next post, move it inside
589                                $parent_id = $next_post_parent;
590                        } elseif ( $next_post_parent !== $parent_id ) {  // otherwise, if the next post's parent isn't the same as our parent, we need to study
591                                $prev_post_parent = $previd ? wp_get_post_parent_id( $previd ) : false;
592                                if ( $prev_post_parent !== $parent_id ) {    // if the previous post is not our parent now, make it so!
593                                        $parent_id = ( false !== $prev_post_parent ) ? $prev_post_parent : $next_post_parent;
594                                }
595                        }
596
597                        // if the next post's parent isn't our parent, it might as well be false (irrelevant to our query)
598                        if ( $next_post_parent !== $parent_id ) {
599                                $nextid = false;
600                        }
601
602                        $max_sortable_posts = (int) apply_filters( 'simple_page_ordering_limit', 50 );    // should reliably be able to do about 50 at a time
603
604                        if ( $max_sortable_posts < 5 ) {    // don't be ridiculous!
605                                $max_sortable_posts = 50;
606                        }
607
608                        // we need to handle all post stati, except trash (in case of custom stati)
609                        $post_stati = get_post_stati(
610                                array(
611                                        'show_in_admin_all_list' => true,
612                                )
613                        );
614
615                        $siblings_query = array(
616                                'depth'                  => 1,
617                                'posts_per_page'         => $max_sortable_posts,
618                                'post_type'              => $post->post_type,
619                                'post_status'            => $post_stati,
620                                'post_parent'            => $parent_id,
621                                'post__not_in'           => $excluded, // phpcs:ignore
622                                'orderby'                => array(
623                                        'menu_order' => 'ASC',
624                                        'title'      => 'ASC',
625                                ),
626                                'update_post_term_cache' => false,
627                                'update_post_meta_cache' => false,
628                                'suppress_filters'       => true, // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.SuppressFiltersTrue
629                                'ignore_sticky_posts'    => true,
630                        );
631
632                        if ( version_compare( $wp_version, '4.0', '<' ) ) {
633                                $siblings_query['orderby'] = 'menu_order title';
634                                $siblings_query['order']   = 'ASC';
635                        }
636
637                        $siblings = new WP_Query( $siblings_query ); // fetch all the siblings (relative ordering)
638
639                        // don't waste overhead of revisions on a menu order change (especially since they can't *all* be rolled back at once)
640                        remove_action( 'post_updated', 'wp_save_post_revision' );
641
642                        foreach ( $siblings->posts as $sibling ) :
643                                // don't handle the actual post
644                                if ( $sibling->ID === $post->ID ) {
645                                        continue;
646                                }
647
648                                // if this is the post that comes after our repositioned post, set our repositioned post position and increment menu order
649                                if ( $nextid === $sibling->ID ) {
650                                        wp_update_post(
651                                                array(
652                                                        'ID'          => $post->ID,
653                                                        'menu_order'  => $start,
654                                                        'post_parent' => $parent_id,
655                                                )
656                                        );
657
658                                        $ancestors            = get_post_ancestors( $post->ID );
659                                        $new_pos[ $post->ID ] = array(
660                                                'menu_order'  => $start,
661                                                'post_parent' => $parent_id,
662                                                'depth'       => count( $ancestors ),
663                                        );
664
665                                        $start ++;
666                                }
667
668                                // if repositioned post has been set, and new items are already in the right order, we can stop
669                                if ( isset( $new_pos[ $post->ID ] ) && $sibling->menu_order >= $start ) {
670                                        $return_data->next = false;
671                                        break;
672                                }
673
674                                // set the menu order of the current sibling and increment the menu order
675                                if ( $sibling->menu_order !== $start ) {
676                                        wp_update_post(
677                                                array(
678                                                        'ID'         => $sibling->ID,
679                                                        'menu_order' => $start,
680                                                )
681                                        );
682                                }
683                                $new_pos[ $sibling->ID ] = $start;
684                                $start ++;
685
686                                if ( ! $nextid && $previd === $sibling->ID ) {
687                                        wp_update_post(
688                                                array(
689                                                        'ID'          => $post->ID,
690                                                        'menu_order'  => $start,
691                                                        'post_parent' => $parent_id,
692                                                )
693                                        );
694
695                                        $ancestors            = get_post_ancestors( $post->ID );
696                                        $new_pos[ $post->ID ] = array(
697                                                'menu_order'  => $start,
698                                                'post_parent' => $parent_id,
699                                                'depth'       => count( $ancestors ),
700                                        );
701                                        $start ++;
702                                }
703
704                        endforeach;
705
706                        // max per request
707                        if ( ! isset( $return_data->next ) && $siblings->max_num_pages > 1 ) {
708                                $return_data->next = array(
709                                        'id'       => $post->ID,
710                                        'previd'   => $previd,
711                                        'nextid'   => $nextid,
712                                        'start'    => $start,
713                                        'excluded' => array_merge( array_keys( $new_pos ), $excluded ),
714                                );
715                        } else {
716                                $return_data->next = false;
717                        }
718
719                        do_action( 'simple_page_ordering_ordered_posts', $post, $new_pos );
720
721                        if ( ! $return_data->next ) {
722                                // if the moved post has children, we need to refresh the page (unless we're continuing)
723                                $children = new WP_Query(
724                                        array(
725                                                'posts_per_page'         => 1,
726                                                'post_type'              => $post->post_type,
727                                                'post_status'            => $post_stati,
728                                                'post_parent'            => $post->ID,
729                                                'fields'                 => 'ids',
730                                                'update_post_term_cache' => false,
731                                                'update_post_meta_cache' => false,
732                                                'ignore_sticky'          => true,
733                                                'no_found_rows'          => true,
734                                        )
735                                );
736
737                                if ( $children->have_posts() ) {
738                                        return 'children';
739                                }
740                        }
741
742                        $return_data->new_pos = $new_pos;
743
744                        return $return_data;
745                }
746
747                /**
748                 * Append a sort by order link to the post actions
749                 *
750                 * @param array $views An array of available list table views.
751                 *
752                 * @return array
753                 */
754                public static function sort_by_order_link( $views ) {
755                        $class        = ( get_query_var( 'orderby' ) === 'menu_order title' ) ? 'current' : '';
756                        $query_string = remove_query_arg( array( 'orderby', 'order' ) );
757                        if ( ! is_post_type_hierarchical( get_post_type() ) ) {
758                                $query_string = add_query_arg( 'orderby', 'menu_order title', $query_string );
759                                $query_string = add_query_arg( 'order', 'asc', $query_string );
760                                $query_string = add_query_arg( 'id', 'simple-page-ordering', $query_string );
761                        }
762                        $views['byorder'] = sprintf( '<a href="%s" class="%s">%s</a>', esc_url( $query_string ), $class, __( 'Sort by Order', 'simple-page-ordering' ) );
763
764                        return $views;
765                }
766
767                /**
768                 * Checks to see if the current user has the capability to "edit others" for a post type
769                 *
770                 * @param string $post_type Post type name
771                 *
772                 * @return bool True or false
773                 */
774                private static function check_edit_others_caps( $post_type ) {
775                        $post_type_object = get_post_type_object( $post_type );
776                        $edit_others_cap  = empty( $post_type_object ) ? 'edit_others_' . $post_type . 's' : $post_type_object->cap->edit_others_posts;
777
778                        return apply_filters( 'simple_page_ordering_edit_rights', current_user_can( $edit_others_cap ), $post_type );
779                }
780
781                /**
782                 * Registers the API endpoint for sorting from the REST endpoint
783                 */
784                public static function rest_api_init() {
785                        register_rest_route(
786                                'simple-page-ordering/v1',
787                                'page_ordering',
788                                [
789                                        'methods'             => 'POST',
790                                        'callback'            => array( __CLASS__, 'rest_page_ordering' ),
791                                        'permission_callback' => array( __CLASS__, 'rest_page_ordering_permissions_check' ),
792                                        'args'                => [
793                                                'id'      => [
794                                                        'description' => __( 'ID of item we want to sort', 'simple-page-ordering' ),
795                                                        'required'    => true,
796                                                        'type'        => 'integer',
797                                                        'minimum'     => 1,
798                                                ],
799                                                'previd'  => [
800                                                        'description' => __( 'ID of item we want to be previous to after sorting', 'simple-page-ordering' ),
801                                                        'required'    => true,
802                                                        'type'        => [ 'boolean', 'integer' ],
803                                                ],
804                                                'nextid'  => [
805                                                        'description' => __( 'ID of item we want to be next to after sorting', 'simple-page-ordering' ),
806                                                        'required'    => true,
807                                                        'type'        => [ 'boolean', 'integer' ],
808                                                ],
809                                                'start'   => [
810                                                        'default'     => 1,
811                                                        'description' => __( 'Index we start with when sorting', 'simple-page-ordering' ),
812                                                        'required'    => false,
813                                                        'type'        => 'integer',
814                                                ],
815                                                'exclude' => [
816                                                        'default'     => [],
817                                                        'description' => __( 'Array of IDs we want to exclude', 'simple-page-ordering' ),
818                                                        'required'    => false,
819                                                        'type'        => 'array',
820                                                        'items'       => [
821                                                                'type' => 'integer',
822                                                        ],
823                                                ],
824                                        ],
825                                ]
826                        );
827                }
828
829                /**
830                 * Check if a given request has access to reorder content.
831                 *
832                 * This check ensures the current user making the request has
833                 * proper permissions to edit the item, that the post type
834                 * is allowed in REST requests and the post type is sortable.
835                 *
836                 * @since 2.5.1
837                 *
838                 * @param WP_REST_Request $request Full data about the request.
839                 * @return bool|WP_Error
840                 */
841                public static function rest_page_ordering_permissions_check( \WP_REST_Request $request ) {
842                        $post_id = $request->get_param( 'id' );
843
844                        // Ensure we have a logged in user that can edit the item.
845                        if ( ! current_user_can( 'edit_post', $post_id ) ) {
846                                return false;
847                        }
848
849                        $post_type     = get_post_type( $post_id );
850                        $post_type_obj = get_post_type_object( $post_type );
851
852                        // Ensure the post type is allowed in REST endpoints.
853                        if ( ! $post_type || empty( $post_type_obj ) || empty( $post_type_obj->show_in_rest ) ) {
854                                return false;
855                        }
856
857                        // Ensure this post type is sortable.
858                        if ( ! self::is_post_type_sortable( $post_type ) ) {
859                                return new WP_Error( 'not_enabled', esc_html__( 'This post type is not sortable.', 'simple-page-ordering' ) );
860                        }
861
862                        return true;
863                }
864
865                /**
866                 * Handle REST page sorting
867                 *
868                 * @param WP_REST_Request $request The REST request object.
869                 */
870                public static function rest_page_ordering( \WP_REST_Request $request ) {
871                        $post_id  = empty( $request->get_param( 'id' ) ) ? false : (int) $request->get_param( 'id' );
872                        $previd   = empty( $request->get_param( 'previd' ) ) ? false : (int) $request->get_param( 'previd' );
873                        $nextid   = empty( $request->get_param( 'nextid' ) ) ? false : (int) $request->get_param( 'nextid' );
874                        $start    = empty( $request->get_param( 'start' ) ) ? 1 : (int) $request->get_param( 'start' );
875                        $excluded = empty( $request->get_param( 'excluded' ) ) ? array( $request->get_param( 'id' ) ) : array_filter( (array) json_decode( $request->get_param( 'excluded' ) ), 'intval' );
876
877                        // Check and make sure we have what we need.
878                        if ( false === $post_id || ( false === $previd && false === $nextid ) ) {
879                                return new WP_Error( 'invalid', __( 'Missing mandatory parameters.', 'simple-page-ordering' ) );
880                        }
881
882                        $page_ordering = self::page_ordering( $post_id, $previd, $nextid, $start, $excluded );
883
884                        if ( is_wp_error( $page_ordering ) ) {
885                                return $page_ordering;
886                        }
887
888                        return new WP_REST_Response(
889                                array(
890                                        'status'        => 200,
891                                        'response'      => 'success',
892                                        'body_response' => $page_ordering,
893                                )
894                        );
895                }
896        }
897
898        Simple_Page_Ordering::get_instance();
899
900endif;
Note: See TracBrowser for help on using the repository browser.