<?php
/*
Plugin Name: April Fools Admin Shuffle
Description: Adds a button to the top of each WordPress admin page that shuffles the labels of links and buttons when clicked as an April Fools joke.
Version: 1.0.0
Author: CodeWP Assistant <info@codewp.ai>
Author URI: https://codewp.ai
Text Domain: codewp
*/

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Enqueue our custom scripts
function cwpai_enqueue_admin_scripts() {
    wp_enqueue_script('cwpai-admin-shuffle', plugin_dir_url(__FILE__) . 'js/cwpai-admin-shuffle.js', array('jquery'), '1.0.0', true);
}
add_action('admin_enqueue_scripts', 'cwpai_enqueue_admin_scripts');

// Add the shuffle button to the admin bar
function cwpai_add_shuffle_button($wp_admin_bar) {
    $args = array(
        'id'    => 'cwpai-shuffle-button',
        'title' => __('Shuffle Labels', 'codewp'),
        'href'  => '#',
        'meta'  => array(
            'class' => 'cwpai-shuffle-button',
            'title' => __('Click to shuffle labels', 'codewp')
        )
    );
    $wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'cwpai_add_shuffle_button', 100);

// JavaScript to handle the shuffling
function cwpai_enqueue_custom_js() {
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $('.cwpai-shuffle-button').on('click', function(e) {
                e.preventDefault();
                var labels = [];
                $('a, button').each(function() {
                    labels.push($(this).text());
                });
                labels = labels.sort(function() { return 0.5 - Math.random(); });
                $('a, button').each(function(index) {
                    $(this).text(labels[index]);
                });
            });
        });
    </script>
    <?php
}
add_action('admin_footer', 'cwpai_enqueue_custom_js');
