PluginProbe
Elementor Website Builder – more than just a page builder / 3.20.4
Elementor Website Builder – more than just a page builder v3.20.4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / base / base-object.php

base-object.php in Elementor Website Builder – more than just a page builder 3.20.4, at core/base/base-object.php

194 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 final public function merge_properties( array $default_props, array $custom_props, array $allowed_props_keys = [] ) {
84 $props = array_replace_recursive( $default_props, $custom_props );
85
86 if ( $allowed_props_keys ) {
87 $props = array_intersect_key( $props, array_flip( $allowed_props_keys ) );
88 }
89
90 return $props;
91 }
92
93 /**
94 * Get items.
95 *
96 * Utility method that receives an array with a needle and returns all the
97 * items that match the needle. If needle is not defined the entire haystack
98 * will be returned.
99 *
100 * @since 2.3.0
101 * @access protected
102 * @static
103 *
104 * @param array $haystack An array of items.
105 * @param string $needle Optional. Needle. Default is null.
106 *
107 * @return mixed The whole haystack or the needle from the haystack when requested.
108 */
109 final protected static function get_items( array $haystack, $needle = null ) {
110 if ( $needle ) {
111 return isset( $haystack[ $needle ] ) ? $haystack[ $needle ] : null;
112 }
113
114 return $haystack;
115 }
116
117 /**
118 * Get init settings.
119 *
120 * Used to define the default/initial settings of the object. Inheriting classes may implement this method to define
121 * their own default/initial settings.
122 *
123 * @since 2.3.0
124 * @access protected
125 *
126 * @return array
127 */
128 protected function get_init_settings() {
129 return [];
130 }
131
132 /**
133 * Ensure settings.
134 *
135 * Ensures that the `$settings` member is initialized
136 *
137 * @since 2.3.0
138 * @access private
139 */
140 private function ensure_settings() {
141 if ( null === $this->settings ) {
142 $this->settings = $this->get_init_settings();
143 }
144 }
145
146 /**
147 * Has Own Method
148 *
149 * Used for check whether the method passed as a parameter was declared in the current instance or inherited.
150 * If a base_class_name is passed, it checks whether the method was declared in that class. If the method's
151 * declaring class is the class passed as $base_class_name, it returns false. Otherwise (method was NOT declared
152 * in $base_class_name), it returns true.
153 *
154 * Example #1 - only $method_name is passed:
155 * The initial declaration of `register_controls()` happens in the `Controls_Stack` class. However, all
156 * widgets which have their own controls declare this function as well, overriding the original
157 * declaration. If `has_own_method()` would be called by a Widget's class which implements `register_controls()`,
158 * with 'register_controls' passed as the first parameter - `has_own_method()` will return true. If the Widget
159 * does not declare `register_controls()`, `has_own_method()` will return false.
160 *
161 * Example #2 - both $method_name and $base_class_name are passed
162 * In this example, the widget class inherits from a base class `Widget_Base`, and the base implements
163 * `register_controls()` to add certain controls to all widgets inheriting from it. `has_own_method()` is called by
164 * the widget, with the string 'register_controls' passed as the first parameter, and 'Elementor\Widget_Base' (its full name
165 * including the namespace) passed as the second parameter. If the widget class implements `register_controls()`,
166 * `has_own_method` will return true. If the widget class DOESN'T implement `register_controls()`, it will return
167 * false (because `Widget_Base` is the declaring class for `register_controls()`, and not the class that called
168 * `has_own_method()`).
169 *
170 * @since 3.1.0
171 *
172 * @param string $method_name
173 * @param string $base_class_name
174 *
175 * @return bool True if the method was declared by the current instance, False if it was inherited.
176 */
177 public function has_own_method( $method_name, $base_class_name = null ) {
178 try {
179 $reflection_method = new \ReflectionMethod( $this, $method_name );
180
181 // If a ReflectionMethod is successfully created, get its declaring class.
182 $declaring_class = $reflection_method->getDeclaringClass();
183 } catch ( \Exception $e ) {
184 return false;
185 }
186
187 if ( $base_class_name ) {
188 return $base_class_name !== $declaring_class->name;
189 }
190
191 return get_called_class() === $declaring_class->name;
192 }
193 }
194