-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.php
147 lines (124 loc) · 3.06 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace ElementorBlockBuilder;
use Elementor\User;
use ElementorBlockBuilder\Blocks;
/**
* Class Plugin
*
* Main Plugin class
*/
class Plugin {
/**
* Instance
*
* @access private
* @static
*
* @var Plugin The single instance of the class.
*/
public static $instance = null;
/**
* Blocks
* @access protected
* @var array of blocks
*/
protected $blocks = [];
/**
* class aliases
* @access private
* @var array
*/
private $classes_aliases = [];
/**
* Instance
*
* Ensures only one instance of the class is loaded or can be loaded.
*
* @access public
*
* @return Plugin An instance of the class.
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
private function add_block( $block ) {
$this->blocks[ $block->get_name() ] = $block;
}
public function get_blocks( $block = null ) {
if ( isset( $this->blocks[ $block ] ) ) {
return $this->blocks[ $block ];
}
return $this->blocks;
}
/**
* @param $template
*
* @access public
* @return string
*/
public function template_include( $template ) {
if ( isset( $_REQUEST['elementor-block'] ) ) {
$post = get_post();
if ( $post && User::is_current_user_can_edit( $post->ID ) ) {
add_filter( 'show_admin_bar', '__return_false' );
//Compatibility for some Themes
remove_action( 'wp_head', '_admin_bar_bump_cb' );
return ELEMENTOR_PATH . 'modules/page-templates/templates/canvas.php';
}
}
return $template;
}
public function register_blocks() {
include_once BLOCK_BUILDER_PATH . '/blocks/template-block.php';
$this->add_block( new Blocks\Template_Block() );
}
public function autoload( $class ) {
if ( 0 !== strpos( $class, __NAMESPACE__ ) ) {
return;
}
$has_class_alias = isset( $this->classes_aliases[ $class ] );
// Backward Compatibility: Save old class name for set an alias after the new class is loaded
if ( $has_class_alias ) {
$class_alias_name = $this->classes_aliases[ $class ];
$class_to_load = $class_alias_name;
} else {
$class_to_load = $class;
}
if ( ! class_exists( $class_to_load ) ) {
$filename = strtolower(
preg_replace(
[ '/^' . __NAMESPACE__ . '\\\/', '/([a-z])([A-Z])/', '/_/', '/\\\/' ],
[ '', '$1-$2', '-', DIRECTORY_SEPARATOR ],
$class_to_load
)
);
$filename = BLOCK_BUILDER_PATH . $filename . '.php';
if ( is_readable( $filename ) ) {
include $filename;
}
}
if ( $has_class_alias ) {
class_alias( $class_alias_name, $class );
}
}
public static function elementor() {
return \Elementor\Plugin::$instance;
}
/**
* Plugin class constructor
*
* Register plugin action hooks and filters
*
* @access public
*/
public function __construct() {
spl_autoload_register( [ $this, 'autoload' ] );
add_filter( 'template_include', [ $this, 'template_include' ], 12 /* After WooCommerce & Elementor Pro - Locations Manager */ );
add_action( 'init', [ $this, 'register_blocks' ] );
}
}
// Instantiate Plugin Class
Plugin::instance();