Plugin Directory

Changeset 3317252


Ignore:
Timestamp:
06/24/2025 11:00:19 PM (9 months ago)
Author:
ianmjones
Message:

Deploy version 1.5.0

Location:
wp-cron-pixie
Files:
11 added
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • wp-cron-pixie/trunk/README.txt

    r3219061 r3317252  
    1 === Plugin Name ===
     1=== WP Cron Pixie ===
    22Contributors: ianmjones
    33Donate link: https://ianmjones.com/
    44Tags: cron, wp-cron, dashboard, admin, widget
    5 Requires at least: 5.3
    6 Tested up to: 6.7
    7 Stable tag: 1.4.4
    8 Requires PHP: 5.6
    9 License: GPLv2 or later
    10 License URI: http://www.gnu.org/licenses/gpl-2.0.html
     5Requires at least: 6.0
     6Tested up to: 6.8
     7Stable tag: 1.5.0
     8Requires PHP: 7.4
     9License: MIT
     10License URI: https://mit-license.org/
    1111
    1212A little dashboard widget to view the WordPress cron.
     
    22221. [Building Reactive WordPress Plugins – Part 3 – Elm](https://deliciousbrains.com/building-reactive-wordpress-plugins-part-3-elm/)
    2323
     24The current version is using Gleam on the front end.
     25
    2426== Installation ==
    2527
     
    3133= From WordPress.org =
    32341. Download WP Cron Pixie.
    33 1. Upload the 'wp-cron-pixie' directory to your '/wp-content/plugins/' directory, using your favorite method (ftp, sftp, scp, etc...)
     351. Upload the 'wp-cron-pixie' directory to your '/wp-content/plugins/' directory, using your favourite method (ftp, sftp, scp, etc...)
    34361. Activate WP Cron Pixie from your Plugins page.
    3537
     
    4547
    4648== Changelog ==
     49
     50= 1.5.0 =
     51* Gleam front end.
     52* Fixed "Error message: Event schedule does not exist." debug.log messages when example events enabled.
     53* Fixed example events not being removed from cron schedules after turning them off.
     54* Tested with WP 6.8
     55* Requires WP 6.0+
     56* Requires PHP 7.4+
    4757
    4858= 1.4.4 =
     
    6777* Added "Refresh" icon for manual refresh of data.
    6878* Fixed not all strings in UI being translatable.
    69 * Elm 0.19 frontend.
     79* Elm 0.19 front end.
    7080
    7181= 1.3.1 =
     
    7484
    7585= 1.3 =
    76 * Elm 0.18 frontend.
     86* Elm 0.18 front end.
    7787
    7888= 1.2 =
    79 * Elm frontend.
     89* Elm front end.
    8090
    8191= 1.1 =
    82 * Vue.js frontend.
     92* Vue.js front end.
    8393
    8494= 1.0 =
    8595* Initial release.
    86 * Backbone.js frontend.
     96* Backbone.js front end.
  • wp-cron-pixie/trunk/includes/class-cron-pixie.php

    r2808278 r3317252  
    11<?php
    2 
     2/**
     3 * Cron_Pixie class file.
     4 *
     5 * @package Cron_Pixie
     6 */
     7
     8namespace Cron_Pixie;
     9
     10use WP_Error;
     11
     12/**
     13 * Cron_Pixie class.
     14 */
    315class Cron_Pixie {
     16    /**
     17     * The context for our nonce.
     18     *
     19     * @var string
     20     */
     21    const NONCE_CONTEXT = 'cron-pixie';
    422
    523    /**
     
    1331     * Often used plugin info.
    1432     *
    15      * @var array
    16      */
    17     private $plugin_meta;
     33     * @var array<string, string>
     34     */
     35    private array $plugin_meta;
    1836
    1937    /**
    2038     * Cron_Pixie constructor.
    2139     *
    22      * Registers all action and filter hooks if user can use widget.
    23      *
    24      * @param array $plugin_meta
    25      */
    26     public function __construct( $plugin_meta = array() ) {
     40     * Bare minimum to set up cron schedule for example events.
     41     *
     42     * @param array<string, string> $plugin_meta Plugin's meta data.
     43     */
     44    public function __construct( array $plugin_meta = array() ) {
    2745        if ( empty( $plugin_meta ) ) {
    2846            return;
     
    3149        $this->plugin_meta = $plugin_meta;
    3250
     51        // Add a schedule of our own for testing.
     52        add_filter( 'cron_schedules', array( $this, 'filter_cron_schedules' ) );
     53
     54        // Process the "passed" event so that we can reschedule for the past again.
     55        add_action( 'cron_pixie_passed_event', array( $this, 'reschedule_passed_event' ) );
     56    }
     57
     58    /**
     59     * Init the plugin, if being loaded in the right context.
     60     *
     61     * Registers all action and filter hooks if user can use widget.
     62     *
     63     * @return void
     64     */
     65    public function init(): void {
    3366        // Using the plugin in the network admin dashboard makes no sense.
    3467        if ( is_network_admin() ) {
     
    4881
    4982        // AJAX handlers.
    50         add_action( 'wp_ajax_cron_pixie_schedules', array( $this, 'ajax_schedules' ) );
    51         add_action( 'wp_ajax_cron_pixie_events', array( $this, 'ajax_events' ) );
    52         add_action( 'wp_ajax_cron_pixie_example_events', array( $this, 'ajax_example_events' ) );
    53         add_action( 'wp_ajax_cron_pixie_auto_refresh', array( $this, 'ajax_auto_refresh' ) );
    54 
    55         if ( $this->_get_setting( 'example_events' ) ) {
    56             // Add a schedule of our own for testing.
    57             add_filter( 'cron_schedules', array( $this, 'filter_cron_schedules' ) );
    58 
    59             // Add events to our test schedule.
    60             $this->_create_test_events();
    61         } else {
    62             // Remove events from our test schedule.
    63             $this->_remove_test_events();
    64         }
     83        add_action( 'wp_ajax_cron_pixie_get_schedules', array( $this, 'ajax_get_schedules' ) );
     84        add_action( 'wp_ajax_cron_pixie_update_event', array( $this, 'ajax_update_event' ) );
     85        add_action( 'wp_ajax_cron_pixie_update_example_events_setting', array( $this, 'ajax_update_example_events_setting' ) );
     86        add_action( 'wp_ajax_cron_pixie_update_auto_refresh_setting', array( $this, 'ajax_update_auto_refresh_setting' ) );
    6587    }
    6688
     
    6890     * Registers the widget and content callback.
    6991     */
    70     public function add_dashboard_widget() {
    71 
     92    public function add_dashboard_widget(): void {
    7293        wp_add_dashboard_widget(
    7394            $this->plugin_meta['slug'],
     
    80101     * Provides the initial content for the widget.
    81102     */
    82     public function dashboard_widget_content() {
     103    public function dashboard_widget_content(): void {
    83104        ?>
    84105        <!-- Main content -->
     
    90111     * Enqueues the JS scripts when the main dashboard page is loading.
    91112     *
    92      * @param string $hook_page
    93      */
    94     public function enqueue_scripts( $hook_page ) {
     113     * @param string $hook_page Current page hook.
     114     */
     115    public function enqueue_scripts( string $hook_page ): void {
    95116        if ( 'index.php' !== $hook_page ) {
    96117            return;
     
    109130            $script_handle,
    110131            plugin_dir_url( $this->plugin_meta['file'] ) . 'js/main.js',
    111             array(),
    112             $this->plugin_meta['version'],
    113             true // Load JS in footer so that templates in DOM can be referenced.
    114         );
    115 
    116         wp_enqueue_script(
    117             $script_handle . '-build',
    118             plugin_dir_url( $this->plugin_meta['file'] ) . 'js/CronPixie.js',
    119132            array(),
    120133            $this->plugin_meta['version'],
     
    138151                'refresh'                => _x( 'Refresh Now', 'Tooltip for refresh now icon', 'wp-cron-pixie' ),
    139152                'schedules'              => _x( 'Schedules', 'Title for list of schedules', 'wp-cron-pixie' ),
    140                 'example_events'         => _x( 'Example Events', 'Label for Example Events checkbox', 'wp-cron-pixie' ),
    141                 'example_events_tooltip' => _x( 'Include some example events in the cron schedule', 'Tooltip for Example Events checkbox', 'wp-cron-pixie' ),
     153                'example_events'         => _x(
     154                    'Example Events',
     155                    'Label for Example Events checkbox',
     156                    'wp-cron-pixie'
     157                ),
     158                'example_events_tooltip' => _x(
     159                    'Include some example events in the cron schedule',
     160                    'Tooltip for Example Events checkbox',
     161                    'wp-cron-pixie'
     162                ),
    142163                'auto_refresh'           => _x( 'Auto Refresh', 'Label for Auto Refresh checkbox', 'wp-cron-pixie' ),
    143                 'auto_refresh_tooltip'   => _x( 'Refresh the display of cron events every 5 seconds', 'Tooltip for Auto Refresh checkbox', 'wp-cron-pixie' ),
     164                'auto_refresh_tooltip'   => _x(
     165                    'Refresh the display of cron events every 5 seconds',
     166                    'Tooltip for Auto Refresh checkbox',
     167                    'wp-cron-pixie'
     168                ),
    144169            ),
    145170            'admin_url'      => untrailingslashit( admin_url() ),
    146             'nonce'          => wp_create_nonce( 'cron-pixie' ),
     171            'nonce'          => wp_create_nonce( self::NONCE_CONTEXT ),
    147172            'timer_period'   => 5, // How often should display be updated, in seconds.
    148173            'data'           => array(
    149                 'schedules' => $this->_get_schedules(),
     174                'schedules' => $this->get_schedules(),
    150175            ),
    151             'example_events' => (bool) $this->_get_setting( 'example_events' ),
    152             'auto_refresh'   => (bool) $this->_get_setting( 'auto_refresh' ),
     176            'example_events' => (bool) $this->get_setting( 'example_events' ),
     177            'auto_refresh'   => (bool) $this->get_setting( 'auto_refresh' ),
     178            'version'        => $this->plugin_meta['version'],
    153179        );
    154180        wp_localize_script( $script_handle, 'CronPixie', $data );
     
    158184     * Returns list of cron schedules.
    159185     *
    160      * @return array
    161      */
    162     private function _get_schedules() {
     186     * @return array<array<string, mixed>>
     187     */
     188    private function get_schedules(): array {
     189        // If we're getting schedules, widget should be visible.
     190        // Therefore we may or may not want our example events available.
     191        $this->manage_example_events();
     192
    163193        // Get list of schedules.
    164194        $schedules = wp_get_schedules();
     
    179209        foreach ( $cron_array as $timestamp => $jobs ) {
    180210            foreach ( $jobs as $hook => $events ) {
    181                 foreach ( $events as $key => $event ) {
     211                foreach ( $events as $event ) {
    182212                    $event['hook']        = $hook;
    183213                    $event['timestamp']   = $timestamp;
     
    193223
    194224        // We need to change the associative array (map) into an indexed one (set) for easier use in collection.
     225        // And if any schedles are empty, might as well drop them too.
    195226        $set = array();
    196227        foreach ( $schedules as $name => $schedule ) {
     228            if ( empty( $schedule['events'] ) ) {
     229                continue;
     230            }
     231
    197232            $schedule['name'] = $name;
    198233            $set[]            = $schedule;
     
    205240     * Send a response to ajax request, as JSON.
    206241     *
    207      * @param mixed $response
    208      */
    209     private function _ajax_return( $response = true ) {
    210         echo json_encode( $response );
    211         exit;
     242     * @param mixed $response Data to be returned.
     243     */
     244    private function ajax_return( $response = true ): void {
     245        if ( is_wp_error( $response ) ) {
     246            wp_send_json_error( $response );
     247        }
     248
     249        wp_send_json_success( $response );
    212250    }
    213251
    214252    /**
    215253     * Displays a JSON encoded list of cron schedules.
    216      *
    217      * @return mixed|string|void
    218      */
    219     public function ajax_schedules() {
    220         $this->_ajax_return( $this->_get_schedules() );
     254     */
     255    public function ajax_get_schedules(): void {
     256        check_ajax_referer( self::NONCE_CONTEXT );
     257
     258        $this->ajax_return( $this->get_schedules() );
    221259    }
    222260
     
    224262     * Run a cron event now rather than later.
    225263     */
    226     public function ajax_events() {
    227         // TODO: Sanitize inputs!
    228         $event         = json_decode( stripcslashes( $_POST['model'] ), true );
     264    public function ajax_update_event(): void {
     265        check_ajax_referer( self::NONCE_CONTEXT );
     266
     267        if ( ! isset( $_REQUEST['event'] ) ) {
     268            $this->ajax_return(
     269                new WP_Error(
     270                    'cron-pixie-update-event-missing-value',
     271                    __( 'No event given to be run now.', 'wp-cron-pixie' )
     272                )
     273            );
     274
     275            exit;
     276        }
     277
     278        $event = ! empty( $_REQUEST['event'] ) && is_string( $_REQUEST['event'] ) ? $_REQUEST['event'] : '';
     279
     280        if ( empty( $event ) ) {
     281            $this->ajax_return(
     282                new WP_Error(
     283                    'cron-pixie-update-event-missing-value',
     284                    __( 'Type invalid for JSON data given for event to be run now.', 'wp-cron-pixie' )
     285                )
     286            );
     287
     288            exit;
     289        }
     290
     291        $event = json_decode( stripcslashes( $event ), true );
     292
     293        $this->ajax_return( $this->update_event( $event ) );
     294    }
     295
     296    /**
     297     * Update an event.
     298     *
     299     * @param mixed $event Event data to be updated.
     300     *
     301     * @return bool|WP_Error
     302     */
     303    private function update_event( $event ): bool|WP_Error {
     304        if ( ! is_array( $event ) ) {
     305            return new WP_Error(
     306                'cron-pixie-update-event-missing-value',
     307                __( 'JSON data did not decode to an array for given for event to be run now.', 'wp-cron-pixie' )
     308            );
     309        }
     310
     311        if ( empty( $event['hook'] ) || ! is_string( $event['hook'] ) ) {
     312            return new WP_Error(
     313                'cron-pixie-update-event-missing-value',
     314                __( 'No hook value given for event to be run now.', 'wp-cron-pixie' )
     315            );
     316        }
     317
     318        if ( ! isset( $event['args'] ) ) {
     319            return new WP_Error(
     320                'cron-pixie-update-event-missing-value',
     321                __( 'No args value given for event to be run now.', 'wp-cron-pixie' )
     322            );
     323        }
     324
     325        if ( ! isset( $event['schedule'] ) ) {
     326            return new WP_Error(
     327                'cron-pixie-update-event-missing-value',
     328                __( 'No schedule value given for event to be run now.', 'wp-cron-pixie' )
     329            );
     330        }
     331
     332        if ( ! isset( $event['timestamp'] ) ) {
     333            return new WP_Error(
     334                'cron-pixie-update-event-missing-value',
     335                __( 'No timestamp value given for event to be run now.', 'wp-cron-pixie' )
     336            );
     337        }
     338
    229339        $event['args'] = empty( $event['args'] ) ? array() : $event['args'];
    230340
     
    235345        // If not expecting a schedule, but cron says it's on one, do nothing.
    236346        if ( 'false' === $event['schedule'] && ! empty( $schedule ) ) {
    237             $this->_ajax_return( new WP_Error( 'cron-pixie-scheduled-single', __( 'The single event is also in a schedule.', 'wp-cron-pixie' ) ) );
     347            return new WP_Error(
     348                'cron-pixie-update-event-scheduled-single',
     349                __( 'The single event is also in a schedule.', 'wp-cron-pixie' )
     350            );
    238351        }
    239352
    240353        // If expecting a schedule, but cron says it's not on one, do nothing.
    241         if ( 'false' !== $event['schedule'] && empty( $schedule ) ) {
    242             $this->_ajax_return( new WP_Error( 'cron-pixie-schedule-missing', __( 'The scheduled event is not scheduled.', 'wp-cron-pixie' ) ) );
     354        if ( 'false' !== strtolower( $event['schedule'] ) && empty( $schedule ) ) {
     355            return new WP_Error(
     356                'cron-pixie-update-event-schedule-missing',
     357                __( 'The scheduled event is not scheduled.', 'wp-cron-pixie' )
     358            );
    243359        }
    244360
    245361        // We only want to reschedule an event if it already exists and is in the future.
    246362        if ( false !== $timestamp && $now < $timestamp ) {
    247             wp_unschedule_event( $timestamp, $event['hook'], $event['args'] );
    248 
    249             if ( 'false' === $event['schedule'] ) {
    250                 wp_schedule_single_event( $event['timestamp'], $event['hook'], $event['args'] );
     363            $unscheduled = wp_unschedule_event( $timestamp, $event['hook'], $event['args'], true );
     364
     365            if ( is_wp_error( $unscheduled ) ) {
     366                $this->ajax_return( $unscheduled );
     367            }
     368
     369            if ( 'false' === strtolower( $event['schedule'] ) ) {
     370                $scheduled = wp_schedule_single_event( $event['timestamp'], $event['hook'], $event['args'], true );
    251371            } else {
    252                 wp_schedule_event( $event['timestamp'], $event['schedule'], $event['hook'], $event['args'] );
     372                $scheduled = wp_schedule_event( $event['timestamp'], $event['schedule'], $event['hook'], $event['args'], true );
     373            }
     374
     375            if ( is_wp_error( $scheduled ) ) {
     376                return $scheduled;
    253377            }
    254378        }
     
    257381        spawn_cron();
    258382
    259         $this->_ajax_return();
     383        return true;
    260384    }
    261385
     
    263387     * Update the setting for whether example events should be included in the cron.
    264388     */
    265     public function ajax_example_events() {
    266         // TODO: Sanitize inputs!
    267         if ( isset( $_POST['example_events'] ) ) {
    268             $value = empty( $_POST['example_events'] ) ? false : true;
    269 
    270             $settings = get_site_option( self::SETTINGS_KEY );
    271 
    272             if ( is_array( $settings ) ) {
    273                 $settings['example_events'] = $value;
    274             } else {
    275                 $settings = array( 'example_events' => $value );
    276             }
    277 
    278             if ( ! update_site_option( self::SETTINGS_KEY, $settings ) ) {
    279                 $this->_ajax_return( new WP_Error( 'cron-pixie-example-events-update-settings', __( 'Could not update settings.', 'wp-cron-pixie' ) ) );
    280             }
     389    public function ajax_update_example_events_setting(): void {
     390        check_ajax_referer( self::NONCE_CONTEXT );
     391
     392        if ( ! isset( $_REQUEST['example_events'] ) ) {
     393            $this->ajax_return(
     394                new WP_Error(
     395                    'cron-pixie-update-example-events-setting-missing-value',
     396                    __( 'No value given for whether Example Events should be included in cron.', 'wp-cron-pixie' )
     397                )
     398            );
     399
     400            exit;
     401        }
     402
     403        $example_events = ! empty( $_REQUEST['example_events'] ) && is_string( $_REQUEST['example_events'] );
     404
     405        $this->ajax_return( $this->update_example_events_setting( $example_events ) );
     406    }
     407
     408    /**
     409     * Update the setting for whether example events should be included in the cron.
     410     *
     411     * @param bool $example_events Example events setting.
     412     *
     413     * @return bool|WP_Error
     414     */
     415    private function update_example_events_setting( bool $example_events ): bool|WP_Error {
     416        $settings = get_site_option( self::SETTINGS_KEY );
     417
     418        if ( is_array( $settings ) ) {
     419            $settings['example_events'] = $example_events;
    281420        } else {
    282             $this->_ajax_return( new WP_Error( 'cron-pixie-example-events-missing-value', __( 'No value given for whether Example Events should be included in cron.', 'wp-cron-pixie' ) ) );
    283         }
    284 
    285         $this->_ajax_return( $value );
     421            $settings = array( 'example_events' => $example_events );
     422        }
     423
     424        if ( ! update_site_option( self::SETTINGS_KEY, $settings ) ) {
     425            return new WP_Error(
     426                'cron-pixie-update-example-events-setting-update-settings',
     427                __( 'Could not update settings.', 'wp-cron-pixie' )
     428            );
     429        }
     430
     431        // As we've potentially changed whether example events are needed,
     432        // proactively add or remove them.
     433        $this->manage_example_events();
     434
     435        return true;
    286436    }
    287437
     
    289439     * Update the setting for whether the display should auto refresh.
    290440     */
    291     public function ajax_auto_refresh() {
    292         // TODO: Sanitize inputs!
    293         if ( isset( $_POST['auto_refresh'] ) ) {
    294             $value = empty( $_POST['auto_refresh'] ) ? false : true;
    295 
    296             $settings = get_site_option( self::SETTINGS_KEY );
    297 
    298             if ( is_array( $settings ) ) {
    299                 $settings['auto_refresh'] = $value;
    300             } else {
    301                 $settings = array( 'auto_refresh' => $value );
    302             }
    303 
    304             if ( ! update_site_option( self::SETTINGS_KEY, $settings ) ) {
    305                 $this->_ajax_return( new WP_Error( 'cron-pixie-auto-refresh-update-settings', __( 'Could not update settings.', 'wp-cron-pixie' ) ) );
    306             }
     441    public function ajax_update_auto_refresh_setting(): void {
     442        check_ajax_referer( self::NONCE_CONTEXT );
     443
     444        if ( ! isset( $_REQUEST['auto_refresh'] ) ) {
     445            $this->ajax_return(
     446                new WP_Error(
     447                    'cron-pixie-update-auto-refresh-setting-missing-value',
     448                    __( 'No value given for whether the display should auto refresh.', 'wp-cron-pixie' )
     449                )
     450            );
     451
     452            exit;
     453        }
     454
     455        $auto_refresh = ! empty( $_REQUEST['auto_refresh'] ) && is_string( $_REQUEST['auto_refresh'] );
     456
     457        $this->ajax_return( $this->update_auto_refresh_setting( $auto_refresh ) );
     458    }
     459
     460    /**
     461     * Update the setting for whether the display should auto refresh.
     462     *
     463     * @param bool $auto_refresh Auto refresh setting.
     464     *
     465     * @return bool|WP_Error
     466     */
     467    private function update_auto_refresh_setting( bool $auto_refresh ): bool|WP_Error {
     468        $settings = get_site_option( self::SETTINGS_KEY );
     469
     470        if ( is_array( $settings ) ) {
     471            $settings['auto_refresh'] = $auto_refresh;
    307472        } else {
    308             $this->_ajax_return( new WP_Error( 'cron-pixie-auto-refresh-missing-value', __( 'No value given for whether the display should auto refresh.', 'wp-cron-pixie' ) ) );
    309         }
    310 
    311         $this->_ajax_return( $value );
     473            $settings = array( 'auto_refresh' => $auto_refresh );
     474        }
     475
     476        if ( ! update_site_option( self::SETTINGS_KEY, $settings ) ) {
     477            return new WP_Error(
     478                'cron-pixie-update-auto-refresh-setting-update-settings',
     479                __( 'Could not update settings.', 'wp-cron-pixie' )
     480            );
     481        }
     482
     483        return true;
    312484    }
    313485
     
    315487     * Adds an "every_minute" schedule to the Schedules list.
    316488     *
    317      * @param array $schedules
    318      *
    319      * @return array
    320      */
    321     public function filter_cron_schedules( $schedules = array() ) {
     489     * @param array<string, array<string, mixed>> $schedules Current schedules.
     490     *
     491     * @return array<string, array<string, mixed>>
     492     */
     493    public function filter_cron_schedules( array $schedules = array() ): array {
    322494        $schedules['every_minute'] = array(
    323495            'interval' => 60,
     
    329501
    330502    /**
     503     * Reschedules the passed event when due so that it is due in the past again.
     504     *
     505     * @param mixed $wibble Args for the event.
     506     */
     507    public function reschedule_passed_event( $wibble ): void {
     508        $args = array( 'wibble' => $wibble );
     509
     510        // Remove the event that has already been missed (2 minutes over due).
     511        // Elsewhere the event will be recreated if example events are turned on.
     512        if ( wp_next_scheduled( 'cron_pixie_passed_event', $args ) ) {
     513            wp_clear_scheduled_hook( 'cron_pixie_passed_event', $args );
     514        }
     515    }
     516
     517    /**
     518     * Create or remove example events based on setting.
     519     */
     520    private function manage_example_events(): void {
     521        if ( $this->get_setting( 'example_events' ) ) {
     522            $this->create_example_events();
     523        } else {
     524            $this->remove_example_events();
     525        }
     526    }
     527
     528    /**
    331529     * Creates test cron events in the cron schedule if they do not already exist.
    332530     */
    333     private function _create_test_events() {
     531    private function create_example_events(): void {
    334532        $args = array( 'wibble' => 'wobble' );
    335533
    336         // Create an event that has already been missed (2 minutes over due).
     534        // Create an event that has already been missed (2 minutes overdue).
    337535        if ( ! wp_next_scheduled( 'cron_pixie_passed_event', $args ) ) {
    338536            wp_schedule_event( time() - 120, 'every_minute', 'cron_pixie_passed_event', $args );
     
    353551     * Remove test cron events from the cron schedule if they already exist.
    354552     */
    355     private function _remove_test_events() {
     553    private function remove_example_events(): void {
    356554        $args = array( 'wibble' => 'wobble' );
    357555
    358         // Create an event that has already been missed (2 minutes over due).
     556        // Remove the event that has already been missed (2 minutes over due).
    359557        if ( wp_next_scheduled( 'cron_pixie_passed_event', $args ) ) {
    360558            wp_clear_scheduled_hook( 'cron_pixie_passed_event', $args );
    361559        }
    362560
    363         // Create an event that is just coming up (initially 30 seconds until due).
    364         if ( ! wp_next_scheduled( 'cron_pixie_future_event', $args ) ) {
     561        // Remove the event that is just coming up (initially 30 seconds until due).
     562        if ( wp_next_scheduled( 'cron_pixie_future_event', $args ) ) {
    365563            wp_clear_scheduled_hook( 'cron_pixie_future_event', $args );
    366564        }
    367565
    368         // Create a single event that is in the future (initially 5 minutes until due).
    369         if ( ! wp_next_scheduled( 'cron_pixie_single_event', $args ) ) {
     566        // Remove the single event that is in the future (initially 5 minutes until due).
     567        if ( wp_next_scheduled( 'cron_pixie_single_event', $args ) ) {
    370568            wp_clear_scheduled_hook( 'cron_pixie_single_event', $args );
    371569        }
     
    375573     * Get a single setting based on its key name.
    376574     *
    377      * @param string $key
     575     * @param string $key Setting key.
    378576     *
    379577     * @return mixed defaults to false if not found
    380578     */
    381     private function _get_setting( $key ) {
     579    private function get_setting( string $key ) {
    382580        $value = false;
    383581
    384582        $settings = get_site_option( self::SETTINGS_KEY );
    385583
    386         if ( ! empty( $settings ) ) {
    387             if ( isset( $settings[ $key ] ) ) {
     584        if ( ! empty( $settings ) && is_array( $settings ) && isset( $settings[ $key ] ) ) {
    388585                $value = $settings[ $key ];
    389             }
    390586        }
    391587
  • wp-cron-pixie/trunk/js/main.js

    r2263440 r3317252  
    1 document.addEventListener( 'DOMContentLoaded', () => {
    2     var $mountPoint = document.getElementById( 'cron-pixie-main' );
    3     var app = Elm.CronPixie.init( {
    4         node: $mountPoint,
    5         flags: {
    6             strings: CronPixie.strings,
    7             admin_url: CronPixie.admin_url,
    8             nonce: CronPixie.nonce,
    9             timer_period: CronPixie.timer_period,
    10             schedules: CronPixie.data.schedules,
    11             example_events: CronPixie.example_events,
    12             auto_refresh: CronPixie.auto_refresh
    13         }
    14     } );
    15 } );
     1document.addEventListener('DOMContentLoaded', () => {
     2    import('./ui.js?ver=' + CronPixie.version).then((CronPixieUI) => {
     3        CronPixieUI.main(JSON.stringify(CronPixie));
     4    });
     5});
  • wp-cron-pixie/trunk/wp-cron-pixie.php

    r3061791 r3317252  
    11<?php
    2 
    32/**
     3 * A little dashboard widget to manage the WordPress cron.
     4 *
    45 * @link              https://github.com/ianmjones/wp-cron-pixie
    56 * @package           Cron_Pixie
     
    910 * Plugin URI:        https://github.com/ianmjones/wp-cron-pixie
    1011 * Description:       A little dashboard widget to manage the WordPress cron.
    11  * Version:           1.4.4
     12 * Version:           1.5.0
    1213 * Author:            Ian M. Jones
    1314 * Author URI:        https://ianmjones.com/
    14  * License:           GPL-2.0+
    15  * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
     15 * License:           MIT
     16 * License URI:       https://mit-license.org/
    1617 * Text Domain:       wp-cron-pixie
    1718 * Domain Path:       /languages
    1819 * Network:           False
    1920 */
     21
     22namespace Cron_Pixie;
    2023
    2124// If this file is called directly, abort.
     
    2730 * Returns info about the plugin.
    2831 *
    29  * @return array
     32 * @return array<string,string>
    3033 */
    31 function cron_pixie_meta() {
     34function cron_pixie_meta(): array {
    3235    return array(
    3336        'slug'    => 'wp-cron-pixie',
    3437        'name'    => 'WP Cron Pixie',
    3538        'file'    => __FILE__,
    36         'version' => '1.4.4',
     39        'version' => '1.5.0',
    3740    );
    3841}
     
    4346/**
    4447 * Initialize the plugin's functionality once the correct hook fires.
     48 *
     49 * We create an instance of the main class fairly early so that we can
     50 * hook in and create the cron schedule for our example events.
     51 * The cron schedule needs to be there to avoid errors when the jobs
     52 * are later scheduled.
     53 *
     54 * Very little else is done at this point, the rest happens in admin_init
     55 * as then we know for sure we're in admin mode.
    4556 */
    46 function cron_pixie_admin_init() {
     57function cron_pixie_init(): void {
     58    global $cron_pixie;
     59
    4760    $cron_pixie = new Cron_Pixie( cron_pixie_meta() );
    4861}
    4962
    50 add_action( 'admin_init', 'cron_pixie_admin_init' );
     63add_action( 'init', __NAMESPACE__ . '\cron_pixie_init' );
     64
     65/**
     66 * Initialize the plugin's real functionality.
     67 */
     68function cron_pixie_admin_init(): void {
     69    global $cron_pixie;
     70
     71    $cron_pixie->init();
     72}
     73
     74add_action( 'admin_init', __NAMESPACE__ . '\cron_pixie_admin_init' );
Note: See TracChangeset for help on using the changeset viewer.