星期三 , 22 1 月 2025

两个插件代码对比

<?php
/*
Plugin Name: Category Reorder
Description: Enables drag-and-drop reordering of categories in the backend.
*/

// Register and enqueue scripts
function my_enqueue_scripts() {
    wp_enqueue_script('jquery-ui-sortable');
    wp_enqueue_script('my-category-reorder', plugin_dir_url(__FILE__) . 'category-reorder.js', array('jquery-ui-sortable'), false, true);
    wp_localize_script('my-category-reorder', 'myAjax', array(
        'ajaxurl' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('my-category-reorder-nonce')
    ));
}
add_action('admin_enqueue_scripts', 'my_enqueue_scripts');

// Custom function to update term order based on term_group
function wp_update_term_order($term_id, $taxonomy, $new_order) {
    global $wpdb;

    // Get current term_group (if any)
    $current_group = $wpdb->get_var($wpdb->prepare("SELECT term_group FROM $wpdb->terms WHERE term_id = %d", $term_id));

    // Determine new term_group based on the new order
    $new_group = 0; // Default starting group
    foreach ($new_order as $index => $term_id) {
        if ($index !== 0 && $term_id === $current_term_id) { // Reached the current term
            $new_group++;
        }
    }

    // Update term_group for the target term
    $wpdb->update($wpdb->terms, array('term_group' => $new_group), array('term_id' => $term_id));
}

// Handle AJAX call to update category order
add_action('wp_ajax_update_category_order', 'update_category_order');
function update_category_order() {
    if (check_ajax_referer('my-category-reorder-nonce', 'security')) {
        $new_order = (array) $_POST['new_order']; // Sanitize input

        foreach ($new_order as $term_id => $order) {
            wp_update_term_order($term_id, 'category', $new_order); // Update term_group
        }

        // Retrieve and sort categories based on updated term_group
        $categories = $wpdb->get_results($wpdb->prepare("SELECT t.*, tt.name AS taxonomy_name FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s ORDER BY term_group ASC, name ASC", 'category'));

        // Apply reordering logic or display categories in the desired order using $categories

        wp_send_json_success();
    } else {
        wp_send_json_error();
    }
}
?>

<script>
jQuery(function($) {
    $('#the-list').sortable({
        // Options for sortable behavior (adjust as needed)
    });

    $('#the-list').on('sortupdate', function() {
        var new_order = $(this).sortable('toArray');

        $.ajax({
            url: myAjax.ajaxurl,
            type: 'POST',
            data: {
                action: 'update_category_order',
                new_order: new_order,
                nonce: myAjax.nonce
            },
            success: function(response) {
                console.log('Category order updated successfully!');
            }
        });
    });
});
</script>

版本二

<?php
/*
Plugin Name: Category Reorder
Description: Enables drag-and-drop reordering of categories in the backend.
*/

// Register and enqueue scripts
function my_enqueue_scripts() {
    wp_enqueue_script('jquery-ui-sortable');
    wp_enqueue_script('my-category-reorder', plugin_dir_url(__FILE__) . 'category-reorder.js', array('jquery-ui-sortable'), false, true);
    wp_localize_script('my-category-reorder', 'myAjax', array(
        'ajaxurl' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('my-category-reorder-nonce')
    ));
}
add_action('admin_enqueue_scripts', 'my_enqueue_scripts');

// Custom function to update term order based on term_group
function wp_update_term_order($term_id, $taxonomy, $new_order) {
    global $wpdb;

    // Get current term_group (if any)
    $current_group = $wpdb->get_var($wpdb->prepare("SELECT term_group FROM $wpdb->terms WHERE term_id = %d", $term_id));

    // Determine new term_group based on the new order
    $new_group = 0; // Default starting group
    foreach ($new_order as $index => $order) {
        if ($index !== 0 && $order === $term_id) { // Reached the current term
            $new_group++;
        }
    }

    // Update term_group for the target term
    $wpdb->update($wpdb->terms, array('term_group' => $new_group), array('term_id' => $term_id));
}

// Handle AJAX call to update category order
add_action('wp_ajax_update_category_order', 'update_category_order');
function update_category_order() {
    global $wpdb;

    if (check_ajax_referer('my-category-reorder-nonce', 'security')) {
        $new_order = (array) $_POST['new_order']; // Sanitize input

        foreach ($new_order as $term_id => $order) {
            wp_update_term_order($term_id, 'category', $new_order); // Update term_group
        }

        // Retrieve and sort categories based on updated term_group
        $categories = $wpdb->get_results($wpdb->prepare("SELECT t.*, tt.name AS taxonomy_name FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s ORDER BY term_group ASC, name ASC", 'category'));

        // Apply reordering logic or display categories in the desired order using $categories

        wp_send_json_success();
    } else {
        wp_send_json_error();
    }
}
?>

<script>
jQuery(function($) {
    $('#the-list').sortable({
        // Options for sortable behavior (adjust as needed)
    });

    $('#the-list').on('sortupdate', function() {
        var new_order = $(this).sortable('toArray');

        $.ajax({
            url: myAjax.ajaxurl,
            type: 'POST',
            data: {
                action: 'update_category_order',
                new_order: new_order,
                nonce: myAjax.nonce
            },
            success: function(response) {
                console.log('Category order updated successfully!');
            }
        });
    });
});
</script>