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 / template.php

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

1,464 lines 39.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 //////////////////////////////////////////////////////////////
4 //===========================================================
5 // template.php
6 //===========================================================
7 // PAGELAYER
8 // Inspired by the DESIRE to be the BEST OF ALL
9 // ----------------------------------------------------------
10 // Started by: Pulkit Gupta
11 // Date: 23rd Jan 2017
12 // Time: 23:00 hrs
13 // Site: http://pagelayer.com/wordpress (PAGELAYER)
14 // ----------------------------------------------------------
15 // Please Read the Terms of use at http://pagelayer.com/tos
16 // ----------------------------------------------------------
17 //===========================================================
18 // (c)Pagelayer Team
19 //===========================================================
20 //////////////////////////////////////////////////////////////
21
22 // Are we being accessed directly ?
23 if(!defined('PAGELAYER_VERSION')) {
24 exit('Hacking Attempt !');
25 }
26
27 // Define our array
28 function pagelayer_builder_array(){
29
30 global $pagelayer;
31
32 // Array for custom post type data
33 $pagelayer->builder = array(
34 'name' => 'pagelayer-template',
35 'type_slug' => 'pagelayer_template_type',
36 'type'=> [
37 '' => __('Select'),
38 'header' => __('Header'),
39 'footer' => __('Footer'),
40 'single' => array(
41 'label' => __('Single'),
42 'need_selection' => 'singular', // To select the value in template select and hide it
43 ),
44 'archive' => array(
45 'label' => __('Archive'),
46 'need_selection' => 'archives', // To select the value in template select and hide it
47 ),
48 'popup' => __('Popup'),
49 'section' => array(
50 'label' => __('Section'),
51 'no_condition' => 1, // To hide the conditions options
52 ),
53 'global_section' => array(
54 'label' => __('Global Section'),
55 'no_condition' => 1, // To hide the conditions options
56 'hide_drop' => 1,
57 ),
58 'global_widget' => array(
59 'label' => __('Global Widget'),
60 'no_condition' => 1, // To hide the conditions options
61 'hide_drop' => 1,
62 ),
63 ],
64 'action' => [
65 'include' => __('Include'),
66 'exclude' => __('Exclude'),
67 ],
68 'dispay_on' => [
69 '' => __('Full Site'),
70 'archives' => array(
71 'label' => __('Archives'),
72 'check_conditions' => function ( $args = null ){
73 return is_archive() || is_home() || is_search();
74 },
75 ),
76 'singular' => array(
77 'label' => __('Singular'),
78 'check_conditions' => function ( $args = null ){
79 return ( is_singular() && ! is_embed() ) || is_404();
80 },
81 ),
82 ],
83 // NOTE: Templates list array name should be dispay_on name with suffix _templates
84 'archives_templates'=> [
85 '' => __('All Archives / Posts Archive'),
86 'author' => array(
87 'label' => __('Author Archives'),
88 'check_conditions' => function ( $args = null ){
89 return is_author($args['id']);
90 },
91 'filter_type' => 'author', // should be (author, taxonomy and post type)
92 ),
93 'date' => array(
94 'label' => __('Date Archives'),
95 'check_conditions' => function ( $args = null ){
96 return is_date();
97 },
98 'no_id_section' => 1, // ID select section not required
99 ),
100 'search' => array(
101 'label' => __('Search Results'),
102 'check_conditions' => function ( $args = null ){
103 return is_search();
104 },
105 'no_id_section' => 1, // Id select section not required
106 ),
107 ],
108 // NOTE: Templates list array name should be dispay_on name with suffix _templates
109 'singular_templates'=> [
110 '' => __('All Singular'), // singular.php
111 'by_author' => array(
112 'label' => __('By Author'),
113 'check_conditions' => function ( $args = null ){
114 return is_singular() && get_post_field( 'post_author' ) === $args['id'];
115 },
116 'filter_type' => 'author',
117 ),
118 'front_page' => array(
119 'label' => __('Front Page'),
120 'check_conditions' => function ( $args = null ){
121 return is_front_page();
122 },
123 'no_id_section' => 1, // Id select section not required
124 ),
125 '404' => array(
126 'label' => __('404'),
127 'check_conditions' => function ( $args = null ){
128 return is_404();
129 },
130 'no_id_section' => 1, // Id select section not required
131 ),
132 //'direct_child_of' => __('Direct Child Of'),
133 //'any_child_of' => __('Any Child Of'),
134 ], // conditions by post type
135
136 );
137
138 add_shortcode($pagelayer->builder['name'], 'pagelayer_builder_posts_shortcode');
139
140 add_filter( 'manage_'. $pagelayer->builder['name'] .'_posts_columns', 'pagelayer_builder_add_columns' );
141
142 add_action( 'manage_'. $pagelayer->builder['name'] .'_posts_custom_column' , 'pagelayer_builder_columns', 10, 2 );
143
144 add_filter( 'views_edit-' . $pagelayer->builder['name'], 'pagelayer_builder_template_tabs' );
145
146 }
147
148 // Create template builder conditions for singular by post type
149 function pagelayer_builder_singular_conditions($post_types = []){
150 $condition = [];
151
152 if(empty($post_types)){
153 $post_types = pagelayer_get_public_post_types();
154 //unset( $post_types['product'] ); // WooCommerce related
155 $post_types['attachment'] = get_post_type_object( 'attachment' )->label;
156 }
157
158 foreach ( $post_types as $post_type => $label ) {
159 //$post_type_object = get_post_type_object( $post_type );
160
161 if(array_key_exists($label, $condition)){
162 continue;
163 }
164
165 $condition[$label] = [ //TC: Need to change the name of variable
166 $post_type => array(
167 'label' => $label,
168 'check_conditions' => function ( $args = null ) use ($post_type){
169 if ( isset($args['id']) && !empty($args['id']) ) {
170 $id = (int) $args['id'];
171 if ( $id ) {
172 return is_singular($post_type) && get_queried_object_id() === $id;
173 }
174 }
175
176 return is_singular( $post_type );
177 },
178 'filter_type' => 'post',
179 'object_type' => $post_type,
180 ),
181 ];
182
183 $taxonomies = get_object_taxonomies( $post_type, 'objects' );
184 $post_taxonomies = wp_filter_object_list( $taxonomies, [
185 'public' => true,
186 'show_in_nav_menus' => true,
187 ] );
188
189 foreach ( $post_taxonomies as $slug => $object ) {
190
191 $condition[$label] += [
192 $object->name => array(
193 'label' => sprintf( __( 'In %s' ), $object->labels->singular_name ),
194 'check_conditions' => function ( $args = null ) use ($object){
195 return is_singular() && has_term( (int) $args['id'], $object->name );
196 },
197 'filter_type' => 'taxonomy',
198 'object_type' => $object->name,
199 ),
200 ];
201
202 if ( $object->hierarchical ) {
203 $condition[$label] += [
204 'in_' . $object->name . '_children' => array(
205 'label' => sprintf( __( 'In Child %s' ), $object->labels->name ),
206 'check_conditions' => function ( $args = null ) use ($object){
207 $id = (int) $args['id'];
208 if ( ! is_singular() || ! $id ) {
209 return false;
210 }
211 $child_terms = get_term_children( $id, $object->name );
212
213 return ! empty( $child_terms ) && has_term( $child_terms, $object->name );
214 },
215 'filter_type' => 'taxonomy',
216 'object_type' => $object->name,
217 ),
218 ];
219 }
220
221 }
222
223 $condition[$label] += [
224 $object->name . '_by_author' => array(
225 'label' => sprintf( __( '%s By Author' ), $label ),
226 'check_conditions' => function ( $args = null ) use ($post_type){
227 return is_singular($post_type) && get_post_field( 'post_author' ) === $args['id'];
228 },
229 'filter_type' => 'author',
230 'object_type' => $post_type . '_by_author',
231 ),
232 ];
233
234 }
235
236 return $condition;
237 }
238
239 // Create template builder conditions for archives by post type
240 function pagelayer_builder_archives_conditions($post_types = []){
241
242 $condition = [];
243
244 if(empty($post_types)){
245 $post_types = pagelayer_get_public_post_types();
246 //unset( $post_types['product'] );
247 }
248
249 foreach ( $post_types as $post_type => $label ) {
250
251 //$post_type_object = get_post_type_object( $post_type );
252 if ( ! get_post_type_archive_link( $post_type ) ) {
253 continue;
254 }
255
256 $condition[$label] = [ //TC: Need to change the name of variable
257 $post_type => array(
258 'label' => $label,
259 'check_conditions' => function ( $args = null ) use ($post_type){
260 return is_post_type_archive( $post_type ) || ( 'post' === $post_type && is_home() );
261 },
262 'no_id_section' => 1, // Id select section not required
263 ),
264 ];
265
266 $taxonomies = get_object_taxonomies( $post_type, 'objects' );
267 $post_taxonomies = wp_filter_object_list( $taxonomies, [
268 'public' => true,
269 'show_in_nav_menus' => true,
270 ] );
271
272 foreach ( $post_taxonomies as $slug => $object ) {
273
274 $condition[$label] += [
275 $object->name => array(
276 'label' => sprintf( __( '%s' ), $object->label ),
277 'check_conditions' => function ( $args = null ) use ($object){
278 $taxonomy = $object->name;
279 $id = (int) $args['id'];
280
281 if ( 'category' === $taxonomy ) {
282 return is_category( $id );
283 }
284
285 if ( 'post_tag' === $taxonomy ) {
286 return is_tag( $id );
287 }
288
289 return is_tax( $taxonomy, $id );
290 },
291 'filter_type' => 'taxonomy',
292 'object_type' => $object->name,
293 ),
294 ];
295
296 }
297
298 $all = 'pagelayer_all_'.$post_type;
299
300 $condition[$label] += [
301 $all => array(
302 'label' => 'All '.$label.' Archive',
303 'check_conditions' => function ( $args = null ) use ($post_type, $condition, $all, $label){
304
305 foreach($condition[$label] as $k => $v){
306 if($all == $k){
307 continue;
308 }
309
310 $ret = $v['check_conditions']($args);
311
312 if(!empty($ret)){
313 return $ret;
314 }
315
316 }
317
318 },
319 'no_id_section' => 1, // Id select section not required
320 ),
321 ];
322
323 }
324
325 return $condition;
326 }
327
328 // Handle the links for the add URL
329 add_action('admin_head', 'pagelayer_builder_admin_head', 9999);
330 function pagelayer_builder_admin_head(){
331 global $pagelayer, $post_type;
332
333 if(!isset($pagelayer->builder) || $post_type != $pagelayer->builder['name']){
334 return;
335 }
336
337
338 echo '<script type="text/javascript">
339
340 jQuery(document).ready(function(){
341 var but = jQuery(".page-title-action");
342
343 if(but.length < 1){
344 return;
345 }
346
347 but.attr("href", "'.esc_url(admin_url('admin.php?page=pagelayer_template_wizard')).'");
348
349 var export_but = jQuery(but[0].outerHTML);
350 export_but.html("'.__pl('export_template').'");
351 export_but.addClass("pagelayer-temp-export-but");
352 export_but.attr("href", "'.(defined('SITEPAD') ? esc_url(admin_url('admin.php?page=pagelayer_tools#export_theme_tab')) : esc_url(admin_url('admin.php?page=pagelayer_template_export'))).'");
353 //console.log(export_but[0].outerHTML);
354
355 but.after(export_but);
356 });
357
358 </script>';
359
360 }
361
362 // This function will handle the Template Builder pages in Pagelayer
363 add_action('init', 'pagelayer_builder_post_type', 9999);
364 function pagelayer_builder_post_type() {
365 global $pagelayer;
366
367 if(!isset($pagelayer->builder)){
368 return;
369 }
370
371 // Add Template Post Supports
372 $supports = array(
373 'title', // post title
374 'editor', // post content
375 'author', // post author
376 'custom-fields', // custom fields
377 'revisions', // post revisions
378 );
379
380 // Add Template Post lables
381 $labels = array(
382 'name' => _x('Templates', 'plural'),
383 'singular_name' => _x('Pagelayer Template', 'singular'),
384 'menu_name' => _x('Pagelayer Templates', 'admin menu'),
385 'name_admin_bar' => _x('Pagelayer Templates', 'admin bar'),
386 'add_new' => _x('Add New', 'add templates'),
387 'add_new_item' => __('Add New'),
388 'new_item' => __('New Template'),
389 'edit_item' => __('Edit Template'),
390 'view_item' => __('View Template'),
391 'all_items' => __('All Templates'),
392 'search_items' => __('Search Templates'),
393 'not_found' => __('No Pagelayer templates found'),
394 );
395
396 $args = array(
397 'supports' => $supports,
398 'labels' => $labels,
399 'menu_icon' => PAGELAYER_URL.'/images/pagelayer-logo-19.png',
400 'public' => true,
401 'show_in_menu' => false,
402 'publicly_queryable' => true,
403 'query_var' => true,
404 'rewrite' => false,
405 'has_archive' => true,
406 'hierarchical' => false,
407 'exclude_from_search' => true,
408 );
409
410 // Register custom post type
411 register_post_type($pagelayer->builder['name'] , $args);
412
413 // Add any other templates as well
414 $pagelayer->builder['archives_templates'] += pagelayer_builder_archives_conditions();
415 $pagelayer->builder['singular_templates'] += pagelayer_builder_singular_conditions();
416
417 }
418
419 // Register shortcode for custom post type
420 function pagelayer_builder_posts_shortcode($atts){
421
422 global $pagelayer;
423
424 // If id not exist
425 if( !isset($atts['id']) ){return '';}
426
427 // Get the post data
428 $post = get_post((int) $atts['id']);
429
430 // If post is not empty
431 if(empty($post) || $post->post_type != 'pagelayer-template' || ($post->post_status === 'private' && !current_user_can('read_private_posts'))){
432 return '';
433 }
434
435 $cached_editable = $pagelayer->dont_make_editable;
436 $pagelayer->dont_make_editable = true;
437 $content = $post->post_content;
438 $content = apply_filters( 'the_content', $content );
439 $pagelayer->dont_make_editable = $cached_editable;
440
441 return $content;
442
443 }
444
445 // Handle edit link of the post type
446 add_filter('get_edit_post_link', 'pagelayer_builder_edit_link', 1000, 3);
447 function pagelayer_builder_edit_link($link, $postID, $context){
448 global $pagelayer;
449
450 $post = get_post($postID);
451
452 if($post->post_type != $pagelayer->builder['name']){
453 return $link;
454 }
455
456 return admin_url('admin.php?page=pagelayer_template_wizard&post='.$post->ID);
457
458 }
459
460 // Handle row actions for pagelayer-template
461 add_filter('post_row_actions', 'pagelayer_builder_row_actions', 10, 2);
462 function pagelayer_builder_row_actions($actions, $post){
463 global $pagelayer;
464
465 if($post->post_type != $pagelayer->builder['name']){
466 return $actions;
467 }
468
469 $actions['pagelayer'] = '<a href="'.esc_url( pagelayer_shortlink($post->ID).'&pagelayer-live=1' ).'">'.__pl('edit_using').'</a>';
470
471 return $actions;
472 }
473
474 // Add the custom columns to the pagelayer-template post type:
475 function pagelayer_builder_add_columns($columns) {
476
477 // unset for re-setup
478 unset( $columns['taxonomy-pagelayer_template_type'] );
479
480 $offset = 2;
481 $type = array(
482 'pagelayer_template_type' => __( 'Type' ),
483 'pagelayer_template_conditions' => __( 'Display On' ),
484 'pagelayer_template_shortcode' => __( 'Shortcode' )
485 );
486
487 // Add the $type array in 2 position of $columns;
488 $columns = array_slice( $columns, 0, $offset, true ) + $type + array_slice( $columns, $offset, null, true );
489
490 //print_r($columns);die();
491
492 return $columns;
493
494 }
495
496 // Add the data to the custom columns for the pagelayer-template post type:
497 function pagelayer_builder_columns( $column, $post_id ) {
498 global $pagelayer;
499
500 switch ( $column ) {
501 case 'pagelayer_template_type' :
502 $type = get_post_meta( $post_id, 'pagelayer_template_type', true );
503
504 if(!empty($type)){
505 $type = pagelayer_multi_array_search($pagelayer->builder['type'], $type);
506
507 if(is_array($type) && array_key_exists('label', $type)){
508 $type = $type['label'];
509 }
510
511 echo '<span>' . $type . '</span>';
512 }else{
513 echo __('None');
514 }
515
516 break;
517
518 case 'pagelayer_template_conditions' :
519 $dis_conditions = get_post_meta( $post_id, 'pagelayer_template_conditions', true );
520 if( !empty($dis_conditions) && is_array($dis_conditions)){
521 $dis_html = '';
522 foreach($dis_conditions as $condi){
523 $dis_html .= '<span>';
524
525 if(isset($condi['template'])){
526 $template = pagelayer_multi_array_search($pagelayer->builder['dispay_on'], $condi['template']);
527
528 if(is_array($template) && array_key_exists('label', $template)){
529 $template = $template['label'];
530 }
531
532 $dis_html .= $template;
533 }
534
535 if(isset($condi['sub_template'])){
536 $sub_template = pagelayer_multi_array_search($pagelayer->builder[$condi['template'].'_templates'], $condi['sub_template']);
537
538 if(is_array($sub_template) && array_key_exists('label', $sub_template)){
539 $sub_template = $sub_template['label'];
540 }
541
542 if(!empty($sub_template)){
543 $dis_html .= ' > '. $sub_template;
544 }
545 }
546
547 if(!empty($condi['id'])){
548 $dis_html .= ' > #'. $condi['id'];
549 }
550
551 $dis_html .= '</span></br>';
552 }
553 }
554
555 if(!empty($dis_html)){
556 echo $dis_html;
557 }else{
558 echo __('None');
559 }
560
561 break;
562
563 case 'pagelayer_template_shortcode' :
564
565 echo '[pagelayer-template id="'.$post_id.'"]';
566
567 break;
568
569 }
570 }
571
572 // Print template tabs.
573 function pagelayer_builder_template_tabs( $views ) {
574 global $pagelayer;
575
576 $current_type = '';
577 $active_class = ' nav-tab-active';
578
579 if ( ! empty( $_REQUEST[$pagelayer->builder['type_slug']] ) ) {
580 $current_type = $_REQUEST[$pagelayer->builder['type_slug']];
581 $active_class = '';
582 }
583
584 $url_args = [
585 'post_type' => $pagelayer->builder['name'],
586 ];
587
588 $baseurl = add_query_arg( $url_args, admin_url( 'edit.php' ) );
589
590 $template_types = $pagelayer->builder['type'];
591
592 if ( 1 >= count( $template_types ) ) {
593 return $views;
594 }
595
596 // If any pre define title
597 $all_title = '';
598 if ( ! $all_title ) {
599 $all_title = __( 'All' );
600 }
601
602 echo '<div id="pagelayer-template-tabs-wrapper" class="nav-tab-wrapper" style="margin-bottom:15px">
603 <a class="nav-tab'. $active_class .'" href="'. $baseurl .'"> '. $all_title .'</a>';
604
605 foreach ( $template_types as $type => $class_name ) {
606 $active_class = '';
607
608 // If type is empty
609 if( empty($type) ) continue;
610
611 if ( $current_type === $type ) {
612 $active_class = ' nav-tab-active';
613 }
614
615 $type_url = add_query_arg( $pagelayer->builder['type_slug'], $type, $baseurl );
616 $type_label = is_array($class_name) && array_key_exists( 'label', $class_name) ? $class_name['label'] : $class_name;
617
618 echo "<a class='nav-tab{$active_class}' href='{$type_url}'>{$type_label}</a>";
619 }
620
621 echo '</div>';
622
623 return $views;
624 }
625
626 // Add filter for pagelayer template
627 add_action( 'parse_query', 'pagelayer_builder_query_filter_types' );
628 function pagelayer_builder_query_filter_types( \WP_Query $query ) {
629 global $pagenow, $typenow, $pagelayer;
630
631 if ( ! ('edit.php' === $pagenow && $pagelayer->builder['name'] === $typenow) || ! empty( $query->query_vars['meta_key'] ) ) {
632 return;
633 }
634
635 if ( empty($_REQUEST[$pagelayer->builder['type_slug']]) ){
636 return;
637 }
638
639 $current_tab = $_REQUEST[$pagelayer->builder['type_slug']];
640
641 $template_types = $pagelayer->builder['type'];
642
643 if( !array_key_exists($current_tab , $template_types )){
644 return;
645 }
646
647 $query->query_vars['meta_key'] = 'pagelayer_template_type';
648 $query->query_vars['meta_value'] = $current_tab;
649 }
650
651 // Add the button for choose template type - Remove
652 add_action('edit_form_after_title', 'pagelayer_builder_editor_button', 11);
653 function pagelayer_builder_editor_button(){
654
655 global $pagelayer, $post;
656
657 if($post->post_type != $pagelayer->builder['name']){
658 return;
659 }
660
661 // Get the current screen
662 $current_screen = get_current_screen();
663
664 // For gutenberg - Add the code in the footer
665 if(method_exists($current_screen, 'is_block_editor') && $current_screen->is_block_editor()){
666 add_action('admin_footer', 'pagelayer_builder_gutenberg');
667 return;
668 }
669
670 echo '
671 <div style="margin-top:15px;display:inline-block;">
672 <a href="'.esc_url(admin_url('admin.php?page=pagelayer_template_wizard&post='.$post->ID)).'" class="button button-primary button-large" style="height:auto; padding:6px; font-size:15px;">
673 <img src="'.PAGELAYER_URL.'/images/pagelayer-logo-40.png" align="top" width="24" /> <span>'.__('Edit Template Options').'</span>
674 </a>
675 </div>';
676
677 }
678
679 // For gutenberg editor
680 function pagelayer_builder_gutenberg(){
681
682 global $pagelayer, $post;
683
684 echo '
685 <div id="pagelayer-editor-template-edit" style="margin-left:15px; display:none">
686 <a href="'.esc_url(admin_url('admin.php?page=pagelayer_template_wizard&post='.$post->ID)).'" class="button button-primary button-large" style="height:auto; padding:6px; font-size:15px;">
687 <img src="'.PAGELAYER_URL.'/images/pagelayer-logo-40.png" align="top" width="24" /> <span>'.__('Edit Template Options').'</span>
688 </a>
689 </div>
690
691 <script type="text/javascript">
692 jQuery(document).ready(function(){
693
694 var pagelayer_timer;
695 var pagelayer_button = function(){
696 var button = jQuery("#pagelayer-editor-template-edit");
697 var g = jQuery(".edit-post-header-toolbar");
698 if(g.length < 1){
699 return;
700 }
701 button.detach();
702 //console.log(button);
703 g.append(button);
704 button.show();
705 clearInterval(pagelayer_timer);
706 }
707 pagelayer_timer = setInterval(pagelayer_button, 100);
708 });
709 </script>';
710 }
711
712 // Load all templates
713 function pagelayer_builder_load_templates(){
714
715 global $pagelayer;
716
717 // Load all post types that are pagelayer-template
718 $args = [
719 'post_type' => $pagelayer->builder['name'],
720 'status' => 'publish',
721 'meta_key' => 'pagelayer_template_conditions',
722 'posts_per_page' => -1 // For get all posts
723 ];
724
725 $query = new WP_Query($args);
726 //print_r($query->posts);die();
727
728 // Cache the same
729 $pagelayer->templates = $query->posts;
730
731 }
732
733 // Load all our templates
734 add_action( 'template_redirect', 'pagelayer_builder_template_redirect');
735 function pagelayer_builder_template_redirect(){
736
737 global $pagelayer, $post;
738
739 // Load all post types that are pagelayer-template
740 pagelayer_builder_load_templates();
741
742 // If there is a match for a header
743 $pagelayer->template_header = pagelayer_builder_try_to_apply('header');
744
745 // Singular style posts
746 if ( is_singular() || is_404() ) {
747 $pagelayer->template_post = pagelayer_builder_try_to_apply('single');
748
749 // Archive style posts
750 } elseif ( is_archive() || is_home() || is_search() ) {
751 $pagelayer->template_post = pagelayer_builder_try_to_apply('archive');
752 }
753
754 // If there is a match for a footer
755 $pagelayer->template_footer = pagelayer_builder_try_to_apply('footer');
756
757 // If the post type is pagelayer-template, then we are viewing i.e. EDITING PAGELAYER
758 if(!empty($post) && $post->post_type == $pagelayer->builder['name']){
759
760 // Turn on template editor and default it to pagelayer-content
761 $pagelayer->template_editor = 'pagelayer-content';
762 $pagelayer->template_post = $post->ID;
763
764 // The type
765 $pagelayer_template_type = get_post_meta($post->ID, 'pagelayer_template_type', true);
766
767 // If the type is header
768 if( $pagelayer_template_type == 'header' ){
769
770 $pagelayer->template_editor = 'pagelayer-header';
771 $pagelayer->template_header = $post->ID;// Fill in that we are rendering the header we want to edit
772 $pagelayer->template_post = 0;
773 $pagelayer->template_footer = 0;
774
775 // If the type is footer
776 }elseif( $pagelayer_template_type == 'footer' ){
777
778 $pagelayer->template_editor = 'pagelayer-footer';
779 $pagelayer->template_header = 0;
780 $pagelayer->template_post = 0;
781 $pagelayer->template_footer = $post->ID;// Fill in that we are rendering the footer we want to edit
782
783 }
784
785 }
786
787 // If there is a match for a popup, get all ids with priority
788 $pagelayer->template_popup_ids = pagelayer_builder_try_to_apply('popup', true);
789
790 // Return all the posts
791 //return $query['posts'];
792
793 // Remove the filter first because it was added by
794 remove_filter('template_include', 'pagelayer_template_include', 1000);
795
796 // Add the filter again
797 add_filter('template_include', 'pagelayer_template_include', 1000, 1);
798
799 do_action('pagelayer_builder_template_redirect');
800
801 }
802
803 // For check which template will be applied
804 function pagelayer_builder_try_to_apply($type , $return_all = false){
805
806 global $pagelayer;
807
808 // Get templates id by type
809 $ids = pagelayer_builder_template_ids($type);
810 $sel_id = pagelayer_template_check_conditons($ids, false, $return_all);
811
812 if( !empty($ids) && !empty($sel_id) ){
813 return $sel_id;
814 }
815
816 return false;
817
818 }
819
820 // Get template post ids and conditions by type
821 function pagelayer_builder_template_ids($type){
822
823 global $pagelayer;
824
825 // No templates
826 if(empty($pagelayer->templates)){
827 return [];
828 }
829
830 // List of templates to return
831 $id_list = array();
832 foreach($pagelayer->templates as $template){
833
834 // The type
835 $pagelayer_template_type = get_post_meta($template->ID, 'pagelayer_template_type', true);
836
837 if($type == $pagelayer_template_type){
838 $id_list[] = $template->ID;
839 }
840
841 }
842
843 return $id_list;
844 }
845
846 add_action('plugins_loaded', 'pagelayer_free_templ_wizards', 1);
847 function pagelayer_free_templ_wizards(){
848 // Its Free
849 if(!defined('PAGELAYER_PREMIUM')){
850
851 // Wizard to create / edit templates
852 function pagelayer_builder_template_wizard(){
853 pagelayer_show_pro_div('Theme Template Creator', 'With the Pagelayer Theme Templates wizard you can create Headers, Footers, Singular, Archives and WooCommerce Templates. It allows you to control each and every aspect of your theme. You can also set conditions for these templates !');
854 }
855
856 // Wizard to export theme
857 function pagelayer_builder_export(){
858 pagelayer_show_pro_div('Export Templates', 'With the Pagelayer Export wizard you can export your Headers, Footers, Singular, Archives and WooCommerce Templates. These templates can then be imported in any other WordPress site.');
859 }
860
861 }
862 }
863
864 // Fix the default blog template we insert for woocommerce
865 function pagelayer_template_product_fix(){
866
867 global $pagelayer;
868
869 // Have we already fixed
870 $fixed = get_option('pagelayer_template_product_fix');
871 if(!empty($fixed)){
872 return;
873 }
874
875 // Make sure we have templates
876 pagelayer_builder_load_templates();
877
878
879 if(empty($pagelayer->templates)){
880 return;
881 }
882
883 foreach($pagelayer->templates as $k => $v){
884 if($v->post_name == 'blog-template'){
885 $blog = $v;
886 }
887 }
888
889 if(empty($blog)){
890 return;
891 }
892
893 // Get meta
894 $dis_conditions = get_post_meta( $blog->ID, 'pagelayer_template_conditions', true );
895 //print_r($dis_conditions);
896
897 foreach($dis_conditions as $k => $v){
898 if(!empty($v['sub_template']) && $v['sub_template'] == 'pagelayer_all_product'){
899 $found = 1;
900 }
901 }
902 //echo $found;return;
903
904 // We need to add conditions
905 if(empty($found)){
906
907 $dis_conditions[] = ['type' => 'exclude',
908 'template' => 'archives',
909 'sub_template' => 'pagelayer_all_product',
910 'id' => ''];
911
912 update_post_meta( $blog->ID, 'pagelayer_template_conditions', $dis_conditions );
913
914 }
915
916 update_option('pagelayer_template_product_fix', time());
917
918 }
919
920 // Pagelayer Template Loading Mechanism
921 add_action('setup_theme', 'pagelayer_template_setup_theme', 5);
922 function pagelayer_template_setup_theme(){
923
924 global $pagelayer;
925
926 //$theme = wp_get_theme();
927 //$theme_tags = $theme->get('Tags');
928 //print_r($theme);
929 //echo $theme->get('Tags').' Get option';
930
931 $theme_dir = get_stylesheet_directory();
932 $conf = $theme_dir.'/pagelayer.conf';
933 //echo get_template_directory();
934
935 // Pagelayer based template ?
936 if(file_exists($conf)){
937
938 $pagelayer->cache['template'] = 1;
939 $pagelayer->template_conf = @json_decode(file_get_contents($conf), true);
940
941 // Not a pagelayer theme
942 }else{
943 return;
944 }
945
946 // ORDER of preference of every template
947 // 1) POST ID as per conditions - Only Premium
948 // 2) TPL file if there - Free and Premium when pagelayer.conf
949 // 3) PHP file if no Posts - Free and Premium
950
951 // Filter to finally INCLUDE and render our template
952 add_filter('template_include', 'pagelayer_template_include', 1000, 1);
953
954 }
955
956 // Handle the template files if any
957 // NOTE : This has a priority of 100 while the posts based pagelayer_builder_template_redirect has a priority of 10
958 // If there are any post based templates, then that is given priority
959 add_action( 'template_redirect', 'pagelayer_template_redirect', 100);
960 function pagelayer_template_redirect(){
961
962 global $pagelayer, $post;
963
964 // If no conf, then we dont have to do anything
965 if(empty($pagelayer->template_conf)){
966 return;
967 }
968
969 // If post template was not there, search for a header PGL file
970 // Also when we are editing, we can render header only when its a pagelayer-content edit
971 if(
972 (empty($pagelayer->template_editor) || @$pagelayer->template_editor == 'pagelayer-content')
973 && empty($pagelayer->template_header)
974 ){
975 $pagelayer->template_header = pagelayer_template_try_to_apply('header');
976 }
977
978 // If post template was not there, search for a header PGL file
979 // Also when we are editing, we cannot render the template file as post is being rendered
980 if(empty($pagelayer->template_editor) && empty($pagelayer->template_post)){
981
982 // Singular style posts
983 if ( is_singular() || is_404() ) {
984 $pagelayer->template_post = pagelayer_template_try_to_apply('single');
985
986 // Archive style posts
987 } elseif ( is_archive() || is_home() || is_search() ) {
988 $pagelayer->template_post = pagelayer_template_try_to_apply('archive');
989 }
990
991 }
992
993 // If post template was not there, search for a footer PGL file
994 // Also when we are editing, we can render footer only when its a pagelayer-content edit
995 if(
996 (empty($pagelayer->template_editor) || @$pagelayer->template_editor == 'pagelayer-content')
997 && empty($pagelayer->template_footer)
998 ){
999 $pagelayer->template_footer = pagelayer_template_try_to_apply('footer');
1000 }
1001
1002 }
1003
1004 // Is our template being rendered
1005 function pagelayer_template_include($template){
1006
1007 global $pagelayer;
1008
1009 $pagelayer_enqueue_frontend = false;
1010
1011 // If we do have a header but not the footer or we have the footer and no header,
1012 // then we need to make sure to blank the other
1013 if(!empty($pagelayer->template_header) || !empty($pagelayer->template_footer)){
1014 $pagelayer_enqueue_frontend = true;
1015
1016 // Disable AIOSEO for pagelayer post types
1017 if(!empty($GLOBALS['post']) && $GLOBALS['post']->post_type == $pagelayer->builder['name']){
1018 add_filter( 'aioseo_disable', '__return_true' );
1019 }
1020
1021 add_action('get_header', 'pagelayer_get_header');
1022 add_action('get_footer', 'pagelayer_get_footer');
1023 }
1024
1025 // Handle the sidebar settings !
1026 //add_action('get_sidebar', 'pagelayer_get_sidebar');
1027
1028 // If we do have Popup templates, then append it in body
1029 if(!empty($pagelayer->template_popup_ids) && empty($pagelayer->template_editor)){
1030 $pagelayer_enqueue_frontend = true;
1031 add_action('wp_body_open', 'pagelayer_builder_popup_append');
1032 add_action('wp_footer', 'pagelayer_builder_popup_append');
1033 }
1034
1035 // If the post being shown to the user is not a Pagelayer post, then we need to enqueue forcefully
1036 if(empty($pagelayer->cache['enqueue_frontend']) && $pagelayer_enqueue_frontend){
1037 pagelayer_enqueue_frontend(true);
1038 }
1039
1040 // Is there any post templates OR are we editing a pagelayer-template ?
1041 if(!empty($pagelayer->template_post) || !empty($pagelayer->template_editor)){
1042 $template = $pagelayer->template_post;
1043 }
1044
1045 // Its our template OR are we editing a pagelayer-template, then render it
1046 if(pathinfo($template, PATHINFO_EXTENSION) == 'pgl' || !empty($pagelayer->template_post) || !empty($pagelayer->template_editor)){
1047
1048 // We rendered from header to footer
1049 $pagelayer->from_header_to_footer = true;
1050
1051 get_header();
1052 echo '<div class="pagelayer-content">';
1053 pagelayer_template_render($template);
1054 echo '</div>';
1055
1056 // If a template needs to call the sidebar !
1057 if(!empty($pagelayer->template_call_sidebar)){
1058 get_sidebar();
1059 }
1060 get_footer();
1061
1062 return false;
1063 }
1064
1065 // Just return the original template if its not our file
1066 return $template;
1067
1068 }
1069
1070 // Expects the file to include or the POST ID
1071 function pagelayer_template_render($template){
1072
1073 global $pagelayer;
1074
1075 // $template can be blank, e.g. blank header / footer
1076 if(empty($template)){
1077 return;
1078 }
1079
1080 if(is_numeric($template)){
1081 echo pagelayer_get_post_content($template);
1082 }else{
1083 echo pagelayer_the_content(file_get_contents(get_stylesheet_directory().'/'.$template.'.pgl'));
1084 }
1085 }
1086
1087 // For check which template will be applied
1088 function pagelayer_template_try_to_apply($type){
1089
1090 global $pagelayer;
1091
1092 // Get templates id by type
1093 $ids = [];
1094
1095 // Find the templates by type
1096 foreach($pagelayer->template_conf as $k => $v){
1097 if($v['type'] == $type){
1098 $ids[] = $k;
1099 }
1100 }
1101
1102 $file = pagelayer_template_check_conditons($ids, true);
1103
1104 if( !empty($ids) && !empty($file) ){
1105 return $file;
1106 }
1107
1108 return false;
1109
1110 }
1111
1112 // Check conditions of template post ids / template files
1113 function pagelayer_template_check_conditons($ids = [], $file = false, $return_all = false){
1114
1115 global $pagelayer;
1116
1117 $selected_templs = [];
1118
1119 foreach( $ids as $id ){
1120
1121 $priority = 0;
1122 $selected_template = 0;
1123 $exclude_check = 1;
1124
1125 // File based
1126 if($file){
1127 $pagelayer_template_conditions = $pagelayer->template_conf[$id]['conditions'];
1128
1129 // Post Template based
1130 }else{
1131 $pagelayer_template_conditions = get_post_meta( $id, 'pagelayer_template_conditions', true );
1132 }
1133
1134 if( !empty($pagelayer_template_conditions) ){
1135 foreach( $pagelayer_template_conditions as $condi ){
1136
1137 $check = 0;
1138
1139 // Get template array
1140 $tmpl_array = (array) pagelayer_multi_array_search( $pagelayer->builder['dispay_on'], $condi['template'] );
1141
1142 // Get sub_template array
1143 $sub_tmpl_array = (array) pagelayer_multi_array_search( $pagelayer->builder[$condi['template'].'_templates'], $condi['sub_template']);
1144
1145 // If the condition name is general priority
1146 if(empty($condi['template'])){
1147
1148 $check = 1;
1149 $set_prio = 1; // Set General Property 1
1150
1151 // If the condition name is singular
1152 }elseif( array_key_exists('check_conditions', $tmpl_array) ){
1153
1154 // If the condition callback is false, continue the loop
1155 if( is_callable($tmpl_array['check_conditions']) ){
1156 if( empty($tmpl_array['check_conditions']($condi)) ){
1157 continue;
1158 }
1159 }elseif( empty($tmpl_array['check_conditions']) ){
1160 continue;
1161 }
1162
1163 // Check sub_template conditions
1164 if( empty($condi['sub_template']) ){
1165 $check = 1;
1166 $set_prio = 2; // Set all sub_template Property 2
1167 }elseif( array_key_exists('check_conditions', $sub_tmpl_array ) ){
1168
1169 // If the condition callback is false, continue the loop
1170 if( is_callable($sub_tmpl_array['check_conditions']) ){
1171 if( empty($sub_tmpl_array['check_conditions']($condi)) ){
1172 continue;
1173 }
1174 }elseif( empty($sub_tmpl_array['check_conditions']) ){
1175 continue;
1176 }
1177
1178 $check = 1;
1179
1180 if( !empty($condi['id']) ){
1181 $set_prio = 4; // Set id Property 4
1182 }else{
1183 $set_prio = 3;// Set sub_template Property 3
1184 // If no id section then Property
1185 if(!empty($sub_tmpl_array['no_id_section'])){ $set_prio = 4; }
1186 }
1187 }
1188 }
1189
1190 // IF is set to the exclude then
1191 if($condi['type'] == 'exclude' && $check){
1192 $exclude_check = 0;
1193 }
1194
1195 if($check){
1196 // If the template is valid for apply
1197 $selected_template = $check;
1198
1199 // Set priority
1200 if($priority < $set_prio){ $priority = $set_prio; }
1201 }
1202 }
1203 }
1204
1205 // Set priority to template id
1206 if( $selected_template && $exclude_check ){
1207 $selected_templs[$id] = $priority;
1208 }
1209 }
1210
1211 // Return all ids with priority
1212 if($return_all){
1213 return $selected_templs;
1214 }
1215
1216 $gprior = 0;
1217 $sel_id = '';
1218 foreach( $selected_templs as $id => $prior ){
1219 if($gprior <= $prior){
1220 $gprior = $prior;
1221 $sel_id = $id;
1222 }
1223 }
1224
1225 return $sel_id;
1226 }
1227
1228 // The header to substitute
1229 function pagelayer_get_header($name) {
1230
1231 global $pagelayer;
1232
1233 // Output default header always if we have a header or footer
1234 ?>
1235 <!DOCTYPE html>
1236 <html <?php language_attributes(); ?>>
1237 <head>
1238 <meta charset="<?php bloginfo( 'charset' ); ?>" />
1239 <meta name="viewport" content="width=device-width, initial-scale=1">
1240 <link rel="profile" href="https://gmpg.org/xfn/11">
1241 <?php wp_head(); ?>
1242 </head>
1243
1244 <body <?php body_class(); ?>>
1245 <?php if(function_exists('wp_body_open')) { wp_body_open(); } ?>
1246 <?php
1247
1248 // Output our content
1249 if(!empty($pagelayer->template_header)){
1250
1251 echo '
1252 <header class="pagelayer-header">';
1253
1254 // Render the content
1255 pagelayer_template_render($pagelayer->template_header);
1256
1257 echo '
1258 </header>';
1259
1260 }
1261
1262 // Avoid running wp_head hooks again
1263 remove_all_actions('wp_head');
1264
1265 $templates = [];
1266 $name = (string) $name;
1267 if ($name !== '') {
1268 $templates[] = 'header-'.$name.'.php';
1269 }
1270
1271 $templates[] = 'header.php';
1272
1273 // Since, we already outputted our header, we need to do a locate_template for the existing theme
1274 // This is because, locate_template has the 3rd param as require once, so in the get_header
1275 // the header.php will not load again
1276 ob_start();
1277 locate_template( $templates, true );
1278 ob_get_clean();
1279
1280 }
1281
1282 // The footer to load
1283 function pagelayer_get_footer($name) {
1284
1285 global $pagelayer;
1286
1287 // Output our content
1288 if(!empty($pagelayer->template_footer)){
1289
1290 echo '
1291 <footer class="pagelayer-footer">';
1292
1293 pagelayer_template_render($pagelayer->template_footer);
1294
1295 echo '
1296 </footer>';
1297
1298 }
1299
1300 // Output default footer always if we have a header or footer
1301 wp_footer();
1302 echo '</body>
1303 </html>';
1304
1305 // Avoid running wp_footer hooks again
1306 remove_all_actions( 'wp_footer' );
1307
1308 $templates = [];
1309 $name = (string) $name;
1310 if ($name !== '') {
1311 $templates[] = 'footer-'.$name.'.php';
1312 }
1313
1314 $templates[] = 'footer.php';
1315
1316 // Since, we already outputted our footer, we need to do a locate_template for the existing theme
1317 // This is because, locate_template has the 3rd param as require once, so in the get_footer
1318 // the footer.php will not load again
1319 ob_start();
1320 locate_template( $templates, true );
1321 ob_get_clean();
1322
1323 }
1324
1325 // Any sidebar to load ?
1326 function pagelayer_get_sidebar($name = '') {
1327
1328 global $pagelayer;
1329
1330 // If any of our setting has been set, then only we apply. Otherwise we return !
1331 if(is_customize_preview()){
1332 $pagelayer->settings['sidebar'] = get_option('pagelayer_sidebar');
1333 }
1334
1335 if(is_array($pagelayer->settings['sidebar'])){
1336 foreach($pagelayer->settings['sidebar'] as $k => $v){
1337 $set = 1;
1338 break;
1339 }
1340 }
1341
1342 // If no settings were saved for pagelayer, let the default persist
1343 // BUT if we are from_header_to_footer then we want default as no header and hence, we will render ours !
1344 if(empty($set) && empty($pagelayer->from_header_to_footer)){
1345 return;
1346 }
1347
1348 // Output our content
1349 if(!empty($pagelayer->template_sidebar)){
1350 pagelayer_template_render($pagelayer->template_sidebar);
1351 }
1352
1353 $templates = [];
1354 $name = (string) $name;
1355 if ($name !== '') {
1356 $templates[] = 'sidebar-'.$name.'.php';
1357 }
1358
1359 $templates[] = 'sidebar.php';
1360
1361 // Since, we already outputted our sidebar, we need to do a locate_template for the existing theme
1362 // This is because, locate_template has the 3rd param as require once, so in the get_sidebar
1363 // the sidebar.php will not load again
1364 ob_start();
1365 locate_template( $templates, true );
1366 $sidebar = ob_get_clean();
1367
1368 // Lets see what are settings are ?
1369 $set = !empty($pagelayer->settings['sidebar']) ? $pagelayer->settings['sidebar'] : [];
1370
1371 // For page
1372 if(is_page()){
1373 $enabled = isset($set['page']) ? $set['page'] : 'default';
1374
1375 // For post
1376 }elseif(is_single()){
1377 $enabled = isset($set['post']) ? $set['post'] : 'default';
1378
1379 // For Archives
1380 }elseif(is_archive() || is_home()){
1381 $enabled = isset($set['archives']) ? $set['archives'] : 'default';
1382 }
1383
1384 // Load the default
1385 if(@$enabled == 'default' || is_front_page()){
1386 $enabled = @$set['default'];
1387 }
1388
1389 // If its not right or left, then its disabled. Disabled values can be stored as "no" or "0"
1390 if(!in_array($enabled, ['left', 'right'])){
1391 return;
1392 }
1393
1394 $width = (int) (empty($set['width']) ? 20 : $set['width']);
1395
1396 if(empty($sidebar)){
1397 return;
1398 }
1399
1400 echo $sidebar;
1401
1402 ?><style>
1403 aside, #sidebar {
1404 width: <?php echo $width;?>%;
1405 float: <?php echo $enabled;?>;
1406 }
1407
1408 main, .pagelayer-content{
1409 width: <?php echo round(99 - $width);?>% !important;
1410 display: inline-block;
1411 }
1412
1413 #wp-calendar{
1414 min-width: 100%;
1415 }
1416 </style>
1417
1418 <?php
1419
1420 }
1421
1422 // Get the custom post content by id
1423 function pagelayer_get_post_content($id){
1424 global $pagelayer;
1425
1426 // Need to set post ID in Pagelayer boject, so we can get it while do shortcode
1427 $pagelayer->rendering_template_id = $id;
1428
1429 // Get the content
1430 $post = get_post($id);
1431
1432 if(is_attachment()){
1433 remove_filter( 'the_content', 'prepend_attachment' );
1434 }
1435
1436 $content = $post->post_content;
1437 pagelayer_load_shortcodes();
1438
1439 $cached_editable = $pagelayer->dont_make_editable;
1440 $pagelayer->dont_make_editable = true;
1441 $content = apply_filters( 'the_content', $content );
1442 $content = str_replace( ']]>', ']]&gt;', $content );
1443 $pagelayer->dont_make_editable = $cached_editable;
1444
1445 // Reset the id
1446 $pagelayer->rendering_template_id = 0;
1447
1448 return $content;
1449
1450 }
1451
1452 // Vars that can be used in template files
1453 function pagelayer_template_vars(){
1454
1455 $replacers['{{theme_url}}'] = get_stylesheet_directory_uri();
1456 $replacers['{{theme_images}}'] = get_stylesheet_directory_uri().'/images/';
1457 $replacers['{{themes_dir}}'] = dirname(get_stylesheet_directory_uri());
1458 $replacers['{{content_url}}'] = dirname(dirname(dirname(get_stylesheet_directory_uri())));
1459 $replacers['{{pl_site_url}}'] = home_url();
1460 $replacers['{{pl_plugin_url}}'] = PAGELAYER_URL;
1461
1462 return $replacers;
1463
1464 }