Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
WP Plugins
Cron-Control
Commits
da0f0991
Commit
da0f0991
authored
Apr 14, 2017
by
Erick Hitter
Committed by
GitHub
Apr 14, 2017
Browse files
Merge pull request #99 from Automattic/develop
Ensure `DOING_CRON` is set for all REST requests
parents
bcf0e85c
a2bb6b30
Changes
4
Hide whitespace changes
Inline
Side-by-side
includes/class-events-store.php
View file @
da0f0991
...
...
@@ -121,7 +121,7 @@ class Events_Store extends Singleton {
* Does not include front-end requests
*/
public
function
maybe_create_table_on_shutdown
()
{
if
(
!
is_admin
()
&&
!
is_rest_endpoint_request
(
'list'
)
)
{
if
(
!
is_admin
()
&&
!
is_rest_endpoint_request
(
REST_API
::
ENDPOINT_LIST
)
)
{
return
;
}
...
...
includes/class-events.php
View file @
da0f0991
...
...
@@ -31,13 +31,20 @@ class Events extends Singleton {
* This also runs before Core has parsed the request and set the \REST_REQUEST constant
*/
public
function
prepare_environment
()
{
if
(
!
is_rest_endpoint_request
(
'run'
)
)
{
// Limit to plugin's endpoints
$endpoint
=
get_endpoint_type
();
if
(
false
===
$endpoint
)
{
return
;
}
ignore_user_abort
(
true
);
set_time_limit
(
JOB_TIMEOUT_IN_MINUTES
*
MINUTE_IN_SECONDS
);
// Flag is used in many contexts, so should be set for all of our requests, regardless of the action
define
(
'DOING_CRON'
,
true
);
// When running events, allow for long-running ones, and non-blocking trigger requests
if
(
REST_API
::
ENDPOINT_RUN
===
$endpoint
)
{
ignore_user_abort
(
true
);
set_time_limit
(
JOB_TIMEOUT_IN_MINUTES
*
MINUTE_IN_SECONDS
);
}
}
/**
...
...
includes/functions.php
View file @
da0f0991
...
...
@@ -10,34 +10,46 @@ function is_internal_event( $action ) {
}
/**
* Check if the current request is to one of the plugin's REST endpoints
*
* @param string $type list|run
* Check which of the plugin's REST endpoints the current request is for, if any
*
* @return bool
* @return
string|
bool
*/
function
is_rest_endpoint_request
(
$type
=
'list'
)
{
// Which endpoint are we checking
$endpoint
=
null
;
switch
(
$type
)
{
case
'list'
:
$endpoint
=
REST_API
::
ENDPOINT_LIST
;
break
;
case
'run'
:
$endpoint
=
REST_API
::
ENDPOINT_RUN
;
break
;
function
get_endpoint_type
()
{
// Request won't change, so hold for the duration
static
$endpoint_slug
=
null
;
if
(
!
is_null
(
$endpoint_slug
)
)
{
return
$endpoint_slug
;
}
// No endpoint to check
if
(
is_null
(
$endpoint
)
)
{
return
false
;
// Determine request URL according to how Core does
$request
=
parse_request
();
// Search by our URL "prefix"
$namespace
=
sprintf
(
'%s/%s'
,
rest_get_url_prefix
(),
REST_API
::
API_NAMESPACE
);
// Check if any parts of the parse request are in our namespace
$endpoint_slug
=
false
;
foreach
(
$request
as
$req
)
{
if
(
0
===
stripos
(
$req
,
$namespace
)
)
{
$req_parts
=
explode
(
'/'
,
$req
);
$endpoint_slug
=
array_pop
(
$req_parts
);
break
;
}
}
// Build the full endpoint and check against the current request
$run_endpoint
=
sprintf
(
'%s/%s/%s'
,
rest_get_url_prefix
(),
REST_API
::
API_NAMESPACE
,
$endpoint
);
return
$endpoint_slug
;
}
return
in_array
(
$run_endpoint
,
parse_request
(),
true
);
/**
* Check if the current request is to one of the plugin's REST endpoints
*
* @param string $type Endpoint Constant from REST_API class to compare against
*
* @return bool
*/
function
is_rest_endpoint_request
(
$type
)
{
return
get_endpoint_type
()
===
$type
;
}
/**
...
...
includes/utils.php
View file @
da0f0991
...
...
@@ -54,6 +54,13 @@ function collapse_events_array( $events, $timestamp = null ) {
* We have occasion to check the request before Core has done so, such as when preparing the environment to run a cron job
*/
function
parse_request
()
{
// Hold onto this as it won't change during the request
static
$parsed_request
=
null
;
if
(
is_array
(
$parsed_request
)
)
{
return
$parsed_request
;
}
// Starting somewhere
$rewrite_index
=
'index.php'
;
/**
...
...
@@ -104,5 +111,7 @@ function parse_request() {
*/
// Return array of data about the request
return
compact
(
'requested_path'
,
'requested_file'
,
'self'
);
$parsed_request
=
compact
(
'requested_path'
,
'requested_file'
,
'self'
);
return
$parsed_request
;
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment