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