balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

module.php (6826B)


      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 		// Cloning instances of the class is forbidden
    130 		_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'elementor' ), '1.0.0' );
    131 	}
    132 
    133 	/**
    134 	 * Wakeup.
    135 	 *
    136 	 * Disable unserializing of the class.
    137 	 *
    138 	 * @since 1.7.0
    139 	 * @access public
    140 	 */
    141 	public function __wakeup() {
    142 		// Unserializing instances of the class is forbidden
    143 		_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'elementor' ), '1.0.0' );
    144 	}
    145 
    146 	/**
    147 	 * @since 2.0.0
    148 	 * @access public
    149 	 */
    150 	public function get_reflection() {
    151 		if ( null === $this->reflection ) {
    152 			$this->reflection = new \ReflectionClass( $this );
    153 		}
    154 
    155 		return $this->reflection;
    156 	}
    157 
    158 	/**
    159 	 * Add module component.
    160 	 *
    161 	 * Add new component to the current module.
    162 	 *
    163 	 * @since 1.7.0
    164 	 * @access public
    165 	 *
    166 	 * @param string $id       Component ID.
    167 	 * @param mixed  $instance An instance of the component.
    168 	 */
    169 	public function add_component( $id, $instance ) {
    170 		$this->components[ $id ] = $instance;
    171 	}
    172 
    173 	/**
    174 	 * @since 2.3.0
    175 	 * @access public
    176 	 * @return Module[]
    177 	 */
    178 	public function get_components() {
    179 		return $this->components;
    180 	}
    181 
    182 	/**
    183 	 * Get module component.
    184 	 *
    185 	 * Retrieve the module component.
    186 	 *
    187 	 * @since 1.7.0
    188 	 * @access public
    189 	 *
    190 	 * @param string $id Component ID.
    191 	 *
    192 	 * @return mixed An instance of the component, or `false` if the component
    193 	 *               doesn't exist.
    194 	 */
    195 	public function get_component( $id ) {
    196 		if ( isset( $this->components[ $id ] ) ) {
    197 			return $this->components[ $id ];
    198 		}
    199 
    200 		return false;
    201 	}
    202 
    203 	/**
    204 	 * Get assets url.
    205 	 *
    206 	 * @since 2.3.0
    207 	 * @access protected
    208 	 *
    209 	 * @param string $file_name
    210 	 * @param string $file_extension
    211 	 * @param string $relative_url Optional. Default is null.
    212 	 * @param string $add_min_suffix Optional. Default is 'default'.
    213 	 *
    214 	 * @return string
    215 	 */
    216 	final protected function get_assets_url( $file_name, $file_extension, $relative_url = null, $add_min_suffix = 'default' ) {
    217 		static $is_test_mode = null;
    218 
    219 		if ( null === $is_test_mode ) {
    220 			$is_test_mode = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || defined( 'ELEMENTOR_TESTS' ) && ELEMENTOR_TESTS;
    221 		}
    222 
    223 		if ( ! $relative_url ) {
    224 			$relative_url = $this->get_assets_relative_url() . $file_extension . '/';
    225 		}
    226 
    227 		$url = $this->get_assets_base_url() . $relative_url . $file_name;
    228 
    229 		if ( 'default' === $add_min_suffix ) {
    230 			$add_min_suffix = ! $is_test_mode;
    231 		}
    232 
    233 		if ( $add_min_suffix ) {
    234 			$url .= '.min';
    235 		}
    236 
    237 		return $url . '.' . $file_extension;
    238 	}
    239 
    240 	/**
    241 	 * Get js assets url
    242 	 *
    243 	 * @since 2.3.0
    244 	 * @access protected
    245 	 *
    246 	 * @param string $file_name
    247 	 * @param string $relative_url Optional. Default is null.
    248 	 * @param string $add_min_suffix Optional. Default is 'default'.
    249 	 *
    250 	 * @return string
    251 	 */
    252 	final protected function get_js_assets_url( $file_name, $relative_url = null, $add_min_suffix = 'default' ) {
    253 		return $this->get_assets_url( $file_name, 'js', $relative_url, $add_min_suffix );
    254 	}
    255 
    256 	/**
    257 	 * Get css assets url
    258 	 *
    259 	 * @since 2.3.0
    260 	 * @access protected
    261 	 *
    262 	 * @param string $file_name
    263 	 * @param string $relative_url         Optional. Default is null.
    264 	 * @param string $add_min_suffix       Optional. Default is 'default'.
    265 	 * @param bool   $add_direction_suffix Optional. Default is `false`
    266 	 *
    267 	 * @return string
    268 	 */
    269 	final protected function get_css_assets_url( $file_name, $relative_url = null, $add_min_suffix = 'default', $add_direction_suffix = false ) {
    270 		static $direction_suffix = null;
    271 
    272 		if ( ! $direction_suffix ) {
    273 			$direction_suffix = is_rtl() ? '-rtl' : '';
    274 		}
    275 
    276 		if ( $add_direction_suffix ) {
    277 			$file_name .= $direction_suffix;
    278 		}
    279 
    280 		return $this->get_assets_url( $file_name, 'css', $relative_url, $add_min_suffix );
    281 	}
    282 
    283 	/**
    284 	 * Get assets base url
    285 	 *
    286 	 * @since 2.6.0
    287 	 * @access protected
    288 	 *
    289 	 * @return string
    290 	 */
    291 	protected function get_assets_base_url() {
    292 		return ELEMENTOR_URL;
    293 	}
    294 
    295 	/**
    296 	 * Get assets relative url
    297 	 *
    298 	 * @since 2.3.0
    299 	 * @access protected
    300 	 *
    301 	 * @return string
    302 	 */
    303 	protected function get_assets_relative_url() {
    304 		return 'assets/';
    305 	}
    306 
    307 	/**
    308 	 * Get the module's associated widgets.
    309 	 *
    310 	 * @return string[]
    311 	 */
    312 	protected function get_widgets() {
    313 		return [];
    314 	}
    315 
    316 	/**
    317 	 * Initialize the module related widgets.
    318 	 */
    319 	public function init_widgets() {
    320 		$widget_manager = Plugin::instance()->widgets_manager;
    321 
    322 		foreach ( $this->get_widgets() as $widget ) {
    323 			$class_name = $this->get_reflection()->getNamespaceName() . '\Widgets\\' . $widget;
    324 
    325 			$widget_manager->register_widget_type( new $class_name() );
    326 		}
    327 	}
    328 
    329 	public function __construct() {
    330 		add_action( 'elementor/widgets/widgets_registered', [ $this, 'init_widgets' ] );
    331 	}
    332 }