| 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_stylesheet_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 |
} |
| 110 |
|
| 111 |
// Is our template being rendered |
| 112 |
function pagelayer_template_include($template){ |
| 113 |
|
| 114 |
global $pagelayer; |
| 115 |
|
| 116 |
$pagelayer_enqueue_frontend = false; |
| 117 |
|
| 118 |
// If we do have a header but not the footer or we have the footer and no header, |
| 119 |
// then we need to make sure to blank the other |
| 120 |
if(!empty($pagelayer->template_header) || !empty($pagelayer->template_footer)){ |
| 121 |
$pagelayer_enqueue_frontend = true; |
| 122 |
add_action('get_header', 'pagelayer_get_header'); |
| 123 |
add_action('get_footer', 'pagelayer_get_footer'); |
| 124 |
} |
| 125 |
|
| 126 |
// If we do have Popup templates, then append it in body |
| 127 |
if(!empty($pagelayer->template_popup_ids) && empty($pagelayer->template_editor)){ |
| 128 |
$pagelayer_enqueue_frontend = true; |
| 129 |
add_action('wp_footer', 'pagelayer_builder_popup_append'); |
| 130 |
} |
| 131 |
|
| 132 |
// If the post being shown to the user is not a Pagelayer post, then we need to enqueue forcefully |
| 133 |
if(empty($pagelayer->cache['enqueue_frontend']) && $pagelayer_enqueue_frontend){ |
| 134 |
pagelayer_enqueue_frontend(true); |
| 135 |
} |
| 136 |
|
| 137 |
// Is there any post templates OR are we editing a pagelayer-template ? |
| 138 |
if(!empty($pagelayer->template_post) || !empty($pagelayer->template_editor)){ |
| 139 |
$template = $pagelayer->template_post; |
| 140 |
} |
| 141 |
|
| 142 |
// Its our template OR are we editing a pagelayer-template, then render it |
| 143 |
if(pathinfo($template, PATHINFO_EXTENSION) == 'pgl' || !empty($pagelayer->template_post) || !empty($pagelayer->template_editor)){ |
| 144 |
|
| 145 |
get_header(); |
| 146 |
echo '<div class="pagelayer-content">'; |
| 147 |
pagelayer_template_render($template); |
| 148 |
echo '</div>'; |
| 149 |
get_footer(); |
| 150 |
|
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
// Just return the original template if its not our file |
| 155 |
return $template; |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
// Expects the file to include or the POST ID |
| 160 |
function pagelayer_template_render($template){ |
| 161 |
|
| 162 |
global $pagelayer; |
| 163 |
|
| 164 |
// $template can be blank, e.g. blank header / footer |
| 165 |
if(empty($template)){ |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
if(is_numeric($template)){ |
| 170 |
echo pagelayer_get_post_content($template); |
| 171 |
}else{ |
| 172 |
echo do_shortcode(file_get_contents(get_stylesheet_directory().'/'.$template.'.pgl')); |
| 173 |
} |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
// For check which template will be applied |
| 178 |
function pagelayer_template_try_to_apply($type){ |
| 179 |
|
| 180 |
global $pagelayer; |
| 181 |
|
| 182 |
// Get templates id by type |
| 183 |
$ids = []; |
| 184 |
|
| 185 |
// Find the templates by type |
| 186 |
foreach($pagelayer->template_conf as $k => $v){ |
| 187 |
if($v['type'] == $type){ |
| 188 |
$ids[] = $k; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
$file = pagelayer_template_check_conditons($ids, true); |
| 193 |
|
| 194 |
if( !empty($ids) && !empty($file) ){ |
| 195 |
return $file; |
| 196 |
} |
| 197 |
|
| 198 |
return false; |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
// Check conditions of template post ids / template files |
| 203 |
function pagelayer_template_check_conditons($ids = [], $file = false, $return_all = false){ |
| 204 |
|
| 205 |
global $pagelayer; |
| 206 |
|
| 207 |
$selected_templs = []; |
| 208 |
|
| 209 |
foreach( $ids as $id ){ |
| 210 |
|
| 211 |
$priority = 0; |
| 212 |
$selected_template = 0; |
| 213 |
$exclude_check = 1; |
| 214 |
|
| 215 |
// File based |
| 216 |
if($file){ |
| 217 |
$pagelayer_template_conditions = $pagelayer->template_conf[$id]['conditions']; |
| 218 |
|
| 219 |
// Post Template based |
| 220 |
}else{ |
| 221 |
$pagelayer_template_conditions = get_post_meta( $id, 'pagelayer_template_conditions', true ); |
| 222 |
} |
| 223 |
|
| 224 |
if( !empty($pagelayer_template_conditions) ){ |
| 225 |
foreach( $pagelayer_template_conditions as $condi ){ |
| 226 |
|
| 227 |
$check = 0; |
| 228 |
|
| 229 |
// Get template array |
| 230 |
$tmpl_array = pagelayer_multi_array_search( $pagelayer->builder['dispay_on'], $condi['template'] ); |
| 231 |
|
| 232 |
// Get sub_template array |
| 233 |
$sub_tmpl_array = pagelayer_multi_array_search( $pagelayer->builder[$condi['template'].'_templates'], $condi['sub_template']); |
| 234 |
|
| 235 |
// If the condition name is general priority |
| 236 |
if(empty($condi['template'])){ |
| 237 |
|
| 238 |
$check = 1; |
| 239 |
$set_prio = 1; // Set General Property 1 |
| 240 |
|
| 241 |
// If the condition name is singular |
| 242 |
}elseif( array_key_exists('check_conditions', $tmpl_array) ){ |
| 243 |
|
| 244 |
// If the condition callback is false, continue the loop |
| 245 |
if( is_callable($tmpl_array['check_conditions']) ){ |
| 246 |
if( empty($tmpl_array['check_conditions']($condi)) ){ |
| 247 |
continue; |
| 248 |
} |
| 249 |
}elseif( empty($tmpl_array['check_conditions']) ){ |
| 250 |
continue; |
| 251 |
} |
| 252 |
|
| 253 |
// Check sub_template conditions |
| 254 |
if( empty($condi['sub_template']) ){ |
| 255 |
$check = 1; |
| 256 |
$set_prio = 2; // Set all sub_template Property 2 |
| 257 |
}elseif( array_key_exists('check_conditions', $sub_tmpl_array ) ){ |
| 258 |
|
| 259 |
// If the condition callback is false, continue the loop |
| 260 |
if( is_callable($sub_tmpl_array['check_conditions']) ){ |
| 261 |
if( empty($sub_tmpl_array['check_conditions']($condi)) ){ |
| 262 |
continue; |
| 263 |
} |
| 264 |
}elseif( empty($sub_tmpl_array['check_conditions']) ){ |
| 265 |
continue; |
| 266 |
} |
| 267 |
|
| 268 |
$check = 1; |
| 269 |
|
| 270 |
if( !empty($condi['id']) ){ |
| 271 |
$set_prio = 4; // Set id Property 4 |
| 272 |
}else{ |
| 273 |
$set_prio = 3;// Set sub_template Property 3 |
| 274 |
// If no id section then Property |
| 275 |
if($sub_tmpl_array['no_id_section']){ $set_prio = 4; } |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
// IF is set to the exclude then |
| 281 |
if($condi['type'] == 'exclude' && $check){ |
| 282 |
$exclude_check = 0; |
| 283 |
} |
| 284 |
|
| 285 |
if($check){ |
| 286 |
// If the template is valid for apply |
| 287 |
$selected_template = $check; |
| 288 |
|
| 289 |
// Set priority |
| 290 |
if($priority < $set_prio){ $priority = $set_prio; } |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
// Set priority to template id |
| 296 |
if( $selected_template && $exclude_check ){ |
| 297 |
$selected_templs[$id] = $priority; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
// Return all ids with priority |
| 302 |
if($return_all){ |
| 303 |
return $selected_templs; |
| 304 |
} |
| 305 |
|
| 306 |
$gprior = 0; |
| 307 |
$sel_id = ''; |
| 308 |
foreach( $selected_templs as $id => $prior ){ |
| 309 |
if($gprior <= $prior){ |
| 310 |
$gprior = $prior; |
| 311 |
$sel_id = $id; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
return $sel_id; |
| 316 |
} |
| 317 |
|
| 318 |
// The header to substitute |
| 319 |
function pagelayer_get_header($name) { |
| 320 |
|
| 321 |
global $pagelayer; |
| 322 |
|
| 323 |
// Output default header always if we have a header or footer |
| 324 |
?> |
| 325 |
<!DOCTYPE html> |
| 326 |
<html <?php language_attributes(); ?>> |
| 327 |
<head> |
| 328 |
<meta charset="<?php bloginfo( 'charset' ); ?>" /> |
| 329 |
<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 330 |
<link rel="profile" href="https://gmpg.org/xfn/11"> |
| 331 |
<?php wp_head(); ?> |
| 332 |
</head> |
| 333 |
|
| 334 |
<body <?php body_class(); ?>> |
| 335 |
<?php |
| 336 |
|
| 337 |
// Output our content |
| 338 |
if(!empty($pagelayer->template_header)){ |
| 339 |
|
| 340 |
echo ' |
| 341 |
<header class="pagelayer-header">'; |
| 342 |
|
| 343 |
// Render the content |
| 344 |
pagelayer_template_render($pagelayer->template_header); |
| 345 |
|
| 346 |
echo ' |
| 347 |
</header>'; |
| 348 |
|
| 349 |
} |
| 350 |
|
| 351 |
// Avoid running wp_head hooks again |
| 352 |
remove_all_actions('wp_head'); |
| 353 |
|
| 354 |
$templates = []; |
| 355 |
$name = (string) $name; |
| 356 |
if ($name !== '') { |
| 357 |
$templates[] = 'header-'.$name.'.php'; |
| 358 |
} |
| 359 |
|
| 360 |
$templates[] = 'header.php'; |
| 361 |
|
| 362 |
// Since, we already outputted our header, we need to do a locate_template for the existing theme |
| 363 |
// This is because, locate_template has the 3rd param as require once, so in the get_header |
| 364 |
// the header.php will not load again |
| 365 |
ob_start(); |
| 366 |
locate_template( $templates, true ); |
| 367 |
ob_get_clean(); |
| 368 |
|
| 369 |
} |
| 370 |
|
| 371 |
// The footer to load |
| 372 |
function pagelayer_get_footer($name) { |
| 373 |
|
| 374 |
global $pagelayer; |
| 375 |
|
| 376 |
// Output our content |
| 377 |
if(!empty($pagelayer->template_footer)){ |
| 378 |
|
| 379 |
echo ' |
| 380 |
<footer class="pagelayer-footer">'; |
| 381 |
|
| 382 |
pagelayer_template_render($pagelayer->template_footer); |
| 383 |
|
| 384 |
echo ' |
| 385 |
</footer>'; |
| 386 |
|
| 387 |
} |
| 388 |
|
| 389 |
// Output default footer always if we have a header or footer |
| 390 |
wp_footer(); |
| 391 |
echo '</body> |
| 392 |
</html>'; |
| 393 |
|
| 394 |
// Avoid running wp_footer hooks again |
| 395 |
remove_all_actions( 'wp_footer' ); |
| 396 |
|
| 397 |
|
| 398 |
$templates = []; |
| 399 |
$name = (string) $name; |
| 400 |
if ($name !== '') { |
| 401 |
$templates[] = 'footer-'.$name.'.php'; |
| 402 |
} |
| 403 |
|
| 404 |
$templates[] = 'footer.php'; |
| 405 |
|
| 406 |
// Since, we already outputted our footer, we need to do a locate_template for the existing theme |
| 407 |
// This is because, locate_template has the 3rd param as require once, so in the get_header |
| 408 |
// the footer.php will not load again |
| 409 |
ob_start(); |
| 410 |
locate_template( $templates, true ); |
| 411 |
ob_get_clean(); |
| 412 |
|
| 413 |
} |
| 414 |
|
| 415 |
// Any sidebar to load ? |
| 416 |
function pagelayer_get_sidebar($name) { |
| 417 |
|
| 418 |
global $pagelayer; |
| 419 |
|
| 420 |
// Output our content |
| 421 |
if(!empty($pagelayer->template_sidebar)){ |
| 422 |
pagelayer_template_render($pagelayer->template_sidebar); |
| 423 |
} |
| 424 |
|
| 425 |
$templates = []; |
| 426 |
$name = (string) $name; |
| 427 |
if ($name !== '') { |
| 428 |
$templates[] = 'sidebar-'.$name.'.php'; |
| 429 |
} |
| 430 |
|
| 431 |
$templates[] = 'sidebar.php'; |
| 432 |
|
| 433 |
// Since, we already outputted our sidebar, we need to do a locate_template for the existing theme |
| 434 |
// This is because, locate_template has the 3rd param as require once, so in the get_header |
| 435 |
// the sidebar.php will not load again |
| 436 |
ob_start(); |
| 437 |
locate_template( $templates, true ); |
| 438 |
ob_get_clean(); |
| 439 |
|
| 440 |
} |
| 441 |
|
| 442 |
// Get the custom post content by id |
| 443 |
function pagelayer_get_post_content($id){ |
| 444 |
global $pagelayer; |
| 445 |
|
| 446 |
// Get the content |
| 447 |
$post = get_post($id); |
| 448 |
|
| 449 |
$content = $post->post_content; |
| 450 |
pagelayer_load_shortcodes(); |
| 451 |
//$content = do_shortcode( $content ); |
| 452 |
$content = apply_filters( 'the_content', $content ); |
| 453 |
$content = str_replace( ']]>', ']]>', $content ); |
| 454 |
|
| 455 |
return $content; |
| 456 |
|
| 457 |
} |
| 458 |
|
| 459 |
// Vars that can be used in template files |
| 460 |
function pagelayer_template_vars(){ |
| 461 |
|
| 462 |
$replacers['{{theme_url}}'] = get_stylesheet_directory_uri(); |
| 463 |
$replacers['{{theme_images}}'] = get_stylesheet_directory_uri().'/images/'; |
| 464 |
$replacers['{{themes_dir}}'] = dirname(get_stylesheet_directory_uri()); |
| 465 |
$replacers['{{pl_site_url}}'] = home_url(); |
| 466 |
|
| 467 |
return $replacers; |
| 468 |
|
| 469 |
} |