Commit cea92eb9 authored by Erick Hitter's avatar Erick Hitter
Browse files

Review feedback: introduce generic function to more-easily make multiple endpoint checks

parent 73367f48
......@@ -32,7 +32,8 @@ class Events extends Singleton {
*/
public function prepare_environment() {
// Limit to plugin's endpoints
if ( ! is_rest_endpoint_request( 'list' ) && ! is_rest_endpoint_request( 'run' ) ) {
$endpoint = get_endpoint_type();
if ( false === $endpoint ) {
return;
}
......@@ -40,7 +41,7 @@ class Events extends Singleton {
define( 'DOING_CRON', true );
// When running events, allow for long-running ones, and non-blocking trigger requests
if ( is_rest_endpoint_request( 'run' ) ) {
if ( 'run' === $endpoint ) {
ignore_user_abort( true );
set_time_limit( JOB_TIMEOUT_IN_MINUTES * MINUTE_IN_SECONDS );
}
......
......@@ -9,6 +9,53 @@ function is_internal_event( $action ) {
return Internal_Events::instance()->is_internal_event( $action );
}
/**
* Check which of the plugin's REST endpoints the current request is for, if any
*
* @return string|bool
*/
function get_endpoint_type() {
// Request won't change, so hold for the duration
static $endpoint = null;
if ( ! is_null( $endpoint ) ) {
return $endpoint;
}
// 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;
}
}
// Convert endpoint slug to its type
switch ( $endpoint_slug ) {
case REST_API::ENDPOINT_LIST :
$endpoint = 'list';
break;
case REST_API::ENDPOINT_RUN :
$endpoint = 'run';
break;
default :
$endpoint = false;
break;
}
return $endpoint;
}
/**
* Check if the current request is to one of the plugin's REST endpoints
*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment