PluginProbe
Code Snippets / trunk
Code Snippets vtrunk
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
← All changes | php/REST_API/Cloud/Cloud_Snippets_REST_Controller.php +34 -17 3.10.0-beta.1 → trunk View file →
@@ -30,8 +30,13 @@
30 30 */
31 31 public const BASE_ROUTE = 'cloud/snippets';
32 32
33 33 /**
34 + * Search methods supported by the cloud API.
35 + */
36 + private const SEARCH_METHODS = [ 'term', 'codevault', 'ai' ];
37 +
38 + /**
34 39 * Search controller instance.
35 40 *
36 41 * @var Cloud_Search_Controller
37 42 */
@@ -131,8 +136,14 @@
131 136 'description' => esc_html__( 'Treat the search query as the name of a CodeVault instead of a search term.', 'code-snippets' ),
132 137 'type' => 'boolean',
133 138 'default' => false,
134 139 ],
140 + 'searchMethod' => [
141 + 'description' => esc_html__( 'Optional search method. Set to "ai" to match snippets by natural-language intent.', 'code-snippets' ),
142 + 'type' => 'string',
143 + 'enum' => self::SEARCH_METHODS,
144 + 'default' => 'term',
145 + ],
135 146 'page' => $collection_args['page'],
136 147 'per_page' => $collection_args['per_page'],
137 148 ],
138 149 $filter_args
@@ -143,22 +154,8 @@
143 154 );
144 155
145 156 register_rest_route(
146 157 $this->namespace,
147 - $this->rest_base . '/codevault',
148 - [
149 - [
150 - 'methods' => WP_REST_Server::READABLE,
151 - 'callback' => [ $this, 'get_codevault_items' ],
152 - 'permission_callback' => [ $this, 'get_items_permissions_check' ],
153 - 'args' => [ 'page' => $collection_args['page'] ],
154 - 'schema' => [ $this, 'get_item_schema' ],
155 - ],
156 - ]
157 - );
158 -
159 - register_rest_route(
160 - $this->namespace,
161 158 $this->rest_base . '/featured',
162 159 [
163 160 [
164 161 'methods' => WP_REST_Server::READABLE,
@@ -237,8 +234,25 @@
237 234 return $snippets;
238 235 }
239 236
240 237 /**
238 + * Determine the search method to use based on request parameters.
239 + *
240 + * @param WP_REST_Request $request The request object containing the search parameters.
241 + *
242 + * @return string The search method to use.
243 + */
244 + private function get_search_method( WP_REST_Request $request ): string {
245 + $search_by_codevault = $request->get_param( 'searchByCodevault' );
246 + if ( $search_by_codevault ) {
247 + return 'codevault';
248 + }
249 +
250 + $method_param = $request->get_param( 'searchMethod' );
251 + return in_array( $method_param, self::SEARCH_METHODS, true ) ? $method_param : 'term';
252 + }
253 +
254 + /**
241 255 * Retrieve cloud snippets using a search query.
242 256 *
243 257 * @param WP_REST_Request $request The request object containing the search parameters.
244 258 *
@@ -244,17 +258,15 @@
244 258 *
245 259 * @return WP_REST_Response|WP_Error
246 260 */
247 261 public function get_items( $request ) {
248 - $method = $request->get_param( 'searchByCodevault' ) ? 'codevault' : 'term';
249 262 $query = $request->get_param( 'query' ) ?? '';
250 -
251 263 $page = max( 1, intval( $request->get_param( 'page' ) ) );
252 264 $per_page = intval( $request->get_param( 'per_page' ) ?? Manage_Menu::get_cloud_search_per_page() );
253 265 $filters = $this->extract_filters( $request );
254 266
255 267 $snippets = $this->search_controller
256 - ->fetch_search_results( $method, $query, $page, $per_page, $filters );
268 + ->fetch_search_results( $this->get_search_method( $request ), $query, $page, $per_page, $filters );
257 269
258 270 return $snippets
259 271 ? rest_ensure_response( $this->attach_local_ids( $snippets )->to_rest_response() )
260 272 : new WP_Error(
@@ -363,8 +375,13 @@
363 375 ],
364 376 'revision' => [
365 377 'description' => esc_html__( 'Snippet revision number.', 'code-snippets' ),
366 378 'type' => 'integer',
379 + ],
380 + 'active' => [
381 + 'description' => esc_html__( 'Whether the snippet should be active on the receiving site. Defaults to false to preserve install-as-inactive behaviour; set true for the self-heal redeploy path.', 'code-snippets' ),
382 + 'type' => 'boolean',
383 + 'default' => false,
367 384 ],
368 385 ],
369 386 ];
370 387