← All changes
|
php/REST_API/Cloud/Cloud_Snippets_REST_Controller.php
+34
-3
3.10.2
→
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 |
| @@ -223,8 +234,25 @@ | ||
| 223 | 234 | return $snippets; |
| 224 | 235 | } |
| 225 | 236 | |
| 226 | 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 | + /** | |
| 227 | 255 | * Retrieve cloud snippets using a search query. |
| 228 | 256 | * |
| 229 | 257 | * @param WP_REST_Request $request The request object containing the search parameters. |
| 230 | 258 | * |
| @@ -230,17 +258,15 @@ | ||
| 230 | 258 | * |
| 231 | 259 | * @return WP_REST_Response|WP_Error |
| 232 | 260 | */ |
| 233 | 261 | public function get_items( $request ) { |
| 234 | - $method = $request->get_param( 'searchByCodevault' ) ? 'codevault' : 'term'; | |
| 235 | 262 | $query = $request->get_param( 'query' ) ?? ''; |
| 236 | - | |
| 237 | 263 | $page = max( 1, intval( $request->get_param( 'page' ) ) ); |
| 238 | 264 | $per_page = intval( $request->get_param( 'per_page' ) ?? Manage_Menu::get_cloud_search_per_page() ); |
| 239 | 265 | $filters = $this->extract_filters( $request ); |
| 240 | 266 | |
| 241 | 267 | $snippets = $this->search_controller |
| 242 | - ->fetch_search_results( $method, $query, $page, $per_page, $filters ); | |
| 268 | + ->fetch_search_results( $this->get_search_method( $request ), $query, $page, $per_page, $filters ); | |
| 243 | 269 | |
| 244 | 270 | return $snippets |
| 245 | 271 | ? rest_ensure_response( $this->attach_local_ids( $snippets )->to_rest_response() ) |
| 246 | 272 | : new WP_Error( |
| @@ -349,8 +375,13 @@ | ||
| 349 | 375 | ], |
| 350 | 376 | 'revision' => [ |
| 351 | 377 | 'description' => esc_html__( 'Snippet revision number.', 'code-snippets' ), |
| 352 | 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, | |
| 353 | 384 | ], |
| 354 | 385 | ], |
| 355 | 386 | ]; |
| 356 | 387 | |