Skip to content

Commit 48fa552

Browse files
committed
feat: use action scheduler when available
1 parent 2aeada6 commit 48fa552

File tree

2 files changed

+87
-9
lines changed

2 files changed

+87
-9
lines changed

includes/admin/feedzy-rss-feeds-import.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ public function save_feedzy_import_feed_meta( $post_id, $post ) {
573573
add_action( 'save_post_feedzy_imports', array( $this, 'save_feedzy_import_feed_meta' ), 1, 2 );
574574
}
575575
// Clear the import job cron schedule if it exists.
576-
wp_clear_scheduled_hook( 'feedzy_cron', array( 100, $post_id ) );
576+
Feedzy_Rss_Feeds_Util_Scheduler::clear_scheduled_hook( 'feedzy_cron', array( 100, $post_id ) );
577577
do_action( 'feedzy_save_fields', $post_id, $post );
578578
}
579579

@@ -754,9 +754,9 @@ public function manage_feedzy_import_columns( $column, $post_id ) {
754754

755755
break;
756756
case 'feedzy-next_run':
757-
$next = wp_next_scheduled( 'feedzy_cron', array( 100, $post_id ) );
757+
$next = Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'feedzy_cron', array( 100, $post_id ) );
758758
if ( ! $next ) {
759-
$next = wp_next_scheduled( 'feedzy_cron' );
759+
$next = Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'feedzy_cron' );
760760
}
761761
if ( $next ) {
762762
$now = new DateTime();
@@ -2326,11 +2326,11 @@ public function add_cron() {
23262326
$offset = sanitize_text_field( wp_unslash( $_POST['fz_execution_offset'] ) );
23272327
$time = $this->get_cron_execution( $execution, $offset );
23282328
$schedule = sanitize_text_field( wp_unslash( $_POST['fz_cron_schedule'] ) );
2329-
wp_clear_scheduled_hook( 'feedzy_cron' );
2329+
Feedzy_Rss_Feeds_Util_Scheduler::clear_scheduled_hook( 'feedzy_cron' );
23302330
}
23312331
}
2332-
if ( false === wp_next_scheduled( 'feedzy_cron' ) ) {
2333-
wp_schedule_event( $time, $schedule, 'feedzy_cron' );
2332+
if ( false === Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'feedzy_cron' ) ) {
2333+
Feedzy_Rss_Feeds_Util_Scheduler::schedule_event( $time, $schedule, 'feedzy_cron' );
23342334
}
23352335

23362336
// Register import jobs based cron jobs.
@@ -2362,8 +2362,8 @@ public function add_cron() {
23622362
$fz_execution_offset = get_post_meta( $job_id, 'fz_execution_offset', true );
23632363
$time = $this->get_cron_execution( $fz_cron_execution, $fz_execution_offset );
23642364

2365-
if ( false === wp_next_scheduled( 'feedzy_cron', array( 100, $job_id ) ) ) {
2366-
wp_schedule_event( $time, $fz_cron_schedule, 'feedzy_cron', array( 100, $job_id ) );
2365+
if ( false === Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'feedzy_cron', array( 100, $job_id ) ) ) {
2366+
Feedzy_Rss_Feeds_Util_Scheduler::schedule_event( $time, $fz_cron_schedule, 'feedzy_cron', array( 100, $job_id ) );
23672367
}
23682368
}
23692369
}
@@ -2401,7 +2401,7 @@ public function admin_notices() {
24012401
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>' );
24022402
}
24032403

2404-
if ( false === wp_next_scheduled( 'feedzy_cron' ) ) {
2404+
if ( false === Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'feedzy_cron' ) ) {
24052405
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>' );
24062406
}
24072407
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* The class that contains utility functions for scheduling tasks.
4+
*
5+
* @link https://themeisle.com
6+
*
7+
* @package feedzy-rss-feeds
8+
* @subpackage feedzy-rss-feeds/includes/util
9+
*/
10+
11+
/**
12+
* The class that contains utility functions for scheduling tasks.
13+
*
14+
* Class that contains utility functions for scheduling tasks.
15+
*
16+
* @package feedzy-rss-feeds
17+
* @subpackage feedzy-rss-feeds/includes/util
18+
* @author Themeisle <[email protected]>
19+
*/
20+
class Feedzy_Rss_Feeds_Util_Scheduler {
21+
22+
/**
23+
* Check if an action hook is scheduled.
24+
*
25+
* @param string $hook The hook to check.
26+
* @param array $args Optional. Arguments to pass to the hook
27+
*
28+
* @return bool
29+
*/
30+
public static function is_scheduled( $hook, $args = array() ) {
31+
if ( function_exists( 'as_has_scheduled_action' ) ) {
32+
return as_has_scheduled_action( $hook, $args );
33+
}
34+
35+
if ( function_exists( 'as_next_scheduled_action' ) ) {
36+
// For older versions of AS.
37+
return as_next_scheduled_action( $hook, $args ) !== false;
38+
}
39+
40+
return wp_next_scheduled( $hook, $args ) !== false;
41+
}
42+
43+
/**
44+
* Clear scheduled hook.
45+
*
46+
* @param string $hook The name of the hook to clear.
47+
* @param array $args Optional. Arguments that were to be passed to the hook's callback function. Default empty array.
48+
* @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.
49+
*/
50+
public static function clear_scheduled_hook( $hook, $args = array() ) {
51+
if ( function_exists( 'as_unschedule_all_actions' ) ) {
52+
return as_unschedule_all_actions( $hook, $args );
53+
}
54+
55+
return wp_clear_scheduled_hook( $hook, $args );
56+
}
57+
58+
/**
59+
* Schedule an event.
60+
*
61+
* @param int $time The first time that the event will occur.
62+
* @param string $recurrence How often the event should recur. See wp_get_schedules() for accepted values.
63+
* @param string $hook The name of the hook that will be triggered by the event.
64+
* @param array $args Optional. Arguments to pass to the hook's callback function. Default empty array.
65+
* @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.
66+
*/
67+
public static function schedule_event( $time, $recurrence, $hook, $args = array() ) {
68+
if ( function_exists( 'as_schedule_recurring_action' ) ) {
69+
$schedules = wp_get_schedules();
70+
if ( isset( $schedules[ $recurrence ] ) ) {
71+
$interval = $schedules[ $recurrence ]['interval'];
72+
return as_schedule_recurring_action( $time, $interval, $hook, $args );
73+
}
74+
}
75+
76+
return wp_schedule_event( $time, $recurrence, $hook, $args );
77+
}
78+
}

0 commit comments

Comments
 (0)