/**
  * экспортирует HTML и список используемых файлов (картинок, стилей и скриптов) из Тильды
  * @param integer $page_id код страницы в Тильде
  * @param integer $project_id код проекта в Тильде
  * @param integer $post_id код страницы или поста на Вордпрессе
  *
  * @return array $arDownload - список файлов для закачки (откуда и куда сохранить, в случае ошибки возвращает WP_Error)
  */
 public static function export_tilda_page($page_id, $project_id, $post_id)
 {
     $project = Tilda::get_local_project($project_id);
     /* если проекта нет, или у него устаревший формат, то запросим его с Тильды */
     if (!$project || !isset($project->css) || !isset($project->css[0]) || !isset($project->css[0]->to)) {
         $project = self::update_project($project_id);
     }
     if (is_wp_error($project)) {
         return $project;
         //$arResult['error'] = __("Error. Can't find project with this 'projectid' parameter");
         //echo json_encode($arResult);
         //wp_die();
     }
     $tildapage = Tilda::get_pageexport($page_id);
     if (is_wp_error($tildapage)) {
         return $tildapage;
         //$arResult['error'] = __("Error. Can't find page with this 'pageid' parameter");
         //echo json_encode($arResult);
         //wp_die();
     }
     $tildapage->html = htmlspecialchars_decode($tildapage->html);
     self::update_maps($page_id, $post_id);
     $tildapage = Tilda_Admin::replace_outer_image_to_local($tildapage, $project->export_imgpath);
     $meta = get_post_meta($post_id, '_tilda', true);
     $meta['export_imgpath'] = $project->export_imgpath;
     $meta['export_csspath'] = $project->export_csspath;
     $meta['export_jspath'] = $project->export_jspath;
     $meta['page_id'] = $tildapage->id;
     $meta['project_id'] = $tildapage->projectid;
     $meta['post_id'] = $post_id;
     $arDownload = array();
     $tildapage->css = array();
     $upload_path = Tilda::get_upload_path() . $project->id . '/';
     $upload_dir = Tilda::get_upload_dir() . $project->id . '/';
     if (!is_dir($upload_dir)) {
         mkdir($upload_dir, 0755);
     }
     if (!is_dir($upload_dir . 'pages/')) {
         mkdir($upload_dir . 'pages/', 0755);
     }
     if (!is_dir($upload_dir . 'css/')) {
         mkdir($upload_dir . 'css/', 0755);
     }
     if (!is_dir($upload_dir . 'js/')) {
         mkdir($upload_dir . 'js/', 0755);
     }
     foreach ($project->css as $file) {
         $tildapage->css[] = $upload_path . 'css/' . $file->to;
         $arDownload[] = array('from_url' => $file->from, 'to_dir' => $upload_dir . 'css/' . $file->to);
     }
     $tildapage->js = array();
     foreach ($project->js as $file) {
         $tildapage->js[] = $upload_path . 'js/' . $file->to;
         $arDownload[] = array('from_url' => $file->from, 'to_dir' => $upload_dir . 'js/' . $file->to);
     }
     $post = get_post($post_id);
     $post->post_content = 'Страница синхронизирована с Tilda.cc и вносить правки нужно там';
     //$tildapage->html;
     wp_update_post($post);
     $tildapage->sync_time = current_time('mysql');
     $meta['current_page'] = $tildapage;
     //unset($meta['current_page']->html);
     update_post_meta($post_id, '_tilda', $meta);
     $upload_dir = Tilda::get_upload_dir() . $project->id . '/pages/' . $tildapage->id . '/';
     if (!is_dir($upload_dir)) {
         mkdir($upload_dir, 0755);
     }
     foreach ($tildapage->images as $file) {
         $arDownload[] = array('from_url' => $file->from, 'to_dir' => $upload_dir . $file->to);
     }
     return $arDownload;
 }
 /**
  * экспортирует HTML и список используемых файлов (картинок, стилей и скриптов) из Тильды
  * @param integer $page_id код страницы в Тильде
  * @param integer $project_id код проекта в Тильде
  * @param integer $post_id код страницы или поста на Вордпрессе
  *
  * @return array $arDownload - список файлов для закачки (откуда и куда сохранить, в случае ошибки возвращает WP_Error)
  */
 public static function export_tilda_page($page_id, $project_id, $post_id)
 {
     // так как при изменении страницы мог изменится css или js, поэтому всегда запрашиваем данные проекта с Тильды
     $project = self::update_project($project_id);
     if (is_wp_error($project)) {
         return $project;
         //$arResult['error'] = __("Error. Can't find project with this 'projectid' parameter");
         //echo json_encode($arResult);
         //wp_die();
     }
     $tildapage = Tilda::get_pageexport($page_id);
     if (is_wp_error($tildapage)) {
         return $tildapage;
         //$arResult['error'] = __("Error. Can't find page with this 'pageid' parameter");
         //echo json_encode($arResult);
         //wp_die();
     }
     $tildapage->html = htmlspecialchars_decode($tildapage->html);
     self::update_maps($page_id, $post_id);
     $tildapage = Tilda_Admin::replace_outer_image_to_local($tildapage, $project->export_imgpath);
     $meta = get_post_meta($post_id, '_tilda', true);
     $meta['export_imgpath'] = $project->export_imgpath;
     $meta['export_csspath'] = $project->export_csspath;
     $meta['export_jspath'] = $project->export_jspath;
     $meta['page_id'] = $tildapage->id;
     $meta['project_id'] = $tildapage->projectid;
     $meta['post_id'] = $post_id;
     $arDownload = array();
     $upload_path = Tilda::get_upload_path() . $project->id . '/';
     $upload_dir = Tilda::get_upload_dir() . $project->id . '/';
     if (!is_dir($upload_dir) && !wp_mkdir_p($upload_dir, 0755)) {
         Tilda::$errors->add('no_directory', 'Cannot create directory: ' . $upload_dir);
         return Tilda::$errors;
     }
     if (!is_dir($upload_dir . 'pages/') && !wp_mkdir_p($upload_dir . 'pages/', 0755)) {
         Tilda::$errors->add('no_directory', 'Cannot create directory: ' . $upload_dir . 'pages/');
         return Tilda::$errors;
     }
     if (!is_dir($upload_dir . 'css/') && !wp_mkdir_p($upload_dir . 'css/', 0755)) {
         Tilda::$errors->add('no_directory', 'Cannot create directory: ' . $upload_dir . 'css/');
         return Tilda::$errors;
     }
     if (!is_dir($upload_dir . 'js/') && !wp_mkdir_p($upload_dir . 'js/', 0755)) {
         Tilda::$errors->add('no_directory', 'Cannot create directory: ' . $upload_dir . 'js/');
         return Tilda::$errors;
     }
     if (isset($tildapage->css) && is_array($tildapage->css)) {
         $arCSS = $tildapage->css;
     } else {
         $arCSS = $project->css;
     }
     $tildapage->css = array();
     foreach ($arCSS as $file) {
         $tildapage->css[] = $upload_path . 'css/' . $file->to;
         $arDownload[] = array('from_url' => $file->from, 'to_dir' => $upload_dir . 'css/' . $file->to);
     }
     if (isset($tildapage->js) && is_array($tildapage->js)) {
         $arJS = $tildapage->js;
     } else {
         $arJS = $project->js;
     }
     $tildapage->js = array();
     foreach ($arJS as $file) {
         $tildapage->js[] = $upload_path . 'js/' . $file->to;
         $arDownload[] = array('from_url' => $file->from, 'to_dir' => $upload_dir . 'js/' . $file->to);
     }
     $tildapage->html = str_replace('$(', 'jQuery(', $tildapage->html);
     $tildapage->html = str_replace('$.', 'jQuery.', $tildapage->html);
     $post = get_post($post_id);
     $tildaoptions = get_option('tilda_options');
     if (!empty($tildaoptions['type_stored']) && $tildaoptions['type_stored'] == 'post') {
         $post->post_content = strip_tags($tildapage->html, '<style><script><p><br><span><img><b><i><strike><strong><em><u><h1><h2><h3><a><ul><li>');
         while (($pos = mb_strpos($post->post_content, "<style", 0, 'UTF-8')) !== false) {
             $substring = mb_substr($post->post_content, $pos, mb_strpos($post->post_content, "</style>", 0, 'UTF-8') - $pos + 8, 'UTF-8');
             if ($substring > '') {
                 $post->post_content = str_replace($substring, "", $post->post_content);
             } else {
                 break;
             }
         }
         while (($pos = mb_strpos($post->post_content, "<script", 0, 'UTF-8')) !== false) {
             $substring = mb_substr($post->post_content, $pos, mb_strpos($post->post_content, "</script>", 0, 'UTF-8') - $pos + 9, 'UTF-8');
             if ($substring > '') {
                 $post->post_content = str_replace($substring, "", $post->post_content);
             } else {
                 break;
             }
         }
         $post->post_content = str_replace("\r\n", "\n", $post->post_content);
         $tmp = str_replace("\n\n\n\n", "\n", $post->post_content);
         if ($tmp > '') {
             $tmp = str_replace("\n\n\n", "\n", $tmp);
             if ($tmp > '') {
                 $tmp = str_replace("\n\n", "\n", $tmp);
                 if ($tmp > '') {
                     $tmp = str_replace("\n\n", "\n", $tmp);
                 }
             }
         }
         if ($tmp > '') {
             $post->post_content = nl2br($tmp);
         } else {
             $post->post_content = nl2br($post->post_content);
         }
     } else {
         $post->post_content = 'Page synchronized. Edit page only on site Tilda.cc';
         //$tildapage->html;
     }
     wp_update_post($post);
     $tildapage->sync_time = current_time('mysql');
     $meta['current_page'] = $tildapage;
     //unset($meta['current_page']->html);
     update_post_meta($post_id, '_tilda', $meta);
     $upload_dir = Tilda::get_upload_dir() . $project->id . '/pages/' . $tildapage->id . '/';
     if (!is_dir($upload_dir) && !mkdir($upload_dir, 0755)) {
         Tilda::$errors->add('no_directory', 'Cannot create directory: ' . $upload_dir);
         return Tilda::$errors;
     }
     foreach ($tildapage->images as $file) {
         $arDownload[] = array('from_url' => $file->from, 'to_dir' => $upload_dir . $file->to);
     }
     /* скачиваем спец картинки */
     if (isset($tildapage->img) && substr($tildapage->img, 0, 4) == 'http') {
         $path = parse_url($tildapage->img, PHP_URL_PATH);
         $path = explode('/', $path);
         $fname = array_pop($path);
         if ($fname && ($pos = strrpos($fname, '.')) > 0) {
             $ext = substr($fname, $pos);
             $arDownload[] = array('from_url' => $tildapage->img, 'to_dir' => $upload_dir . 'cover' . $ext);
         }
     }
     if (isset($tildapage->featureimg) && substr($tildapage->featureimg, 0, 4) == 'http') {
         $path = parse_url($tildapage->featureimg, PHP_URL_PATH);
         $path = explode('/', $path);
         $fname = array_pop($path);
         if ($fname && ($pos = strrpos($fname, '.')) > 0) {
             $ext = substr($fname, $pos);
             $arDownload[] = array('from_url' => $tildapage->featureimg, 'to_dir' => $upload_dir . 'feature' . $ext);
         }
     }
     if (isset($tildapage->fb_img) && substr($tildapage->fb_img, 0, 4) == 'http') {
         $path = parse_url($tildapage->fb_img, PHP_URL_PATH);
         $path = explode('/', $path);
         $fname = array_pop($path);
         if ($fname && ($pos = strrpos($fname, '.')) > 0) {
             $ext = substr($fname, $pos);
             $arDownload[] = array('from_url' => $tildapage->fb_img, 'to_dir' => $upload_dir . 'socnet' . $ext);
         }
     }
     return $arDownload;
 }