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