Skip to content

Commit 0013df2

Browse files
committed
improve(COURIER-1001): Remove WP Cron in favor of Action Scheduler
1 parent 7467ced commit 0013df2

File tree

4 files changed

+196
-19
lines changed

4 files changed

+196
-19
lines changed

composer.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@
4646
"autoload": {
4747
"psr-4": {
4848
"CourierNotices\\": "includes/"
49-
}
49+
},
50+
"files": [
51+
"vendor/woocommerce/action-scheduler/action-scheduler.php"
52+
]
5053
},
5154
"require-dev": {
5255
"dealerdirect/phpcodesniffer-composer-installer": "^1.1.1",
@@ -58,6 +61,13 @@
5861
"friendsofphp/php-cs-fixer": "^3.83.0",
5962
"phpcompatibility/phpcompatibility-wp": "2.1.7"
6063
},
64+
"extra": {
65+
"installer-paths": {
66+
"vendor/woocommerce/action-scheduler/": [
67+
"woocommerce/action-scheduler"
68+
]
69+
}
70+
},
6171
"scripts": {
6272
"fixer": "php-cs-fixer -v fix --allow-risky=yes",
6373
"fixer:test": "php-cs-fixer fix -v --dry-run --stop-on-violation --using-cache=no --allow-risky=yes",

composer.lock

Lines changed: 44 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/Controller/Action_Scheduler.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,12 @@ public function expire_notice( $post_id ) {
159159
}
160160

161161
// Update post status to expired
162-
wp_update_post( [
163-
'ID' => $post_id,
164-
'post_status' => 'courier_expired',
165-
] );
162+
wp_update_post(
163+
[
164+
'ID' => $post_id,
165+
'post_status' => 'courier_expired',
166+
]
167+
);
166168

167169
// Clear related caches
168170
$this->clear_notice_caches();
@@ -245,10 +247,12 @@ public function bulk_expire_notices() {
245247

246248
if ( $notices_query->have_posts() ) {
247249
foreach ( $notices_query->posts as $post_id ) {
248-
wp_update_post( [
249-
'ID' => $post_id,
250-
'post_status' => 'courier_expired',
251-
] );
250+
wp_update_post(
251+
[
252+
'ID' => $post_id,
253+
'post_status' => 'courier_expired',
254+
]
255+
);
252256
}
253257
}
254258

@@ -285,11 +289,13 @@ private function clear_notice_caches() {
285289
* @return array Array of scheduled actions.
286290
*/
287291
public function get_scheduled_actions_for_notice( $post_id ) {
288-
return as_get_scheduled_actions( [
289-
'hook' => self::EXPIRE_NOTICE_ACTION,
290-
'args' => [ $post_id ],
291-
'group' => self::SCHEDULER_GROUP,
292-
] );
292+
return as_get_scheduled_actions(
293+
[
294+
'hook' => self::EXPIRE_NOTICE_ACTION,
295+
'args' => [ $post_id ],
296+
'group' => self::SCHEDULER_GROUP,
297+
]
298+
);
293299
}
294300

295301
/**
@@ -300,9 +306,11 @@ public function get_scheduled_actions_for_notice( $post_id ) {
300306
* @return array Array of all scheduled actions.
301307
*/
302308
public function get_all_scheduled_actions() {
303-
return as_get_scheduled_actions( [
304-
'group' => self::SCHEDULER_GROUP,
305-
] );
309+
return as_get_scheduled_actions(
310+
[
311+
'group' => self::SCHEDULER_GROUP,
312+
]
313+
);
306314
}
307315

308316
/**
@@ -348,4 +356,4 @@ public function reschedule_all_notices() {
348356

349357
return count( $notices_query->posts );
350358
}
351-
}
359+
}

includes/Controller/Admin/Settings/General.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ public function settings_init() {
191191
// Setup General Settings.
192192
$this->setup_general_settings();
193193

194+
// Add Action Scheduler information section.
195+
$this->setup_action_scheduler_settings();
196+
194197
$active_subtab = isset( $_GET['subtab'] ) ? sanitize_text_field( wp_unslash( $_GET['subtab'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
195198

196199
switch ( $active_subtab ) {
@@ -358,6 +361,110 @@ private function setup_design_global_settings() {
358361
}
359362

360363

364+
/**
365+
* Setup Action Scheduler information section
366+
*
367+
* @since 1.8.0
368+
*/
369+
private function setup_action_scheduler_settings() {
370+
$tab_section = 'courier_settings';
371+
372+
// Action Scheduler Information Section.
373+
add_settings_section(
374+
'courier_action_scheduler_settings',
375+
esc_html__( 'Action Scheduler', 'courier-notices' ),
376+
array( $this, 'create_action_scheduler_section' ),
377+
'courier'
378+
);
379+
380+
add_settings_field(
381+
'action_scheduler_info',
382+
esc_html__( 'Status', 'courier-notices' ),
383+
array( $this, 'render_action_scheduler_info' ),
384+
'courier',
385+
'courier_action_scheduler_settings',
386+
array(
387+
'field' => 'action_scheduler_info',
388+
'section' => $tab_section,
389+
'class' => 'action-scheduler-info',
390+
'label' => esc_html__( 'Action Scheduler Status', 'courier-notices' ),
391+
'description' => esc_html__( 'Information about the Action Scheduler integration.', 'courier-notices' ),
392+
)
393+
);
394+
}
395+
396+
/**
397+
* Create Action Scheduler section
398+
*
399+
* @since 1.8.0
400+
*
401+
* @param array $args Array of arguments.
402+
*/
403+
public function create_action_scheduler_section( $args ) {
404+
if ( ! empty( $args['title'] ) ) {
405+
?>
406+
<div class="gray-bg negative-bg">
407+
<div class="wrapper">
408+
<h2 class="light-weight">
409+
<?php echo esc_html( $args['title'] ); ?>
410+
</h2>
411+
</div>
412+
</div>
413+
<?php
414+
}
415+
}
416+
417+
/**
418+
* Render Action Scheduler information
419+
*
420+
* @since 1.8.0
421+
*
422+
* @param array $args Array of arguments.
423+
*/
424+
public function render_action_scheduler_info( $args ) {
425+
$plugin_options = get_option( 'courier_notices_options', [] );
426+
$use_action_scheduler = ! empty( $plugin_options['use_action_scheduler'] );
427+
$action_scheduler_available = class_exists( 'ActionScheduler' );
428+
429+
?>
430+
<div class="action-scheduler-info">
431+
<?php if ( $action_scheduler_available ) : ?>
432+
<div class="notice notice-success inline">
433+
<p>
434+
<strong><?php esc_html_e( 'Action Scheduler is available.', 'courier-notices' ); ?></strong>
435+
<?php if ( $use_action_scheduler ) : ?>
436+
<?php esc_html_e( 'Courier Notices is using Action Scheduler for scheduled tasks.', 'courier-notices' ); ?>
437+
<?php else : ?>
438+
<?php esc_html_e( 'Courier Notices is using WordPress Cron for scheduled tasks.', 'courier-notices' ); ?>
439+
<?php endif; ?>
440+
</p>
441+
</div>
442+
<p>
443+
<?php if ( $use_action_scheduler ) : ?>
444+
<a href="<?php echo esc_url( admin_url( 'tools.php?page=action-scheduler' ) ); ?>" class="button button-secondary">
445+
<?php esc_html_e( 'View Scheduled Actions', 'courier-notices' ); ?>
446+
</a>
447+
<?php else : ?>
448+
<a href="<?php echo esc_url( admin_url( 'tools.php?page=action-scheduler' ) ); ?>" class="button button-secondary">
449+
<?php esc_html_e( 'View Action Scheduler', 'courier-notices' ); ?>
450+
</a>
451+
<?php endif; ?>
452+
</p>
453+
<?php else : ?>
454+
<div class="notice notice-warning inline">
455+
<p>
456+
<strong><?php esc_html_e( 'Action Scheduler is not available.', 'courier-notices' ); ?></strong>
457+
<?php esc_html_e( 'Courier Notices is using WordPress Cron for scheduled tasks.', 'courier-notices' ); ?>
458+
</p>
459+
</div>
460+
<p>
461+
<?php esc_html_e( 'To use Action Scheduler for better reliability and scalability, install WooCommerce or another plugin that provides Action Scheduler.', 'courier-notices' ); ?>
462+
</p>
463+
<?php endif; ?>
464+
</div>
465+
<?php
466+
}
467+
361468
/**
362469
* Setup our different types of informational courier notices
363470
*
@@ -502,6 +609,15 @@ public static function get_tabs() {
502609
),
503610
);
504611

612+
// Add Action Scheduler tab if Action Scheduler is available.
613+
if ( class_exists( 'ActionScheduler' ) ) {
614+
$tabs['action_scheduler'] = array(
615+
'url' => admin_url( 'tools.php?page=action-scheduler' ),
616+
'label' => esc_html__( 'Action Scheduler', 'courier-notices' ),
617+
'sub_tabs' => array(),
618+
);
619+
}
620+
505621
return apply_filters( 'courier_notices_settings_tabs', $tabs );
506622
}
507623

0 commit comments

Comments
 (0)