Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
wp-cli-cron-control-offload
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
WP Plugins
wp-cli-cron-control-offload
Commits
f2808dd0
Commit
f2808dd0
authored
7 years ago
by
Erick Hitter
Browse files
Options
Downloads
Patches
Plain Diff
Primative scaffolding to schedule a CLI command
parent
9d25b5df
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
includes/functions.php
+44
-4
44 additions, 4 deletions
includes/functions.php
includes/run.php
+6
-1
6 additions, 1 deletion
includes/run.php
includes/schedule.php
+5
-0
5 additions, 0 deletions
includes/schedule.php
with
55 additions
and
5 deletions
includes/functions.php
+
44
−
4
View file @
f2808dd0
...
...
@@ -9,7 +9,7 @@ namespace Automattic\WP\WP_CLI_Cron_Control_Offload;
* @return bool
*/
function
is_subcommand_allowed
(
$subcommand
)
{
return
in_array
(
$subcommand
,
get_
sub
command_whitelist
(),
true
)
&&
!
in_array
(
$subcommand
,
get_
sub
command_blacklist
(),
true
);
return
in_array
(
$subcommand
,
get_command_whitelist
(),
true
)
&&
!
in_array
(
$subcommand
,
get_command_blacklist
(),
true
);
}
/**
...
...
@@ -17,13 +17,12 @@ function is_subcommand_allowed( $subcommand ) {
*
* @return array
*/
function
get_
sub
command_whitelist
()
{
function
get_command_whitelist
()
{
// Supported built-in commands
$whitelist
=
array
(
'cache'
,
'cap'
,
'comment'
,
'import'
,
'media'
,
'menu'
,
'network'
,
...
...
@@ -52,7 +51,7 @@ function get_subcommand_whitelist() {
*
* @return array
*/
function
get_
sub
command_blacklist
()
{
function
get_command_blacklist
()
{
return
array
(
'cli'
,
'config'
,
...
...
@@ -60,9 +59,50 @@ function get_subcommand_blacklist() {
'cron'
,
'db'
,
'eval'
,
'export'
,
'eval-file'
,
'import'
,
'package'
,
'scaffold'
,
'server'
,
);
}
/**
* Create cron event for a given WP-CLI command
*
* return bool|\WP_Error
*/
function
schedule_cli_command
(
$args
)
{
$event_args
=
validate_args
(
$args
);
if
(
is_wp_error
(
$event_args
)
)
{
return
$event_args
;
}
$scheduled
=
wp_schedule_single_event
(
strtotime
(
'+30 seconds'
),
ACTION
,
$event_args
);
return
false
!==
$scheduled
;
}
/**
* Validate WP-CLI command to be scheduled
*
* @param array $args
* @return array|\WP_Error
*/
function
validate_args
(
$args
)
{
$validated_args
=
array
();
// TODO: validate
// TODO: strip leading "wp"
// TODO: check first positional argument against `is_command_allowed()`
$validated_args
[
'command'
]
=
$args
;
if
(
empty
(
$validated_args
)
)
{
return
new
\WP_Error
(
'Arguments could not be parsed for validation.'
);
}
return
$validated_args
;
}
This diff is collapsed.
Click to expand it.
includes/run.php
+
6
−
1
View file @
f2808dd0
...
...
@@ -3,14 +3,19 @@
namespace
Automattic\WP\WP_CLI_Cron_Control_Offload
;
/**
* Intended for non-interactive use, so all output ends up in the error log
*
* @param array $args
* @return null
*/
function
run_event
(
$args
)
{
if
(
!
defined
(
'WP_CLI'
)
||
!
\WP_CLI
)
{
trigger_error
(
'Attempted to run event without WP-CLI loaded. '
.
compact
(
$args
),
E_USER_WARNING
);
// TODO: reschedule at least once or twice
trigger_error
(
'Attempted to run event without WP-CLI loaded. '
.
var_export
(
$args
,
true
),
E_USER_WARNING
);
return
false
;
}
// TODO: run event, sending output to error log
trigger_error
(
var_export
(
$args
,
true
),
E_USER_NOTICE
);
}
add_action
(
ACTION
,
__NAMESPACE__
.
'\run_event'
);
This diff is collapsed.
Click to expand it.
includes/schedule.php
+
5
−
0
View file @
f2808dd0
...
...
@@ -2,3 +2,8 @@
namespace
Automattic\WP\WP_CLI_Cron_Control_Offload
;
if
(
!
defined
(
'WP_CLI'
)
||
!
\WP_CLI
)
{
return
false
;
}
// TODO: WP-CLI command to schedule an event
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment