woocommerce/includes/abstracts/abstract-wc-settings-api.php

958 lines
29 KiB
PHP
Raw Normal View History

<?php
/**
* Abstract Settings API Class
*
* Admin Settings API used by Integrations, Shipping Methods, and Payment Gateways.
*
2014-11-20 01:11:08 +00:00
* @package WooCommerce/Abstracts
2018-01-22 11:33:15 +00:00
*/
defined( 'ABSPATH' ) || exit;
/**
* WC_Settings_API class.
*/
abstract class WC_Settings_API {
2014-11-20 01:11:08 +00:00
/**
* The plugin ID. Used for option names.
2018-01-22 11:33:15 +00:00
*
2014-11-20 01:11:08 +00:00
* @var string
*/
public $plugin_id = 'woocommerce_';
2012-08-14 20:19:41 +00:00
2014-11-20 01:11:08 +00:00
/**
* ID of the class extending the settings API. Used in option names.
2018-01-22 11:33:15 +00:00
*
2014-11-20 01:11:08 +00:00
* @var string
*/
public $id = '';
/**
* Validation errors.
2018-01-22 11:33:15 +00:00
*
* @var array of strings
2014-11-20 02:14:06 +00:00
*/
public $errors = array();
2014-11-20 02:14:06 +00:00
2014-11-20 01:11:08 +00:00
/**
* Setting values.
2018-01-22 11:33:15 +00:00
*
2014-11-20 01:11:08 +00:00
* @var array
*/
public $settings = array();
2012-08-14 20:19:41 +00:00
2014-11-20 01:11:08 +00:00
/**
* Form option fields.
2018-01-22 11:33:15 +00:00
*
2014-11-20 01:11:08 +00:00
* @var array
*/
public $form_fields = array();
2012-08-14 20:19:41 +00:00
/**
2016-04-20 14:02:19 +00:00
* The posted settings data. When empty, $_POST data will be used.
2018-01-22 11:33:15 +00:00
*
* @var array
*/
protected $data = array();
/**
* Get the form fields after they are initialized.
2018-01-22 11:33:15 +00:00
*
* @return array of options
*/
public function get_form_fields() {
2016-01-08 13:56:01 +00:00
return apply_filters( 'woocommerce_settings_api_form_fields_' . $this->id, array_map( array( $this, 'set_defaults' ), $this->form_fields ) );
}
/**
* Set default required properties for each field.
2017-05-12 08:48:46 +00:00
*
2018-01-22 11:33:15 +00:00
* @param array $field Setting field array.
2017-05-12 08:48:46 +00:00
* @return array
2016-01-08 13:56:01 +00:00
*/
protected function set_defaults( $field ) {
2016-01-08 13:56:01 +00:00
if ( ! isset( $field['default'] ) ) {
$field['default'] = '';
}
return $field;
}
/**
* Output the admin options table.
*/
public function admin_options() {
2018-01-22 11:33:15 +00:00
echo '<table class="form-table">' . $this->generate_settings_html( $this->get_form_fields(), false ) . '</table>'; // WPCS: XSS ok.
}
/**
* Initialise settings form fields.
*
2018-01-22 11:33:15 +00:00
* Add an array of fields to be displayed on the gateway's settings screen.
*
* @since 1.0.0
*/
public function init_form_fields() {}
/**
* Return the name of the option in the WP DB.
2018-01-22 11:33:15 +00:00
*
2015-12-14 14:03:46 +00:00
* @since 2.6.0
* @return string
*/
public function get_option_key() {
return $this->plugin_id . $this->id . '_settings';
}
/**
2015-12-14 14:03:46 +00:00
* Get a fields type. Defaults to "text" if not set.
2018-01-22 11:33:15 +00:00
*
* @param array $field Field key.
2015-12-14 14:03:46 +00:00
* @return string
*/
public function get_field_type( $field ) {
return empty( $field['type'] ) ? 'text' : $field['type'];
}
2015-12-14 16:56:39 +00:00
/**
* Get a fields default value. Defaults to "" if not set.
2018-01-22 11:33:15 +00:00
*
* @param array $field Field key.
2015-12-14 16:56:39 +00:00
* @return string
*/
public function get_field_default( $field ) {
return empty( $field['default'] ) ? '' : $field['default'];
}
2015-12-14 14:03:46 +00:00
/**
* Get a field's posted and validated value.
2018-01-22 11:33:15 +00:00
*
* @param string $key Field key.
* @param array $field Field array.
* @param array $post_data Posted data.
2015-12-14 14:03:46 +00:00
* @return string
*/
2016-03-24 17:26:40 +00:00
public function get_field_value( $key, $field, $post_data = array() ) {
$type = $this->get_field_type( $field );
$field_key = $this->get_field_key( $key );
2018-01-22 11:33:15 +00:00
$post_data = empty( $post_data ) ? $_POST : $post_data; // WPCS: CSRF ok, input var ok.
2016-03-24 17:26:40 +00:00
$value = isset( $post_data[ $field_key ] ) ? $post_data[ $field_key ] : null;
2015-12-14 14:03:46 +00:00
2018-01-22 14:09:56 +00:00
if ( isset( $field['sanitize_callback'] ) && is_callable( $field['sanitize_callback'] ) ) {
return call_user_func( $field['sanitize_callback'], $value );
}
2018-01-22 11:33:15 +00:00
// Look for a validate_FIELDID_field method for special handling.
if ( is_callable( array( $this, 'validate_' . $key . '_field' ) ) ) {
return $this->{'validate_' . $key . '_field'}( $key, $value );
2015-12-14 14:03:46 +00:00
}
2018-01-22 11:33:15 +00:00
// Look for a validate_FIELDTYPE_field method.
if ( is_callable( array( $this, 'validate_' . $type . '_field' ) ) ) {
return $this->{'validate_' . $type . '_field'}( $key, $value );
2015-12-14 14:03:46 +00:00
}
2018-01-22 11:33:15 +00:00
// Fallback to text.
return $this->validate_text_field( $key, $value );
2015-12-14 14:03:46 +00:00
}
/**
2018-01-22 11:33:15 +00:00
* Sets the POSTed data. This method can be used to set specific data, instead of taking it from the $_POST array.
*
* @param array $data Posted data.
*/
2016-04-20 14:02:19 +00:00
public function set_post_data( $data = array() ) {
$this->data = $data;
}
/**
* Returns the POSTed data, to be used to save the settings.
2018-01-22 11:33:15 +00:00
*
* @return array
*/
2016-04-20 14:02:19 +00:00
public function get_post_data() {
if ( ! empty( $this->data ) && is_array( $this->data ) ) {
return $this->data;
}
2018-01-22 11:33:15 +00:00
return $_POST; // WPCS: CSRF ok, input var ok.
}
/**
* Update a single option.
*
* @since 3.4.0
* @param string $key Option key.
* @param mixed $value Value to set.
* @return bool was anything saved?
*/
public function update_option( $key, $value = '' ) {
if ( empty( $this->settings ) ) {
$this->init_settings();
}
$this->settings[ $key ] = $value;
return update_option( $this->get_option_key(), apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $this->id, $this->settings ) );
}
2015-12-14 14:03:46 +00:00
/**
* Processes and saves options.
* If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out.
2018-01-22 11:33:15 +00:00
*
* @return bool was anything saved?
*/
public function process_admin_options() {
2015-12-14 14:03:46 +00:00
$this->init_settings();
$post_data = $this->get_post_data();
2016-03-24 17:26:40 +00:00
2015-12-14 14:03:46 +00:00
foreach ( $this->get_form_fields() as $key => $field ) {
2016-02-10 13:21:16 +00:00
if ( 'title' !== $this->get_field_type( $field ) ) {
2015-12-14 16:56:39 +00:00
try {
2016-03-24 17:26:40 +00:00
$this->settings[ $key ] = $this->get_field_value( $key, $field, $post_data );
2015-12-14 16:56:39 +00:00
} catch ( Exception $e ) {
$this->add_error( $e->getMessage() );
}
2015-12-14 14:03:46 +00:00
}
}
2015-12-14 14:03:46 +00:00
return update_option( $this->get_option_key(), apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $this->id, $this->settings ) );
}
2015-12-14 14:03:46 +00:00
/**
2015-12-14 16:56:39 +00:00
* Add an error message for display in admin on save.
2018-01-22 11:33:15 +00:00
*
* @param string $error Error message.
2015-12-14 14:03:46 +00:00
*/
2015-12-14 16:56:39 +00:00
public function add_error( $error ) {
$this->errors[] = $error;
}
2015-12-14 14:03:46 +00:00
2016-03-24 17:26:40 +00:00
/**
* Get admin error messages.
*/
public function get_errors() {
return $this->errors;
}
/**
* Display admin error messages.
2012-08-14 20:19:41 +00:00
*/
2015-12-14 14:03:46 +00:00
public function display_errors() {
2016-03-24 17:26:40 +00:00
if ( $this->get_errors() ) {
2015-12-14 16:56:39 +00:00
echo '<div id="woocommerce_errors" class="error notice is-dismissible">';
2016-03-24 17:26:40 +00:00
foreach ( $this->get_errors() as $error ) {
2015-12-14 16:56:39 +00:00
echo '<p>' . wp_kses_post( $error ) . '</p>';
}
echo '</div>';
2015-12-14 14:03:46 +00:00
}
}
/**
* Initialise Settings.
*
* Store all settings in a single database entry
* and make sure the $settings array is either the default
* or the settings stored in the database.
*
* @since 1.0.0
* @uses get_option(), add_option()
2012-08-14 20:19:41 +00:00
*/
public function init_settings() {
$this->settings = get_option( $this->get_option_key(), null );
2015-12-14 16:56:39 +00:00
// If there are no settings defined, use defaults.
2015-12-14 14:03:46 +00:00
if ( ! is_array( $this->settings ) ) {
2015-12-14 16:56:39 +00:00
$form_fields = $this->get_form_fields();
$this->settings = array_merge( array_fill_keys( array_keys( $form_fields ), '' ), wp_list_pluck( $form_fields, 'default' ) );
}
}
/**
2018-01-22 11:33:15 +00:00
* Get option from DB.
*
* Gets an option from the settings API, using defaults if necessary to prevent undefined notices.
*
2018-01-22 11:33:15 +00:00
* @param string $key Option key.
* @param mixed $empty_value Value when empty.
* @return string The value specified for the option or a default value for the option.
*/
public function get_option( $key, $empty_value = null ) {
if ( empty( $this->settings ) ) {
$this->init_settings();
}
// Get option default if unset.
if ( ! isset( $this->settings[ $key ] ) ) {
$form_fields = $this->get_form_fields();
2015-12-14 16:56:39 +00:00
$this->settings[ $key ] = isset( $form_fields[ $key ] ) ? $this->get_field_default( $form_fields[ $key ] ) : '';
}
2015-12-14 16:56:39 +00:00
if ( ! is_null( $empty_value ) && '' === $this->settings[ $key ] ) {
$this->settings[ $key ] = $empty_value;
}
return $this->settings[ $key ];
}
/**
* Prefix key for settings.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @return string
*/
public function get_field_key( $key ) {
return $this->plugin_id . $this->id . '_' . $key;
}
/**
* Generate Settings HTML.
*
* Generate the HTML for the fields on the "settings" screen.
*
2018-01-22 11:33:15 +00:00
* @param array $form_fields (default: array()) Array of form fields.
* @param bool $echo Echo or return.
* @return string the html for the settings
2015-05-25 11:11:44 +00:00
* @since 1.0.0
* @uses method_exists()
*/
public function generate_settings_html( $form_fields = array(), $echo = true ) {
2015-03-27 15:15:40 +00:00
if ( empty( $form_fields ) ) {
$form_fields = $this->get_form_fields();
}
$html = '';
foreach ( $form_fields as $k => $v ) {
2015-12-14 14:05:00 +00:00
$type = $this->get_field_type( $v );
2015-12-14 14:03:46 +00:00
if ( method_exists( $this, 'generate_' . $type . '_html' ) ) {
$html .= $this->{'generate_' . $type . '_html'}( $k, $v );
} else {
2015-12-14 14:03:46 +00:00
$html .= $this->generate_text_html( $k, $v );
}
}
if ( $echo ) {
2018-01-22 11:33:15 +00:00
echo $html; // WPCS: XSS ok.
} else {
return $html;
}
}
/**
* Get HTML for tooltips.
*
2018-01-22 11:33:15 +00:00
* @param array $data Data for the tooltip.
* @return string
*/
public function get_tooltip_html( $data ) {
2016-09-07 22:32:24 +00:00
if ( true === $data['desc_tip'] ) {
$tip = $data['description'];
} elseif ( ! empty( $data['desc_tip'] ) ) {
$tip = $data['desc_tip'];
} else {
$tip = '';
}
return $tip ? wc_help_tip( $tip, true ) : '';
}
/**
* Get HTML for descriptions.
*
2018-01-22 11:33:15 +00:00
* @param array $data Data for the description.
* @return string
*/
public function get_description_html( $data ) {
2016-09-07 22:32:24 +00:00
if ( true === $data['desc_tip'] ) {
$description = '';
} elseif ( ! empty( $data['desc_tip'] ) ) {
$description = $data['description'];
} elseif ( ! empty( $data['description'] ) ) {
$description = $data['description'];
} else {
$description = '';
}
2012-11-27 16:22:47 +00:00
return $description ? '<p class="description">' . wp_kses_post( $description ) . '</p>' . "\n" : '';
}
2012-11-27 16:22:47 +00:00
/**
* Get custom attributes.
*
2018-01-22 11:33:15 +00:00
* @param array $data Field data.
* @return string
*/
public function get_custom_attribute_html( $data ) {
$custom_attributes = array();
if ( ! empty( $data['custom_attributes'] ) && is_array( $data['custom_attributes'] ) ) {
foreach ( $data['custom_attributes'] as $attribute => $attribute_value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
}
}
return implode( ' ', $custom_attributes );
}
/**
* Generate Text Input HTML.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param array $data Field data.
2015-05-25 11:11:44 +00:00
* @since 1.0.0
* @return string
*/
public function generate_text_html( $key, $data ) {
$field_key = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
'class' => '',
'css' => '',
'placeholder' => '',
'type' => 'text',
'desc_tip' => false,
'description' => '',
2016-02-10 13:21:16 +00:00
'custom_attributes' => array(),
);
$data = wp_parse_args( $data, $defaults );
ob_start();
?>
<tr valign="top">
<th scope="row" class="titledesc">
2018-01-22 11:33:15 +00:00
<?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
2016-12-21 11:36:48 +00:00
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th>
<td class="forminp">
<fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
2018-01-22 11:33:15 +00:00
<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 ); // WPCS: XSS ok. ?>
</fieldset>
</td>
</tr>
<?php
return ob_get_clean();
}
/**
* Generate Price Input HTML.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param array $data Field data.
2015-05-25 11:11:44 +00:00
* @since 1.0.0
* @return string
*/
public function generate_price_html( $key, $data ) {
$field_key = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
'class' => '',
'css' => '',
'placeholder' => '',
'type' => 'text',
'desc_tip' => false,
'description' => '',
2016-02-10 13:21:16 +00:00
'custom_attributes' => array(),
);
$data = wp_parse_args( $data, $defaults );
ob_start();
?>
<tr valign="top">
<th scope="row" class="titledesc">
2018-01-22 11:33:15 +00:00
<?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
2016-12-21 11:36:48 +00:00
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th>
<td class="forminp">
<fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
2018-01-22 11:33:15 +00:00
<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 ); // WPCS: XSS ok. ?>
</fieldset>
</td>
</tr>
<?php
return ob_get_clean();
}
/**
* Generate Decimal Input HTML.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param array $data Field data.
2015-05-25 11:11:44 +00:00
* @since 1.0.0
* @return string
*/
public function generate_decimal_html( $key, $data ) {
$field_key = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
'class' => '',
'css' => '',
'placeholder' => '',
'type' => 'text',
'desc_tip' => false,
'description' => '',
2016-02-10 13:21:16 +00:00
'custom_attributes' => array(),
);
$data = wp_parse_args( $data, $defaults );
ob_start();
?>
<tr valign="top">
<th scope="row" class="titledesc">
2018-01-22 11:33:15 +00:00
<?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
2016-12-21 11:36:48 +00:00
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th>
<td class="forminp">
<fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
2018-01-22 11:33:15 +00:00
<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 ); // WPCS: XSS ok. ?>
</fieldset>
</td>
</tr>
<?php
return ob_get_clean();
}
/**
* Generate Password Input HTML.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param array $data Field data.
2015-05-25 11:11:44 +00:00
* @since 1.0.0
* @return string
*/
public function generate_password_html( $key, $data ) {
$data['type'] = 'password';
return $this->generate_text_html( $key, $data );
}
/**
* Generate Color Picker Input HTML.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param array $data Field data.
2015-05-25 11:11:44 +00:00
* @since 1.0.0
* @return string
*/
2014-12-09 12:10:59 +00:00
public function generate_color_html( $key, $data ) {
$field_key = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
'class' => '',
'css' => '',
'placeholder' => '',
'desc_tip' => false,
'description' => '',
2016-02-10 13:21:16 +00:00
'custom_attributes' => array(),
);
$data = wp_parse_args( $data, $defaults );
ob_start();
?>
<tr valign="top">
<th scope="row" class="titledesc">
2018-01-22 11:33:15 +00:00
<?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
2016-12-21 11:36:48 +00:00
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th>
<td class="forminp">
<fieldset>
<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>
2018-01-22 11:33:15 +00:00
<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>
2018-01-22 11:33:15 +00:00
<?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
</fieldset>
</td>
</tr>
<?php
return ob_get_clean();
}
/**
* Generate Textarea HTML.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param array $data Field data.
2015-05-25 11:11:44 +00:00
* @since 1.0.0
* @return string
*/
public function generate_textarea_html( $key, $data ) {
$field_key = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
'class' => '',
'css' => '',
'placeholder' => '',
'type' => 'text',
'desc_tip' => false,
'description' => '',
2016-02-10 13:21:16 +00:00
'custom_attributes' => array(),
);
$data = wp_parse_args( $data, $defaults );
ob_start();
?>
<tr valign="top">
<th scope="row" class="titledesc">
2018-01-22 11:33:15 +00:00
<?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
2016-12-21 11:36:48 +00:00
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th>
<td class="forminp">
<fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
2018-01-22 11:33:15 +00:00
<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 ); // WPCS: XSS ok. ?>
</fieldset>
</td>
</tr>
<?php
return ob_get_clean();
}
/**
* Generate Checkbox HTML.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param array $data Field data.
2015-05-25 11:11:44 +00:00
* @since 1.0.0
* @return string
*/
public function generate_checkbox_html( $key, $data ) {
$field_key = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'label' => '',
'disabled' => false,
'class' => '',
'css' => '',
'type' => 'text',
'desc_tip' => false,
'description' => '',
2016-02-10 13:21:16 +00:00
'custom_attributes' => array(),
);
$data = wp_parse_args( $data, $defaults );
if ( ! $data['label'] ) {
$data['label'] = $data['title'];
}
ob_start();
?>
<tr valign="top">
<th scope="row" class="titledesc">
2018-01-22 11:33:15 +00:00
<?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
2016-12-21 11:36:48 +00:00
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th>
<td class="forminp">
<fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
<label for="<?php echo esc_attr( $field_key ); ?>">
2018-01-22 11:33:15 +00:00
<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 ); // WPCS: XSS ok. ?>
</fieldset>
</td>
</tr>
<?php
return ob_get_clean();
}
/**
* Generate Select HTML.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param array $data Field data.
2015-05-25 11:11:44 +00:00
* @since 1.0.0
* @return string
*/
public function generate_select_html( $key, $data ) {
$field_key = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
'class' => '',
'css' => '',
'placeholder' => '',
'type' => 'text',
'desc_tip' => false,
'description' => '',
'custom_attributes' => array(),
2016-02-10 13:21:16 +00:00
'options' => array(),
);
$data = wp_parse_args( $data, $defaults );
ob_start();
?>
<tr valign="top">
<th scope="row" class="titledesc">
2018-01-22 11:33:15 +00:00
<?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
2016-12-21 11:36:48 +00:00
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th>
<td class="forminp">
<fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
2018-01-22 11:33:15 +00:00
<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 ) : ?>
<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; ?>
</select>
2018-01-22 11:33:15 +00:00
<?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
</fieldset>
</td>
</tr>
<?php
return ob_get_clean();
}
/**
* Generate Multiselect HTML.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param array $data Field data.
2015-05-25 11:11:44 +00:00
* @since 1.0.0
* @return string
*/
public function generate_multiselect_html( $key, $data ) {
$field_key = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
'class' => '',
'css' => '',
'placeholder' => '',
'type' => 'text',
'desc_tip' => false,
'description' => '',
'custom_attributes' => array(),
'select_buttons' => false,
2016-02-10 13:21:16 +00:00
'options' => array(),
);
$data = wp_parse_args( $data, $defaults );
$value = (array) $this->get_option( $key, array() );
ob_start();
?>
<tr valign="top">
<th scope="row" class="titledesc">
2018-01-22 11:33:15 +00:00
<?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?>
2016-12-21 11:36:48 +00:00
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
</th>
<td class="forminp">
<fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
2018-01-22 11:33:15 +00:00
<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 if ( is_array( $option_value ) ) : ?>
<optgroup label="<?php echo esc_attr( $option_key ); ?>">
<?php foreach ( $option_value as $option_key_inner => $option_value_inner ) : ?>
2018-03-02 19:31:37 +00:00
<option value="<?php echo esc_attr( $option_key_inner ); ?>" <?php selected( in_array( $option_key_inner, $value, true ), true ); ?>><?php echo esc_attr( $option_value_inner ); ?></option>
<?php endforeach; ?>
2018-01-25 14:56:16 +00:00
</optgroup>
<?php else : ?>
2018-03-02 19:31:37 +00:00
<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 endif; ?>
<?php endforeach; ?>
</select>
2018-01-22 11:33:15 +00:00
<?php echo $this->get_description_html( $data ); // WPCS: XSS ok. ?>
<?php if ( $data['select_buttons'] ) : ?>
2018-01-22 11:33:15 +00:00
<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; ?>
</fieldset>
</td>
</tr>
<?php
return ob_get_clean();
}
2012-07-23 18:43:38 +00:00
/**
* Generate Title HTML.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param array $data Field data.
2015-05-25 11:11:44 +00:00
* @since 1.0.0
* @return string
*/
public function generate_title_html( $key, $data ) {
$field_key = $this->get_field_key( $key );
$defaults = array(
'title' => '',
2016-02-10 13:21:16 +00:00
'class' => '',
);
$data = wp_parse_args( $data, $defaults );
ob_start();
?>
</table>
<h3 class="wc-settings-sub-title <?php echo esc_attr( $data['class'] ); ?>" id="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></h3>
<?php if ( ! empty( $data['description'] ) ) : ?>
<p><?php echo wp_kses_post( $data['description'] ); ?></p>
<?php endif; ?>
<table class="form-table">
<?php
return ob_get_clean();
}
/**
* Validate Text Field.
*
* Make sure the data is escaped correctly, etc.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param string $value Posted Value.
* @return string
*/
public function validate_text_field( $key, $value ) {
$value = is_null( $value ) ? '' : $value;
return wp_kses_post( trim( stripslashes( $value ) ) );
}
/**
* Validate Price Field.
*
* Make sure the data is escaped correctly, etc.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param string $value Posted Value.
* @return string
*/
public function validate_price_field( $key, $value ) {
$value = is_null( $value ) ? '' : $value;
2016-09-07 22:32:24 +00:00
return ( '' === $value ) ? '' : wc_format_decimal( trim( stripslashes( $value ) ) );
}
/**
* Validate Decimal Field.
*
* Make sure the data is escaped correctly, etc.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param string $value Posted Value.
* @return string
*/
public function validate_decimal_field( $key, $value ) {
$value = is_null( $value ) ? '' : $value;
2016-09-07 22:32:24 +00:00
return ( '' === $value ) ? '' : wc_format_decimal( trim( stripslashes( $value ) ) );
}
2012-08-14 20:19:41 +00:00
/**
* Validate Password Field. No input sanitization is used to avoid corrupting passwords.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param string $value Posted Value.
* @return string
*/
public function validate_password_field( $key, $value ) {
$value = is_null( $value ) ? '' : $value;
return trim( stripslashes( $value ) );
}
/**
* Validate Textarea Field.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param string $value Posted Value.
* @return string
*/
public function validate_textarea_field( $key, $value ) {
$value = is_null( $value ) ? '' : $value;
return wp_kses( trim( stripslashes( $value ) ),
array_merge(
array(
2018-01-22 11:33:15 +00:00
'iframe' => array(
'src' => true,
'style' => true,
'id' => true,
'class' => true,
),
),
wp_kses_allowed_html( 'post' )
)
);
}
/**
* Validate Checkbox Field.
*
* If not set, return "no", otherwise return "yes".
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param string $value Posted Value.
* @return string
*/
public function validate_checkbox_field( $key, $value ) {
return ! is_null( $value ) ? 'yes' : 'no';
}
/**
* Validate Select Field.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param string $value Posted Value.
* @return string
*/
public function validate_select_field( $key, $value ) {
$value = is_null( $value ) ? '' : $value;
return wc_clean( stripslashes( $value ) );
}
/**
* Validate Multiselect Field.
*
2018-01-22 11:33:15 +00:00
* @param string $key Field key.
* @param string $value Posted Value.
* @return string|array
*/
public function validate_multiselect_field( $key, $value ) {
return is_array( $value ) ? array_map( 'wc_clean', array_map( 'stripslashes', $value ) ) : '';
}
2015-12-14 16:56:39 +00:00
/**
* Validate the data on the "Settings" form.
*
2018-01-22 11:33:15 +00:00
* @deprecated 2.6.0 No longer used.
* @param array $form_fields Array of fields.
2015-12-14 16:56:39 +00:00
*/
2016-02-10 13:21:16 +00:00
public function validate_settings_fields( $form_fields = array() ) {
2016-11-23 16:15:00 +00:00
wc_deprecated_function( 'validate_settings_fields', '2.6' );
2016-02-10 13:21:16 +00:00
}
2015-12-14 16:56:39 +00:00
/**
* Format settings if needed.
2018-01-22 11:33:15 +00:00
*
* @deprecated 2.6.0 Unused.
* @param array $value Value to format.
2015-12-14 16:56:39 +00:00
* @return array
*/
public function format_settings( $value ) {
2016-11-23 16:15:00 +00:00
wc_deprecated_function( 'format_settings', '2.6' );
2015-12-14 16:56:39 +00:00
return $value;
}
}