run($run_type); } if ($run_type == 'restart') { $migration_content = self::get_object_file_content(); $migration_content->run($run_type); } } /** * @param $run_type * * @throws \Exception */ public function run($run_type) { if ($run_type == "run") { // check if zip extension exists if ($this->check_if_zip_extension_exists() === false) { throw new \Exception("PHP zip extension is missing"); } // check if config json exists if (!file_exists(self::get_tmp_dir() . "/" . self::MIGRATION_CONFIG_FILE_NAME)) { throw new \Exception("Config json file is missing"); } // check if db sql exists if (!file_exists(self::get_tmp_dir() . "/" . self::MIGRATION_DB_FILE_NAME)) { throw new \Exception("Database file is missing"); } $this->files = $this->get_content_files(); } // if everything ok, create zip archive $this->create_archive(); } /** * function for crating archive from wp-content dir * * @throws \Exception */ private function create_archive() { $this->archive_path = self::get_tmp_dir() . "/" . self::MIGRATION_ARCHIVE; // initialize archive object $this->zip = new \ZipArchive(); if ($this->zip->open($this->archive_path, \ZipArchive::CREATE) !== true) { throw new \Exception("Unable to open zip"); } $all_files = $this->files; foreach ($all_files as $type => &$files) { if (!empty($files)) { foreach ($files as $key => $file_path) { $this->check_for_restart(); unset($files[$key]); $this->files = $all_files; if ($type == "wp-content") { $relative_path = $type . "/" . substr($file_path, strlen(WP_CONTENT_DIR) + 1); } else { $relative_path = $type . "/" . substr($file_path, strlen(ABSPATH . $type) + 1); } if (!is_dir($file_path)) { // add current file to archive $this->add_file_to_zip($file_path, $relative_path); } else { $this->zip->addEmptyDir($relative_path); } $this->files_count++; if ($this->files_count % 600 == 0) { $this->archive_reload(); } } } } // add other necessary files to zip $this->add_external_files_to_archive(); // after all close @$this->zip->close(); } private static function restart_request() { $url = add_query_arg(array( 'rest_route' => '/' . TENWEB_REST_NAMESPACE . '/restart_migration_file' ), get_home_url() . "/"); wp_remote_post($url, array( 'method' => 'POST', 'timeout' => 0.1, 'body' => array( 'tenweb_nonce' => wp_create_nonce('wp_rest') ) )); } /** * */ private function restart() { $this->write_object_file(); @$this->zip->close(); self::restart_request(); die(); } /** * @return bool */ private function check_for_restart() { $max_exec_time_server = ini_get('max_execution_time'); $start = get_site_transient(TENWEB_PREFIX . "_migration_start_time"); $script_exec_time = microtime(true) - $start; if ($script_exec_time >= ((int)$max_exec_time_server - 5)) { $this->restart(); return false; } } /** * * @throws \Exception */ private function add_external_files_to_archive() { // add config json to archive $this->add_file_to_zip(self::get_tmp_dir() . "/" . self::MIGRATION_CONFIG_FILE_NAME, '10web_meta/' . self::MIGRATION_CONFIG_FILE_NAME); // add db sql file to archive $this->add_file_to_zip(self::get_tmp_dir() . "/" . self::MIGRATION_DB_FILE_NAME, '10web_meta/' . self::MIGRATION_DB_FILE_NAME); // add wp-config.php to archive $this->add_file_to_zip(ABSPATH . "/wp-config.php", "wp-config.php"); } /** * @return bool */ private function check_if_zip_extension_exists() { if (!extension_loaded('zip')) { return false; } return true; } /** * @param $file_path * @param $file_relative_path * * @throws \Exception */ private function add_file_to_zip($file_path, $file_relative_path) { if ($this->zip->addFile($file_path, $file_relative_path) !== true) { throw new \Exception("Unable to add " . $file_relative_path . " to zip"); } } /** * function for reloading zip archive */ private function archive_reload() { @$this->zip->close(); $this->zip = new \ZipArchive(); $this->zip->open($this->archive_path, \ZipArchive::CREATE); } /** * @return mixed */ private static function get_object_file_content() { $content = file_get_contents(self::get_tmp_dir() . '/content_object.php'); return unserialize($content); } /** * */ private function write_object_file() { $handle = fopen(self::get_tmp_dir() . '/content_object.php', 'w'); $content = serialize($this); fwrite($handle, $content); fclose($handle); } /** * @return array */ private function get_content_files() { $all_files = array(); // get wp-content files $all_files['wp-content'] = $this->get_files(WP_CONTENT_DIR); // get media files $uploads_dir = Migration::get_uploads_dir(); if (strpos($uploads_dir, WP_CONTENT_DIR) === false) { $uploads_dir_basename = str_replace(ABSPATH, '', $uploads_dir); $all_files[$uploads_dir_basename] = $this->get_files($uploads_dir); } return $all_files; } /** * @param $dir * * @return array */ private function get_files($dir) { $files = array(); $innerIterator = new \RecursiveDirectoryIterator($dir, \RecursiveIteratorIterator::LEAVES_ONLY); $iterator = new \RecursiveIteratorIterator( new \RecursiveCallbackFilterIterator($innerIterator, $this->get_filter()) ); foreach ($iterator as $file) { $file_path = $file->getRealPath(); $files[] = $file_path; } return array_unique($files); } private function get_filter() { $excluded_files = array('imagecache', 'wp\-content\/w3tc', 'wp\-content\/w3\-', 'wp\-config\-sample\.php', 'mu\-plugins\/kinsta\-mu\-plugins\.php', '\.svn$', '\.git$', '\.log$', '\.tmp$', '\.listing$', '\.cache$', '\.bak$', '\.swp$', '\~', '_wpeprivate', 'wp\-content\/cache', 'ics\-importer\-cache', 'gt\-cache', 'plugins\/wpengine\-snapshot\/snapshots', 'uploads\/snapshots', 'wp\-content\/backups', 'wp\-content\/managewp', 'wp\-content\/upgrade', 'kinsta\-mu\-plugins', 'wp\-content\/advanced\-cache\.php', 'wp-content\/wp\-cache\-config\.php' ); $excluded_files[] = preg_quote(self::get_tmp_dir(), '/'); $filter = function ($file, $key, $iterator) use ($excluded_files) { return !preg_match("/(" . implode("|", $excluded_files) . ")/i", $file->getRealPath(), $matches); }; return $filter; } } }