Merge pull request #8221 from shivapoudel/settings-api

Introduce get_field_key method
This commit is contained in:
Claudio Sanches 2015-05-25 09:55:45 -03:00
commit 70b33b7919
1 changed files with 121 additions and 101 deletions

View File

@ -89,14 +89,13 @@ abstract class WC_Settings_API {
* Add an array of fields to be displayed
* on the gateway's settings screen.
*
* @since 1.0.0
* @since 1.0.0
* @return string
*/
public function init_form_fields() {}
/**
* Get the form fields after they are initialized
*
* @return array of options
*/
public function get_form_fields() {
@ -170,9 +169,9 @@ abstract class WC_Settings_API {
*
* Gets and option from the settings API, using defaults if necessary to prevent undefined notices.
*
* @param string $key
* @param mixed $empty_value
* @return mixed The value specified for the option or a default value for the option
* @param string $key
* @param mixed $empty_value
* @return mixed The value specified for the option or a default value for the option
*/
public function get_option( $key, $empty_value = null ) {
@ -193,10 +192,20 @@ abstract class WC_Settings_API {
return $this->settings[ $key ];
}
/**
* Prefix key for settings.
*
* @param mixed $key
* @return string
*/
public function get_field_key( $key ) {
return $this->plugin_id . $this->id . '_' . $key;
}
/**
* Decode values for settings.
*
* @param mixed $value
* @param mixed $value
* @return array
*/
public function format_settings( $value ) {
@ -208,9 +217,9 @@ abstract class WC_Settings_API {
*
* Generate the HTML for the fields on the "settings" screen.
*
* @param array $form_fields (default: array())
* @since 1.0.0
* @uses method_exists()
* @param array $form_fields (default: array())
* @since 1.0.0
* @uses method_exists()
* @return string the html for the settings
*/
public function generate_settings_html( $form_fields = array() ) {
@ -298,14 +307,14 @@ abstract class WC_Settings_API {
/**
* Generate Text Input HTML.
*
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @return string
*/
public function generate_text_html( $key, $data ) {
$field = $this->plugin_id . $this->id . '_' . $key;
$field = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
@ -343,14 +352,14 @@ abstract class WC_Settings_API {
/**
* Generate Price Input HTML.
*
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @return string
*/
public function generate_price_html( $key, $data ) {
$field = $this->plugin_id . $this->id . '_' . $key;
$field = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
@ -388,14 +397,14 @@ abstract class WC_Settings_API {
/**
* Generate Decimal Input HTML.
*
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @return string
*/
public function generate_decimal_html( $key, $data ) {
$field = $this->plugin_id . $this->id . '_' . $key;
$field = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
@ -433,9 +442,9 @@ abstract class WC_Settings_API {
/**
* Generate Password Input HTML.
*
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @return string
*/
public function generate_password_html( $key, $data ) {
@ -446,13 +455,14 @@ abstract class WC_Settings_API {
/**
* Generate Color Picker Input HTML.
*
* @param mixed $key
* @param mixed $data
* @since 2.3.0
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @return string
*/
public function generate_color_html( $key, $data ) {
$field = $this->plugin_id . $this->id . '_' . $key;
$field = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
@ -492,14 +502,14 @@ abstract class WC_Settings_API {
/**
* Generate Textarea HTML.
*
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @return string
*/
public function generate_textarea_html( $key, $data ) {
$field = $this->plugin_id . $this->id . '_' . $key;
$field = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
@ -537,14 +547,14 @@ abstract class WC_Settings_API {
/**
* Generate Checkbox HTML.
*
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @return string
*/
public function generate_checkbox_html( $key, $data ) {
$field = $this->plugin_id . $this->id . '_' . $key;
$field = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'label' => '',
@ -587,14 +597,14 @@ abstract class WC_Settings_API {
/**
* Generate Select HTML.
*
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @return string
*/
public function generate_select_html( $key, $data ) {
$field = $this->plugin_id . $this->id . '_' . $key;
$field = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
@ -637,14 +647,14 @@ abstract class WC_Settings_API {
/**
* Generate Multiselect HTML.
*
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @return string
*/
public function generate_multiselect_html( $key, $data ) {
$field = $this->plugin_id . $this->id . '_' . $key;
$field = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
@ -688,13 +698,14 @@ abstract class WC_Settings_API {
/**
* Generate Title HTML.
*
* @param mixed $key
* @param mixed $data
* @since 1.6.2
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @return string
*/
public function generate_title_html( $key, $data ) {
$field = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'class' => ''
@ -705,7 +716,7 @@ abstract class WC_Settings_API {
ob_start();
?>
</table>
<h3 class="wc-settings-sub-title <?php echo esc_attr( $data['class'] ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></h3>
<h3 class="wc-settings-sub-title <?php echo esc_attr( $data['class'] ); ?>" id="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></h3>
<?php if ( ! empty( $data['description'] ) ) : ?>
<p><?php echo wp_kses_post( $data['description'] ); ?></p>
<?php endif; ?>
@ -721,7 +732,7 @@ abstract class WC_Settings_API {
* Validate the data on the "Settings" form.
*
* @since 1.0.0
* @uses method_exists()
* @uses method_exists()
* @param array $form_fields (default: array())
*/
public function validate_settings_fields( $form_fields = array() ) {
@ -756,40 +767,21 @@ abstract class WC_Settings_API {
}
}
/**
* Validate Checkbox Field.
*
* If not set, return "no", otherwise return "yes".
*
* @param mixed $key
* @since 1.0.0
* @return string
*/
public function validate_checkbox_field( $key ) {
$status = 'no';
if ( isset( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) && ( 1 == $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) {
$status = 'yes';
}
return $status;
}
/**
* Validate Text Field.
*
* Make sure the data is escaped correctly, etc.
*
* @param mixed $key
* @param mixed $key
* @return string
*/
public function validate_text_field( $key ) {
$text = $this->get_option( $key );
$text = $this->get_option( $key );
$field = $this->get_field_key( $key );
if ( isset( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) {
$text = wp_kses_post( trim( stripslashes( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) );
if ( isset( $_POST[ $field ] ) ) {
$text = wp_kses_post( trim( stripslashes( $_POST[ $field ] ) ) );
}
return $text;
@ -800,17 +792,18 @@ abstract class WC_Settings_API {
*
* Make sure the data is escaped correctly, etc.
*
* @param mixed $key
* @param mixed $key
* @return string
*/
public function validate_price_field( $key ) {
$text = $this->get_option( $key );
$text = $this->get_option( $key );
$field = $this->get_field_key( $key );
if ( isset( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) {
if ( isset( $_POST[ $field ] ) ) {
if ( $_POST[ $this->plugin_id . $this->id . '_' . $key ] !== '' ) {
$text = wc_format_decimal( trim( stripslashes( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) );
if ( $_POST[ $field ] !== '' ) {
$text = wc_format_decimal( trim( stripslashes( $_POST[ $field ] ) ) );
} else {
$text = '';
}
@ -824,17 +817,18 @@ abstract class WC_Settings_API {
*
* Make sure the data is escaped correctly, etc.
*
* @param mixed $key
* @param mixed $key
* @return string
*/
public function validate_decimal_field( $key ) {
$text = $this->get_option( $key );
$text = $this->get_option( $key );
$field = $this->get_field_key( $key );
if ( isset( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) {
if ( isset( $_POST[ $field ] ) ) {
if ( $_POST[ $this->plugin_id . $this->id . '_' . $key ] !== '' ) {
$text = wc_format_decimal( trim( stripslashes( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) );
if ( $_POST[ $field ] !== '' ) {
$text = wc_format_decimal( trim( stripslashes( $_POST[ $field ] ) ) );
} else {
$text = '';
}
@ -848,16 +842,17 @@ abstract class WC_Settings_API {
*
* Make sure the data is escaped correctly, etc.
*
* @param mixed $key
* @since 1.0.0
* @param mixed $key
* @since 1.0.0
* @return string
*/
public function validate_password_field( $key ) {
$text = $this->get_option( $key );
$text = $this->get_option( $key );
$field = $this->get_field_key( $key );
if ( isset( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) {
$text = wc_clean( stripslashes( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) );
if ( isset( $_POST[ $field ] ) ) {
$text = wc_clean( stripslashes( $_POST[ $field ] ) );
}
return $text;
@ -868,17 +863,18 @@ abstract class WC_Settings_API {
*
* Make sure the data is escaped correctly, etc.
*
* @param mixed $key
* @since 1.0.0
* @param mixed $key
* @since 1.0.0
* @return string
*/
public function validate_textarea_field( $key ) {
$text = $this->get_option( $key );
$text = $this->get_option( $key );
$field = $this->get_field_key( $key );
if ( isset( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) {
if ( isset( $_POST[ $field ] ) ) {
$text = wp_kses( trim( stripslashes( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ),
$text = wp_kses( trim( stripslashes( $_POST[ $field ] ) ),
array_merge(
array(
'iframe' => array( 'src' => true, 'style' => true, 'id' => true, 'class' => true )
@ -891,21 +887,43 @@ abstract class WC_Settings_API {
return $text;
}
/**
* Validate Checkbox Field.
*
* If not set, return "no", otherwise return "yes".
*
* @param mixed $key
* @since 1.0.0
* @return string
*/
public function validate_checkbox_field( $key ) {
$status = 'no';
$field = $this->get_field_key( $key );
if ( isset( $_POST[ $field ] ) && ( 1 == $_POST[ $field ] ) ) {
$status = 'yes';
}
return $status;
}
/**
* Validate Select Field.
*
* Make sure the data is escaped correctly, etc.
*
* @param mixed $key
* @since 1.0.0
* @param mixed $key
* @since 1.0.0
* @return string
*/
public function validate_select_field( $key ) {
$value = $this->get_option( $key );
$field = $this->get_field_key( $key );
if ( isset( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) {
$value = wc_clean( stripslashes( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) );
if ( isset( $_POST[ $field ] ) ) {
$value = wc_clean( stripslashes( $_POST[ $field ] ) );
}
return $value;
@ -916,14 +934,16 @@ abstract class WC_Settings_API {
*
* Make sure the data is escaped correctly, etc.
*
* @param mixed $key
* @since 1.0.0
* @param mixed $key
* @since 1.0.0
* @return string
*/
public function validate_multiselect_field( $key ) {
if ( isset( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) {
$value = array_map( 'wc_clean', array_map( 'stripslashes', (array) $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) );
$field = $this->get_field_key( $key );
if ( isset( $_POST[ $field ] ) ) {
$value = array_map( 'wc_clean', array_map( 'stripslashes', (array) $_POST[ $field ] ) );
} else {
$value = '';
}