laipower/wp-content/plugins/static-html-output-plugin/plugin/WP2Static/ArchiveProcessor.php

2 lines
5.9 KiB
PHP
Raw Normal View History

2020-04-07 13:03:04 +00:00
<?php
class ArchiveProcessor extends WP2Static { public function __construct() { require_once dirname( __FILE__ ) . '/../WP2Static/Archive.php'; $this->archive = new Archive(); $this->archive->setToCurrentArchive(); $this->loadSettings( array( 'wpenv', 'crawling', 'advanced', 'processing', 'netlify', 'zip', 'folder', ) ); } public function renameWPDirectory( $source, $target ) { if ( empty( $source ) || empty( $target ) ) { require_once dirname( __FILE__ ) . '/../WP2Static/WsLog.php'; WsLog::l( 'Failed trying to rename: ' . 'Source: ' . $source . ' to: ' . $target ); die(); } $original_dir = $this->archive->path . $source; $new_dir = $this->archive->path . $target; if ( is_dir( $original_dir ) ) { $this->recursive_copy( $original_dir, $new_dir ); WP2Static_FilesHelper::delete_dir_with_files( $original_dir ); } else { require_once dirname( __FILE__ ) . '/../WP2Static/WsLog.php'; WsLog::l( 'Trying to rename non-existent directory: ' . $original_dir ); } } public function recursive_copy( $srcdir, $dstdir ) { $dir = opendir( $srcdir ); if ( ! is_dir( $dstdir ) ) { mkdir( $dstdir ); } while ( $file = readdir( $dir ) ) { if ( $file != '.' && $file != '..' ) { $src = $srcdir . '/' . $file; $dst = $dstdir . '/' . $file; if ( is_dir( $src ) ) { $this->recursive_copy( $src, $dst ); } else { copy( $src, $dst ); } } } closedir( $dir ); } public function dir_is_empty( $dirname ) { if ( ! is_dir( $dirname ) ) { return false; } $dotfiles = array( '.', '..', '/.wp2static_safety' ); foreach ( scandir( $dirname ) as $file ) { if ( ! in_array( $file, $dotfiles ) ) { return false; } } return true; } public function dir_has_safety_file( $dirname ) { if ( ! is_dir( $dirname ) ) { return false; } foreach ( scandir( $dirname ) as $file ) { if ( $file == '.wp2static_safety' ) { return true; } } return false; } public function put_safety_file( $dirname ) { if ( ! is_dir( $dirname ) ) { return false; } $safety_file = $dirname . '/.wp2static_safety'; $result = file_put_contents( $safety_file, 'wp2static' ); chmod( $safety_file, 0664 ); return $result; } public function copyStaticSiteToPublicFolder() { if ( $this->settings['selected_deployment_option'] === 'folder' ) { $target_folder = trim( $this->settings['targetFolder'] ); $this->target_folder = $target_folder; if ( ! $target_folder ) { return; } $directory_exists = true; $directory_empty = false; $dir_has_safety_file = false; $directory_exists = is_dir( $target_folder ); if ( $directory_exists ) { $directory_empty = $this->dir_is_empty( $target_folder ); } else { if ( wp_mkdir_p( $target_folder ) ) { if ( ! $this->put_safety_file( $target_folder ) ) { require_once dirname( __FILE__ ) . '/../WP2Static/WsLog.php'; WsLog::l( 'Couldn\'t put safety file in ' . 'Target Directory' . $target_folder ); die(); } } else { require_once dirname( __FILE__ ) . '/../WP2Static/WsLog.php'; WsLog::l( 'Couldn\'t create Target Directory: ' . $target_folder ); die(); } } if ( $directory_empty ) { if ( ! $this->put_safety_file( $target_folder ) ) { require_once dirname( __FILE__ ) . '/../WP2Static/WsLog.php'; WsLog::l( 'Couldn\'t put safety file in ' . 'Target Directory' . $target_folder ); die(); } } $dir_has_safety_file = $this->dir_has_safety_file( $target_folder ); if ( $directory_empty || $dir_has_safety_file ) { $this->recursive_copy( $this->archive->path, $this->target_folder ); if ( ! $this->put_safety_file( $target_folder ) ) { require_once dirname( __FILE__ ) . '/../WP2Static/WsLog.php'; WsLog::l( 'Couldn\'t put safety file in ' . 'Target Directory' . $target_folder ); die(); } } else { require_once dirname( __FILE__ ) . '/../WP2Static/WsLog.php'; WsLog::l( 'Target Directory wasn\'t empty ' . 'or didn\'t contain safety file ' . $target_folder ); die(); } } } public function createNetlifySpecialFiles() { if ( $this->settings['selected_deployment_option'] !== 'netlify' ) { return false; } if ( isset( $this->settings['netlifyRedirects'] ) ) { $redirect_content = $this->settings['netlifyRedirects']; $redirect_path = $this->archive->path . '_redirects'; file_put_contents( $redirect_path, $redirect_content ); chmod( $