PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 4.0.5.4
Adminify – White Label, Admin Menu Editor, Login Customizer v4.0.5.4
4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 4.1.17 All 164 releases
adminify / Inc / Modules / PostTypesOrder / PostTypesOrder.php

PostTypesOrder.php in Adminify – White Label, Admin Menu Editor, Login Customizer 4.0.5.4, at Inc/Modules/PostTypesOrder/PostTypesOrder.php

794 lines 23.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPAdminify\Inc\Modules\PostTypesOrder;
4
5 use WPAdminify\Inc\Utils;
6 use WPAdminify\Inc\Admin\AdminSettings;
7 use WPAdminify\Inc\Admin\AdminSettingsModel;
8 use WPAdminify\Inc\Admin\Options\Productivity;
9 use WPAdminify\Inc\Modules\PostTypesOrder\PostTypesOrderWalker;
10
11 // no direct access allowed
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * WP Adminify
18 *
19 * @package WP Adminify: Post Types Order
20 *
21 * @author WP Adminify <support@wpadminify.com>
22 */
23
24 class PostTypesOrder extends AdminSettingsModel {
25
26 private $url;
27
28 public function __construct() {
29 global $pagenow, $typenow;
30
31 $this->options = (array) AdminSettings::get_instance()->get('post_types_order');
32
33
34 // Admin Init
35 if ( empty( $_GET ) ) {
36 add_action( 'admin_init', [ $this, 'jltwp_adminify_refresh' ] );
37 }
38
39 add_action( 'admin_init', [ $this, 'adminify_pto_load_scripts' ] );
40 add_action( 'admin_enqueue_scripts', [ $this, 'adminify_pto_css' ], 99 );
41
42 // sortable ajax action
43 add_action( 'wp_ajax_update_post_types_order', [ $this, 'adminify_pto_update_order' ] );
44 add_action( 'wp_ajax_update_post_types_taxonomy_order', [ $this, 'adminify_pto_update_taxonomy' ] );
45
46 // reorder post types
47 add_action( 'pre_get_posts', [ $this, 'adminify_pto_pre_get_posts' ] );
48
49 add_filter( 'get_previous_post_where', array( $this, 'adminify_pto_previous_post_where' ) );
50 add_filter( 'get_previous_post_sort', [ $this, 'adminify_pto_previous_post_sort' ] );
51 add_filter( 'get_next_post_where', [ $this, 'adminify_pto_next_post_where' ] );
52 add_filter( 'get_next_post_sort', [ $this, 'adminify_pto_next_post_sort' ] );
53
54 // reorder taxonomies
55 add_filter( 'get_terms_orderby', [ $this, 'adminify_pto_get_terms_orderby' ], 10, 3 );
56 add_filter( 'wp_get_object_terms', [ $this, 'adminify_pto_get_object_terms' ], 10, 3 );
57 add_filter( 'get_terms', [ $this, 'adminify_pto_get_object_terms' ], 10, 3 );
58
59 // reorder sites
60 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
61 add_action( 'wp_ajax_update_post_types_order_sites', [ $this, 'adminify_pto_update_sites' ] );
62
63 // networkadmin
64 if (
65 empty( $_SERVER['QUERY_STRING'] ) ||
66 ( ! empty( $_SERVER['QUERY_STRING'] ) &&
67 'action=deleteblog' !== $_SERVER['QUERY_STRING'] && // delete
68 'action=allblogs' !== $_SERVER['QUERY_STRING'] // delete all
69 )
70 ) {
71
72 // call from 'get_sites'
73 add_filter( 'sites_clauses', [ $this, 'adminify_pto_sites_clauses' ], 10, 1 );
74
75 add_action( 'admin_init', [ $this, 'adminify_pto_refresh_network' ] );
76
77 // adminbar sites reorder
78 add_filter( 'get_blogs_of_user', [ $this, 'adminify_pto_get_blogs_of_user' ], 10, 3 );
79 }
80
81 // before wp v4.6.0 * wp_get_sites
82 add_action( 'init', [ $this, 'adminify_pto_refresh_front_network' ] );
83 }
84 }
85
86
87
88
89 public function adminiy_pto_media_list( $args = '' ) {
90 $defaults = [
91 'depth' => -1,
92 'date_format' => get_option( 'date_format' ),
93 'child_of' => 0,
94 'sort_column' => 'menu_order',
95 'post_status' => 'any',
96 ];
97
98 $r = wp_parse_args( $args, $defaults );
99 extract( $r, EXTR_SKIP );
100
101 $output = '';
102
103 $r['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', [] ) );
104
105 // Query pages.
106 $r['hierarchical'] = 0;
107 $args = [
108 'sort_column' => 'menu_order',
109 'post_type' => $post_type,
110 'posts_per_page' => -1,
111 'post_status' => 'any',
112 'orderby' => [
113 'menu_order' => 'ASC',
114 'post_date' => 'DESC',
115 ],
116 ];
117
118 $the_query = new \WP_Query( $args );
119 $pages = $the_query->posts;
120
121 if ( ! empty( $pages ) ) {
122 $output .= $this->walkTree( $pages, $r['depth'], $r );
123 }
124
125 echo wp_kses_post( $output );
126 }
127
128 public function walkTree( $pages, $depth, $r ) {
129 $walker = new PostTypesOrderWalker();
130
131 $args = [ $pages, $depth, $r ];
132 return call_user_func_array( [ &$walker, 'walk' ], $args );
133 }
134
135
136 public function jltwp_adminify_refresh() {
137
138 // global $wp_post_types;
139 // $pto_obj = $wp_post_types['attachment'];
140
141 global $wpdb;
142 $objects = $this->adminify_pto_get_options();
143 // $tags = $this->adminify_pto_get_options_taxonomies();
144
145 if ( ! empty( $objects ) ) {
146 foreach ( $objects as $object ) {
147 $result = $wpdb->get_results(
148 "
149 SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min
150 FROM $wpdb->posts
151 WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
152 "
153 );
154 if ( $result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max ) {
155 continue;
156 }
157
158 $results = $wpdb->get_results(
159 "
160 SELECT ID
161 FROM $wpdb->posts
162 WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
163 ORDER BY menu_order ASC
164 "
165 );
166 foreach ( $results as $key => $result ) {
167 $wpdb->update( $wpdb->posts, [ 'menu_order' => $key + 1 ], [ 'ID' => $result->ID ] );
168 }
169 }
170 }
171
172 // if ( ! empty( $tags ) ) {
173 // foreach ( $tags as $taxonomy ) {
174 // $result = $wpdb->get_results(
175 // "
176 // SELECT count(*) as cnt, max(term_order) as max, min(term_order) as min
177 // FROM $wpdb->terms AS terms
178 // INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
179 // WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
180 // "
181 // );
182 // if ( $result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max ) {
183 // continue;
184 // }
185
186 // $results = $wpdb->get_results(
187 // "
188 // SELECT terms.term_id
189 // FROM $wpdb->terms AS terms
190 // INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
191 // WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
192 // ORDER BY term_order ASC
193 // "
194 // );
195 // foreach ( $results as $key => $result ) {
196 // $wpdb->update( $wpdb->terms, [ 'term_order' => $key + 1 ], [ 'term_id' => $result->term_id ] );
197 // }
198 // }
199 // }
200 }
201
202
203
204 function adminify_pto_pre_get_posts( $wp_query ) {
205 $objects = $this->adminify_pto_get_options();
206 if ( empty( $objects ) ) {
207 return false;
208 }
209
210 /**
211 * for Admin
212 *
213 * @default
214 * post pto: [order] => null(desc) [orderby] => null(date)
215 * page: [order] => asc [orderby] => menu_order title
216 */
217
218 if ( is_admin() ) {
219
220 // $wp_query->query['post_type']=post
221 if ( isset( $wp_query->query['post_type'] ) && ! isset( $_GET['orderby'] ) ) {
222 if ( in_array( $wp_query->query['post_type'], $objects ) ) {
223 $wp_query->set( 'orderby', 'menu_order' );
224 $wp_query->set( 'order', 'ASC' );
225 }
226 }
227 } else {
228 $active = false;
229
230 // page or custom post types
231 if ( isset( $wp_query->query['post_type'] ) ) {
232 // exclude array()
233 if ( ! is_array( $wp_query->query['post_type'] ) ) {
234 if ( in_array( $wp_query->query['post_type'], $objects ) ) {
235 $active = true;
236 }
237 }
238 // post
239 } else {
240 if ( in_array( 'post', $objects ) ) {
241 $active = true;
242 }
243 }
244
245 if ( ! $active ) {
246 return false;
247 }
248
249 // get_posts()
250 if ( isset( $wp_query->query['suppress_filters'] ) ) {
251 if ( $wp_query->get( 'orderby' ) == 'date' || $wp_query->get( 'orderby' ) == 'menu_order' ) {
252 $wp_query->set( 'orderby', 'menu_order' );
253 $wp_query->set( 'order', 'ASC' );
254 } elseif ( $wp_query->get( 'orderby' ) == 'default_date' ) {
255 $wp_query->set( 'orderby', 'date' );
256 }
257 // WP_Query( contain main_query )
258 } else {
259 if (
260 ! $wp_query->get( 'orderby' )
261 ) {
262 $wp_query->set( 'orderby', 'menu_order' );
263 }
264 if (
265 ! $wp_query->get( 'order' )
266 ) {
267 $wp_query->set( 'order', 'ASC' );
268 }
269 }
270 }
271 }
272
273
274 public function adminify_pto_update_order() {
275 global $wpdb;
276 $data = [];
277 parse_str( sanitize_text_field( wp_unslash( $_POST['order'] ) ), $data );
278
279 if ( ! is_array( $data ) ) {
280 return false;
281 }
282
283 // get objects per now page
284 $id_arr = [];
285 foreach ( $data as $key => $values ) {
286 foreach ( $values as $position => $id ) {
287 $id_arr[] = $id;
288 }
289 }
290
291 // get menu_order of objects per now page
292 $menu_order_arr = [];
293 foreach ( $id_arr as $key => $id ) {
294 $results = $wpdb->get_results( "SELECT menu_order FROM $wpdb->posts WHERE ID = " . intval( $id ) );
295 foreach ( $results as $result ) {
296 $menu_order_arr[] = $result->menu_order;
297 }
298 }
299
300 // maintains key association = no
301 sort( $menu_order_arr );
302
303 foreach ( $data as $key => $values ) {
304 foreach ( $values as $position => $id ) {
305 $wpdb->update( $wpdb->posts, [ 'menu_order' => $menu_order_arr[ $position ] ], [ 'ID' => intval( $id ) ] );
306 }
307 }
308
309 // same number check
310 $post_type = get_post_type( $id );
311 $sql = "SELECT COUNT(menu_order) AS mo_count, post_type, menu_order FROM $wpdb->posts
312 WHERE post_type = '{$post_type}' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
313 AND menu_order > 0 GROUP BY post_type, menu_order HAVING (mo_count) > 1";
314 $results = $wpdb->get_results( $sql );
315 if ( count( $results ) > 0 ) {
316
317 // menu_order refresh
318 $sql = "SELECT ID, menu_order FROM $wpdb->posts
319 WHERE post_type = '{$post_type}' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
320 AND menu_order > 0 ORDER BY menu_order";
321 $results = $wpdb->get_results( $sql );
322 foreach ( $results as $key => $result ) {
323 $view_posi = array_search( $result->ID, $id_arr, true );
324 if ( $view_posi === false ) {
325 $view_posi = 999;
326 }
327 $sort_key = ( $result->menu_order * 1000 ) + $view_posi;
328 $sort_ids[ $sort_key ] = $result->ID;
329 }
330 ksort( $sort_ids );
331 $oreder_no = 0;
332 foreach ( $sort_ids as $key => $id ) {
333 $oreder_no = $oreder_no + 1;
334 $wpdb->update( $wpdb->posts, [ 'menu_order' => $oreder_no ], [ 'ID' => intval( $id ) ] );
335 }
336 }
337 }
338
339 public function adminify_pto_update_taxonomy() {
340 global $wpdb;
341
342 parse_str( sanitize_text_field( wp_unslash( $_POST['order'] ) ), $data );
343
344 if ( ! is_array( $data ) ) {
345 return false;
346 }
347
348 $id_arr = [];
349 foreach ( $data as $key => $values ) {
350 foreach ( $values as $position => $id ) {
351 $id_arr[] = $id;
352 }
353 }
354
355 $menu_order_arr = [];
356 foreach ( $id_arr as $key => $id ) {
357 $results = $wpdb->get_results( "SELECT term_order FROM $wpdb->terms WHERE term_id = " . intval( $id ) );
358 foreach ( $results as $result ) {
359 $menu_order_arr[] = $result->term_order;
360 }
361 }
362 sort( $menu_order_arr );
363
364 foreach ( $data as $key => $values ) {
365 foreach ( $values as $position => $id ) {
366 $wpdb->update( $wpdb->terms, [ 'term_order' => $menu_order_arr[ $position ] ], [ 'term_id' => intval( $id ) ] );
367 }
368 }
369
370 // same number check
371 $term = get_term( $id );
372 $taxonomy = $term->taxonomy;
373 $sql = "SELECT COUNT(term_order) AS to_count, term_order
374 FROM $wpdb->terms AS terms
375 INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
376 WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'GROUP BY taxonomy, term_order HAVING (to_count) > 1";
377 $results = $wpdb->get_results( $sql );
378 if ( count( $results ) > 0 ) {
379 // term_order refresh
380 $sql = "SELECT terms.term_id, term_order
381 FROM $wpdb->terms AS terms
382 INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
383 WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
384 ORDER BY term_order ASC";
385 $results = $wpdb->get_results( $sql );
386 foreach ( $results as $key => $result ) {
387 $view_posi = array_search( $result->term_id, $id_arr, true );
388 if ( $view_posi === false ) {
389 $view_posi = 999;
390 }
391 $sort_key = ( $result->term_order * 1000 ) + $view_posi;
392 $sort_ids[ $sort_key ] = $result->term_id;
393 }
394 ksort( $sort_ids );
395 $oreder_no = 0;
396 foreach ( $sort_ids as $key => $id ) {
397 $oreder_no = $oreder_no + 1;
398 $wpdb->update( $wpdb->terms, [ 'term_order' => $oreder_no ], [ 'term_id' => $id ] );
399 }
400 }
401
402 do_action( 'wpadminify_update_post_types_order_taxonomy' );
403 }
404
405 public function jltwp_adminify_taxonomy_compare( $a, $b ) {
406 if ( $a->term_order == $b->term_order ) {
407 return 0;
408 }
409 return ( $a->term_order < $b->term_order ) ? -1 : 1;
410 }
411
412 public function adminify_pto_load_scripts() {
413 global $pagenow, $typenow;
414
415 if ( $this->conditional_script_load() || ( $pagenow === 'upload.php' && ( isset( $this->options['pto_media'] ) && $this->options['pto_media'] ) ) ) {
416 wp_enqueue_script( 'jquery' );
417 wp_enqueue_script( 'jquery-ui-sortable' );
418 wp_enqueue_script( 'wp-adminify-post-type-order', WP_ADMINIFY_URL . 'Inc/Modules/PostTypesOrder/js/post-type-order.js', [ 'jquery' ], WP_ADMINIFY_VER, true );
419 }
420 }
421
422 public function adminify_pto_css() {
423 global $pagenow, $typenow;
424 if ( $this->conditional_script_load() || ( $pagenow === 'upload.php' && ( ! empty( $this->options['pto_media'] ) ) ) ) {
425 echo '<!-- Start of Post Type and Taxonomy Order --->';
426 echo '<style type="text/css">';
427 // echo '.ui-sortable tr:hover { cursor: move; } .ui-sortable tr.alternate { background-color: #F9F9F9; } .ui-sortable tr.ui-sortable-helper { background-color: #F9F9F9; border-top: 1px solid #DFDFDF; border-bottom: 1px solid #DFDFDF; box-shadow:0 -5px 5px -4px rgba(0,0,0,.1019607843),0 5px 5px -3px rgba(0,0,0,.1019607843) } .ui-sortable-placeholder { height: 100px; } .wp-list-table { table-layout: auto; width: 100%;}';
428 echo '.adminify-table-responsive-wrapper .ui-sortable tr:hover{cursor:move}.adminify-table-responsive-wrapper .ui-sortable tr.alternate{background-color:#f9f9f9 !important}.adminify-table-responsive-wrapper .ui-sortable tr.ui-sortable-helper{background-color:#f9f9f9 !important;border-top:1px solid #dfdfdf;border-bottom:1px solid #dfdfdf;box-shadow:0 -5px 5px -4px rgba(0,0,0,.1),0 5px 5px -3px rgba(0,0,0,.1)}.adminify-table-responsive-wrapper .ui-sortable-placeholder{height:50px;background-color:#e9e9e9;border:2px dashed #dfdfdf;visibility:visible!important}.adminify-table-responsive-wrapper .wp-list-table{table-layout:fixed;width:100%;border-collapse:collapse}.adminify-table-responsive-wrapper.wp-list-table td,.adminify-table-responsive-wrapper .wp-list-table th{padding:8px 16px;text-align:left;white-space:nowrap;word-wrap:break-word;overflow:hidden}.adminify-table-responsive-wrapper.wp-list-table .column-title,.wp-list-table td.column-title,.adminify-table-responsive-wrapper{overflow-x:auto;-webkit-overflow-scrolling:touch}@media screen and (max-width:768px){.adminify-table-responsive-wrapper .wp-list-table td,.adminify-table-responsive-wrapper.wp-list-table th{padding:8px}}';
429 echo '</style>';
430 echo '<!-- End of Post Type and Taxonomy Order --->';
431 }
432 }
433
434 public function conditional_script_load() {
435 global $pagenow, $typenow;
436
437 $active = false;
438
439 // Multisite > Sites
440 if (
441 function_exists( 'is_multisite' )
442 && is_multisite()
443 && $pagenow == 'sites.php'
444 // && get_option('adminify_pto_network_sites')
445 ) {
446 return true;
447 }
448
449 $objects = $this->adminify_pto_get_options();
450 $tags = $this->adminify_pto_get_options_taxonomies();
451
452 if ( empty( $objects ) && empty( $tags ) ) {
453 return false;
454 }
455
456 // exclude (sorting, addnew page, edit page)
457 if ( isset( $_GET['orderby'] ) || ( ! empty( $_SERVER['REQUEST_URI'] ) && strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'action=edit' ) || strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'wp-admin/post-new.php' ) ) ) {
458 return false;
459 }
460
461 if ( ! empty( $objects ) ) {
462 if ( isset( $_GET['post_type'] ) && ! isset( $_GET['taxonomy'] ) && in_array( $_GET['post_type'], $objects ) ) { // if page or custom post types
463 $active = true;
464 }
465 if ( ! isset( $_GET['post_type'] ) && ( ! empty( $_SERVER['REQUEST_URI'] && strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'wp-admin/edit.php' ) ) && in_array( 'post', $objects ) ) ) {
466 // if post
467 $active = true;
468 }
469 }
470
471 if ( ! empty( $tags ) ) {
472 if ( isset( $_GET['taxonomy'] ) && in_array( $_GET['taxonomy'], $tags ) ) {
473 $active = true;
474 }
475 }
476
477 return $active;
478 }
479
480 function adminify_pto_get_options() {
481 $objects = isset( $this->options['pto_posts'] ) && is_array( $this->options['pto_posts'] ) ? $this->options['pto_posts'] : [];
482 return $objects;
483 }
484
485 function adminify_pto_get_options_taxonomies() {
486 $tags = isset( $this->options['pto_taxonomies'] ) && is_array( $this->options['pto_taxonomies'] ) ? $this->options['pto_taxonomies'] : [];
487 return $tags;
488 }
489
490 function adminify_pto_previous_post_where( $where ) {
491 global $post;
492
493 $objects = $this->adminify_pto_get_options();
494 if ( empty( $objects ) ) {
495 return $where;
496 }
497
498 if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
499 $current_menu_order = $post->menu_order;
500 $where = str_replace( "p.post_date < '" . $post->post_date . "'", "p.menu_order > '" . $current_menu_order . "'", $where );
501 }
502 return $where;
503 }
504
505
506 function adminify_pto_previous_post_sort( $orderby ) {
507 global $post;
508
509 $objects = $this->adminify_pto_get_options();
510 if ( empty( $objects ) ) {
511 return $orderby;
512 }
513
514 if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
515 $orderby = 'ORDER BY p.menu_order ASC LIMIT 1';
516 }
517 return $orderby;
518 }
519
520
521 function adminify_pto_next_post_where( $where ) {
522 global $post;
523
524 $objects = $this->adminify_pto_get_options();
525 if ( empty( $objects ) ) {
526 return $where;
527 }
528
529 if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
530 $current_menu_order = $post->menu_order;
531 $where = str_replace( "p.post_date > '" . $post->post_date . "'", "p.menu_order < '" . $current_menu_order . "'", $where );
532 }
533 return $where;
534 }
535
536 function adminify_pto_next_post_sort( $orderby ) {
537 global $post;
538
539 $objects = $this->adminify_pto_get_options();
540 if ( empty( $objects ) ) {
541 return $orderby;
542 }
543
544 if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
545 $orderby = 'ORDER BY p.menu_order DESC LIMIT 1';
546 }
547 return $orderby;
548 }
549
550
551 function adminify_pto_get_terms_orderby( $orderby, $args ) {
552 if ( is_admin() ) {
553 return $orderby;
554 }
555
556 $tags = $this->adminify_pto_get_options_taxonomies();
557
558 if (
559 ! isset( $args['taxonomy'] )
560 ) {
561 return $orderby;
562 }
563
564 $taxonomy = $args['taxonomy'];
565 if ( ! in_array( $taxonomy, $tags ) ) {
566 return $orderby;
567 }
568
569 $orderby = 't.term_order';
570 return $orderby;
571 }
572
573 function adminify_pto_get_object_terms( $terms ) {
574 $tags = $this->adminify_pto_get_options_taxonomies();
575
576 if ( is_admin() && isset( $_GET['orderby'] ) ) {
577 return $terms;
578 }
579
580 foreach ( $terms as $key => $term ) {
581 if ( is_object( $term ) && isset( $term->taxonomy ) ) {
582 $taxonomy = $term->taxonomy;
583 if (
584 ! in_array( $taxonomy, $tags )
585 ) {
586 return $terms;
587 }
588 } else {
589 return $terms;
590 }
591 }
592
593 usort(
594 $terms,
595 [ $this, 'jltwp_adminify_taxonomy_compare' ]
596 );
597 return $terms;
598 }
599
600
601
602 public function adminify_pto_sites_clauses( $pieces = [] ) {
603 global $blog_id;
604
605 if ( is_admin() ) {
606 return $pieces;
607 }
608 // if (1 != $blog_id) {
609 // $current = $blog_id;
610 // switch_to_blog(1);
611 // $adminify_pto_sites = get_option('adminify_pto_sites');
612 // switch_to_blog($current);
613 // if (!$adminify_pto_sites) return $pieces;
614 // } else {
615 // if (!get_option('adminify_pto_sites')) return $pieces;
616 // }
617
618 global $wp_version;
619 if ( version_compare( $wp_version, '4.6.0' ) >= 0 ) {
620 if ( 'blog_id ASC' === $pieces['orderby'] ) {
621 $pieces['orderby'] = 'menu_order ASC';
622 }
623 }
624 return $pieces;
625 }
626
627 public function adminify_pto_update_sites() {
628 global $wpdb;
629
630 parse_str( sanitize_text_field( wp_unslash( $_POST['order'] ) ), $data );
631
632 if ( ! is_array( $data ) ) {
633 return false;
634 }
635
636 $id_arr = [];
637 foreach ( $data as $key => $values ) {
638 foreach ( $values as $position => $id ) {
639 $id_arr[] = $id;
640 }
641 }
642
643 foreach ( $data as $key => $values ) {
644 foreach ( $values as $position => $id ) {
645 $wpdb->update( $wpdb->prefix . 'blogs', [ 'menu_order' => $position + 1 ], [ 'blog_id' => intval( $id ) ] );
646 }
647 }
648
649 die();
650 }
651
652 /* before wp v4.6.0 */
653 public function adminify_pto_refresh_front_network() {
654 global $wp_version;
655 if ( version_compare( $wp_version, '4.6.0' ) < 0 ) {
656 // global $blog_id;
657 // if (1 != $blog_id) {
658 // $current = $blog_id;
659 // switch_to_blog(1);
660 // $adminify_pto_sites = get_option('adminify_pto_sites');
661 // switch_to_blog($current);
662 // if (!$adminify_pto_sites) return;
663 // }
664 // else {
665 // if (!get_option('adminify_pto_sites')) return;
666 // }
667 add_filter( 'query', [ $this, 'adminify_pto_refresh_front_network_second' ] );
668 }
669 }
670
671 public function adminify_pto_refresh_network() {
672 global $pagenow;
673 if ( 'sites.php' === $pagenow && ! isset( $_GET['orderby'] ) ) {
674 add_filter( 'query', [ $this, 'adminify_pto_refresh_network_second' ] );
675 }
676 }
677
678
679 public function adminify_pto_refresh_network_second( $query ) {
680 global $wpdb, $wp_version, $blog_id;
681
682 /**
683 * after wp4.7.0
684 * eq.) SELECT option_name, option_value FROM wp_11_options WHERE autoload = 'yes'
685 */
686
687 // $wpdb->get_varやswitch_to_blog(1)
688 if ( version_compare( $wp_version, '4.7.0' ) >= 0 ) {
689 if ( 1 !== $blog_id ) {
690 return $query;
691 }
692 }
693
694 // $adminify_pto_sites = get_option('adminify_pto_sites');
695 // if (!$adminify_pto_sites) return $query;
696
697 if (
698 false !== strpos( $query, "SELECT * FROM $wpdb->blogs WHERE site_id = '1'" ) ||
699 false !== strpos( $query, "SQL_CALC_FOUND_ROWS blog_id FROM $wpdb->blogs WHERE site_id = 1" )
700 ) {
701 if ( false !== strpos( $query, ' LIMIT ' ) ) {
702 $query = preg_replace( '/^(.*) LIMIT(.*)$/', '$1 ORDER BY menu_order ASC LIMIT $2', $query );
703 } else {
704 $query .= ' ORDER BY menu_order ASC';
705 }
706 }
707 return $query;
708 }
709
710
711 public function adminify_pto_get_blogs_of_user( $blogs ) {
712 // global $blog_id;
713 // if (1 != $blog_id) {
714 // $current = $blog_id;
715 // switch_to_blog(1);
716 // $adminify_pto_sites = get_option('adminify_pto_sites');
717 // switch_to_blog($current);
718 // if (!$adminify_pto_sites) return $blogs;
719 // } else {
720 // if (!get_option('adminify_pto_sites')) return $blogs;
721 // }
722 global $wpdb, $wp_version;
723
724 if ( version_compare( $wp_version, '4.6.0' ) >= 0 ) {
725 $sites = get_sites( [] );
726 $sort_keys = [];
727 foreach ( $sites as $k => $v ) {
728 $sort_keys[] = $v->menu_order;
729 }
730 array_multisort( $sort_keys, SORT_ASC, $sites );
731
732 $blog_list = [];
733 foreach ( $blogs as $k => $v ) {
734 $blog_list[ $v->userblog_id ] = $v;
735 }
736
737 $new = [];
738 foreach ( $sites as $k => $v ) {
739 if (
740 isset( $v->blog_id ) &&
741 isset( $blog_list[ $v->blog_id ] ) &&
742 is_object( $blog_list[ $v->blog_id ] )
743 ) {
744 $new[] = $blog_list[ $v->blog_id ];
745 }
746 }
747 } else {
748 $sites = wp_get_sites( [ 'limit' => 9999 ] );
749 $sort_keys = [];
750 foreach ( $sites as $k => $v ) {
751 $sort_keys[] = $v['menu_order'];
752 }
753 array_multisort( $sort_keys, SORT_ASC, $sites );
754
755 $blog_list = [];
756 foreach ( $blogs as $k => $v ) {
757 $blog_list[ $v->userblog_id ] = $v;
758 }
759
760 $new = [];
761 foreach ( $sites as $k => $v ) {
762 if (
763 isset( $v['blog_id'] ) &&
764 isset( $blog_list[ $v['blog_id'] ] ) &&
765 is_object( $blog_list[ $v['blog_id'] ] )
766 ) {
767 $new[] = $blog_list[ $v['blog_id'] ];
768 }
769 }
770 }
771 return $new;
772 }
773
774
775 public function adminify_pto_refresh_front_network_second( $query ) {
776 global $wpdb;
777 if ( false !== strpos( $query, "SELECT blog_id FROM $wpdb->blogs ORDER BY blog_id ASC" ) ) {
778 $query = str_replace( 'ORDER BY blog_id ASC', '', $query );
779 if ( false !== strpos( $query, ' LIMIT ' ) ) {
780 $query = preg_replace( '/^(.*) LIMIT(.*)$/', '$1 ORDER BY menu_order ASC LIMIT $2', $query );
781 } else {
782 $query .= ' ORDER BY menu_order ASC';
783 }
784 } elseif ( false !== strpos( $query, "SELECT * FROM $wpdb->blogs WHERE 1=1 AND site_id IN (1)" ) ) {
785 if ( false !== strpos( $query, ' LIMIT ' ) ) {
786 $query = preg_replace( '/^(.*) LIMIT(.*)$/', '$1 ORDER BY menu_order ASC LIMIT $2', $query );
787 } else {
788 $query .= ' ORDER BY menu_order ASC';
789 }
790 }
791 return $query;
792 }
793 }
794