Reuse WC Admin Setting’s save_fields() instead of duplicating code for sanitizing saved values.
This commit is contained in:
parent
f3e21123d1
commit
cca2a0ff5e
|
@ -41,73 +41,6 @@ class WC_REST_Settings_API_Controller extends WC_REST_Controller {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans a value before setting it.
|
||||
*
|
||||
* @since 2.7.0
|
||||
*
|
||||
* @param array $setting WC Setting Array
|
||||
* @param mixed $raw_value Raw value from PUT request
|
||||
* @return mixed Sanitized value
|
||||
*/
|
||||
public function sanitize_setting_value( $setting, $raw_value ) {
|
||||
switch ( $setting['type'] ) {
|
||||
case 'checkbox' :
|
||||
$default = ( ! empty( $setting['default'] ) ? $setting['default'] : 'no' );
|
||||
$value = ( in_array( $raw_value, array( 'yes', 'no' ) ) ? $raw_value : $default );
|
||||
break;
|
||||
case 'email' :
|
||||
$value = sanitize_email( $raw_value );
|
||||
$default = ( ! empty( $setting['default'] ) ? $setting['default'] : '' );
|
||||
$value = ( ! empty( $value ) ? $value : $default );
|
||||
break;
|
||||
case 'textarea' :
|
||||
$value = wp_kses( trim( $raw_value ),
|
||||
array_merge(
|
||||
array(
|
||||
'iframe' => array( 'src' => true, 'style' => true, 'id' => true, 'class' => true )
|
||||
),
|
||||
wp_kses_allowed_html( 'post' )
|
||||
)
|
||||
);
|
||||
break;
|
||||
case 'multiselect' :
|
||||
case 'multi_select_countries' :
|
||||
$value = array_filter( array_map( 'wc_clean', (array) $raw_value ) );
|
||||
break;
|
||||
case 'image_width' :
|
||||
$value = array();
|
||||
if ( isset( $raw_value['width'] ) ) {
|
||||
$value['width'] = wc_clean( $raw_value['width'] );
|
||||
$value['height'] = wc_clean( $raw_value['height'] );
|
||||
$value['crop'] = isset( $raw_value['crop'] ) ? 1 : 0;
|
||||
} else {
|
||||
$value['width'] = $setting['default']['width'];
|
||||
$value['height'] = $setting['default']['height'];
|
||||
$value['crop'] = $setting['default']['crop'];
|
||||
}
|
||||
break;
|
||||
case 'select':
|
||||
$options = array_keys( $setting['options'] );
|
||||
$default = ( empty( $setting['default'] ) ? $options[0] : $setting['default'] );
|
||||
$value = in_array( $raw_value, $options ) ? $raw_value : $default;
|
||||
break;
|
||||
default :
|
||||
$value = wc_clean( $raw_value );
|
||||
break;
|
||||
}
|
||||
|
||||
// A couple fields changed in the REST API -- we can just pass these too so old filters still work
|
||||
$setting['desc'] = ( ! empty( $setting['description'] ) ? $setting['description'] : '' );
|
||||
$setting['title'] = ( ! empty( $setting['label'] ) ? $setting['label'] : '' );
|
||||
|
||||
$value = apply_filters( 'woocommerce_admin_settings_sanitize_option', $value, $setting, $raw_value );
|
||||
$value = apply_filters( "woocommerce_admin_settings_sanitize_option_" . $setting['id'], $value, $setting, $raw_value );
|
||||
do_action( 'woocommerce_update_option', $setting );
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters out bad values from the settings array/filter so we
|
||||
* only return known values via the API.
|
||||
|
|
|
@ -206,12 +206,12 @@ class WC_Rest_Settings_Controller extends WC_REST_Settings_API_Controller {
|
|||
return $setting;
|
||||
}
|
||||
|
||||
$update_data = array();
|
||||
$update_data[ $setting['id'] ] = $request['value'];
|
||||
|
||||
WC_Admin_Settings::save_fields( array( $setting ), $update_data );
|
||||
|
||||
$response = $this->prepare_item_for_response( $setting, $request );
|
||||
$value = $this->sanitize_setting_value( $setting, $request['value'] );
|
||||
|
||||
$response->set_data( array_merge( $response->get_data(), compact( 'value' ) ) );
|
||||
|
||||
update_option( $setting['id'], $value );
|
||||
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
|
|
@ -404,52 +404,6 @@ class Settings extends WC_Unit_Test_Case {
|
|||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes sure our sanitize function runs correctly for different types.
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public function test_sanitize_setting() {
|
||||
$endpoint = new WC_Rest_Settings_Controller;
|
||||
|
||||
// checkbox
|
||||
$value = $endpoint->sanitize_setting_value( array( 'id' => 'test', 'type' => 'checkbox', 'default' => 'yes' ), 'no' );
|
||||
$this->assertEquals( 'no', $value );
|
||||
$value = $endpoint->sanitize_setting_value( array( 'id' => 'test', 'type' => 'checkbox', 'default' => 'yes' ), 'yes' );
|
||||
$this->assertEquals( 'yes', $value );
|
||||
$value = $endpoint->sanitize_setting_value( array( 'id' => 'test', 'type' => 'checkbox', 'default' => 'yes' ), 'invalid' );
|
||||
$this->assertEquals( 'yes', $value );
|
||||
|
||||
// email
|
||||
$value = $endpoint->sanitize_setting_value( array( 'id' => 'test', 'type' => 'email' ), 'test@woo.local' );
|
||||
$this->assertEquals( 'test@woo.local', $value );
|
||||
$value = $endpoint->sanitize_setting_value( array( 'id' => 'test', 'type' => 'email' ), ' admin@woo.local! ' );
|
||||
$this->assertEquals( 'admin@woo.local', $value );
|
||||
$value = $endpoint->sanitize_setting_value( array( 'id' => 'test', 'type' => 'email' ), 'blah' );
|
||||
$this->assertEquals( '', $value );
|
||||
$value = $endpoint->sanitize_setting_value( array( 'id' => 'test', 'type' => 'email', 'default' => 'woo@woo.local' ), 'blah' );
|
||||
$this->assertEquals( 'woo@woo.local', $value );
|
||||
|
||||
// textarea
|
||||
$value = $endpoint->sanitize_setting_value( array( 'id' => 'test', 'type' => 'textarea' ), ' <strong>blah</strong>' );
|
||||
$this->assertEquals( '<strong>blah</strong>', $value );
|
||||
$value = $endpoint->sanitize_setting_value( array( 'id' => 'test', 'type' => 'textarea' ), '<script></script><strong>blah</strong>' );
|
||||
$this->assertEquals( '<strong>blah</strong>', $value );
|
||||
|
||||
// multiselect / multiselect countries
|
||||
$value = $endpoint->sanitize_setting_value( array( 'id' => 'test', 'type' => 'multiselect' ), array( 'test', '<test ' ) );
|
||||
$this->assertEquals( array( 'test', '<test' ), $value );
|
||||
$value = $endpoint->sanitize_setting_value( array( 'id' => 'test', 'type' => 'multi_select_countries' ), array( 'test', '<test ' ) );
|
||||
$this->assertEquals( array( 'test', '<test' ), $value );
|
||||
|
||||
// image_width
|
||||
$value = $endpoint->sanitize_setting_value( array( 'id' => 'test', 'type' => 'image_width' ), array( 'width' => ' 100%', 'height' => '25px ' ) );
|
||||
$this->assertEquals( array( 'width' => '100%', 'height' => '25px', 'crop' => 0 ), $value );
|
||||
$value = $endpoint->sanitize_setting_value( array( 'id' => 'test', 'type' => 'image_width' ), array( 'width' => '100%', 'height' => '25px', 'crop' => 'something' ) );
|
||||
$this->assertEquals( array( 'width' => '100%', 'height' => '25px', 'crop' => 1 ), $value );
|
||||
$value = $endpoint->sanitize_setting_value( array( 'id' => 'test', 'type' => 'image_width', 'default' => array( 'width' => '50px', 'height' => '50px', 'crop' => true ) ), array() );
|
||||
$this->assertEquals( array( 'width' => '50px', 'height' => '50px', 'crop' => 1 ), $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests our classic setting registration to make sure settings added for WP-Admin are available over the API.
|
||||
* @since 2.7.0
|
||||
|
|
Loading…
Reference in New Issue