Styling fix

This commit is contained in:
moon 2022-04-15 06:51:12 -07:00
parent c3ad996eb2
commit 9aaf994638
1 changed files with 93 additions and 87 deletions

View File

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