Merge pull request #18554 from woocommerce/fix/18545

Flat rate: remove currency symbols
This commit is contained in:
Mike Jolley 2018-02-22 12:21:49 +00:00 committed by GitHub
commit 58796bf981
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 317 additions and 276 deletions

View File

@ -1,60 +1,64 @@
<?php <?php // @codingStandardsIgnoreLine.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/** /**
* Abstract Settings API Class * Abstract Settings API Class
* *
* Admin Settings API used by Integrations, Shipping Methods, and Payment Gateways. * Admin Settings API used by Integrations, Shipping Methods, and Payment Gateways.
* *
* @class WC_Settings_API
* @version 2.6.0
* @package WooCommerce/Abstracts * @package WooCommerce/Abstracts
* @category Abstract Class */
* @author WooThemes
defined( 'ABSPATH' ) || exit;
/**
* WC_Settings_API class.
*/ */
abstract class WC_Settings_API { abstract class WC_Settings_API {
/** /**
* The plugin ID. Used for option names. * The plugin ID. Used for option names.
*
* @var string * @var string
*/ */
public $plugin_id = 'woocommerce_'; public $plugin_id = 'woocommerce_';
/** /**
* ID of the class extending the settings API. Used in option names. * ID of the class extending the settings API. Used in option names.
*
* @var string * @var string
*/ */
public $id = ''; public $id = '';
/** /**
* Validation errors. * Validation errors.
*
* @var array of strings * @var array of strings
*/ */
public $errors = array(); public $errors = array();
/** /**
* Setting values. * Setting values.
*
* @var array * @var array
*/ */
public $settings = array(); public $settings = array();
/** /**
* Form option fields. * Form option fields.
*
* @var array * @var array
*/ */
public $form_fields = array(); public $form_fields = array();
/** /**
* The posted settings data. When empty, $_POST data will be used. * The posted settings data. When empty, $_POST data will be used.
*
* @var array * @var array
*/ */
protected $data = array(); protected $data = array();
/** /**
* Get the form fields after they are initialized. * Get the form fields after they are initialized.
*
* @return array of options * @return array of options
*/ */
public function get_form_fields() { public function get_form_fields() {
@ -64,8 +68,7 @@ abstract class WC_Settings_API {
/** /**
* Set default required properties for each field. * Set default required properties for each field.
* *
* @param array $field * @param array $field Setting field array.
*
* @return array * @return array
*/ */
protected function set_defaults( $field ) { protected function set_defaults( $field ) {
@ -79,14 +82,13 @@ abstract class WC_Settings_API {
* Output the admin options table. * Output the admin options table.
*/ */
public function admin_options() { public function admin_options() {
echo '<table class="form-table">' . $this->generate_settings_html( $this->get_form_fields(), false ) . '</table>'; echo '<table class="form-table">' . $this->generate_settings_html( $this->get_form_fields(), false ) . '</table>'; // WPCS: XSS ok.
} }
/** /**
* Initialise settings form fields. * Initialise settings form fields.
* *
* Add an array of fields to be displayed * Add an array of fields to be displayed on the gateway's settings screen.
* on the gateway's settings screen.
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@ -94,6 +96,7 @@ abstract class WC_Settings_API {
/** /**
* Return the name of the option in the WP DB. * Return the name of the option in the WP DB.
*
* @since 2.6.0 * @since 2.6.0
* @return string * @return string
*/ */
@ -103,7 +106,8 @@ abstract class WC_Settings_API {
/** /**
* Get a fields type. Defaults to "text" if not set. * Get a fields type. Defaults to "text" if not set.
* @param array $field *
* @param array $field Field key.
* @return string * @return string
*/ */
public function get_field_type( $field ) { public function get_field_type( $field ) {
@ -112,7 +116,8 @@ abstract class WC_Settings_API {
/** /**
* Get a fields default value. Defaults to "" if not set. * Get a fields default value. Defaults to "" if not set.
* @param array $field *
* @param array $field Field key.
* @return string * @return string
*/ */
public function get_field_default( $field ) { public function get_field_default( $field ) {
@ -121,35 +126,40 @@ abstract class WC_Settings_API {
/** /**
* Get a field's posted and validated value. * Get a field's posted and validated value.
* @param string $key *
* @param array $field * @param string $key Field key.
* @param array $post_data * @param array $field Field array.
* @param array $post_data Posted data.
* @return string * @return string
*/ */
public function get_field_value( $key, $field, $post_data = array() ) { public function get_field_value( $key, $field, $post_data = array() ) {
$type = $this->get_field_type( $field ); $type = $this->get_field_type( $field );
$field_key = $this->get_field_key( $key ); $field_key = $this->get_field_key( $key );
$post_data = empty( $post_data ) ? $_POST : $post_data; $post_data = empty( $post_data ) ? $_POST : $post_data; // WPCS: CSRF ok, input var ok.
$value = isset( $post_data[ $field_key ] ) ? $post_data[ $field_key ] : null; $value = isset( $post_data[ $field_key ] ) ? $post_data[ $field_key ] : null;
// Look for a validate_FIELDID_field method for special handling if ( isset( $field['sanitize_callback'] ) && is_callable( $field['sanitize_callback'] ) ) {
return call_user_func( $field['sanitize_callback'], $value );
}
// Look for a validate_FIELDID_field method for special handling.
if ( is_callable( array( $this, 'validate_' . $key . '_field' ) ) ) { if ( is_callable( array( $this, 'validate_' . $key . '_field' ) ) ) {
return $this->{'validate_' . $key . '_field'}( $key, $value ); return $this->{'validate_' . $key . '_field'}( $key, $value );
} }
// Look for a validate_FIELDTYPE_field method // Look for a validate_FIELDTYPE_field method.
if ( is_callable( array( $this, 'validate_' . $type . '_field' ) ) ) { if ( is_callable( array( $this, 'validate_' . $type . '_field' ) ) ) {
return $this->{'validate_' . $type . '_field'}( $key, $value ); return $this->{'validate_' . $type . '_field'}( $key, $value );
} }
// Fallback to text // Fallback to text.
return $this->validate_text_field( $key, $value ); return $this->validate_text_field( $key, $value );
} }
/** /**
* Sets the POSTed data. This method can be used to set specific data, instead * Sets the POSTed data. This method can be used to set specific data, instead of taking it from the $_POST array.
* of taking it from the $_POST array. *
* @param array data * @param array $data Posted data.
*/ */
public function set_post_data( $data = array() ) { public function set_post_data( $data = array() ) {
$this->data = $data; $this->data = $data;
@ -157,18 +167,20 @@ abstract class WC_Settings_API {
/** /**
* Returns the POSTed data, to be used to save the settings. * Returns the POSTed data, to be used to save the settings.
*
* @return array * @return array
*/ */
public function get_post_data() { public function get_post_data() {
if ( ! empty( $this->data ) && is_array( $this->data ) ) { if ( ! empty( $this->data ) && is_array( $this->data ) ) {
return $this->data; return $this->data;
} }
return $_POST; return $_POST; // WPCS: CSRF ok, input var ok.
} }
/** /**
* Processes and saves options. * Processes and saves options.
* If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out. * If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out.
*
* @return bool was anything saved? * @return bool was anything saved?
*/ */
public function process_admin_options() { public function process_admin_options() {
@ -191,7 +203,8 @@ abstract class WC_Settings_API {
/** /**
* Add an error message for display in admin on save. * Add an error message for display in admin on save.
* @param string $error *
* @param string $error Error message.
*/ */
public function add_error( $error ) { public function add_error( $error ) {
$this->errors[] = $error; $this->errors[] = $error;
@ -238,12 +251,12 @@ abstract class WC_Settings_API {
} }
/** /**
* get_option function. * Get option from DB.
* *
* Gets an option from the settings API, using defaults if necessary to prevent undefined notices. * Gets an option from the settings API, using defaults if necessary to prevent undefined notices.
* *
* @param string $key * @param string $key Option key.
* @param mixed $empty_value * @param mixed $empty_value Value when empty.
* @return string The value specified for the option or a default value for the option. * @return string The value specified for the option or a default value for the option.
*/ */
public function get_option( $key, $empty_value = null ) { public function get_option( $key, $empty_value = null ) {
@ -267,7 +280,7 @@ abstract class WC_Settings_API {
/** /**
* Prefix key for settings. * Prefix key for settings.
* *
* @param mixed $key * @param string $key Field key.
* @return string * @return string
*/ */
public function get_field_key( $key ) { public function get_field_key( $key ) {
@ -279,9 +292,8 @@ abstract class WC_Settings_API {
* *
* Generate the HTML for the fields on the "settings" screen. * Generate the HTML for the fields on the "settings" screen.
* *
* @param array $form_fields (default: array()) * @param array $form_fields (default: array()) Array of form fields.
* @param bool $echo * @param bool $echo Echo or return.
*
* @return string the html for the settings * @return string the html for the settings
* @since 1.0.0 * @since 1.0.0
* @uses method_exists() * @uses method_exists()
@ -303,7 +315,7 @@ abstract class WC_Settings_API {
} }
if ( $echo ) { if ( $echo ) {
echo $html; echo $html; // WPCS: XSS ok.
} else { } else {
return $html; return $html;
} }
@ -312,7 +324,7 @@ abstract class WC_Settings_API {
/** /**
* Get HTML for tooltips. * Get HTML for tooltips.
* *
* @param array $data * @param array $data Data for the tooltip.
* @return string * @return string
*/ */
public function get_tooltip_html( $data ) { public function get_tooltip_html( $data ) {
@ -330,7 +342,7 @@ abstract class WC_Settings_API {
/** /**
* Get HTML for descriptions. * Get HTML for descriptions.
* *
* @param array $data * @param array $data Data for the description.
* @return string * @return string
*/ */
public function get_description_html( $data ) { public function get_description_html( $data ) {
@ -350,7 +362,7 @@ abstract class WC_Settings_API {
/** /**
* Get custom attributes. * Get custom attributes.
* *
* @param array $data * @param array $data Field data.
* @return string * @return string
*/ */
public function get_custom_attribute_html( $data ) { public function get_custom_attribute_html( $data ) {
@ -368,8 +380,8 @@ abstract class WC_Settings_API {
/** /**
* Generate Text Input HTML. * Generate Text Input HTML.
* *
* @param mixed $key * @param string $key Field key.
* @param mixed $data * @param array $data Field data.
* @since 1.0.0 * @since 1.0.0
* @return string * @return string
*/ */
@ -393,14 +405,14 @@ abstract class WC_Settings_API {
?> ?>
<tr valign="top"> <tr valign="top">
<th scope="row" class="titledesc"> <th scope="row" class="titledesc">
<?php echo $this->get_tooltip_html( $data ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th> </th>
<td class="forminp"> <td class="forminp">
<fieldset> <fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
<input class="input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="<?php echo esc_attr( $data['type'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $this->get_option( $key ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> <input class="input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="<?php echo esc_attr( $data['type'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $this->get_option( $key ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?> />
<?php echo $this->get_description_html( $data ); ?> <?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
</fieldset> </fieldset>
</td> </td>
</tr> </tr>
@ -412,8 +424,8 @@ abstract class WC_Settings_API {
/** /**
* Generate Price Input HTML. * Generate Price Input HTML.
* *
* @param mixed $key * @param string $key Field key.
* @param mixed $data * @param array $data Field data.
* @since 1.0.0 * @since 1.0.0
* @return string * @return string
*/ */
@ -437,14 +449,14 @@ abstract class WC_Settings_API {
?> ?>
<tr valign="top"> <tr valign="top">
<th scope="row" class="titledesc"> <th scope="row" class="titledesc">
<?php echo $this->get_tooltip_html( $data ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th> </th>
<td class="forminp"> <td class="forminp">
<fieldset> <fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
<input class="wc_input_price input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( wc_format_localized_price( $this->get_option( $key ) ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> <input class="wc_input_price input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( wc_format_localized_price( $this->get_option( $key ) ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?> />
<?php echo $this->get_description_html( $data ); ?> <?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
</fieldset> </fieldset>
</td> </td>
</tr> </tr>
@ -456,8 +468,8 @@ abstract class WC_Settings_API {
/** /**
* Generate Decimal Input HTML. * Generate Decimal Input HTML.
* *
* @param mixed $key * @param string $key Field key.
* @param mixed $data * @param array $data Field data.
* @since 1.0.0 * @since 1.0.0
* @return string * @return string
*/ */
@ -481,14 +493,14 @@ abstract class WC_Settings_API {
?> ?>
<tr valign="top"> <tr valign="top">
<th scope="row" class="titledesc"> <th scope="row" class="titledesc">
<?php echo $this->get_tooltip_html( $data ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th> </th>
<td class="forminp"> <td class="forminp">
<fieldset> <fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
<input class="wc_input_decimal input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( wc_format_localized_decimal( $this->get_option( $key ) ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> <input class="wc_input_decimal input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( wc_format_localized_decimal( $this->get_option( $key ) ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?> />
<?php echo $this->get_description_html( $data ); ?> <?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
</fieldset> </fieldset>
</td> </td>
</tr> </tr>
@ -500,8 +512,8 @@ abstract class WC_Settings_API {
/** /**
* Generate Password Input HTML. * Generate Password Input HTML.
* *
* @param mixed $key * @param string $key Field key.
* @param mixed $data * @param array $data Field data.
* @since 1.0.0 * @since 1.0.0
* @return string * @return string
*/ */
@ -513,8 +525,8 @@ abstract class WC_Settings_API {
/** /**
* Generate Color Picker Input HTML. * Generate Color Picker Input HTML.
* *
* @param mixed $key * @param string $key Field key.
* @param mixed $data * @param array $data Field data.
* @since 1.0.0 * @since 1.0.0
* @return string * @return string
*/ */
@ -537,16 +549,16 @@ abstract class WC_Settings_API {
?> ?>
<tr valign="top"> <tr valign="top">
<th scope="row" class="titledesc"> <th scope="row" class="titledesc">
<?php echo $this->get_tooltip_html( $data ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th> </th>
<td class="forminp"> <td class="forminp">
<fieldset> <fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
<span class="colorpickpreview" style="background:<?php echo esc_attr( $this->get_option( $key ) ); ?>;"></span> <span class="colorpickpreview" style="background:<?php echo esc_attr( $this->get_option( $key ) ); ?>;"></span>
<input class="colorpick <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $this->get_option( $key ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> <input class="colorpick <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $this->get_option( $key ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?> />
<div id="colorPickerDiv_<?php echo esc_attr( $field_key ); ?>" class="colorpickdiv" style="z-index: 100; background: #eee; border: 1px solid #ccc; position: absolute; display: none;"></div> <div id="colorPickerDiv_<?php echo esc_attr( $field_key ); ?>" class="colorpickdiv" style="z-index: 100; background: #eee; border: 1px solid #ccc; position: absolute; display: none;"></div>
<?php echo $this->get_description_html( $data ); ?> <?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
</fieldset> </fieldset>
</td> </td>
</tr> </tr>
@ -558,8 +570,8 @@ abstract class WC_Settings_API {
/** /**
* Generate Textarea HTML. * Generate Textarea HTML.
* *
* @param mixed $key * @param string $key Field key.
* @param mixed $data * @param array $data Field data.
* @since 1.0.0 * @since 1.0.0
* @return string * @return string
*/ */
@ -583,14 +595,14 @@ abstract class WC_Settings_API {
?> ?>
<tr valign="top"> <tr valign="top">
<th scope="row" class="titledesc"> <th scope="row" class="titledesc">
<?php echo $this->get_tooltip_html( $data ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th> </th>
<td class="forminp"> <td class="forminp">
<fieldset> <fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
<textarea rows="3" cols="20" class="input-text wide-input <?php echo esc_attr( $data['class'] ); ?>" type="<?php echo esc_attr( $data['type'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?>><?php echo esc_textarea( $this->get_option( $key ) ); ?></textarea> <textarea rows="3" cols="20" class="input-text wide-input <?php echo esc_attr( $data['class'] ); ?>" type="<?php echo esc_attr( $data['type'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?>><?php echo esc_textarea( $this->get_option( $key ) ); ?></textarea>
<?php echo $this->get_description_html( $data ); ?> <?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
</fieldset> </fieldset>
</td> </td>
</tr> </tr>
@ -602,8 +614,8 @@ abstract class WC_Settings_API {
/** /**
* Generate Checkbox HTML. * Generate Checkbox HTML.
* *
* @param mixed $key * @param string $key Field key.
* @param mixed $data * @param array $data Field data.
* @since 1.0.0 * @since 1.0.0
* @return string * @return string
*/ */
@ -631,15 +643,15 @@ abstract class WC_Settings_API {
?> ?>
<tr valign="top"> <tr valign="top">
<th scope="row" class="titledesc"> <th scope="row" class="titledesc">
<?php echo $this->get_tooltip_html( $data ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th> </th>
<td class="forminp"> <td class="forminp">
<fieldset> <fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
<label for="<?php echo esc_attr( $field_key ); ?>"> <label for="<?php echo esc_attr( $field_key ); ?>">
<input <?php disabled( $data['disabled'], true ); ?> class="<?php echo esc_attr( $data['class'] ); ?>" type="checkbox" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="1" <?php checked( $this->get_option( $key ), 'yes' ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> <?php echo wp_kses_post( $data['label'] ); ?></label><br/> <input <?php disabled( $data['disabled'], true ); ?> class="<?php echo esc_attr( $data['class'] ); ?>" type="checkbox" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="1" <?php checked( $this->get_option( $key ), 'yes' ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?> /> <?php echo wp_kses_post( $data['label'] ); ?></label><br/>
<?php echo $this->get_description_html( $data ); ?> <?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
</fieldset> </fieldset>
</td> </td>
</tr> </tr>
@ -651,8 +663,8 @@ abstract class WC_Settings_API {
/** /**
* Generate Select HTML. * Generate Select HTML.
* *
* @param mixed $key * @param string $key Field key.
* @param mixed $data * @param array $data Field data.
* @since 1.0.0 * @since 1.0.0
* @return string * @return string
*/ */
@ -677,18 +689,18 @@ abstract class WC_Settings_API {
?> ?>
<tr valign="top"> <tr valign="top">
<th scope="row" class="titledesc"> <th scope="row" class="titledesc">
<?php echo $this->get_tooltip_html( $data ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th> </th>
<td class="forminp"> <td class="forminp">
<fieldset> <fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
<select class="select <?php echo esc_attr( $data['class'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?>> <select class="select <?php echo esc_attr( $data['class'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?>>
<?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?> <?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, esc_attr( $this->get_option( $key ) ) ); ?>><?php echo esc_attr( $option_value ); ?></option> <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, esc_attr( $this->get_option( $key ) ) ); ?>><?php echo esc_attr( $option_value ); ?></option>
<?php endforeach; ?> <?php endforeach; ?>
</select> </select>
<?php echo $this->get_description_html( $data ); ?> <?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
</fieldset> </fieldset>
</td> </td>
</tr> </tr>
@ -700,8 +712,8 @@ abstract class WC_Settings_API {
/** /**
* Generate Multiselect HTML. * Generate Multiselect HTML.
* *
* @param mixed $key * @param string $key Field key.
* @param mixed $data * @param array $data Field data.
* @since 1.0.0 * @since 1.0.0
* @return string * @return string
*/ */
@ -728,20 +740,20 @@ abstract class WC_Settings_API {
?> ?>
<tr valign="top"> <tr valign="top">
<th scope="row" class="titledesc"> <th scope="row" class="titledesc">
<?php echo $this->get_tooltip_html( $data ); ?> <?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th> </th>
<td class="forminp"> <td class="forminp">
<fieldset> <fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
<select multiple="multiple" class="multiselect <?php echo esc_attr( $data['class'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>[]" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?>> <select multiple="multiple" class="multiselect <?php echo esc_attr( $data['class'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>[]" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); // WPCS: XSS ok. ?>>
<?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?> <?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( in_array( $option_key, $value ), true ); ?>><?php echo esc_attr( $option_value ); ?></option> <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( in_array( $option_key, $value, true ), true ); ?>><?php echo esc_attr( $option_value ); ?></option>
<?php endforeach; ?> <?php endforeach; ?>
</select> </select>
<?php echo $this->get_description_html( $data ); ?> <?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
<?php if ( $data['select_buttons'] ) : ?> <?php if ( $data['select_buttons'] ) : ?>
<br/><a class="select_all button" href="#"><?php _e( 'Select all', 'woocommerce' ); ?></a> <a class="select_none button" href="#"><?php _e( 'Select none', 'woocommerce' ); ?></a> <br/><a class="select_all button" href="#"><?php esc_html_e( 'Select all', 'woocommerce' ); ?></a> <a class="select_none button" href="#"><?php esc_html_e( 'Select none', 'woocommerce' ); ?></a>
<?php endif; ?> <?php endif; ?>
</fieldset> </fieldset>
</td> </td>
@ -754,8 +766,8 @@ abstract class WC_Settings_API {
/** /**
* Generate Title HTML. * Generate Title HTML.
* *
* @param mixed $key * @param string $key Field key.
* @param mixed $data * @param array $data Field data.
* @since 1.0.0 * @since 1.0.0
* @return string * @return string
*/ */
@ -786,8 +798,8 @@ abstract class WC_Settings_API {
* *
* Make sure the data is escaped correctly, etc. * Make sure the data is escaped correctly, etc.
* *
* @param string $key Field key * @param string $key Field key.
* @param string|null $value Posted Value * @param string $value Posted Value.
* @return string * @return string
*/ */
public function validate_text_field( $key, $value ) { public function validate_text_field( $key, $value ) {
@ -800,8 +812,8 @@ abstract class WC_Settings_API {
* *
* Make sure the data is escaped correctly, etc. * Make sure the data is escaped correctly, etc.
* *
* @param string $key * @param string $key Field key.
* @param string|null $value Posted Value * @param string $value Posted Value.
* @return string * @return string
*/ */
public function validate_price_field( $key, $value ) { public function validate_price_field( $key, $value ) {
@ -814,8 +826,8 @@ abstract class WC_Settings_API {
* *
* Make sure the data is escaped correctly, etc. * Make sure the data is escaped correctly, etc.
* *
* @param string $key * @param string $key Field key.
* @param string|null $value Posted Value * @param string $value Posted Value.
* @return string * @return string
*/ */
public function validate_decimal_field( $key, $value ) { public function validate_decimal_field( $key, $value ) {
@ -826,8 +838,8 @@ abstract class WC_Settings_API {
/** /**
* Validate Password Field. No input sanitization is used to avoid corrupting passwords. * Validate Password Field. No input sanitization is used to avoid corrupting passwords.
* *
* @param string $key * @param string $key Field key.
* @param string|null $value Posted Value * @param string $value Posted Value.
* @return string * @return string
*/ */
public function validate_password_field( $key, $value ) { public function validate_password_field( $key, $value ) {
@ -838,8 +850,8 @@ abstract class WC_Settings_API {
/** /**
* Validate Textarea Field. * Validate Textarea Field.
* *
* @param string $key * @param string $key Field key.
* @param string|null $value Posted Value * @param string $value Posted Value.
* @return string * @return string
*/ */
public function validate_textarea_field( $key, $value ) { public function validate_textarea_field( $key, $value ) {
@ -847,7 +859,12 @@ abstract class WC_Settings_API {
return wp_kses( trim( stripslashes( $value ) ), return wp_kses( trim( stripslashes( $value ) ),
array_merge( array_merge(
array( array(
'iframe' => array( 'src' => true, 'style' => true, 'id' => true, 'class' => true ), 'iframe' => array(
'src' => true,
'style' => true,
'id' => true,
'class' => true,
),
), ),
wp_kses_allowed_html( 'post' ) wp_kses_allowed_html( 'post' )
) )
@ -859,8 +876,8 @@ abstract class WC_Settings_API {
* *
* If not set, return "no", otherwise return "yes". * If not set, return "no", otherwise return "yes".
* *
* @param string $key * @param string $key Field key.
* @param string|null $value Posted Value * @param string $value Posted Value.
* @return string * @return string
*/ */
public function validate_checkbox_field( $key, $value ) { public function validate_checkbox_field( $key, $value ) {
@ -870,8 +887,8 @@ abstract class WC_Settings_API {
/** /**
* Validate Select Field. * Validate Select Field.
* *
* @param string $key * @param string $key Field key.
* @param string $value Posted Value * @param string $value Posted Value.
* @return string * @return string
*/ */
public function validate_select_field( $key, $value ) { public function validate_select_field( $key, $value ) {
@ -882,8 +899,8 @@ abstract class WC_Settings_API {
/** /**
* Validate Multiselect Field. * Validate Multiselect Field.
* *
* @param string $key * @param string $key Field key.
* @param string $value Posted Value * @param string $value Posted Value.
* @return string|array * @return string|array
*/ */
public function validate_multiselect_field( $key, $value ) { public function validate_multiselect_field( $key, $value ) {
@ -892,9 +909,9 @@ abstract class WC_Settings_API {
/** /**
* Validate the data on the "Settings" form. * Validate the data on the "Settings" form.
* @deprecated 2.6.0 No longer used
* *
* @param array $form_fields * @deprecated 2.6.0 No longer used.
* @param array $form_fields Array of fields.
*/ */
public function validate_settings_fields( $form_fields = array() ) { public function validate_settings_fields( $form_fields = array() ) {
wc_deprecated_function( 'validate_settings_fields', '2.6' ); wc_deprecated_function( 'validate_settings_fields', '2.6' );
@ -902,8 +919,9 @@ abstract class WC_Settings_API {
/** /**
* Format settings if needed. * Format settings if needed.
* @deprecated 2.6.0 Unused *
* @param array $value * @deprecated 2.6.0 Unused.
* @param array $value Value to format.
* @return array * @return array
*/ */
public function format_settings( $value ) { public function format_settings( $value ) {

View File

@ -244,7 +244,7 @@ class WC_Settings_Shipping extends WC_Settings_Page {
} }
wp_localize_script( 'wc-shipping-zone-methods', 'shippingZoneMethodsLocalizeScript', array( wp_localize_script( 'wc-shipping-zone-methods', 'shippingZoneMethodsLocalizeScript', array(
'methods' => $zone->get_shipping_methods(), 'methods' => $zone->get_shipping_methods( false, 'json' ),
'zone_name' => $zone->get_zone_name(), 'zone_name' => $zone->get_zone_name(),
'zone_id' => $zone->get_id(), 'zone_id' => $zone->get_id(),
'wc_shipping_zones_nonce' => wp_create_nonce( 'wc_shipping_zones_nonce' ), 'wc_shipping_zones_nonce' => wp_create_nonce( 'wc_shipping_zones_nonce' ),

View File

@ -2449,7 +2449,7 @@ class WC_AJAX {
'instance_id' => $instance_id, 'instance_id' => $instance_id,
'zone_id' => $zone->get_id(), 'zone_id' => $zone->get_id(),
'zone_name' => $zone->get_zone_name(), 'zone_name' => $zone->get_zone_name(),
'methods' => $zone->get_shipping_methods(), 'methods' => $zone->get_shipping_methods( false, 'json' ),
) ); ) );
} }
@ -2547,7 +2547,7 @@ class WC_AJAX {
wp_send_json_success( array( wp_send_json_success( array(
'zone_id' => $zone->get_id(), 'zone_id' => $zone->get_id(),
'zone_name' => $zone->get_zone_name(), 'zone_name' => $zone->get_zone_name(),
'methods' => $zone->get_shipping_methods(), 'methods' => $zone->get_shipping_methods( false, 'json' ),
) ); ) );
} }
@ -2579,7 +2579,7 @@ class WC_AJAX {
wp_send_json_success( array( wp_send_json_success( array(
'zone_id' => $zone->get_id(), 'zone_id' => $zone->get_id(),
'zone_name' => $zone->get_zone_name(), 'zone_name' => $zone->get_zone_name(),
'methods' => $zone->get_shipping_methods(), 'methods' => $zone->get_shipping_methods( false, 'json' ),
'errors' => $shipping_method->get_errors(), 'errors' => $shipping_method->get_errors(),
) ); ) );
} }

View File

@ -1,36 +1,38 @@
<?php <?php
include_once( 'legacy/class-wc-legacy-shipping-zone.php' );
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/** /**
* Represents a single shipping zone * Represents a single shipping zone
* *
* @class WC_Shipping_Zone * @since 2.6.0
* @since 2.6.0 * @version 3.0.0
* @version 3.0.0 * @package WooCommerce/Classes
* @package WooCommerce/Classes */
* @category Class
* @author WooCommerce defined( 'ABSPATH' ) || exit;
require_once 'legacy/class-wc-legacy-shipping-zone.php';
/**
* WC_Shipping_Zone class.
*/ */
class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone { class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
/** /**
* Zone ID * Zone ID
*
* @var int|null * @var int|null
*/ */
protected $id = null; protected $id = null;
/** /**
* This is the name of this object type. * This is the name of this object type.
*
* @var string * @var string
*/ */
protected $object_type = 'shipping_zone'; protected $object_type = 'shipping_zone';
/** /**
* Zone Data * Zone Data.
*
* @var array * @var array
*/ */
protected $data = array( protected $data = array(
@ -49,7 +51,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
$this->set_id( $zone ); $this->set_id( $zone );
} elseif ( is_object( $zone ) ) { } elseif ( is_object( $zone ) ) {
$this->set_id( $zone->zone_id ); $this->set_id( $zone->zone_id );
} elseif ( 0 === $zone || "0" === $zone ) { } elseif ( 0 === $zone || '0' === $zone ) {
$this->set_id( 0 ); $this->set_id( 0 );
} else { } else {
$this->set_object_read( true ); $this->set_object_read( true );
@ -61,16 +63,16 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
} }
} }
/* /**
|-------------------------------------------------------------------------- * --------------------------------------------------------------------------
| Getters * Getters
|-------------------------------------------------------------------------- * --------------------------------------------------------------------------
*/ */
/** /**
* Get zone name. * Get zone name.
* *
* @param string $context * @param string $context View or edit context.
* @return string * @return string
*/ */
public function get_zone_name( $context = 'view' ) { public function get_zone_name( $context = 'view' ) {
@ -80,7 +82,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
/** /**
* Get zone order. * Get zone order.
* *
* @param string $context * @param string $context View or edit context.
* @return int * @return int
*/ */
public function get_zone_order( $context = 'view' ) { public function get_zone_order( $context = 'view' ) {
@ -90,7 +92,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
/** /**
* Get zone locations. * Get zone locations.
* *
* @param string $context * @param string $context View or edit context.
* @return array of zone objects * @return array of zone objects
*/ */
public function get_zone_locations( $context = 'view' ) { public function get_zone_locations( $context = 'view' ) {
@ -100,8 +102,8 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
/** /**
* Return a text string representing what this zone is for. * Return a text string representing what this zone is for.
* *
* @param int $max * @param int $max Max locations to return.
* @param string $context * @param string $context View or edit context.
* @return string * @return string
*/ */
public function get_formatted_location( $max = 10, $context = 'view' ) { public function get_formatted_location( $max = 10, $context = 'view' ) {
@ -124,7 +126,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
} }
foreach ( $states as $location ) { foreach ( $states as $location ) {
$location_codes = explode( ':', $location->code ); $location_codes = explode( ':', $location->code );
$location_parts[] = $all_states[ $location_codes[0] ][ $location_codes[1] ]; $location_parts[] = $all_states[ $location_codes[0] ][ $location_codes[1] ];
} }
@ -135,8 +137,8 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
// Fix display of encoded characters. // Fix display of encoded characters.
$location_parts = array_map( 'html_entity_decode', $location_parts ); $location_parts = array_map( 'html_entity_decode', $location_parts );
if ( sizeof( $location_parts ) > $max ) { if ( count( $location_parts ) > $max ) {
$remaining = sizeof( $location_parts ) - $max; $remaining = count( $location_parts ) - $max;
// @codingStandardsIgnoreStart // @codingStandardsIgnoreStart
return sprintf( _n( '%s and %d other region', '%s and %d other regions', $remaining, 'woocommerce' ), implode( ', ', array_splice( $location_parts, 0, $max ) ), $remaining ); return sprintf( _n( '%s and %d other region', '%s and %d other regions', $remaining, 'woocommerce' ), implode( ', ', array_splice( $location_parts, 0, $max ) ), $remaining );
// @codingStandardsIgnoreEnd // @codingStandardsIgnoreEnd
@ -150,10 +152,11 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
/** /**
* Get shipping methods linked to this zone. * Get shipping methods linked to this zone.
* *
* @param bool Only return enabled methods. * @param bool $enabled_only Only return enabled methods.
* @param string $context Getting shipping methods for what context. Valid values, admin, json.
* @return array of objects * @return array of objects
*/ */
public function get_shipping_methods( $enabled_only = false ) { public function get_shipping_methods( $enabled_only = false, $context = 'admin' ) {
if ( null === $this->get_id() ) { if ( null === $this->get_id() ) {
return array(); return array();
} }
@ -165,30 +168,37 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
foreach ( $raw_methods as $raw_method ) { foreach ( $raw_methods as $raw_method ) {
if ( in_array( $raw_method->method_id, array_keys( $allowed_classes ), true ) ) { if ( in_array( $raw_method->method_id, array_keys( $allowed_classes ), true ) ) {
$class_name = $allowed_classes[ $raw_method->method_id ]; $class_name = $allowed_classes[ $raw_method->method_id ];
$instance_id = $raw_method->instance_id;
// The returned array may contain instances of shipping methods, as well // The returned array may contain instances of shipping methods, as well
// as classes. If the "class" is an instance, just use it. If not, // as classes. If the "class" is an instance, just use it. If not,
// create an instance. // create an instance.
if ( is_object( $class_name ) ) { if ( is_object( $class_name ) ) {
$class_name_of_instance = get_class( $class_name ); $class_name_of_instance = get_class( $class_name );
$methods[ $raw_method->instance_id ] = new $class_name_of_instance( $raw_method->instance_id ); $methods[ $instance_id ] = new $class_name_of_instance( $instance_id );
} else { } else {
// If the class is not an object, it should be a string. It's better // If the class is not an object, it should be a string. It's better
// to double check, to be sure (a class must be a string, anything) // to double check, to be sure (a class must be a string, anything)
// else would be useless // else would be useless.
if ( is_string( $class_name ) && class_exists( $class_name ) ) { if ( is_string( $class_name ) && class_exists( $class_name ) ) {
$methods[ $raw_method->instance_id ] = new $class_name( $raw_method->instance_id ); $methods[ $instance_id ] = new $class_name( $instance_id );
} }
} }
// Let's make sure that we have an instance before setting its attributes // Let's make sure that we have an instance before setting its attributes.
if ( is_object( $methods[ $raw_method->instance_id ] ) ) { if ( is_object( $methods[ $instance_id ] ) ) {
$methods[ $raw_method->instance_id ]->method_order = absint( $raw_method->method_order ); $methods[ $instance_id ]->method_order = absint( $raw_method->method_order );
$methods[ $raw_method->instance_id ]->enabled = $raw_method->is_enabled ? 'yes' : 'no'; $methods[ $instance_id ]->enabled = $raw_method->is_enabled ? 'yes' : 'no';
$methods[ $raw_method->instance_id ]->has_settings = $methods[ $raw_method->instance_id ]->has_settings(); $methods[ $instance_id ]->has_settings = $methods[ $instance_id ]->has_settings();
$methods[ $raw_method->instance_id ]->settings_html = $methods[ $raw_method->instance_id ]->supports( 'instance-settings-modal' ) ? $methods[ $raw_method->instance_id ]->get_admin_options_html() : false; $methods[ $instance_id ]->settings_html = $methods[ $instance_id ]->supports( 'instance-settings-modal' ) ? $methods[ $instance_id ]->get_admin_options_html(): false;
$methods[ $raw_method->instance_id ]->method_description = wp_kses_post( wpautop( $methods[ $raw_method->instance_id ]->method_description ) ); $methods[ $instance_id ]->method_description = wp_kses_post( wpautop( $methods[ $instance_id ]->method_description ) );
}
if ( 'json' === $context ) {
// We don't want the entire object in this context, just the public props.
$methods[ $instance_id ] = (object) get_object_vars( $methods[ $instance_id ] );
unset( $methods[ $instance_id ]->instance_form_fields, $methods[ $instance_id ]->form_fields );
} }
} }
} }
@ -198,25 +208,25 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
return apply_filters( 'woocommerce_shipping_zone_shipping_methods', $methods, $raw_methods, $allowed_classes, $this ); return apply_filters( 'woocommerce_shipping_zone_shipping_methods', $methods, $raw_methods, $allowed_classes, $this );
} }
/* /**
|-------------------------------------------------------------------------- * --------------------------------------------------------------------------
| Setters * Setters
|-------------------------------------------------------------------------- * --------------------------------------------------------------------------
*/ */
/** /**
* Set zone name. * Set zone name.
* *
* @param string $set * @param string $set Value to set.
*/ */
public function set_zone_name( $set ) { public function set_zone_name( $set ) {
$this->set_prop( 'zone_name', wc_clean( $set ) ); $this->set_prop( 'zone_name', wc_clean( $set ) );
} }
/** /**
* Set zone order. * Set zone order. Value to set.
* *
* @param int $set * @param int $set Value to set.
*/ */
public function set_zone_order( $set ) { public function set_zone_order( $set ) {
$this->set_prop( 'zone_order', absint( $set ) ); $this->set_prop( 'zone_order', absint( $set ) );
@ -226,7 +236,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
* Set zone locations. * Set zone locations.
* *
* @since 3.0.0 * @since 3.0.0
* @param array * @param array $locations Value to set.
*/ */
public function set_zone_locations( $locations ) { public function set_zone_locations( $locations ) {
if ( 0 !== $this->get_id() ) { if ( 0 !== $this->get_id() ) {
@ -234,10 +244,10 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
} }
} }
/* /**
|-------------------------------------------------------------------------- * --------------------------------------------------------------------------
| Other Methods * Other
|-------------------------------------------------------------------------- * --------------------------------------------------------------------------
*/ */
/** /**
@ -280,7 +290,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
/** /**
* Location type detection. * Location type detection.
* *
* @param object $location * @param object $location Location to check.
* @return boolean * @return boolean
*/ */
private function location_is_continent( $location ) { private function location_is_continent( $location ) {
@ -290,7 +300,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
/** /**
* Location type detection. * Location type detection.
* *
* @param object $location * @param object $location Location to check.
* @return boolean * @return boolean
*/ */
private function location_is_country( $location ) { private function location_is_country( $location ) {
@ -300,7 +310,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
/** /**
* Location type detection. * Location type detection.
* *
* @param object $location * @param object $location Location to check.
* @return boolean * @return boolean
*/ */
private function location_is_state( $location ) { private function location_is_state( $location ) {
@ -310,7 +320,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
/** /**
* Location type detection. * Location type detection.
* *
* @param object $location * @param object $location Location to check.
* @return boolean * @return boolean
*/ */
private function location_is_postcode( $location ) { private function location_is_postcode( $location ) {
@ -320,29 +330,29 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
/** /**
* Is passed location type valid? * Is passed location type valid?
* *
* @param string $type * @param string $type Type to check.
* @return boolean * @return boolean
*/ */
public function is_valid_location_type( $type ) { public function is_valid_location_type( $type ) {
return in_array( $type, array( 'postcode', 'state', 'country', 'continent' ) ); return in_array( $type, array( 'postcode', 'state', 'country', 'continent' ), true );
} }
/** /**
* Add location (state or postcode) to a zone. * Add location (state or postcode) to a zone.
* *
* @param string $code * @param string $code Location code.
* @param string $type state or postcode * @param string $type state or postcode.
*/ */
public function add_location( $code, $type ) { public function add_location( $code, $type ) {
if ( 0 !== $this->get_id() && $this->is_valid_location_type( $type ) ) { if ( 0 !== $this->get_id() && $this->is_valid_location_type( $type ) ) {
if ( 'postcode' === $type ) { if ( 'postcode' === $type ) {
$code = trim( strtoupper( str_replace( chr( 226 ) . chr( 128 ) . chr( 166 ), '...', $code ) ) ); // No normalization - postcodes are matched against both normal and formatted versions to support wildcards. $code = trim( strtoupper( str_replace( chr( 226 ) . chr( 128 ) . chr( 166 ), '...', $code ) ) ); // No normalization - postcodes are matched against both normal and formatted versions to support wildcards.
} }
$location = array( $location = array(
'code' => wc_clean( $code ), 'code' => wc_clean( $code ),
'type' => wc_clean( $type ), 'type' => wc_clean( $type ),
); );
$zone_locations = $this->get_prop( 'zone_locations', 'edit' ); $zone_locations = $this->get_prop( 'zone_locations', 'edit' );
$zone_locations[] = (object) $location; $zone_locations[] = (object) $location;
$this->set_prop( 'zone_locations', $zone_locations ); $this->set_prop( 'zone_locations', $zone_locations );
} }
@ -352,7 +362,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
/** /**
* Clear all locations for this zone. * Clear all locations for this zone.
* *
* @param array|string $types of location to clear * @param array|string $types of location to clear.
*/ */
public function clear_locations( $types = array( 'postcode', 'state', 'country', 'continent' ) ) { public function clear_locations( $types = array( 'postcode', 'state', 'country', 'continent' ) ) {
if ( ! is_array( $types ) ) { if ( ! is_array( $types ) ) {
@ -360,7 +370,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
} }
$zone_locations = $this->get_prop( 'zone_locations', 'edit' ); $zone_locations = $this->get_prop( 'zone_locations', 'edit' );
foreach ( $zone_locations as $key => $values ) { foreach ( $zone_locations as $key => $values ) {
if ( in_array( $values->type, $types ) ) { if ( in_array( $values->type, $types, true ) ) {
unset( $zone_locations[ $key ] ); unset( $zone_locations[ $key ] );
} }
} }
@ -371,7 +381,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
/** /**
* Set locations. * Set locations.
* *
* @param array $locations Array of locations * @param array $locations Array of locations.
*/ */
public function set_locations( $locations = array() ) { public function set_locations( $locations = array() ) {
$this->clear_locations(); $this->clear_locations();
@ -383,7 +393,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
/** /**
* Add a shipping method to this zone. * Add a shipping method to this zone.
* *
* @param string $type shipping method type * @param string $type shipping method type.
* @return int new instance_id, 0 on failure * @return int new instance_id, 0 on failure
*/ */
public function add_shipping_method( $type ) { public function add_shipping_method( $type ) {
@ -396,7 +406,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
$allowed_classes = $wc_shipping->get_shipping_method_class_names(); $allowed_classes = $wc_shipping->get_shipping_method_class_names();
$count = $this->data_store->get_method_count( $this->get_id() ); $count = $this->data_store->get_method_count( $this->get_id() );
if ( in_array( $type, array_keys( $allowed_classes ) ) ) { if ( in_array( $type, array_keys( $allowed_classes ), true ) ) {
$instance_id = $this->data_store->add_method( $this->get_id(), $type, $count + 1 ); $instance_id = $this->data_store->add_method( $this->get_id(), $type, $count + 1 );
} }
@ -412,7 +422,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
/** /**
* Delete a shipping method from a zone. * Delete a shipping method from a zone.
* *
* @param int $instance_id * @param int $instance_id Shipping method instance ID.
* @return True on success, false on failure * @return True on success, false on failure
*/ */
public function delete_shipping_method( $instance_id ) { public function delete_shipping_method( $instance_id ) {

View File

@ -1,32 +1,36 @@
<?php <?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/** /**
* Flat Rate Shipping Method. * Flat Rate Shipping Method.
* *
* @class WC_Shipping_Flat_Rate * @version 2.6.0
* @version 2.6.0 * @package WooCommerce/Classes/Shipping
* @package WooCommerce/Classes/Shipping */
* @author WooThemes
defined( 'ABSPATH' ) || exit;
/**
* WC_Shipping_Flat_Rate class.
*/ */
class WC_Shipping_Flat_Rate extends WC_Shipping_Method { class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
/** @var string cost passed to [fee] shortcode */ /**
* Cost passed to [fee] shortcode.
*
* @var string Cost.
*/
protected $fee_cost = ''; protected $fee_cost = '';
/** /**
* Constructor. * Constructor.
* *
* @param int $instance_id * @param int $instance_id Shipping method instance ID.
*/ */
public function __construct( $instance_id = 0 ) { public function __construct( $instance_id = 0 ) {
$this->id = 'flat_rate'; $this->id = 'flat_rate';
$this->instance_id = absint( $instance_id ); $this->instance_id = absint( $instance_id );
$this->method_title = __( 'Flat rate', 'woocommerce' ); $this->method_title = __( 'Flat rate', 'woocommerce' );
$this->method_description = __( 'Lets you charge a fixed rate for shipping.', 'woocommerce' ); $this->method_description = __( 'Lets you charge a fixed rate for shipping.', 'woocommerce' );
$this->supports = array( $this->supports = array(
'shipping-zones', 'shipping-zones',
'instance-settings', 'instance-settings',
'instance-settings-modal', 'instance-settings-modal',
@ -37,10 +41,10 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
} }
/** /**
* init user set variables. * Init user set variables.
*/ */
public function init() { public function init() {
$this->instance_form_fields = include( 'includes/settings-flat-rate.php' ); $this->instance_form_fields = include 'includes/settings-flat-rate.php';
$this->title = $this->get_option( 'title' ); $this->title = $this->get_option( 'title' );
$this->tax_status = $this->get_option( 'tax_status' ); $this->tax_status = $this->get_option( 'tax_status' );
$this->cost = $this->get_option( 'cost' ); $this->cost = $this->get_option( 'cost' );
@ -49,20 +53,21 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
/** /**
* Evaluate a cost from a sum/string. * Evaluate a cost from a sum/string.
* @param string $sum *
* @param array $args * @param string $sum Sum of shipping.
* @param array $args Args.
* @return string * @return string
*/ */
protected function evaluate_cost( $sum, $args = array() ) { protected function evaluate_cost( $sum, $args = array() ) {
include_once( WC()->plugin_path() . '/includes/libraries/class-wc-eval-math.php' ); include_once WC()->plugin_path() . '/includes/libraries/class-wc-eval-math.php';
// Allow 3rd parties to process shipping cost arguments // Allow 3rd parties to process shipping cost arguments.
$args = apply_filters( 'woocommerce_evaluate_shipping_cost_args', $args, $sum, $this ); $args = apply_filters( 'woocommerce_evaluate_shipping_cost_args', $args, $sum, $this );
$locale = localeconv(); $locale = localeconv();
$decimals = array( wc_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'], ',' ); $decimals = array( wc_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'], ',' );
$this->fee_cost = $args['cost']; $this->fee_cost = $args['cost'];
// Expand shortcodes // Expand shortcodes.
add_shortcode( 'fee', array( $this, 'fee' ) ); add_shortcode( 'fee', array( $this, 'fee' ) );
$sum = do_shortcode( str_replace( $sum = do_shortcode( str_replace(
@ -79,22 +84,23 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
remove_shortcode( 'fee', array( $this, 'fee' ) ); remove_shortcode( 'fee', array( $this, 'fee' ) );
// Remove whitespace from string // Remove whitespace from string.
$sum = preg_replace( '/\s+/', '', $sum ); $sum = preg_replace( '/\s+/', '', $sum );
// Remove locale from string // Remove locale from string.
$sum = str_replace( $decimals, '.', $sum ); $sum = str_replace( $decimals, '.', $sum );
// Trim invalid start/end characters // Trim invalid start/end characters.
$sum = rtrim( ltrim( $sum, "\t\n\r\0\x0B+*/" ), "\t\n\r\0\x0B+-*/" ); $sum = rtrim( ltrim( $sum, "\t\n\r\0\x0B+*/" ), "\t\n\r\0\x0B+-*/" );
// Do the math // Do the math.
return $sum ? WC_Eval_Math::evaluate( $sum ) : 0; return $sum ? WC_Eval_Math::evaluate( $sum ) : 0;
} }
/** /**
* Work out fee (shortcode). * Work out fee (shortcode).
* @param array $atts *
* @param array $atts Attributes.
* @return string * @return string
*/ */
public function fee( $atts ) { public function fee( $atts ) {
@ -122,9 +128,9 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
} }
/** /**
* calculate_shipping function. * Calculate the shipping costs.
* *
* @param array $package (default: array()) * @param array $package Package of items from cart.
*/ */
public function calculate_shipping( $package = array() ) { public function calculate_shipping( $package = array() ) {
$rate = array( $rate = array(
@ -134,7 +140,7 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
'package' => $package, 'package' => $package,
); );
// Calculate the costs // Calculate the costs.
$has_costs = false; // True when a cost is set. False if all costs are blank strings. $has_costs = false; // True when a cost is set. False if all costs are blank strings.
$cost = $this->get_option( 'cost' ); $cost = $this->get_option( 'cost' );
@ -154,7 +160,7 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
$highest_class_cost = 0; $highest_class_cost = 0;
foreach ( $found_shipping_classes as $shipping_class => $products ) { foreach ( $found_shipping_classes as $shipping_class => $products ) {
// Also handles BW compatibility when slugs were used instead of ids // Also handles BW compatibility when slugs were used instead of ids.
$shipping_class_term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' ); $shipping_class_term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' );
$class_cost_string = $shipping_class_term && $shipping_class_term->term_id ? $this->get_option( 'class_cost_' . $shipping_class_term->term_id, $this->get_option( 'class_cost_' . $shipping_class, '' ) ) : $this->get_option( 'no_class_cost', '' ); $class_cost_string = $shipping_class_term && $shipping_class_term->term_id ? $this->get_option( 'class_cost_' . $shipping_class_term->term_id, $this->get_option( 'class_cost_' . $shipping_class, '' ) ) : $this->get_option( 'no_class_cost', '' );
@ -180,7 +186,6 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
} }
} }
// Add the rate
if ( $has_costs ) { if ( $has_costs ) {
$this->add_rate( $rate ); $this->add_rate( $rate );
} }
@ -190,27 +195,14 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
* *
* Previously there were (overly complex) options to add additional rates however this was not user. * Previously there were (overly complex) options to add additional rates however this was not user.
* friendly and goes against what Flat Rate Shipping was originally intended for. * friendly and goes against what Flat Rate Shipping was originally intended for.
*
* This example shows how you can add an extra rate based on this flat rate via custom function:
*
* add_action( 'woocommerce_flat_rate_shipping_add_rate', 'add_another_custom_flat_rate', 10, 2 );
*
* function add_another_custom_flat_rate( $method, $rate ) {
* $new_rate = $rate;
* $new_rate['id'] .= ':' . 'custom_rate_name'; // Append a custom ID.
* $new_rate['label'] = 'Rushed Shipping'; // Rename to 'Rushed Shipping'.
* $new_rate['cost'] += 2; // Add $2 to the cost.
*
* // Add it to WC.
* $method->add_rate( $new_rate );
* }.
*/ */
do_action( 'woocommerce_' . $this->id . '_shipping_add_rate', $this, $rate ); do_action( 'woocommerce_' . $this->id . '_shipping_add_rate', $this, $rate );
} }
/** /**
* Get items in package. * Get items in package.
* @param array $package *
* @param array $package Package of items from cart.
* @return int * @return int
*/ */
public function get_package_item_qty( $package ) { public function get_package_item_qty( $package ) {
@ -225,7 +217,8 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
/** /**
* Finds and returns shipping classes and the products with said class. * Finds and returns shipping classes and the products with said class.
* @param mixed $package *
* @param mixed $package Package of items from cart.
* @return array * @return array
*/ */
public function find_shipping_classes( $package ) { public function find_shipping_classes( $package ) {
@ -245,4 +238,18 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
return $found_shipping_classes; return $found_shipping_classes;
} }
/**
* Sanitize the cost field.
*
* @since 3.4.0
* @param string $value Unsanitized value.
* @return string
*/
public function sanitize_cost( $value ) {
$value = is_null( $value ) ? '' : $value;
$value = wp_kses_post( trim( wp_unslash( $value ) ) );
$value = str_replace( array( get_woocommerce_currency_symbol(), html_entity_decode( get_woocommerce_currency_symbol() ) ), '', $value );
return $value;
}
} }

View File

@ -1,39 +1,40 @@
<?php <?php
/**
* Settings for flat rate shipping.
*
* @package WooCommerce/Classes/Shipping
*/
if ( ! defined( 'ABSPATH' ) ) { defined( 'ABSPATH' ) || exit;
exit;
}
$cost_desc = __( 'Enter a cost (excl. tax) or sum, e.g. <code>10.00 * [qty]</code>.', 'woocommerce' ) . '<br/><br/>' . __( 'Use <code>[qty]</code> for the number of items, <br/><code>[cost]</code> for the total cost of items, and <code>[fee percent="10" min_fee="20" max_fee=""]</code> for percentage based fees.', 'woocommerce' ); $cost_desc = __( 'Enter a cost (excl. tax) or sum, e.g. <code>10.00 * [qty]</code>.', 'woocommerce' ) . '<br/><br/>' . __( 'Use <code>[qty]</code> for the number of items, <br/><code>[cost]</code> for the total cost of items, and <code>[fee percent="10" min_fee="20" max_fee=""]</code> for percentage based fees.', 'woocommerce' );
/**
* Settings for flat rate shipping.
*/
$settings = array( $settings = array(
'title' => array( 'title' => array(
'title' => __( 'Method title', 'woocommerce' ), 'title' => __( 'Method title', 'woocommerce' ),
'type' => 'text', 'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ), 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'Flat rate', 'woocommerce' ), 'default' => __( 'Flat rate', 'woocommerce' ),
'desc_tip' => true, 'desc_tip' => true,
), ),
'tax_status' => array( 'tax_status' => array(
'title' => __( 'Tax status', 'woocommerce' ), 'title' => __( 'Tax status', 'woocommerce' ),
'type' => 'select', 'type' => 'select',
'class' => 'wc-enhanced-select', 'class' => 'wc-enhanced-select',
'default' => 'taxable', 'default' => 'taxable',
'options' => array( 'options' => array(
'taxable' => __( 'Taxable', 'woocommerce' ), 'taxable' => __( 'Taxable', 'woocommerce' ),
'none' => _x( 'None', 'Tax status', 'woocommerce' ), 'none' => _x( 'None', 'Tax status', 'woocommerce' ),
), ),
), ),
'cost' => array( 'cost' => array(
'title' => __( 'Cost', 'woocommerce' ), 'title' => __( 'Cost', 'woocommerce' ),
'type' => 'text', 'type' => 'text',
'placeholder' => '', 'placeholder' => '',
'description' => $cost_desc, 'description' => $cost_desc,
'default' => '0', 'default' => '0',
'desc_tip' => true, 'desc_tip' => true,
'sanitize_callback' => array( $this, 'sanitize_cost' ),
), ),
); );
@ -41,10 +42,11 @@ $shipping_classes = WC()->shipping->get_shipping_classes();
if ( ! empty( $shipping_classes ) ) { if ( ! empty( $shipping_classes ) ) {
$settings['class_costs'] = array( $settings['class_costs'] = array(
'title' => __( 'Shipping class costs', 'woocommerce' ), 'title' => __( 'Shipping class costs', 'woocommerce' ),
'type' => 'title', 'type' => 'title',
'default' => '', 'default' => '',
'description' => sprintf( __( 'These costs can optionally be added based on the <a href="%s">product shipping class</a>.', 'woocommerce' ), admin_url( 'admin.php?page=wc-settings&tab=shipping&section=classes' ) ), /* translators: %s: URL for link. */
'description' => sprintf( __( 'These costs can optionally be added based on the <a href="%s">product shipping class</a>.', 'woocommerce' ), admin_url( 'admin.php?page=wc-settings&tab=shipping&section=classes' ) ),
); );
foreach ( $shipping_classes as $shipping_class ) { foreach ( $shipping_classes as $shipping_class ) {
if ( ! isset( $shipping_class->term_id ) ) { if ( ! isset( $shipping_class->term_id ) ) {
@ -52,30 +54,34 @@ if ( ! empty( $shipping_classes ) ) {
} }
$settings[ 'class_cost_' . $shipping_class->term_id ] = array( $settings[ 'class_cost_' . $shipping_class->term_id ] = array(
/* translators: %s: shipping class name */ /* translators: %s: shipping class name */
'title' => sprintf( __( '"%s" shipping class cost', 'woocommerce' ), esc_html( $shipping_class->name ) ), 'title' => sprintf( __( '"%s" shipping class cost', 'woocommerce' ), esc_html( $shipping_class->name ) ),
'type' => 'text', 'type' => 'text',
'placeholder' => __( 'N/A', 'woocommerce' ), 'placeholder' => __( 'N/A', 'woocommerce' ),
'description' => $cost_desc, 'description' => $cost_desc,
'default' => $this->get_option( 'class_cost_' . $shipping_class->slug ), // Before 2.5.0, we used slug here which caused issues with long setting names 'default' => $this->get_option( 'class_cost_' . $shipping_class->slug ), // Before 2.5.0, we used slug here which caused issues with long setting names.
'desc_tip' => true, 'desc_tip' => true,
'sanitize_callback' => array( $this, 'sanitize_cost' ),
); );
} }
$settings['no_class_cost'] = array( $settings['no_class_cost'] = array(
'title' => __( 'No shipping class cost', 'woocommerce' ), 'title' => __( 'No shipping class cost', 'woocommerce' ),
'type' => 'text', 'type' => 'text',
'placeholder' => __( 'N/A', 'woocommerce' ), 'placeholder' => __( 'N/A', 'woocommerce' ),
'description' => $cost_desc, 'description' => $cost_desc,
'default' => '', 'default' => '',
'desc_tip' => true, 'desc_tip' => true,
'sanitize_callback' => array( $this, 'sanitize_cost' ),
); );
$settings['type'] = array( $settings['type'] = array(
'title' => __( 'Calculation type', 'woocommerce' ), 'title' => __( 'Calculation type', 'woocommerce' ),
'type' => 'select', 'type' => 'select',
'class' => 'wc-enhanced-select', 'class' => 'wc-enhanced-select',
'default' => 'class', 'default' => 'class',
'options' => array( 'options' => array(
'class' => __( 'Per class: Charge shipping for each shipping class individually', 'woocommerce' ), 'class' => __( 'Per class: Charge shipping for each shipping class individually', 'woocommerce' ),
'order' => __( 'Per order: Charge shipping for the most expensive shipping class', 'woocommerce' ), 'order' => __( 'Per order: Charge shipping for the most expensive shipping class', 'woocommerce' ),
), ),
); );
} }