-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-reorder.php
638 lines (590 loc) · 19.9 KB
/
class-reorder.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
<?php
/**
* Reorder posts
*
* @package WordPress
* @subpackage Metronet Reorder Posts plugin
*/
/**
* Reorder posts
* Adds drag and drop editor for reordering WordPress posts
*
* Based on work by Scott Basgaard and Ronald Huereca
*
* To use this class, simply instantiate it using an argument to set the post type as follows:
* new MN_Reorder( array( 'post_type' => 'post', 'order'=> 'ASC' ) );
*
* @copyright Copyright (c), Metronet
* @license http://www.gnu.org/licenses/gpl.html GPL
* @author Ryan Hellyer <[email protected]>
* @since 1.0
*/
final class MN_Reorder {
/**
* @var $post_type
* @desc Post type to be reordered
* @access private
*/
private $post_type;
/**
* @var $posts_per_page
* @desc How many posts to show
* @access private
*/
private $posts_per_page;
/**
* @var $offset
* @desc How many posts to offset by
* @access private
*/
private $offset;
/**
* @var $heading
* @desc Admin page heading
* @access private
*/
private $heading;
/**
* @var $initial
* @desc HTML outputted at end of admin page
* @access private
*/
private $initial;
/**
* @var $final
* @desc HTML outputted at end of admin page
* @access private
*/
private $final;
/**
* @var $post_statush
* @desc The post status of posts to be reordered
* @access private
*/
private $post_status;
/**
* @var $menu_label
* @desc Admin page menu label
* @access private
*/
private $menu_label;
/**
* @var $order
* @desc ASC or DESC
* @access private
*/
private $order;
/**
* @var $reorder_page
* @desc Where the reorder interface is being added
* @access private
*/
private $reorder_page = '';
/**
* Get method for post status
*
* @author Ronald Huereca <[email protected]>
* @since Reorder 2.1.0
* @access public
* @returns string $post_status Post Status of Posts
*/
public function get_post_status() {
return $this->post_status;
}
/**
* Get method for post order
*
* @author Ronald Huereca <[email protected]>
* @since Reorder 2.1.0
* @access public
* @returns string $order Order of posts (ASC or DESC)
*/
public function get_post_order() {
return $this->order;
}
/**
* Get method for posts per page
*
* @author Ronald Huereca <[email protected]>
* @since Reorder 2.1.0
* @access public
* @returns int $posts_per_page How many posts to display
*/
public function get_posts_per_page() {
return $this->posts_per_page;
}
/**
* Get method for post offset used in pagination
*
* @author Ronald Huereca <[email protected]>
* @since Reorder 2.1.0
* @access public
* @returns int $offset Offset of posts
*/
public function get_offset() {
return $this->offset;
}
/**
* Class constructor
*
* Sets definitions
* Adds methods to appropriate hooks
*
* @author Ryan Hellyer <[email protected]>
* @since Reorder 1.0
* @access public
* @param array $args If not set, then uses $defaults instead
*/
public function __construct( $args = array() ) {
// Get posts per page
$user_id = get_current_user_id();
$posts_per_page = get_user_meta( $user_id, 'reorder_items_per_page', true );
if ( ! is_numeric( $posts_per_page ) ) {
$posts_per_page = 50;
}
$offset = $posts_per_page - 2;
// Parse arguments
$defaults = array(
'post_type' => 'post', // Setting the post type to be reordered
'order' => 'ASC', // Setting the order of the posts
'heading' => __( 'Reorder', 'metronet-reorder-posts' ), // Default text for heading
'initial' => '', // Initial text displayed before sorting code
'final' => '', // Initial text displayed before sorting code
'post_status' => 'publish', // Post status of posts to be reordered
'menu_label' => __( 'Reorder', 'metronet-reorder-posts' ), //Menu label for the post type
'offset' => $offset,
'posts_per_page' => $posts_per_page
);
$args = wp_parse_args( $args, $defaults );
// Set variables
$this->post_type = $args[ 'post_type' ];
$this->order = $args[ 'order' ];;
$this->heading = $args[ 'heading' ];
$this->initial = $args[ 'initial' ];
$this->final = $args[ 'final' ];
$this->menu_label = $args[ 'menu_label' ];
$this->post_status = $args[ 'post_status' ];
//Get offset and posts_per_page
$this->posts_per_page = absint( $args[ 'posts_per_page' ] ); //todo - filterable?
$this->offset = absint( $args[ 'offset' ] ); //todo - filterable?
if ( $this->offset > $this->posts_per_page ) {
$this->offset = $this->posts_per_page;
}
// Add actions
add_filter( 'set-screen-option', array( $this, 'add_screen_option_save' ), 10, 3 );
add_action( 'wp_ajax_post_sort', array( $this, 'ajax_save_post_order' ) );
add_action( 'admin_menu', array( $this, 'enable_post_sort' ), 10, 'page' );
add_action( 'metronet_reorder_posts_interface_' . $this->post_type, array( $this, 'output_interface' ) );
}
/**
* Adjust the found posts for the offset
*
* @author Ronald Huereca <[email protected]>
* @since Reorder 2.1.0
* @access public
* @returns int $found_posts Number of posts
*/
public function adjust_offset_pagination( $found_posts, $query ) {
//This sometimes will have a bug of showing an extra page, but it doesn't break anything, so leaving it for now.
if( $found_posts > $this->posts_per_page ) {
$num_pages = $found_posts / $this->offset;
$found_posts = (string)round( $num_pages * $this->posts_per_page );
}
return $found_posts;
}
/**
* Saving the post oder for later use
*
* @author Ronald Huereca <[email protected]>
* @since Reorder 1.0
* @access public
* @global object $wpdb The primary global database object used internally by WordPress
*/
public function ajax_save_post_order() {
global $wpdb;
if ( !current_user_can( 'edit_pages' ) ) die( '' );
// Verify nonce value, for security purposes
if ( !wp_verify_nonce( $_POST['nonce'], 'sortnonce' ) ) die( '' );
//Get Ajax Vars
$post_parent = isset( $_POST[ 'post_parent' ] ) ? absint( $_POST[ 'post_parent' ] ) : 0;
$menu_order_start = isset( $_POST[ 'start' ] ) ? absint( $_POST[ 'start' ] ) : 0;
$post_id = isset( $_POST[ 'post_id' ] ) ? absint( $_POST[ 'post_id' ] ) : 0;
$post_menu_order = isset( $_POST[ 'menu_order' ] ) ? absint( $_POST[ 'menu_order' ] ) : 0;
$posts_to_exclude = isset( $_POST[ 'excluded' ] ) ? array_filter( $_POST[ 'excluded' ], 'absint' ) : array();
$post_type = isset( $_POST[ 'post_type' ] ) ? sanitize_text_field( $_POST[ 'post_type' ] ) : false;
if ( !$post_type ) die( '' );
//Performance
remove_action( 'pre_post_update', 'wp_save_post_revision' );
//Build Initial Return
$return = array();
$return[ 'more_posts' ] = false;
$return[ 'action' ] = 'post_sort';
$return[ 'post_parent' ] = $post_parent;
$return[ 'nonce' ] = sanitize_text_field( $_POST[ 'nonce' ] );
$return[ 'post_id'] = $post_id;
$return[ 'menu_order' ] = $post_menu_order;
$return[ 'post_type' ] = $post_type;
//Update post if passed - Should run only on beginning of first iteration
if( $post_id > 0 && !isset( $_POST[ 'more_posts' ] ) ) {
$wpdb->update(
$wpdb->posts,
array( 'menu_order' => $post_menu_order, 'post_parent' => $post_parent ), array( 'ID' => $post_id )
);
clean_post_cache( $post_id );
$posts_to_exclude[] = $post_id;
}
//Build Query
$query_args = array(
'post_type' => $post_type,
'orderby' => 'menu_order title',
'order' => $this->order,
'posts_per_page' => 50,
'suppress_filters' => true,
'ignore_sticky_posts' => true,
'post_status' => $this->post_status,
'post_parent' => $post_parent,
'post__not_in' => $posts_to_exclude,
'update_post_term_cache' => false,
'update_post_meta_cache' => false
);
$posts = new WP_Query( $query_args );
$start = $menu_order_start;
if ( $posts->have_posts() ) {
foreach( $posts->posts as $post ) {
//Increment start if matches menu_order and there is a post to change
if ( $start == $post_menu_order && $post_id > 0 ) {
$start++;
}
if ( $post_id != $post->ID ) {
//Update post and counts
$wpdb->update(
$wpdb->posts,
array( 'menu_order' => $start, 'post_parent' => $post_parent ),
array( 'ID' => $post->ID )
);
clean_post_cache( $post );
}
$posts_to_exclude[] = $post->ID;
$start++;
}
$return[ 'excluded' ] = $posts_to_exclude;
$return[ 'start' ] = $start;
if ( $posts->max_num_pages > 1 ) {
$return[ 'more_posts' ] = true;
} else {
$return[ 'more_posts' ] = false;
}
die( json_encode( $return ) );
} else {
die( json_encode( $return ) );
}
} //end ajax_save_post_order
/**
* Print styles to admin page
*
* @author Ryan Hellyer <[email protected]>
* @since Reorder 1.0
* @access public
* @global string $pagenow Used internally by WordPress to designate what the current page is in the admin panel
*/
public function print_styles() {
wp_enqueue_style( 'reorderpages_style', REORDER_URL . '/css/admin.css', array(), '20160813' );
}
/**
* Print scripts to admin page
*
* @author Ryan Hellyer <[email protected]>
* @since Reorder 1.0
* @access public
* @global string $pagenow Used internally by WordPress to designate what the current page is in the admin panel
*/
public function print_scripts() {
wp_enqueue_script( 'jquery-ui-touch-punch', REORDER_URL . '/scripts/jquery.ui.touch-punch.js', array( 'jquery-ui-sortable' ), '0.2.3', true );
wp_register_script( 'reorder_nested', REORDER_URL . '/scripts/jquery.mjs.nestedSortable.js', array( 'jquery-ui-touch-punch' ), '2.0.1', true );
wp_enqueue_script( 'reorder_posts', REORDER_URL . '/scripts/sort.js', array( 'reorder_nested' ), '20210214', true );
wp_localize_script( 'reorder_posts', 'reorder_posts', array(
'action' => 'post_sort',
'expand' => esc_js( __( 'Expand', 'metronet-reorder-posts' ) ),
'collapse' => esc_js( __( 'Collapse', 'metronet-reorder-posts' ) ),
'sortnonce' => wp_create_nonce( 'sortnonce' ),
'hierarchical' => is_post_type_hierarchical( $this->post_type ) ? 'true' : 'false',
) );
}
/**
* Add submenu
*
* @author Ryan Hellyer <[email protected]>
* @since Reorder 1.0
* @access public
*/
public function enable_post_sort() {
$post_type = $this->post_type;
if ( 'post' != $post_type ) {
$menu_location = apply_filters( 'metronet_reorder_menu_location_' . $post_type, 'edit.php?post_type=' . $post_type, $post_type );
$hook = add_submenu_page(
$menu_location, // Parent slug
$this->heading, // Page title (unneeded since specified directly)
apply_filters( 'metronet_reorder_menu_label_' . $post_type, $this->menu_label , $post_type ), // Menu title
'edit_pages', // Capability
'reorder-' . $post_type, // Menu slug
array( $this, 'sort_posts' ) // Callback function
);
$this->reorder_page = add_query_arg( array( 'page' => 'reorder-' . $post_type ), admin_url( $menu_location ) );
}
else {
$hook = add_posts_page(
$this->heading, // Page title (unneeded since specified directly)
apply_filters( 'metronet_reorder_menu_label_' . $post_type, $this->menu_label ), // Menu title
'edit_pages', // Capability
'reorder-posts', // Menu slug
array( $this, 'sort_posts' ) // Callback function
);
$this->reorder_page = add_query_arg( array( 'page' => 'reorder-posts' ), admin_url( 'edit.php' ) );
}
add_action( "load-$hook", array( $this, 'add_screen_option' ) );
do_action( 'metronet_reorder_posts_add_menu_' . $post_type, $hook ); //Allow other plugin authors to add scripts/styles to our menu items
do_action( 'metronet_reorder_menu_url_' . $post_type, $this->reorder_page );
add_action( 'admin_print_styles-' . $hook, array( $this, 'print_styles' ) );
add_action( 'admin_print_scripts-' . $hook, array( $this, 'print_scripts' ) );
}
/**
* Add screen option for setting items per page
*
* @author Ronald Huereca
* @since 2.3.0
* @access public
*/
public function add_screen_option() {
$args = array(
'label' => __( 'Items per Page', 'metronet_reorder_posts' ),
'default' => 50,
'option' => 'reorder_items_per_page'
);
add_screen_option( 'per_page', $args );
}
/**
* Saves the screen options setting
*
* @author Ronald Huereca
* @since 2.3.0
* @access public
*/
public function add_screen_option_save( $status, $option, $value ) {
if ( 'reorder_items_per_page' == $option ) return $value;
return $status;
}
/**
* Output the main Reorder Interface
*
* @author Ryan Hellyer <[email protected]> and Ronald Huereca <[email protected]>
* @since Reorder 2.1.0
* @access public
* @global string $post_type
*/
public function output_interface() {
echo '<br />';
$post_count_obj = wp_count_posts( $this->post_type );
$post_count = isset( $post_count_obj->{$this->post_status} ) ?absint( $post_count_obj->{$this->post_status} ) : absint( $post_count_obj[ 'publish' ] );
if ( $post_count >= 1000 ) {
printf( '<div class="error"><p>%s</p></div>', sprintf( __( 'There are over %s posts found. We do not recommend you sort these posts for performance reasons.', 'metronet_reorder_posts' ), number_format( $post_count ) ) );
}
?>
<div id="reorder-error"></div>
<div><img src="<?php echo esc_url( admin_url( 'images/loading.gif' ) ); ?>" id="loading-animation" /></div>
<?php echo esc_html( $this->initial ); ?>
<?php
//Output non hierarchical posts
$page = isset( $_GET[ 'paged' ] ) ? absint( $_GET[ 'paged' ] ) : 0;
if ( $page == 0 || $page == 1 ) {
$offset = 0;
} elseif ( $page > 1 ) {
$offset = $this->offset * ( $page - 1 );
}
printf( '<input type="hidden" id="reorder-offset" value="%s" />', absint( $offset ) );
add_filter( 'found_posts', array( $this, 'adjust_offset_pagination' ), 10, 2 );
$post_query = new WP_Query(
array(
'post_type' => $this->post_type,
'posts_per_page' => $this->posts_per_page,
'orderby' => 'menu_order title',
'order' => $this->order,
'post_status' => $this->post_status,
'post_parent' => 0,
'offset' => $offset
)
);
remove_filter( 'found_posts', array( $this, 'adjust_offset_pagination' ), 10, 2 );
if( $post_query->have_posts() ) {
echo '<ul id="post-list">';
while( $post_query->have_posts() ) {
global $post;
$post_query->the_post();
$this->output_row( $post );
}
echo '</ul><!-- #post-list -->';
//Show pagination links
if( $post_query->max_num_pages > 1 ) {
echo '<div id="reorder-pagination">';
$current_url = add_query_arg( array( 'paged' => '%#%' ) );
$pagination_args = array(
'base' => $current_url,
'total' => $post_query->max_num_pages,
'current' => ( $page == 0 ) ? 1 : $page
);
echo paginate_links( $pagination_args );
echo '</div>';
}
} else {
echo sprintf( '<h3>%s</h3> ', esc_html__( 'There is nothing to sort at this time', 'metronet-reorder-posts' ) );
}
echo esc_html( $this->final );
$options = get_option( 'metronet-reorder-posts' );
if ( ! isset( $options[ 'show_query' ] ) || 'on' === $options[ 'show_query' ] ):
printf( '<h3>%s</h3>', esc_html__( 'Reorder Posts Query', 'metronet-reorder-posts' ) );
printf( '<p>%s</p>', esc_html__( 'You will need custom code to reorder posts. Here are some example query arguments for getting your content.', 'metronet-reorder-posts' ) );
$query = "
\$query = array(
'orderby' => 'menu_order',
'order' => 'ASC',
'post_status' => 'publish',
'post_type' => '{$this->post_type}',
'posts_per_page' => {$this->posts_per_page}
);
\$posts = get_posts( \$query );
if( ! empty( \$posts ) ) {
echo '<ul>';
foreach( \$posts as \$post ) {
printf( '<li><a href=\"%s\">%s</a></li>', esc_url( get_permalink( \$post->ID ) ), esc_html( \$post->post_title ) );
}
echo '</ul>';
}
";
printf( '<blockquote><pre><code>%s</code></pre></blockquote>', esc_html( print_r( $query, true ) ) );
endif;
}
/**
* Post Row Output
*
* @author Ronald Huereca <[email protected]>
* @since Reorder 2.1.0
* @access private
* @param stdclass $post object to post
*/
private function output_row( $post ) {
global $post;
setup_postdata( $post );
?>
<li id="list_<?php the_id(); ?>" data-id="<?php the_id(); ?>" data-menu-order="<?php echo absint( $post->menu_order ); ?>" data-parent="<?php echo absint( $post->post_parent ); ?>" data-post-type="<?php echo esc_attr( $post->post_type ); ?>">
<?php
//Get the children
$args = array(
'post_type' => $this->post_type,
'post_status' => $this->post_status,
'posts_per_page' => 100, /*hope there's never more than 100 children*/
'post_parent' => get_the_ID(),
'orderby' => 'menu_order',
'order' => $this->order,
);
$children = new WP_Query( $args );
//Output parent title
if( $children->have_posts() ) {
?>
<div class="row">
<div class="expand row-action">
<span class="dashicons dashicons-arrow-right"></span>
</div><!-- .row-action -->
<div class="row-content">
<?php the_title(); ?><?php echo ( defined( 'REORDER_DEBUG' ) && REORDER_DEBUG == true ) ? ' - Menu Order:' . absint( $post->menu_order ) : ''; ?>
</div><!-- .row-content -->
</div><!-- .row -->
<?php
} else {
?>
<div class="row">
<?php
$is_hierarchical = true;
if( is_post_type_hierarchical( $post->post_type ) ) {
?>
<div class="row-action">
</div><!-- .row-action -->
<?php
} else {
$is_hierarchical = false;
}
?>
<div class="row-content <?php echo $is_hierarchical ? '' : 'non-hierarchical'; ?>">
<?php the_title(); ?><?php echo ( defined( 'REORDER_DEBUG' ) && REORDER_DEBUG == true ) ? ' - Menu Order:' . absint( $post->menu_order ) : ''; ?>
</div><!-- .row-content -->
</div><!-- .row -->
<?php
}
if( $children->have_posts() ) {
echo '<ul class="children">';
while( $children->have_posts() ) {
global $post;
$children->the_post();
$this->output_row( $post );
}
echo '</ul>';
}
?>
</li>
<?php
} //end output_row
/**
* Initial HTML output
*
* @author Ryan Hellyer <[email protected]> and Ronald Huereca <[email protected]>
* @since Reorder 2.1.0
* @access public
* @global string $post_type
*/
public function sort_posts() {
//Dev note - Settings API not used here because there are no options to save.
?>
<div class="wrap">
<h2>
<?php echo esc_html( $this->heading ); ?>
</h2>
<?php
$tabs =
array(
array(
'url' => $this->reorder_page /* URL to the tab */,
'label' => $this->heading,
'get' => 'main' /*$_GET variable*/,
'action' => 'metronet_reorder_posts_interface_' . $this->post_type /* action variable in do_action */
)
);
$tabs = apply_filters( 'metronet_reorder_posts_tabs_' . $this->post_type, (array)$tabs );
$tabs_count = count( $tabs );
//Output tabs
$tab_html = '';
if ( $tabs && !empty( $tabs ) ) {
$tab_html .= '<h2 class="nav-tab-wrapper">';
$active_tab = isset( $_GET[ 'tab' ] ) ? sanitize_text_field( $_GET[ 'tab' ] ) : 'main';
$do_action = false;
foreach( $tabs as $tab ) {
$classes = array( 'nav-tab' );
$tab_get = isset( $tab[ 'get' ] ) ? $tab[ 'get' ] : '';
if ( $active_tab == $tab_get ) {
$classes[] = 'nav-tab-active';
$do_action = isset( $tab[ 'action' ] ) ? $tab[ 'action' ] : false;
}
$tab_url = isset( $tab[ 'url' ] ) ? $tab[ 'url' ] : '';
$tab_label = isset( $tab[ 'label' ] ) ? $tab[ 'label' ] : '';
$tab_html .= sprintf( '<a href="%s" class="%s">%s</a>', esc_url( $tab_url ), esc_attr( implode( ' ', $classes ) ), esc_html( $tab[ 'label' ] ) );
}
$tab_html .= '</h2>';
if ( $tabs_count > 1 ) {
echo $tab_html;
}
if ( $do_action ) {
do_action( $do_action );
}
}
?>
</div><!-- .wrap -->
<?php
} //end sort_posts
}