Skip to content
Permalink
Browse files

caputure current REST API request using rest_pre_serve_request filter

  • Loading branch information...
Shelob9 committed May 28, 2018
1 parent e2e1719 commit 1bfce073049db26486503bcd2f1e392e32be7f6e
Showing with 19 additions and 2 deletions.
  1. +19 −2 src/FilterWPQuery.php
@@ -21,6 +21,13 @@ class FilterWPQuery implements FiltersPreWPQuery
*/
protected static $contentGetter;
/**
* Current REST request
*
* @var \WP_REST_Request
*/
protected static $request;
/**
* Priority for filter
*
@@ -84,13 +91,15 @@ private static function doingREST(): bool
/** @inheritdoc */
public static function addFilter(): bool
{
add_filter( 'rest_pre_serve_request', [FilterWPQuery::class, 'captureRequest' ] );
return add_filter('posts_pre_query', [FilterWPQuery::class, 'filterPreQuery'], static::$filterPriority, 2);
}
/** @inheritdoc */
public static function removeFilter(): bool
{
return remove_filter('posts_pre_query', [FilterWPQuery::class, 'filterPreQuery'], static::$filterPriority);
remove_filter( 'rest_pre_serve_request', [FilterWPQuery::class, 'captureRequest' ] );
return remove_filter('posts_pre_query', [FilterWPQuery::class, 'filterPreQuery'], static::$filterPriority);
}
/** @inheritdoc */
@@ -99,9 +108,17 @@ public static function getFilterPriority(): int
return static::$filterPriority;
}
public static function captureRequest( $return, $result, $request )
{
static::$request = $request;
return $return;
}
/** @inheritdoc */
public static function getPosts(WP_Query $query): array
{
return static::$contentGetter->getContent($query);
$request = is_object( static::$request ) ? static::$request ? new \WP_REST_Request();
return static::$contentGetter->getContent($query,$request);
}
}

0 comments on commit 1bfce07

Please sign in to comment.
You can’t perform that action at this time.