Skip to content

Commit 93b4ded

Browse files
Brent ShepherdJPry
authored andcommitted
Use as_ prefix not wc_ prefix for public APIs
To avoid confusion once Action Scheduler is included in WooCommerce core, and it provides a different set of public APIs for working with the action queue.
1 parent 350f3ac commit 93b4ded

File tree

7 files changed

+179
-49
lines changed

7 files changed

+179
-49
lines changed

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ The Action Scheduler API functions are designed to mirror the WordPress [WP-Cron
137137

138138
Functions return similar values and accept similar arguments to their WP-Cron counterparts. The notable differences are:
139139

140-
* `wc_schedule_single_action()` & `wc_schedule_recurring_action()` will return the post ID of the scheduled action rather than boolean indicating whether the event was scheduled
141-
* `wc_schedule_recurring_action()` takes an interval in seconds as the recurring interval rather than an arbitrary string
142-
* `wc_schedule_single_action()` & `wc_schedule_recurring_action()` can accept a `$group` parameter to group different actions for the one plugin together.
143-
* the `wp_` prefix is substituted with `wc_` and the term `event` is replaced with `action`
140+
* `as_schedule_single_action()` & `as_schedule_recurring_action()` will return the post ID of the scheduled action rather than boolean indicating whether the event was scheduled
141+
* `as_schedule_recurring_action()` takes an interval in seconds as the recurring interval rather than an arbitrary string
142+
* `as_schedule_single_action()` & `as_schedule_recurring_action()` can accept a `$group` parameter to group different actions for the one plugin together.
143+
* the `wp_` prefix is substituted with `as_` and the term `event` is replaced with `action`
144144

145145

146-
#### Function Reference / `wc_schedule_single_action()`
146+
#### Function Reference / `as_schedule_single_action()`
147147

148148
##### Description
149149

@@ -152,7 +152,7 @@ Schedule an action to run one time.
152152
##### Usage
153153

154154
```php
155-
<?php wc_schedule_single_action( $timestamp, $hook, $args, $group ); ?>
155+
<?php as_schedule_single_action( $timestamp, $hook, $args, $group ); ?>
156156
````
157157

158158
##### Parameters
@@ -167,7 +167,7 @@ Schedule an action to run one time.
167167
(integer) the action's ID in the [posts](http://codex.wordpress.org/Database_Description#Table_Overview) table.
168168

169169

170-
#### Function Reference / `wc_schedule_recurring_action()`
170+
#### Function Reference / `as_schedule_recurring_action()`
171171

172172
##### Description
173173

@@ -176,7 +176,7 @@ Schedule an action to run repeatedly with a specified interval in seconds.
176176
##### Usage
177177

178178
```php
179-
<?php wc_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group ); ?>
179+
<?php as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group ); ?>
180180
````
181181

182182
##### Parameters
@@ -192,7 +192,7 @@ Schedule an action to run repeatedly with a specified interval in seconds.
192192
(integer) the action's ID in the [posts](http://codex.wordpress.org/Database_Description#Table_Overview) table.
193193

194194

195-
#### Function Reference / `wc_schedule_cron_action()`
195+
#### Function Reference / `as_schedule_cron_action()`
196196

197197
##### Description
198198

@@ -201,7 +201,7 @@ Schedule an action that recurs on a cron-like schedule.
201201
##### Usage
202202

203203
```php
204-
<?php wc_schedule_cron_action( $timestamp, $schedule, $hook, $args, $group ); ?>
204+
<?php as_schedule_cron_action( $timestamp, $schedule, $hook, $args, $group ); ?>
205205
````
206206

207207
##### Parameters
@@ -217,7 +217,7 @@ Schedule an action that recurs on a cron-like schedule.
217217
(integer) the action's ID in the [posts](http://codex.wordpress.org/Database_Description#Table_Overview) table.
218218

219219

220-
#### Function Reference / `wc_unschedule_action()`
220+
#### Function Reference / `as_unschedule_action()`
221221

222222
##### Description
223223

@@ -226,7 +226,7 @@ Cancel the next occurrence of a job.
226226
##### Usage
227227

228228
```php
229-
<?php wc_unschedule_action( $hook, $args, $group ); ?>
229+
<?php as_unschedule_action( $hook, $args, $group ); ?>
230230
````
231231

