-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathwp-term-order.php
1199 lines (989 loc) · 29.4 KB
/
wp-term-order.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
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: WP Term Order
* Plugin URI: https://wordpress.org/plugins/wp-term-order/
* Description: Sort taxonomy terms, your way
* Author: John James Jacoby
* Author URI: https://jjj.blog
* Text Domain: wp-term-order
* License: GPL v2 or later
* Requires PHP: 5.6.20
* Requires at least: 5.3
* Version: 2.1.0
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'WP_Term_Order' ) ) :
/**
* Main WP Term Order class
*
* @link https://make.wordpress.org/core/2013/07/28/potential-roadmap-for-taxonomy-meta-and-post-relationships/ Taxonomy Roadmap
*
* @since 0.1.0
*/
final class WP_Term_Order {
/**
* @var string Plugin version
*/
public $version = '2.1.0';
/**
* @var string Database version
*/
public $db_version = 202303100001;
/**
* @var string Database version
*/
public $db_version_key = 'wpdb_term_taxonomy_version';
/**
* @var string Database strategy
*/
public $db_strategy = 'modify_tables';
/**
* @var string File for plugin
*/
public $file = '';
/**
* @var string URL to plugin
*/
public $url = '';
/**
* @var string Path to plugin
*/
public $path = '';
/**
* @var string Basename for plugin
*/
public $basename = '';
/**
* @var array Which taxonomies are being targeted?
*/
public $taxonomies = array();
/**
* @var bool Whether to use fancy ordering
*/
public $fancy = true;
/**
* @var WP_Meta_Query Meta query arguments
*/
public $meta_query = false;
/**
* @var array Term query clauses
*/
public $term_clauses = array();
/**
* @var array Term query clauses
*/
public $meta_clauses = array();
/**
* Empty constructor
*
* @since 0.1.0
*/
public function __construct() {
// Intentionally empty
}
/**
* Hook into queries, admin screens, and more!
*
* @since 1.0.0
*/
public function init() {
// Setup plugin
$this->file = __FILE__;
$this->url = plugin_dir_url( $this->file );
$this->path = plugin_dir_path( $this->file );
$this->basename = plugin_basename( $this->file );
/**
* Allow overriding the UI approach
*
* @since 1.0.0
* @param bool True to use jQuery sortable. False for numbers only.
*/
$this->fancy = apply_filters( 'wp_fancy_term_order', true );
/**
* Allow overriding the database strategy.
*
* Change this to "meta" to only ever use term meta and not modify
* the term_taxonomy database table.
*
* @since 2.0.0
* @param string "modify_tables" by default. Return "meta" to not modify tables.
*/
$this->db_strategy = apply_filters( 'wp_term_order_db_strategy', $this->db_strategy );
// Queries
add_filter( 'get_terms_orderby', array( $this, 'get_terms_orderby' ), 20, 2 );
add_action( 'terms_clauses', array( $this, 'terms_clauses' ), 20, 3 );
add_action( 'create_term', array( $this, 'add_term_order' ), 20, 3 );
add_action( 'edit_term', array( $this, 'add_term_order' ), 20, 3 );
// Get visible taxonomies
$this->taxonomies = $this->get_taxonomies();
// Always hook these in, for ajax actions
foreach ( $this->taxonomies as $value ) {
// Unfancy gets the column
add_filter( "manage_edit-{$value}_columns", array( $this, 'add_column_header' ) );
add_filter( "manage_{$value}_custom_column", array( $this, 'add_column_value' ), 10, 3 );
add_filter( "manage_edit-{$value}_sortable_columns", array( $this, 'sortable_columns' ) );
add_action( "{$value}_add_form_fields", array( $this, 'term_order_add_form_field' ) );
add_action( "{$value}_edit_form_fields", array( $this, 'term_order_edit_form_field' ) );
// Register "order" meta value
register_term_meta( $value, 'order', array(
'type' => 'integer',
'description' => esc_html__( 'Numeric order for terms, useful when sorting', 'wp-term-order' ),
'default' => 0,
'single' => true,
'show_in_rest' => true,
) );
}
// Hide the "order" column by default
if ( false !== $this->fancy ) {
add_filter( 'default_hidden_columns', array( $this, 'hidden_columns' ), 10, 2 );
}
// Ajax actions
add_action( 'wp_ajax_reordering_terms', array( $this, 'ajax_reordering_terms' ) );
// Only blog admin screens
if ( is_blog_admin() || doing_action( 'wp_ajax_inline_save_tax' ) || defined( 'WP_CLI' ) ) {
add_action( 'admin_init', array( $this, 'admin_init' ) );
// Proceed only if taxonomy supported
if ( ! empty( $_REQUEST['taxonomy'] ) && $this->taxonomy_supported( $_REQUEST['taxonomy'] ) && ! defined( 'WP_CLI' ) ) {
add_action( 'load-edit-tags.php', array( $this, 'edit_tags' ) );
}
}
// Pass this object into an action
do_action_ref_array( 'wp_term_meta_order_init', array( &$this ) );
}
/**
* Administration area hooks
*
* @since 0.1.0
*/
public function admin_init() {
// Check for DB update
$this->maybe_upgrade_database();
}
/**
* Administration area hooks
*
* @since 0.1.0
*/
public function edit_tags() {
add_action( 'admin_print_scripts-edit-tags.php', array( $this, 'enqueue_scripts' ) );
add_action( 'admin_head-edit-tags.php', array( $this, 'admin_head' ) );
add_action( 'admin_head-edit-tags.php', array( $this, 'help_tabs' ) );
add_action( 'quick_edit_custom_box', array( $this, 'quick_edit_term_order' ), 10, 3 );
}
/** Assets ****************************************************************/
/**
* Check if a taxonomy supports ordering its terms.
*
* @since 1.0.0
* @param array $taxonomy
* @return bool Default true
*/
public function taxonomy_supported( $taxonomy = array() ) {
// Defaut return value
$retval = true;
if ( is_string( $taxonomy ) ) {
$taxonomy = (array) $taxonomy;
}
if ( is_array( $taxonomy ) ) {
$taxonomy = array_map( 'sanitize_key', $taxonomy );
foreach ( $taxonomy as $tax ) {
if ( ! in_array( $tax, $this->taxonomies, true ) ) {
$retval = false;
break;
}
}
}
// Filter & return
return (bool) apply_filters( 'wp_term_order_taxonomy_supported', $retval, $taxonomy );
}
/**
* Check if a taxonomy supports overriding the orderby of a WP_Term_Query.
*
* Allows filtering of overriding the orderby specifically.
*
* @since 2.0.0
* @param array $taxonomy
* @return bool Default true
*/
public function taxonomy_override_orderby_supported( $taxonomy = array() ) {
// Defaut return value
$retval = $this->taxonomy_supported( $taxonomy );
// Filter & return
return (bool) apply_filters( 'wp_term_order_taxonomy_override_orderby_supported', $retval, $taxonomy );
}
/**
* Enqueue quick-edit JS
*
* @since 0.1.0
*/
public function enqueue_scripts() {
wp_enqueue_script( 'term-order-quick-edit', $this->url . 'js/quick-edit.js', array( 'jquery' ), $this->db_version, true );
// Enqueue fancy ordering
if ( true === $this->fancy ) {
wp_enqueue_script( 'term-order-reorder', $this->url . 'js/reorder.js', array( 'jquery-ui-sortable' ), $this->db_version, true );
}
}
/**
* Contextual help tabs
*
* @since 0.1.5
*/
public function help_tabs() {
// Drag & Drop
if ( true === $this->fancy ) {
get_current_screen()->add_help_tab( array(
'id' => 'wp_term_order_help_tab',
'title' => esc_html__( 'Term Order', 'wp-term-order' ),
'content' => '<p>' . esc_html__( 'To reposition an item, drag and drop the row by "clicking and holding" it anywhere and moving it to its new position.', 'wp-term-order' ) . '</p>',
) );
// Numbers only
} else {
get_current_screen()->add_help_tab( array(
'id' => 'wp_term_order_help_tab',
'title' => esc_html__( 'Term Order', 'wp-term-order' ),
'content' => '<p>' . esc_html__( 'To position an item, Quick Edit the row and change the order value to a more suitable number.', 'wp-term-order' ) . '</p>',
) );
}
}
/**
* Align custom `order` column, and fancy sortable styling.
*
* @since 0.1.0
*/
public function admin_head() {
?>
<style type="text/css">
.column-order {
text-align: center;
width: 74px;
}
<?php if ( true === $this->fancy ) : ?>
.wp-list-table .ui-sortable tr:not(.no-items) {
cursor: move;
}
.striped.dragging > tbody > .ui-sortable-helper ~ tr:nth-child(even) {
background: #f9f9f9;
}
.striped.dragging > tbody > .ui-sortable-helper ~ tr:nth-child(odd) {
background: #fff;
}
.wp-list-table .to-updating tr,
.wp-list-table .ui-sortable tr.inline-editor {
cursor: default;
}
.wp-list-table .ui-sortable-placeholder {
outline: 1px dashed #bbb;
background: #f1f1f1 !important;
visibility: visible !important;
}
.wp-list-table .ui-sortable-helper {
background-color: #fff !important;
outline: 1px solid #bbb;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.175);
}
.wp-list-table.dragging .row-actions,
.wp-list-table .ui-sortable-helper .row-actions,
.wp-list-table .ui-sortable-disabled .row-actions,
.wp-list-table .ui-sortable-disabled tr:hover .row-actions {
position: relative !important;
visibility: hidden !important;
}
.to-row-updating .check-column {
background: url('<?php echo admin_url( '/images/spinner.gif' );?>') 10px 9px no-repeat;
}
@media print,
(-o-min-device-pixel-ratio: 5/4),
(-webkit-min-device-pixel-ratio: 1.25),
(min-resolution: 120dpi) {
.to-row-updating .check-column {
background-image: url('<?php echo admin_url( '/images/spinner-2x.gif' );?>');
background-size: 20px 20px;
}
}
.to-row-updating .check-column input {
visibility: hidden;
}
<?php endif; ?>
</style>
<?php
}
/**
* Return the taxonomies used by this plugin
*
* @since 0.1.0
*
* @param array $args
* @return array
*/
private function get_taxonomies( $args = array() ) {
// Parse arguments
$r = wp_parse_args( $args, array(
'show_ui' => true
) );
// Get & return the taxonomies
$taxonomies = get_taxonomies( $r );
// Filter taxonomies & return
return (array) apply_filters( 'wp_term_order_get_taxonomies', $taxonomies, $r, $args );
}
/** Columns ***************************************************************/
/**
* Add the "Order" column to taxonomy terms list-tables
*
* @since 0.1.0
*
* @param array $columns
* @return array
*/
public function add_column_header( $columns = array() ) {
$columns['order'] = esc_html__( 'Order', 'wp-term-order' );
return $columns;
}
/**
* Output the value for the custom column, in our case: `order`
*
* @since 0.1.0
*
* @param string $empty
* @param string $custom_column
* @param int $term_id
* @return mixed
*/
public function add_column_value( $empty = '', $custom_column = '', $term_id = 0 ) {
// Bail if no taxonomy passed or not on the `order` column
if ( empty( $_REQUEST['taxonomy'] ) || ( 'order' !== $custom_column ) || ! empty( $empty ) ) {
return;
}
return $this->get_term_order( $term_id );
}
/**
* Allow sorting by `order` order
*
* @since 0.1.0
* @param array $columns
* @return array
*/
public function sortable_columns( $columns = array() ) {
$columns['order'] = 'order';
return $columns;
}
/**
* Add `order` to hidden columns
*
* @since 2.0.0
* @param array $columns
* @param WP_Screen $screen
* @return array
*/
public function hidden_columns( $columns = array(), $screen = '' ) {
// Bail if not on the `edit-tags` screen for a visible taxonomy
if ( ( 'edit-tags' !== $screen->base ) || ! $this->taxonomy_supported( $screen->taxonomy ) ) {
return $columns;
}
$columns[] = 'order';
return $columns;
}
/**
* Add `order` to term when updating
*
* @since 0.1.0
* @param int $term_id The ID of the term
* @param int $tt_id Not used
* @param string $taxonomy Taxonomy of the term
*/
public function add_term_order( $term_id = 0, $tt_id = 0, $taxonomy = '' ) {
/*
* Bail if order info hasn't been POSTed, like when the "Quick Edit"
* form is used to update a term.
*/
if ( ! isset( $_POST['order'] ) ) {
return;
}
// Sanitize the value.
$order = ! empty( $_POST['order'] )
? (int) $_POST['order']
: 0;
// No cache clean required
$this->set_term_order( $term_id, $taxonomy, $order, false );
}
/**
* Set order of a specific term
*
* @since 0.1.0
* @global object $wpdb
* @param int $term_id
* @param string $taxonomy
* @param int $order
* @param bool $clean_cache
*/
public function set_term_order( $term_id = 0, $taxonomy = '', $order = 0, $clean_cache = false ) {
global $wpdb;
// Avoid malformed order values
if ( ! is_numeric( $order ) ) {
$order = 0;
}
// Cast to int
$order = (int) $order;
// Get existing term order
$existing_order = $this->get_term_order( $term_id );
// Bail if no change
if ( $order === $existing_order ) {
return;
}
/*
* Update the database row
*
* We cannot call wp_update_term() here because it would cause recursion,
* and also the database columns are hardcoded and we can't modify them.
*/
if ( 'modify_tables' === $this->db_strategy ) {
// Database query
$success = $wpdb->update(
$wpdb->term_taxonomy,
array(
'order' => $order
),
array(
'term_id' => $term_id,
'taxonomy' => $taxonomy
),
array(
'%d'
),
array(
'%d',
'%s'
)
);
// Only execute action and clean cache when update succeeds
if ( ! empty( $success ) ) {
// Maybe clean the term cache
if ( true === $clean_cache ) {
clean_term_cache( $term_id, $taxonomy );
}
}
}
// Update the "order" meta data value
update_term_meta( $term_id, 'order', (int) $order );
/**
* A term order was successfully set/changed.
*
* @since 1.0.0
*/
do_action( 'wp_term_order_set_term_order', $term_id, $taxonomy, $order );
}
/**
* Return the order of a term
*
* @since 0.1.0
* @param int $term_id
*/
public function get_term_order( $term_id = 0 ) {
// Use false
$retval = false;
// Use term order if set and strategy allows
if ( 'modify_tables' === $this->db_strategy ) {
// Use taxonomy if available
$tax = ! empty( $_REQUEST['taxonomy'] )
? sanitize_key( $_REQUEST['taxonomy'] )
: '';
// Get the term, probably from cache at this point
$term = get_term( $term_id, $tax );
if ( isset( $term->order ) ) {
$retval = $term->order;
}
}
// Fallback to term meta
if ( false === $retval ) {
$retval = get_term_meta( $term_id, 'order', true );
}
// Cast & return
return (int) $retval;
}
/** Markup ****************************************************************/
/**
* Output the "order" form field when adding a new term
*
* @since 0.1.0
*/
public function term_order_add_form_field() {
// Default classes
$classes = array(
'form-field',
'form-required',
'wp-term-order-form-field',
);
/**
* Allows filtering HTML classes on the wrapper of the "order" form
* field shown when adding a new term.
*
* @param array $classes
*/
$classes = (array) apply_filters( 'wp_term_order_add_form_field_classes', $classes, $this );
?>
<div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>">
<label for="order">
<?php esc_html_e( 'Order', 'wp-term-order' ); ?>
</label>
<input type="number" pattern="[0-9.]+" name="order" id="order" value="0" size="11">
<p class="description">
<?php esc_html_e( 'Set a specific order by entering a number (1 for first, etc.) in this field.', 'wp-term-order' ); ?>
</p>
</div>
<?php
}
/**
* Output the "order" form field when editing an existing term
*
* @since 0.1.0
* @param object $term
*/
public function term_order_edit_form_field( $term = false ) {
// Default classes
$classes = array(
'form-field',
'wp-term-order-form-field',
);
/**
* Allows filtering HTML classes on the wrapper of the "order" form
* field shown when editing an existing term.
*
* @param array $classes
*/
$classes = (array) apply_filters( 'wp_term_order_edit_form_field_classes', $classes, $this );
?>
<tr class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>">
<th scope="row" valign="top">
<label for="order">
<?php esc_html_e( 'Order', 'wp-term-order' ); ?>
</label>
</th>
<td>
<input name="order" id="order" type="text" value="<?php echo $this->get_term_order( $term ); ?>" size="11" />
<p class="description">
<?php esc_html_e( 'Terms are usually ordered alphabetically, but you can choose your own order by entering a number (1 for first, etc.) in this field.', 'wp-term-order' ); ?>
</p>
</td>
</tr>
<?php
}
/**
* Output the "order" quick-edit field
*
* @since 0.1.0
* @param string $column_name
* @param string $screen
* @param string $name
*/
public function quick_edit_term_order( $column_name = '', $screen = '', $name = '' ) {
// Bail if not the `order` column on the `edit-tags` screen for a visible taxonomy
if ( ( 'order' !== $column_name ) || ( 'edit-tags' !== $screen ) || ! $this->taxonomy_supported( $name ) ) {
return false;
}
// Default classes
$classes = array(
'inline-edit-col',
'wp-term-order-edit-col',
);
/**
* Allows filtering HTML classes on the wrapper of the "order"
* quick-edit field.
*
* @param array $classes
*/
$classes = (array) apply_filters( 'wp_term_order_quick_edit_field_classes', $classes, $this );
?>
<fieldset>
<div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>">
<label>
<span class="title"><?php esc_html_e( 'Order', 'wp-term-order' ); ?></span>
<span class="input-text-wrap">
<input type="number" pattern="[0-9.]+" class="ptitle" name="order" value="" size="11">
</span>
</label>
</div>
</fieldset>
<?php
}
/** Query Filters *********************************************************/
/**
* Maybe filter the terms query clauses.
*
* @since 2.0.0
* @param array $clauses
* @param array $taxonomies
* @param array $args
* @return array
*/
public function terms_clauses( $clauses = array(), $taxonomies = array(), $args = array() ) {
// Bail if not supported taxonomies
if ( ! $this->taxonomy_supported( $taxonomies ) ) {
return $clauses;
}
// Bail if no clauses
if ( empty( $this->term_clauses ) ) {
return $clauses;
}
// Explicitly meta
if ( 'meta' === $this->db_strategy ) {
$clauses['where'] .= $this->term_clauses['where'];
$clauses['join'] .= $this->term_clauses['join'];
}
// Return clauses
return $clauses;
}
/**
* Force `orderby` to `tt.order` if not explicitly set to something else
*
* @since 0.1.0
* @param string $orderby
* @param array $args
* @return string
*/
public function get_terms_orderby( $orderby = 't.name', $args = array() ) {
// Bail if taxonomy not supported
if ( ! $this->taxonomy_supported( $args['taxonomy'] ) ) {
return $orderby;
}
// Bail if taxonomy orderby override not supported
if ( ! $this->taxonomy_override_orderby_supported( $args['taxonomy'] ) ) {
return $orderby;
}
// Default to not overriding
$override = false;
// Ordering on admin screens
if ( is_admin() ) {
// Look for custom orderby
$get_orderby = ! empty( $_GET['orderby'] )
? sanitize_key( $_GET['orderby'] )
: $orderby;
// Override if explicitly sorting the UI by the "order" column
if ( 'order' === $get_orderby ) {
$override = true;
}
}
// Ordering by the database column
if ( 'modify_tables' === $this->db_strategy ) {
// Explicitly asking for "order" column
if ( 'order' === $args['orderby'] ) {
$orderby = 'tt.order';
// Falling back to "t.name" so we'll guess at an override
} elseif ( 't.name' === $orderby ) {
$orderby = 'tt.order, t.name';
// Fallback or override
} elseif ( empty( $orderby ) || ( true === $override ) ) {
$orderby = 'tt.order';
}
// Explicitly meta
} elseif ( 'meta' === $this->db_strategy ) {
if (
( 'order' === $args['orderby'] )
||
( 't.name' === $orderby )
||
empty( $orderby )
||
( true === $override )
) {
// Merge query args with custom meta query
$r = array_merge( $args, array(
'meta_query' => array(
'order_clause' => array(
'key' => 'order',
'type' => 'NUMERIC'
)
)
) );
// Setup the meta query & clauses
$this->meta_query = new WP_Meta_Query();
$this->meta_query->parse_query_vars( $r );
$this->term_clauses = $this->meta_query->get_sql( 'term', 't', 'term_id' );
$this->meta_clauses = $this->meta_query->get_clauses();
// Get the orderby string
$orderby = $this->parse_orderby_meta( $orderby );
}
}
// Return possibly modified `orderby` value
return $orderby;
}
/**
* Parse the "orderby" meta query for the 'order' meta key & value.
*
* This is largely copied from: WP_Term_Query::parse_orderby_meta();
*
* @since 2.0.0
* @param string $orderby_raw
* @param string $strategy
* @return string
*/
private function parse_orderby_meta( $orderby_raw = 't.name', $strategy = 'order' ) {
// Bail if no meta clauses
if ( empty( $this->meta_clauses ) ) {
return $orderby_raw;
}
// Default values
$allowed_keys = array( 'meta_value', 'meta_value_num' );
$primary_meta_key = null;
$primary_meta_query = reset( $this->meta_clauses );
if ( ! empty( $primary_meta_query['key'] ) ) {
$primary_meta_key = $primary_meta_query['key'];
$allowed_keys[] = $primary_meta_key;
}
$allowed_keys = array_merge( $allowed_keys, array_keys( $this->meta_clauses ) );
// Bail if not in allowed keys
if ( ! in_array( $strategy, $allowed_keys, true ) ) {
return $orderby_raw;
}
// Default return value
$retval = '';
// Strategy to use to order by
switch ( $strategy ) {
case $primary_meta_key :
case 'meta_value' :
if ( ! empty( $primary_meta_query['type'] ) ) {
$retval = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
} else {
$retval = "{$primary_meta_query['alias']}.meta_value";
}
break;
case 'meta_value_num' :
$retval = "{$primary_meta_query['alias']}.meta_value+0";
break;
default :
// $retval corresponds to a meta_query clause.
if ( array_key_exists( $orderby_raw, $this->meta_clauses ) ) {
$meta_clause = $this->meta_clauses[ $orderby_raw ];
$retval = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
}
break;
}
return $retval;
}
/** Database Alters *******************************************************/
/**
* Should a database update occur.
*
* Runs on `admin_init` hook.
*
* @since 0.1.0
*/
private function maybe_upgrade_database() {
// Check DB for version
$db_version = get_option( $this->db_version_key );
// Needs
if ( $db_version < $this->db_version ) {
$this->upgrade_database( $db_version );
}
}
/**
* Modify the `term_taxonomy` table and add an `order` column to it
*
* @since 0.1.0
* @param int $old_version
* @global object $wpdb
*/
private function upgrade_database( $old_version = 0 ) {
global $wpdb;
$old_version = (int) $old_version;
// Only modify if using strategy
if ( 'modify_tables' === $this->db_strategy ) {
// The main column alter
if ( $old_version < 201508110005 ) {
$wpdb->query( "ALTER TABLE `{$wpdb->term_taxonomy}` ADD `order` INT (11) NOT NULL DEFAULT 0;" );
}
}
// Migrate column values to meta
if ( $old_version < 202106140001 ) {
// Query for all terms
$terms = $wpdb->get_results( "SELECT * FROM `{$wpdb->term_taxonomy}`;" );
// Loop through and copy to meta
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
// Skip if not set
if ( ! isset( $term->order ) || empty( $term->taxonomy ) ) {
continue;
}
// Skip if not supported
if ( ! $this->taxonomy_supported( $term->taxonomy ) ) {
continue;
}
// Add/update meta
update_term_meta( $term->term_id, 'order', (int) $term->order );
}
}
}
// Update the DB version
update_option( $this->db_version_key, $this->db_version );
}
/** Admin Ajax ************************************************************/
/**
* Handle AJAX term reordering
*
* @since 0.1.0
*/
public function ajax_reordering_terms() {
// Bail if required term data is missing
if ( empty( $_POST['id'] ) || empty( $_POST['tax'] ) || ( ! isset( $_POST['previd'] ) && ! isset( $_POST['nextid'] ) ) ) {
die( -1 );