| 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; } .ui-sortable-placeholder { display: none; } .wp-list-table { table-layout: auto; width: 100%;}'; |
| 428 |
echo '</style>'; |
| 429 |
echo '<!-- End of Post Type and Taxonomy Order --->'; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
public function conditional_script_load() { |
| 434 |
global $pagenow, $typenow; |
| 435 |
|
| 436 |
$active = false; |
| 437 |
|
| 438 |
// Multisite > Sites |
| 439 |
if ( |
| 440 |
function_exists( 'is_multisite' ) |
| 441 |
&& is_multisite() |
| 442 |
&& $pagenow == 'sites.php' |
| 443 |
// && get_option('adminify_pto_network_sites') |
| 444 |
) { |
| 445 |
return true; |
| 446 |
} |
| 447 |
|
| 448 |
$objects = $this->adminify_pto_get_options(); |
| 449 |
$tags = $this->adminify_pto_get_options_taxonomies(); |
| 450 |
|
| 451 |
if ( empty( $objects ) && empty( $tags ) ) { |
| 452 |
return false; |
| 453 |
} |
| 454 |
|
| 455 |
// exclude (sorting, addnew page, edit page) |
| 456 |
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' ) ) ) { |
| 457 |
return false; |
| 458 |
} |
| 459 |
|
| 460 |
if ( ! empty( $objects ) ) { |
| 461 |
if ( isset( $_GET['post_type'] ) && ! isset( $_GET['taxonomy'] ) && in_array( $_GET['post_type'], $objects ) ) { // if page or custom post types |
| 462 |
$active = true; |
| 463 |
} |
| 464 |
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 ) ) ) { |
| 465 |
// if post |
| 466 |
$active = true; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
if ( ! empty( $tags ) ) { |
| 471 |
if ( isset( $_GET['taxonomy'] ) && in_array( $_GET['taxonomy'], $tags ) ) { |
| 472 |
$active = true; |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
return $active; |
| 477 |
} |
| 478 |
|
| 479 |
function adminify_pto_get_options() { |
| 480 |
$objects = isset( $this->options['pto_posts'] ) && is_array( $this->options['pto_posts'] ) ? $this->options['pto_posts'] : []; |
| 481 |
return $objects; |
| 482 |
} |
| 483 |
|
| 484 |
function adminify_pto_get_options_taxonomies() { |
| 485 |
$tags = isset( $this->options['pto_taxonomies'] ) && is_array( $this->options['pto_taxonomies'] ) ? $this->options['pto_taxonomies'] : []; |
| 486 |
return $tags; |
| 487 |
} |
| 488 |
|
| 489 |
function adminify_pto_previous_post_where( $where ) { |
| 490 |
global $post; |
| 491 |
|
| 492 |
$objects = $this->adminify_pto_get_options(); |
| 493 |
if ( empty( $objects ) ) { |
| 494 |
return $where; |
| 495 |
} |
| 496 |
|
| 497 |
if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) { |
| 498 |
$current_menu_order = $post->menu_order; |
| 499 |
$where = str_replace( "p.post_date < '" . $post->post_date . "'", "p.menu_order > '" . $current_menu_order . "'", $where ); |
| 500 |
} |
| 501 |
return $where; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
function adminify_pto_previous_post_sort( $orderby ) { |
| 506 |
global $post; |
| 507 |
|
| 508 |
$objects = $this->adminify_pto_get_options(); |
| 509 |
if ( empty( $objects ) ) { |
| 510 |
return $orderby; |
| 511 |
} |
| 512 |
|
| 513 |
if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) { |
| 514 |
$orderby = 'ORDER BY p.menu_order ASC LIMIT 1'; |
| 515 |
} |
| 516 |
return $orderby; |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
function adminify_pto_next_post_where( $where ) { |
| 521 |
global $post; |
| 522 |
|
| 523 |
$objects = $this->adminify_pto_get_options(); |
| 524 |
if ( empty( $objects ) ) { |
| 525 |
return $where; |
| 526 |
} |
| 527 |
|
| 528 |
if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) { |
| 529 |
$current_menu_order = $post->menu_order; |
| 530 |
$where = str_replace( "p.post_date > '" . $post->post_date . "'", "p.menu_order < '" . $current_menu_order . "'", $where ); |
| 531 |
} |
| 532 |
return $where; |
| 533 |
} |
| 534 |
|
| 535 |
function adminify_pto_next_post_sort( $orderby ) { |
| 536 |
global $post; |
| 537 |
|
| 538 |
$objects = $this->adminify_pto_get_options(); |
| 539 |
if ( empty( $objects ) ) { |
| 540 |
return $orderby; |
| 541 |
} |
| 542 |
|
| 543 |
if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) { |
| 544 |
$orderby = 'ORDER BY p.menu_order DESC LIMIT 1'; |
| 545 |
} |
| 546 |
return $orderby; |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
function adminify_pto_get_terms_orderby( $orderby, $args ) { |
| 551 |
if ( is_admin() ) { |
| 552 |
return $orderby; |
| 553 |
} |
| 554 |
|
| 555 |
$tags = $this->adminify_pto_get_options_taxonomies(); |
| 556 |
|
| 557 |
if ( |
| 558 |
! isset( $args['taxonomy'] ) |
| 559 |
) { |
| 560 |
return $orderby; |
| 561 |
} |
| 562 |
|
| 563 |
$taxonomy = $args['taxonomy']; |
| 564 |
if ( ! in_array( $taxonomy, $tags ) ) { |
| 565 |
return $orderby; |
| 566 |
} |
| 567 |
|
| 568 |
$orderby = 't.term_order'; |
| 569 |
return $orderby; |
| 570 |
} |
| 571 |
|
| 572 |
function adminify_pto_get_object_terms( $terms ) { |
| 573 |
$tags = $this->adminify_pto_get_options_taxonomies(); |
| 574 |
|
| 575 |
if ( is_admin() && isset( $_GET['orderby'] ) ) { |
| 576 |
return $terms; |
| 577 |
} |
| 578 |
|
| 579 |
foreach ( $terms as $key => $term ) { |
| 580 |
if ( is_object( $term ) && isset( $term->taxonomy ) ) { |
| 581 |
$taxonomy = $term->taxonomy; |
| 582 |
if ( |
| 583 |
! in_array( $taxonomy, $tags ) |
| 584 |
) { |
| 585 |
return $terms; |
| 586 |
} |
| 587 |
} else { |
| 588 |
return $terms; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
usort( |
| 593 |
$terms, |
| 594 |
[ $this, 'jltwp_adminify_taxonomy_compare' ] |
| 595 |
); |
| 596 |
return $terms; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
public function adminify_pto_sites_clauses( $pieces = [] ) { |
| 602 |
global $blog_id; |
| 603 |
|
| 604 |
if ( is_admin() ) { |
| 605 |
return $pieces; |
| 606 |
} |
| 607 |
// if (1 != $blog_id) { |
| 608 |
// $current = $blog_id; |
| 609 |
// switch_to_blog(1); |
| 610 |
// $adminify_pto_sites = get_option('adminify_pto_sites'); |
| 611 |
// switch_to_blog($current); |
| 612 |
// if (!$adminify_pto_sites) return $pieces; |
| 613 |
// } else { |
| 614 |
// if (!get_option('adminify_pto_sites')) return $pieces; |
| 615 |
// } |
| 616 |
|
| 617 |
global $wp_version; |
| 618 |
if ( version_compare( $wp_version, '4.6.0' ) >= 0 ) { |
| 619 |
if ( 'blog_id ASC' === $pieces['orderby'] ) { |
| 620 |
$pieces['orderby'] = 'menu_order ASC'; |
| 621 |
} |
| 622 |
} |
| 623 |
return $pieces; |
| 624 |
} |
| 625 |
|
| 626 |
public function adminify_pto_update_sites() { |
| 627 |
global $wpdb; |
| 628 |
|
| 629 |
parse_str( sanitize_text_field( wp_unslash( $_POST['order'] ) ), $data ); |
| 630 |
|
| 631 |
if ( ! is_array( $data ) ) { |
| 632 |
return false; |
| 633 |
} |
| 634 |
|
| 635 |
$id_arr = []; |
| 636 |
foreach ( $data as $key => $values ) { |
| 637 |
foreach ( $values as $position => $id ) { |
| 638 |
$id_arr[] = $id; |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
foreach ( $data as $key => $values ) { |
| 643 |
foreach ( $values as $position => $id ) { |
| 644 |
$wpdb->update( $wpdb->prefix . 'blogs', [ 'menu_order' => $position + 1 ], [ 'blog_id' => intval( $id ) ] ); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
die(); |
| 649 |
} |
| 650 |
|
| 651 |
/* before wp v4.6.0 */ |
| 652 |
public function adminify_pto_refresh_front_network() { |
| 653 |
global $wp_version; |
| 654 |
if ( version_compare( $wp_version, '4.6.0' ) < 0 ) { |
| 655 |
// global $blog_id; |
| 656 |
// if (1 != $blog_id) { |
| 657 |
// $current = $blog_id; |
| 658 |
// switch_to_blog(1); |
| 659 |
// $adminify_pto_sites = get_option('adminify_pto_sites'); |
| 660 |
// switch_to_blog($current); |
| 661 |
// if (!$adminify_pto_sites) return; |
| 662 |
// } |
| 663 |
// else { |
| 664 |
// if (!get_option('adminify_pto_sites')) return; |
| 665 |
// } |
| 666 |
add_filter( 'query', [ $this, 'adminify_pto_refresh_front_network_second' ] ); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
public function adminify_pto_refresh_network() { |
| 671 |
global $pagenow; |
| 672 |
if ( 'sites.php' === $pagenow && ! isset( $_GET['orderby'] ) ) { |
| 673 |
add_filter( 'query', [ $this, 'adminify_pto_refresh_network_second' ] ); |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
public function adminify_pto_refresh_network_second( $query ) { |
| 679 |
global $wpdb, $wp_version, $blog_id; |
| 680 |
|
| 681 |
/** |
| 682 |
* after wp4.7.0 |
| 683 |
* eq.) SELECT option_name, option_value FROM wp_11_options WHERE autoload = 'yes' |
| 684 |
*/ |
| 685 |
|
| 686 |
// $wpdb->get_varやswitch_to_blog(1) |
| 687 |
if ( version_compare( $wp_version, '4.7.0' ) >= 0 ) { |
| 688 |
if ( 1 !== $blog_id ) { |
| 689 |
return $query; |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
// $adminify_pto_sites = get_option('adminify_pto_sites'); |
| 694 |
// if (!$adminify_pto_sites) return $query; |
| 695 |
|
| 696 |
if ( |
| 697 |
false !== strpos( $query, "SELECT * FROM $wpdb->blogs WHERE site_id = '1'" ) || |
| 698 |
false !== strpos( $query, "SQL_CALC_FOUND_ROWS blog_id FROM $wpdb->blogs WHERE site_id = 1" ) |
| 699 |
) { |
| 700 |
if ( false !== strpos( $query, ' LIMIT ' ) ) { |
| 701 |
$query = preg_replace( '/^(.*) LIMIT(.*)$/', '$1 ORDER BY menu_order ASC LIMIT $2', $query ); |
| 702 |
} else { |
| 703 |
$query .= ' ORDER BY menu_order ASC'; |
| 704 |
} |
| 705 |
} |
| 706 |
return $query; |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
public function adminify_pto_get_blogs_of_user( $blogs ) { |
| 711 |
// global $blog_id; |
| 712 |
// if (1 != $blog_id) { |
| 713 |
// $current = $blog_id; |
| 714 |
// switch_to_blog(1); |
| 715 |
// $adminify_pto_sites = get_option('adminify_pto_sites'); |
| 716 |
// switch_to_blog($current); |
| 717 |
// if (!$adminify_pto_sites) return $blogs; |
| 718 |
// } else { |
| 719 |
// if (!get_option('adminify_pto_sites')) return $blogs; |
| 720 |
// } |
| 721 |
global $wpdb, $wp_version; |
| 722 |
|
| 723 |
if ( version_compare( $wp_version, '4.6.0' ) >= 0 ) { |
| 724 |
$sites = get_sites( [] ); |
| 725 |
$sort_keys = []; |
| 726 |
foreach ( $sites as $k => $v ) { |
| 727 |
$sort_keys[] = $v->menu_order; |
| 728 |
} |
| 729 |
array_multisort( $sort_keys, SORT_ASC, $sites ); |
| 730 |
|
| 731 |
$blog_list = []; |
| 732 |
foreach ( $blogs as $k => $v ) { |
| 733 |
$blog_list[ $v->userblog_id ] = $v; |
| 734 |
} |
| 735 |
|
| 736 |
$new = []; |
| 737 |
foreach ( $sites as $k => $v ) { |
| 738 |
if ( |
| 739 |
isset( $v->blog_id ) && |
| 740 |
isset( $blog_list[ $v->blog_id ] ) && |
| 741 |
is_object( $blog_list[ $v->blog_id ] ) |
| 742 |
) { |
| 743 |
$new[] = $blog_list[ $v->blog_id ]; |
| 744 |
} |
| 745 |
} |
| 746 |
} else { |
| 747 |
$sites = wp_get_sites( [ 'limit' => 9999 ] ); |
| 748 |
$sort_keys = []; |
| 749 |
foreach ( $sites as $k => $v ) { |
| 750 |
$sort_keys[] = $v['menu_order']; |
| 751 |
} |
| 752 |
array_multisort( $sort_keys, SORT_ASC, $sites ); |
| 753 |
|
| 754 |
$blog_list = []; |
| 755 |
foreach ( $blogs as $k => $v ) { |
| 756 |
$blog_list[ $v->userblog_id ] = $v; |
| 757 |
} |
| 758 |
|
| 759 |
$new = []; |
| 760 |
foreach ( $sites as $k => $v ) { |
| 761 |
if ( |
| 762 |
isset( $v['blog_id'] ) && |
| 763 |
isset( $blog_list[ $v['blog_id'] ] ) && |
| 764 |
is_object( $blog_list[ $v['blog_id'] ] ) |
| 765 |
) { |
| 766 |
$new[] = $blog_list[ $v['blog_id'] ]; |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
return $new; |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
public function adminify_pto_refresh_front_network_second( $query ) { |
| 775 |
global $wpdb; |
| 776 |
if ( false !== strpos( $query, "SELECT blog_id FROM $wpdb->blogs ORDER BY blog_id ASC" ) ) { |
| 777 |
$query = str_replace( 'ORDER BY blog_id ASC', '', $query ); |
| 778 |
if ( false !== strpos( $query, ' LIMIT ' ) ) { |
| 779 |
$query = preg_replace( '/^(.*) LIMIT(.*)$/', '$1 ORDER BY menu_order ASC LIMIT $2', $query ); |
| 780 |
} else { |
| 781 |
$query .= ' ORDER BY menu_order ASC'; |
| 782 |
} |
| 783 |
} elseif ( false !== strpos( $query, "SELECT * FROM $wpdb->blogs WHERE 1=1 AND site_id IN (1)" ) ) { |
| 784 |
if ( false !== strpos( $query, ' LIMIT ' ) ) { |
| 785 |
$query = preg_replace( '/^(.*) LIMIT(.*)$/', '$1 ORDER BY menu_order ASC LIMIT $2', $query ); |
| 786 |
} else { |
| 787 |
$query .= ' ORDER BY menu_order ASC'; |
| 788 |
} |
| 789 |
} |
| 790 |
return $query; |
| 791 |
} |
| 792 |
} |
| 793 |
|