232232
##### Parameters
@@ -240,7 +240,7 @@ Cancel the next occurrence of a job.
240240
(null)
241241

242242

243-
#### Function Reference / `wc_next_scheduled_action()`
243+
#### Function Reference / `as_next_scheduled_action()`
244244

245245
##### Description
246246

@@ -249,7 +249,7 @@ Returns the next timestamp for a scheduled action.
249249
##### Usage
250250

251251
```php
252-
<?php wc_next_scheduled_action( $hook, $args, $group ); ?>
252+
<?php as_next_scheduled_action( $hook, $args, $group ); ?>
253253
````
254254

255255
##### Parameters
@@ -263,7 +263,7 @@ Returns the next timestamp for a scheduled action.
263263
(integer|boolean) The timestamp for the next occurrence, or false if nothing was found.
264264

265265

266-
#### Function Reference / `wc_get_scheduled_actions()`
266+
#### Function Reference / `as_get_scheduled_actions()`
267267

268268
##### Description
269269

@@ -272,7 +272,7 @@ Find scheduled actions.
272272
##### Usage
273273

274274
```php
275-
<?php wc_get_scheduled_actions( $args, $return_format ); ?>
275+
<?php as_get_scheduled_actions( $args, $return_format ); ?>
276276
````
277277

278278
##### Parameters

classes/ActionScheduler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public static function init( $plugin_file ) {
9797

9898
require_once( self::plugin_path('functions.php') );
9999

100+
if ( apply_filters( 'action_scheduler_load_deprecated_functions', true ) ) {
101+
require_once( self::plugin_path('deprecated/functions.php') );
102+
}
103+
100104
if ( defined( 'WP_CLI' ) && WP_CLI ) {
101105
WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_Scheduler_command' );
102106
}

deprecated/functions.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
/**
4+
* Deprecated API functions for scheduling actions
5+
*
6+
* Functions with the wc prefix were deprecated to avoid confusion with
7+
* Action Scheduler being included in WooCommerce core, and it providing
8+
* a different set of APIs for working with the action queue.
9+
*/
10+
11+
/**
12+
* Schedule an action to run one time
13+
*
14+
* @param int $timestamp When the job will run
15+
* @param string $hook The hook to trigger
16+
* @param array $args Arguments to pass when the hook triggers
17+
* @param string $group The group to assign this job to
18+
*
19+
* @return string The job ID
20+
*/
21+
function wc_schedule_single_action( $timestamp, $hook, $args = array(), $group = '' ) {
22+
_deprecated_function( __FUNCTION__, '2.1.0', 'as_schedule_single_action()' );
23+
return as_schedule_single_action( $timestamp, $hook, $args, $group );
24+
}
25+
26+
/**
27+
* Schedule a recurring action
28+
*
29+
* @param int $timestamp When the first instance of the job will run
30+
* @param int $interval_in_seconds How long to wait between runs
31+
* @param string $hook The hook to trigger
32+
* @param array $args Arguments to pass when the hook triggers
33+
* @param string $group The group to assign this job to
34+
*
35+
* @deprecated 2.1.0
36+
*
37+
* @return string The job ID
38+
*/
39+
function wc_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) {
40+
_deprecated_function( __FUNCTION__, '2.1.0', 'as_schedule_recurring_action()' );
41+
return as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group );
42+
}
43+
44+
/**
45+
* Schedule an action that recurs on a cron-like schedule.
46+
*
47+
* @param int $timestamp The schedule will start on or after this time
48+
* @param string $schedule A cron-link schedule string
49+
* @see http://en.wikipedia.org/wiki/Cron
50+
* * * * * * *
51+
* ┬ ┬ ┬ ┬ ┬ ┬
52+
* | | | | | |
53+
* | | | | | + year [optional]
54+
* | | | | +----- day of week (0 - 7) (Sunday=0 or 7)
55+
* | | | +---------- month (1 - 12)
56+
* | | +--------------- day of month (1 - 31)
57+
* | +-------------------- hour (0 - 23)
58+
* +------------------------- min (0 - 59)
59+
* @param string $hook The hook to trigger
60+
* @param array $args Arguments to pass when the hook triggers
61+
* @param string $group The group to assign this job to
62+
*
63+
* @deprecated 2.1.0
64+
*
65+
* @return string The job ID
66+
*/
67+
function wc_schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '' ) {
68+
_deprecated_function( __FUNCTION__, '2.1.0', 'as_schedule_cron_action()' );
69+
return as_schedule_cron_action( $timestamp, $schedule, $hook, $args, $group );
70+
}
71+
72+
/**
73+
* Cancel the next occurrence of a job.
74+
*
75+
* @param string $hook The hook that the job will trigger
76+
* @param array $args Args that would have been passed to the job
77+
* @param string $group
78+
*
79+
* @deprecated 2.1.0
80+
*/
81+
function wc_unschedule_action( $hook, $args = array(), $group = '' ) {
82+
_deprecated_function( __FUNCTION__, '2.1.0', 'as_unschedule_action()' );
83+
as_unschedule_action( $hook, $args, $group );
84+
}
85+
86+
/**
87+
* @param string $hook
88+
* @param array $args
89+
* @param string $group
90+
*
91+
* @deprecated 2.1.0
92+
*
93+
* @return int|bool The timestamp for the next occurrence, or false if nothing was found
94+
*/
95+
function wc_next_scheduled_action( $hook, $args = NULL, $group = '' ) {
96+
_deprecated_function( __FUNCTION__, '2.1.0', 'as_next_scheduled_action()' );
97+
return as_next_scheduled_action( $hook, $args, $group );
98+
}
99+
100+
/**
101+
* Find scheduled actions
102+
*
103+
* @param array $args Possible arguments, with their default values:
104+
* 'hook' => '' - the name of the action that will be triggered
105+
* 'args' => NULL - the args array that will be passed with the action
106+
* 'date' => NULL - the scheduled date of the action. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
107+
* 'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '='
108+
* 'modified' => NULL - the date the action was last updated. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
109+
* 'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '='
110+
* 'group' => '' - the group the action belongs to
111+
* 'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING
112+
* 'claimed' => NULL - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID
113+
* 'per_page' => 5 - Number of results to return
114+
* 'offset' => 0
115+
* 'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', or 'date'
116+
* 'order' => 'ASC'
117+
* @param string $return_format OBJECT, ARRAY_A, or ids
118+
*
119+
* @deprecated 2.1.0
120+
*
121+
* @return array
122+
*/
123+
function wc_get_scheduled_actions( $args = array(), $return_format = OBJECT ) {
124+
_deprecated_function( __FUNCTION__, '2.1.0', 'as_get_scheduled_actions()' );
125+
return as_get_scheduled_actions( $args, $return_format );
126+
}

