'.json_encode($data).'';
wp_die();
}
function popularfx_import_template($slug, $items = array()){
$data = [];
$destination = pfx_templates_dir().'/'.$slug;
include_once(PAGELAYER_DIR.'/main/import.php');
// Our function needs to efficiently replace the variables
$GLOBALS['popularfx_template_import_slug'] = $slug;
add_filter('pagelayer_start_insert_content', 'popularfx_pagelayer_start_insert_content', 10);
// Full import
if(empty($items)){
// Now import the template
if(!pagelayer_import_theme($slug, $destination)){
$data['error']['import_err'] = __('Could not import the template !', 'popularfx');
$data['error'] = array_merge($data['error'], $pl_error);
return $data;
}
// Save the name of the slug
set_theme_mod('popularfx_template', $slug);
// Single items
}else{
// Now import the SINGLE ITEMS
if(!pagelayer_import_single($slug, $items, $destination)){
$data['error']['import_err'] = __('Could not import the single item !', 'popularfx');
$data['error'] = array_merge($data['error'], $pl_error);
return $data;
}
}
$data['done'] = 1;
return $data;
}
// Download the template
function popularfx_download_template($slug){
global $popularfx, $pl_error;
set_time_limit(300);
$data = [];
// Now lets download the templates
if(!function_exists( 'download_url' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$url = pfx_api_url().'/givetemplate.php?slug='.$slug.'&license='.@$popularfx['license']['license'];
//echo $url;
$tmp_file = download_url($url);
//echo filesize($tmp_file);
//var_dump($tmp_file);
// Error downloading
if(is_wp_error($tmp_file) || filesize($tmp_file) < 1){
if(!empty($tmp_file->errors)){
$data['error']['download_err'] = __('Could not download the theme !', 'popularfx').var_export($tmp_file->errors, true);
return $data;
}
}
$destination = pfx_templates_dir().'/'.$slug;
@mkdir($destination, 0755, true);
//echo $destination;
define('FS_METHOD', 'direct');
WP_Filesystem();
$ret = unzip_file($tmp_file, $destination);
//r_print($ret);
// Try to delete
@unlink($tmp_file);
// Error downloading
if(is_wp_error($ret) || !file_exists($destination.'/style.css')){
if(!empty($ret->errors)){
$data['error']['download'] = __('Could not extract the template !', 'popularfx').var_export($ret->errors, true);
return $data;
}
}
return $data;
}
// Get list of templates
function popularfx_get_templates_list(){
$data = get_transient('popularfx_templates');
// Get any existing copy of our transient data
if(false === $data){
// Start checking for an update
$send_for_check = array(
'timeout' => 90,
'user-agent' => 'WordPress'
);
$raw_response = wp_remote_post( pfx_api_url().'templates.json', $send_for_check );
//pagelayer_print($raw_response);die();
// Is the response valid ?
if ( !is_wp_error( $raw_response ) && ( $raw_response['response']['code'] == 200 ) ){
$data = json_decode($raw_response['body'], true);
}
//pagelayer_print($data);die();
// Feed the updated data into the transient
if(!empty($data['list']) && count($data['list']) > 10){
set_transient('popularfx_templates', $data, 2 * HOUR_IN_SECONDS);
}
}
return $data;
}
// Get the template info from our servers
function popularfx_ajax_template_info(){
// Some AJAX security
check_ajax_referer('popularfx_ajax', 'popularfx_nonce');
$data = [];
if(isset($_REQUEST['slug'])){
$resp = wp_remote_get(pfx_api_url().'template-info.php?slug='.$_REQUEST['slug'], array('timeout' => 30));
// Is the response valid ?
if ( !is_wp_error( $resp ) && ( $resp['response']['code'] == 200 ) ){
$data = json_decode($resp['body'], true);
}
}
popularfx_ajax_output($data);
}
// Start the installation of the template
function popularfx_ajax_start_install_template(){
global $popularfx;
// Some AJAX security
check_ajax_referer('popularfx_ajax', 'popularfx_nonce');
set_time_limit(300);
$data = [];
//pagelayer_print($_POST);die();
$license = pfx_optpost('popularfx_license');
// Check if its a valid license
if(!empty($license)){
$resp = wp_remote_get(pfx_api_url().'license.php?license='.$license, array('timeout' => 30));
if(is_array($resp)){
$json = json_decode($resp['body'], true);
//print_r($json);
}else{
$data['error']['resp_invalid'] = __('The response from PopularFX servers was malformed. Please try again in sometime !', 'popularfx').var_export($resp, true);
popularfx_ajax_output($data);
}
// Save the License
if(empty($json['license'])){
$data['error']['lic_invalid'] = __('The license key is invalid', 'popularfx');
popularfx_ajax_output($data);
}else{
update_option('popularfx_license', $json);
// Load license
pfx_load_license();
}
}
// Load templates
$popularfx['templates'] = popularfx_get_templates_list();
$slug = pfx_optpost('theme');
if(!defined('PAGELAYER_VERSION')){
$data['error']['pl_req'] = __('Pagelayer is required to run these templates !', 'popularfx');
popularfx_ajax_output($data);
}
$single = pfx_optpost('single');
if(!empty($single) && version_compare(PAGELAYER_VERSION, '1.3.2', '<')){
$data['error']['pl_single_import'] = __('You need Pagelayer 1.3.2 to import single pages', 'popularfx');
popularfx_ajax_output($data);
}
// See if the theme is valid
if(empty($popularfx['templates']['list'][$slug])){
$data['error']['template_invalid'] = __('The template you submitted is invalid !', 'popularfx');
popularfx_ajax_output($data);
}
$template = $popularfx['templates']['list'][$slug];
// Do we have the req PL version ?
if(!empty($template['pl_ver']) && version_compare(PAGELAYER_VERSION, $template['pl_ver'], '<')){
$data['error']['pl_ver'] = __('Your Pagelayer version is '.PAGELAYER_VERSION.' while the template requires '.$template['pl_ver'], 'popularfx');
popularfx_ajax_output($data);
}
// Do we have the req PFX Plugin version ?
if(!empty($template['pfx_ver']) && version_compare(PFX_VERSION, $template['pfx_ver'], '<')){
$data['error']['pfx_ver'] = __('Your PopularFX Plugin version is '.PFX_VERSION.' while the template requires '.$template['pfx_ver'], 'popularfx');
popularfx_ajax_output($data);
}
// Is it a pro template ?
if($template['type'] > 1 && empty($popularfx['license']['status'])){
$data['error']['template_pro'] = sprintf(__('The selected template is a Pro template and you have a free or expired license. Please purchase the PopularFX Pro license from here.', 'popularfx'), PFX_PRO_URL);
popularfx_ajax_output($data);
}
$do_we_have_pro = defined('PAGELAYER_PREMIUM');
// Do we need to install Pagelayer or Pagelayer PRO ?
if(!function_exists('pagelayer_theme_import_notices') || (empty($do_we_have_pro) && $template['type'] > 1)){
if($template['type'] > 1){
$installed = pfx_install_pagelayer_pro(@$popularfx['license']['license']);
}else{
$installed = popularfx_install_pagelayer();
}
// Did we fail to install ?
if(is_wp_error($installed) || empty($installed)){
$install_url = admin_url('admin.php?page=popularfx_install_pagelayer&license=').@$popularfx['license']['license'];
$data['error']['pagelayer'] = sprintf(__('There was an error in installing Pagelayer which is required by this template. Please install Pagelayer manually by clicking here and then install the template !', 'popularfx'), $install_url);
if(!empty($installed->errors)){
$data['error']['pagelayer_logs'] = var_export($installed->errors, true);
}
popularfx_ajax_output_xmlwrap($data);
}
}
// Lets notify to download
$data['download'] = 1;
popularfx_ajax_output_xmlwrap($data);
}
// Download template
function popularfx_ajax_download_template(){
global $popularfx;
// Some AJAX security
check_ajax_referer('popularfx_ajax', 'popularfx_nonce');
$slug = pfx_optpost('theme');
// Do the download
$data = popularfx_download_template($slug);
// Any error ?
if(!empty($data['error'])){
popularfx_ajax_output($data);
}
// Lets import then
$data['import'] = 1;
popularfx_ajax_output($data);
}
// Import template
function popularfx_ajax_import_template(){
global $popularfx, $pl_error;
// Some AJAX security
check_ajax_referer('popularfx_ajax', 'popularfx_nonce');
$slug = pfx_optpost('theme');
$single = pfx_optpost('single');
$items = !empty($single) ? ['page' => [$single]] : [];
// Import the template
$data = popularfx_import_template($slug, $items);
popularfx_ajax_output($data);
}
// This is to replace the image variables for the template URL
function popularfx_pagelayer_start_insert_content($post){
$url = pfx_templates_dir_url().'/'.$GLOBALS['popularfx_template_import_slug'].'/';
$replacers['{{theme_url}}/images/'] = $url.'images/';
$replacers['{{theme_url}}'] = $url;
$replacers['{{theme_images}}'] = $url.'images/';
$replacers['{{themes_dir}}'] = dirname(get_stylesheet_directory_uri());
foreach($replacers as $key => $val){
$post['post_content'] = str_replace($key, $val, $post['post_content']);
}
return $post;
}
if(!function_exists('popularfx_templates')){
// The Templates Page
function popularfx_templates(){
global $popularfx, $pl_error;
$popularfx['templates'] = popularfx_get_templates_list();
if(isset($_REQUEST['install'])){
check_admin_referer('popularfx-template');
}
// Is there a license key ?
if(isset($_POST['install'])){
$done = 1;
}
popularfx_templates_T();
}
// The License Page - THEME
function popularfx_templates_T(){
global $popularfx, $pagelayer, $pl_error;
// Any errors ?
if(!empty($pl_error)){
pagelayer_report_error($pl_error);echo '
';
}
?>