PluginProbe
Page Builder: Pagelayer – Drag and Drop website builder / 2.2.0
Page Builder: Pagelayer – Drag and Drop website builder v2.2.0
2.2.1 2.2.0 2.1.9 2.1.8 2.1.7 2.1.6 2.1.5 2.1.4 2.1.3 trunk 0.9.0 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5 0.9.6 0.9.7 0.9.8 0.9.9 1.0.0 1.0.2 1.0.3 1.0.4 1.0.5 All 129 releases
pagelayer / main / blocks.php

blocks.php in Page Builder: Pagelayer – Drag and Drop website builder 2.2.0, at main/blocks.php

550 lines 14.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 //////////////////////////////////////////////////////////////
4 //===========================================================
5 // PAGELAYER
6 // Inspired by the DESIRE to be the BEST OF ALL
7 // ----------------------------------------------------------
8 // Started by: Pulkit Gupta
9 // Date: 23rd Jan 2017
10 // Time: 23:00 hrs
11 // Site: http://pagelayer.com/wordpress (PAGELAYER)
12 // ----------------------------------------------------------
13 // Please Read the Terms of use at http://pagelayer.com/tos
14 // ----------------------------------------------------------
15 //===========================================================
16 // (c)Pagelayer Team
17 //===========================================================
18 //////////////////////////////////////////////////////////////
19
20 // Are we being accessed directly ?
21 if(!defined('PAGELAYER_VERSION')) {
22 exit('Hacking Attempt !');
23 }
24
25 if( version_compare( get_bloginfo( 'version' ), '5.8', '>=' ) ){
26 add_filter( 'block_categories_all', 'pagelayer_block_category', 999999999);
27 }else{
28 add_filter( 'block_categories', 'pagelayer_block_category', 999999999 );
29 }
30
31 function pagelayer_block_category( $categories ) {
32
33 // Create a custom category and add it at the beginning of the list
34 array_unshift($categories, array(
35 'slug' => 'pagelayer',
36 'title' => 'Pagelayer',
37 ));
38
39 return $categories;
40 }
41
42 function pagelayer_block_name_by_tag($tag){
43 return 'pagelayer/' . str_replace('_', '-', $tag);
44 }
45
46 add_action('template_redirect', 'pagelayer_block_init');
47 function pagelayer_block_init(){
48 global $pagelayer;
49
50 // Not load for gutenberg
51 if(!function_exists('register_block_type') || !pagelayer_has_blocks() || pagelayer_is_gutenberg_editor()){
52 return;
53 }
54
55 // Load shortcodes
56 pagelayer_load_shortcodes();
57
58 $pl_blocks_styles = $pagelayer->styles;
59
60 foreach ($pagelayer->shortcodes as $block => $pl_props) {
61
62 // Skip blocks not meant for Gutenberg
63 if(!empty($pl_props['no_gt']) || $pl_props['group'] == 'woocommerce'){
64 continue;
65 }
66
67 // Create attribute Object
68 $attributes = [];
69 $pagelayer_tabs = ['settings', 'options'];
70
71 foreach($pagelayer_tabs as $tab){
72
73 if (empty($pl_props[$tab]) || !is_array($pl_props[$tab])) continue;
74
75 foreach($pl_props[$tab] as $section => $props){
76 $props = array_key_exists($section, $pl_props) ? $pl_props[$section] : $pl_blocks_styles[$section];
77
78 // Reset / Create the cache
79 foreach($props as $x => $prop){
80 $attributes[$x] = [
81 'type' => $prop['type']
82 ];
83
84 if ($prop['type'] === 'image') {
85 $attributes['pagelayer-srcset'] = [
86 'type' => 'string'
87 ];
88 }
89
90 // Are we to set this value?
91 if (isset($prop['default']) && !empty($prop['default'])) {
92 $tmp_val = $prop['default'];
93
94 // If there is a unit and there is no unit suffix in atts value
95 if(isset($prop['units'])){
96 if (is_numeric($tmp_val)) {
97 $tmp_val = $tmp_val . $prop['units'][0];
98 } else {
99 $sep = isset($prop['sep']) ? $prop['sep'] : ',';
100 $tmp2 = explode($sep, $tmp_val);
101 foreach ($tmp2 as $k => $value) {
102 if (is_numeric($value)) {
103 $tmp2[$k] = $value . $prop['units'][0];
104 }
105 }
106 $tmp_val = implode($sep, $tmp2);
107 }
108 }
109
110 $attributes[$x]['default'] = $tmp_val;
111 }
112
113 $modes = ['tablet', 'mobile'];
114
115 // Do we have screen?
116 if (array_key_exists('screen', $prop)) {
117 foreach ($modes as $m) {
118 $prop_name = $x . '_' . $m;
119
120 $attributes[$prop_name] = [
121 'type' => $prop['type']
122 ];
123
124 // TODO:
125 // if (array_key_exists('default', $props[$prop_name])) {
126 // $attributes[$prop_name]['default'] = $props[$x]['default'];
127 // }
128 }
129 }
130 }
131 }
132 }
133
134 // Register blocks
135 register_block_type(
136 pagelayer_block_name_by_tag($block),
137 array(
138 'attributes' => $attributes,
139 'render_callback' => 'pagelayer_block_renderer',
140 )
141 );
142 }
143 }
144
145 function pagelayer_block_renderer($attributes, $content, $_this){
146 global $pagelayer;
147
148 $parsed_block = $_this->parsed_block;
149 $block_name = $parsed_block['blockName'];
150 $tag = '';
151 $inner_blocks = array(
152 'blocks' => $parsed_block['innerBlocks'],
153 'content' => $parsed_block['innerContent']
154 );
155 $attributes['is_not_sc'] = 1;
156
157 if ( is_string( $block_name ) && 0 === strpos( $block_name, 'pagelayer/' ) ) {
158 $tag = substr( $block_name, 10 );
159 }
160
161 // Convert as pagelayer shortcode
162 $tag = str_replace('-', '_', $tag);
163
164 if( empty($tag) || !array_key_exists($tag, $pagelayer->shortcodes) ){
165 return '';
166 }
167
168 return pagelayer_render_shortcode($attributes, $content, $tag, $inner_blocks);
169 }
170
171 add_action('enqueue_block_editor_assets', 'pagelayer_enqueue_block_assets');
172 function pagelayer_enqueue_block_assets(){
173 global $pagelayer;
174
175 // For gutenberg
176 if(!pagelayer_is_gutenberg_editor()){
177 return;
178 }
179
180 wp_enqueue_style( 'pagelayer-block-icon', PAGELAYER_CSS . '/pagelayer-icons.css', array('wp-edit-blocks'), PAGELAYER_VERSION );
181
182 // Load styles and javascript
183 pagelayer_enqueue_frontend(true);
184
185 wp_enqueue_style( 'pagelayer-block-editor', PAGELAYER_CSS . '/pagelayer-blocks.css', array('wp-edit-blocks'), PAGELAYER_VERSION );
186
187 // Components
188 wp_enqueue_script( 'pagelayer-blocks', PAGELAYER_JS . '/blocks/index.js', [ 'wp-blob', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-html-entities', 'wp-i18n' ], PAGELAYER_VERSION, true );
189
190 // Load shortcode
191 pagelayer_load_shortcodes();
192
193 // Load fonts
194 pagelayer_load_font_options();
195
196 $shortcodes = array();
197
198 foreach($pagelayer->shortcodes as $block => $pl_props) {
199
200 if(!empty($pl_props['no_gt']) || $pl_props['group'] == 'woocommerce'){
201 continue;
202 }
203
204 $shortcodes[$block] = $pl_props;
205 }
206
207 wp_localize_script( 'pagelayer-blocks', 'pagelayer_config',
208 array(
209 'pagelayer_shortcodes' => $shortcodes,
210 'pagelayer_styles' => $pagelayer->styles,
211 'pagelayer_groups' => $pagelayer->groups,
212 'internal_linking_nonce' => wp_create_nonce('internal-linking'),
213 'pagelayer_fonts' => $pagelayer->fonts,
214 )
215 );
216 }
217
218 // Load global JS
219 add_action( 'admin_print_scripts', 'pagelayer_block_global_js');
220 function pagelayer_block_global_js(){
221 global $pagelayer, $post;
222
223 // For gutenberg
224 if(!pagelayer_is_gutenberg_editor()){
225 return;
226 }
227
228 // Load global colors and fonts
229 pagelayer_load_global_palette();
230
231 $pagelayer_recaptch_lang = get_option('pagelayer_google_captcha_lang');
232 $pagelayer_recaptch_version = get_option('pagelayer_recaptcha_version', '');
233
234 $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
235
236 // Get CAPTCHA site key
237 $pagelayer_recaptch_site_key = get_option('pagelayer_google_captcha');
238 $pro_url = defined('POPULARFX_PRO_URL') ? POPULARFX_PRO_URL : PAGELAYER_PRO_PRICE_URL;
239 $pro_txt = defined('POPULARFX_PRO_URL') ? 'PopularFX Pro' : 'Pagelayer Pro';
240
241 echo '<script type="text/javascript" id="pagelayer-block-global-js">
242 pagelayer_ajax_url = "'.admin_url( 'admin-ajax.php' ).'?";
243 pagelayer_url = "'.PAGELAYER_URL.'";
244 pagelayer_ver = "'.PAGELAYER_VERSION.'";
245 pagelayer_global_nonce = "'.wp_create_nonce('pagelayer_global').'";
246 pagelayer_server_time = '.time().';
247 pagelayer_pro = '.(int)defined('PAGELAYER_PREMIUM').';
248 pagelayer_is_live = 1;
249 pagelayer_pro_url = "'. $pro_url .'";
250 pagelayer_pro_txt = "'. $pro_txt .'";
251 pagelayer_facebook_id = "'.get_option('pagelayer-fbapp-id').'";
252 pagelayer_settings = '.json_encode($pagelayer->settings).';
253 pagelayer_recaptch_lang = "'.(!empty($pagelayer_recaptch_lang) ? $pagelayer_recaptch_lang : '').'";
254 pagelayer_recaptch_version = "'.(!empty($pagelayer_recaptch_version) ? $pagelayer_recaptch_version : '').'";
255 pagelayer_global_colors = '.json_encode($pagelayer->global_colors).';
256 pagelayer_global_fonts = '.json_encode($pagelayer->global_fonts).';
257 pagelayer_ajax_nonce = "'.wp_create_nonce('pagelayer_ajax').'";
258 pagelayer_post_permalink = "'.get_permalink($post->ID).'";
259 pagelayer_author = '.json_encode(pagelayer_author_data($post->ID)).';
260 pagelayer_postID = "'.$post->ID.'";
261 pagelayer_site_logo = '.json_encode(pagelayer_site_logo()).';
262 pagelayer_recaptch_site_key = "'.(!empty($pagelayer_recaptch_site_key) ? $pagelayer_recaptch_site_key : '').'";
263 pagelayer_global_colors = '.json_encode($pagelayer->global_colors).';
264 pagelayer_global_fonts = '.json_encode($pagelayer->global_fonts).';
265 pagelayer_loaded_icons = '.json_encode(pagelayer_enabled_icons()).';
266 pagelayer_customizer_url = "'.admin_url("/customize.php?return=").urlencode($referer).'";
267 pagelayerCacheBlockTags = {};
268 </script>';
269
270 echo '<style id="pagelayer-block-global-style">
271 @media (min-width: '.($pagelayer->settings['tablet_breakpoint'] + 1).'px){
272 .pagelayer-hide-desktop{
273 filter:blur(3px);
274 }
275 .pagelayer-hide-desktop *{
276 filter:blur(2px);
277 }
278 }
279
280 @media (max-width: '.$pagelayer->settings['tablet_breakpoint'].'px) and (min-width: '.($pagelayer->settings['mobile_breakpoint'] + 1).'px){
281 .pagelayer-hide-tablet{
282 filter:blur(3px);
283 }
284 .pagelayer-hide-tablet *{
285 filter:blur(2px);
286 }
287 }
288
289 @media (max-width: '.$pagelayer->settings['mobile_breakpoint'].'px){
290 .pagelayer-hide-mobile{
291 filter:blur(3px);
292 }
293
294 .pagelayer-hide-mobile *{
295 filter:blur(2px);
296 }
297 }
298 </style>';
299
300 }
301
302 // Schema for save contact form template via react
303 add_action( 'init', 'pagelayer_register_metadata' );
304 function pagelayer_register_metadata() {
305
306 register_meta(
307 'post',
308 'pagelayer_contact_templates',
309 array(
310 'type' => 'object',
311 'description' => 'Contacts Data',
312 'single' => true,
313 'show_in_rest' => array(
314 'schema' => array(
315 'additionalProperties' => true,
316 'items' => array(
317 'type' => 'array',
318 'items' => array(
319 'type' => 'array',
320 'items' => array(
321 'type' => 'string',
322 ),
323 ),
324 ),
325 ),
326 ),
327 'auth_callback' => function() {
328 return current_user_can('activate_plugins');
329 }
330 )
331 );
332
333 register_meta(
334 'post',
335 '_pagelayer_content',
336 array(
337 'type' => 'string',
338 'description' => 'Menu Content',
339 'single' => true,
340 'show_in_rest' => true,
341 'auth_callback' => function() {
342 return current_user_can('edit_posts');
343 }
344 )
345 );
346 }
347
348 add_filter( 'the_post', 'pagelayer_blocks_the_post' );
349 function pagelayer_blocks_the_post( $post ) {
350
351 if(!pagelayer_is_gutenberg_editor() || !has_blocks( $post ) ){
352 return;
353 }
354
355 // call block register
356 // It is being used to load the runtime font family
357 pagelayer_block_init();
358
359 $post->post_content = pagelayer_add_tmp_atts($post->post_content);
360 }
361
362 // Add tmp attribute to block code
363 function pagelayer_add_tmp_atts($content){
364
365 $blocks = parse_blocks( $content );
366 $output = '';
367
368 foreach ( $blocks as $block ) {
369 $block_name = $block['blockName'];
370
371 // Is pagelayer block
372 if ( is_string( $block_name ) && 0 === strpos( $block_name, 'pagelayer/' ) ) {
373 $_block = pagelayer_serialize_block($block);
374 $output .= serialize_block($_block);
375 continue;
376 }
377
378 $output .= serialize_block($block);
379 }
380
381 return $output;
382 }
383
384 function pagelayer_serialize_block($block){
385 global $pagelayer;
386
387 // Load shortcode
388 pagelayer_load_shortcodes();
389
390 // If block saved by Pagelayer Editor
391 if(in_array( $block['blockName'], ['pagelayer/pl_inner_col', 'pagelayer/pl_inner_row'])){
392 $block['blockName'] = str_replace('inner_', '', $block['blockName']);
393 }
394
395 $tag = substr( $block['blockName'], 10 );
396 $pl_tag = str_replace('-', '_', $tag);
397
398 if(isset($pagelayer->shortcodes[$pl_tag])){
399
400 // Create attribute Object
401 $pl_props = $pagelayer->shortcodes[$pl_tag];
402 $el = array(
403 'atts' => $block['attrs'],
404 'tmp' => []
405 );
406
407 foreach($pagelayer->tabs as $tab){
408
409 if(empty($pl_props[$tab])){
410 continue;
411 }
412
413 foreach($pl_props[$tab] as $section => $_props){
414
415 $props = !empty($pl_props[$section]) ? $pl_props[$section] : $pagelayer->styles[$section];
416
417 if(empty($props)){
418 continue;
419 }
420
421 // Reset / Create the cache
422 foreach($props as $prop => $param){
423
424 // No value set
425 if(empty($el['atts'][$prop])){
426 continue;
427 }
428
429 // Load any attachment values - This should go on top in the newer version @TODO
430 if(in_array($param['type'], ['image', 'video', 'audio', 'media'])){
431 $attachment = ($param['type'] == 'image') ? pagelayer_image(@$el['atts'][$prop]) : pagelayer_attachment(@$el['atts'][$prop]);
432
433 if(!empty($attachment)){
434 foreach($attachment as $k => $v){
435 $el['tmp'][$prop.'-'.$k] = $v;
436 }
437 }
438
439 }
440
441 // Load any attachment values - This should go on top in the newer version @TODO
442 if($param['type'] == 'multi_image'){
443
444 $img_ids = pagelayer_maybe_explode(',', $el['atts'][$prop]);
445 $img_urls = [];
446
447 // Make the image URL
448 foreach($img_ids as $k => $v){
449 $image = pagelayer_image($v);
450 $img_urls['i'.$v] = @$image['url'];
451 }
452
453 $el['tmp'][$prop.'-urls'] = json_encode($img_urls);
454 }
455
456 // Load permalink values
457 if($param['type'] == 'link'){
458
459 $link = $el['atts'][$prop];
460
461 if( is_array($el['atts'][$prop]) ){
462
463 // Link is required for check IF and IF-EXT in html
464 if(!isset($el['atts'][$prop]['link']) || strlen(trim($el['atts'][$prop]['link'])) < 1){
465 $link = '';
466 unset($el['atts'][$prop]);
467 continue;
468 }
469
470 $link = $el['atts'][$prop]['link'];
471 }
472
473 $el['tmp'][$prop] = pagelayer_permalink($link);
474 }
475 }
476 }
477 }
478
479 $func = null;
480
481 if(substr($pl_tag, 0, 3) == 'pl_'){
482 $func = 'pagelayer_sc_block_'.substr($pl_tag, 3);
483 }
484
485 if(function_exists($func)){
486 call_user_func_array($func, array(&$el));
487 }
488
489 if(!empty($el['tmp'])){
490 $_tmp = $el['tmp'];
491 $block['attrs']['tmpAtts'] = array_filter($_tmp);
492 }
493
494 // If block saved by Pagelayer Editor
495 if(strpos($block['blockName'], '_') !== false){
496 $block['blockName'] = str_replace('_', '-', $block['blockName']);
497 }
498 }
499
500 // This have innerBlocks
501 if(!empty($block['innerBlocks'])){
502 foreach($block['innerBlocks'] as $key => $inner_block){
503 $block['innerBlocks'][$key] = pagelayer_serialize_block($inner_block);
504 }
505 }
506
507 return $block;
508 }
509
510 // TODO: create a seprate file or use all the functions from pagelayer editor files
511 //Grid Gallery Handler
512 function pagelayer_sc_block_grid_gallery(&$el){
513
514 if(empty($el['atts']['ids'])){
515 $el['atts']['ids'] = '';
516 }
517
518 $ids = pagelayer_maybe_explode(',', $el['atts']['ids']);
519 $urls = [];
520 $all_urls = [];
521 $size = $el['atts']['size'];
522
523 // Make the image URL
524 foreach($ids as $k => $v){
525
526 $image = pagelayer_image($v);
527
528 $urls['i'.$v] = @$image['url'];
529 $links['i'.$v] = @$image['link'];
530 $titles['i'.$v] = @$image['title'];
531 $captions['i'.$v] = @$image['caption'];
532
533 foreach($image as $kk => $vv){
534 $si = strstr($kk, '-url', true);
535 if(!empty($si)){
536 $all_urls['i'.$v][$si] = $vv;
537 }
538 }
539
540 }
541
542 // Make the TMP vars
543 if(!empty($urls)){
544 $el['tmp']['ids-urls'] = json_encode($urls);
545 $el['tmp']['ids-all-urls'] = json_encode($all_urls);
546 $el['tmp']['ids-all-links'] = json_encode($links);
547 $el['tmp']['ids-all-titles'] = json_encode($titles);
548 $el['tmp']['ids-all-captions'] = json_encode($captions);
549 }
550 }