functions.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* @return string The job ID
1616
*/
17-
function wc_schedule_single_action( $timestamp, $hook, $args = array(), $group = '' ) {
17+
function as_schedule_single_action( $timestamp, $hook, $args = array(), $group = '' ) {
1818
return ActionScheduler::factory()->single( $hook, $args, $timestamp, $group );
1919
}
2020

@@ -29,7 +29,7 @@ function wc_schedule_single_action( $timestamp, $hook, $args = array(), $group =
2929
*
3030
* @return string The job ID
3131
*/
32-
function wc_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) {
32+
function as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) {
3333
return ActionScheduler::factory()->recurring( $hook, $args, $timestamp, $interval_in_seconds, $group );
3434
}
3535

@@ -54,7 +54,7 @@ function wc_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook,
5454
*
5555
* @return string The job ID
5656
*/
57-
function wc_schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '' ) {
57+
function as_schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '' ) {
5858
return ActionScheduler::factory()->cron( $hook, $args, $timestamp, $schedule, $group );
5959
}
6060

@@ -65,7 +65,7 @@ function wc_schedule_cron_action( $timestamp, $schedule, $hook, $args = array(),
6565
* @param array $args Args that would have been passed to the job
6666
* @param string $group
6767
*/
68-
function wc_unschedule_action( $hook, $args = array(), $group = '' ) {
68+
function as_unschedule_action( $hook, $args = array(), $group = '' ) {
6969
$params = array();
7070
if ( is_array($args) ) {
7171
$params['args'] = $args;
@@ -88,7 +88,7 @@ function wc_unschedule_action( $hook, $args = array(), $group = '' ) {
8888
*
8989
* @return int|bool The timestamp for the next occurrence, or false if nothing was found
9090
*/
91-
function wc_next_scheduled_action( $hook, $args = NULL, $group = '' ) {
91+
function as_next_scheduled_action( $hook, $args = NULL, $group = '' ) {
9292
$params = array();
9393
if ( is_array($args) ) {
9494
$params['args'] = $args;
@@ -130,7 +130,7 @@ function wc_next_scheduled_action( $hook, $args = NULL, $group = '' ) {
130130
*
131131
* @return array
132132
*/
133-
function wc_get_scheduled_actions( $args = array(), $return_format = OBJECT ) {
133+
function as_get_scheduled_actions( $args = array(), $return_format = OBJECT ) {
134134
$store = ActionScheduler::store();
135135
foreach ( array('date', 'modified') as $key ) {
136136
if ( isset($args[$key]) ) {

tests/phpunit/logging/ActionScheduler_wpCommentLogger_Test.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function test_default_logger() {
1212
}
1313

1414
public function test_add_log_entry() {
15-
$action_id = wc_schedule_single_action( time(), 'a hook' );
15+
$action_id = as_schedule_single_action( time(), 'a hook' );
1616
$logger = ActionScheduler::logger();
1717
$message = 'Logging that something happened';
1818
$log_id = $logger->log( $action_id, $message );
@@ -42,7 +42,7 @@ public function test_erroneous_entry_id() {
4242
}
4343

4444
public function test_storage_comments() {
45-
$action_id = wc_schedule_single_action( time(), 'a hook' );
45+
$action_id = as_schedule_single_action( time(), 'a hook' );
4646
$logger = ActionScheduler::logger();
4747
$logs = $logger->get_logs( $action_id );
4848
$expected = new ActionScheduler_LogEntry( $action_id, 'action created' );
@@ -62,7 +62,7 @@ protected function log_entry_to_array( $logs ) {
6262
}
6363

6464
public function test_execution_comments() {
65-
$action_id = wc_schedule_single_action( time(), 'a hook' );
65+
$action_id = as_schedule_single_action( time(), 'a hook' );
6666
$logger = ActionScheduler::logger();
6767
$started = new ActionScheduler_LogEntry( $action_id, 'action started' );
6868
$finished = new ActionScheduler_LogEntry( $action_id, 'action complete' );
@@ -78,7 +78,7 @@ public function test_execution_comments() {
7878
public function test_failed_execution_comments() {
7979
$hook = md5(rand());
8080
add_action( $hook, array( $this, '_a_hook_callback_that_throws_an_exception' ) );
81-
$action_id = wc_schedule_single_action( time(), $hook );
81+
$action_id = as_schedule_single_action( time(), $hook );
8282
$logger = ActionScheduler::logger();
8383
$started = new ActionScheduler_LogEntry( $action_id, 'action started' );
8484
$finished = new ActionScheduler_LogEntry( $action_id, 'action complete' );
@@ -95,7 +95,7 @@ public function test_failed_execution_comments() {
9595

9696
public function test_fatal_error_comments() {
9797
$hook = md5(rand());
98-
$action_id = wc_schedule_single_action( time(), $hook );
98+
$action_id = as_schedule_single_action( time(), $hook );
9999
$logger = ActionScheduler::logger();
100100
do_action( 'action_scheduler_unexpected_shutdown', $action_id, array(
101101
'type' => E_ERROR,
@@ -115,8 +115,8 @@ public function test_fatal_error_comments() {
115115
}
116116

117117
public function test_canceled_action_comments() {
118-
$action_id = wc_schedule_single_action( time(), 'a hook' );
119-
wc_unschedule_action( 'a hook' );
118+
$action_id = as_schedule_single_action( time(), 'a hook' );
119+
as_unschedule_action( 'a hook' );
120120
$logger = ActionScheduler::logger();
121121
$logs = $logger->get_logs( $action_id );
122122
$expected = new ActionScheduler_LogEntry( $action_id, 'action canceled' );
@@ -143,7 +143,7 @@ public function test_filtering_of_get_comments() {
143143
$this->assertEquals( $comment_id, $comments[0]->comment_ID );
144144

145145

146-
$action_id = wc_schedule_single_action( time(), 'a hook' );
146+
$action_id = as_schedule_single_action( time(), 'a hook' );
147147
$logger = ActionScheduler::logger();
148148
$message = 'Logging that something happened';
149149
$log_id = $logger->log( $action_id, $message );

0 commit comments

Comments
 (0)