module-base.php
98 lines
| 1 | <?php |
| 2 | namespace WprAddons\Base; |
| 3 | |
| 4 | abstract class Module_Base { |
| 5 | |
| 6 | /** |
| 7 | * @var \ReflectionClass |
| 8 | */ |
| 9 | private $reflection; |
| 10 | |
| 11 | private $components = []; |
| 12 | |
| 13 | /** |
| 14 | * @var Module_Base |
| 15 | */ |
| 16 | protected static $_instances = []; |
| 17 | |
| 18 | /** |
| 19 | * Throw error on object clone |
| 20 | * |
| 21 | * The whole idea of the singleton design pattern is that there is a single |
| 22 | * object therefore, we don't want the object to be cloned. |
| 23 | * |
| 24 | * @since 1.0 |
| 25 | * @return void |
| 26 | */ |
| 27 | public function __clone() { |
| 28 | // Cloning instances of the class is forbidden |
| 29 | _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin huh?', 'wpr-addons' ), '1.0' ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Disable unserializing of the class |
| 34 | * |
| 35 | * @since 1.0 |
| 36 | * @return void |
| 37 | */ |
| 38 | public function __wakeup() { |
| 39 | // Unserializing instances of the class is forbidden |
| 40 | _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin huh?', 'wpr-addons' ), '1.0' ); |
| 41 | } |
| 42 | |
| 43 | public static function class_name() { |
| 44 | return get_called_class(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return Module_Base |
| 49 | */ |
| 50 | public static function instance() { |
| 51 | if ( empty( static::$_instances[ static::class_name() ] ) ) { |
| 52 | static::$_instances[ static::class_name() ] = new static(); |
| 53 | } |
| 54 | |
| 55 | return static::$_instances[ static::class_name() ]; |
| 56 | } |
| 57 | |
| 58 | abstract public function get_name(); |
| 59 | |
| 60 | public function get_assets_url() { |
| 61 | return WPR_ADDONS_MODULES_URL . $this->get_name() . '/assets/'; |
| 62 | } |
| 63 | |
| 64 | public function get_widgets() { |
| 65 | return []; |
| 66 | } |
| 67 | |
| 68 | public function __construct() { |
| 69 | $this->reflection = new \ReflectionClass( $this ); |
| 70 | |
| 71 | // GOGA |
| 72 | // add_action( 'elementor/widgets/widgets_registered', [ $this, 'init_widgets' ] ); |
| 73 | add_action( 'elementor/widgets/register', [ $this, 'init_widgets' ] ); |
| 74 | } |
| 75 | |
| 76 | public function init_widgets() { |
| 77 | $widget_manager = \Elementor\Plugin::instance()->widgets_manager; |
| 78 | |
| 79 | foreach ( $this->get_widgets() as $widget ) { |
| 80 | $class_name = $this->reflection->getNamespaceName() .'\Widgets\\'. $widget; |
| 81 | // $widget_manager->register_widget_type( new $class_name() ); |
| 82 | $widget_manager->register( new $class_name() ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public function add_component( $id, $instance ) { |
| 87 | $this->components[ $id ] = $instance; |
| 88 | } |
| 89 | |
| 90 | public function get_component( $id ) { |
| 91 | if ( isset( $this->components[ $id ] ) ) { |
| 92 | return $this->components[ $id ]; |
| 93 | } |
| 94 | |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 |