| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Core\Base; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Base Object |
| 11 |
* |
| 12 |
* Base class that provides basic settings handling functionality. |
| 13 |
* |
| 14 |
* @since 2.3.0 |
| 15 |
*/ |
| 16 |
class Base_Object { |
| 17 |
|
| 18 |
/** |
| 19 |
* Settings. |
| 20 |
* |
| 21 |
* Holds the object settings. |
| 22 |
* |
| 23 |
* @access private |
| 24 |
* |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
private $settings; |
| 28 |
|
| 29 |
/** |
| 30 |
* Get Settings. |
| 31 |
* |
| 32 |
* @since 2.3.0 |
| 33 |
* @access public |
| 34 |
* |
| 35 |
* @param string $setting Optional. The key of the requested setting. Default is null. |
| 36 |
* |
| 37 |
* @return mixed An array of all settings, or a single value if `$setting` was specified. |
| 38 |
*/ |
| 39 |
final public function get_settings( $setting = null ) { |
| 40 |
$this->ensure_settings(); |
| 41 |
|
| 42 |
return self::get_items( $this->settings, $setting ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Set settings. |
| 47 |
* |
| 48 |
* @since 2.3.0 |
| 49 |
* @access public |
| 50 |
* |
| 51 |
* @param array|string $key If key is an array, the settings are overwritten by that array. Otherwise, the |
| 52 |
* settings of the key will be set to the given `$value` param. |
| 53 |
* |
| 54 |
* @param mixed $value Optional. Default is null. |
| 55 |
*/ |
| 56 |
final public function set_settings( $key, $value = null ) { |
| 57 |
$this->ensure_settings(); |
| 58 |
|
| 59 |
if ( is_array( $key ) ) { |
| 60 |
$this->settings = $key; |
| 61 |
} else { |
| 62 |
$this->settings[ $key ] = $value; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Delete setting. |
| 68 |
* |
| 69 |
* Deletes the settings array or a specific key of the settings array if `$key` is specified. |
| 70 |
* @since 2.3.0 |
| 71 |
* @access public |
| 72 |
* |
| 73 |
* @param string $key Optional. Default is null. |
| 74 |
*/ |
| 75 |
public function delete_setting( $key = null ) { |
| 76 |
if ( $key ) { |
| 77 |
unset( $this->settings[ $key ] ); |
| 78 |
} else { |
| 79 |
$this->settings = []; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get items. |
| 85 |
* |
| 86 |
* Utility method that receives an array with a needle and returns all the |
| 87 |
* items that match the needle. If needle is not defined the entire haystack |
| 88 |
* will be returned. |
| 89 |
* |
| 90 |
* @since 2.3.0 |
| 91 |
* @access protected |
| 92 |
* @static |
| 93 |
* |
| 94 |
* @param array $haystack An array of items. |
| 95 |
* @param string $needle Optional. Needle. Default is null. |
| 96 |
* |
| 97 |
* @return mixed The whole haystack or the needle from the haystack when requested. |
| 98 |
*/ |
| 99 |
final protected static function get_items( array $haystack, $needle = null ) { |
| 100 |
if ( $needle ) { |
| 101 |
return isset( $haystack[ $needle ] ) ? $haystack[ $needle ] : null; |
| 102 |
} |
| 103 |
|
| 104 |
return $haystack; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get init settings. |
| 109 |
* |
| 110 |
* Used to define the default/initial settings of the object. Inheriting classes may implement this method to define |
| 111 |
* their own default/initial settings. |
| 112 |
* |
| 113 |
* @since 2.3.0 |
| 114 |
* @access protected |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
protected function get_init_settings() { |
| 119 |
return []; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Ensure settings. |
| 124 |
* |
| 125 |
* Ensures that the `$settings` member is initialized |
| 126 |
* |
| 127 |
* @since 2.3.0 |
| 128 |
* @access private |
| 129 |
*/ |
| 130 |
private function ensure_settings() { |
| 131 |
if ( null === $this->settings ) { |
| 132 |
$this->settings = $this->get_init_settings(); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|