PluginProbe
Page Builder: Pagelayer – Drag and Drop website builder / 1.0.3
Page Builder: Pagelayer – Drag and Drop website builder v1.0.3
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 1.0.3, at main/template.php

456 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 //////////////////////////////////////////////////////////////
4 //===========================================================
5 // tampalte.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 // Pagelayer Template Loading Mechanism
28 add_action('setup_theme', 'pagelayer_template_setup_theme', 5);
29 function pagelayer_template_setup_theme(){
30
31 global $pagelayer;
32
33 //$theme = wp_get_theme();
34 //$theme_tags = $theme->get('Tags');
35 //print_r($theme);
36 //echo $theme->get('Tags').' Get option';
37
38 $theme_dir = get_template_directory();
39 $conf = $theme_dir.'/pagelayer.conf';
40 //echo get_template_directory();
41
42 // Pagelayer based template ?
43 if(file_exists($conf)){
44
45 $pagelayer->cache['template'] = 1;
46 $pagelayer->template_conf = @json_decode(file_get_contents($conf), true);
47
48 // Not a pagelayer theme
49 }else{
50 return;
51 }
52
53 // ORDER of preference of every template
54 // 1) POST ID as per conditions - Only Premium
55 // 2) TPL file if there - Free and Premium when pagelayer.conf
56 // 3) PHP file if no Posts - Free and Premium
57
58 // Filter to finally INCLUDE and render our template
59 add_filter('template_include', 'pagelayer_template_include', 1000, 1);
60
61 }
62
63 // Handle the template files if any
64 // NOTE : This has a priority of 100 while the posts based pagelayer_builder_template_redirect has a priority of 10
65 // If there are any post based templates, then that is given priority
66 add_action( 'template_redirect', 'pagelayer_template_redirect', 100);
67 function pagelayer_template_redirect(){
68
69 global $pagelayer, $post;
70
71 // If no conf, then we dont have to do anything
72 if(empty($pagelayer->template_conf)){
73 return;
74 }
75
76 // If post template was not there, search for a header PGL file
77 // Also when we are editing, we can render header only when its a pagelayer-content edit
78 if(
79 (empty($pagelayer->template_editor) || @$pagelayer->template_editor == 'pagelayer-content')
80 && empty($pagelayer->template_header)
81 ){
82 $pagelayer->template_header = pagelayer_template_try_to_apply('header');
83 }
84
85 // If post template was not there, search for a header PGL file
86 // Also when we are editing, we cannot render the template file as post is being rendered
87 if(empty($pagelayer->template_editor) && empty($pagelayer->template_post)){
88
89 // Singular style posts
90 if ( is_singular() || is_404() ) {
91 $pagelayer->template_post = pagelayer_template_try_to_apply('single');
92
93 // Archive style posts
94 } elseif ( is_archive() || is_home() || is_search() ) {
95 $pagelayer->template_post = pagelayer_template_try_to_apply('archive');
96 }
97
98 }
99
100 // If post template was not there, search for a footer PGL file
101 // Also when we are editing, we can render footer only when its a pagelayer-content edit
102 if(
103 (empty($pagelayer->template_editor) || @$pagelayer->template_editor == 'pagelayer-content')
104 && empty($pagelayer->template_footer)
105 ){
106 $pagelayer->template_footer = pagelayer_template_try_to_apply('footer');
107 }
108
109 // If we do have a header but not the footer or we have the footer and no header,
110 // then we need to make sure to blank the other
111 if(!empty($pagelayer->template_header) || !empty($pagelayer->template_footer)){
112
113 // If the post being shown to the user is not a Pagelayer post, then we need to enqueue forcefully
114 if(empty($pagelayer->cache['enqueue_frontend'])){
115 pagelayer_enqueue_frontend(true);
116 }
117
118 add_action('get_header', 'pagelayer_get_header');
119 add_action('get_footer', 'pagelayer_get_footer');
120 }
121
122 }
123
124 // Is our template being rendered
125 function pagelayer_template_include($template){
126
127 global $pagelayer;
128
129 // Is there any post templates OR are we editing a pagelayer-template ?
130 if(!empty($pagelayer->template_post) || !empty($pagelayer->template_editor)){
131 $template = $pagelayer->template_post;
132 }
133
134 // Its our template OR are we editing a pagelayer-template, then render it
135 if(pathinfo($template, PATHINFO_EXTENSION) == 'pgl' || !empty($pagelayer->template_post) || !empty($pagelayer->template_editor)){
136
137 get_header();
138 echo '<div class="pagelayer-content">';
139 pagelayer_template_render($template);
140 echo '</div>';
141 get_footer();
142
143 return false;
144 }
145
146 // Just return the original template if its not our file
147 return $template;
148
149 }
150
151 // Expects the file to include or the POST ID
152 function pagelayer_template_render($template){
153
154 global $pagelayer;
155
156 // $template can be blank, e.g. blank header / footer
157 if(empty($template)){
158 return;
159 }
160
161 if(is_numeric($template)){
162 echo pagelayer_get_post_content($template);
163 }else{
164 echo do_shortcode(file_get_contents(get_template_directory().'/'.$template.'.pgl'));
165 }
166
167 }
168
169 // For check which template will be applied
170 function pagelayer_template_try_to_apply($type){
171
172 global $pagelayer;
173
174 // Get templates id by type
175 $ids = [];
176
177 // Find the templates by type
178 foreach($pagelayer->template_conf as $k => $v){
179 if($v['type'] == $type){
180 $ids[] = $k;
181 }
182 }
183
184 $file = pagelayer_template_check_conditons($ids, true);
185
186 if( !empty($ids) && !empty($file) ){
187 return $file;
188 }
189
190 return false;
191
192 }
193
194 // Check conditions of template post ids / template files
195 function pagelayer_template_check_conditons($ids = [], $file = false){
196
197 global $pagelayer;
198
199 $selected_templs = [];
200
201 foreach( $ids as $id ){
202
203 $priority = 0;
204 $selected_template = 0;
205 $exclude_check = 1;
206
207 // File based
208 if($file){
209 $pagelayer_template_conditions = $pagelayer->template_conf[$id]['conditions'];
210
211 // Post Template based
212 }else{
213 $pagelayer_template_conditions = get_post_meta( $id, 'pagelayer_template_conditions', true );
214 }
215
216 if( !empty($pagelayer_template_conditions) ){
217 foreach( $pagelayer_template_conditions as $condi ){
218
219 $check = 0;
220
221 // Get template array
222 $tmpl_array = pagelayer_multi_array_search( $pagelayer->builder['dispay_on'], $condi['template'] );
223
224 // Get sub_template array
225 $sub_tmpl_array = pagelayer_multi_array_search( $pagelayer->builder[$condi['template'].'_templates'], $condi['sub_template']);
226
227 // If the condition name is general priority
228 if(empty($condi['template'])){
229
230 $check = 1;
231 $set_prio = 1; // Set General Property 1
232
233 // If the condition name is singular
234 }elseif( array_key_exists('check_conditions', $tmpl_array) ){
235
236 // If the condition callback is false, continue the loop
237 if( is_callable($tmpl_array['check_conditions']) ){
238 if( empty($tmpl_array['check_conditions']($condi)) ){
239 continue;
240 }
241 }elseif( empty($tmpl_array['check_conditions']) ){
242 continue;
243 }
244
245 // Check sub_template conditions
246 if( empty($condi['sub_template']) ){
247 $check = 1;
248 $set_prio = 2; // Set all sub_template Property 2
249 }elseif( array_key_exists('check_conditions', $sub_tmpl_array ) ){
250
251 // If the condition callback is false, continue the loop
252 if( is_callable($sub_tmpl_array['check_conditions']) ){
253 if( empty($sub_tmpl_array['check_conditions']($condi)) ){
254 continue;
255 }
256 }elseif( empty($sub_tmpl_array['check_conditions']) ){
257 continue;
258 }
259
260 $check = 1;
261
262 if( !empty($condi['id']) ){
263 $set_prio = 4; // Set id Property 4
264 }else{
265 $set_prio = 3;// Set sub_template Property 3
266 // If no id section then Property
267 if($sub_tmpl_array['no_id_section']){ $set_prio = 4; }
268 }
269 }
270 }
271
272 // IF is set to the exclude then
273 if($condi['type'] == 'exclude' && $check){
274 $exclude_check = 0;
275 }
276
277 if($check){
278 // If the template is valid for apply
279 $selected_template = $check;
280
281 // Set priority
282 if($priority < $set_prio){ $priority = $set_prio; }
283 }
284 }
285 }
286
287 // Set priority to template id
288 if( $selected_template && $exclude_check ){
289 $selected_templs[$id] = $priority;
290 }
291 }
292
293 $gprior = 0;
294 $sel_id = '';
295 foreach( $selected_templs as $id => $prior ){
296 if($gprior <= $prior){
297 $gprior = $prior;
298 $sel_id = $id;
299 }
300 }
301
302 return $sel_id;
303 }
304
305 // The header to substitute
306 function pagelayer_get_header($name) {
307
308 global $pagelayer;
309
310 // Output default header always if we have a header or footer
311 ?>
312 <!DOCTYPE html>
313 <html <?php language_attributes(); ?>>
314 <head>
315 <meta charset="<?php bloginfo( 'charset' ); ?>" />
316 <meta name="viewport" content="width=device-width, initial-scale=1">
317 <link rel="profile" href="https://gmpg.org/xfn/11">
318 <?php wp_head(); ?>
319 </head>
320
321 <body <?php body_class(); ?>>
322 <?php
323
324 // Output our content
325 if(!empty($pagelayer->template_header)){
326
327 echo '
328 <header class="pagelayer-header">';
329
330 // Render the content
331 pagelayer_template_render($pagelayer->template_header);
332
333 echo '
334 </header>';
335
336 }
337
338 // Avoid running wp_head hooks again
339 remove_all_actions('wp_head');
340
341 $templates = [];
342 $name = (string) $name;
343 if ($name !== '') {
344 $templates[] = 'header-'.$name.'.php';
345 }
346
347 $templates[] = 'header.php';
348
349 // Since, we already outputted our header, we need to do a locate_template for the existing theme
350 // This is because, locate_template has the 3rd param as require once, so in the get_header
351 // the header.php will not load again
352 ob_start();
353 locate_template( $templates, true );
354 ob_get_clean();
355
356 }
357
358 // The footer to load
359 function pagelayer_get_footer($name) {
360
361 global $pagelayer;
362
363 // Output our content
364 if(!empty($pagelayer->template_footer)){
365
366 echo '
367 <footer class="pagelayer-footer">';
368
369 pagelayer_template_render($pagelayer->template_footer);
370
371 echo '
372 </footer>';
373
374 }
375
376 // Output default footer always if we have a header or footer
377 wp_footer();
378 echo '</body>
379 </html>';
380
381 // Avoid running wp_footer hooks again
382 remove_all_actions( 'wp_footer' );
383
384
385 $templates = [];
386 $name = (string) $name;
387 if ($name !== '') {
388 $templates[] = 'footer-'.$name.'.php';
389 }
390
391 $templates[] = 'footer.php';
392
393 // Since, we already outputted our footer, we need to do a locate_template for the existing theme
394 // This is because, locate_template has the 3rd param as require once, so in the get_header
395 // the footer.php will not load again
396 ob_start();
397 locate_template( $templates, true );
398 ob_get_clean();
399
400 }
401
402 // Any sidebar to load ?
403 function pagelayer_get_sidebar($name) {
404
405 global $pagelayer;
406
407 // Output our content
408 if(!empty($pagelayer->template_sidebar)){
409 pagelayer_template_render($pagelayer->template_sidebar);
410 }
411
412 $templates = [];
413 $name = (string) $name;
414 if ($name !== '') {
415 $templates[] = 'sidebar-'.$name.'.php';
416 }
417
418 $templates[] = 'sidebar.php';
419
420 // Since, we already outputted our sidebar, we need to do a locate_template for the existing theme
421 // This is because, locate_template has the 3rd param as require once, so in the get_header
422 // the sidebar.php will not load again
423 ob_start();
424 locate_template( $templates, true );
425 ob_get_clean();
426
427 }
428
429 // Get the custom post content by id
430 function pagelayer_get_post_content($id){
431 global $pagelayer;
432
433 // Get the content
434 $post = get_post($id);
435
436 $content = $post->post_content;
437 pagelayer_load_shortcodes();
438 //$content = do_shortcode( $content );
439 $content = apply_filters( 'the_content', $content );
440 $content = str_replace( ']]>', ']]&gt;', $content );
441
442 return $content;
443
444 }
445
446 // Vars that can be used in template files
447 function pagelayer_template_vars(){
448
449 $replacers['{{theme_url}}'] = get_stylesheet_directory_uri();
450 $replacers['{{theme_images}}'] = get_stylesheet_directory_uri().'/images/';
451 $replacers['{{themes_dir}}'] = dirname(get_stylesheet_directory_uri());
452 $replacers['{{pl_site_url}}'] = home_url();
453
454 return $replacers;
455
456 }