woocommerce/plugins/woocommerce-beta-tester/api/options/rest-api.php

100 lines
2.7 KiB
PHP
Raw Normal View History

<?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(
'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(
'/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(
'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' ) );
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
return new WP_REST_RESPONSE( null, 204 );
}
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 );
$query .= ' WHERE option_name LIKE %s';
}
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 );
}