Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use action scheduler when available #1032

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 ) );
}
}
}
Expand Down Expand Up @@ -2401,7 +2401,7 @@ public function admin_notices() {
echo wp_kses_post( '<div class="notice notice-error feedzy-error-critical is-dismissible"><p>' . __( 'WP Cron is disabled. Your feeds would not get updated. Please contact your hosting provider or system administrator', 'feedzy-rss-feeds' ) . '</p></div>' );
}

if ( false === wp_next_scheduled( 'feedzy_cron' ) ) {
if ( false === Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'feedzy_cron' ) ) {
echo wp_kses_post( '<div class="notice notice-error"><p>' . __( 'Unable to register cron job. Your feeds might not get updated', 'feedzy-rss-feeds' ) . '</p></div>' );
}
}
Expand Down
78 changes: 78 additions & 0 deletions includes/util/feedzy-rss-feeds-util-scheduler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* The class that contains utility functions for scheduling tasks.
*
* @link https://themeisle.com
*
* @package feedzy-rss-feeds
* @subpackage feedzy-rss-feeds/includes/util
*/

/**
* The class that contains utility functions for scheduling tasks.
*
* Class that contains utility functions for scheduling tasks.
*
* @package feedzy-rss-feeds
* @subpackage feedzy-rss-feeds/includes/util
* @author Themeisle <[email protected]>
*/
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 );
}
}
Loading