Settings api

This commit is contained in:
Mike Jolley 2015-12-14 14:03:46 +00:00
parent 63f8eebd6a
commit b27aed5753
4 changed files with 152 additions and 103 deletions

View File

@ -11,7 +11,7 @@ if ( ! defined( 'ABSPATH' ) ) {
*
* @class WC_Integration
* @extends WC_Settings_API
* @version 2.0.0
* @version 2.6.0
* @package WooCommerce/Abstracts
* @category Abstract Class
* @author WooThemes
@ -19,24 +19,54 @@ if ( ! defined( 'ABSPATH' ) ) {
abstract class WC_Integration extends WC_Settings_API {
/**
* Admin Options.
*
* Setup the gateway settings screen.
* Override this in your gateway.
* yes or no based on whether the integration is enabled.
* @var string
*/
public function admin_options() { ?>
public $enabled = 'yes';
<h3><?php echo isset( $this->method_title ) ? $this->method_title : __( 'Settings', 'woocommerce' ) ; ?></h3>
/**
* Integration title.
* @var string
*/
public $method_title = '';
<?php echo isset( $this->method_description ) ? wpautop( $this->method_description ) : ''; ?>
/**
* Integration description.
* @var string
*/
public $method_description = '';
<table class="form-table">
<?php $this->generate_settings_html(); ?>
</table>
/**
* Return the title for admin screens.
* @return string
*/
public function get_method_title() {
return apply_filters( 'woocommerce_integration_title', $this->method_title, $this );
}
<!-- Section -->
<div><input type="hidden" name="section" value="<?php echo $this->id; ?>" /></div>
/**
* Return the description for admin screens.
* @return string
*/
public function get_method_description() {
return apply_filters( 'woocommerce_integration_description', $this->method_description, $this );
}
<?php
/**
* Output the gateway settings screen.
*/
public function admin_options() {
echo '<h2>' . esc_html( $this->get_method_title() ) . '</h2>';
echo wp_kses_post( wpautop( $this->get_method_description() ) );
echo '<div><input type="hidden" name="section" value="' . esc_attr( $this->id ) . '" /></div>';
parent::admin_options();
}
/**
* Init settings for gateways.
*/
public function init_settings() {
parent::init_settings();
$this->enabled = ! empty( $this->settings['enabled'] ) && 'yes' === $this->settings['enabled'] ? 'yes' : 'no';
}
}

View File

@ -127,6 +127,14 @@ abstract class WC_Payment_Gateway extends WC_Settings_API {
parent::admin_options();
}
/**
* Init settings for gateways.
*/
public function init_settings() {
parent::init_settings();
$this->enabled = ! empty( $this->settings['enabled'] ) && 'yes' === $this->settings['enabled'] ? 'yes' : 'no';
}
/**
* Get the return url (thank you page).
*
@ -194,7 +202,6 @@ abstract class WC_Payment_Gateway extends WC_Settings_API {
* @return bool
*/
public function is_available() {
$is_available = ( 'yes' === $this->enabled ) ? true : false;
if ( WC()->cart && 0 < $this->get_order_total() && 0 < $this->max_amount && $this->max_amount < $this->get_order_total() ) {

View File

@ -42,12 +42,6 @@ abstract class WC_Settings_API {
*/
public $form_fields = array();
/**
* Sanitized fields after validation.
* @var array
*/
public $sanitized_fields = array();
/**
* Get the form fields after they are initialized.
* @return array of options
@ -65,6 +59,7 @@ abstract class WC_Settings_API {
/**
* Return the name of the option in the WP DB.
* @since 2.6.0
* @return string
*/
public function get_option_key() {
@ -72,28 +67,84 @@ abstract class WC_Settings_API {
}
/**
* Process and save options.
* @return bool was anything saved?
* Get a fields type. Defaults to "text" if not set.
* @param array $field
* @return string
*/
public function process_admin_options() {
$this->validate_settings_fields();
if ( count( $this->errors ) > 0 ) {
$this->display_errors();
return false;
} else {
update_option( $this->get_option_key(), apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $this->id, $this->sanitized_fields ) );
$this->init_settings();
return true;
}
public function get_field_type( $field ) {
return empty( $field['type'] ) ? 'text' : $field['type'];
}
/**
* Display admin error messages.
*
* @since 1.0.0
* Get a field's posted and validated value.
* @return string
*/
public function display_errors() {}
public function get_field_value( $key, $field ) {
$type = $this->get_field_type( $field );
// Look for a validate_FIELDID_field method for special handling
if ( method_exists( $this, 'validate_' . $key . '_field' ) ) {
return $this->{'validate_' . $key . '_field'}( $key );
}
// Look for a validate_FIELDTYPE_field method
if ( method_exists( $this, 'validate_' . $type . '_field' ) ) {
return $this->{'validate_' . $type . '_field'}( $key );
}
// Fallback to text
return $this->validate_text_field( $key );
}
/**
* Processes and saves options.
*
* 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?
*/
public function process_admin_options() {
$this->init_settings();
$this->errors = array();
foreach ( $this->get_form_fields() as $key => $field ) {
if ( ! empty( $field['type'] ) && in_array( $field['type'], array( 'title' ) ) ) {
continue; // Exclude certain types from saving
}
try {
$value = $this->get_field_value( $key, $field );
$this->settings[ $key ] = $value;
} catch ( Exception $e ) {
$this->errors[] = $e->getMessage();
}
}
if ( count( $this->errors ) > 0 ) {
$this->display_errors();
}
return update_option( $this->get_option_key(), apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $this->id, $this->settings ) );
}
/**
* Validate the data on the "Settings" form.
* @deprecated 2.6.0 No longer used
*/
public function validate_settings_fields( $form_fields = array() ) {}
/**
* Display admin error messages.
*/
public function display_errors() {
echo '<div id="woocommerce_errors" class="error notice is-dismissible">';
foreach ( $this->errors as $error ) {
echo '<p>' . wp_kses_post( $error ) . '</p>';
}
echo '</div>';
}
/**
* Initialise Settings.
@ -106,27 +157,20 @@ abstract class WC_Settings_API {
* @uses get_option(), add_option()
*/
public function init_settings() {
// Load form_field settings.
$this->settings = get_option( $this->get_option_key(), null );
if ( ! $this->settings || ! is_array( $this->settings ) ) {
if ( ! is_array( $this->settings ) ) {
$this->settings = array();
// If there are no settings defined, load defaults.
if ( $form_fields = $this->get_form_fields() ) {
foreach ( $form_fields as $k => $v ) {
$this->settings[ $k ] = isset( $v['default'] ) ? $v['default'] : '';
}
}
}
if ( ! empty( $this->settings ) && is_array( $this->settings ) ) {
$this->settings = array_map( array( $this, 'format_settings' ), $this->settings );
$this->enabled = isset( $this->settings['enabled'] ) && $this->settings['enabled'] == 'yes' ? 'yes' : 'no';
}
$this->settings = array_map( array( $this, 'format_settings' ), $this->settings );
}
/**
@ -193,15 +237,12 @@ abstract class WC_Settings_API {
$html = '';
foreach ( $form_fields as $k => $v ) {
$type = $this->get_field_type( $field );
if ( ! isset( $v['type'] ) || ( $v['type'] == '' ) ) {
$v['type'] = 'text'; // Default to "text" field type.
}
if ( method_exists( $this, 'generate_' . $v['type'] . '_html' ) ) {
$html .= $this->{'generate_' . $v['type'] . '_html'}( $k, $v );
if ( method_exists( $this, 'generate_' . $type . '_html' ) ) {
$html .= $this->{'generate_' . $type . '_html'}( $k, $v );
} else {
$html .= $this->{'generate_text_html'}( $k, $v );
$html .= $this->generate_text_html( $k, $v );
}
}
@ -680,45 +721,6 @@ abstract class WC_Settings_API {
return ob_get_clean();
}
/**
* Validate the data on the "Settings" form.
*
* @since 1.0.0
* @param array $form_fields (default: array())
*/
public function validate_settings_fields( $form_fields = array() ) {
if ( empty( $form_fields ) ) {
$form_fields = $this->get_form_fields();
}
$this->sanitized_fields = array();
foreach ( $form_fields as $key => $field ) {
// Default to "text" field type.
$type = empty( $field['type'] ) ? 'text' : $field['type'];
// Look for a validate_FIELDID_field method for special handling
if ( method_exists( $this, 'validate_' . $key . '_field' ) ) {
$field = $this->{'validate_' . $key . '_field'}( $key );
// Exclude certain types from saving
} elseif ( in_array( $type, array( 'title' ) ) ) {
continue;
// Look for a validate_FIELDTYPE_field method
} elseif ( method_exists( $this, 'validate_' . $type . '_field' ) ) {
$field = $this->{'validate_' . $type . '_field'}( $key );
// Fallback to text
} else {
$field = $this->validate_text_field( $key );
}
$this->sanitized_fields[ $key ] = $field;
}
}
/**
* Validate Text Field.
*
@ -728,7 +730,6 @@ abstract class WC_Settings_API {
* @return string
*/
public function validate_text_field( $key ) {
$text = $this->get_option( $key );
$field = $this->get_field_key( $key );
@ -748,7 +749,6 @@ abstract class WC_Settings_API {
* @return string
*/
public function validate_price_field( $key ) {
$text = $this->get_option( $key );
$field = $this->get_field_key( $key );
@ -839,7 +839,7 @@ abstract class WC_Settings_API {
*/
public function validate_checkbox_field( $key ) {
$field = $this->get_field_key( $key );
return isset( $_POST[ $field ] ) && '1' === $_POST[ $field ] ) ? 'yes' : 'no';
return isset( $_POST[ $field ] ) && '1' === $_POST[ $field ] ? 'yes' : 'no';
}
/**

View File

@ -23,7 +23,7 @@ abstract class WC_Shipping_Method extends WC_Settings_API {
* - global-settings Non-instance settings screens. Enabled by default for BW compatibility with methods before instances existed.
* @var array
*/
public $supports = array( 'global-settings' );
public $supports = array( 'global-settings' );
/**
* Unique ID for the shipping method - must be set.
@ -54,8 +54,11 @@ abstract class WC_Shipping_Method extends WC_Settings_API {
* @var string
*/
public $enabled = 'yes';
/** @var string User set title */
/**
* Shipping method title for the frontend.
* @var string
*/
public $title;
/** @var bool True if the method is available. */
@ -73,10 +76,10 @@ abstract class WC_Shipping_Method extends WC_Settings_API {
/** @var float Minimum fee for the method */
public $minimum_fee = null;
/** @var bool Enabled for disabled */
public $enabled = false;
/** @var array This is an array of rates - methods must populate this array to register shipping costs */
/**
* This is an array of rates - methods must populate this array to register shipping costs.
* @var array
*/
public $rates = array();
/**
@ -140,6 +143,7 @@ abstract class WC_Shipping_Method extends WC_Settings_API {
/**
* Return the name of the option in the WP DB.
* @since 2.6.0
* @return string
*/
public function get_option_key() {
@ -159,6 +163,14 @@ abstract class WC_Shipping_Method extends WC_Settings_API {
parent::admin_options();
}
/**
* Init settings for shipping methods.
*/
public function init_settings() {
parent::init_settings();
$this->enabled = ! empty( $this->settings['enabled'] ) && 'yes' === $this->settings['enabled'] ? 'yes' : 'no';
}
/**
* Return the shipping title which is user set.
*