| 1 |
<?php |
| 2 |
namespace Elementor\Core\Base; |
| 3 |
|
| 4 |
use Elementor\Plugin; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; // Exit if accessed directly. |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Elementor module. |
| 12 |
* |
| 13 |
* An abstract class that provides the needed properties and methods to |
| 14 |
* manage and handle modules in inheriting classes. |
| 15 |
* |
| 16 |
* @since 1.7.0 |
| 17 |
* @abstract |
| 18 |
*/ |
| 19 |
abstract class Module extends Base_Object { |
| 20 |
|
| 21 |
/** |
| 22 |
* Module class reflection. |
| 23 |
* |
| 24 |
* Holds the information about a class. |
| 25 |
* |
| 26 |
* @since 1.7.0 |
| 27 |
* @access private |
| 28 |
* |
| 29 |
* @var \ReflectionClass |
| 30 |
*/ |
| 31 |
private $reflection; |
| 32 |
|
| 33 |
/** |
| 34 |
* Module components. |
| 35 |
* |
| 36 |
* Holds the module components. |
| 37 |
* |
| 38 |
* @since 1.7.0 |
| 39 |
* @access private |
| 40 |
* |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
private $components = []; |
| 44 |
|
| 45 |
/** |
| 46 |
* Module instance. |
| 47 |
* |
| 48 |
* Holds the module instance. |
| 49 |
* |
| 50 |
* @since 1.7.0 |
| 51 |
* @access protected |
| 52 |
* |
| 53 |
* @var Module |
| 54 |
*/ |
| 55 |
protected static $_instances = []; |
| 56 |
|
| 57 |
/** |
| 58 |
* Get module name. |
| 59 |
* |
| 60 |
* Retrieve the module name. |
| 61 |
* |
| 62 |
* @since 1.7.0 |
| 63 |
* @access public |
| 64 |
* @abstract |
| 65 |
* |
| 66 |
* @return string Module name. |
| 67 |
*/ |
| 68 |
abstract public function get_name(); |
| 69 |
|
| 70 |
/** |
| 71 |
* Instance. |
| 72 |
* |
| 73 |
* Ensures only one instance of the module class is loaded or can be loaded. |
| 74 |
* |
| 75 |
* @since 1.7.0 |
| 76 |
* @access public |
| 77 |
* @static |
| 78 |
* |
| 79 |
* @return Module An instance of the class. |
| 80 |
*/ |
| 81 |
public static function instance() { |
| 82 |
$class_name = static::class_name(); |
| 83 |
|
| 84 |
if ( empty( static::$_instances[ $class_name ] ) ) { |
| 85 |
static::$_instances[ $class_name ] = new static(); |
| 86 |
} |
| 87 |
|
| 88 |
return static::$_instances[ $class_name ]; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @since 2.0.0 |
| 93 |
* @access public |
| 94 |
* @static |
| 95 |
*/ |
| 96 |
public static function is_active() { |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Class name. |
| 102 |
* |
| 103 |
* Retrieve the name of the class. |
| 104 |
* |
| 105 |
* @since 1.7.0 |
| 106 |
* @access public |
| 107 |
* @static |
| 108 |
*/ |
| 109 |
public static function class_name() { |
| 110 |
return get_called_class(); |
| 111 |
} |
| 112 |
|
| 113 |
public static function get_experimental_data() { |
| 114 |
return []; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Clone. |
| 119 |
* |
| 120 |
* Disable class cloning and throw an error on object clone. |
| 121 |
* |
| 122 |
* The whole idea of the singleton design pattern is that there is a single |
| 123 |
* object. Therefore, we don't want the object to be cloned. |
| 124 |
* |
| 125 |
* @since 1.7.0 |
| 126 |
* @access public |
| 127 |
*/ |
| 128 |
public function __clone() { |
| 129 |
_doing_it_wrong( |
| 130 |
__FUNCTION__, |
| 131 |
sprintf( 'Cloning instances of the singleton "%s" class is forbidden.', get_class( $this ) ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 132 |
'1.0.0' |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Wakeup. |
| 138 |
* |
| 139 |
* Disable unserializing of the class. |
| 140 |
* |
| 141 |
* @since 1.7.0 |
| 142 |
* @access public |
| 143 |
*/ |
| 144 |
public function __wakeup() { |
| 145 |
_doing_it_wrong( |
| 146 |
__FUNCTION__, |
| 147 |
sprintf( 'Unserializing instances of the singleton "%s" class is forbidden.', get_class( $this ) ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 148 |
'1.0.0' |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* @since 2.0.0 |
| 154 |
* @access public |
| 155 |
*/ |
| 156 |
public function get_reflection() { |
| 157 |
if ( null === $this->reflection ) { |
| 158 |
$this->reflection = new \ReflectionClass( $this ); |
| 159 |
} |
| 160 |
|
| 161 |
return $this->reflection; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Add module component. |
| 166 |
* |
| 167 |
* Add new component to the current module. |
| 168 |
* |
| 169 |
* @since 1.7.0 |
| 170 |
* @access public |
| 171 |
* |
| 172 |
* @param string $id Component ID. |
| 173 |
* @param mixed $instance An instance of the component. |
| 174 |
*/ |
| 175 |
public function add_component( $id, $instance ) { |
| 176 |
$this->components[ $id ] = $instance; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* @since 2.3.0 |
| 181 |
* @access public |
| 182 |
* @return Module[] |
| 183 |
*/ |
| 184 |
public function get_components() { |
| 185 |
return $this->components; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Get module component. |
| 190 |
* |
| 191 |
* Retrieve the module component. |
| 192 |
* |
| 193 |
* @since 1.7.0 |
| 194 |
* @access public |
| 195 |
* |
| 196 |
* @param string $id Component ID. |
| 197 |
* |
| 198 |
* @return mixed An instance of the component, or `false` if the component |
| 199 |
* doesn't exist. |
| 200 |
*/ |
| 201 |
public function get_component( $id ) { |
| 202 |
if ( isset( $this->components[ $id ] ) ) { |
| 203 |
return $this->components[ $id ]; |
| 204 |
} |
| 205 |
|
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Get assets url. |
| 211 |
* |
| 212 |
* @since 2.3.0 |
| 213 |
* @access protected |
| 214 |
* |
| 215 |
* @param string $file_name |
| 216 |
* @param string $file_extension |
| 217 |
* @param string $relative_url Optional. Default is null. |
| 218 |
* @param string $add_min_suffix Optional. Default is 'default'. |
| 219 |
* |
| 220 |
* @return string |
| 221 |
*/ |
| 222 |
final protected function get_assets_url( $file_name, $file_extension, $relative_url = null, $add_min_suffix = 'default' ) { |
| 223 |
static $is_test_mode = null; |
| 224 |
|
| 225 |
if ( null === $is_test_mode ) { |
| 226 |
$is_test_mode = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || defined( 'ELEMENTOR_TESTS' ) && ELEMENTOR_TESTS; |
| 227 |
} |
| 228 |
|
| 229 |
if ( ! $relative_url ) { |
| 230 |
$relative_url = $this->get_assets_relative_url() . $file_extension . '/'; |
| 231 |
} |
| 232 |
|
| 233 |
$url = $this->get_assets_base_url() . $relative_url . $file_name; |
| 234 |
|
| 235 |
if ( 'default' === $add_min_suffix ) { |
| 236 |
$add_min_suffix = ! $is_test_mode; |
| 237 |
} |
| 238 |
|
| 239 |
if ( $add_min_suffix ) { |
| 240 |
$url .= '.min'; |
| 241 |
} |
| 242 |
|
| 243 |
return $url . '.' . $file_extension; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Get js assets url |
| 248 |
* |
| 249 |
* @since 2.3.0 |
| 250 |
* @access protected |
| 251 |
* |
| 252 |
* @param string $file_name |
| 253 |
* @param string $relative_url Optional. Default is null. |
| 254 |
* @param string $add_min_suffix Optional. Default is 'default'. |
| 255 |
* |
| 256 |
* @return string |
| 257 |
*/ |
| 258 |
final protected function get_js_assets_url( $file_name, $relative_url = null, $add_min_suffix = 'default' ) { |
| 259 |
return $this->get_assets_url( $file_name, 'js', $relative_url, $add_min_suffix ); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Get css assets url |
| 264 |
* |
| 265 |
* @since 2.3.0 |
| 266 |
* @access protected |
| 267 |
* |
| 268 |
* @param string $file_name |
| 269 |
* @param string $relative_url Optional. Default is null. |
| 270 |
* @param string $add_min_suffix Optional. Default is 'default'. |
| 271 |
* @param bool $add_direction_suffix Optional. Default is `false` |
| 272 |
* |
| 273 |
* @return string |
| 274 |
*/ |
| 275 |
final protected function get_css_assets_url( $file_name, $relative_url = null, $add_min_suffix = 'default', $add_direction_suffix = false ) { |
| 276 |
static $direction_suffix = null; |
| 277 |
|
| 278 |
if ( ! $direction_suffix ) { |
| 279 |
$direction_suffix = is_rtl() ? '-rtl' : ''; |
| 280 |
} |
| 281 |
|
| 282 |
if ( $add_direction_suffix ) { |
| 283 |
$file_name .= $direction_suffix; |
| 284 |
} |
| 285 |
|
| 286 |
return $this->get_assets_url( $file_name, 'css', $relative_url, $add_min_suffix ); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Get assets base url |
| 291 |
* |
| 292 |
* @since 2.6.0 |
| 293 |
* @access protected |
| 294 |
* |
| 295 |
* @return string |
| 296 |
*/ |
| 297 |
protected function get_assets_base_url() { |
| 298 |
return ELEMENTOR_URL; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Get assets relative url |
| 303 |
* |
| 304 |
* @since 2.3.0 |
| 305 |
* @access protected |
| 306 |
* |
| 307 |
* @return string |
| 308 |
*/ |
| 309 |
protected function get_assets_relative_url() { |
| 310 |
return 'assets/'; |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Get the module's associated widgets. |
| 315 |
* |
| 316 |
* @return string[] |
| 317 |
*/ |
| 318 |
protected function get_widgets() { |
| 319 |
return []; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Initialize the module related widgets. |
| 324 |
*/ |
| 325 |
public function init_widgets() { |
| 326 |
$widget_manager = Plugin::instance()->widgets_manager; |
| 327 |
|
| 328 |
foreach ( $this->get_widgets() as $widget ) { |
| 329 |
$class_name = $this->get_reflection()->getNamespaceName() . '\Widgets\\' . $widget; |
| 330 |
|
| 331 |
$widget_manager->register( new $class_name() ); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
public function __construct() { |
| 336 |
add_action( 'elementor/widgets/register', [ $this, 'init_widgets' ] ); |
| 337 |
} |
| 338 |
} |
| 339 |
|