2021-03-03 06:05:45 +00:00
< ? php
2023-08-09 00:20:34 +00:00
use function WP_CLI\Utils\esc_like ;
defined ( 'ABSPATH' ) || exit ;
2021-03-04 02:36:13 +00:00
register_woocommerce_admin_test_helper_rest_route (
'/options' ,
'wca_test_helper_get_options' ,
array (
'methods' => 'GET' ,
2023-08-09 00:20:34 +00:00
'args' => array (
2021-03-03 06:05:45 +00:00
'page' => array (
'description' => 'Current page of the collection.' ,
'type' => 'integer' ,
'default' => 1 ,
'sanitize_callback' => 'absint' ,
),
'per_page' => array (
'description' => 'Maximum number of items to be returned in result set.' ,
'type' => 'integer' ,
'default' => 10 ,
'sanitize_callback' => 'absint' ,
),
'search' => array (
'description' => 'Limit results to those matching a string.' ,
'type' => 'string' ,
'sanitize_callback' => 'sanitize_text_field' ,
),
2021-03-04 02:36:13 +00:00
),
)
);
register_woocommerce_admin_test_helper_rest_route (
2021-04-27 01:03:24 +00:00
'/options/(?P<option_names>(.*)+)' ,
2021-03-04 02:36:13 +00:00
'wca_test_helper_delete_option' ,
array (
'methods' => 'DELETE' ,
2023-08-09 00:20:34 +00:00
'args' => array (
2021-04-27 01:03:24 +00:00
'option_names' => array (
2023-08-09 00:20:34 +00:00
'type' => 'string' ,
2021-03-04 02:36:13 +00:00
),
),
)
);
2023-08-09 00:20:34 +00:00
/**
* A helper to delete options .
*
* @ param WP_REST_Request $request The full request data .
*/
2021-03-04 02:36:13 +00:00
function wca_test_helper_delete_option ( $request ) {
global $wpdb ;
2023-08-09 00:20:34 +00:00
$option_names = explode ( ',' , $request -> get_param ( 'option_names' ) );
$option_tokens = implode ( ',' , array_fill ( 0 , count ( $option_names ), '%s' ) );
2021-04-27 01:03:24 +00:00
2023-08-09 00:20:34 +00:00
$wpdb -> query (
$wpdb -> prepare (
" DELETE FROM { $wpdb -> prefix } options WHERE option_name IN ( { $option_tokens } ) " , // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
... $option_names ,
)
);
2021-03-04 02:36:13 +00:00
2024-08-12 10:05:01 +00:00
wp_cache_flush ();
2021-03-04 02:36:13 +00:00
return new WP_REST_RESPONSE ( null , 204 );
2021-03-03 06:05:45 +00:00
}
2023-08-09 00:20:34 +00:00
/**
* A helper to get options .
*
* @ param WP_REST_Request $request The full request data .
*/
2021-03-04 02:36:13 +00:00
function wca_test_helper_get_options ( $request ) {
global $wpdb ;
$per_page = $request -> get_param ( 'per_page' );
$page = $request -> get_param ( 'page' );
$search = $request -> get_param ( 'search' );
2023-08-09 00:20:34 +00:00
$query = " SELECT option_id, option_name, option_value, autoload FROM { $wpdb -> prefix } options " ;
2021-03-04 02:36:13 +00:00
if ( $search ) {
2023-08-09 00:20:34 +00:00
$search = $wpdb -> esc_like ( $search );
2023-11-21 03:18:45 +00:00
$search = " % { $search } % " ;
2023-08-09 00:20:34 +00:00
$query .= ' WHERE option_name LIKE %s' ;
2021-03-03 06:05:45 +00:00
}
2021-03-04 02:36:13 +00:00
2023-08-09 00:20:34 +00:00
$query .= ' ORDER BY option_id DESC LIMIT %d OFFSET %d' ;
$offset = ( $page - 1 ) * $per_page ;
if ( $search ) {
$query = $wpdb -> prepare ( $query , $search , $per_page , $offset ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
} else {
$query = $wpdb -> prepare ( $query , $per_page , $offset ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
2021-03-04 02:36:13 +00:00
2023-08-09 00:20:34 +00:00
$options = $wpdb -> get_results ( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
2021-03-04 02:36:13 +00:00
return new WP_REST_Response ( $options , 200 );
}