-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathlazy-blocks.php
435 lines (374 loc) · 9.91 KB
/
lazy-blocks.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
/**
* Plugin Name: Lazy Blocks
* Description: Easily create custom blocks and custom meta fields for Gutenberg without hard coding.
* Version: 3.8.3
* Author: Lazy Blocks Team
* Author URI: https://www.lazyblocks.com/?utm_source=wordpress.org&utm_medium=readme&utm_campaign=byline
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: lazy-blocks
*
* @package lazyblocks
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! defined( 'LAZY_BLOCKS_VERSION' ) ) {
define( 'LAZY_BLOCKS_VERSION', '3.8.3' );
}
if ( ! class_exists( 'LazyBlocks' ) ) :
/**
* LazyBlocks Class
*/
class LazyBlocks {
/**
* The single class instance.
*
* @var null
*/
private static $instance = null;
/**
* Main Instance
* Ensures only one instance of this class exists in memory at any one time.
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
self::$instance->init();
}
return self::$instance;
}
/**
* The base path to the plugin in the file system.
*
* @var string
*/
public $plugin_path;
/**
* URL Link to plugin
*
* @var string
*/
public $plugin_url;
/**
* Plugin basename
*
* @var string
*/
public $plugin_basename;
/**
* Icons class object.
*
* @var LazyBlocks_Icons
*/
private $icons;
/**
* Controls class object.
*
* @var LazyBlocks_Controls
*/
private $controls;
/**
* Handlebars class object.
*
* @var LazyBlocks_Handlebars
*/
private $handlebars;
/**
* Blocks class object.
*
* @var LazyBlocks_Blocks
*/
private $blocks;
/**
* Templates class object.
*
* @var LazyBlocks_Templates
*/
private $templates;
/**
* Tools class object.
*
* @var LazyBlocks_Tools
*/
private $tools;
/**
* LazyBlocks constructor.
*/
public function __construct() {
/* We do nothing here! */
}
/**
* Activation Hook
*/
public function activation_hook() {
LazyBlocks_Dummy::add();
}
/**
* Deactivation Hook
*/
public function deactivation_hook() {}
/**
* Init.
*/
public function init() {
$this->plugin_path = plugin_dir_path( __FILE__ );
$this->plugin_url = plugin_dir_url( __FILE__ );
$this->plugin_basename = plugin_basename( __FILE__ );
$this->include_dependencies();
$this->icons = new LazyBlocks_Icons();
$this->controls = new LazyBlocks_Controls();
$this->handlebars = new LazyBlocks_Handlebars();
$this->blocks = new LazyBlocks_Blocks();
$this->templates = new LazyBlocks_Templates();
$this->tools = new LazyBlocks_Tools();
add_action( 'init', array( $this, 'init_hook' ), 5 );
}
/**
* Init hook should be used to register user blocks and add customizations.
*/
public function init_hook() {
$this->load_text_domain();
do_action( 'lzb/init' );
}
/**
* Get plugin_path.
*/
public function plugin_path() {
return apply_filters( 'lzb/plugin_path', $this->plugin_path );
}
/**
* Get plugin_url.
*/
public function plugin_url() {
return apply_filters( 'lzb/plugin_url', $this->plugin_url );
}
/**
* Get plugin_basename.
*/
public function plugin_basename() {
return apply_filters( 'lzb/plugin_basename', $this->plugin_basename );
}
/**
* Sets the text domain with the plugin translated into other languages.
*/
public function load_text_domain() {
load_plugin_textdomain( 'lazy-blocks', false, dirname( $this->plugin_basename() ) . '/languages/' );
}
/**
* Check if Pro plugin installed.
*/
public function is_pro() {
return defined( 'LAZY_BLOCKS_PRO' ) && LAZY_BLOCKS_PRO;
}
/**
* Get URL to main site with UTM tags.
*
* @param array $args - Arguments of link.
* @return string
*/
public function get_plugin_site_url( $args = array() ) {
$args = array_merge(
array(
'sub_path' => 'pro',
'utm_source' => 'plugin',
'utm_medium' => 'admin_menu',
'utm_campaign' => 'go_pro',
'utm_content' => LAZY_BLOCKS_VERSION,
),
$args
);
$url = 'https://www.lazyblocks.com/';
$first_flag = true;
if ( isset( $args['sub_path'] ) && ! empty( $args['sub_path'] ) ) {
$url .= $args['sub_path'] . '/';
}
foreach ( $args as $key => $value ) {
if ( 'sub_path' !== $key && ! empty( $value ) ) {
$url .= ( $first_flag ? '?' : '&' );
$url .= $key . '=' . $value;
$first_flag = false;
}
}
return $url;
}
/**
* Set plugin Dependencies.
*/
private function include_dependencies() {
// Deprecations run before all features.
require_once $this->plugin_path() . 'classes/class-deprecated.php';
require_once $this->plugin_path() . '/classes/class-assets.php';
require_once $this->plugin_path() . '/classes/class-migration.php';
require_once $this->plugin_path() . '/classes/class-admin.php';
require_once $this->plugin_path() . '/classes/class-icons.php';
require_once $this->plugin_path() . '/classes/class-controls.php';
require_once $this->plugin_path() . '/classes/class-handlebars.php';
require_once $this->plugin_path() . '/classes/class-blocks.php';
require_once $this->plugin_path() . '/classes/class-templates.php';
require_once $this->plugin_path() . '/classes/class-tools.php';
require_once $this->plugin_path() . '/classes/class-rest.php';
require_once $this->plugin_path() . '/classes/class-dummy.php';
require_once $this->plugin_path() . '/classes/class-force-gutenberg.php';
require_once $this->plugin_path() . '/classes/class-deactivate-duplicate-plugin.php';
require_once $this->plugin_path() . '/classes/class-wpml.php';
require_once $this->plugin_path() . '/classes/3rd/class-astra.php';
}
/**
* Get lazyblocks icons object.
*/
public function icons() {
return $this->icons;
}
/**
* Get lazyblocks controls object.
*/
public function controls() {
return $this->controls;
}
/**
* Get lazyblocks handlebars object.
*/
public function handlebars() {
return $this->handlebars;
}
/**
* Get lazyblocks blocks object.
*/
public function blocks() {
return $this->blocks;
}
/**
* Get lazyblocks templates object.
*/
public function templates() {
return $this->templates;
}
/**
* Get lazyblocks tools object.
*/
public function tools() {
return $this->tools;
}
/**
* Add lazyblocks block.
*
* @param array $data - block data.
*/
public function add_block( $data ) {
return $this->blocks()->add_block( $data );
}
/**
* Add lazyblocks block collection.
*
* This method is does nothing in Free plugin,
* the actual implementation is in Pro plugin.
*
* @param array $data - collection data.
*/
public function add_collection( $data ) {
do_action( 'lzb/add_collection', $data );
}
/**
* Add lazyblocks template.
*
* @param array $data - template data.
*/
public function add_template( $data ) {
return $this->templates()->add_template( $data );
}
/**
* Include plugin with theme or plugin.
*
* @link https://www.lazyblocks.com/docs/examples/include-lazy-blocks-within-theme-or-plugin/
*
* @param string $url - url for the new root of the Lazy Blocks.
*/
public function include_within( $url ) {
// Customize the url setting to fix incorrect asset URLs.
add_filter(
'lzb/plugin_url',
function() use ( $url ) {
return $url . ( lazyblocks()->is_pro() ? 'core-plugin/' : '' );
}
);
add_filter(
'lzb_pro/plugin_url',
function() use ( $url ) {
return $url;
}
);
}
}
/**
* The main cycle of the plugin.
*
* @return null|LazyBlocks
*/
function lazyblocks() {
return LazyBlocks::instance();
}
// Initialize.
lazyblocks();
// Activation / Deactivation hooks.
register_activation_hook( __FILE__, array( lazyblocks(), 'activation_hook' ) );
register_deactivation_hook( __FILE__, array( lazyblocks(), 'deactivation_hook' ) );
/**
* Function to get meta value with some improvements for Lazyblocks metas.
*
* @param string $name - metabox name.
* @param int|null $id - post id.
*
* @return array|mixed|object
*/
// phpcs:ignore
function get_lzb_meta( $name, $id = null ) {
// global variable used to fix `get_lzb_meta` call inside block preview in editor.
global $lzb_preview_block_data;
$block_data = null;
$control_data = null;
if ( null === $id ) {
global $post;
if ( isset( $post->ID ) ) {
$id = $post->ID;
}
}
// Find control data by meta name.
$blocks = lazyblocks()->blocks()->get_blocks();
foreach ( $blocks as $block ) {
if ( isset( $block['controls'] ) && is_array( $block['controls'] ) ) {
foreach ( $block['controls'] as $control ) {
if ( $control_data || 'true' !== $control['save_in_meta'] ) {
continue;
}
$meta_name = false;
if ( isset( $control['save_in_meta_name'] ) && $control['save_in_meta_name'] ) {
$meta_name = $control['save_in_meta_name'];
} elseif ( $control['name'] ) {
$meta_name = $control['name'];
}
if ( $meta_name && $meta_name === $name ) {
$block_data = $block;
$control_data = $control;
}
}
}
}
$result = null;
$context = isset( $lzb_preview_block_data ) ? 'editor' : 'frontend';
if (
isset( $lzb_preview_block_data ) &&
is_array( $lzb_preview_block_data ) &&
isset( $control_data['name'] ) &&
isset( $lzb_preview_block_data['block_attributes'][ $control_data['name'] ] )
) {
$result = $lzb_preview_block_data['block_attributes'][ $control_data['name'] ];
} elseif ( $id ) {
$result = get_post_meta( $id, $name, true );
}
// apply filters for control values.
$result = lazyblocks()->controls()->filter_control_value( $result, $control_data, $block_data, $context );
return apply_filters( 'lzb/get_meta', $result, $name, $id, $control_data );
}
endif;