diff --git a/includes/admin/feedzy-rss-feeds-import.php b/includes/admin/feedzy-rss-feeds-import.php index f8dcc0a2..2464ff3a 100644 --- a/includes/admin/feedzy-rss-feeds-import.php +++ b/includes/admin/feedzy-rss-feeds-import.php @@ -573,7 +573,7 @@ public function save_feedzy_import_feed_meta( $post_id, $post ) { add_action( 'save_post_feedzy_imports', array( $this, 'save_feedzy_import_feed_meta' ), 1, 2 ); } // Clear the import job cron schedule if it exists. - wp_clear_scheduled_hook( 'feedzy_cron', array( 100, $post_id ) ); + Feedzy_Rss_Feeds_Util_Scheduler::clear_scheduled_hook( 'feedzy_cron', array( 100, $post_id ) ); do_action( 'feedzy_save_fields', $post_id, $post ); } @@ -754,9 +754,9 @@ public function manage_feedzy_import_columns( $column, $post_id ) { break; case 'feedzy-next_run': - $next = wp_next_scheduled( 'feedzy_cron', array( 100, $post_id ) ); + $next = Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'feedzy_cron', array( 100, $post_id ) ); if ( ! $next ) { - $next = wp_next_scheduled( 'feedzy_cron' ); + $next = Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'feedzy_cron' ); } if ( $next ) { $now = new DateTime(); @@ -2326,11 +2326,11 @@ public function add_cron() { $offset = sanitize_text_field( wp_unslash( $_POST['fz_execution_offset'] ) ); $time = $this->get_cron_execution( $execution, $offset ); $schedule = sanitize_text_field( wp_unslash( $_POST['fz_cron_schedule'] ) ); - wp_clear_scheduled_hook( 'feedzy_cron' ); + Feedzy_Rss_Feeds_Util_Scheduler::clear_scheduled_hook( 'feedzy_cron' ); } } - if ( false === wp_next_scheduled( 'feedzy_cron' ) ) { - wp_schedule_event( $time, $schedule, 'feedzy_cron' ); + if ( false === Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'feedzy_cron' ) ) { + Feedzy_Rss_Feeds_Util_Scheduler::schedule_event( $time, $schedule, 'feedzy_cron' ); } // Register import jobs based cron jobs. @@ -2362,8 +2362,8 @@ public function add_cron() { $fz_execution_offset = get_post_meta( $job_id, 'fz_execution_offset', true ); $time = $this->get_cron_execution( $fz_cron_execution, $fz_execution_offset ); - if ( false === wp_next_scheduled( 'feedzy_cron', array( 100, $job_id ) ) ) { - wp_schedule_event( $time, $fz_cron_schedule, 'feedzy_cron', array( 100, $job_id ) ); + if ( false === Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'feedzy_cron', array( 100, $job_id ) ) ) { + Feedzy_Rss_Feeds_Util_Scheduler::schedule_event( $time, $fz_cron_schedule, 'feedzy_cron', array( 100, $job_id ) ); } } } @@ -2401,7 +2401,7 @@ public function admin_notices() { echo wp_kses_post( '

' . __( 'WP Cron is disabled. Your feeds would not get updated. Please contact your hosting provider or system administrator', 'feedzy-rss-feeds' ) . '

' ); } - if ( false === wp_next_scheduled( 'feedzy_cron' ) ) { + if ( false === Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'feedzy_cron' ) ) { echo wp_kses_post( '

' . __( 'Unable to register cron job. Your feeds might not get updated', 'feedzy-rss-feeds' ) . '

' ); } } diff --git a/includes/util/feedzy-rss-feeds-util-scheduler.php b/includes/util/feedzy-rss-feeds-util-scheduler.php new file mode 100644 index 00000000..2fc89fce --- /dev/null +++ b/includes/util/feedzy-rss-feeds-util-scheduler.php @@ -0,0 +1,78 @@ + + */ +class Feedzy_Rss_Feeds_Util_Scheduler { + + /** + * Check if an action hook is scheduled. + * + * @param string $hook The hook to check. + * @param array $args Optional. Arguments to pass to the hook + * + * @return bool + */ + public static function is_scheduled( $hook, $args = array() ) { + if ( function_exists( 'as_has_scheduled_action' ) ) { + return as_has_scheduled_action( $hook, $args ); + } + + if ( function_exists( 'as_next_scheduled_action' ) ) { + // For older versions of AS. + return as_next_scheduled_action( $hook, $args ) !== false; + } + + return wp_next_scheduled( $hook, $args ) !== false; + } + + /** + * Clear scheduled hook. + * + * @param string $hook The name of the hook to clear. + * @param array $args Optional. Arguments that were to be passed to the hook's callback function. Default empty array. + * @return mixed The scheduled action ID if a scheduled action was found, or null if no matching action found. If WP_Cron is used, on success an integer indicating number of events unscheduled, false or WP_Error if unscheduling one or more events fail. + */ + public static function clear_scheduled_hook( $hook, $args = array() ) { + if ( function_exists( 'as_unschedule_all_actions' ) ) { + return as_unschedule_all_actions( $hook, $args ); + } + + return wp_clear_scheduled_hook( $hook, $args ); + } + + /** + * Schedule an event. + * + * @param int $time The first time that the event will occur. + * @param string $recurrence How often the event should recur. See wp_get_schedules() for accepted values. + * @param string $hook The name of the hook that will be triggered by the event. + * @param array $args Optional. Arguments to pass to the hook's callback function. Default empty array. + * @return integer|bool|WP_Error The action ID if Action Scheduler is used. True if event successfully scheduled, False or WP_Error on failure if WP Cron is used. + */ + public static function schedule_event( $time, $recurrence, $hook, $args = array() ) { + if ( function_exists( 'as_schedule_recurring_action' ) ) { + $schedules = wp_get_schedules(); + if ( isset( $schedules[ $recurrence ] ) ) { + $interval = $schedules[ $recurrence ]['interval']; + return as_schedule_recurring_action( $time, $interval, $hook, $args ); + } + } + + return wp_schedule_event( $time, $recurrence, $hook, $args ); + } +}