Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move REST API endpoints to controllers #3650

Merged
merged 19 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/js/blocks/related-posts/Edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const RelatedPostsEdit = ({ attributes, context, setAttributes }) => {
const { postId = 0 } = context;

apiFetch({
path: addQueryArgs(`/wp/v2/posts/${postId}/related`, urlArgs),
path: addQueryArgs(`/elasticpress/v1/related-posts/${postId}`, urlArgs),
})
.then((posts) => {
setPosts(posts);
Expand Down
6 changes: 3 additions & 3 deletions assets/js/blocks/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ const selectors = {

const controls = {
GET_META_KEYS() {
return apiFetch({ path: 'elasticpress/v1/facets/meta/keys' });
return apiFetch({ path: 'elasticpress/v1/meta-keys' });
},
GET_TAXONOMIES() {
return apiFetch({ path: 'elasticpress/v1/facets/taxonomies' });
return apiFetch({ path: 'elasticpress/v1/taxonomies' });
},
GET_META_RANGE({ key }) {
const params = new URLSearchParams({ facet: key });

return apiFetch({
path: `/elasticpress/v1/facets/meta-range/block-preview?${params}`,
path: `/elasticpress/v1/meta-range?${params}`,
});
},
};
Expand Down
112 changes: 3 additions & 109 deletions includes/classes/Feature/Comments/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use ElasticPress\Indexable;
use ElasticPress\Indexables;
use ElasticPress\Utils;
use ElasticPress\REST;

/**
* Comments feature class
Expand Down Expand Up @@ -147,115 +148,8 @@ public function register_widget() {
* @since 3.6.0
*/
public function rest_api_init() {
register_rest_route(
'elasticpress/v1',
'comments',
[
'methods' => 'GET',
'callback' => [ $this, 'handle_comments_search' ],
'permission_callback' => '__return_true',
'args' => [
's' => [
'validate_callback' => function ( $param ) {
return ! empty( $param );
},
'required' => true,
],
'post_type' => [
'validate_callback' => function ( $param ) {
return ! empty( $param );
},
],
],
]
);
}

/**
* Handles the search for comments
*
* @since 3.6.0
* @param \WP_REST_Request $request Rest request
* @return array
*/
public function handle_comments_search( $request ) {
$search = $request->get_param( 's' );

if ( empty( $search ) ) {
return new \WP_Error( 400 );
}

$post_type_filter = explode( ',', $request->get_param( 'post_type' ) );
$searchable_post_types = array_filter(
Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types(),
function ( $post_type ) {
return post_type_supports( $post_type, 'comments' );
}
);

if ( ! empty( $post_type_filter ) && is_array( $searchable_post_types ) ) {
$post_type_filter = array_intersect( $post_type_filter, $searchable_post_types );
}

$default_args = [
'status' => 'approve',
'search' => $search,
'type' => Indexables::factory()->get( 'comment' )->get_indexable_comment_types(),
'post_type' => empty( $post_type_filter ) ? $searchable_post_types : $post_type_filter,
'post_status' => 'publish',
'number' => 5,
];

/**
* Filter to args used in WP_Comment_Query in Widget Search Comment
*
* @hook ep_comment_search_widget_args
* @since 3.6.0
* @param {array} $default_args Defaults args
* @return {array} New value
*/
$args = apply_filters( 'ep_comment_search_widget_args', $default_args );

/**
* Fires before the comment query is executed.
*
* @hook ep_comment_pre_search_widget
* @since 3.6.0
* @param {array} $args Args passed to `WP_Comment_Query`.
* @param {WP_REST_Request} $request Rest request.
*/
do_action( 'ep_comment_pre_search_widget', $args, $request );

$comment_query = new \WP_Comment_Query( $args );

/**
* Fires after the comment query is executed.
*
* @hook ep_comment_after_search_widget
* @since 3.6.0
* @param {WP_Comment_Query} $comment_query WP_Comment_Query object.
* @param {WP_REST_Request} $request Rest request.
*/
do_action( 'ep_comment_after_search_widget', $comment_query, $request );

$return = [];
foreach ( $comment_query->comments as $comment ) {
$return[ $comment->comment_ID ] = [
'id' => $comment->comment_ID,
'content' => $comment->comment_content,
'link' => get_comment_link( $comment ),
];
}

/**
* Filters the comments response
*
* @hook ep_comment_search_widget_response
* @since 3.6.0
* @param {array} $return The result of fetched comments.
* @return {array} New value
*/
return apply_filters( 'ep_comment_search_widget_response', $return );
$controller = new REST\Comments();
$controller->register_routes();
}

/**
Expand Down
25 changes: 0 additions & 25 deletions includes/classes/Feature/Facets/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,11 @@ abstract public function setup();
*/
abstract public function register_block();

/**
* Setup REST endpoints for facet feature.
*/
abstract public function setup_endpoints();

/**
* Render the block.
*
* @param array $attributes Block attributes.
* @return string
*/
abstract public function render_block( $attributes );

/**
* Outputs the block preview
*
* @param \WP_REST_Request $request REST request
* @return string
*/
abstract public function render_block_preview( $request );

/**
* Check if the current user has permission to view the facets REST endpoint.
*
* @return true|\WP_Error
*/
public function check_facets_rest_permission() {
if ( ! current_user_can( 'edit_theme_options' ) ) {
return new \WP_Error( 'ep_rest_forbidden', esc_html__( 'Sorry, you cannot view this resource.', 'elasticpress' ), array( 'status' => 401 ) );
}
return true;
}
}
18 changes: 18 additions & 0 deletions includes/classes/Feature/Facets/Facets.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ElasticPress\Feature;
use ElasticPress\Features;
use ElasticPress\Indexables;
use ElasticPress\REST;
use ElasticPress\Utils;

if ( ! defined( 'ABSPATH' ) ) {
Expand Down Expand Up @@ -115,6 +116,7 @@ public function setup() {
add_filter( 'ep_post_formatted_args', [ $this, 'set_agg_filters' ], 10, 3 );
add_action( 'pre_get_posts', [ $this, 'facet_query' ] );
add_filter( 'ep_post_filters', [ $this, 'apply_facets_filters' ], 10, 3 );
add_action( 'rest_api_init', [ $this, 'setup_endpoints' ] );
}

/**
Expand Down Expand Up @@ -679,4 +681,20 @@ protected function set_settings_schema() {
protected function is_facetable_page( $query ) {
return $query->is_home() || $query->is_search() || $query->is_tax() || $query->is_tag() || $query->is_category() || $query->is_post_type_archive();
}

/**
* Setup REST endpoints
*
* @since 5.0.0
*/
public function setup_endpoints() {
$meta_keys = new REST\MetaKeys();
$meta_keys->register_routes();

$meta_range = new REST\MetaRange();
$meta_range->register_routes();

$taxonomies = new REST\Taxonomies();
$taxonomies->register_routes();
}
}
74 changes: 0 additions & 74 deletions includes/classes/Feature/Facets/Types/Meta/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,9 @@ class Block extends \ElasticPress\Feature\Facets\Block {
*/
public function setup() {
add_action( 'init', [ $this, 'register_block' ] );
add_action( 'rest_api_init', [ $this, 'setup_endpoints' ] );
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ] );
}

/**
* Setup REST endpoints for the feature.
*/
public function setup_endpoints() {
register_rest_route(
'elasticpress/v1',
'facets/meta/keys',
[
'methods' => 'GET',
'permission_callback' => [ $this, 'check_facets_rest_permission' ],
'callback' => [ $this, 'get_rest_registered_metakeys' ],
]
);
}

/**
* Return an array of registered meta keys.
*
* @return array
*/
public function get_rest_registered_metakeys() {
$post_indexable = \ElasticPress\Indexables::factory()->get( 'post' );

try {
$meta_keys = $post_indexable->get_distinct_meta_field_keys();
} catch ( \Throwable $th ) {
$meta_keys = [];
}

return $meta_keys;
}

/**
* Register the block.
*/
Expand Down Expand Up @@ -144,45 +111,4 @@ function ( $meta_fields ) use ( $attributes ) {
$block_content
);
}

/**
* Outputs the block preview
*
* @param \WP_REST_Request $request REST request
* @return string
*/
public function render_block_preview( $request ) {
_deprecated_function( __METHOD__, '4.7.0', '\ElasticPress\Feature\Facets\Types\Meta\render_block()' );

$attributes = $request->get_params();

return $this->render_block( $attributes );
}

/**
* Utilitary method to set default attributes.
*
* @param array $attributes Attributes passed
* @return array
*/
protected function parse_attributes( $attributes ) {
_doing_it_wrong(
__METHOD__,
esc_html__( 'Attribute parsing is now left to block.json.', 'elasticpress' ),
'4.7.0'
);

return $attributes;
}

/**
* DEPRECATED. Check permissions of the /facets/meta/* REST endpoints.
*
* @return WP_Error|true
*/
public function check_facets_meta_rest_permission() {
_deprecated_function( __METHOD__, '4.7.0', '\ElasticPress\Feature\Facets\Types\Meta\Block::check_facets_rest_permission()' );

return $this->check_facets_rest_permission();
}
}
Loading