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

Search Templates feature #118

Merged
merged 26 commits into from
Jan 30, 2025
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6819f1d
Search Templates feature
felipeelia Jan 9, 2025
9a12791
PHP Lint
felipeelia Jan 9, 2025
14acb12
Update the installation script and install svn in GH Action
felipeelia Jan 9, 2025
7213999
Initial commit of php unit tests
felipeelia Jan 9, 2025
8407ba0
Adjust style handler
felipeelia Jan 10, 2025
99da081
Unit tests
felipeelia Jan 10, 2025
42c9686
Merge branch 'develop' into feature/search-templates
felipeelia Jan 14, 2025
041d0d6
Ignore .wp-env.json in the final package
felipeelia Jan 14, 2025
f8964e7
e2e tests
felipeelia Jan 14, 2025
e7bace5
Overwrite wpCliEval as the path is always elasticpress-labs
felipeelia Jan 14, 2025
233662a
Setup permalinks structure
felipeelia Jan 14, 2025
c55fee3
Test templates limit
felipeelia Jan 14, 2025
fdb73a3
Delete all templates in the account
felipeelia Jan 14, 2025
06515f4
Unit test for test_delete_all_search_templates
felipeelia Jan 14, 2025
256f100
Fix unit tests
felipeelia Jan 14, 2025
6161fb2
php lint
felipeelia Jan 14, 2025
9f7ba84
php lint
felipeelia Jan 14, 2025
ff66165
Merge branch 'develop' into feature/search-templates
felipeelia Jan 15, 2025
880739e
Remove empty space
felipeelia Jan 15, 2025
25764dd
Remove empty lines
felipeelia Jan 15, 2025
342acce
Fix text domains
felipeelia Jan 22, 2025
6fdee9f
Run delete template CLI before running the tests
burhandodhy Jan 28, 2025
943161e
Cleanup for search template
burhandodhy Jan 28, 2025
b062248
Cleanup for search template in git action
burhandodhy Jan 28, 2025
d2877ec
Typo
burhandodhy Jan 28, 2025
2b26513
Merge branch 'develop' into feature/search-templates
felipeelia Jan 28, 2025
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
Prev Previous commit
Next Next commit
Fix unit tests
felipeelia committed Jan 14, 2025
commit 256f100c78acf8a618660ef823fdc60649783e5d
30 changes: 27 additions & 3 deletions includes/classes/Feature/SearchTemplates.php
Original file line number Diff line number Diff line change
@@ -164,7 +164,7 @@ public function scripts() {
* Setup REST endpoints
*/
public function setup_endpoint() {
$controller = new \ElasticPressLabs\REST\SearchTemplates();
$controller = new \ElasticPressLabs\REST\SearchTemplates( $this );
$controller->register_routes();
}

@@ -191,21 +191,45 @@ public function admin_page() {
* @return void
*/
public function delete_all_search_templates() {
$response = \ElasticPress\Elasticsearch::factory()->remote_request( 'api/v1/search/posts/templates' );
$response = \ElasticPress\Elasticsearch::factory()->remote_request( $this->get_search_templates_endpoint() );
$response_body = json_decode( wp_remote_retrieve_body( $response ), true );

if ( ! empty( $response_body ) ) {
foreach ( $response_body as $index_name => $templates ) {
foreach ( $templates as $template ) {
\ElasticPress\Elasticsearch::factory()->remote_request(
'api/v1/search/posts/' . $index_name . '/template?template_name=' . $template,
$this->get_search_template_endpoint( $index_name ) . '?template_name=' . $template,
[ 'method' => 'DELETE' ]
);
}
}
}
}

/**
* EP.io search templates endpoint.
*
* @return string
*/
public function get_search_templates_endpoint(): string {
return 'api/v1/search/posts/templates';
}

/**
* EP.io (single) search template endpoint.
*
* @param null|string $index_name Index name.
* @return string
*/
public function get_search_template_endpoint( $index_name = null ): string {
if ( ! $index_name ) {
$index_name = \ElasticPress\Indexables::factory()->get( 'post' )->get_index_name();
}

return "api/v1/search/posts/{$index_name}/template";
}


/**
* Set the `settings_schema` attribute
*/
45 changes: 21 additions & 24 deletions includes/classes/REST/SearchTemplates.php
Original file line number Diff line number Diff line change
@@ -9,11 +9,28 @@
namespace ElasticPressLabs\REST;

use ElasticPress\Utils;
use ElasticPressLabs\Feature\SearchTemplates as SearchTemplatesFeature;

/**
* Search Templates API controller class.
*/
class SearchTemplates {
/**
* The SearchTemplatesFeature instance.
*
* @var SearchTemplatesFeature
*/
protected $feature;

/**
* Class constructor
*
* @param SearchTemplatesFeature $feature The feature instance.
*/
public function __construct( SearchTemplatesFeature $feature ) {
$this->feature = $feature;
}

/**
* Register routes.
*
@@ -70,33 +87,13 @@ public function check_permission() {
return current_user_can( $capability );
}

/**
* EP.io search templates endpoint.
*
* @return string
*/
protected function get_search_templates_endpoint(): string {
return 'api/v1/search/posts/templates';
}

/**
* EP.io (single) search template endpoint.
*
* @return string
*/
protected function get_search_template_endpoint(): string {
$index_name = \ElasticPress\Indexables::factory()->get( 'post' )->get_index_name();

return "api/v1/search/posts/{$index_name}/template";
}

/**
* List search templates handler.
*
* @return array|\WP_Error
*/
public function get_search_templates() {
$response = \ElasticPress\Elasticsearch::factory()->remote_request( $this->get_search_templates_endpoint() );
$response = \ElasticPress\Elasticsearch::factory()->remote_request( $this->feature->get_search_templates_endpoint() );

if ( is_wp_error( $response ) ) {
return $response;
@@ -119,7 +116,7 @@ public function get_search_templates() {
* @return object|\WP_Error
*/
public function get_search_template( \WP_REST_Request $request ) {
$path = $this->get_search_template_endpoint() . '?template_name=' . $request['template_name'];
$path = $this->feature->get_search_template_endpoint() . '?template_name=' . $request['template_name'];
$response = \ElasticPress\Elasticsearch::factory()->remote_request( $path );

if ( is_wp_error( $response ) ) {
@@ -140,7 +137,7 @@ public function get_search_template( \WP_REST_Request $request ) {
* @return object|\WP_Error
*/
public function update_search_template( \WP_REST_Request $request ) {
$path = $this->get_search_template_endpoint() . '?template_name=' . $request['template_name'];
$path = $this->feature->get_search_template_endpoint() . '?template_name=' . $request['template_name'];
$response = \ElasticPress\Elasticsearch::factory()->remote_request(
$path,
[
@@ -175,7 +172,7 @@ public function update_search_template( \WP_REST_Request $request ) {
* @return object|\WP_Error
*/
public function delete_search_template( \WP_REST_Request $request ) {
$path = $this->get_search_template_endpoint() . '?template_name=' . $request['template_name'];
$path = $this->feature->get_search_template_endpoint() . '?template_name=' . $request['template_name'];
$response = \ElasticPress\Elasticsearch::factory()->remote_request(
$path,
[
5 changes: 3 additions & 2 deletions tests/phpunit/REST/TestSearchTemplates.php
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
public function set_up() {
parent::set_up();

$this->controller = new SearchTemplates();
$this->controller = new SearchTemplates( \ElasticPress\Features::factory()->get_registered_feature( 'search_templates' ) );
add_filter( 'ep_intercept_remote_request', '__return_true' );
}

@@ -218,7 +218,7 @@

$error = $this->controller->update_search_template( new \WP_REST_Request() );
$this->assertEquals( 500, $error->get_error_code() );
$this->assertEquals( 'Testing message', $error->get_error_message() );
$this->assertEquals( 'Testing body message', $error->get_error_message() );
}

/**
@@ -326,6 +326,7 @@
'code' => 500,
'message' => 'Testing message',
],
'body' => 'Testing body message',

Check warning on line 329 in tests/phpunit/REST/TestSearchTemplates.php

GitHub Actions / PHP Lint

Array double arrow not aligned correctly; expected 5 space(s) between "'body'" and double arrow, but found 1.
];
};
add_filter( 'ep_do_intercept_request', $return_http_code );

Unchanged files with check annotations Beta

$expected_template = $expected[ $calls ][1];
$this->assertStringEndsWith( "api/v1/search/posts/{$expected_index}/template?template_name={$expected_template}", $request['url'] );
$calls++;

Check warning on line 127 in tests/phpunit/feature/TestSearchTemplates.php

GitHub Actions / PHP Lint

Stand-alone post-increment statement found. Use pre-increment instead: ++$calls.
return [
'response' => [