woocommerce/api/rest-api-filters/rest-api-filters.php

109 lines
3.5 KiB
PHP
Raw Normal View History

<?php
register_woocommerce_admin_test_helper_rest_route(
2022-04-15 13:51:12 +00:00
'/rest-api-filters',
[ WCA_Test_Helper_Rest_Api_Filters::class, 'create' ],
array(
'methods' => 'POST',
'args' => array(
'endpoint' => array(
'description' => 'Rest API endpoint.',
'type' => 'string',
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
),
'dot_notation' => array(
'description' => 'Dot notation of the target field.',
'type' => 'string',
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
),
'replacement' => array(
'description' => 'Replacement value for the target field.',
'type' => 'string',
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
),
),
)
);
register_woocommerce_admin_test_helper_rest_route(
2022-04-15 13:51:12 +00:00
'/rest-api-filters',
[ WCA_Test_Helper_Rest_Api_Filters::class, 'delete' ],
array(
'methods' => 'DELETE',
'args' => array(
'index' => array(
'description' => 'Rest API endpoint.',
'type' => 'integer',
'required' => true,
),
),
)
);
register_woocommerce_admin_test_helper_rest_route(
2022-04-15 13:51:12 +00:00
'/rest-api-filters/(?P<index>\d+)/toggle',
[ WCA_Test_Helper_Rest_Api_Filters::class, 'toggle' ],
array(
'methods' => 'POST',
)
);
class WCA_Test_Helper_Rest_Api_Filters {
2022-04-15 13:51:12 +00:00
const WC_ADMIN_TEST_HELPER_REST_API_FILTER_OPTION = 'wc-admin-test-helper-rest-api-filters';
2022-04-15 13:51:12 +00:00
public static function create( $request ) {
$endpoint = $request->get_param('endpoint');
$dot_notation = $request->get_param('dot_notation');
$replacement = $request->get_param('replacement');
2022-04-15 13:51:12 +00:00
self::update(
function ( $filters ) use (
$endpoint,
$dot_notation,
$replacement
) {
$filters[] = array(
'endpoint' => $endpoint,
'dot_notation' => $dot_notation,
'replacement' => filter_var( $replacement, FILTER_VALIDATE_BOOLEAN ),
2022-04-15 13:51:12 +00:00
'enabled' => true,
);
return $filters;
}
);
return new WP_REST_RESPONSE(null, 204);
}
2022-04-15 13:51:12 +00:00
public static function update( callable $callback ) {
$filters = get_option(self::WC_ADMIN_TEST_HELPER_REST_API_FILTER_OPTION, array());
$filters = $callback( $filters );
return update_option(self::WC_ADMIN_TEST_HELPER_REST_API_FILTER_OPTION, $filters);
}
2022-04-15 13:51:12 +00:00
public static function delete( $request ) {
self::update(
function ( $filters ) use ( $request ) {
array_splice($filters, $request->get_param('index'), 1);
return $filters;
}
);
2022-04-15 13:51:12 +00:00
return new WP_REST_RESPONSE(null, 204);
}
2022-04-15 13:51:12 +00:00
public static function toggle( $request ) {
self::update(
function ( $filters ) use ( $request ) {
$index = $request->get_param('index');
$filters[$index]['enabled'] = !$filters[$index]['enabled'];
return $filters;
}
);
return new WP_REST_RESPONSE(null, 204);
}
}