/home/desid573/verdenaturopathy.com.au/site3/wp-content/plugins/pagelayer/main
Edit: /home/desid573/verdenaturopathy.com.au/site3/wp-content/plugins/pagelayer/main/import.php (45375B)
template);
}
// Have we already imported ?
$imported = get_option('pagelayer_theme_'.get_template().'_imported');
if(!empty($imported)){
$GLOBALS['pl_warn'] = __('You have already imported the content of this theme. You can re-import the same by either choosing to over-write existing pages / pagelayer templates OR creating duplicate content !', 'pagelayer');
}
// Call the theme
pagelayer_import_T();
}
function pagelayer_import_T(){
global $pagelayer, $pagelayer_theme, $pagelayer_theme_url, $pagelayer_theme_path, $pagelayer_pages, $pl_error;
pagelayer_page_header('Pagelayer - Import Template');
// Any errors ?
if(!empty($pl_error)){
pagelayer_report_error($pl_error);echo '
';
}
// Saved ?
if(!empty($GLOBALS['pl_saved'])){
echo '
'. __('The theme content was successfully imported', 'pagelayer'). '
';
// Warn ?
}elseif(!empty($GLOBALS['pl_warn'])){
echo '
';
}
// Is it a pagelayer theme ?
if(!file_exists($pagelayer_theme_path.'/pagelayer.conf')){
echo 'This utility is for importing content of the current active theme if its a Pagelayer Theme. Your current theme is
not a Pagelayer exported theme ! If you want to export your content and make it into a distributable theme, please refer to the guide
here .';
die();
}
// Home screenshot
$screenshot = $pagelayer_theme_url.'/screenshots/home.jpg';
if(!file_exists($pagelayer_theme_path.'/screenshots/home.jpg')){
$screenshot = PAGELAYER_URL.'/images/no_screenshot.png';
}
echo '
'.$pagelayer_theme->name.'
';
foreach($pagelayer_pages['page'] as $k => $v){
$screenshot = $pagelayer_theme_url.'/screenshots/'.$k.'.jpg';
if(!file_exists($pagelayer_theme_path.'/screenshots/'.$k.'.jpg')){
$screenshot = PAGELAYER_URL.'/images/no_screenshot.png';
}
echo '
';
}
echo '
';
add_filter('pagelayer_right_bar_promos', '__return_false');
pagelayer_page_footer(1);
}
// Imports the required conf
function pagelayer_import_conf(&$conf){
foreach($conf as $k => $v){
if(in_array($k, ['page_for_posts'])){
continue;
}
update_option($k, $v);
}
}
// The actual function to import the theme
function pagelayer_import_single($template_name, $items, $pagelayer_theme_path = ''){
global $wpdb, $wp_rewrite;
global $pagelayer, $pl_error;
if(empty($pagelayer_theme_path)){
$pagelayer_theme_path = get_stylesheet_directory();
}
if(empty($items)){
$pl_error[] = 'Items were not submitted';
return false;
}
/////////////////////////
// Handle the PAGES Data
/////////////////////////
// Load the new themes pages array
$data = file_get_contents($pagelayer_theme_path.'/pagelayer-data.conf');
$data = @json_decode($data, true);
//r_print($data);die();
if(empty($data['page'])){
$pl_error[] = 'Pages list not found. This is not a proper template !';
return false;
}
// Check the theme files
foreach($data['page'] as $k => $v){
$path = pagelayer_cleanpath($pagelayer_theme_path.'/data/page/'.$k);
// Does it have the title and slug ?
if(empty($v['post_title']) || empty($v['post_name'])){
$pl_error[] = 'Something is fishy with this theme as there is no title or slug for '.$k;
return false;
}
// Does the page exist ?
if(!file_exists($path) || pagelayer_cleanpath(realpath($path)) != $path){
$pl_error[] = 'Something is fishy with this theme';
return false;
}
}
$status = empty($_POST['save_as_draft']) ? 'publish' : 'draft';
// Now check the pages if it exist in this installation ?
foreach($data['page'] as $k => $v){
if(!in_array($k, $items['page'])){
continue;
}
$path = pagelayer_cleanpath($pagelayer_theme_path.'/data/page/'.$k);
// Is the page there ?
$page = get_page_by_path($v['post_name']);
//r_print($page);
$new_post = array();
// It does exist so save the revision IF its the header and footer
if(!empty($page) && isset($_POST['overwrite'])){
$rev = wp_save_post_revision($page->ID);
$new_post['ID'] = $page->ID;
}
// Make an array
$new_post['post_content'] = file_get_contents($path);
$new_post['post_title'] = $v['post_title'];
$new_post['post_name'] = $v['post_name'];
$new_post['post_type'] = 'page';
$new_post['post_status'] = $status;
//r_print($new_post);die();
// Now insert / update the post
$ret = pagelayer_insert_content($new_post, $err);
// Did we save the post ?
if(empty($ret)){
$pl_error[] = 'Could not update the page '.$v['post_name'];
return false;
}
update_post_meta($ret, 'pagelayer_imported_content', $template_name);
}
//To import typography and breakpoint
if(!empty($data['conf'])){
pagelayer_import_conf($data['conf']);
}
return true;
}
// The actual function to import the theme
function pagelayer_import_theme($template_name, $pagelayer_theme_path = ''){
global $wpdb, $wp_rewrite;
global $pagelayer, $pl_error, $sitepad;
if(empty($pagelayer_theme_path)){
$pagelayer_theme_path = get_stylesheet_directory();
}
//die($pagelayer_theme_path);
// Delete Old Data ?
if(isset($_POST['delete_old_import'])){
$args = array(
'post_type' => ['page', 'post', $pagelayer->builder['name']],
'meta_query' => array(
array(
'key' => 'pagelayer_imported_content',
'compare' => 'EXISTS'
)
)
);
$query = new WP_Query($args);
foreach ( $query->posts as $p ) {
//echo $p->ID.'
';
wp_delete_post($p->ID);
}
}
$pagelayer->import_links = [];
/////////////////////////
// Handle PAGELAYER DATA
/////////////////////////
// Load the PGL conf
$pgl = file_get_contents($pagelayer_theme_path.'/pagelayer.conf');
$pgl = @json_decode($pgl, true);
if(empty($pgl['header'])){
$pl_error[] = 'Header list not found. Report to Website Builder Team';
return false;
}
// Load the new themes pages array
$data = file_get_contents($pagelayer_theme_path.'/pagelayer-data.conf');
$data = @json_decode($data, true);
//r_print($data);die();
if(empty($data['page'])){
$pl_error[] = 'Pages list not found. This is not a proper template !';
return false;
}
// Check the theme files
foreach($pgl as $k => $v){
$path = pagelayer_cleanpath($pagelayer_theme_path.'/'.$k.'.pgl');
//print_r($path);
// Does the page exist ?
if(!file_exists($path) || (empty($GLOBALS['sitepad']['dev']) && pagelayer_cleanpath(realpath($path)) != $path)){
$pl_error[] = 'Something is fishy with this theme as the template - '.$k.' - of type - '.$v['type'].' - was not found';
return false;
}
}
// Are we to add default templates ?
if(empty($_POST['no_blog_templates'])){
add_filter('pagelayer_importing_templates', 'pagelayer_blog_templates', 10, 1);
}
///////////////////////////
// Lets import all MEDIA
///////////////////////////
// Now lets download the templates
if(!function_exists( 'list_files' ) ) {
require_once ABSPATH . PAGELAYER_CMS_DIR_PREFIX.'-admin/includes/file.php';
}
$_media = list_files($pagelayer_theme_path.'/images', 1);
//r_print($_media);die();
// Download images
if(!empty($_REQUEST['download_imgs']) && file_exists($pagelayer_theme_path.'/images.json')){
$images_list = @json_decode(file_get_contents($pagelayer_theme_path.'/images.json'), true);
//r_print($images_list);die();
foreach($images_list as $k => $v){
if(empty($v['download_url'])){
continue;
}
$dest_dir = $pagelayer_theme_path.'/images';
$dest_file = $dest_dir.'/'.$k;
$image_file = file_get_contents($v['download_url']);
// Compare image md5
if($v['md5'] != md5($image_file)){
continue;
}
if(file_exists($dest_file)){
$imagesize = getimagesize($dest_file);
// Download and resize image
$resize_file = pagelayer_resizeImage($v['download_url'], $imagesize[0], $imagesize[1]);
if(!empty($resize_file)){
$image_file = $resize_file;
}
}
// Put image in file
file_put_contents($dest_file, $image_file);
}
// Update option to set no
update_option('pagelayer_import_images_'.$template_name, 'yes');
}elseif(empty($_REQUEST['download_imgs']) && !file_exists($pagelayer_theme_path.'/images.json')){
foreach($_media as $k => $v){
$imagesize = getimagesize($v);
// Create blank image
if(strpos($imagesize['mime'], "image/" ) !== false) {
$blank_image = pagelayer_create_blank_image($imagesize[0], $imagesize[1]);
file_put_contents($v, $blank_image);
}
}
// Update option to set no
update_option('pagelayer_import_images_'.$template_name, 'no');
}
foreach($_media as $k => $v){
$file_name = basename($v);
$ret = pagelayer_upload_media($file_name, file_get_contents($v));
if(!empty($ret)){
$pagelayer->import_media['{{theme_url}}/images/'.$file_name] = $ret;
}
}
//r_print($pagelayer->import_media);die();
// If we are to import default templates
$pgl = apply_filters('pagelayer_importing_templates', $pgl);
//////////////////////
// Create Menus
//////////////////////
// Create the menu
if(empty($_POST['no_header_menu'])){
// Is there any MENU in this theme ?
if(empty($data['menus'])){
$menu_id = pagelayer_import_create_menu($template_name.' Header Menu');
}else{
foreach($data['menus'] as $k => $v){
$new_id = pagelayer_import_create_menu($v['name']);
$pagelayer->imported_menus[$v['term_id']] = $new_id;
$pagelayer->imported_menus_slug[$new_id] = $k;
}
//r_print($pagelayer->imported_menus);die();
$menu_id = current($pagelayer->imported_menus);
}
}else{
// Get the first menu that has items if we still can't find a menu.
$menus = wp_get_nav_menus();
foreach ( $menus as $menu_maybe ) {
$menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) );
if ( $menu_items ) {
$menu_id = $menu_maybe->term_id;
break;
}
}
}
// Make a array of OLD IDs => NEW IDs for replace
$pagelayer->imported_menus_preg = [];
// If we have menus !
if(!empty($pagelayer->imported_menus)){
foreach($pagelayer->imported_menus as $k => $v){
$pagelayer->imported_menus_preg['('.$k.')'] = $v;
}
// Theme didnt import menus, so lets replace with 0
}else{
$pagelayer->imported_menus_preg['(\d*)'] = $menu_id;
}
//////////////////////
// Start import
//////////////////////
// Import the Pagelayer Templates files
foreach($pgl as $k => $v){
$path = pagelayer_cleanpath($pagelayer_theme_path.'/'.$k.'.pgl');
$new_post = array();
// Is the page there ?
$template = get_page_by_path($k, OBJECT, $pagelayer->builder['name']);
// It does exist so save the revision IF its the header and footer
if(!empty($template)){
$rev = wp_save_post_revision($template->ID);
// Did we save the rev ?
if(empty($rev)){
// TODO : Throw error
}
$new_post['ID'] = $template->ID;
}
// Make an array
$new_post['post_content'] = empty($v['post_content']) ? file_get_contents($path) : $v['post_content'];
$new_post['post_title'] = $v['title'];
$new_post['post_name'] = $k;
$new_post['post_type'] = $pagelayer->builder['name'];
$new_post['post_status'] = 'publish';
$new_post['comment_status'] = 'closed';
$new_post['ping_status'] = 'closed';
//pagelayer_print($new_post);die();
// Handle Menu data
$new_post['post_content'] = pagelayer_import_handle_replaces($new_post['post_content']);
//pagelayer_print($new_post);die();
// Now insert / update the post
$ret = pagelayer_insert_content($new_post, $err);
$post_id = $ret;
$pagelayer->import_map[$k] = $ret;
$pagelayer->imported_ids[$new_post['post_type']][$new_post['post_name']] = $ret;
// Did we save the rev ?
if(empty($ret)){
$pl_error[] = 'Could not update the Pagelayer Template '.$k;
return false;
}
// Save our template type
update_post_meta($post_id, 'pagelayer_template_type', $v['type']);
update_post_meta($post_id, 'pagelayer_template_conditions', $v['conditions']);
update_post_meta($post_id, 'pagelayer_imported_content', $template_name);
// Any conditions having Page IDs that need to be updated ?
if(!empty($v['conditions'])){
foreach($v['conditions'] as $ck => $cv){
if(!empty($cv['id'])){
$conditions[$post_id][$ck] = $cv['id'];
}
}
}
}
/////////////////////////
// Handle the PAGES Data
/////////////////////////
//pagelayer_print($data);
// Import taxonomies
$taxonomy_ids = array();
if(!empty($data['taxonomies'])){
$taxonomy_ids = pagelayer_import_taxonomies($data['taxonomies']);
}
foreach($data as $data_type => $data_v){
$pagelayer->imported[$data_type] = 1;
// To import theme related settings
if($data_type == 'conf'){
pagelayer_import_conf($data['conf']);
continue;
}
if($data_type == 'menus' || $data_type == 'taxonomies'){
continue;
}
// Check the theme files
foreach($data[$data_type] as $k => $v){
$path = pagelayer_cleanpath($pagelayer_theme_path.'/data/'.$data_type.'/'.$k);
// Does it have the title and slug ?
if(empty($v['post_title']) || empty($v['post_name'])){
$pl_error[] = 'Something is fishy with this theme as there is no title or slug for '.$k;
return false;
}
// Does the file exist ?
if(!file_exists($path) || (empty($GLOBALS['sitepad']['dev']) && pagelayer_cleanpath(realpath($path)) != $path)){
$pl_error[] = 'Something is fishy with this theme';
return false;
}
}
$menu_pages = [];
// Now check the pages if it exist in this installation ?
foreach($data[$data_type] as $k => $v){
$path = pagelayer_cleanpath($pagelayer_theme_path.'/data/'.$data_type.'/'.$k);
// Is the page there ?
$page = get_page_by_path($v['post_name']);
//r_print($page);
$new_post = array();
$insert_meta = 1;
// It does exist so save the revision IF its the header and footer
if(!empty($page)){
$insert_meta = 0;
if(isset($_POST['overwrite'])){
$rev = wp_save_post_revision($page->ID);
$new_post['ID'] = $page->ID;
$insert_meta = 1;
}
}
// Make an array
$new_post['post_content'] = file_get_contents($path);
$new_post['post_excerpt'] = $v['post_excerpt'];
$new_post['post_title'] = $v['post_title'];
$new_post['post_name'] = $v['post_name'];
$new_post['post_type'] = $data_type;
$new_post['post_status'] = 'publish';
// Category register
if(!empty($v['taxonomies'])){
foreach($v['taxonomies'] as $tax => $tax_ids){
if(!empty($tax_ids)){
// Need to replace ids with new ids
$tax_ids = explode(',', $tax_ids);
foreach($tax_ids as $key => $id){
$tax_ids[$key] = (int) $taxonomy_ids[$id];
}
switch ($tax) {
case 'category':
$new_post['post_category'] = $tax_ids;
break;
case 'post_tag':
$new_post['tags_input'] = $tax_ids;
break;
default:
$new_post['tax_input'][$tax] = $tax_ids;
}
}
}
}
// Meta file path
$meta_path = pagelayer_cleanpath($pagelayer_theme_path.'/data/'.$data_type.'/'.$k.'.meta');
if($insert_meta && file_exists($meta_path)){
$meta_path = pagelayer_cleanpath($pagelayer_theme_path.'/data/'.$data_type.'/'.$k.'.meta');
$new_post['meta_input'] = file_get_contents($meta_path);
$new_post['meta_input'] = json_decode($new_post['meta_input']);
}
//r_print($new_post);die();
// Handle Menu data
$new_post['post_content'] = pagelayer_import_handle_replaces($new_post['post_content']);
// Now insert / update the post
$ret = pagelayer_insert_content($new_post, $err);
// Did we save the post ?
if(empty($ret)){
$pl_error[] = 'Could not update the '.$data_type.' '.$v['post_name'];
return false;
}
update_post_meta($ret, 'pagelayer_imported_content', $template_name);
$pagelayer->import_map[$v['ID']] = $ret;
$pagelayer->imported_ids[$new_post['post_type']][$new_post['post_name']] = $ret;
// Skip Header, Footer and Home pages
if($data_type == 'page' && preg_match('/^home/is', $new_post['post_name'])){
$home_page = $ret;
}
if(defined('SITEPAD')){
// Does the screenshot exist ?
$screenshot_file = $pagelayer_theme_path.'/screenshots/'.$v['post_name'].'.jpg';
if(file_exists($screenshot_file)){
@mkdir($sitepad['screenshots_path'], 0755, true);
@copy($screenshot_file, $sitepad['screenshots_path'].'/'.$v['post_name'].'.jpg');
}
}
}
}
// Update Post for import
if(!empty($conditions)){
foreach($conditions as $post_ID => $v){
$cond = get_post_meta($post_ID, 'pagelayer_template_conditions', 1);
foreach($v as $ck => $cv){
if(!empty($pagelayer->import_map[$cv])){
$cond[$ck]['id'] = $pagelayer->import_map[$cv];
}
}
update_post_meta($post_id, 'pagelayer_template_conditions', $cond);
}
}
// Call a function for the theme if they want to execute something like create more templates, etc
$ret = apply_filters('pagelayer_theme_imported', $template_name);
if(isset($_POST['set_home_page']) || isset($_POST['create_blog_page'])){
// Get the home page ID
$blog = get_page_by_path('blog');
// Insert the blog page
if(empty($blog)){
$new_post['post_content'] = '';
$new_post['post_title'] = 'Blog';
$new_post['post_name'] = 'blog';
$new_post['post_type'] = 'page';
$new_post['post_status'] = 'publish';
// Now insert / update the post
$blog_id = wp_insert_post($new_post);
}else{
$blog_id = $blog->ID;
}
// Set the blog page
update_option('page_for_posts', $blog_id);
}
if(!empty($data['conf']['page_for_posts'])){
$pagelayer->import_map[$data['conf']['page_for_posts']] = $blog_id;
$pagelayer->imported_ids['page']['blog'] = $blog_id;
}
// Update any links that are to be updated
if(!empty($pagelayer->import_links)){
foreach($pagelayer->import_links as $post_type => $v){
foreach($v as $slug => $link_maps){
// Lets get the post
$tmp_post = get_post($pagelayer->imported_ids[$post_type][$slug]);
foreach($link_maps as $old_link_type => $old_link_slugs){
//pagelayer_print($old_link_slugs);die();
foreach($old_link_slugs as $old_link_slug){
// Did we have such a link ?
$new_link_id = @$pagelayer->imported_ids[$old_link_type][$old_link_slug];
// If not found, lets try to find a similar post
if(empty($new_link_id)){
$args = ['name' => $old_link_slug,
'post_type' => $old_link_type];
// Make query
$query = new WP_Query($args);
// Get post
if(!empty($query->posts)){
$link_post = current($query->posts);
//echo $old_link_slug.' - ';pagelayer_print($link_post->post_name);die();
$new_link_id = @$link_post->ID;
}
}
if(empty($new_link_id)){
continue;
}
$tmp_post->post_content = str_replace('||link_id|'.$old_link_type.'|'.$old_link_slug.'||', $new_link_id, $tmp_post->post_content);
}
}
//pagelayer_print($tmp_post);
wp_update_post($tmp_post);
}
}
}
if(isset($_POST['set_home_page'])){
// Set the blog page
update_option('show_on_front', 'page');
// Set home page as the default page
if(!empty($home_page)){
update_option('page_on_front', $home_page);
}
}
// Update the menu
if(empty($_POST['no_header_menu'])){
// Are we importing from the theme ?
if(!empty($pagelayer->imported_menus)){
foreach($pagelayer->imported_menus as $k => $v){
pagelayer_import_update_menus($v, $pagelayer_theme_path);
}
// We created the menu, lets update it
}else{
pagelayer_update_header_menu($menu_id, $pagelayer->import_map);
}
}
// Save that we have imported the theme
update_option('pagelayer_theme_'.$template_name.'_imported', time(), true);
// Blank woocommerce fix
update_option('pagelayer_template_product_fix', 0);
return true;
}
// Import Taxonomies Handler
function pagelayer_import_taxonomies($taxonomy){
$new_ids = array();
foreach($taxonomy as $term_id => $term){
$parent_id = null;
$term_par = 0;
// If tern has parent
if(!empty($term['parent']) && empty($new_ids[$term['parent']])){
// Get parent taxonomy
$par_terms = get_terms( array(
'taxonomy' => $term['taxonomy'],
'hide_empty' => false,
'meta_key' => 'pagelayer_imported_id',
'meta_value' => $term['parent']
) );
$par_terms_len = count($par_terms) - 1;
// If not exists
if(is_wp_error($par_terms) || empty($par_terms)){
$par_ids = pagelayer_import_taxonomies(array($term['parent'] => $taxonomy[$term['parent']]));
$term_par = $parent_id = $par_ids[$term['parent']];
$new_ids[$term['parent']] = $parent_id;
}elseif($par_terms_len > -1){
$term_par = $parent_id = $par_terms[$par_terms_len]->term_id;
}
}elseif(!empty($new_ids[$term['parent']])){
$term_par = $parent_id = $new_ids[$term['parent']];
}
$exist_term = term_exists($term['name'], $term['taxonomy'], $parent_id );
if($exist_term === null){
$tax_details = wp_insert_term($term['name'], $term['taxonomy'] ,array('description' => $term['description'],'parent' => $term_par, 'slug' => $term['slug']));
if(!(is_wp_error( $tax_details ))){
$new_id = $tax_details['term_id'];
}
}elseif(is_array($exist_term)){
$new_id = $exist_term['term_id'];
}else{
$new_id = $exist_term;
}
// ID is empty?
if(empty($new_id)){
continue;
}
update_term_meta( $new_id, 'pagelayer_imported_id', $term_id);
$new_ids[$term_id] = $new_id;
}
return $new_ids;
}
add_filter('pagelayer_start_insert_content', 'pagelayer_import_start_insert_content');
function pagelayer_import_start_insert_content($post){
global $pagelayer;
$_post = json_encode($post);
// Does it have links ?
if(preg_match_all('/(\|\|link_id\|([\w-]*)\|([\w-]*)\|\|)/', $_post, $matches)){
foreach($matches[3] as $kk => $link){
$pagelayer->import_links[$post['post_type']][$post['post_name']][$matches[2][$kk]][] = $link;
}
//pagelayer_print($matches);pagelayer_print($pagelayer->import_links);die();
}
if(preg_match('/theme_url/is', $_post)){
$do = 1;
}
// Lets replace the images
foreach($pagelayer->import_media as $k => $v){
$_post = str_replace($k, $v, $_post);
$k = str_replace('/', '\/', $k);// Handle JSON
$_post = str_replace($k, $v, $_post);
$k = str_replace('/', '\/', addslashes($k));// Handle Doubled JSON
$_post = str_replace($k, $v, $_post);
}
$post = json_decode($_post, true);
if(!empty($do)){
//echo $_post;
//pagelayer_print($post);die();
}
return $post;
}
// Create the menu
function pagelayer_import_create_menu($name){
// Create the menu if not exists
$menu_name = (empty($name) ? 'Pagelayer Menu' : $name);
$menu_exists = wp_get_nav_menu_object($menu_name);
// If there is no menu we will need to add it
if(!empty($menu_exists)){
wp_delete_nav_menu($menu_exists);
}
// Insert the Menu
$menu_id = wp_create_nav_menu($menu_name);
//r_print($menu_exists);r_print($menu_name);r_print($menu_id);die();
if(!is_int($menu_id)){
return false;
}
// We need to DISABLE auto add TEMPORARILY
$options = (array) get_option('nav_menu_options');
if (isset($options['auto_add'])){
$key = array_search($menu_id, $options['auto_add']);
if(!empty($key)){
unset($options['auto_add'][$key]);
update_option('nav_menu_options', $options);
}
}
return $menu_id;
}
// Callback for menu replacement
function pagelayer_import_handle_replaces($content){
global $pagelayer;
// Replace the old ID structure
$content = preg_replace_callback('/pagelayer-id="(\w{16})"/s', 'pagelayer_handle_id_sc', $content);
$content = preg_replace_callback('/"pagelayer-id"\:"(\w{16})"/s', 'pagelayer_handle_id', $content);
foreach($pagelayer->imported_menus_preg as $k => $v){
$content = preg_replace('/\[pl_wp_menu ([^\]]*)nav_list="'.$k.'"([^\]]*)\]/is', '[pl_wp_menu ${1}nav_list="'.$v.'"${3}]', $content);
}
// Also for block format
$content = preg_replace_callback('/).)*+)?}\s+)?(?P
\/)?-->/s', 'pagelayer_handle_wp_menu', $content);
// Lets replace the variables for social icons
$content = preg_replace_callback('/\[pl_social ([^\]]*)\]/is', 'pagelayer_handle_social_urls', $content);
$content = preg_replace_callback('/).)*+)?}\s+)?(?P\/)?-->/s', 'pagelayer_handle_social_urls_blocks', $content);
return $content;
}
// Update the header menu
function pagelayer_update_header_menu($menu_id, $pages){
$menu_pages = [];
$home = get_option('page_on_front');
if(!empty($home)){
$menu_pages[] = $home;
}
$blog = get_option('page_for_posts');
if(!empty($blog)){
$menu_pages[] = $blog;
}
// The other links
foreach($pages as $pk => $pv){
$tmp = get_post($pv);
if(is_wp_error($tmp) || $tmp->post_type !== 'page'){
continue;
}
// Skip Header, Footer and Home pages
if(in_array($pv, $menu_pages)){
continue;
}
$menu_pages[] = $pv;
}
// Get the pages
foreach($menu_pages as $pk => $page_id){
$menu_pages[$pk] = get_post($page_id);
}
// The other links
foreach($menu_pages as $pk => $pv){
wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => $pv->post_title,
'menu-item-url' => home_url( '/'.$pv->post_name.'/' ),
'menu-item-status' => 'publish',
'menu-item-type' => 'post_type',
'menu-item-object' => 'page',
'menu-item-object-id' => $pv->ID));
}
// We need to enable auto add new pages
$options = (array) get_option('nav_menu_options');
if (!isset($options['auto_add'])){
$options['auto_add'] = array();
}
$options['auto_add'][] = $menu_id;
update_option('nav_menu_options', $options);
}
// For import of our exported menus
function pagelayer_import_update_menus($menu_id, $pagelayer_theme_path = ''){
global $pagelayer;
$old_id = array_search($menu_id, $pagelayer->imported_menus);
$slug = $pagelayer->imported_menus_slug[$menu_id];
$data = file_get_contents($pagelayer_theme_path.'/data/menus/'.$slug);
$data = @json_decode($data, true);
$ids = [];
// Insert the links
foreach($data as $k => $v){
$r = [];
$r['menu-item-title'] = $v['post']['title'];
$r['menu-item-status'] = $v['post']['post_status'];
$r['menu-item-type'] = $v['post']['type'];
$r['menu-item-object'] = $v['post']['object'];
$r['menu-item-classes'] = implode(' ', $v['post']['classes']);
// Any parent ?
if(!empty($v['post']['menu_item_parent'])){
$parent = $ids[$v['post']['menu_item_parent']];
if(!empty($parent)){
$r['menu-item-parent-id'] = $parent;
}
}
// Regular Data Object
if($r['menu-item-type'] !== 'custom'){
$r['menu-item-object-id'] = $pagelayer->import_map[$v['post']['object_id']];
if(empty($r['menu-item-object-id'])){
continue;
}
$r['menu-item-url'] = get_permalink($r['menu-item-object-id']);
// Custom URL
}else{
$r['menu-item-url'] = $v['post']['url'];
}
//r_print($r);
$ids[$v['post']['db_id']] = wp_update_nav_menu_item($menu_id, 0, $r);
}
// We need to enable auto add new pages
$options = (array) get_option('nav_menu_options');
if (!isset($options['auto_add'])){
$options['auto_add'] = array();
}
$options['auto_add'][] = $menu_id;
update_option('nav_menu_options', $options);
}
// Callback for menu replacement
function pagelayer_handle_wp_menu($matches){
global $pagelayer;
foreach($pagelayer->imported_menus_preg as $k => $v){
$matches[0] = preg_replace('/nav_list"\s*:\s*"'.$k.'"/is', 'nav_list":"'.$v.'"', $matches[0]);
}
return $matches[0];
}
// Change the old style ID to the new style
function pagelayer_handle_id($matches){
//r_print($matches);die();
$str = '"pagelayer-id":"'.pagelayer_create_id().'"';
return $str;
}
// Change the old style ID to the new style
function pagelayer_handle_id_sc($matches){
//r_print($matches);die();
$str = 'pagelayer-id="'.pagelayer_create_id().'"';
return $str;
}
// Replace Social URLs with the one given in setup
function pagelayer_handle_social_urls($matches){
//r_print($matches);die();
// Get the icon
preg_match('/icon=(\'|")([^\'"]*)(\'|")/is', $matches[0], $icon);
$icon = $icon[2];
$urls = pagelayer_get_social_urls();
foreach($urls as $k => $v){
if(preg_match('/'.preg_quote($k, '/').'/is', $icon)){
$social_url = $v;
break;
}
}
if(!empty($social_url)){
// Is the social_url param there ?
if(!preg_match('/social_url=/is', $matches[0])){
$matches[0] = substr($matches[0], 0, -1).'social_url="#"]';
}
$matches[0] = preg_replace('/social_url=(\'|")([^\'"]*)(\'|")/is', 'social_url="'.$social_url.'"', $matches[0]);
}
//r_print($matches);die();
return $matches[0];
}
// Replace Social URLs with the one given in setup
function pagelayer_handle_social_urls_blocks($matches){
// Get the icon
preg_match('/icon":"([^"]*)"/is', $matches[0], $icon);
$icon = $icon[1];
$urls = pagelayer_get_social_urls();
foreach($urls as $k => $v){
if(preg_match('/'.preg_quote($k, '/').'/is', $icon)){
$social_url = $v;
break;
}
}
if(!empty($social_url)){
// Is the social_url param there ?
if(!preg_match('/"social_url"/is', $matches[0])){
$matches[0] = preg_replace('/("icon"\s*:\s*"([^"]*)")/is', '"icon":"'.$icon.'","social_url":"#"', $matches[0]);
}
$matches[0] = preg_replace('/social_url"\s*:\s*"([^"]*)"/is', 'social_url":"'.$social_url.'"', $matches[0]);
}
return $matches[0];
}
// Add the blog templates
function pagelayer_blog_templates($pgl){
$conf = '{
"single-template": {
"type": "single",
"title": "Single Template",
"conditions": [
{
"type": "include",
"template": "singular",
"sub_template": "post",
"id": ""
},
{
"type": "include",
"template": "singular",
"sub_template": "attachment",
"id": ""
}
]
},
"blog-template": {
"type": "archive",
"title": "Blog Template",
"conditions": [
{
"type": "include",
"template": "archives",
"sub_template": "",
"id": ""
}
]
}
}';
$conf = json_decode($conf, true);
// Do we have the blog template ?
if(empty($pgl['blog-template'])){
$conf['blog-template']['post_content'] = '[pl_row pagelayer-id="ffbgB5e4xPIruUJC" stretch="auto" col_gap="10" width_content="auto" row_height="default" overlay_hover_delay="400" row_shape_top_color="#227bc3" row_shape_top_width="100" row_shape_top_height="100" row_shape_bottom_color="#e44993" row_shape_bottom_width="100" row_shape_bottom_height="100"]
[pl_col pagelayer-id="aF6cze85x0CVnb4I" overlay_hover_delay="400"]
[pl_archive_title pagelayer-id="a6sL2H8c5FJDwHmL" align="left" typo=",,,,,,Solid,,,," ele_margin="0px,0px,18px,0px" font_size="28"]
[/pl_archive_title]
[pl_archive_posts pagelayer-id="CrFuxlpqwrKx1cok" type="default" columns="3" columns_mobile="1" col_gap="20" row_gap="40" data_padding="5,5,5,5" bg_color="#ffffff" show_thumb="true" show_title="true" meta="author,date,comments" meta_sep="|" show_content="excerpt" content_color="#121212" content_align="left" pagination="number_prev_next" thumb_size="medium_large" ratio="0.7" title_color="#0986c0" title_typo=",18,,,,,solid,,,," exc_length="10" pagi_prev_text="Previous" pagi_next_text="Next" pagi_end_size="1" pagi_mid_size="2" pagi_align="center"]
[/pl_archive_posts]
[/pl_col]
[/pl_row]';
$pgl['blog-template'] = $conf['blog-template'];
}
// Do we have the blog template ?
if(empty($pgl['single-template'])){
$conf['single-template']['post_content'] = '[pl_row pagelayer-id="TeNMIn3gRsvsyDZj" stretch="auto" col_gap="10" width_content="auto" row_height="default" overlay_hover_delay="400" row_shape_top_color="#227bc3" row_shape_top_width="100" row_shape_top_height="100" row_shape_bottom_color="#e44993" row_shape_bottom_width="100" row_shape_bottom_height="100"]
[pl_col pagelayer-id="qyP2XV3ClSd9cEWM" overlay_hover_delay="400"]
[pl_post_title pagelayer-id="nNt87422AXwZoBQg" title_color="" typo=",35,,700,,,solid,,,," align="center"]
[/pl_post_title]
[/pl_col]
[/pl_row]
[pl_row pagelayer-id="6UuOjtSrBDhWOnWG" stretch="auto" col_gap="10" width_content="fixed" row_height="default" overlay_hover_delay="400" row_shape_top_color="#227bc3" row_shape_top_width="100" row_shape_top_height="100" row_shape_bottom_color="#e44993" row_shape_bottom_width="100" row_shape_bottom_height="100" row_width="70%" fixed_width="70%" fixed_width_tablet="85%" fixed_width_mobile="100%"]
[pl_col pagelayer-id="gzGSF2JVwcPcNUk6" overlay_hover_delay="400" col_width="80" col="12"]
[pl_post_info pagelayer-id="gBDuE9nYBu0bIHyv" layout="vertical" space_between="15" align="center" icon_colors="normal" text_colors="normal"]
[pl_post_info_list pagelayer-id="ZMZjpaTiEc9Ien3t" type="author" info_link="true" info_icon_on="true" info_icon="fas fa-user-circle"]
[/pl_post_info_list]
[pl_post_info_list pagelayer-id="xI8gpn9VRfPDkZ0Q" type="date" info_link="true" info_icon_on="true" info_icon="fas fa-calendar-alt" date_format="default"]
[/pl_post_info_list]
[pl_post_info_list pagelayer-id="LaEZYd9SjEnQHsg3" type="time" info_link="true" info_icon_on="true" info_icon="fas fa-clock" time_format="default"]
[/pl_post_info_list]
[pl_post_info_list pagelayer-id="9lTHiEQJQqESt6YG" type="comments" info_link="true" info_icon_on="true" info_icon="fas fa-comment"]
[/pl_post_info_list]
[/pl_post_info]
[pl_post_excerpt pagelayer-id="NklzzZGW3ve1X8BS" pe_margin="20px,0px,15px,0px" align="left"]
[/pl_post_excerpt]
[pl_featured_img pagelayer-id="sZLiICVhGCbBTx1a" size="full" img_filter="0,100,100,0,0,100,100" caption_color="#0986c0" img_hover_delay="400" custom_size="70%,0%" align="center"]
[/pl_featured_img]
[pl_post_content pagelayer-id="7JbkxQEvq0skyUUl" ele_margin="25px,0px,35px,0px" font_size="NaN"]
[/pl_post_content]
[pl_post_info pagelayer-id="tvno5FCIKdwGa8IE" layout="horizontal" space_between="5" align="left" icon_colors="normal" text_colors="normal" input_typo=",,,,,,Solid,,,,"]
[pl_post_info_list pagelayer-id="SwoZ4cxl3XFMLE3l" type="terms" info_link="true" info_icon_on="" info_icon="fas fa-user-circle" taxonomy="category" info_before="Category :"]
[/pl_post_info_list]
[pl_post_info_list pagelayer-id="sgTqNx5LkBHODkrG" type="terms" info_link="true" info_icon_on="" info_icon="fas fa-user-circle" taxonomy="post_tag" info_before="Tags :"]
[/pl_post_info_list]
[/pl_post_info]
[/pl_col]
[/pl_row]
[pl_row pagelayer-id="heO1UxRj8lIQZ52M" stretch="auto" col_gap="10" width_content="auto" row_height="default" overlay_hover_delay="400" row_shape_top_color="#227bc3" row_shape_top_width="100" row_shape_top_height="100" row_shape_bottom_color="#e44993" row_shape_bottom_width="100" row_shape_bottom_height="100"]
[pl_col pagelayer-id="s3sgObVllcHz0CB7" overlay_hover_delay="400"]
[pl_post_nav pagelayer-id="RrRky7duRa9KGmsA" lables="true" post_title="true" arrows="true" sep_color="#bdbdbd" sep_rotate="20" sep_width="5" prev_label="Previous" next_label="Next" label_colors="normal" title_colors="normal" arrows_list="angle" icon_colors="normal"]
[/pl_post_nav]
[/pl_col]
[/pl_row]
[pl_row pagelayer-id="duGtpLrwHkOWbE0m" stretch="auto" col_gap="10" width_content="auto" row_height="default" overlay_hover_delay="400" row_shape_top_color="#227bc3" row_shape_top_width="100" row_shape_top_height="100" row_shape_bottom_color="#e44993" row_shape_bottom_width="100" row_shape_bottom_height="100"]
[pl_col pagelayer-id="ad58IjV6dHjcRBmV" overlay_hover_delay="400"]
[pl_post_comment pagelayer-id="bwueyBxPgdNLC1Ec" comment_skin="theme_comment" post_type="current"]
[/pl_post_comment]
[/pl_col]
[/pl_row]';
$pgl['single-template'] = $conf['single-template'];
}
return $pgl;
}
// Resize Image
function pagelayer_resizeImage($filename, $newwidth, $newheight){
$imagesize = getimagesize($filename);
$width = $imagesize[0];
$height = $imagesize[1];
// Calculate the Height and width
if($width <= $newwidth || $height <= $newheight){
return false;
}
$thumb = imagecreatetruecolor($newwidth, $newheight);
switch($imagesize['mime']) {
case 'image/jpg':
case 'image/jpeg':
$source = imagecreatefromjpeg($filename);
break;
case 'image/gif':
$source = imagecreatefromgif($filename);
break;
case 'image/png':
$source = imagecreatefrompng($filename);
break;
}
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagedestroy($source);
ob_start();
imagepng($thumb);
$image = ob_get_clean();
imagedestroy($thumb);
return $image;
}