Changeset 3404972
- Timestamp:
- 11/28/2025 09:30:32 AM (4 months ago)
- Location:
- custom-post-order-category/trunk
- Files:
-
- 2 edited
-
readme.txt (modified) (2 diffs)
-
wp-customcategorypostorder.php (modified) (40 diffs)
Legend:
- Unmodified
- Added
- Removed
-
custom-post-order-category/trunk/readme.txt
r3311928 r3404972 3 3 Tags: custom post order, drag and drop post order, reorder posts, post sorting, custom post type order, category post order, soft delete posts, alphabetical post order, reverse post order, WordPress post management, custom post sorting, reorder posts plugin, sort posts by category, sort custom post types, post order plugin, WordPress content control, advanced post ordering, intuitive post reordering, toggle post sorting. 4 4 Requires at least: 3.3 5 Tested up to: 6.8. 15 Tested up to: 6.8.3 6 6 Stable tag: trunk 7 7 License: GPLv2 or later … … 43 43 44 44 == Changelog == 45 = 2.2 = 46 Fixed compatibility issues with WordPress 6.8.3 and improved performance, fix some minor bugs. 47 45 48 = 2.1 = 46 49 Added translations for French, German, Italian, Russian, and Polish languages. -
custom-post-order-category/trunk/wp-customcategorypostorder.php
r3311928 r3404972 4 4 * Plugin URI: https://scriptut.com/wordpress/custom-category-post-order/ 5 5 * Description: Arrange posts by category or custom post type using a simple drag-and-drop interface. Supports ordering for home page, taxonomies, and custom post types. 6 * Version: 2. 16 * Version: 2.2 7 7 * Author: Faaiq Ahmed 8 8 * Author URI: mailto:nfaaiq@gmail.com … … 34 34 add_action('wp_ajax_ccpo_load_posts', [$this, 'ajax_load_posts']); 35 35 36 add_action( 'wp_ajax_ccpo_get_meta_keys', [ $this, 'ajax_ccpo_get_meta_keys' ]);36 add_action('wp_ajax_ccpo_get_meta_keys', [$this, 'ajax_ccpo_get_meta_keys']); 37 37 38 38 add_action('pre_get_posts', [$this, 'ccpo_custom_taxonomy_ordering']); 39 39 add_action('pre_get_posts', [$this, 'ccpo_custom_category_ordering']); 40 41 42 add_action( 'plugins_loaded', [$this,'ccpo_load_textdomain']);40 41 42 add_action('plugins_loaded', [$this, 'ccpo_load_textdomain']); 43 43 44 44 register_activation_hook(__FILE__, array($this, 'ccpo_install')); … … 46 46 } 47 47 48 function ccpo_load_textdomain() { 49 load_plugin_textdomain( 'custom-category-post-order', false, dirname( plugin_basename(__FILE__) ) . '/languages' ); 50 } 51 52 53 function ccpo_custom_taxonomy_ordering($query) { 48 function ccpo_load_textdomain() 49 { 50 load_plugin_textdomain('custom-category-post-order', false, dirname(plugin_basename(__FILE__)) . '/languages'); 51 } 52 53 54 function ccpo_custom_taxonomy_ordering($query) 55 { 54 56 if (is_admin() || !$query->is_main_query() || is_category()) { 55 57 return; 56 58 } 57 59 58 60 // Check if this is a taxonomy archive for your custom taxonomy 59 61 $term = get_queried_object(); 60 if ($term) {62 if ($term) { 61 63 $term_id = $term->term_id; 62 63 64 65 64 66 $option_name = 'ccpo_category_ordering_' . sanitize_key($term_id); 65 67 $ordering_enabled = get_option($option_name) ? true : false; … … 73 75 74 76 // Attach clause filter 75 add_filter('posts_clauses', array($this,'ccpo_custom_posts_clauses_filter') , 10, 2); 76 } 77 78 } 79 80 function ccpo_custom_posts_clauses_filter($clauses, $query) { 77 add_filter('posts_clauses', array($this, 'ccpo_custom_posts_clauses_filter'), 10, 2); 78 } 79 80 } 81 82 function ccpo_custom_posts_clauses_filter($clauses, $query) 83 { 81 84 global $wpdb; 82 85 … … 93 96 ON {$wpdb->posts}.ID = ccpo_rel.post_id 94 97 AND ccpo_rel.category_id = " . intval($term_id); 95 //. " AND ccpo_rel.incl = 1";98 //. " AND ccpo_rel.incl = 1"; 96 99 97 100 $clauses['orderby'] = "ccpo_rel.weight ASC"; … … 101 104 102 105 103 function ccpo_custom_category_ordering($query) { 106 function ccpo_custom_category_ordering($query) 107 { 104 108 if (is_admin() || !$query->is_main_query() || !is_category()) { 105 109 return; … … 108 112 $category = get_queried_object(); 109 113 $term_id = $category->term_id; 110 if ($term_id) {111 114 if ($term_id) { 115 112 116 $option_name = 'ccpo_category_ordering_' . sanitize_key($term_id); 113 117 114 118 $ordering_enabled = get_option($option_name) ? true : false; 115 116 119 120 117 121 118 122 if (!$ordering_enabled) { 119 123 return; // Custom ordering not enabled for this category 120 124 } 121 125 122 126 123 127 // Store category ID to use later in SQL filters 124 128 $query->set('ccpo_custom_category_id', $term_id); 125 129 126 130 // Set orderby to none to avoid default ordering 127 131 $query->set('orderby', 'none'); 128 132 129 133 // Add custom SQL clauses 130 add_filter('posts_clauses', array($this,'ccpo_posts_clauses_filter'), 10, 2); 131 } 132 } 133 134 function ccpo_posts_clauses_filter($clauses, $query) { 134 add_filter('posts_clauses', array($this, 'ccpo_posts_clauses_filter'), 10, 2); 135 } 136 } 137 138 function ccpo_posts_clauses_filter($clauses, $query) 139 { 135 140 global $wpdb; 136 141 … … 148 153 ON {$wpdb->posts}.ID = ccpo_rel.post_id 149 154 AND ccpo_rel.category_id = " . intval($category_id); 150 //. " AND ccpo_rel.incl = 1";155 //. " AND ccpo_rel.incl = 1"; 151 156 152 157 // Order by weight … … 157 162 158 163 159 160 161 public function ajax_get_terms() { 164 165 166 public function ajax_get_terms() 167 { 162 168 if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'ccpo_get_terms')) { 163 169 wp_send_json_error('Invalid nonce'); 164 170 } 165 if (!current_user_can( 'ccpo_sort_posts')) {166 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'custom-category-post-order' ));171 if (!current_user_can('ccpo_sort_posts')) { 172 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'custom-category-post-order')); 167 173 } 168 174 $taxonomy = sanitize_text_field($_POST['taxonomy'] ?? ''); … … 186 192 $data[] = [ 187 193 'term_id' => $term->term_id, 188 'name' => $term->name194 'name' => $term->name 189 195 ]; 190 196 } 191 197 192 198 wp_send_json_success($data); 193 199 } 194 200 195 public function ajax_load_posts() { 201 public function ajax_load_posts() 202 { 196 203 if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'ccpo_load_posts')) { 197 204 wp_send_json_error('Invalid nonce'); 198 205 } 199 206 200 if (!current_user_can( 'ccpo_sort_posts')) {201 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'custom-category-post-order' ));207 if (!current_user_can('ccpo_sort_posts')) { 208 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'custom-category-post-order')); 202 209 } 203 210 204 211 $post_type = sanitize_text_field($_POST['post_type'] ?? ''); 205 $taxonomy = sanitize_text_field($_POST['taxonomy'] ?? '');206 $term_id = sanitize_text_field($_POST['term'] ?? '');212 $taxonomy = sanitize_text_field($_POST['taxonomy'] ?? ''); 213 $term_id = sanitize_text_field($_POST['term'] ?? ''); 207 214 208 215 // Special case: Home page 209 216 210 217 $option_name = 'ccpo_category_ordering_' . sanitize_key($term_id); 211 212 218 219 213 220 $ordering_enabled = get_option($option_name) ? true : false; 214 221 … … 227 234 228 235 229 236 230 237 231 238 $ordered_ids = wp_list_pluck($order_result, 'post_id'); … … 241 248 if (!empty($ordered_ids)) { 242 249 $ordered_query_args = [ 243 'post_type' => $post_type,244 'post__in' => $ordered_ids,245 'orderby' => 'post__in',250 'post_type' => $post_type, 251 'post__in' => $ordered_ids, 252 'orderby' => 'post__in', 246 253 'posts_per_page' => -1, 247 'post_status' => 'publish'254 'post_status' => 'publish' 248 255 ]; 249 256 … … 263 270 // Query 2: Remaining posts not in order 264 271 $remaining_query_args = [ 265 'post_type' => $post_type,272 'post_type' => $post_type, 266 273 'posts_per_page' => -1, 267 'post_status' => 'publish',268 'post__not_in' => $ordered_ids,269 'orderby' => 'title',270 'order' => 'ASC'274 'post_status' => 'publish', 275 'post__not_in' => $ordered_ids, 276 'orderby' => 'title', 277 'order' => 'ASC' 271 278 ]; 272 279 273 280 // Only apply taxonomy filter if NOT home 274 275 $remaining_query_args['tax_query'] = [[ 276 'taxonomy' => $taxonomy, 277 'field' => 'term_id', 278 'terms' => [$term_id], 279 ]]; 280 281 281 282 $remaining_query_args['tax_query'] = [ 283 [ 284 'taxonomy' => $taxonomy, 285 'field' => 'term_id', 286 'terms' => [$term_id], 287 ] 288 ]; 289 290 282 291 $remaining_query = new WP_Query($remaining_query_args); 283 292 … … 297 306 } 298 307 299 public function ajax_get_taxonomies() { 308 public function ajax_get_taxonomies() 309 { 300 310 if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'ccpo_get_taxonomies')) { 301 311 wp_send_json_error('Invalid nonce'); 302 312 } 303 if (!current_user_can( 'ccpo_sort_posts')) {304 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'custom-category-post-order' ));313 if (!current_user_can('ccpo_sort_posts')) { 314 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'custom-category-post-order')); 305 315 } 306 316 … … 318 328 if ($taxonomy && $taxonomy->public) { 319 329 $data[] = [ 320 'name' => $taxonomy->name,330 'name' => $taxonomy->name, 321 331 'label' => $taxonomy->labels->singular_name 322 332 ]; … … 327 337 if ($taxonomy->public) { 328 338 $data[] = [ 329 'name' => $taxonomy->name,339 'name' => $taxonomy->name, 330 340 'label' => $taxonomy->labels->singular_name 331 341 ]; … … 337 347 } 338 348 339 function ccpo_enqueue_admin_scripts($hook) { 340 if ($hook !== 'toplevel_page_ccpo') return; // Adjust based on your actual page slug 341 349 function ccpo_enqueue_admin_scripts($hook) 350 { 351 if ($hook !== 'toplevel_page_ccpo') 352 return; // Adjust based on your actual page slug 353 342 354 wp_enqueue_script( 343 355 'ccpo-admin-script', … … 354 366 '1.0.0' // Version 355 367 ); 356 368 357 369 wp_localize_script('ccpo-admin-script', 'ccpo_ajax_object', array( 358 370 'ajax_url' => admin_url('admin-ajax.php'), 359 'nonces' => array(371 'nonces' => array( 360 372 'user_ordering' => wp_create_nonce('ccpo_user_ordering_nonce'), 361 'build_order' => wp_create_nonce('ccpo_build_order_nonce'),373 'build_order' => wp_create_nonce('ccpo_build_order_nonce'), 362 374 'get_taxonomies' => wp_create_nonce('ccpo_get_taxonomies'), 363 'get_terms' => wp_create_nonce('ccpo_get_terms'),364 'load_posts' => wp_create_nonce('ccpo_load_posts'),365 'ccpo_sort_nonce' => wp_create_nonce('ccpo_sort_nonce'),366 'ccpo_get_meta_key_nonce' => wp_create_nonce('ccpo_get_meta_key_nonce'),367 'ccpo_meta_key_search_apply' => wp_create_nonce('ccpo_meta_key_search_apply'),368 375 'get_terms' => wp_create_nonce('ccpo_get_terms'), 376 'load_posts' => wp_create_nonce('ccpo_load_posts'), 377 'ccpo_sort_nonce' => wp_create_nonce('ccpo_sort_nonce'), 378 'ccpo_get_meta_key_nonce' => wp_create_nonce('ccpo_get_meta_key_nonce'), 379 'ccpo_meta_key_search_apply' => wp_create_nonce('ccpo_meta_key_search_apply'), 380 369 381 // Add more as needed 370 382 ) … … 377 389 } 378 390 379 function ccpo_menu() { 391 function ccpo_menu() 392 { 380 393 // Get the capability assigned to manage post ordering (defaults to 'administrator') 381 382 394 395 383 396 // Always allow administrators to access full plugin menu 384 if ( current_user_can( 'ccpo_sort_posts' )) {397 if (current_user_can('ccpo_sort_posts')) { 385 398 add_menu_page( 386 __( 'Post Orders', 'custom-category-post-order'),387 __( 'Post Order', 'custom-category-post-order'),399 __('Post Orders', 'custom-category-post-order'), 400 __('Post Order', 'custom-category-post-order'), 388 401 'ccpo_sort_posts', 389 402 'ccpo', … … 394 407 add_submenu_page( 395 408 'ccpo', 396 __( 'Order Permission', 'custom-category-post-order'),397 __( 'Permission', 'custom-category-post-order'),409 __('Order Permission', 'custom-category-post-order'), 410 __('Permission', 'custom-category-post-order'), 398 411 'administrator', 399 412 'subccpo', … … 404 417 405 418 406 function ccpo_admin_right() { 419 function ccpo_admin_right() 420 { 407 421 global $wp_roles; 408 422 409 423 // ✅ Only users with permission to manage the plugin 410 if ( ! current_user_can( 'ccpo_sort_posts' )) {411 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'custom-category-post-order' ));424 if (!current_user_can('ccpo_sort_posts')) { 425 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'custom-category-post-order')); 412 426 } 413 427 … … 417 431 if ( 418 432 $_SERVER['REQUEST_METHOD'] === 'POST' && 419 isset( $_POST['_wpnonce']) &&420 wp_verify_nonce( $_POST['_wpnonce'], 'update-options')433 isset($_POST['_wpnonce']) && 434 wp_verify_nonce($_POST['_wpnonce'], 'update-options') 421 435 ) { 422 $submitted_roles = isset( $_POST['roles']) ? (array) $_POST['roles'] : [];423 $submitted_roles = array_map( 'sanitize_text_field', $submitted_roles);436 $submitted_roles = isset($_POST['roles']) ? (array) $_POST['roles'] : []; 437 $submitted_roles = array_map('sanitize_text_field', $submitted_roles); 424 438 425 439 $all_roles = $wp_roles->get_names(); 426 $valid_roles = array_keys( $all_roles);427 428 $selected_roles = array_intersect( $submitted_roles, $valid_roles);440 $valid_roles = array_keys($all_roles); 441 442 $selected_roles = array_intersect($submitted_roles, $valid_roles); 429 443 430 444 // Update capability for selected roles 431 foreach ( $valid_roles as $role_key) {432 $role_obj = get_role( $role_key);433 if ( ! $role_obj) {445 foreach ($valid_roles as $role_key) { 446 $role_obj = get_role($role_key); 447 if (!$role_obj) { 434 448 continue; 435 449 } 436 450 437 if ( in_array( $role_key, $selected_roles, true )) {438 $role_obj->add_cap( 'ccpo_sort_posts');451 if (in_array($role_key, $selected_roles, true)) { 452 $role_obj->add_cap('ccpo_sort_posts'); 439 453 } else { 440 $role_obj->remove_cap( 'ccpo_sort_posts');454 $role_obj->remove_cap('ccpo_sort_posts'); 441 455 } 442 456 } 443 457 444 458 // Save selected roles in an option (optional) 445 update_option( 'ccpo_order_managers', $selected_roles);446 447 $message = esc_html__( 'Roles updated successfully.', 'custom-category-post-order');448 } 449 450 $current_roles = (array) get_option( 'ccpo_order_managers', [ 'administrator' ]);451 $roles = $wp_roles->get_names();459 update_option('ccpo_order_managers', $selected_roles); 460 461 $message = esc_html__('Roles updated successfully.', 'custom-category-post-order'); 462 } 463 464 $current_roles = (array) get_option('ccpo_order_managers', ['administrator']); 465 $roles = $wp_roles->get_names(); 452 466 453 467 // ✅ Build checkboxes 454 468 $checkboxes = ''; 455 foreach ( $roles as $key => $label) {456 $checked = in_array( $key, $current_roles, true) ? 'checked' : '';457 $checkboxes .= '<label><input type="checkbox" name="roles[]" value="' . esc_attr( $key ) . '" ' . $checked . '> ' . esc_html( $label) . '</label><br>';469 foreach ($roles as $key => $label) { 470 $checked = in_array($key, $current_roles, true) ? 'checked' : ''; 471 $checkboxes .= '<label><input type="checkbox" name="roles[]" value="' . esc_attr($key) . '" ' . $checked . '> ' . esc_html($label) . '</label><br>'; 458 472 } 459 473 460 474 // ✅ Output UI 461 475 echo '<div class="wrap">'; 462 echo '<h2>' . esc_html__( 'Who can arrange the post', 'custom-category-post-order') . '</h2>';463 464 if ( ! empty( $message )) {465 echo '<div class="notice notice-success is-dismissible"><p>' . esc_html( $message) . '</p></div>';476 echo '<h2>' . esc_html__('Who can arrange the post', 'custom-category-post-order') . '</h2>'; 477 478 if (!empty($message)) { 479 echo '<div class="notice notice-success is-dismissible"><p>' . esc_html($message) . '</p></div>'; 466 480 } 467 481 468 482 echo '<form method="post">'; 469 wp_nonce_field( 'update-options');483 wp_nonce_field('update-options'); 470 484 471 485 echo '<table class="form-table"> 472 486 <tr valign="top"> 473 <th scope="row">' . esc_html__( 'Select Roles:', 'custom-category-post-order') . '</th>487 <th scope="row">' . esc_html__('Select Roles:', 'custom-category-post-order') . '</th> 474 488 <td>' . $checkboxes . '</td> 475 489 </tr> 476 490 <tr valign="top"> 477 491 <td colspan="2"> 478 <input type="submit" class="button-primary" value="' . esc_attr__( 'Submit', 'custom-category-post-order') . '" />492 <input type="submit" class="button-primary" value="' . esc_attr__('Submit', 'custom-category-post-order') . '" /> 479 493 </td> 480 494 </tr> … … 484 498 } 485 499 486 function ccpo_get_post_type() { 500 function ccpo_get_post_type() 501 { 487 502 $cache_key = 'ccpo_post_types_with_taxonomies'; 488 503 $post_types = wp_cache_get($cache_key, 'custom-category-post-order'); … … 519 534 } 520 535 521 function check_order_table(int $post_id, string $category_id): int { 536 function check_order_table(int $post_id, string $category_id): int 537 { 522 538 global $wpdb; 523 539 … … 535 551 536 552 537 function process_post() { 553 function process_post() 554 { 538 555 // Enqueue jQuery UI Sortable (no need to define the path manually) 539 wp_localize_script('custom-post-order', 'ccpo_ajax_object', array( 540 'ajax_url' => admin_url('admin-ajax.php'), 541 'nonce' => wp_create_nonce('ccpo_rmppost_nonce') 542 )); 543 544 wp_enqueue_script('jquery-ui-sortable'); 545 } 546 547 548 549 550 function build_order_callback() { 556 wp_localize_script('custom-post-order', 'ccpo_ajax_object', array( 557 'ajax_url' => admin_url('admin-ajax.php'), 558 'nonce' => wp_create_nonce('ccpo_rmppost_nonce') 559 )); 560 561 wp_enqueue_script('jquery-ui-sortable'); 562 } 563 564 565 566 567 function build_order_callback() 568 { 551 569 global $wpdb; 552 if ( ! isset($_POST['nonce']) || ! wp_verify_nonce($_POST['nonce'], 'ccpo_build_order_nonce')) {570 if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'ccpo_build_order_nonce')) { 553 571 wp_send_json_error('Invalid nonce'); 554 572 } 555 if (!current_user_can( 'ccpo_sort_posts')) {556 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'custom-category-post-order' ));573 if (!current_user_can('ccpo_sort_posts')) { 574 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'custom-category-post-order')); 557 575 } 558 576 … … 566 584 567 585 $total = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table WHERE category_id = %s", $category)); 568 586 569 587 if ($total == 0) { 570 588 $values = []; … … 577 595 } 578 596 } 579 597 580 598 if (!empty($values)) { 581 599 $sql = "INSERT INTO $table (category_id, post_id, weight) VALUES " . implode(',', $values); … … 589 607 $wpdb->query($wpdb->prepare( 590 608 "UPDATE $table SET weight = %d WHERE post_id = %d AND category_id = %s", 591 $weight, $post_id, $category 609 $weight, 610 $post_id, 611 $category 592 612 )); 593 613 $exists = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table WHERE post_id = %d AND category_id = %s", $post_id, $category)); … … 624 644 625 645 626 627 628 function user_ordering() { 646 647 648 function user_ordering() 649 { 629 650 global $wpdb; 630 651 631 652 // Verify nonce (security field must be named 'nonce' in JS) 632 653 check_ajax_referer('ccpo_user_ordering_nonce', 'nonce'); 633 if (!current_user_can( 'ccpo_sort_posts')) {634 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'custom-category-post-order' ));654 if (!current_user_can('ccpo_sort_posts')) { 655 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'custom-category-post-order')); 635 656 } 636 657 // Allow category to be either string or integer (e.g., post type or category ID) … … 652 673 653 674 654 function ccpo_update_post_order($post_id) { 675 function ccpo_update_post_order($post_id) 676 { 655 677 global $wpdb; 656 657 if (!current_user_can( 'ccpo_sort_posts' )) { 658 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'custom-category-post-order' ) ); 659 } 660 661 if (!wp_is_post_revision($post_id)) { 662 $cats = get_the_category($post_id); 663 foreach ($cats as $cat) { 664 $cat_id = intval($cat->term_id); 665 $total = $wpdb->get_var($wpdb->prepare( 666 "SELECT COUNT(*) FROM {$wpdb->prefix}ccpo_post_order_rel WHERE category_id = %d AND post_id = %d", 667 $cat_id, $post_id 668 )); 669 670 if ($total == 0 && $post_id > 0) { 671 $wpdb->query($wpdb->prepare( 672 "INSERT INTO {$wpdb->prefix}ccpo_post_order_rel (category_id, post_id) VALUES (%d, %d)", 673 $cat_id, $post_id 678 if (!current_user_can('ccpo_sort_posts')) { 679 680 if (!wp_is_post_revision($post_id)) { 681 $cats = get_the_category($post_id); 682 foreach ($cats as $cat) { 683 $cat_id = intval($cat->term_id); 684 $total = $wpdb->get_var($wpdb->prepare( 685 "SELECT COUNT(*) FROM {$wpdb->prefix}ccpo_post_order_rel WHERE category_id = %d AND post_id = %d", 686 $cat_id, 687 $post_id 674 688 )); 689 690 if ($total == 0 && $post_id > 0) { 691 $wpdb->query($wpdb->prepare( 692 "INSERT INTO {$wpdb->prefix}ccpo_post_order_rel (category_id, post_id) VALUES (%d, %d)", 693 $cat_id, 694 $post_id 695 )); 696 } 675 697 } 676 698 } 677 } 678 } 679 680 681 682 683 function ccpo_install() { 699 700 } 701 } 702 703 704 705 706 function ccpo_install() 707 { 684 708 global $wpdb; 685 709 global $ccpo_db_version; … … 706 730 707 731 708 function ccpo_add_capability() { 732 function ccpo_add_capability() 733 { 709 734 // Add capability to Administrator 710 $admin = get_role( 'administrator');711 if ( $admin && !$admin->has_cap( 'ccpo_sort_posts' )) {712 $admin->add_cap( 'ccpo_sort_posts');735 $admin = get_role('administrator'); 736 if ($admin && !$admin->has_cap('ccpo_sort_posts')) { 737 $admin->add_cap('ccpo_sort_posts'); 713 738 } 714 739 715 740 // Optionally add to Editor too 716 $editor = get_role( 'editor' ); 717 if ( $editor && !$editor->has_cap( 'ccpo_sort_posts' ) ) { 718 $editor->add_cap( 'ccpo_sort_posts' ); 719 } 720 } 721 722 723 function ccpo_remove_capability() { 724 $admin = get_role( 'administrator' ); 725 if ( $admin ) { 726 $admin->remove_cap( 'ccpo_sort_posts' ); 727 } 728 729 $editor = get_role( 'editor' ); 730 if ( $editor ) { 731 $editor->remove_cap( 'ccpo_sort_posts' ); 732 } 733 } 734 735 function ccpo_uninstall() { 741 $editor = get_role('editor'); 742 if ($editor && !$editor->has_cap('ccpo_sort_posts')) { 743 $editor->add_cap('ccpo_sort_posts'); 744 } 745 } 746 747 748 function ccpo_remove_capability() 749 { 750 $admin = get_role('administrator'); 751 if ($admin) { 752 $admin->remove_cap('ccpo_sort_posts'); 753 } 754 755 $editor = get_role('editor'); 756 if ($editor) { 757 $editor->remove_cap('ccpo_sort_posts'); 758 } 759 } 760 761 function ccpo_uninstall() 762 { 736 763 global $wpdb; 737 764 … … 751 778 752 779 753 //new funciton 754 755 public function post_order_category() { 756 if (!current_user_can( 'ccpo_sort_posts' )) { 757 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'custom-category-post-order' ) ); 780 //new funciton 781 782 public function post_order_category() 783 { 784 if (!current_user_can('ccpo_sort_posts')) { 785 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'custom-category-post-order')); 758 786 } 759 787 $term = $this->sanitize_category_input(); 760 788 // $categories = $this->get_all_categories(); 761 789 // $post_types = $this->ccpo_get_post_type(); 762 790 763 791 $post_types_options = $this->generate_post_type_options(); 764 792 … … 766 794 767 795 $order_data = $this->get_post_order_data($term); 768 796 769 797 $checked = get_option("ccpo_category_ordering_" . $term); 770 798 771 772 echo $this->render_admin_page($post_types_options, $term, $order_data, $checked); 773 } 774 775 private function render_admin_page($post_types_options, $term, $order_data, $checked) { 799 800 echo $this->render_admin_page($post_types_options, $term, $order_data, $checked); 801 } 802 803 private function render_admin_page($post_types_options, $term, $order_data, $checked) 804 { 776 805 ob_start(); 777 806 include plugin_dir_path(__FILE__) . 'admin-post-order-page.php'; … … 779 808 } 780 809 781 private function sanitize_category_input() { 810 private function sanitize_category_input() 811 { 782 812 return isset($_POST['term']) ? sanitize_text_field($_POST['term']) : ''; 783 813 } 784 814 785 private function get_all_categories() { 815 private function get_all_categories() 816 { 786 817 return get_categories([ 787 818 'type' => 'post', … … 796 827 } 797 828 798 799 private function generate_category_posttype_options($categories, $selected_category) { 829 830 private function generate_category_posttype_options($categories, $selected_category) 831 { 800 832 $options = ['<option value="" selected>' . esc_html__('Select Category / Post Type', 'custom-category-post-order') . '</option>']; 801 833 … … 832 864 } 833 865 834 private function generate_post_type_options($selected_post_type = '') { 866 private function generate_post_type_options($selected_post_type = '') 867 { 835 868 $options = ['<option value="" selected>' . esc_html__('Select Post Type', 'custom-category-post-order') . '</option>']; 836 869 … … 842 875 esc_html__('Home Page (Pro)', 'custom-category-post-order') 843 876 ); 844 877 845 878 $all_post_types = get_post_types(['public' => true], 'objects'); 846 879 … … 891 924 892 925 893 private function get_post_order_data($term) { 926 private function get_post_order_data($term) 927 { 894 928 global $wpdb; 895 929 … … 969 1003 } 970 1004 971 public function ajax_ccpo_get_meta_keys() { 972 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'ccpo_get_meta_key_nonce' ) ) { 973 wp_send_json_error( 'Bad nonce' ); 974 } 975 976 if (!current_user_can( 'ccpo_sort_posts' )) { 977 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'custom-category-post-order' ) ); 978 } 979 980 $post_type = sanitize_text_field( $_POST['post_type'] ?? 'post' ); 1005 public function ajax_ccpo_get_meta_keys() 1006 { 1007 if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'ccpo_get_meta_key_nonce')) { 1008 wp_send_json_error('Bad nonce'); 1009 } 1010 1011 if (!current_user_can('ccpo_sort_posts')) { 1012 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'custom-category-post-order')); 1013 } 1014 1015 $post_type = sanitize_text_field($_POST['post_type'] ?? 'post'); 981 1016 982 1017 global $wpdb; 983 1018 // Pull distinct keys (limit to 100 to keep it light) 984 $keys = $wpdb->get_col( $wpdb->prepare(1019 $keys = $wpdb->get_col($wpdb->prepare( 985 1020 "SELECT DISTINCT pm.meta_key 986 1021 FROM {$wpdb->postmeta} pm … … 993 1028 )); 994 1029 995 wp_send_json_success( [ 'keys' => $keys ]);1030 wp_send_json_success(['keys' => $keys]); 996 1031 } 997 1032
Note: See TracChangeset
for help on using the changeset viewer.