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

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