PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 3.0.5
Adminify – White Label, Admin Menu Editor, Login Customizer v3.0.5
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 3.0.5, at Inc/Modules/PostTypesOrder/PostTypesOrder.php

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