Skip to content

Commit 55dbd11

Browse files
release: fixes
- Enhanced security
2 parents ccea9a0 + eed0ed8 commit 55dbd11

File tree

3 files changed

+131
-7
lines changed

3 files changed

+131
-7
lines changed

inc/class-registration.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,12 @@ public function enqueue_block_editor_assets() {
267267
'canTrack' => 'yes' === get_option( 'otter_blocks_logger_flag', false ) ? true : false,
268268
'userRoles' => $wp_roles->roles,
269269
'isBlockEditor' => 'post' === $current_screen->base,
270-
'postTypes' => get_post_types( [ 'public' => true ] ),
270+
'postTypes' => get_post_types(
271+
[
272+
'public' => true,
273+
'exclude_from_search' => false,
274+
]
275+
),
271276
'rootUrl' => get_site_url(),
272277
'restRoot' => get_rest_url( null, 'otter/v1' ),
273278
'isPrettyPermalinks' => boolval( get_option( 'permalink_structure' ) ),

plugins/otter-pro/inc/server/class-live-search-server.php

+49-6
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,7 @@ public function register_routes() {
136136
*/
137137
public function search( WP_REST_Request $request ) {
138138
$query = new WP_Query(
139-
array(
140-
'posts_per_page' => 20,
141-
'post_type' => $request->get_param( 'post_type' ),
142-
's' => $request->get_param( 's' ),
143-
)
139+
$this->prepare_search_query( $request->get_param( 's' ), $request->get_param( 'post_type' ) )
144140
);
145141

146142
return new WP_REST_Response(
@@ -155,7 +151,7 @@ function( $post ) {
155151
'type' => $post->post_type,
156152
'date' => get_the_date( 'F d, Y', $post ),
157153
'author' => get_the_author_meta( 'display_name', intval( $post->post_author ) ),
158-
'parent' => get_post( $post->post_parent )->post_title,
154+
'parent' => get_post( $post->post_parent ) ? get_post( $post->post_parent )->post_title : '',
159155
);
160156

161157
if ( 'product' === $post->post_type && class_exists( 'WooCommerce' ) ) {
@@ -170,6 +166,53 @@ function( $post ) {
170166
);
171167
}
172168

169+
/**
170+
* Prepare the search query. Remove the post types that are not searchable.
171+
*
172+
* @param string $s Search query.
173+
* @param string|array $post_types Post type.
174+
*
175+
* @return array
176+
*/
177+
public function prepare_search_query( $s, $post_types ) {
178+
179+
$s = sanitize_text_field( $s );
180+
181+
if ( is_array( $post_types ) ) {
182+
$post_types = array_map( 'sanitize_text_field', $post_types );
183+
} else {
184+
$post_types = sanitize_text_field( $post_types );
185+
}
186+
187+
if ( ! empty( $post_types ) ) {
188+
$searchable_post_types = get_post_types(
189+
array(
190+
'public' => true,
191+
'exclude_from_search' => false,
192+
),
193+
'names'
194+
);
195+
196+
$needed_post_types = is_array( $post_types ) ? $post_types : explode( ',', $post_types );
197+
198+
$post_types = array_values(
199+
array_filter(
200+
$searchable_post_types,
201+
function( $post_type ) use ( $needed_post_types ) {
202+
return in_array( $post_type, $needed_post_types, true );
203+
}
204+
)
205+
);
206+
}
207+
208+
return array(
209+
'posts_per_page' => 20,
210+
's' => $s,
211+
'post_status' => 'publish',
212+
'post_type' => $post_types,
213+
);
214+
}
215+
173216
/**
174217
* Throw error on object clone
175218
*

tests/test-live-search.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Class CSS
4+
*
5+
* @package gutenberg-blocks
6+
*/
7+
8+
use ThemeIsle\OtterPro\Server\Live_Search_Server;
9+
use Yoast\PHPUnitPolyfills\Polyfills\AssertEqualsCanonicalizing;
10+
use Yoast\PHPUnitPolyfills\Polyfills\AssertNotEqualsCanonicalizing;
11+
12+
/**
13+
* Live Search Test Case.
14+
*/
15+
class TestLiveSearch extends WP_UnitTestCase
16+
{
17+
/**
18+
* Set up the test.
19+
*/
20+
public function set_up() {
21+
parent::set_up();
22+
23+
register_post_type( 'otter_shop_coupon', array(
24+
'public' => false,
25+
'label' => 'Shop Coupon',
26+
) );
27+
28+
register_post_type( 'otter_shop_product', array(
29+
'public' => true,
30+
'label' => 'Shop Product',
31+
) );
32+
33+
register_post_type( 'otter_page', array(
34+
'public' => true,
35+
'exclude_from_search' => true,
36+
'label' => 'Otter Page',
37+
) );
38+
}
39+
40+
/**
41+
* Tear down the test.
42+
*/
43+
public function tear_dow() {
44+
unregister_post_type( 'otter_shop_coupon' );
45+
unregister_post_type( 'otter_shop_product' );
46+
unregister_post_type( 'otter_page' );
47+
parent::tear_down();
48+
}
49+
50+
/**
51+
* Test live search prepare query function.
52+
*/
53+
public function test_live_search_prepare_query() {
54+
$live_search = new Live_Search_Server();
55+
56+
$search_query = $live_search->prepare_search_query( 'test', '' );
57+
$this->assertEquals( 'test', $search_query['s'] );
58+
$this->assertEquals( '', $search_query['post_type'] );
59+
60+
$search_query = $live_search->prepare_search_query( 'test', 'otter_shop_product' );
61+
$this->assertEquals( 'test', $search_query['s'] );
62+
$this->assertEquals( array('otter_shop_product'), $search_query['post_type'] );
63+
64+
$search_query = $live_search->prepare_search_query( 'test', 'otter_shop_coupon' );
65+
$this->assertEquals( 'test', $search_query['s'] );
66+
$this->assertEquals( array(), $search_query['post_type'] ); // Non-public post type are filtered out.
67+
68+
$search_query = $live_search->prepare_search_query( 'test', 'otter_page' );
69+
$this->assertEquals( 'test', $search_query['s'] );
70+
$this->assertEquals( array(), $search_query['post_type'] ); // Exclude from search post type are filtered out.
71+
72+
$search_query = $live_search->prepare_search_query( 'test', array('otter_shop_product', 'otter_shop_coupon', 'otter_page') );
73+
$this->assertEquals( 'test', $search_query['s'] );
74+
$this->assertEquals( array('otter_shop_product'), $search_query['post_type'] ); // Keep only the public post type.
75+
}
76+
}

0 commit comments

Comments
 (0)