';
}
// Load all templates
function pagelayer_builder_load_templates(){
global $pagelayer;
// Load all post types that are pagelayer-template
$args = [
'post_type' => $pagelayer->builder['name'],
'status' => 'publish',
'meta_key' => 'pagelayer_template_conditions',
'posts_per_page' => -1 // For get all posts
];
$query = new WP_Query($args);
//print_r($query->posts);die();
// Cache the same
$pagelayer->templates = $query->posts;
}
// Load all our templates
add_action( 'template_redirect', 'pagelayer_builder_template_redirect');
function pagelayer_builder_template_redirect(){
global $pagelayer, $post;
// Load all post types that are pagelayer-template
pagelayer_builder_load_templates();
// If there is a match for a header
$pagelayer->template_header = pagelayer_builder_try_to_apply('header');
// Singular style posts
if ( is_singular() || is_404() ) {
$pagelayer->template_post = pagelayer_builder_try_to_apply('single');
// Archive style posts
} elseif ( is_archive() || is_home() || is_search() ) {
$pagelayer->template_post = pagelayer_builder_try_to_apply('archive');
}
// If there is a match for a footer
$pagelayer->template_footer = pagelayer_builder_try_to_apply('footer');
// If the post type is pagelayer-template, then we are viewing i.e. EDITING PAGELAYER
if(!empty($post) && $post->post_type == $pagelayer->builder['name']){
// Turn on template editor and default it to pagelayer-content
$pagelayer->template_editor = 'pagelayer-content';
$pagelayer->template_post = $post->ID;
// The type
$pagelayer_template_type = get_post_meta($post->ID, 'pagelayer_template_type', true);
// If the type is header
if( $pagelayer_template_type == 'header' ){
$pagelayer->template_editor = 'pagelayer-header';
$pagelayer->template_header = $post->ID;// Fill in that we are rendering the header we want to edit
$pagelayer->template_post = 0;
$pagelayer->template_footer = 0;
// If the type is footer
}elseif( $pagelayer_template_type == 'footer' ){
$pagelayer->template_editor = 'pagelayer-footer';
$pagelayer->template_header = 0;
$pagelayer->template_post = 0;
$pagelayer->template_footer = $post->ID;// Fill in that we are rendering the footer we want to edit
}
}
// If there is a match for a popup, get all ids with priority
$pagelayer->template_popup_ids = pagelayer_builder_try_to_apply('popup', true);
// Return all the posts
//return $query['posts'];
// Remove the filter first because it was added by
remove_filter('template_include', 'pagelayer_template_include', 1000);
// Add the filter again
add_filter('template_include', 'pagelayer_template_include', 1000, 1);
do_action('pagelayer_builder_template_redirect');
}
// For check which template will be applied
function pagelayer_builder_try_to_apply($type , $return_all = false){
global $pagelayer;
// Get templates id by type
$ids = pagelayer_builder_template_ids($type);
$sel_id = pagelayer_template_check_conditons($ids, false, $return_all);
if( !empty($ids) && !empty($sel_id) ){
return $sel_id;
}
return false;
}
// Get template post ids and conditions by type
function pagelayer_builder_template_ids($type){
global $pagelayer;
// No templates
if(empty($pagelayer->templates)){
return [];
}
// List of templates to return
$id_list = array();
foreach($pagelayer->templates as $template){
// The type
$pagelayer_template_type = get_post_meta($template->ID, 'pagelayer_template_type', true);
if($type == $pagelayer_template_type){
$id_list[] = $template->ID;
}
}
return $id_list;
}
add_action('plugins_loaded', 'pagelayer_free_templ_wizards', 1);
function pagelayer_free_templ_wizards(){
// Its Free
if(!defined('PAGELAYER_PREMIUM')){
// Wizard to create / edit templates
function pagelayer_builder_template_wizard(){
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 !');
}
// Wizard to export theme
function pagelayer_builder_export(){
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.');
}
}
}
// Fix the default blog template we insert for woocommerce
function pagelayer_template_product_fix(){
global $pagelayer;
// Have we already fixed
$fixed = get_option('pagelayer_template_product_fix');
if(!empty($fixed)){
return;
}
// Make sure we have templates
pagelayer_builder_load_templates();
if(empty($pagelayer->templates)){
return;
}
foreach($pagelayer->templates as $k => $v){
if($v->post_name == 'blog-template'){
$blog = $v;
}
}
if(empty($blog)){
return;
}
// Get meta
$dis_conditions = get_post_meta( $blog->ID, 'pagelayer_template_conditions', true );
//print_r($dis_conditions);
foreach($dis_conditions as $k => $v){
if(!empty($v['sub_template']) && $v['sub_template'] == 'pagelayer_all_product'){
$found = 1;
}
}
//echo $found;return;
// We need to add conditions
if(empty($found)){
$dis_conditions[] = ['type' => 'exclude',
'template' => 'archives',
'sub_template' => 'pagelayer_all_product',
'id' => ''];
update_post_meta( $blog->ID, 'pagelayer_template_conditions', $dis_conditions );
}
update_option('pagelayer_template_product_fix', time());
}
// Pagelayer Template Loading Mechanism
add_action('setup_theme', 'pagelayer_template_setup_theme', 5);
function pagelayer_template_setup_theme(){
global $pagelayer;
//$theme = wp_get_theme();
//$theme_tags = $theme->get('Tags');
//print_r($theme);
//echo $theme->get('Tags').' Get option';
$theme_dir = get_stylesheet_directory();
$conf = $theme_dir.'/pagelayer.conf';
//echo get_template_directory();
// Pagelayer based template ?
if(file_exists($conf)){
$pagelayer->cache['template'] = 1;
$pagelayer->template_conf = @json_decode(file_get_contents($conf), true);
// Not a pagelayer theme
}else{
return;
}
// ORDER of preference of every template
// 1) POST ID as per conditions - Only Premium
// 2) TPL file if there - Free and Premium when pagelayer.conf
// 3) PHP file if no Posts - Free and Premium
// Filter to finally INCLUDE and render our template
add_filter('template_include', 'pagelayer_template_include', 1000, 1);
}
// Handle the template files if any
// NOTE : This has a priority of 100 while the posts based pagelayer_builder_template_redirect has a priority of 10
// If there are any post based templates, then that is given priority
add_action( 'template_redirect', 'pagelayer_template_redirect', 100);
function pagelayer_template_redirect(){
global $pagelayer, $post;
// If no conf, then we dont have to do anything
if(empty($pagelayer->template_conf)){
return;
}
// If post template was not there, search for a header PGL file
// Also when we are editing, we can render header only when its a pagelayer-content edit
if(
(empty($pagelayer->template_editor) || @$pagelayer->template_editor == 'pagelayer-content')
&& empty($pagelayer->template_header)
){
$pagelayer->template_header = pagelayer_template_try_to_apply('header');
}
// If post template was not there, search for a header PGL file
// Also when we are editing, we cannot render the template file as post is being rendered
if(empty($pagelayer->template_editor) && empty($pagelayer->template_post)){
// Singular style posts
if ( is_singular() || is_404() ) {
$pagelayer->template_post = pagelayer_template_try_to_apply('single');
// Archive style posts
} elseif ( is_archive() || is_home() || is_search() ) {
$pagelayer->template_post = pagelayer_template_try_to_apply('archive');
}
}
// If post template was not there, search for a footer PGL file
// Also when we are editing, we can render footer only when its a pagelayer-content edit
if(
(empty($pagelayer->template_editor) || @$pagelayer->template_editor == 'pagelayer-content')
&& empty($pagelayer->template_footer)
){
$pagelayer->template_footer = pagelayer_template_try_to_apply('footer');
}
}
// Is our template being rendered
function pagelayer_template_include($template){
global $pagelayer;
$pagelayer_enqueue_frontend = false;
// If we do have a header but not the footer or we have the footer and no header,
// then we need to make sure to blank the other
if(!empty($pagelayer->template_header) || !empty($pagelayer->template_footer)){
$pagelayer_enqueue_frontend = true;
// Disable AIOSEO for pagelayer post types
if(!empty($GLOBALS['post']) && $GLOBALS['post']->post_type == $pagelayer->builder['name']){
add_filter( 'aioseo_disable', '__return_true' );
}
add_action('get_header', 'pagelayer_get_header');
add_action('get_footer', 'pagelayer_get_footer');
}
// Handle the sidebar settings !
//add_action('get_sidebar', 'pagelayer_get_sidebar');
// If we do have Popup templates, then append it in body
if(!empty($pagelayer->template_popup_ids) && empty($pagelayer->template_editor)){
$pagelayer_enqueue_frontend = true;
add_action('wp_body_open', 'pagelayer_builder_popup_append');
add_action('wp_footer', 'pagelayer_builder_popup_append');
}
// If the post being shown to the user is not a Pagelayer post, then we need to enqueue forcefully
if(empty($pagelayer->cache['enqueue_frontend']) && $pagelayer_enqueue_frontend){
pagelayer_enqueue_frontend(true);
}
// Is there any post templates OR are we editing a pagelayer-template ?
if(!empty($pagelayer->template_post) || !empty($pagelayer->template_editor)){
$template = $pagelayer->template_post;
}
// Its our template OR are we editing a pagelayer-template, then render it
if(pathinfo($template, PATHINFO_EXTENSION) == 'pgl' || !empty($pagelayer->template_post) || !empty($pagelayer->template_editor)){
// We rendered from header to footer
$pagelayer->from_header_to_footer = true;
get_header();
echo '
';
pagelayer_template_render($template);
echo '
';
// If a template needs to call the sidebar !
if(!empty($pagelayer->template_call_sidebar)){
get_sidebar();
}
get_footer();
return false;
}
// Just return the original template if its not our file
return $template;
}
// Expects the file to include or the POST ID
function pagelayer_template_render($template){
global $pagelayer;
// $template can be blank, e.g. blank header / footer
if(empty($template)){
return;
}
if(is_numeric($template)){
echo pagelayer_get_post_content($template);
}else{
echo pagelayer_the_content(file_get_contents(get_stylesheet_directory().'/'.$template.'.pgl'));
}
}
// For check which template will be applied
function pagelayer_template_try_to_apply($type){
global $pagelayer;
// Get templates id by type
$ids = [];
// Find the templates by type
foreach($pagelayer->template_conf as $k => $v){
if($v['type'] == $type){
$ids[] = $k;
}
}
$file = pagelayer_template_check_conditons($ids, true);
if( !empty($ids) && !empty($file) ){
return $file;
}
return false;
}
// Check conditions of template post ids / template files
function pagelayer_template_check_conditons($ids = [], $file = false, $return_all = false){
global $pagelayer;
$selected_templs = [];
foreach( $ids as $id ){
$priority = 0;
$selected_template = 0;
$exclude_check = 1;
// File based
if($file){
$pagelayer_template_conditions = $pagelayer->template_conf[$id]['conditions'];
// Post Template based
}else{
$pagelayer_template_conditions = get_post_meta( $id, 'pagelayer_template_conditions', true );
}
if( !empty($pagelayer_template_conditions) ){
foreach( $pagelayer_template_conditions as $condi ){
$check = 0;
// Get template array
$tmpl_array = (array) pagelayer_multi_array_search( $pagelayer->builder['dispay_on'], $condi['template'] );
// Get sub_template array
$sub_tmpl_array = (array) pagelayer_multi_array_search( $pagelayer->builder[$condi['template'].'_templates'], $condi['sub_template']);
// If the condition name is general priority
if(empty($condi['template'])){
$check = 1;
$set_prio = 1; // Set General Property 1
// If the condition name is singular
}elseif( array_key_exists('check_conditions', $tmpl_array) ){
// If the condition callback is false, continue the loop
if( is_callable($tmpl_array['check_conditions']) ){
if( empty($tmpl_array['check_conditions']($condi)) ){
continue;
}
}elseif( empty($tmpl_array['check_conditions']) ){
continue;
}
// Check sub_template conditions
if( empty($condi['sub_template']) ){
$check = 1;
$set_prio = 2; // Set all sub_template Property 2
}elseif( array_key_exists('check_conditions', $sub_tmpl_array ) ){
// If the condition callback is false, continue the loop
if( is_callable($sub_tmpl_array['check_conditions']) ){
if( empty($sub_tmpl_array['check_conditions']($condi)) ){
continue;
}
}elseif( empty($sub_tmpl_array['check_conditions']) ){
continue;
}
$check = 1;
if( !empty($condi['id']) ){
$set_prio = 4; // Set id Property 4
}else{
$set_prio = 3;// Set sub_template Property 3
// If no id section then Property
if(!empty($sub_tmpl_array['no_id_section'])){ $set_prio = 4; }
}
}
}
// IF is set to the exclude then
if($condi['type'] == 'exclude' && $check){
$exclude_check = 0;
}
if($check){
// If the template is valid for apply
$selected_template = $check;
// Set priority
if($priority < $set_prio){ $priority = $set_prio; }
}
}
}
// Set priority to template id
if( $selected_template && $exclude_check ){
$selected_templs[$id] = $priority;
}
}
// Return all ids with priority
if($return_all){
return $selected_templs;
}
$gprior = 0;
$sel_id = '';
foreach( $selected_templs as $id => $prior ){
if($gprior <= $prior){
$gprior = $prior;
$sel_id = $id;
}
}
return $sel_id;
}
// The header to substitute
function pagelayer_get_header($name) {
global $pagelayer;
// Output default header always if we have a header or footer
?>
>
>
template_header)){
echo '
';
// Render the content
pagelayer_template_render($pagelayer->template_header);
echo '
';
}
// Avoid running wp_head hooks again
remove_all_actions('wp_head');
$templates = [];
$name = (string) $name;
if ($name !== '') {
$templates[] = 'header-'.$name.'.php';
}
$templates[] = 'header.php';
// Since, we already outputted our header, we need to do a locate_template for the existing theme
// This is because, locate_template has the 3rd param as require once, so in the get_header
// the header.php will not load again
ob_start();
locate_template( $templates, true );
ob_get_clean();
}
// The footer to load
function pagelayer_get_footer($name) {
global $pagelayer;
// Output our content
if(!empty($pagelayer->template_footer)){
echo '
';
}
// Output default footer always if we have a header or footer
wp_footer();
echo '
';
// Avoid running wp_footer hooks again
remove_all_actions( 'wp_footer' );
$templates = [];
$name = (string) $name;
if ($name !== '') {
$templates[] = 'footer-'.$name.'.php';
}
$templates[] = 'footer.php';
// Since, we already outputted our footer, we need to do a locate_template for the existing theme
// This is because, locate_template has the 3rd param as require once, so in the get_footer
// the footer.php will not load again
ob_start();
locate_template( $templates, true );
ob_get_clean();
}
// Any sidebar to load ?
function pagelayer_get_sidebar($name = '') {
global $pagelayer;
// If any of our setting has been set, then only we apply. Otherwise we return !
if(is_customize_preview()){
$pagelayer->settings['sidebar'] = get_option('pagelayer_sidebar');
}
if(is_array($pagelayer->settings['sidebar'])){
foreach($pagelayer->settings['sidebar'] as $k => $v){
$set = 1;
break;
}
}
// If no settings were saved for pagelayer, let the default persist
// BUT if we are from_header_to_footer then we want default as no header and hence, we will render ours !
if(empty($set) && empty($pagelayer->from_header_to_footer)){
return;
}
// Output our content
if(!empty($pagelayer->template_sidebar)){
pagelayer_template_render($pagelayer->template_sidebar);
}
$templates = [];
$name = (string) $name;
if ($name !== '') {
$templates[] = 'sidebar-'.$name.'.php';
}
$templates[] = 'sidebar.php';
// Since, we already outputted our sidebar, we need to do a locate_template for the existing theme
// This is because, locate_template has the 3rd param as require once, so in the get_sidebar
// the sidebar.php will not load again
ob_start();
locate_template( $templates, true );
$sidebar = ob_get_clean();
// Lets see what are settings are ?
$set = !empty($pagelayer->settings['sidebar']) ? $pagelayer->settings['sidebar'] : [];
// For page
if(is_page()){
$enabled = isset($set['page']) ? $set['page'] : 'default';
// For post
}elseif(is_single()){
$enabled = isset($set['post']) ? $set['post'] : 'default';
// For Archives
}elseif(is_archive() || is_home()){
$enabled = isset($set['archives']) ? $set['archives'] : 'default';
}
// Load the default
if(@$enabled == 'default' || is_front_page()){
$enabled = @$set['default'];
}
// If its not right or left, then its disabled. Disabled values can be stored as "no" or "0"
if(!in_array($enabled, ['left', 'right'])){
return;
}
$width = (int) (empty($set['width']) ? 20 : $set['width']);
if(empty($sidebar)){
return;
}
echo $sidebar;
?>
rendering_template_id = $id;
// Get the content
$post = get_post($id);
if(is_attachment()){
remove_filter( 'the_content', 'prepend_attachment' );
}
$content = $post->post_content;
pagelayer_load_shortcodes();
$pagelayer->dont_make_editable = true;
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]>', $content );
$pagelayer->dont_make_editable = false;
// Reset the id
$pagelayer->rendering_template_id = 0;
return $content;
}
// Vars that can be used in template files
function pagelayer_template_vars(){
$replacers['{{theme_url}}'] = get_stylesheet_directory_uri();
$replacers['{{theme_images}}'] = get_stylesheet_directory_uri().'/images/';
$replacers['{{themes_dir}}'] = dirname(get_stylesheet_directory_uri());
$replacers['{{content_url}}'] = dirname(dirname(dirname(get_stylesheet_directory_uri())));
$replacers['{{pl_site_url}}'] = home_url();
$replacers['{{pl_plugin_url}}'] = PAGELAYER_URL;
return $replacers;
}