public static function sync_single_export_file($arDownload) { if (!class_exists('Tilda_Admin', false)) { require_once TILDA_PLUGIN_DIR . 'class.tilda-admin.php'; } Tilda_Admin::$ts_start_plugin = time(); $arTmp = array(); $downloaded = 0; foreach ($arDownload as $file) { if (time() - Tilda_Admin::$ts_start_plugin > 5) { $arTmp[] = $file; } else { if (!file_exists($file['to_dir'])) { file_put_contents($file['to_dir'], file_get_contents($file['from_url'])); } $downloaded++; } } $arDownload = $arTmp; if (!empty($arDownload) && sizeof($arDownload) > 0) { wp_schedule_single_event(time() + 1, 'tilda_sync_single_export_file', array($arDownload)); } }
/** * метод вызывается 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(); } $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'])) { file_put_contents($file['to_dir'], file_get_contents($file['from_url'])); } $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'] = "Синхронизация заняла больше 30 секунд и все файлы не успелись синхронизироваться. Нажмите еще раз кнопку Синхронизировать для продолжения синхронизации."; } echo json_encode($arResult); wp_die(); }
/** * метод вызывается 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(); }