diff --git a/includes/abstracts/abstract-wc-integration.php b/includes/abstracts/abstract-wc-integration.php index bb6490e9b7d..01f3372a465 100644 --- a/includes/abstracts/abstract-wc-integration.php +++ b/includes/abstracts/abstract-wc-integration.php @@ -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'; -

method_title ) ? $this->method_title : __( 'Settings', 'woocommerce' ) ; ?>

+ /** + * Integration title. + * @var string + */ + public $method_title = ''; - method_description ) ? wpautop( $this->method_description ) : ''; ?> + /** + * Integration description. + * @var string + */ + public $method_description = ''; - - generate_settings_html(); ?> -
+ /** + * Return the title for admin screens. + * @return string + */ + public function get_method_title() { + return apply_filters( 'woocommerce_integration_title', $this->method_title, $this ); + } - -
+ /** + * Return the description for admin screens. + * @return string + */ + public function get_method_description() { + return apply_filters( 'woocommerce_integration_description', $this->method_description, $this ); + } - ' . esc_html( $this->get_method_title() ) . ''; + echo wp_kses_post( wpautop( $this->get_method_description() ) ); + echo '
'; + 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'; } } diff --git a/includes/abstracts/abstract-wc-payment-gateway.php b/includes/abstracts/abstract-wc-payment-gateway.php index 648f2442d6c..4f9b0bd25f4 100644 --- a/includes/abstracts/abstract-wc-payment-gateway.php +++ b/includes/abstracts/abstract-wc-payment-gateway.php @@ -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() ) { diff --git a/includes/abstracts/abstract-wc-settings-api.php b/includes/abstracts/abstract-wc-settings-api.php index dc8f5c73b24..55b4d47da09 100644 --- a/includes/abstracts/abstract-wc-settings-api.php +++ b/includes/abstracts/abstract-wc-settings-api.php @@ -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 '
'; + + foreach ( $this->errors as $error ) { + echo '

' . wp_kses_post( $error ) . '

'; + } + + echo '
'; + } /** * 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'; } /** diff --git a/includes/abstracts/abstract-wc-shipping-method.php b/includes/abstracts/abstract-wc-shipping-method.php index f6114ca04ba..43c9188a581 100644 --- a/includes/abstracts/abstract-wc-shipping-method.php +++ b/includes/abstracts/abstract-wc-shipping-method.php @@ -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. *