← All changes
|
vendor/bv-settings/includes/class-bv-settings.php
+122
-124
4.1.1
→
3.0.0
View file →
| @@ -1,145 +1,143 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Stable public wrapper for the active BV Settings implementation. | |
| 3 | + * The core component class. | |
| 4 | 4 | * |
| 5 | 5 | * @link https://bootstrapped.ventures |
| 6 | - * @since 1.1.0 | |
| 6 | + * @since 1.0.0 | |
| 7 | 7 | * |
| 8 | 8 | * @package BV_Settings |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | -if ( ! class_exists( 'BV_Settings' ) ) { | |
| 11 | +/** | |
| 12 | + * The core component class. | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + * @package BV_Settings | |
| 16 | + * @author Brecht Vandersmissen <[email protected]> | |
| 17 | + */ | |
| 18 | +class BV_Settings { | |
| 19 | + public $atts; | |
| 20 | + public $helpers; | |
| 21 | + | |
| 12 | 22 | /** |
| 13 | - * Stable public wrapper for the active BV Settings implementation. | |
| 23 | + * Make sure all is set up for the component to load. | |
| 14 | 24 | * |
| 15 | - * @since 1.1.0 | |
| 25 | + * @since 1.0.0 | |
| 16 | 26 | */ |
| 17 | - class BV_Settings { | |
| 18 | - public $atts; | |
| 19 | - public $helpers; | |
| 20 | - public $component; | |
| 27 | + public function __construct( $atts = array() ) { | |
| 28 | + // Set defaults. | |
| 29 | + $atts = shortcode_atts( array( | |
| 30 | + 'uid' => '', | |
| 31 | + 'menu_priority' => 10, | |
| 32 | + 'menu_title' => __( 'Settings', 'bv-settings' ), | |
| 33 | + 'menu_parent' => 'options-general.php', | |
| 34 | + 'page_title' => __( 'Settings', 'bv-settings' ), | |
| 35 | + 'page_slug' => false, | |
| 36 | + 'required_capability' => 'manage_options', | |
| 37 | + 'settings' => array(), | |
| 38 | + 'required_addons' => array(), | |
| 39 | + ), $atts ); | |
| 21 | 40 | |
| 22 | - /** | |
| 23 | - * Active implementation instance. | |
| 24 | - * | |
| 25 | - * @since 1.1.0 | |
| 26 | - * @var object | |
| 27 | - */ | |
| 28 | - private $implementation; | |
| 41 | + // Make sure required fields are set. | |
| 42 | + $atts['uid'] = sanitize_title( $atts['uid'] ); | |
| 43 | + if ( ! $atts['uid'] ) { | |
| 44 | + throw new Exception( 'You need to initialize the settings with a UID.' ); | |
| 45 | + } | |
| 29 | 46 | |
| 30 | - /** | |
| 31 | - * Resolve and instantiate the active component implementation. | |
| 32 | - * | |
| 33 | - * @since 1.1.0 | |
| 34 | - * @param array $atts Component attributes. | |
| 35 | - */ | |
| 36 | - public function __construct( $atts = array() ) { | |
| 37 | - $component = bv_settings_get_active_component(); | |
| 47 | + // Calculated defaults. | |
| 48 | + if ( ! $atts['page_slug'] ) { | |
| 49 | + $atts['page_slug'] = 'bv_settings_' . $atts['uid']; | |
| 50 | + } | |
| 51 | + | |
| 52 | + // Save attributes and load helpers. | |
| 53 | + $this->atts = $atts; | |
| 54 | + $this->load_helpers(); | |
| 55 | + } | |
| 38 | 56 | |
| 39 | - if ( ! $component ) { | |
| 40 | - throw new Exception( 'No BV Settings component is registered.' ); | |
| 41 | - } | |
| 57 | + /** | |
| 58 | + * Load helper classes. | |
| 59 | + * | |
| 60 | + * @since 1.0.0 | |
| 61 | + */ | |
| 62 | + private function load_helpers() { | |
| 63 | + require_once( BVS_DIR . 'includes/class-bvs-api.php' ); | |
| 64 | + $this->helpers['api'] = new BV_API( $this ); | |
| 42 | 65 | |
| 43 | - if ( ! class_exists( $component['class'] ) ) { | |
| 44 | - require_once $component['loader']; | |
| 45 | - } | |
| 66 | + require_once( BVS_DIR . 'includes/class-bvs-menu.php' ); | |
| 67 | + $this->helpers['menu'] = new BV_Menu( $this ); | |
| 46 | 68 | |
| 47 | - if ( ! class_exists( $component['class'] ) ) { | |
| 48 | - throw new Exception( 'The active BV Settings implementation could not be loaded.' ); | |
| 49 | - } | |
| 69 | + require_once( BVS_DIR . 'includes/class-bvs-saver.php' ); | |
| 70 | + $this->helpers['saver'] = new BV_Saver( $this ); | |
| 50 | 71 | |
| 51 | - $class = $component['class']; | |
| 52 | - $this->implementation = new $class( $atts, $component ); | |
| 53 | - $this->atts = &$this->implementation->atts; | |
| 54 | - $this->helpers = &$this->implementation->helpers; | |
| 55 | - $this->component = &$this->implementation->component; | |
| 56 | - } | |
| 72 | + require_once( BVS_DIR . 'includes/class-bvs-structure.php' ); | |
| 73 | + $this->helpers['structure'] = new BV_Structure( $this ); | |
| 74 | + } | |
| 57 | 75 | |
| 58 | - /** | |
| 59 | - * Forward unknown method calls to the active implementation. | |
| 60 | - * | |
| 61 | - * @since 1.1.0 | |
| 62 | - * @param string $name Method name. | |
| 63 | - * @param array $arguments Method arguments. | |
| 64 | - * @return mixed | |
| 65 | - */ | |
| 66 | - public function __call( $name, $arguments ) { | |
| 67 | - return call_user_func_array( array( $this->implementation, $name ), $arguments ); | |
| 68 | - } | |
| 76 | + /** | |
| 77 | + * Get the settings structure. | |
| 78 | + * | |
| 79 | + * @since 1.0.0 | |
| 80 | + * @param mixed $resolve_callbacks Wether to resolve the callbacks. | |
| 81 | + */ | |
| 82 | + public function get_structure( $resolve_callbacks = false ) { | |
| 83 | + return $this->helpers['structure']->get_structure( $resolve_callbacks ); | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Get the value for a specific setting. | |
| 88 | + * | |
| 89 | + * @since 1.0.0 | |
| 90 | + * @param mixed $setting Setting to get the value for. | |
| 91 | + */ | |
| 92 | + public function get( $setting ) { | |
| 93 | + return $this->helpers['structure']->get( $setting ); | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * Get all the settings. | |
| 98 | + * | |
| 99 | + * @since 1.0.0 | |
| 100 | + */ | |
| 101 | + public function get_settings() { | |
| 102 | + return $this->helpers['structure']->get_settings(); | |
| 103 | + } | |
| 69 | 104 | |
| 70 | - /** | |
| 71 | - * Get the settings structure. | |
| 72 | - * | |
| 73 | - * @since 1.0.0 | |
| 74 | - * @param mixed $resolve_callbacks Whether to resolve the callbacks. | |
| 75 | - * @return array | |
| 76 | - */ | |
| 77 | - public function get_structure( $resolve_callbacks = false ) { | |
| 78 | - return $this->implementation->get_structure( $resolve_callbacks ); | |
| 79 | - } | |
| 80 | - | |
| 81 | - /** | |
| 82 | - * Get the value for a specific setting. | |
| 83 | - * | |
| 84 | - * @since 1.0.0 | |
| 85 | - * @param mixed $setting Setting to get the value for. | |
| 86 | - * @return mixed | |
| 87 | - */ | |
| 88 | - public function get( $setting ) { | |
| 89 | - return $this->implementation->get( $setting ); | |
| 90 | - } | |
| 91 | - | |
| 92 | - /** | |
| 93 | - * Get all settings. | |
| 94 | - * | |
| 95 | - * @since 1.0.0 | |
| 96 | - * @return array | |
| 97 | - */ | |
| 98 | - public function get_settings() { | |
| 99 | - return $this->implementation->get_settings(); | |
| 100 | - } | |
| 101 | - | |
| 102 | - /** | |
| 103 | - * Get all settings with defaults if not set. | |
| 104 | - * | |
| 105 | - * @since 1.0.0 | |
| 106 | - * @return array | |
| 107 | - */ | |
| 108 | - public function get_settings_with_defaults() { | |
| 109 | - return $this->implementation->get_settings_with_defaults(); | |
| 110 | - } | |
| 111 | - | |
| 112 | - /** | |
| 113 | - * Get the default for a specific setting. | |
| 114 | - * | |
| 115 | - * @since 1.0.0 | |
| 116 | - * @param mixed $setting Setting to get the default for. | |
| 117 | - * @return mixed | |
| 118 | - */ | |
| 119 | - public function get_default( $setting ) { | |
| 120 | - return $this->implementation->get_default( $setting ); | |
| 121 | - } | |
| 122 | - | |
| 123 | - /** | |
| 124 | - * Get all defaults. | |
| 125 | - * | |
| 126 | - * @since 1.0.0 | |
| 127 | - * @param boolean $force_update Whether to force a cache refresh. | |
| 128 | - * @return array | |
| 129 | - */ | |
| 130 | - public function get_defaults( $force_update = false ) { | |
| 131 | - return $this->implementation->get_defaults( $force_update ); | |
| 132 | - } | |
| 133 | - | |
| 134 | - /** | |
| 135 | - * Update settings. | |
| 136 | - * | |
| 137 | - * @since 1.0.0 | |
| 138 | - * @param array $settings_to_update Settings to update. | |
| 139 | - * @return array | |
| 140 | - */ | |
| 141 | - public function update_settings( $settings_to_update ) { | |
| 142 | - return $this->implementation->update_settings( $settings_to_update ); | |
| 143 | - } | |
| 105 | + /** | |
| 106 | + * Get all the settings with defaults if not set. | |
| 107 | + * | |
| 108 | + * @since 1.0.0 | |
| 109 | + */ | |
| 110 | + public function get_settings_with_defaults() { | |
| 111 | + return $this->helpers['structure']->get_settings_with_defaults(); | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Get the default for a specific setting. | |
| 116 | + * | |
| 117 | + * @since 1.0.0 | |
| 118 | + * @param mixed $setting Setting to get the default for. | |
| 119 | + */ | |
| 120 | + public function get_default( $setting ) { | |
| 121 | + return $this->helpers['structure']->get_default( $setting ); | |
| 122 | + } | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * Get the default settings. | |
| 126 | + * | |
| 127 | + * @since 1.0.0 | |
| 128 | + * @param boolean $force_update Wether to force an update of the cache. | |
| 129 | + */ | |
| 130 | + public function get_defaults( $force_update = false ) { | |
| 131 | + return $this->helpers['structure']->get_defaults( $force_update ); | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * Update the settings. | |
| 136 | + * | |
| 137 | + * @since 1.0.0 | |
| 138 | + * @param array $settings_to_update Settings to update. | |
| 139 | + */ | |
| 140 | + public function update_settings( $settings_to_update ) { | |
| 141 | + return $this->helpers['saver']->update_settings( $settings_to_update ); | |
| 144 | 142 | } |
| 145 | 143 | } |