Last active
May 30, 2018 15:13
-
-
Save Shelob9/823cb8bddbd9777862fa8ce5994df34c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Whenever a post type is registered, ensure we're hooked into it's WP REST API response. | |
* | |
* @param string $post_type The newly registered post type. | |
* @return string That same post type. | |
*/ | |
function gutenberg_register_post_prepare_functions( $post_type ) { | |
add_filter( "rest_prepare_{$post_type}", 'gutenberg_add_permalink_template_to_posts', 10, 3 ); | |
add_filter( "rest_prepare_{$post_type}", 'gutenberg_add_block_format_to_post_content', 10, 3 ); | |
add_filter( "rest_prepare_{$post_type}", 'gutenberg_add_target_schema_to_links', 10, 3 ); | |
add_filter( "rest_{$post_type}_collection_params", 'gutenberg_filter_post_collection_parameters', 10, 2 ); | |
add_filter( "rest_{$post_type}_query", 'gutenberg_filter_post_query_arguments', 10, 2 ); | |
return $post_type; | |
} | |
add_filter( 'registered_post_type', 'gutenberg_register_post_prepare_functions' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch\Features; | |
use CalderaLearn\RestSearch\ModifyQueryArgsContract; | |
use CalderaLearn\RestSearch\ModifySchemaContract; | |
/** | |
* Class Search | |
* @package CalderaLearn\RestSearch\Features | |
*/ | |
class Search | |
{ | |
/** | |
* @var ModifyQueryArgsContract | |
*/ | |
protected $argsModify; | |
/** | |
* @var ModifySchemaContract | |
*/ | |
protected $schemaModify; | |
/** | |
* Search constructor. | |
* @param ModifyQueryArgsContract $argsModify | |
* @param ModifySchemaContract $schemaModify | |
*/ | |
public function __construct(ModifyQueryArgsContract $argsModify, ModifySchemaContract $schemaModify){} | |
/** | |
* Get underlying ModifyQueryArgsContract | |
* | |
* @return ModifyQueryArgsContract | |
*/ | |
public function getArgsModifier(){} | |
/** | |
* Get underlying ModifySchemaContract | |
* | |
* @return ModifySchemaContract | |
*/ | |
public function getSchemaModifier(){} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch\Features; | |
class Factory | |
{ | |
/** | |
* Create Search system | |
* | |
* @param array $argumentsToAdd | |
* @param array $postTypesToSupport | |
* @return Search | |
*/ | |
public static function search(array $argumentsToAdd, array $postTypesToSupport){} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch\Tests\Unit; | |
use CalderaLearn\RestSearch\Features\Factory; | |
class FeatureFactoryTest extends TestCase | |
{ | |
/** | |
* Test setting up system using factory | |
* | |
* @covers \CalderaLearn\RestSearch\Features\Factory::search() | |
*/ | |
public function testArgs() | |
{ | |
$schema = [ | |
'default' => 'post', | |
'description' => __('Post type(s) for search query'), | |
'type' => 'array', | |
//Limit to public post types and allow query by rest base | |
'items' => | |
[ | |
'enum' => [ | |
'posts', | |
'pages' | |
], | |
'type' => 'string', | |
], | |
]; | |
$args = [ | |
'post_type' => [ | |
'schema' => $schema, | |
'default' => 'post', | |
] | |
]; | |
$search = Factory::search($args, [ 'post' ]); | |
$this->assertSame( | |
[ | |
'post_type' => $schema | |
], | |
$search->getSchemaModifier()->getAdditionalSchemaArguments() | |
); | |
$this->assertSame( | |
[ 'post_type' => 'post' ], | |
$search->getArgsModifier()->getAdditionalQueryArguments() | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch; | |
/** | |
* Interface DoesFilterContract | |
* | |
* Interfaces for classes that add filters. | |
*/ | |
interface DoesFilterContract | |
{ | |
/** | |
* Add the filter | |
*/ | |
public function addHook(); | |
/** | |
* Remove the filter | |
*/ | |
public function removeHook(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch\Features; | |
use CalderaLearn\RestSearch\DoesFilterContract; | |
use CalderaLearn\RestSearch\ModifyQueryArgsContract; | |
use CalderaLearn\RestSearch\ModifySchemaContract; | |
/** | |
* Class Search | |
* @package CalderaLearn\RestSearch\Features | |
*/ | |
class Search implements DoesFilterContract | |
{ | |
/** | |
* @var ModifyQueryArgsContract | |
*/ | |
protected $argsModify; | |
/** | |
* @var ModifySchemaContract | |
*/ | |
protected $schemaModify; | |
/** | |
* Search constructor. | |
* @param ModifyQueryArgsContract $argsModify | |
* @param ModifySchemaContract $schemaModify | |
*/ | |
public function __construct(ModifyQueryArgsContract $argsModify, ModifySchemaContract $schemaModify) | |
{} | |
/** @inheritdoc */ | |
public function addHook() | |
{} | |
/** @inheritdoc */ | |
public function removeHook() | |
{} | |
/** | |
* Get underlying ModifyQueryArgsContract | |
* | |
* @return ModifyQueryArgsContract | |
*/ | |
public function getArgsModifier(){} | |
/** | |
* Get underlying ModifySchemaContract | |
* | |
* @return ModifySchemaContract | |
*/ | |
public function getSchemaModifier(){} | |
/** | |
* Manages interaction of plugins API and system for post type route schema changes | |
* | |
* @param array $query_params | |
* @param \WP_Post_Type $post_type | |
* @return array | |
*/ | |
public function filterSchema(array $query_params, \WP_Post_Type $post_type){} | |
/** | |
* Manages interaction of plugins API and system for post type route WP_Query arg whitelist changes | |
* | |
* @param array $args | |
* @param \WP_REST_Request $request | |
* @return array | |
*/ | |
public function filterQueryArgs(array $args, \WP_REST_Request $request){} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch\Tests\Unit; | |
use CalderaLearn\RestSearch\Features\Search; | |
use CalderaLearn\RestSearch\Tests\Mock\SchemaModifierImplementation; | |
class SearchFeatureGeneratorTest extends TestCase | |
{ | |
/** | |
* | |
* @covers \CalderaLearn\RestSearch\Features\Search::__construct() | |
* @covers \CalderaLearn\RestSearch\Features\Search::getSchemaModifier() | |
* @covers \CalderaLearn\RestSearch\Features\Search::getArgsModifier() | |
*/ | |
public function testSetModifiers() | |
{ | |
$schemaModifer = new SchemaModifierImplementation(); | |
$queryArgModifier = new QueryArgModifierImplementation(); | |
$search = new Search($queryArgModifier, $schemaModifer); | |
$this->assertSame($schemaModifer, $search->getSchemaModifier()); | |
$this->assertSame($queryArgModifier, $search->getArgsModifier()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Test that schema is filtered properly | |
* | |
* @covers \CalderaLearn\RestSearch\Features\Search::filterSchema() | |
* @covers \CalderaLearn\RestSearch\Features\Search::getSchemaModifier() | |
*/ | |
public function testFilterSchema() | |
{ | |
$schemaModifer = new SchemaModifierImplementation(); | |
$queryArgModifier = new QueryArgModifierImplementation(); | |
$search = new Search($queryArgModifier, $schemaModifer); | |
$schemaModifer->setShouldFilter(true); | |
$wpPostTypeMock = \Mockery::mock('WP_Post_Type'); | |
$wpPostTypeMock->name = 'post'; | |
$this->assertTrue($schemaModifer->shouldFilter($wpPostTypeMock)); | |
$this->assertTrue($search->getSchemaModifier()->shouldFilter($wpPostTypeMock)); | |
$otherArgs = [ | |
'page' => [ | |
'default' => 1, | |
'type' => 'integer' | |
] | |
]; | |
$expectArgs = array_merge($otherArgs, $schemaModifer->getAdditionalSchemaArguments()); | |
$args = $search->filterSchema($otherArgs, $wpPostTypeMock); | |
$this->assertEquals($expectArgs, $args); | |
} | |
/** | |
* Test that filter args are filtered correctly | |
* | |
* @since 1.7.0 | |
*/ | |
public function testFilterQueryArgs() | |
{ | |
$schemaModifer = new SchemaModifierImplementation(); | |
$queryArgModifier = new QueryArgModifierImplementation(); | |
$search = new Search($queryArgModifier, $schemaModifer); | |
$queryArgModifier->setShouldFilter(true); | |
$wpPostTypeMock = \Mockery::mock('WP_Post_Type'); | |
$wpPostTypeMock->name = 'post'; | |
$this->assertTrue($queryArgModifier->shouldFilter($wpPostTypeMock)); | |
$this->assertTrue($search->getArgsModifier()->shouldFilter($wpPostTypeMock)); | |
$otherArgs = [ | |
'page' => [ | |
'default' => 1, | |
'type' => 'integer' | |
] | |
]; | |
$expectArgs = array_merge($otherArgs, $queryArgModifier->getAdditionalQueryArguments()); | |
$wpRestRequest = \Mockery::mock('WP_REST_Request'); | |
$args = $search->filterQueryArgs($otherArgs, $wpRestRequest); | |
$this->assertEquals($expectArgs, $args); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch; | |
abstract class FilterQueryArgs implements ModifyQueryArgsContract | |
{ | |
use UsesPreparedPostTypes; | |
/** | |
* @var \WP_REST_Request | |
*/ | |
private $request; | |
/** | |
* @return \WP_REST_Request | |
*/ | |
protected function getRequest() : \WP_REST_Request | |
{ | |
return $this->request; | |
} | |
/** | |
* @param \WP_REST_Request $request | |
* @return $this | |
*/ | |
protected function setRequest(\WP_REST_Request $request) | |
{ | |
$this->request = $request; | |
return $this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch; | |
abstract class FilterSchema implements ModifySchemaContract | |
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A WP_Query with overidden query method | |
*/ | |
$query = new class ( $args ) extends WP_Query { | |
/** @inheritdoc */ | |
public function query( $query ) | |
{ | |
//All your query are belong to us. | |
return []; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch; | |
/** | |
* Class ModifySchema | |
* | |
* Modifies the REST API route schema so it has an argument "post_type" | |
* | |
* | |
* @package ExamplePlugin | |
*/ | |
class ModifySchema | |
{ | |
use UsesPreparedPostTypes; | |
/** | |
* The name of the extra argument we are adding to post type routes | |
*/ | |
const ARGNAME = 'post_type'; | |
/** | |
* Add post_type to schema | |
* | |
* @uses ""rest_{$postType}_collection_params" action | |
* | |
* @param array $query_params JSON Schema-formatted collection parameters. | |
* @param \WP_Post_Type $post_type Post type object. | |
* | |
* @return array | |
*/ | |
public function filterSchema($query_params, $post_type) | |
{ | |
if ($this->shouldFilter($post_type)) { | |
$query_params[self::ARGNAME] = [ | |
[ | |
'default' => PostType::RESTBASE, | |
'description' => __('Post type(s) for search query'), | |
'type' => 'array', | |
//Limit to public post types and allow query by rest base | |
'items' => | |
[ | |
'enum' => $this->preparedPostTypes->getPostTypeRestBases(), | |
'type' => 'string', | |
], | |
] | |
]; | |
} | |
return $query_params; | |
} | |
/** | |
* Check if this post type's schema should be filtered | |
* | |
* @param \WP_Post_Type $WP_Post_Type | |
* @return bool | |
*/ | |
public function shouldFilter(\WP_Post_Type $WP_Post_Type): bool | |
{ | |
return PostType::SLUG === $WP_Post_Type->name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$schemaModify = new class( $postTypesToSupport ) extends FilterSchema {} | |
$queryModify = new class( $postTypesToSupport ) extends FilterQueryArgs {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$schemaModify = new class( $postTypesToSupport ) extends FilterSchema { | |
/** | |
* @var array | |
*/ | |
protected $postTypesToSupport = []; | |
public function __construct(array $postTypesToSupport) | |
{ | |
$this->postTypesToSupport = $postTypesToSupport; | |
} | |
} | |
$queryModify = new class( $postTypesToSupport ) extends FilterQueryArgs { | |
/** | |
* @var array | |
*/ | |
protected $postTypesToSupport = []; | |
public function __construct(array $postTypesToSupport) | |
{ | |
$this->postTypesToSupport = $postTypesToSupport; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch\Features; | |
use CalderaLearn\RestSearch\ContentGetter\ContentGetterContract; | |
use CalderaLearn\RestSearch\FilterQueryArgs; | |
use CalderaLearn\RestSearch\FilterSchema; | |
use CalderaLearn\RestSearch\ModifySchemaContract; | |
class Factory | |
{ | |
/** | |
* Create Search system | |
* | |
* @param array $argumentsToAdd | |
* @param array $postTypesToSupport | |
* @return Search | |
*/ | |
public static function search(array $argumentsToAdd, array $postTypesToSupport) | |
{ | |
foreach ($argumentsToAdd as $argumentName => $argument) { | |
$schemaModify = new class( $postTypesToSupport ) extends FilterSchema { | |
/** | |
* @var array | |
*/ | |
protected $arguments = []; | |
/** | |
* @var array | |
*/ | |
protected $postTypesToSupport = []; | |
public function __construct(array $postTypesToSupport) | |
{ | |
$this->postTypesToSupport = $postTypesToSupport; | |
} | |
public function addArgument(string $argumentName, array $argument = null) | |
{ | |
$this->arguments[$argumentName] = $argument; | |
return $this; | |
} | |
public function getAdditionalSchemaArguments(): array | |
{ | |
return $this->arguments; | |
} | |
public function shouldFilter(string $postTypeSlug): bool | |
{ | |
return in_array($postTypeSlug, $this->postTypesToSupport); | |
} | |
}; | |
$queryModify = new class( $postTypesToSupport ) extends FilterQueryArgs { | |
/** | |
* @var array | |
*/ | |
protected $arguments = []; | |
/** | |
* @var array | |
*/ | |
protected $postTypesToSupport = []; | |
public function __construct(array $postTypesToSupport) | |
{ | |
$this->postTypesToSupport = $postTypesToSupport; | |
} | |
public function addArgument(string $argumentName, $default) | |
{ | |
$this->arguments[$argumentName] = $default; | |
return $this; | |
} | |
public function getAdditionalQueryArguments(): array | |
{ | |
return $this->arguments; | |
} | |
public function shouldFilter(string $postTypeSlug): bool | |
{ | |
return in_array($postTypeSlug, $this->postTypesToSupport); | |
} | |
}; | |
$schemaModify->addArgument($argumentName, $argument['schema']); | |
$queryModify->addArgument($argumentName, $argument['default']); | |
return new Search($queryModify, $schemaModify); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch\Features; | |
use CalderaLearn\RestSearch\ModifyQueryArgsContract; | |
use CalderaLearn\RestSearch\ModifySchemaContract; | |
/** | |
* Class Search | |
* @package CalderaLearn\RestSearch\Features | |
*/ | |
class Search | |
{ | |
/** | |
* @var ModifyQueryArgsContract | |
*/ | |
protected $argsModify; | |
/** | |
* @var ModifySchemaContract | |
*/ | |
protected $schemaModify; | |
/** | |
* Search constructor. | |
* @param ModifyQueryArgsContract $argsModify | |
* @param ModifySchemaContract $schemaModify | |
*/ | |
public function __construct(ModifyQueryArgsContract $argsModify, ModifySchemaContract $schemaModify) | |
{ | |
$this->argsModify = $argsModify; | |
$this->schemaModify = $schemaModify; | |
} | |
/** | |
* Manages interaction of plugins API and system for post type route schema changes | |
* | |
* @param array $query_params | |
* @param \WP_Post_Type $post_type | |
* @return array | |
*/ | |
public function filterSchema(array $query_params, \WP_Post_Type $post_type) | |
{ | |
if ($this->schemaModify->shouldFilter($post_type)) { | |
$query_params = array_merge($query_params, $this->schemaModify->getAdditionalSchemaArguments()); | |
} | |
return $query_params; | |
} | |
/** | |
* Manages interaction of plugins API and system for post type route WP_Query arg whitelist changes | |
* | |
* @param array $args | |
* @param \WP_REST_Request $request | |
* @return array | |
*/ | |
public function filterQueryArgs(array $args, \WP_REST_Request $request) | |
{ | |
if ($this->argsModify->shouldFilter($request)) { | |
$args = array_merge($args, $this->argsModify->getAdditionalQueryArguments()); | |
} | |
return $args; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch; | |
interface ModifyQueryArgsContract | |
{ | |
/** | |
* Check if we should filter request args | |
* | |
* @param string $postTypeRestBase | |
* @return bool | |
*/ | |
public function shouldFilter(string $postTypeRestBase): bool; | |
/** | |
* Return the additional query arguments that should be added | |
* | |
* @return array | |
*/ | |
public function getAdditionalQueryArguments(): array; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch; | |
/** | |
* Class ModifySchema | |
* | |
* Modifies the REST API route schema so it has an argument "post_type" | |
* | |
* | |
* @package ExamplePlugin | |
*/ | |
interface ModifySchemaContract | |
{ | |
/** | |
* Check if this post type's schema should be filtered | |
* | |
* @param string $postTypeRestBase | |
* @return bool | |
*/ | |
public function shouldFilter(string $postTypeRestBase): bool; | |
/** | |
* Return the additional schema arguments that should be added | |
* | |
* @return array | |
*/ | |
public function getAdditionalSchemaArguments(): array; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch; | |
use CalderaLearn\RestSearch\ContentGetter\ContentGetterContract; | |
use WP_Query; | |
/** | |
* Class FilterWPQuery | |
* | |
* Changes WP_Query object during REST API requests | |
* | |
* @package CalderaLearn\RestSearch | |
*/ | |
class FilterWPQuery implements FiltersPreWPQuery | |
{ | |
/** | |
* Content Getter Implementation. | |
* | |
* @var ContentGetterContract | |
*/ | |
protected static $contentGetter; | |
/** | |
* Current REST request | |
* | |
* @var \WP_REST_Request | |
*/ | |
protected static $request; | |
/** | |
* Priority for filter | |
* | |
* @var int | |
*/ | |
protected static $filterPriority = 10; | |
/** | |
* Get or set search filter by binding a specific content getter implementation. | |
* | |
* @param ContentGetterContract $contentGetter Instance of the implementation. | |
* | |
* @return void | |
*/ | |
public static function setContentGetter(ContentGetterContract $contentGetter) | |
{ | |
static::$contentGetter = $contentGetter; | |
} | |
/** | |
* @return ContentGetterContract | |
*/ | |
public static function getContentGetter() | |
{ | |
return static::$contentGetter; | |
} | |
/** | |
* Filters the results of WP_Query objects. | |
* | |
* This callback demonstrates how to use a different way to set the posts that WP_Query returns. | |
* | |
* @uses "posts_pre_query" | |
* | |
* @param array|null $postsOrNull Array of posts or null. | |
* @param WP_Query $query Instance of the query. | |
* | |
* @return array Returns an array of WP_Post objects. | |
*/ | |
public static function filterPreQuery($postsOrNull, WP_Query $query) | |
{ | |
if (! static::shouldFilter($postsOrNull)) { | |
return $postsOrNull; | |
} | |
return static::getPosts($query); | |
} | |
/** @inheritdoc */ | |
public static function shouldFilter($postsOrNull): bool | |
{ | |
if (! is_null($postsOrNull)) { | |
return false; | |
} | |
return static::doingREST(); | |
} | |
/** | |
* Checks if WordPress is doing a REST request. | |
* | |
* @return bool | |
*/ | |
private static function doingREST(): bool | |
{ | |
return did_action('rest_api_init'); | |
} | |
/** @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 | |
{ | |
remove_filter('rest_pre_serve_request', [FilterWPQuery::class, 'captureRequest' ]); | |
return remove_filter('posts_pre_query', [FilterWPQuery::class, 'filterPreQuery'], static::$filterPriority); | |
} | |
/** @inheritdoc */ | |
public static function getFilterPriority(): int | |
{ | |
return static::$filterPriority; | |
} | |
/** | |
* At "rest_pre_serve_request" get request object and store for later | |
* | |
* @param null|\WP_REST_Response $return | |
* @param null|\WP_REST_Response$result | |
* @param \WP_REST_Request $request | |
* @return null|\WP_REST_Response | |
*/ | |
public static function captureRequest($return, $result, $request) | |
{ | |
static::$request = $request; | |
return $return; | |
} | |
/** @inheritdoc */ | |
public static function getPosts(WP_Query $query): array | |
{ | |
$request = is_object(static::$request) ? static::$request : new \WP_REST_Request(); | |
return static::$contentGetter->getContent($query, $request); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public static function captureRequest( $return, $result, $request ) | |
{ | |
static::$request = $request; | |
return $return; | |
} | |
/** @inheritdoc */ | |
public static function getPosts(WP_Query $query): array | |
{ | |
$request = is_object( static::$request ) ? static::$request ? new \WP_REST_Request(); | |
return static::$contentGetter->getContent($query,$request); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch; | |
/** | |
* Class ModifySchema | |
* | |
* Modifies the REST API route schema so it has an argument "post_type" | |
* | |
* | |
* @package ExamplePlugin | |
*/ | |
interface ModifySchemaContract | |
{ | |
/** | |
* Return the additional schema arguments that should be added | |
* | |
* @return array | |
*/ | |
public function getAdditionalSchemaArguments(): array; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch; | |
/** | |
* Class ModifySchema | |
* | |
* Modifies the REST API route schema so it has an argument "post_type" | |
* | |
* | |
* @package ExamplePlugin | |
*/ | |
class ModifySchema implements ModifySchemaContract | |
{ | |
use UsesPreparedPostTypes; | |
/** | |
* The name of the extra argument we are adding to post type routes | |
*/ | |
const ARGNAME = 'post_type'; | |
/* @inheritdoc */ | |
public function getAdditionalSchemaArguments(): array | |
{ | |
return [ | |
self::ARGNAME => [ | |
[ | |
'default' => PostType::RESTBASE, | |
'description' => __('Post type(s) for search query'), | |
'type' => 'array', | |
//Limit to public post types and allow query by rest base | |
'items' => | |
[ | |
'enum' => $this->preparedPostTypes->getPostTypeRestBases(), | |
'type' => 'string', | |
], | |
] | |
] | |
]; | |
} | |
/* @inheritdoc */ | |
public function filterSchema($query_params, $post_type) | |
{ | |
if ($this->shouldFilter($post_type)) { | |
$query_params = array_merge($this->getArguments(), $query_params); | |
} | |
return $query_params; | |
} | |
/* @inheritdoc */ | |
public function shouldFilter(\WP_Post_Type $WP_Post_Type): bool | |
{ | |
return PostType::SLUG === $WP_Post_Type->name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch; | |
/** | |
* Class ModifySchema | |
* | |
* Modifies the REST API route schema so it has an argument "post_type" | |
* | |
* | |
* @package ExamplePlugin | |
*/ | |
interface ModifySchemaContract | |
{ | |
/** | |
* Add post_type to schema | |
* | |
* @uses ""rest_{$postType}_collection_params" action | |
* | |
* @param array $query_params JSON Schema-formatted collection parameters. | |
* @param \WP_Post_Type $post_type Post type object. | |
* | |
* @return array | |
*/ | |
public function filterSchema($query_params, $post_type); | |
/** | |
* Check if this post type's schema should be filtered | |
* | |
* @param \WP_Post_Type $WP_Post_Type | |
* @return bool | |
*/ | |
public function shouldFilter(\WP_Post_Type $WP_Post_Type): bool; | |
/** | |
* Return the additional schema arguments that should be added | |
* | |
* @return array | |
*/ | |
public function getAdditionalSchemaArguments(): array; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch; | |
/** | |
* Class ModifyQuery | |
* | |
* Modify WP_Query Args | |
* | |
* @package ExamplePlugin | |
*/ | |
class ModifyQueryArgs | |
{ | |
use UsesPreparedPostTypes; | |
/** | |
* Filter query args if needed | |
* | |
* @param array $args Key value array of query var to query value. | |
* @param \WP_REST_Request $request The request used. | |
* | |
* @return array | |
*/ | |
public function filterQueryArgs($args, $request) | |
{ | |
if ($this->shouldFilter($request)) { | |
add_filter('posts_pre_query', [FilterWPQuery::class, 'posts_pre_query'], 10, 2); | |
$args['post_type'] = $this->restBasesToPostTypeSlugs($request[ModifySchema::ARGNAME]); | |
} | |
return $args; | |
} | |
/** | |
* Check if we should filter request args | |
* | |
* @param \WP_REST_Request $request | |
* @return bool | |
*/ | |
public function shouldFilter(\WP_REST_Request $request): bool | |
{ | |
$attributes = $request->get_attributes(); | |
if (isset($attributes['args'][ModifySchema::ARGNAME])) { | |
if ($request->get_param(ModifySchema::ARGNAME)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Convert an array of rest bases to post type slugs | |
* | |
* @param array $postTypes | |
* @return array | |
*/ | |
public function restBasesToPostTypeSlugs(array $postTypes): array | |
{ | |
$postTypeSlugs = []; | |
foreach ($postTypes as $postTypeRestBase) { | |
if ($this->preparedPostTypes->restBaseToSlug($postTypeRestBase)) { | |
$postTypeSlugs[] = $this->preparedPostTypes->restBaseToSlug($postTypeRestBase); | |
} | |
} | |
return $postTypeSlugs; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch; | |
interface ModifyQueryArgsContract | |
{ | |
/** | |
* Filter query args if needed | |
* | |
* @param array $args Key value array of query var to query value. | |
* @param \WP_REST_Request $request The request used. | |
* | |
* @return array | |
*/ | |
public function filterQueryArgs($args, $request); | |
/** | |
* Check if we should filter request args | |
* | |
* @param \WP_REST_Request $request | |
* @return bool | |
*/ | |
public function shouldFilter(\WP_REST_Request $request): bool; | |
/** | |
* Return the additional query arguments that should be added | |
* | |
* @return array | |
*/ | |
public function getAdditionalQueryArguments(): array; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch; | |
/** | |
* Class ModifyQuery | |
* | |
* Modify WP_Query Args | |
* | |
* @package ExamplePlugin | |
*/ | |
class ModifyQueryArgs implements ModifyQueryArgsContract | |
{ | |
use UsesPreparedPostTypes; | |
/** | |
* @var \WP_REST_Request | |
*/ | |
protected $request; | |
/** @inheritdoc */ | |
public function getAdditionalQueryArguments(): array | |
{ | |
return [ | |
'post_type' => $this->restBasesToPostTypeSlugs($this->request[ModifySchema::ARGNAME]) | |
]; | |
} | |
/** @inheritdoc */ | |
public function filterQueryArgs($args, $request) | |
{ | |
if ($this->shouldFilter($request)) { | |
$this->request = $request; | |
add_filter('posts_pre_query', [FilterWPQuery::class, 'posts_pre_query'], 10, 2); | |
$args = array_merge($this->getAdditionalQueryArguments(), $args); | |
} | |
return $args; | |
} | |
/** @inheritdoc */ | |
public function shouldFilter(\WP_REST_Request $request): bool | |
{ | |
$attributes = $request->get_attributes(); | |
if (isset($attributes['args'][ModifySchema::ARGNAME])) { | |
if ($request->get_param(ModifySchema::ARGNAME)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Convert an array of rest bases to post type slugs | |
* | |
* @param array $postTypes | |
* @return array | |
*/ | |
public function restBasesToPostTypeSlugs(array $postTypes): array | |
{ | |
$postTypeSlugs = []; | |
foreach ($postTypes as $postTypeRestBase) { | |
if ($this->preparedPostTypes->restBaseToSlug($postTypeRestBase)) { | |
$postTypeSlugs[] = $this->preparedPostTypes->restBaseToSlug($postTypeRestBase); | |
} | |
} | |
return $postTypeSlugs; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CalderaLearn\RestSearch\Tests\Unit; | |
use CalderaLearn\RestSearch\Features\Factory; | |
class FeatureFactoryTest extends TestCase | |
{ | |
/** | |
* Test setting up system using factory | |
* | |
*/ | |
public function testArgs() | |
{ | |
$schema = [ | |
'default' => 'post', | |
'description' => __('Post type(s) for search query'), | |
'type' => 'array', | |
//Limit to public post types and allow query by rest base | |
'items' => | |
[ | |
'enum' => [ | |
'posts', | |
'pages' | |
], | |
'type' => 'string', | |
], | |
]; | |
$args = [ | |
'post_type' => [ | |
'schema' => $schema, | |
'default' => 'post', | |
] | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment