Plugin Directory

Changeset 3404972


Ignore:
Timestamp:
11/28/2025 09:30:32 AM (4 months ago)
Author:
faaiq
Message:

Fix minor bugs

Location:
custom-post-order-category/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • custom-post-order-category/trunk/readme.txt

    r3311928 r3404972  
    33Tags: 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.
    44Requires at least: 3.3
    5 Tested up to: 6.8.1
     5Tested up to: 6.8.3
    66Stable tag: trunk
    77License: GPLv2 or later
     
    4343
    4444== Changelog ==
     45= 2.2 =
     46Fixed compatibility issues with WordPress 6.8.3 and improved performance, fix some minor bugs.
     47
    4548= 2.1 =
    4649Added translations for French, German, Italian, Russian, and Polish languages.
  • custom-post-order-category/trunk/wp-customcategorypostorder.php

    r3311928 r3404972  
    44 * Plugin URI: https://scriptut.com/wordpress/custom-category-post-order/
    55 * 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.1
     6 * Version: 2.2
    77 * Author: Faaiq Ahmed
    88 * Author URI: mailto:nfaaiq@gmail.com
     
    3434        add_action('wp_ajax_ccpo_load_posts', [$this, 'ajax_load_posts']);
    3535
    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']);
    3737
    3838        add_action('pre_get_posts', [$this, 'ccpo_custom_taxonomy_ordering']);
    3939        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']);
    4343
    4444        register_activation_hook(__FILE__, array($this, 'ccpo_install'));
     
    4646    }
    4747
    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    {
    5456        if (is_admin() || !$query->is_main_query() || is_category()) {
    5557            return;
    5658        }
    57        
     59
    5860        // Check if this is a taxonomy archive for your custom taxonomy
    5961        $term = get_queried_object();
    60         if($term) {
     62        if ($term) {
    6163            $term_id = $term->term_id;
    62            
    63            
     64
     65
    6466            $option_name = 'ccpo_category_ordering_' . sanitize_key($term_id);
    6567            $ordering_enabled = get_option($option_name) ? true : false;
     
    7375
    7476            // 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    {
    8184        global $wpdb;
    8285
     
    9396            ON {$wpdb->posts}.ID = ccpo_rel.post_id
    9497            AND ccpo_rel.category_id = " . intval($term_id);
    95             //. " AND ccpo_rel.incl = 1";
     98        //. " AND ccpo_rel.incl = 1";
    9699
    97100        $clauses['orderby'] = "ccpo_rel.weight ASC";
     
    101104
    102105
    103     function ccpo_custom_category_ordering($query) {
     106    function ccpo_custom_category_ordering($query)
     107    {
    104108        if (is_admin() || !$query->is_main_query() || !is_category()) {
    105109            return;
     
    108112        $category = get_queried_object();
    109113        $term_id = $category->term_id;
    110         if($term_id) {
    111        
     114        if ($term_id) {
     115
    112116            $option_name = 'ccpo_category_ordering_' . sanitize_key($term_id);
    113            
     117
    114118            $ordering_enabled = get_option($option_name) ? true : false;
    115            
    116            
     119
     120
    117121
    118122            if (!$ordering_enabled) {
    119123                return; // Custom ordering not enabled for this category
    120124            }
    121            
     125
    122126
    123127            // Store category ID to use later in SQL filters
    124128            $query->set('ccpo_custom_category_id', $term_id);
    125            
     129
    126130            // Set orderby to none to avoid default ordering
    127131            $query->set('orderby', 'none');
    128132
    129133            // 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    {
    135140        global $wpdb;
    136141
     
    148153            ON {$wpdb->posts}.ID = ccpo_rel.post_id
    149154            AND ccpo_rel.category_id = " . intval($category_id);
    150             //. " AND ccpo_rel.incl = 1";
     155        //. " AND ccpo_rel.incl = 1";
    151156
    152157        // Order by weight
     
    157162
    158163
    159    
    160    
    161     public function ajax_get_terms() {
     164
     165
     166    public function ajax_get_terms()
     167    {
    162168        if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'ccpo_get_terms')) {
    163169            wp_send_json_error('Invalid nonce');
    164170        }
    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'));
    167173        }
    168174        $taxonomy = sanitize_text_field($_POST['taxonomy'] ?? '');
     
    186192            $data[] = [
    187193                'term_id' => $term->term_id,
    188                 'name'    => $term->name
     194                'name' => $term->name
    189195            ];
    190196        }
    191        
     197
    192198        wp_send_json_success($data);
    193199    }
    194200
    195     public function ajax_load_posts() {
     201    public function ajax_load_posts()
     202    {
    196203        if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'ccpo_load_posts')) {
    197204            wp_send_json_error('Invalid nonce');
    198205        }
    199206
    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'));
    202209        }
    203210
    204211        $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'] ?? '');
    207214
    208215        // Special case: Home page
    209216
    210217        $option_name = 'ccpo_category_ordering_' . sanitize_key($term_id);
    211    
    212        
     218
     219
    213220        $ordering_enabled = get_option($option_name) ? true : false;
    214221
     
    227234
    228235
    229        
     236
    230237
    231238        $ordered_ids = wp_list_pluck($order_result, 'post_id');
     
    241248        if (!empty($ordered_ids)) {
    242249            $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',
    246253                'posts_per_page' => -1,
    247                 'post_status'    => 'publish'
     254                'post_status' => 'publish'
    248255            ];
    249256
     
    263270        // Query 2: Remaining posts not in order
    264271        $remaining_query_args = [
    265             'post_type'      => $post_type,
     272            'post_type' => $post_type,
    266273            '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'
    271278        ];
    272279
    273280        // 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
    282291        $remaining_query = new WP_Query($remaining_query_args);
    283292
     
    297306    }
    298307
    299     public function ajax_get_taxonomies() {
     308    public function ajax_get_taxonomies()
     309    {
    300310        if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'ccpo_get_taxonomies')) {
    301311            wp_send_json_error('Invalid nonce');
    302312        }
    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'));
    305315        }
    306316
     
    318328            if ($taxonomy && $taxonomy->public) {
    319329                $data[] = [
    320                     'name'  => $taxonomy->name,
     330                    'name' => $taxonomy->name,
    321331                    'label' => $taxonomy->labels->singular_name
    322332                ];
     
    327337                if ($taxonomy->public) {
    328338                    $data[] = [
    329                         'name'  => $taxonomy->name,
     339                        'name' => $taxonomy->name,
    330340                        'label' => $taxonomy->labels->singular_name
    331341                    ];
     
    337347    }
    338348
    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
    342354        wp_enqueue_script(
    343355            'ccpo-admin-script',
     
    354366            '1.0.0' // Version
    355367        );
    356        
     368
    357369        wp_localize_script('ccpo-admin-script', 'ccpo_ajax_object', array(
    358370            'ajax_url' => admin_url('admin-ajax.php'),
    359             'nonces'   => array(
     371            'nonces' => array(
    360372                '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'),
    362374                '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
    369381                // Add more as needed
    370382            )
     
    377389    }
    378390
    379     function ccpo_menu() {
     391    function ccpo_menu()
     392    {
    380393        // Get the capability assigned to manage post ordering (defaults to 'administrator')
    381        
    382        
     394
     395
    383396        // Always allow administrators to access full plugin menu
    384         if ( current_user_can( 'ccpo_sort_posts' ) ) {
     397        if (current_user_can('ccpo_sort_posts')) {
    385398            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'),
    388401                'ccpo_sort_posts',
    389402                'ccpo',
     
    394407            add_submenu_page(
    395408                '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'),
    398411                'administrator',
    399412                'subccpo',
     
    404417
    405418
    406     function ccpo_admin_right() {
     419    function ccpo_admin_right()
     420    {
    407421        global $wp_roles;
    408422
    409423        // ✅ 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'));
    412426        }
    413427
     
    417431        if (
    418432            $_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')
    421435        ) {
    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);
    424438
    425439            $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);
    429443
    430444            // 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) {
    434448                    continue;
    435449                }
    436450
    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');
    439453                } else {
    440                     $role_obj->remove_cap( 'ccpo_sort_posts' );
     454                    $role_obj->remove_cap('ccpo_sort_posts');
    441455                }
    442456            }
    443457
    444458            // 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();
    452466
    453467        // ✅ Build checkboxes
    454468        $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>';
    458472        }
    459473
    460474        // ✅ Output UI
    461475        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>';
    466480        }
    467481
    468482        echo '<form method="post">';
    469         wp_nonce_field( 'update-options' );
     483        wp_nonce_field('update-options');
    470484
    471485        echo '<table class="form-table">
    472486            <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>
    474488                <td>' . $checkboxes . '</td>
    475489            </tr>
    476490            <tr valign="top">
    477491                <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') . '" />
    479493                </td>
    480494            </tr>
     
    484498    }
    485499
    486     function ccpo_get_post_type() {
     500    function ccpo_get_post_type()
     501    {
    487502        $cache_key = 'ccpo_post_types_with_taxonomies';
    488503        $post_types = wp_cache_get($cache_key, 'custom-category-post-order');
     
    519534    }
    520535
    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    {
    522538        global $wpdb;
    523539
     
    535551
    536552
    537     function process_post() {
     553    function process_post()
     554    {
    538555        // 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    {
    551569        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')) {
    553571            wp_send_json_error('Invalid nonce');
    554572        }
    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'));
    557575        }
    558576
     
    566584
    567585        $total = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table WHERE category_id = %s", $category));
    568        
     586
    569587        if ($total == 0) {
    570588            $values = [];
     
    577595                }
    578596            }
    579            
     597
    580598            if (!empty($values)) {
    581599                $sql = "INSERT INTO $table (category_id, post_id, weight) VALUES " . implode(',', $values);
     
    589607                    $wpdb->query($wpdb->prepare(
    590608                        "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
    592612                    ));
    593613                    $exists = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table WHERE post_id = %d AND category_id = %s", $post_id, $category));
     
    624644
    625645
    626    
    627 
    628     function user_ordering() {
     646
     647
     648    function user_ordering()
     649    {
    629650        global $wpdb;
    630651
    631652        // Verify nonce (security field must be named 'nonce' in JS)
    632653        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'));
    635656        }
    636657        // Allow category to be either string or integer (e.g., post type or category ID)
     
    652673
    653674
    654     function ccpo_update_post_order($post_id) {
     675    function ccpo_update_post_order($post_id)
     676    {
    655677        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
    674688                    ));
     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                    }
    675697                }
    676698            }
    677         }
    678     }
    679 
    680 
    681 
    682 
    683     function ccpo_install() {
     699
     700        }
     701    }
     702
     703
     704
     705
     706    function ccpo_install()
     707    {
    684708        global $wpdb;
    685709        global $ccpo_db_version;
     
    706730
    707731
    708     function ccpo_add_capability() {
     732    function ccpo_add_capability()
     733    {
    709734        // 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');
    713738        }
    714739
    715740        // 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    {
    736763        global $wpdb;
    737764
     
    751778
    752779
    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'));
    758786        }
    759787        $term = $this->sanitize_category_input();
    760788        // $categories = $this->get_all_categories();
    761789        // $post_types = $this->ccpo_get_post_type();
    762        
     790
    763791        $post_types_options = $this->generate_post_type_options();
    764792
     
    766794
    767795        $order_data = $this->get_post_order_data($term);
    768        
     796
    769797        $checked = get_option("ccpo_category_ordering_" . $term);
    770798
    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    {
    776805        ob_start();
    777806        include plugin_dir_path(__FILE__) . 'admin-post-order-page.php';
     
    779808    }
    780809
    781     private function sanitize_category_input() {
     810    private function sanitize_category_input()
     811    {
    782812        return isset($_POST['term']) ? sanitize_text_field($_POST['term']) : '';
    783813    }
    784814
    785     private function get_all_categories() {
     815    private function get_all_categories()
     816    {
    786817        return get_categories([
    787818            'type' => 'post',
     
    796827    }
    797828
    798    
    799     private function generate_category_posttype_options($categories, $selected_category) {
     829
     830    private function generate_category_posttype_options($categories, $selected_category)
     831    {
    800832        $options = ['<option value="" selected>' . esc_html__('Select Category / Post Type', 'custom-category-post-order') . '</option>'];
    801833
     
    832864    }
    833865
    834     private function generate_post_type_options($selected_post_type = '') {
     866    private function generate_post_type_options($selected_post_type = '')
     867    {
    835868        $options = ['<option value="" selected>' . esc_html__('Select Post Type', 'custom-category-post-order') . '</option>'];
    836869
     
    842875            esc_html__('Home Page (Pro)', 'custom-category-post-order')
    843876        );
    844        
     877
    845878        $all_post_types = get_post_types(['public' => true], 'objects');
    846879
     
    891924
    892925
    893     private function get_post_order_data($term) {
     926    private function get_post_order_data($term)
     927    {
    894928        global $wpdb;
    895929
     
    9691003    }
    9701004
    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');
    9811016
    9821017        global $wpdb;
    9831018        // Pull distinct keys (limit to 100 to keep it light)
    984         $keys = $wpdb->get_col( $wpdb->prepare(
     1019        $keys = $wpdb->get_col($wpdb->prepare(
    9851020            "SELECT DISTINCT pm.meta_key
    9861021            FROM {$wpdb->postmeta} pm
     
    9931028        ));
    9941029
    995         wp_send_json_success( [ 'keys' => $keys ] );
     1030        wp_send_json_success(['keys' => $keys]);
    9961031    }
    9971032
Note: See TracChangeset for help on using the changeset viewer.