/**
  * метод вызывается ajax-запросом из админки (hook)
  *  http://example.com/wp-admin/admin-ajax.php?action=tilda_admin_sync
  *
  */
 public static function ajax_sync()
 {
     $arResult = array();
     if (empty($_REQUEST['page_id']) || empty($_REQUEST['project_id']) || empty($_REQUEST['post_id'])) {
         $arResult['error'] = __('Bad request line. Missing parameter: projectid', 'tilda');
         echo json_encode($arResult);
         wp_die();
     }
     $project_id = intval($_REQUEST['project_id']);
     $page_id = intval($_REQUEST['page_id']);
     $post_id = intval($_REQUEST['post_id']);
     // запускаем экспорт
     $arDownload = self::export_tilda_page($page_id, $project_id, $post_id);
     if (is_wp_error($arDownload)) {
         echo Tilda::json_errors($arDownload);
         wp_die();
     }
     if (!session_id()) {
         session_start();
     }
     $_SESSION['tildaexport'] = array('arDownload' => $arDownload, 'downloaded' => 0, 'total' => sizeof($arDownload));
     $arResult['total_download'] = $_SESSION['tildaexport']['total'];
     $arResult['need_download'] = $arResult['total_download'];
     $arResult['count_downloaded'] = 0;
     $arResult['page_id'] = $page_id;
     $arResult['project_id'] = $project_id;
     $arResult['post_id'] = $post_id;
     //$arResult['dump'] = $arDownload;
     echo json_encode($arResult);
     wp_die();
 }
    }
    ?>
            </div>
        </div>
        <div class="clear"></div>

        <p class="desc silver">
            Выбранная страница будет полностью скопирована на ваш сайт. В дальнейшем, если будут внесены какие-то
            изменения в страницу на Тильде, то необходимо будет обновить страницу вручную, нажав кнопку “Обновить
            страницу”.
        </p>
    <?php 
}
?>
    <?php 
Tilda::show_errors();
?>
    <div class="clear">

        <a href="javascript:void(0)" id="tilda_toggle" class="alignleft remove_tilda">
            Отменить привязку
        </a>

        <p class="submit text-align-right">
            <input type="submit" class="button" value="Сохранить" id="tilda_save_page" data-postid="<?php 
echo $data['post_id'];
?>
">

            <button type="submit" class="button" name="tilda[update_data]" value="update_data">
                Обновить список
 public static function get_local_page($page_id, $project_id, $post_id = 0)
 {
     $projects = self::get_local_projects();
     if ($post_id == 0) {
         $page = $projects[$project_id]->pages[$page_id];
         if (isset($page->post_id)) {
             $post_id = $page->post_id;
         }
     }
     if ($post_id > 0) {
         $data = get_post_meta($post_id, '_tilda', true);
         if (!empty($data['current_page'])) {
             $page = $data['current_page'];
         }
     }
     $upload_path = Tilda::get_upload_path() . $project_id . '/';
     $ar = array();
     foreach ($projects[$project_id]->css as $css) {
         $ar[] = $upload_path . 'css/' . $css->to;
     }
     $page->css = $ar;
     $ar = array();
     foreach ($projects[$project_id]->js as $js) {
         $ar[] = $upload_path . 'js/' . $js->to;
     }
     $page->js = $ar;
     return $page;
 }
            </small>
        <?php 
} else {
    ?>
            <input type="submit"
                   id="tilda_toggle"
                   class="button <?php 
    echo esc_attr($toggle_class);
    ?>
"
                   value="<?php 
    echo __("Activate Tilda for this page?", 'tilda');
    ?>
">
            &nbsp;&nbsp;&nbsp;
            <?php 
    if (!Tilda::verify_access()) {
        ?>
                <a href="options-general.php?page=tilda-config">
                    <?php 
        echo __("Tilda accounts assign", 'tilda');
        ?>
                </a>
            <?php 
    }
    ?>
        <?php 
}
?>
    </p>
</div>
 /**
  * метод вызывается ajax-запросом из админки
  *  http://example.com/wp-admin/admin-ajax.php?action=tilda_admin_export_file
  *  закачивает файлы порциями
  *
  */
 public static function ajax_export_file()
 {
     if (empty(self::$ts_start_plugin)) {
         self::$ts_start_plugin = time();
     }
     if (!session_id()) {
         session_start();
         /*if (session_status() != PHP_SESSION_ACTIVE) {
               Tilda::$errors->add( 'no_start_session', 'Cannoyt start session.');
               echo Tilda::json_errors();
               wp_die();
           }*/
     }
     $arResult = array();
     if (empty($_SESSION['tildaexport']['arDownload'])) {
         $arResult['error'] = 'Error! All downloads';
         $arResult['dump'] = $_SESSION['tildaexport'];
         echo json_encode($arResult);
         die(0);
     }
     $arDownload = $_SESSION['tildaexport']['arDownload'];
     $arTmp = array();
     $downloaded = 0;
     foreach ($arDownload as $file) {
         if (time() - self::$ts_start_plugin > 20) {
             $arTmp[] = $file;
         } else {
             if (!file_exists($file['to_dir']) || strpos($file['to_dir'], '/pages/') === false) {
                 $content = Tilda::getRemoteFile($file['from_url']);
                 if (is_wp_error($content)) {
                     echo Tilda::json_errors();
                     wp_die();
                 }
                 /* replace  short jQuery function $(...) to jQuery(...) */
                 if (strpos($file['to_dir'], 'tilda-blocks-') > 0 && strpos($file['to_dir'], '.js') > 0) {
                     $content = str_replace('$(', 'jQuery(', $content);
                     $content = str_replace('$.', 'jQuery.', $content);
                 }
                 if (file_put_contents($file['to_dir'], $content) === false) {
                     Tilda::$errors->add('error_download', 'Cannot save file to [' . $file['to_dir'] . '].');
                     echo Tilda::json_errors();
                     wp_die();
                 }
             }
             $downloaded++;
         }
     }
     $arDownload = $arTmp;
     $_SESSION['tildaexport']['arDownload'] = $arDownload;
     $_SESSION['tildaexport']['downloaded'] += $downloaded;
     $arResult['total_download'] = $_SESSION['tildaexport']['total'];
     $arResult['need_download'] = sizeof($arDownload);
     //$arResult['total_download'] - $_SESSION['tildaexport']['downloaded'];
     $arResult['count_downloaded'] = $_SESSION['tildaexport']['downloaded'];
     if ($arResult['need_download'] > 0) {
         $arResult['message'] = __("Sync worked more 30 sec and not all files download. Please, click button Synchronization for continue download files from Tilda.cc", 'tilda');
     }
     echo json_encode($arResult);
     wp_die();
 }