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

818 lines 24.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 // Security check - verify nonce and capability
276 check_ajax_referer( 'adminify-pto-security-nonce', 'security' );
277
278 if ( ! current_user_can( 'edit_others_posts' ) ) {
279 wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'adminify' ) ) );
280 }
281
282 global $wpdb;
283 $data = [];
284 parse_str( sanitize_text_field( wp_unslash( $_POST['order'] ) ), $data );
285
286 if ( ! is_array( $data ) ) {
287 return false;
288 }
289
290 // get objects per now page
291 $id_arr = [];
292 foreach ( $data as $key => $values ) {
293 foreach ( $values as $position => $id ) {
294 $id_arr[] = $id;
295 }
296 }
297
298 // get menu_order of objects per now page
299 $menu_order_arr = [];
300 foreach ( $id_arr as $key => $id ) {
301 $results = $wpdb->get_results( "SELECT menu_order FROM $wpdb->posts WHERE ID = " . intval( $id ) );
302 foreach ( $results as $result ) {
303 $menu_order_arr[] = $result->menu_order;
304 }
305 }
306
307 // maintains key association = no
308 sort( $menu_order_arr );
309
310 foreach ( $data as $key => $values ) {
311 foreach ( $values as $position => $id ) {
312 $wpdb->update( $wpdb->posts, [ 'menu_order' => $menu_order_arr[ $position ] ], [ 'ID' => intval( $id ) ] );
313 }
314 }
315
316 // same number check
317 $post_type = get_post_type( $id );
318 $sql = "SELECT COUNT(menu_order) AS mo_count, post_type, 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 GROUP BY post_type, menu_order HAVING (mo_count) > 1";
321 $results = $wpdb->get_results( $sql );
322 if ( count( $results ) > 0 ) {
323
324 // menu_order refresh
325 $sql = "SELECT ID, menu_order FROM $wpdb->posts
326 WHERE post_type = '{$post_type}' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
327 AND menu_order > 0 ORDER BY menu_order";
328 $results = $wpdb->get_results( $sql );
329 foreach ( $results as $key => $result ) {
330 $view_posi = array_search( $result->ID, $id_arr, true );
331 if ( $view_posi === false ) {
332 $view_posi = 999;
333 }
334 $sort_key = ( $result->menu_order * 1000 ) + $view_posi;
335 $sort_ids[ $sort_key ] = $result->ID;
336 }
337 ksort( $sort_ids );
338 $oreder_no = 0;
339 foreach ( $sort_ids as $key => $id ) {
340 $oreder_no = $oreder_no + 1;
341 $wpdb->update( $wpdb->posts, [ 'menu_order' => $oreder_no ], [ 'ID' => intval( $id ) ] );
342 }
343 }
344 }
345
346 public function adminify_pto_update_taxonomy() {
347 // Security check - verify nonce and capability
348 check_ajax_referer( 'adminify-pto-security-nonce', 'security' );
349
350 if ( ! current_user_can( 'manage_categories' ) ) {
351 wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'adminify' ) ) );
352 }
353
354 global $wpdb;
355
356 parse_str( sanitize_text_field( wp_unslash( $_POST['order'] ) ), $data );
357
358 if ( ! is_array( $data ) ) {
359 return false;
360 }
361
362 $id_arr = [];
363 foreach ( $data as $key => $values ) {
364 foreach ( $values as $position => $id ) {
365 $id_arr[] = $id;
366 }
367 }
368
369 $menu_order_arr = [];
370 foreach ( $id_arr as $key => $id ) {
371 $results = $wpdb->get_results( "SELECT term_order FROM $wpdb->terms WHERE term_id = " . intval( $id ) );
372 foreach ( $results as $result ) {
373 $menu_order_arr[] = $result->term_order;
374 }
375 }
376 sort( $menu_order_arr );
377
378 foreach ( $data as $key => $values ) {
379 foreach ( $values as $position => $id ) {
380 $wpdb->update( $wpdb->terms, [ 'term_order' => $menu_order_arr[ $position ] ], [ 'term_id' => intval( $id ) ] );
381 }
382 }
383
384 // same number check
385 $term = get_term( $id );
386 $taxonomy = $term->taxonomy;
387 $sql = "SELECT COUNT(term_order) AS to_count, term_order
388 FROM $wpdb->terms AS terms
389 INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
390 WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'GROUP BY taxonomy, term_order HAVING (to_count) > 1";
391 $results = $wpdb->get_results( $sql );
392 if ( count( $results ) > 0 ) {
393 // term_order refresh
394 $sql = "SELECT terms.term_id, term_order
395 FROM $wpdb->terms AS terms
396 INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
397 WHERE term_taxonomy.taxonomy = '" . $taxonomy . "'
398 ORDER BY term_order ASC";
399 $results = $wpdb->get_results( $sql );
400 foreach ( $results as $key => $result ) {
401 $view_posi = array_search( $result->term_id, $id_arr, true );
402 if ( $view_posi === false ) {
403 $view_posi = 999;
404 }
405 $sort_key = ( $result->term_order * 1000 ) + $view_posi;
406 $sort_ids[ $sort_key ] = $result->term_id;
407 }
408 ksort( $sort_ids );
409 $oreder_no = 0;
410 foreach ( $sort_ids as $key => $id ) {
411 $oreder_no = $oreder_no + 1;
412 $wpdb->update( $wpdb->terms, [ 'term_order' => $oreder_no ], [ 'term_id' => $id ] );
413 }
414 }
415
416 do_action( 'wpadminify_update_post_types_order_taxonomy' );
417 }
418
419 public function jltwp_adminify_taxonomy_compare( $a, $b ) {
420 if ( $a->term_order == $b->term_order ) {
421 return 0;
422 }
423 return ( $a->term_order < $b->term_order ) ? -1 : 1;
424 }
425
426 public function adminify_pto_load_scripts() {
427 global $pagenow, $typenow;
428
429 if ( $this->conditional_script_load() || ( $pagenow === 'upload.php' && ( isset( $this->options['pto_media'] ) && $this->options['pto_media'] ) ) ) {
430 wp_enqueue_script( 'jquery' );
431 wp_enqueue_script( 'jquery-ui-sortable' );
432 wp_enqueue_script( 'wp-adminify-post-type-order', WP_ADMINIFY_URL . 'Inc/Modules/PostTypesOrder/js/post-type-order.js', [ 'jquery' ], WP_ADMINIFY_VER, true );
433 wp_localize_script( 'wp-adminify-post-type-order', 'adminify_pto', array(
434 'nonce' => wp_create_nonce( 'adminify-pto-security-nonce' ),
435 ) );
436 }
437 }
438
439 public function adminify_pto_css() {
440 global $pagenow, $typenow;
441 if ( $this->conditional_script_load() || ( $pagenow === 'upload.php' && ( ! empty( $this->options['pto_media'] ) ) ) ) {
442 echo '<!-- Start of Post Type and Taxonomy Order --->';
443 echo '<style type="text/css">';
444 // 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%;}';
445 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}}';
446 echo '</style>';
447 echo '<!-- End of Post Type and Taxonomy Order --->';
448 }
449 }
450
451 public function conditional_script_load() {
452 global $pagenow, $typenow;
453
454 $active = false;
455
456 // Multisite > Sites
457 if (
458 function_exists( 'is_multisite' )
459 && is_multisite()
460 && $pagenow == 'sites.php'
461 // && get_option('adminify_pto_network_sites')
462 ) {
463 return true;
464 }
465
466 $objects = $this->adminify_pto_get_options();
467 $tags = $this->adminify_pto_get_options_taxonomies();
468
469 if ( empty( $objects ) && empty( $tags ) ) {
470 return false;
471 }
472
473 // exclude (sorting, addnew page, edit page)
474 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' ) ) ) {
475 return false;
476 }
477
478 if ( ! empty( $objects ) ) {
479 if ( isset( $_GET['post_type'] ) && ! isset( $_GET['taxonomy'] ) && in_array( $_GET['post_type'], $objects ) ) { // if page or custom post types
480 $active = true;
481 }
482 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 ) ) ) {
483 // if post
484 $active = true;
485 }
486 }
487
488 if ( ! empty( $tags ) ) {
489 if ( isset( $_GET['taxonomy'] ) && in_array( $_GET['taxonomy'], $tags ) ) {
490 $active = true;
491 }
492 }
493
494 return $active;
495 }
496
497 function adminify_pto_get_options() {
498 $objects = isset( $this->options['pto_posts'] ) && is_array( $this->options['pto_posts'] ) ? $this->options['pto_posts'] : [];
499 return $objects;
500 }
501
502 function adminify_pto_get_options_taxonomies() {
503 $tags = isset( $this->options['pto_taxonomies'] ) && is_array( $this->options['pto_taxonomies'] ) ? $this->options['pto_taxonomies'] : [];
504 return $tags;
505 }
506
507 function adminify_pto_previous_post_where( $where ) {
508 global $post;
509
510 $objects = $this->adminify_pto_get_options();
511 if ( empty( $objects ) ) {
512 return $where;
513 }
514
515 if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
516 $current_menu_order = $post->menu_order;
517 $where = str_replace( "p.post_date < '" . $post->post_date . "'", "p.menu_order > '" . $current_menu_order . "'", $where );
518 }
519 return $where;
520 }
521
522
523 function adminify_pto_previous_post_sort( $orderby ) {
524 global $post;
525
526 $objects = $this->adminify_pto_get_options();
527 if ( empty( $objects ) ) {
528 return $orderby;
529 }
530
531 if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
532 $orderby = 'ORDER BY p.menu_order ASC LIMIT 1';
533 }
534 return $orderby;
535 }
536
537
538 function adminify_pto_next_post_where( $where ) {
539 global $post;
540
541 $objects = $this->adminify_pto_get_options();
542 if ( empty( $objects ) ) {
543 return $where;
544 }
545
546 if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
547 $current_menu_order = $post->menu_order;
548 $where = str_replace( "p.post_date > '" . $post->post_date . "'", "p.menu_order < '" . $current_menu_order . "'", $where );
549 }
550 return $where;
551 }
552
553 function adminify_pto_next_post_sort( $orderby ) {
554 global $post;
555
556 $objects = $this->adminify_pto_get_options();
557 if ( empty( $objects ) ) {
558 return $orderby;
559 }
560
561 if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
562 $orderby = 'ORDER BY p.menu_order DESC LIMIT 1';
563 }
564 return $orderby;
565 }
566
567
568 function adminify_pto_get_terms_orderby( $orderby, $args ) {
569 if ( is_admin() ) {
570 return $orderby;
571 }
572
573 $tags = $this->adminify_pto_get_options_taxonomies();
574
575 if (
576 ! isset( $args['taxonomy'] )
577 ) {
578 return $orderby;
579 }
580
581 $taxonomy = $args['taxonomy'];
582 if ( ! in_array( $taxonomy, $tags ) ) {
583 return $orderby;
584 }
585
586 $orderby = 't.term_order';
587 return $orderby;
588 }
589
590 function adminify_pto_get_object_terms( $terms ) {
591 $tags = $this->adminify_pto_get_options_taxonomies();
592
593 if ( is_admin() && isset( $_GET['orderby'] ) ) {
594 return $terms;
595 }
596
597 foreach ( $terms as $key => $term ) {
598 if ( is_object( $term ) && isset( $term->taxonomy ) ) {
599 $taxonomy = $term->taxonomy;
600 if (
601 ! in_array( $taxonomy, $tags )
602 ) {
603 return $terms;
604 }
605 } else {
606 return $terms;
607 }
608 }
609
610 usort(
611 $terms,
612 [ $this, 'jltwp_adminify_taxonomy_compare' ]
613 );
614 return $terms;
615 }
616
617
618
619 public function adminify_pto_sites_clauses( $pieces = [] ) {
620 global $blog_id;
621
622 if ( is_admin() ) {
623 return $pieces;
624 }
625 // if (1 != $blog_id) {
626 // $current = $blog_id;
627 // switch_to_blog(1);
628 // $adminify_pto_sites = get_option('adminify_pto_sites');
629 // switch_to_blog($current);
630 // if (!$adminify_pto_sites) return $pieces;
631 // } else {
632 // if (!get_option('adminify_pto_sites')) return $pieces;
633 // }
634
635 global $wp_version;
636 if ( version_compare( $wp_version, '4.6.0' ) >= 0 ) {
637 if ( 'blog_id ASC' === $pieces['orderby'] ) {
638 $pieces['orderby'] = 'menu_order ASC';
639 }
640 }
641 return $pieces;
642 }
643
644 public function adminify_pto_update_sites() {
645 // Security check - verify nonce and capability
646 check_ajax_referer( 'adminify-pto-security-nonce', 'security' );
647
648 if ( ! current_user_can( 'manage_sites' ) ) {
649 wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'adminify' ) ) );
650 }
651
652 global $wpdb;
653
654 parse_str( sanitize_text_field( wp_unslash( $_POST['order'] ) ), $data );
655
656 if ( ! is_array( $data ) ) {
657 return false;
658 }
659
660 $id_arr = [];
661 foreach ( $data as $key => $values ) {
662 foreach ( $values as $position => $id ) {
663 $id_arr[] = $id;
664 }
665 }
666
667 foreach ( $data as $key => $values ) {
668 foreach ( $values as $position => $id ) {
669 $wpdb->update( $wpdb->prefix . 'blogs', [ 'menu_order' => $position + 1 ], [ 'blog_id' => intval( $id ) ] );
670 }
671 }
672
673 die();
674 }
675
676 /* before wp v4.6.0 */
677 public function adminify_pto_refresh_front_network() {
678 global $wp_version;
679 if ( version_compare( $wp_version, '4.6.0' ) < 0 ) {
680 // global $blog_id;
681 // if (1 != $blog_id) {
682 // $current = $blog_id;
683 // switch_to_blog(1);
684 // $adminify_pto_sites = get_option('adminify_pto_sites');
685 // switch_to_blog($current);
686 // if (!$adminify_pto_sites) return;
687 // }
688 // else {
689 // if (!get_option('adminify_pto_sites')) return;
690 // }
691 add_filter( 'query', [ $this, 'adminify_pto_refresh_front_network_second' ] );
692 }
693 }
694
695 public function adminify_pto_refresh_network() {
696 global $pagenow;
697 if ( 'sites.php' === $pagenow && ! isset( $_GET['orderby'] ) ) {
698 add_filter( 'query', [ $this, 'adminify_pto_refresh_network_second' ] );
699 }
700 }
701
702
703 public function adminify_pto_refresh_network_second( $query ) {
704 global $wpdb, $wp_version, $blog_id;
705
706 /**
707 * after wp4.7.0
708 * eq.) SELECT option_name, option_value FROM wp_11_options WHERE autoload = 'yes'
709 */
710
711 // $wpdb->get_varやswitch_to_blog(1)
712 if ( version_compare( $wp_version, '4.7.0' ) >= 0 ) {
713 if ( 1 !== $blog_id ) {
714 return $query;
715 }
716 }
717
718 // $adminify_pto_sites = get_option('adminify_pto_sites');
719 // if (!$adminify_pto_sites) return $query;
720
721 if (
722 false !== strpos( $query, "SELECT * FROM $wpdb->blogs WHERE site_id = '1'" ) ||
723 false !== strpos( $query, "SQL_CALC_FOUND_ROWS blog_id FROM $wpdb->blogs WHERE site_id = 1" )
724 ) {
725 if ( false !== strpos( $query, ' LIMIT ' ) ) {
726 $query = preg_replace( '/^(.*) LIMIT(.*)$/', '$1 ORDER BY menu_order ASC LIMIT $2', $query );
727 } else {
728 $query .= ' ORDER BY menu_order ASC';
729 }
730 }
731 return $query;
732 }
733
734
735 public function adminify_pto_get_blogs_of_user( $blogs ) {
736 // global $blog_id;
737 // if (1 != $blog_id) {
738 // $current = $blog_id;
739 // switch_to_blog(1);
740 // $adminify_pto_sites = get_option('adminify_pto_sites');
741 // switch_to_blog($current);
742 // if (!$adminify_pto_sites) return $blogs;
743 // } else {
744 // if (!get_option('adminify_pto_sites')) return $blogs;
745 // }
746 global $wpdb, $wp_version;
747
748 if ( version_compare( $wp_version, '4.6.0' ) >= 0 ) {
749 $sites = get_sites( [] );
750 $sort_keys = [];
751 foreach ( $sites as $k => $v ) {
752 $sort_keys[] = $v->menu_order;
753 }
754 array_multisort( $sort_keys, SORT_ASC, $sites );
755
756 $blog_list = [];
757 foreach ( $blogs as $k => $v ) {
758 $blog_list[ $v->userblog_id ] = $v;
759 }
760
761 $new = [];
762 foreach ( $sites as $k => $v ) {
763 if (
764 isset( $v->blog_id ) &&
765 isset( $blog_list[ $v->blog_id ] ) &&
766 is_object( $blog_list[ $v->blog_id ] )
767 ) {
768 $new[] = $blog_list[ $v->blog_id ];
769 }
770 }
771 } else {
772 $sites = wp_get_sites( [ 'limit' => 9999 ] );
773 $sort_keys = [];
774 foreach ( $sites as $k => $v ) {
775 $sort_keys[] = $v['menu_order'];
776 }
777 array_multisort( $sort_keys, SORT_ASC, $sites );
778
779 $blog_list = [];
780 foreach ( $blogs as $k => $v ) {
781 $blog_list[ $v->userblog_id ] = $v;
782 }
783
784 $new = [];
785 foreach ( $sites as $k => $v ) {
786 if (
787 isset( $v['blog_id'] ) &&
788 isset( $blog_list[ $v['blog_id'] ] ) &&
789 is_object( $blog_list[ $v['blog_id'] ] )
790 ) {
791 $new[] = $blog_list[ $v['blog_id'] ];
792 }
793 }
794 }
795 return $new;
796 }
797
798
799 public function adminify_pto_refresh_front_network_second( $query ) {
800 global $wpdb;
801 if ( false !== strpos( $query, "SELECT blog_id FROM $wpdb->blogs ORDER BY blog_id ASC" ) ) {
802 $query = str_replace( 'ORDER BY blog_id ASC', '', $query );
803 if ( false !== strpos( $query, ' LIMIT ' ) ) {
804 $query = preg_replace( '/^(.*) LIMIT(.*)$/', '$1 ORDER BY menu_order ASC LIMIT $2', $query );
805 } else {
806 $query .= ' ORDER BY menu_order ASC';
807 }
808 } elseif ( false !== strpos( $query, "SELECT * FROM $wpdb->blogs WHERE 1=1 AND site_id IN (1)" ) ) {
809 if ( false !== strpos( $query, ' LIMIT ' ) ) {
810 $query = preg_replace( '/^(.*) LIMIT(.*)$/', '$1 ORDER BY menu_order ASC LIMIT $2', $query );
811 } else {
812 $query .= ' ORDER BY menu_order ASC';
813 }
814 }
815 return $query;
816 }
817 }
818