Use WC Admin Settings’s get_option() in REST Settings controller instead of duplicating code.
This commit is contained in:
parent
586bf5d842
commit
832df9ab3e
|
@ -108,34 +108,6 @@ class WC_REST_Settings_API_Controller extends WC_REST_Controller {
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a value from WP's settings API.
|
|
||||||
*
|
|
||||||
* @since 2.7.0
|
|
||||||
* @param string $setting
|
|
||||||
* @param string $default
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function get_value( $setting, $default = '' ) {
|
|
||||||
if ( strstr( $setting, '[' ) ) { // Array value.
|
|
||||||
parse_str( $setting, $setting_array );
|
|
||||||
$setting = current( array_keys( $setting ) );
|
|
||||||
$values = get_option( $setting, '' );
|
|
||||||
$key = key( $setting_array[ $setting ] );
|
|
||||||
$value = isset( $values[ $key ] ) ? $values[ $key ] : null;
|
|
||||||
} else { // Single value.
|
|
||||||
$value = get_option( $setting, null );
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( is_array( $value ) ) {
|
|
||||||
$value = array_map( 'stripslashes', $value );
|
|
||||||
} elseif ( ! is_null( $value ) ) {
|
|
||||||
$value = stripslashes( $value );
|
|
||||||
}
|
|
||||||
|
|
||||||
return $value === null ? $default : $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filters out bad values from the settings array/filter so we
|
* Filters out bad values from the settings array/filter so we
|
||||||
* only return known values via the API.
|
* only return known values via the API.
|
||||||
|
|
|
@ -125,7 +125,7 @@ class WC_Rest_Settings_Controller extends WC_REST_Settings_API_Controller {
|
||||||
foreach ( $settings as $setting ) {
|
foreach ( $settings as $setting ) {
|
||||||
$setting = $this->filter_setting( $setting );
|
$setting = $this->filter_setting( $setting );
|
||||||
if ( $this->is_setting_type_valid( $setting['type'] ) ) {
|
if ( $this->is_setting_type_valid( $setting['type'] ) ) {
|
||||||
$setting['value'] = $this->get_value( $setting['id'] );
|
$setting['value'] = WC_Admin_Settings::get_option( $setting['id'] );
|
||||||
$filtered_settings[] = $setting;
|
$filtered_settings[] = $setting;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -225,7 +225,7 @@ class WC_Rest_Settings_Controller extends WC_REST_Settings_API_Controller {
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $item, $request ) {
|
public function prepare_item_for_response( $item, $request ) {
|
||||||
$data = $this->filter_setting( $item );
|
$data = $this->filter_setting( $item );
|
||||||
$data['value'] = $this->get_value( $data['id'] );
|
$data['value'] = WC_Admin_Settings::get_option( $data['id'] );
|
||||||
|
|
||||||
$context = empty( $request['context'] ) ? 'view' : $request['context'];
|
$context = empty( $request['context'] ) ? 'view' : $request['context'];
|
||||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||||
|
|
Loading…
Reference in New Issue