PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.3
Elementor Website Builder – more than just a page builder v4.1.3
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 4.1.3, at core/base/base-object.php

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