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