Update API

This commit is contained in:
Mike Jolley 2017-11-07 19:08:06 +00:00
parent db4e630600
commit cf448398cd
5 changed files with 34 additions and 74 deletions

View File

@ -225,6 +225,9 @@ class WC_Admin_Settings {
if ( ! isset( $value['placeholder'] ) ) {
$value['placeholder'] = '';
}
if ( ! isset( $value['suffix'] ) ) {
$value['suffix'] = '';
}
// Custom attribute handling
$custom_attributes = array();
@ -289,7 +292,7 @@ class WC_Admin_Settings {
class="<?php echo esc_attr( $value['class'] ); ?>"
placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>"
<?php echo implode( ' ', $custom_attributes ); ?>
/> <?php echo $description; ?>
/><?php echo esc_html( $value['suffix'] ); ?> <?php echo $description; ?>
</td>
</tr><?php
break;
@ -519,28 +522,17 @@ class WC_Admin_Settings {
</tr><?php
break;
// Width in pixels custom input. DEVELOPERS: This is private. Re-use at your own risk.
case 'width_in_pixels' :
$option_value = self::get_option( $value['id'], $value['default'] );
?><tr valign="top">
<th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?> <?php echo $tooltip_html; ?></th>
<td class="forminp">
<input
name="<?php echo esc_attr( $value['id'] ); ?>"
id="<?php echo esc_attr( $value['id'] ); ?>"
type="text"
size="3"
value="<?php echo esc_attr( $option_value ); ?>" /> px
</td>
</tr><?php
break;
// Thumbnail cropping setting. DEVELOPERS: This is private. Re-use at your own risk.
case 'thumbnail_cropping' :
$option_value = self::get_option( $value['id'], $value['default'] );
$width = get_option( 'woocommerce_thumbnail_cropping_aspect_ratio_width', 4 );
$height = get_option( 'woocommerce_thumbnail_cropping_aspect_ratio_height', 3 );
$option_value = self::get_option( $value['id'], $value['default'] );
if ( strstr( $option_value, ':' ) ) {
$cropping_split = explode( ':', $option_value );
$width = max( 1, current( $cropping_split ) );
$height = max( 1, end( $cropping_split ) );
} else {
$width = 4;
$height = 3;
}
?><tr valign="top">
<th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?> <?php echo $tooltip_html; ?></th>
@ -569,7 +561,7 @@ class WC_Admin_Settings {
<label for="thumbnail_cropping_1_1">1:1<br/><span class="description"><?php esc_html_e( 'Images will be cropped into a square', 'woocommerce' ); ?></span></label>
</li>
<li>
<input type="radio" name="woocommerce_thumbnail_cropping" id="thumbnail_cropping_custom" value="custom" <?php checked( $option_value, 'custom' ); ?> />
<input type="radio" name="woocommerce_thumbnail_cropping" id="thumbnail_cropping_custom" value="custom" <?php checked( ! in_array( $option_value, array( '1:1', 'uncropped' ), true ), true ); ?> />
<label for="thumbnail_cropping_custom">
<?php esc_html_e( 'Custom', 'woocommerce' ); ?><br/><span class="description"><?php esc_html_e( 'Images will be cropped to a custom aspect ratio', 'woocommerce' ); ?></span>
<span class="woocommerce-thumbnail-cropping-aspect-ratio">
@ -782,11 +774,9 @@ class WC_Admin_Settings {
$value = wc_clean( $raw_value );
if ( 'custom' === $value ) {
update_option( 'woocommerce_thumbnail_cropping_aspect_ratio_width', wc_clean( wp_unslash( $_POST['thumbnail_cropping_aspect_ratio_width'] ) ) );
update_option( 'woocommerce_thumbnail_cropping_aspect_ratio_height', wc_clean( wp_unslash( $_POST['thumbnail_cropping_aspect_ratio_height'] ) ) );
} else {
update_option( 'woocommerce_thumbnail_cropping_aspect_ratio_width', '4' );
update_option( 'woocommerce_thumbnail_cropping_aspect_ratio_height', '3' );
$width_ratio = wc_clean( wp_unslash( $_POST['thumbnail_cropping_aspect_ratio_width'] ) );
$height_ratio = wc_clean( wp_unslash( $_POST['thumbnail_cropping_aspect_ratio_height'] ) );
$value = $width_ratio . ':' . $height_ratio;
}
break;
case 'select':

View File

@ -176,7 +176,11 @@ class WC_Settings_Products extends WC_Settings_Page {
'desc' => __( 'This is the width used by the main image on single product pages. These images will be uncropped.', 'woocommerce' ),
'id' => 'woocommerce_single_image_width',
'css' => '',
'type' => 'width_in_pixels',
'type' => 'text',
'custom_attributes' => array(
'size' => 3,
),
'suffix' => 'px',
'default' => 600,
'desc_tip' => true,
),
@ -185,7 +189,11 @@ class WC_Settings_Products extends WC_Settings_Page {
'desc' => __( 'This size is used for product archives and product listings.', 'woocommerce' ),
'id' => 'woocommerce_thumbnail_image_width',
'css' => '',
'type' => 'width_in_pixels',
'type' => 'text',
'custom_attributes' => array(
'size' => 3,
),
'suffix' => 'px',
'default' => 300,
'desc_tip' => true,
),

View File

@ -418,6 +418,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller {
* For image_width, Crop can return "0" instead of false -- so we want
* to make sure we return these consistently the same we accept them.
*
* @todo remove in 4.0
* @since 3.0.0
* @param array $setting
* @return array
@ -475,6 +476,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller {
'radio', // Validates with validate_setting_radio_field (-> validate_setting_select_field).
'checkbox', // Validates with validate_setting_checkbox_field.
'image_width', // Validates with validate_setting_image_width_field.
'thumbnail_cropping', // Validates with validate_setting_text_field.
) );
}
@ -553,7 +555,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller {
'sanitize_callback' => 'sanitize_text_field',
),
'context' => array( 'view', 'edit' ),
'enum' => array( 'text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox' ),
'enum' => array( 'text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox', 'thumbnail_cropping' ),
'readonly' => true,
),
'options' => array(

View File

@ -704,15 +704,13 @@ function wc_get_image_size( $image_size ) {
$cropping = get_option( 'woocommerce_thumbnail_cropping', '1:1' );
if ( '1:1' === $cropping ) {
$size['height'] = $size['width'];
$size['crop'] = 1;
} elseif ( 'uncropped' === $cropping ) {
if ( 'uncropped' === $cropping ) {
$size['height'] = 9999999999;
$size['crop'] = 0;
} else {
$width_ratio = max( 1, get_option( 'woocommerce_thumbnail_cropping_aspect_ratio_width', 4 ) );
$height_ratio = max( 1, get_option( 'woocommerce_thumbnail_cropping_aspect_ratio_height', 3 ) );
$cropping_split = explode( ':', $cropping );
$width_ratio = max( 1, current( $cropping_split ) );
$height_ratio = max( 1, end( $cropping_split ) );
$size['height'] = round( ( $size['width'] / $width_ratio ) * $height_ratio );
$size['crop'] = 1;
}

View File

@ -697,44 +697,6 @@ class Settings extends WC_REST_Unit_Test_Case {
$this->assertEquals( 'lbs', $setting['value'] );
}
/**
* Test validation of image_width.
*
* @since 3.0.0
*/
public function test_validation_image_width() {
wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'products', 'shop_thumbnail_image_size' ) ) );
$setting = $response->get_data();
$this->assertEquals( array( 'width' => 180, 'height' => 180, 'crop' => true ), $setting['value'] );
// test bogus
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'shop_thumbnail_image_size' ) );
$request->set_body_params( array(
'value' => array(
'width' => 400,
'height' => 200,
'crop' => 'asdasdasd',
),
) );
$response = $this->server->dispatch( $request );
$setting = $response->get_data();
$this->assertEquals( array( 'width' => 400, 'height' => 200, 'crop' => true ), $setting['value'] );
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'shop_thumbnail_image_size' ) );
$request->set_body_params( array(
'value' => array(
'width' => 200,
'height' => 100,
'crop' => false,
),
) );
$response = $this->server->dispatch( $request );
$setting = $response->get_data();
$this->assertEquals( array( 'width' => 200, 'height' => 100, 'crop' => false ), $setting['value'] );
}
/**
* Test to make sure the 'base location' setting is present in the response.
* That it is returned as 'select' and not 'single_select_country',