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
b7a1650a
Commit
b7a1650a
authored
7 years ago
by
Erick Hitter
Browse files
Options
Downloads
Patches
Plain Diff
Rough approach to run events and log results
parent
f2808dd0
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
includes/functions.php
+28
-14
28 additions, 14 deletions
includes/functions.php
includes/run.php
+29
-6
29 additions, 6 deletions
includes/run.php
with
57 additions
and
20 deletions
includes/functions.php
+
28
−
14
View file @
b7a1650a
...
...
@@ -3,13 +3,13 @@
namespace
Automattic\WP\WP_CLI_Cron_Control_Offload
;
/**
* Check if
sub
command is allowed
* Check if command is allowed
*
* @param string $
sub
command
* @param string $command
* @return bool
*/
function
is_
sub
command_allowed
(
$
sub
command
)
{
return
in_array
(
$
sub
command
,
get_command_whitelist
(),
true
)
&&
!
in_array
(
$
sub
command
,
get_command_blacklist
(),
true
);
function
is_command_allowed
(
$command
)
{
return
in_array
(
$command
,
get_command_whitelist
(),
true
)
&&
!
in_array
(
$command
,
get_command_blacklist
(),
true
);
}
/**
...
...
@@ -23,6 +23,8 @@ function get_command_whitelist() {
'cache'
,
'cap'
,
'comment'
,
'cron-control'
,
'cron-control-fixers'
,
'media'
,
'menu'
,
'network'
,
...
...
@@ -80,7 +82,7 @@ function schedule_cli_command( $args ) {
return
$event_args
;
}
$scheduled
=
wp_schedule_single_event
(
strtotime
(
'+30 seconds'
),
ACTION
,
$event_args
);
$scheduled
=
wp_schedule_single_event
(
strtotime
(
'+30 seconds'
),
ACTION
,
array
(
'command'
=>
$event_args
)
);
return
false
!==
$scheduled
;
}
...
...
@@ -88,21 +90,33 @@ function schedule_cli_command( $args ) {
/**
* Validate WP-CLI command to be scheduled
*
* @param
array
$args
* @param
string
$args
* @return array|\WP_Error
*/
function
validate_args
(
$args
)
{
$validated_args
=
array
();
// Strip `wp` if included
if
(
0
===
stripos
(
$args
,
'wp'
)
)
{
$args
=
trim
(
substr
(
$args
,
2
)
);
}
// Block disallowed commands
$command
=
explode
(
' '
,
$args
);
$command
=
array_shift
(
$command
);
if
(
!
is_command_allowed
(
$command
)
)
{
return
new
\WP_Error
(
"
$command
not allowed"
);
}
// TODO: validate
// TODO: strip leading "wp"
// TODO: check first positional argument against `is_command_allowed()`
// Don't worry about the user WP-CLI runs as
if
(
false
===
stripos
(
$args
,
'--allow-root'
)
)
{
$args
.
=
' --allow-root'
;
}
$validated_args
[
'command'
]
=
$args
;
// TODO: validate further
if
(
empty
(
$validated_args
)
)
{
return
new
\WP_Error
(
'Arguments could not be parsed for validation.'
);
// Nothing to run
if
(
empty
(
$args
)
)
{
return
new
\WP_Error
(
'Invalid command provided'
);
}
return
$
validated_
args
;
return
$args
;
}
This diff is collapsed.
Click to expand it.
includes/run.php
+
29
−
6
View file @
b7a1650a
...
...
@@ -5,17 +5,40 @@ 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
* @param
string $command
* @return null
*/
function
run_event
(
$
args
)
{
function
run_event
(
$
command
)
{
if
(
!
defined
(
'WP_CLI'
)
||
!
\WP_CLI
)
{
// 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
;
trigger_error
(
'Attempted to run event without WP-CLI loaded.
(
'
.
var_export
(
$
command
,
true
)
.
')'
,
E_USER_WARNING
);
return
;
}
// TODO: run event, sending output to error log
trigger_error
(
var_export
(
$args
,
true
),
E_USER_NOTICE
);
if
(
!
validate_args
(
$command
)
)
{
trigger_error
(
'Attempted to run blocked WP-CLI command. ('
.
var_export
(
$command
,
true
)
.
')'
,
E_USER_WARNING
);
return
;
}
// TODO: time event execution
$output
=
\WP_CLI
::
runcommand
(
$command
,
array
(
'return'
=>
'all'
,
)
);
// Command failed
if
(
!
is_object
(
$output
)
||
is_wp_error
(
$output
)
)
{
trigger_error
(
'WP-CLI command failed. ('
.
var_export
(
$command
,
true
)
.
')'
,
E_USER_WARNING
);
trigger_error
(
var_export
(
$output
,
true
),
E_USER_WARNING
);
return
;
}
// On success, reformat response for logging
$output
->
command
=
$command
;
$output
=
var_export
(
$output
,
true
);
$output
=
ACTION
.
":
\n
{
$output
}
"
;
error_log
(
$output
);
}
add_action
(
ACTION
,
__NAMESPACE__
.
'\run_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