Last active
October 11, 2018 17:52
-
-
Save barrykooij/e684c2948ec4edadf34b to your computer and use it in GitHub Desktop.
WooCommerce Settings Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( !defined( 'ABSPATH' ) ) { | |
exit; | |
} // Exit if accessed directly | |
if ( !class_exists( 'WC_BK_Settings' ) ) { | |
class WC_BK_Settings { | |
const SETTINGS_NAMESPACE = 'YOUR_SETTINGS_NAMESPACE'; | |
/** | |
* Get the setting fields | |
* | |
* @since 1.0.0 | |
* @access private | |
* | |
* @return array $setting_fields | |
*/ | |
private function get_fields() { | |
$setting_fields = array( | |
'section_title' => array( | |
'name' => __( 'An example title', 'example-text-domain' ), | |
'type' => 'title', | |
'desc' => '', | |
'id' => 'wc_settings_' . self::SETTINGS_NAMESPACE . '_title' | |
), | |
'example_input' => array( | |
'name' => __( 'Example input', 'example-text-domain' ), | |
'type' => 'text', | |
'desc' => __( 'This is an example field, nothing much I can say about it.', 'example-text-domain' ), | |
'id' => 'wc_settings_' . self::SETTINGS_NAMESPACE . '_example_input', | |
'default' => '10', | |
), | |
'section_end' => array( | |
'type' => 'sectionend', | |
'id' => 'wc_settings_' . self::SETTINGS_NAMESPACE . '_section_end' | |
) | |
); | |
return apply_filters( 'wc_settings_tab_' . self::SETTINGS_NAMESPACE, $setting_fields ); | |
} | |
/** | |
* Get an option set in our settings tab | |
* | |
* @param $key | |
* | |
* @since 1.0.0 | |
* @access public | |
* | |
* @return String | |
*/ | |
public function get_option( $key ) { | |
$fields = $this->get_fields(); | |
return apply_filters( 'wc_option_' . $key, get_option( 'wc_settings_' . self::SETTINGS_NAMESPACE . '_' . $key, ( ( isset( $fields[$key] ) && isset( $fields[$key]['default'] ) ) ? $fields[$key]['default'] : '' ) ) ); | |
} | |
/** | |
* Setup the WooCommerce settings | |
* | |
* @since 1.0.0 | |
* @access public | |
*/ | |
public function setup() { | |
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_tab' ), 70 ); | |
add_action( 'woocommerce_settings_tabs_' . self::SETTINGS_NAMESPACE, array( $this, 'tab_content' ) ); | |
add_action( 'woocommerce_update_options_' . self::SETTINGS_NAMESPACE, array( $this, 'update_settings' ) ); | |
} | |
/** | |
* Add a settings tab to the settings page | |
* | |
* @param array $settings_tabs | |
* | |
* @since 1.0.0 | |
* @access public | |
* | |
* @return array | |
*/ | |
public function add_settings_tab( $settings_tabs ) { | |
$settings_tabs[self::SETTINGS_NAMESPACE] = __( 'Your Label', 'example-text-domain' ); | |
return $settings_tabs; | |
} | |
/** | |
* Output the tab content | |
* | |
* @since 1.0.0 | |
* @access public | |
* | |
*/ | |
public function tab_content() { | |
woocommerce_admin_fields( $this->get_fields() ); | |
} | |
/** | |
* Update the settings | |
* | |
* @since 1.0.0 | |
* @access public | |
*/ | |
public function update_settings() { | |
woocommerce_update_options( $this->get_fields() ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment