Initial commit

This commit is contained in:
2020-04-07 13:03:04 +00:00
committed by Gitium
commit 00f842d9bf
1673 changed files with 471161 additions and 0 deletions

View File

@ -0,0 +1,2 @@
<?php
class Archive extends WP2Static { public function __construct() { $this->loadSettings( array( 'wpenv' ) ); $this->path = ''; $this->name = ''; $this->crawl_list = ''; $this->export_log = ''; } public function setToCurrentArchive() { $handle = fopen( $this->settings['wp_uploads_path'] . '/WP2STATIC-CURRENT-ARCHIVE.txt', 'r' ); $this->path = stream_get_line( $handle, 0 ); $this->name = basename( $this->path ); } public function currentArchiveExists() { return is_file( $this->settings['wp_uploads_path'] . '/WP2STATIC-CURRENT-ARCHIVE.txt' ); } public function create() { $this->name = $this->settings['wp_uploads_path'] . '/wp-static-html-output-' . time(); $this->path = $this->name . '/'; $this->name = basename( $this->path ); if ( wp_mkdir_p( $this->path ) ) { $result = file_put_contents( $this->settings['wp_uploads_path'] . '/WP2STATIC-CURRENT-ARCHIVE.txt', $this->path ); if ( ! $result ) { require_once dirname( __FILE__ ) . '/../WP2Static/WsLog.php'; WsLog::l( 'USER WORKING DIRECTORY NOT WRITABLE' ); } chmod( $this->settings['wp_uploads_path'] . '/WP2STATIC-CURRENT-ARCHIVE.txt', 0664 ); } else { error_log( "Couldn't create archive directory at " . $this->path ); } } }

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,2 @@
<?php
class WPSHO_DBSettings { public static function get( $sets = array() ) { $plugin = WP2Static_Controller::getInstance(); $settings = array(); $key_sets = array(); $target_keys = array(); $key_sets['general'] = array( 'baseUrl', 'debug_mode', 'selected_deployment_option', ); $key_sets['crawling'] = array( 'additionalUrls', 'excludeURLs', 'useBasicAuth', 'basicAuthPassword', 'basicAuthUser', 'detection_level', 'crawl_delay', 'crawlPort', ); $key_sets['processing'] = array( 'removeConditionalHeadComments', 'allowOfflineUsage', 'baseHREF', 'rewrite_rules', 'rename_rules', 'removeWPMeta', 'removeWPLinks', 'useBaseHref', 'useRelativeURLs', 'removeConditionalHeadComments', 'removeWPMeta', 'removeWPLinks', 'removeHTMLComments', ); $key_sets['advanced'] = array( 'crawl_increment', 'completionEmail', 'delayBetweenAPICalls', 'deployBatchSize', ); $key_sets['folder'] = array( 'baseUrl-folder', 'targetFolder', ); $key_sets['zip'] = array( 'baseUrl-zip', 'allowOfflineUsage', ); $key_sets['github'] = array( 'baseUrl-github', 'ghBranch', 'ghPath', 'ghToken', 'ghRepo', 'ghCommitMessage', ); $key_sets['bitbucket'] = array( 'baseUrl-bitbucket', 'bbBranch', 'bbPath', 'bbToken', 'bbRepo', ); $key_sets['gitlab'] = array( 'baseUrl-gitlab', 'glBranch', 'glPath', 'glToken', 'glProject', ); $key_sets['ftp'] = array( 'baseUrl-ftp', 'ftpPassword', 'ftpRemotePath', 'ftpServer', 'ftpPort', 'ftpTLS', 'ftpUsername', 'useActiveFTP', ); $key_sets['bunnycdn'] = array( 'baseUrl-bunnycdn', 'bunnycdnStorageZoneAccessKey', 'bunnycdnPullZoneAccessKey', 'bunnycdnPullZoneID', 'bunnycdnStorageZoneName', 'bunnycdnRemotePath', ); $key_sets['s3'] = array( 'baseUrl-s3', 'cfDistributionId', 's3Bucket', 's3Key', 's3Region', 's3RemotePath', 's3Secret', ); $key_sets['netlify'] = array( 'baseUrl-netlify', 'netlifyHeaders', 'netlifyPersonalAccessToken', 'netlifyRedirects', 'netlifySiteID', ); $key_sets['wpenv'] = array( 'wp_site_url', 'wp_site_path', 'wp_site_subdir', 'wp_uploads_path', 'wp_uploads_url', 'wp_active_theme', 'wp_themes', 'wp_uploads', 'wp_plugins', 'wp_content', 'wp_inc', ); foreach ( $sets as $set ) { $target_keys = array_merge( $target_keys, $key_sets[ $set ] ); } foreach ( $target_keys as $key ) { $settings[ $key ] = $plugin->options->{ $key }; } require_once dirname( __FILE__ ) . '/../WP2Static/WPSite.php'; $wp_site = new WPSite(); foreach ( $key_sets['wpenv'] as $key ) { $settings[ $key ] = $wp_site->{ $key }; } $settings['crawl_increment'] = isset( $plugin->options->crawl_increment ) ? (int) $plugin->options->crawl_increment : 1; $settings['baseUrl'] = rtrim( $plugin->options->baseUrl, '/' ) . '/'; return array_filter( $settings ); } }

View File

@ -0,0 +1,2 @@
<?php
class Deployer extends WP2Static { public function __construct() { $this->loadSettings( array( 'advanced', ) ); } public function deploy( $test = false ) { $method = $this->settings['selected_deployment_option']; WP_CLI::log( 'Deploying static site via: ' . $method ); $start_time = microtime( true ); $deployers_dir = dirname( __FILE__ ) . '/../deployers'; switch ( $this->settings['selected_deployment_option'] ) { case 'folder': break; case 'zip': break; case 's3': require_once dirname( __FILE__ ) . '/../WP2Static/SitePublisher.php'; require_once $deployers_dir . '/S3.php'; if ( $test ) { error_log( 'testing s3 deploy' ); $s3->test_s3(); return; } $s3->bootstrap(); $s3->loadArchive(); $s3->prepareDeploy(); $s3->upload_files(); $s3->cloudfront_invalidate_all_items(); break; case 'bitbucket': require_once dirname( __FILE__ ) . '/../WP2Static/SitePublisher.php'; require_once $deployers_dir . '/Bitbucket.php'; if ( $test ) { error_log( 'testing bitbucket deploy' ); $bitbucket->test_upload(); return; } $bitbucket->bootstrap(); $bitbucket->loadArchive(); $bitbucket->prepareDeploy( true ); $bitbucket->upload_files(); break; case 'bunnycdn': require_once dirname( __FILE__ ) . '/../WP2Static/SitePublisher.php'; require_once $deployers_dir . '/BunnyCDN.php'; if ( $test ) { error_log( 'testing BunnyCDN deploy' ); $bunny->test_deploy(); return; } $bunny->bootstrap(); $bunny->loadArchive(); $bunny->prepareDeploy( true ); $bunny->upload_files(); $bunny->purge_all_cache(); break; case 'ftp': require_once dirname( __FILE__ ) . '/../WP2Static/SitePublisher.php'; require_once $deployers_dir . '/FTP.php'; if ( $test ) { error_log( 'testing FTP deploy' ); $ftp->test_ftp(); return; } $ftp->bootstrap(); $ftp->loadArchive(); $ftp->prepareDeploy(); $ftp->upload_files(); break; case 'github': require_once dirname( __FILE__ ) . '/../WP2Static/SitePublisher.php'; require_once $deployers_dir . '/GitHub.php'; if ( $test ) { error_log( 'testing GitHub deploy' ); $github->test_upload(); return; } $github->bootstrap(); $github->loadArchive(); $github->prepareDeploy( true ); $github->upload_files(); break; case 'gitlab': require_once dirname( __FILE__ ) . '/../WP2Static/SitePublisher.php'; require_once $deployers_dir . '/GitLab.php'; if ( $test ) { error_log( 'testing GitLab deploy' ); $gitlab->test_file_create(); return; } $gitlab->bootstrap(); $gitlab->loadArchive(); $gitlab->getListOfFilesInRepo(); $gitlab->prepareDeploy( true ); $gitlab->upload_files(); break; case 'netlify': require_once dirname( __FILE__ ) . '/../WP2Static/SitePublisher.php'; require_once $deployers_dir . '/Netlify.php'; if ( $test ) { error_log( 'testing Netlify deploy' ); $gitlab->loadArchive(); $netlify->test_netlify(); return; } $netlify->bootstrap(); $netlify->loadArchive(); $netlify->deploy(); break; } $end_time = microtime( true ); $duration = $end_time - $start_time; WP_CLI::success( 'Deployed to: ' . $method . ' in ' . date( 'H:i:s', $duration ) ); $this->finalizeDeployment(); } public function finalizeDeployment() { $this->emailDeployNotification(); $this->triggerPostDeployHooks(); } public function emailDeployNotification() { if ( ! isset( $this->settings['completionEmail'] ) ) { return; } if ( defined( 'WP_CLI' ) ) { WP_CLI::line( 'Sending confirmation email...' ); } $current_user = wp_get_current_user(); $to = $current_user->user_email; $subject = 'Static site deployment: ' . $site_title = get_bloginfo( 'name' ); $body = 'Your WordPress site has been automatically deployed.'; $headers = array( 'Content-Type: text/html; charset=UTF-8' ); wp_mail( $to, $subject, $body, $headers ); } public function triggerPostDeployHooks() { require_once dirname( __FILE__ ) . '/Archive.php'; $this->archive = new Archive(); $this->archive->setToCurrentArchive(); do_action( 'wp2static_post_deploy_trigger', $this->archive ); } }

View File

@ -0,0 +1,2 @@
<?php
$ajax_action = isset( $_POST['ajax_action'] ) ? $_POST['ajax_action'] : ''; $deployers_dir = dirname( __FILE__ ) . '/../deployers'; if ( $ajax_action === 'crawl_site' || $ajax_action === 'crawl_again' ) { require_once dirname( __FILE__ ) . '/WP2Static.php'; require_once dirname( __FILE__ ) . '/SiteCrawler.php'; wp_die(); return null; } elseif ( $ajax_action == 'bitbucket_prepare_export' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/Bitbucket.php'; wp_die(); return null; } elseif ( $ajax_action == 'bitbucket_upload_files' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/Bitbucket.php'; wp_die(); return null; } elseif ( $ajax_action == 'github_prepare_export' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/GitHub.php'; wp_die(); return null; } elseif ( $ajax_action == 'github_upload_files' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/GitHub.php'; wp_die(); return null; } elseif ( $ajax_action == 'test_github' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/GitHub.php'; wp_die(); return null; } elseif ( $ajax_action == 'gitlab_prepare_export' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/GitLab.php'; wp_die(); return null; } elseif ( $ajax_action == 'gitlab_upload_files' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/GitLab.php'; wp_die(); return null; } elseif ( $ajax_action == 'test_gitlab' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/GitLab.php'; wp_die(); return null; } elseif ( $ajax_action == 'test_bitbucket' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/Bitbucket.php'; wp_die(); return null; } elseif ( $ajax_action == 'test_netlify' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/Netlify.php'; wp_die(); return null; } elseif ( $ajax_action == 'netlify_do_export' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/Netlify.php'; wp_die(); return null; } elseif ( $ajax_action == 'test_s3' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/S3.php'; wp_die(); return null; } elseif ( $ajax_action == 's3_prepare_export' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/S3.php'; wp_die(); return null; } elseif ( $ajax_action == 's3_transfer_files' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/S3.php'; wp_die(); return null; } elseif ( $ajax_action == 'cloudfront_invalidate_all_items' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/S3.php'; wp_die(); return null; } elseif ( $ajax_action == 'test_ftp' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/FTP.php'; wp_die(); return null; } elseif ( $ajax_action == 'ftp_prepare_export' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/FTP.php'; wp_die(); return null; } elseif ( $ajax_action == 'ftp_transfer_files' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/FTP.php'; wp_die(); return null; } elseif ( $ajax_action == 'test_bunny' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/BunnyCDN.php'; wp_die(); return null; } elseif ( $ajax_action == 'bunnycdn_prepare_export' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/BunnyCDN.php'; wp_die(); return null; } elseif ( $ajax_action == 'bunnycdn_transfer_files' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/BunnyCDN.php'; wp_die(); return null; } elseif ( $ajax_action == 'bunnycdn_purge_cache' ) { require_once dirname( __FILE__ ) . '/SitePublisher.php'; require_once $deployers_dir . '/BunnyCDN.php'; wp_die(); return null; }

View File

@ -0,0 +1,2 @@
<?php
class Exporter extends WP2Static { public function __construct() { $this->loadSettings( array( 'wpenv', 'crawling', 'advanced', ) ); } public function pre_export_cleanup() { $files_to_clean = array( 'WP-STATIC-2ND-CRAWL-LIST.txt', 'WP-STATIC-404-LOG.txt', 'WP-STATIC-CRAWLED-LINKS.txt', 'WP-STATIC-DISCOVERED-URLS-LOG.txt', 'WP-STATIC-DISCOVERED-URLS.txt', 'WP2STATIC-FILES-TO-DEPLOY.txt', 'WP-STATIC-EXPORT-LOG.txt', 'WP-STATIC-FINAL-2ND-CRAWL-LIST.txt', 'WP-STATIC-FINAL-CRAWL-LIST.txt', 'WP2STATIC-GITLAB-FILES-IN-REPO.txt', ); foreach ( $files_to_clean as $file_to_clean ) { if ( file_exists( $this->settings['wp_uploads_path'] . '/' . $file_to_clean ) ) { unlink( $this->settings['wp_uploads_path'] . '/' . $file_to_clean ); } } } public function cleanup_working_files() { if ( is_file( $this->settings['wp_uploads_path'] . '/WP2STATIC-CURRENT-ARCHIVE.txt' ) ) { $handle = fopen( $this->settings['wp_uploads_path'] . '/WP2STATIC-CURRENT-ARCHIVE.txt', 'r' ); $this->settings['archive_dir'] = stream_get_line( $handle, 0 ); } $files_to_clean = array( '/WP-STATIC-2ND-CRAWL-LIST.txt', '/WP-STATIC-CRAWLED-LINKS.txt', '/WP-STATIC-DISCOVERED-URLS.txt', '/WP2STATIC-FILES-TO-DEPLOY.txt', '/WP-STATIC-FINAL-2ND-CRAWL-LIST.txt', '/WP-STATIC-FINAL-CRAWL-LIST.txt', '/WP2STATIC-GITLAB-FILES-IN-REPO.txt', ); foreach ( $files_to_clean as $file_to_clean ) { if ( file_exists( $this->settings['wp_uploads_path'] . '/' . $file_to_clean ) ) { unlink( $this->settings['wp_uploads_path'] . '/' . $file_to_clean ); } } } public function initialize_cache_files() { $crawled_links_file = $this->settings['wp_uploads_path'] . '/WP-STATIC-CRAWLED-LINKS.txt'; $resource = fopen( $crawled_links_file, 'w' ); fwrite( $resource, '' ); fclose( $resource ); } public function cleanup_leftover_archives() { $leftover_files = preg_grep( '/^([^.])/', scandir( $this->settings['wp_uploads_path'] ) ); foreach ( $leftover_files as $filename ) { if ( strpos( $filename, 'wp-static-html-output-' ) !== false ) { $deletion_target = $this->settings['wp_uploads_path'] . '/' . $filename; if ( is_dir( $deletion_target ) ) { WP2Static_FilesHelper::delete_dir_with_files( $deletion_target ); } else { unlink( $deletion_target ); } } } if ( ! defined( 'WP_CLI' ) ) { echo 'SUCCESS'; } } public function generateModifiedFileList() { copy( $this->settings['wp_uploads_path'] . '/WP-STATIC-INITIAL-CRAWL-LIST.txt', $this->settings['wp_uploads_path'] . '/WP-STATIC-MODIFIED-CRAWL-LIST.txt' ); chmod( $this->settings['wp_uploads_path'] . '/WP-STATIC-MODIFIED-CRAWL-LIST.txt', 0664 ); if ( ! isset( $this->settings['excludeURLs'] ) && ! isset( $this->settings['additionalUrls'] ) ) { copy( $this->settings['wp_uploads_path'] . '/WP-STATIC-INITIAL-CRAWL-LIST.txt', $this->settings['wp_uploads_path'] . '/WP-STATIC-FINAL-CRAWL-LIST.txt' ); return; } $modified_crawl_list = array(); $crawl_list = file( $this->settings['wp_uploads_path'] . '/WP-STATIC-MODIFIED-CRAWL-LIST.txt' ); if ( isset( $this->settings['excludeURLs'] ) ) { $exclusions = explode( "\n", str_replace( "\r", '', $this->settings['excludeURLs'] ) ); foreach ( $crawl_list as $url_to_crawl ) { $url_to_crawl = trim( $url_to_crawl ); $match = false; foreach ( $exclusions as $exclusion ) { $exclusion = trim( $exclusion ); if ( $exclusion != '' ) { if ( strpos( $url_to_crawl, $exclusion ) !== false ) { $this->logAction( 'Excluding ' . $url_to_crawl . ' because of rule ' . $exclusion ); $match = true; } } if ( ! $match ) { $modified_crawl_list[] = $url_to_crawl; } } } } else { $modified_crawl_list = $crawl_list; } if ( isset( $this->settings['additionalUrls'] ) ) { $inclusions = explode( "\n", str_replace( "\r", '', $this->settings['additionalUrls'] ) ); foreach ( $inclusions as $inclusion ) { $inclusion = trim( $inclusion ); $inclusion = $inclusion; $modified_crawl_list[] = $inclusion; } } $modified_crawl_list = array_unique( $modified_crawl_list ); $str = implode( PHP_EOL, $modified_crawl_list ); file_put_contents( $this->settings['wp_uploads_path'] . '/WP-STATIC-FINAL-CRAWL-LIST.txt', $str ); chmod( $this->settings['wp_uploads_path'] . '/WP-STATIC-FINAL-CRAWL-LIST.txt', 0664 ); } public function logAction( $action ) { if ( ! isset( $this->settings['debug_mode'] ) ) { return; } require_once dirname( __FILE__ ) . '/../WP2Static/WsLog.php'; WsLog::l( $action ); } }

View File

@ -0,0 +1,2 @@
<?php
class FileCopier { public function __construct( $url, $wp_site_url, $wp_site_path ) { $this->url = $url; $this->wp_site_url = $wp_site_url; $this->wp_site_path = $wp_site_path; } public function getLocalFileForURL() { $local_file = str_replace( $this->wp_site_url, $this->wp_site_path, $this->url ); if ( is_file( $local_file ) ) { return $local_file; } else { require_once dirname( __FILE__ ) . '/../WP2Static/WsLog.php'; WsLog::l( 'ERROR: trying to copy local file: ' . $local_file . ' for URL: ' . $this->url . ' (FILE NOT FOUND/UNREADABLE)' ); } } public function copyFile( $archive_dir ) { $url_info = parse_url( $this->url ); $path_info = array(); $local_file = $this->getLocalFileForURL(); if ( ! isset( $url_info['path'] ) ) { return false; } $path_info = pathinfo( $url_info['path'] ); $directory_in_archive = isset( $path_info['dirname'] ) ? $path_info['dirname'] : ''; if ( ! empty( $this->settings['wp_site_subdir'] ) ) { $directory_in_archive = str_replace( $this->settings['wp_site_subdir'], '', $directory_in_archive ); } $file_dir = $archive_dir . ltrim( $directory_in_archive, '/' ); if ( ! file_exists( $file_dir ) ) { wp_mkdir_p( $file_dir ); } $file_extension = $path_info['extension']; $basename = $path_info['filename'] . '.' . $file_extension; $filename = $file_dir . '/' . $basename; $filename = str_replace( '//', '/', $filename ); if ( is_file( $local_file ) ) { copy( $local_file, $filename ); } else { require_once dirname( __FILE__ ) . '/../WP2Static/WsLog.php'; WsLog::l( 'ERROR: trying to copy local file: ' . $local_file . ' to: ' . $filename . ' in archive dir: ' . $archive_dir . ' (FILE NOT FOUND/UNREADABLE)' ); } } }

View File

@ -0,0 +1,2 @@
<?php
class FileWriter extends WP2Static { public function __construct( $url, $content, $file_type, $content_type ) { $this->url = $url; $this->content = $content; $this->file_type = $file_type; $this->content_type = $content_type; $this->loadSettings( array( 'wpenv', ) ); } public function saveFile( $archive_dir ) { $url_info = parse_url( $this->url ); $path_info = array(); if ( ! isset( $url_info['path'] ) ) { return false; } if ( $url_info['path'] != '/' ) { $path_info = pathinfo( $url_info['path'] ); } else { $path_info = pathinfo( 'index.html' ); } $directory_in_archive = isset( $path_info['dirname'] ) ? $path_info['dirname'] : ''; if ( ! empty( $this->settings['wp_site_subdir'] ) ) { $directory_in_archive = str_replace( $this->settings['wp_site_subdir'], '', $directory_in_archive ); } $file_dir = $archive_dir . ltrim( $directory_in_archive, '/' ); if ( empty( $path_info['extension'] ) && $path_info['basename'] === $path_info['filename'] ) { $file_dir .= '/' . $path_info['basename']; $path_info['filename'] = 'index'; } if ( ! file_exists( $file_dir ) ) { wp_mkdir_p( $file_dir ); } $file_extension = ''; if ( isset( $path_info['extension'] ) ) { $file_extension = $path_info['extension']; } elseif ( $this->file_type == 'html' ) { $file_extension = 'html'; } elseif ( $this->file_type == 'xml' ) { $file_extension = 'html'; } $filename = ''; if ( $url_info['path'] == '/' ) { $filename = rtrim( $file_dir, '.' ) . 'index.html'; } else { if ( ! empty( $this->settings['wp_site_subdir'] ) ) { $file_dir = str_replace( '/' . $this->settings['wp_site_subdir'], '/', $file_dir ); } $filename = $file_dir . '/' . $path_info['filename'] . '.' . $file_extension; } $file_contents = $this->content; if ( $file_contents ) { $this->logAction( 'SAVING ' . $this->url . ' to ' . $filename ); file_put_contents( $filename, $file_contents ); chmod( $filename, 0664 ); } else { $this->logAction( 'NOT SAVING EMTPY FILE ' . $this->url ); } } public function logAction( $action ) { if ( ! isset( $this->settings['debug_mode'] ) ) { return; } require_once dirname( __FILE__ ) . '/WsLog.php'; WsLog::l( $action ); } }

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,2 @@
<?php
class WP2Static_Options { protected $wp2static_options = array(); protected $wp2static_option_key = null; protected $wp2static_options_keys = array( 'additionalUrls', 'allowOfflineUsage', 'baseHREF', 'baseUrl', 'baseUrl-bitbucket', 'baseUrl-bunnycdn', 'baseUrl-folder', 'baseUrl-ftp', 'baseUrl-github', 'baseUrl-gitlab', 'baseUrl-netlify', 'baseUrl-s3', 'baseUrl-zip', 'baseUrl-zip', 'basicAuthPassword', 'basicAuthUser', 'bbBranch', 'bbPath', 'bbRepo', 'bbToken', 'bunnycdnStorageZoneAccessKey', 'bunnycdnPullZoneAccessKey', 'bunnycdnPullZoneID', 'bunnycdnStorageZoneName', 'bunnycdnRemotePath', 'cfDistributionId', 'completionEmail', 'crawl_delay', 'crawl_increment', 'crawlPort', 'debug_mode', 'detection_level', 'delayBetweenAPICalls', 'deployBatchSize', 'excludeURLs', 'ftpPassword', 'ftpRemotePath', 'ftpServer', 'ftpPort', 'ftpTLS', 'ftpUsername', 'ghBranch', 'ghCommitMessage', 'ghPath', 'ghRepo', 'ghToken', 'glBranch', 'glPath', 'glProject', 'glToken', 'netlifyHeaders', 'netlifyPersonalAccessToken', 'netlifyRedirects', 'netlifySiteID', 'removeConditionalHeadComments', 'removeHTMLComments', 'removeWPLinks', 'removeWPMeta', 'rewrite_rules', 'rename_rules', 's3Bucket', 's3Key', 's3Region', 's3RemotePath', 's3Secret', 'selected_deployment_option', 'targetFolder', 'useActiveFTP', 'useBaseHref', 'useBasicAuth', 'useRelativeURLs', ); protected $whitelisted_keys = array( 'additionalUrls', 'allowOfflineUsage', 'baseHREF', 'baseUrl', 'baseUrl-bitbucket', 'baseUrl-bunnycdn', 'baseUrl-folder', 'baseUrl-ftp', 'baseUrl-github', 'baseUrl-gitlab', 'baseUrl-netlify', 'baseUrl-s3', 'baseUrl-zip', 'baseUrl-zip', 'basicAuthUser', 'bbBranch', 'bbPath', 'bbRepo', 'bunnycdnPullZoneID', 'bunnycdnStorageZoneName', 'bunnycdnRemotePath', 'cfDistributionId', 'completionEmail', 'crawl_delay', 'crawl_increment', 'crawlPort', 'debug_mode', 'detection_level', 'delayBetweenAPICalls', 'deployBatchSize', 'excludeURLs', 'ftpRemotePath', 'ftpServer', 'ftpPort', 'ftpTLS', 'ftpUsername', 'ghBranch', 'ghCommitMessage', 'ghPath', 'ghRepo', 'glBranch', 'glPath', 'glProject', 'netlifyHeaders', 'netlifyRedirects', 'netlifySiteID', 'removeConditionalHeadComments', 'removeHTMLComments', 'removeWPLinks', 'removeWPMeta', 'rewrite_rules', 'rename_rules', 's3Bucket', 's3Key', 's3Region', 's3RemotePath', 'selected_deployment_option', 'targetFolder', 'useActiveFTP', 'useBaseHref', 'useBasicAuth', 'useRelativeURLs', ); public function __construct( $option_key ) { $options = get_option( $option_key ); if ( false === $options ) { $options = array(); } $this->wp2static_options = $options; $this->wp2static_option_key = $option_key; } public function __set( $name, $value ) { $this->wp2static_options[ $name ] = $value; return $this; } public function setOption( $name, $value ) { return $this->__set( $name, $value ); } public function __get( $name ) { $value = array_key_exists( $name, $this->wp2static_options ) ? $this->wp2static_options[ $name ] : null; return $value; } public function getOption( $name ) { return $this->__get( $name ); } public function getAllOptions( $reveal_sensitive_values = false ) { $options_array = array(); foreach ( $this->wp2static_options_keys as $key ) { $value = '*******************'; if ( in_array( $key, $this->whitelisted_keys ) ) { $value = $this->__get( $key ); } elseif ( $reveal_sensitive_values ) { $value = $this->__get( $key ); } $options_array[] = array( 'Option name' => $key, 'Value' => $value, ); } return $options_array; } public function optionExists( $name ) { return in_array( $name, $this->wp2static_options_keys ); } public function save() { return update_option( $this->wp2static_option_key, $this->wp2static_options ); } public function delete() { return delete_option( $this->wp2static_option_key ); } public function saveAllPostData() { foreach ( $this->wp2static_options_keys as $option ) { $this->setOption( $option, filter_input( INPUT_POST, $option ) ); $this->save(); } } }

View File

@ -0,0 +1,2 @@
<?php
class WPSHO_PostSettings { public static function get( $sets = array() ) { $settings = array(); $key_sets = array(); $target_keys = array(); $key_sets['general'] = array( 'baseUrl', 'debug_mode', 'selected_deployment_option', ); $key_sets['crawling'] = array( 'additionalUrls', 'excludeURLs', 'useBasicAuth', 'basicAuthPassword', 'basicAuthUser', 'detection_level', 'crawl_delay', 'crawlPort', ); $key_sets['processing'] = array( 'removeConditionalHeadComments', 'allowOfflineUsage', 'baseHREF', 'rewrite_rules', 'rename_rules', 'removeWPMeta', 'removeWPLinks', 'useBaseHref', 'useRelativeURLs', 'removeConditionalHeadComments', 'removeWPMeta', 'removeWPLinks', 'removeHTMLComments', ); $key_sets['advanced'] = array( 'crawl_increment', 'completionEmail', 'delayBetweenAPICalls', 'deployBatchSize', ); $key_sets['folder'] = array( 'baseUrl-folder', 'targetFolder', ); $key_sets['zip'] = array( 'baseUrl-zip', 'allowOfflineUsage', ); $key_sets['github'] = array( 'baseUrl-github', 'ghBranch', 'ghPath', 'ghToken', 'ghRepo', 'ghCommitMessage', ); $key_sets['bitbucket'] = array( 'baseUrl-bitbucket', 'bbBranch', 'bbPath', 'bbToken', 'bbRepo', ); $key_sets['gitlab'] = array( 'baseUrl-gitlab', 'glBranch', 'glPath', 'glToken', 'glProject', ); $key_sets['ftp'] = array( 'baseUrl-ftp', 'ftpPassword', 'ftpRemotePath', 'ftpServer', 'ftpPort', 'ftpTLS', 'ftpUsername', 'useActiveFTP', ); $key_sets['bunnycdn'] = array( 'baseUrl-bunnycdn', 'bunnycdnStorageZoneAccessKey', 'bunnycdnPullZoneAccessKey', 'bunnycdnPullZoneID', 'bunnycdnStorageZoneName', 'bunnycdnRemotePath', ); $key_sets['s3'] = array( 'baseUrl-s3', 'cfDistributionId', 's3Bucket', 's3Key', 's3Region', 's3RemotePath', 's3Secret', ); $key_sets['netlify'] = array( 'baseUrl-netlify', 'netlifyHeaders', 'netlifyPersonalAccessToken', 'netlifyRedirects', 'netlifySiteID', ); $key_sets['wpenv'] = array( 'wp_site_url', 'wp_site_path', 'wp_site_subdir', 'wp_uploads_path', 'wp_uploads_url', 'baseUrl', 'wp_active_theme', 'wp_themes', 'wp_uploads', 'wp_plugins', 'wp_content', 'wp_inc', ); foreach ( $sets as $set ) { $target_keys = array_merge( $target_keys, $key_sets[ $set ] ); } foreach ( $target_keys as $key ) { $settings[ $key ] = isset( $_POST[ $key ] ) ? $_POST[ $key ] : null; } $settings['crawl_increment'] = isset( $_POST['crawl_increment'] ) ? (int) $_POST['crawl_increment'] : 1; $settings['baseUrl'] = isset( $_POST['baseUrl'] ) ? rtrim( $_POST['baseUrl'], '/' ) . '/' : 'http://OFFLINEZIP.wpsho'; return array_filter( $settings ); } }

View File

@ -0,0 +1,2 @@
<?php
class ProgressLog { public static function l( $portion, $total ) { if ( $total === 0 ) { return; } $target_settings = array( 'wpenv', ); $wp_uploads_path = ''; if ( defined( 'WP_CLI' ) ) { require_once dirname( __FILE__ ) . '/DBSettings.php'; $settings = WPSHO_DBSettings::get( $target_settings ); $wp_uploads_path = $settings['wp_uploads_path']; } else { $wp_uploads_path = $_POST['wp_uploads_path']; } $log_file_path = $wp_uploads_path . '/WP-STATIC-PROGRESS.txt'; $progress_percent = floor( $portion / $total * 100 ); file_put_contents( $log_file_path, $progress_percent . PHP_EOL, LOCK_EX ); chmod( $log_file_path, 0664 ); } }

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,2 @@
<?php
class TXTProcessor extends WP2Static { public function __construct() { $this->loadSettings( array( 'crawling', 'wpenv', 'processing', 'advanced', ) ); } public function processTXT( $txt_document, $page_url ) { if ( $txt_document == '' ) { return false; } $this->txt_doc = $txt_document; $this->destination_protocol = $this->getTargetSiteProtocol( $this->settings['baseUrl'] ); $this->placeholder_url = $this->destination_protocol . 'PLACEHOLDER.wpsho/'; $this->rewriteSiteURLsToPlaceholder(); return true; } public function getTXT() { $processed_txt = $this->txt_doc; $processed_txt = $this->detectEscapedSiteURLs( $processed_txt ); $processed_txt = $this->detectUnchangedURLs( $processed_txt ); return $processed_txt; } public function detectEscapedSiteURLs( $processed_txt ) { $escaped_site_url = addcslashes( $this->placeholder_url, '/' ); if ( strpos( $processed_txt, $escaped_site_url ) !== false ) { return $this->rewriteEscapedURLs( $processed_txt ); } return $processed_txt; } public function detectUnchangedURLs( $processed_txt ) { $site_url = $this->placeholder_url; if ( strpos( $processed_txt, $site_url ) !== false ) { return $this->rewriteUnchangedURLs( $processed_txt ); } return $processed_txt; } public function rewriteUnchangedURLs( $processed_txt ) { if ( ! isset( $this->settings['rewrite_rules'] ) ) { $this->settings['rewrite_rules'] = ''; } $this->settings['rewrite_rules'] .= PHP_EOL . $this->placeholder_url . ',' . $this->settings['baseUrl']; $this->settings['rewrite_rules'] .= PHP_EOL . $this->getProtocolRelativeURL( $this->placeholder_url ) . ',' . $this->getProtocolRelativeURL( $this->settings['baseUrl'] ); $rewrite_from = array(); $rewrite_to = array(); $rewrite_rules = explode( "\n", str_replace( "\r", '', $this->settings['rewrite_rules'] ) ); foreach ( $rewrite_rules as $rewrite_rule_line ) { if ( $rewrite_rule_line ) { list($from, $to) = explode( ',', $rewrite_rule_line ); $rewrite_from[] = $from; $rewrite_to[] = $to; } } $rewritten_source = str_replace( $rewrite_from, $rewrite_to, $processed_txt ); return $rewritten_source; } public function rewriteEscapedURLs( $processed_txt ) { $processed_txt = str_replace( '%5C/', '\\/', $processed_txt ); $site_url = addcslashes( $this->placeholder_url, '/' ); $destination_url = addcslashes( $this->settings['baseUrl'], '/' ); if ( ! isset( $this->settings['rewrite_rules'] ) ) { $this->settings['rewrite_rules'] = ''; } $this->settings['rewrite_rules'] .= PHP_EOL . $site_url . ',' . $destination_url; $rewrite_from = array(); $rewrite_to = array(); $rewrite_rules = explode( "\n", str_replace( "\r", '', $this->settings['rewrite_rules'] ) ); foreach ( $rewrite_rules as $rewrite_rule_line ) { if ( $rewrite_rule_line ) { list($from, $to) = explode( ',', $rewrite_rule_line ); $rewrite_from[] = addcslashes( $from, '/' ); $rewrite_to[] = addcslashes( $to, '/' ); } } $rewritten_source = str_replace( $rewrite_from, $rewrite_to, $processed_txt ); return $rewritten_source; } public function rewriteSiteURLsToPlaceholder() { $patterns = array( $this->settings['wp_site_url'], $this->getProtocolRelativeURL( $this->settings['wp_site_url'] ), $this->getProtocolRelativeURL( rtrim( $this->settings['wp_site_url'], '/' ) ), $this->getProtocolRelativeURL( $this->settings['wp_site_url'] . '//' ), $this->getProtocolRelativeURL( addcslashes( $this->settings['wp_site_url'], '/' ) ), ); $replacements = array( $this->placeholder_url, $this->getProtocolRelativeURL( $this->placeholder_url ), $this->getProtocolRelativeURL( $this->placeholder_url ), $this->getProtocolRelativeURL( $this->placeholder_url . '/' ), $this->getProtocolRelativeURL( addcslashes( $this->placeholder_url, '/' ) ), ); if ( $this->destination_protocol === 'https' ) { $patterns[] = str_replace( 'http:', 'https:', $this->settings['wp_site_url'] ); $replacements[] = $this->placeholder_url; } $rewritten_source = str_replace( $patterns, $replacements, $this->txt_doc ); $this->txt_doc = $rewritten_source; } public function getTargetSiteProtocol( $url ) { $protocol = '//'; if ( strpos( $url, 'https://' ) !== false ) { $protocol = 'https://'; } elseif ( strpos( $url, 'http://' ) !== false ) { $protocol = 'http://'; } else { $protocol = '//'; } return $protocol; } public function getProtocolRelativeURL( $url ) { $this->destination_protocol_relative_url = str_replace( array( 'https:', 'http:', ), array( '', '', ), $url ); return $this->destination_protocol_relative_url; } }

View File

@ -0,0 +1,17 @@
<?php
class TemplateHelper { public function __construct() { } public function displayCheckbox( $tpl_vars, $field_name, $field_label ) { echo "
<fieldset>
<label for='{$field_name}'>
<input name='{$field_name}' id='{$field_name}' value='1' type='checkbox' " . ( $tpl_vars->options->{$field_name} === '1' ? 'checked' : '' ) . ' />
<span>' . __( $field_label, 'static-html-output-plugin' ) . '</span>
</label>
</fieldset>
'; } public function displayTextfield( $tpl_vars, $field_name, $field_label, $description, $type = 'text' ) { echo "
<input name='{$field_name}' class='regular-text' id='{$field_name}' type='{$type}' value='" . esc_attr( $tpl_vars->options->{$field_name} ) . "' placeholder='" . __( $field_label, 'static-html-output-plugin' ) . "' />
<span class='description'>$description</span>
<br>
"; } public function displaySelectMenu( $tpl_vars, $menu_options, $field_name, $field_label, $description, $type = 'text' ) { $menu_code = "
<select name='{$field_name}' id='{$field_name}'>
<option></option>"; foreach ( $menu_options as $value => $text ) { if ( $tpl_vars->options->{$field_name} === $value ) { $menu_code .= "
<option value='{$value}' selected>{$text}</option>"; } else { $menu_code .= "
<option value='{$value}'>{$text}</option>"; } } $menu_code .= '</select>'; echo $menu_code; } public function ifNotEmpty( $value, $substitute = '' ) { $value = $value ? $value : $substitute; echo $value; } }

View File

@ -0,0 +1,2 @@
<?php
class WP2Static_View { protected $variables = array(); protected $path = null; protected $directory = 'views'; protected $extension = '.phtml'; protected $template = null; public function __construct() { list($plugin_dir) = explode( '/', plugin_basename( __FILE__ ) ); $path_array = array( WP_PLUGIN_DIR, $plugin_dir, $this->directory ); $this->path = implode( '/', $path_array ); } public function setTemplate( $tpl ) { $this->template = $tpl; $this->variables = array(); return $this; } public function __set( $name, $value ) { $this->variables[ $name ] = $value; return $this; } public function assign( $name, $value ) { return $this->__set( $name, $value ); } public function __get( $name ) { $value = array_key_exists( $name, $this->variables ) ? $this->variables[ $name ] : null; return $value; } public function render() { $file = $this->path . '/' . $this->template . $this->extension; if ( ! is_readable( $file ) ) { error_log( 'Can\'t find view template: ' . $file ); } include $file; return $this; } public function fetch() { ob_start(); $this->render(); $contents = ob_get_contents(); ob_end_clean(); return $contents; } }

View File

@ -0,0 +1,2 @@
<?php
class WP2Static { public function loadSettings( $target_settings ) { $general_settings = array( 'general', ); $target_settings = array_merge( $general_settings, $target_settings ); if ( isset( $_POST['selected_deployment_option'] ) ) { require_once dirname( __FILE__ ) . '/PostSettings.php'; $this->settings = WPSHO_PostSettings::get( $target_settings ); } else { require_once dirname( __FILE__ ) . '/DBSettings.php'; $this->settings = WPSHO_DBSettings::get( $target_settings ); } } }

View File

@ -0,0 +1,2 @@
<?php
class WPSite { public function __construct() { $wp_upload_path_and_url = wp_upload_dir(); $this->uploads_url = $wp_upload_path_and_url['baseurl']; $this->site_url = get_home_url() . '/'; $this->parent_theme_url = get_template_directory_uri(); $this->wp_content_url = content_url(); $this->site_path = ABSPATH; $this->plugins_path = $this->getWPDirFullPath( 'plugins' ); $this->wp_uploads_path = $this->getWPDirFullPath( 'uploads' ); $this->wp_includes_path = $this->getWPDirFullPath( 'wp-includes' ); $this->wp_content_path = $this->getWPDirFullPath( 'wp-content' ); $this->theme_root_path = $this->getWPDirFullPath( 'theme-root' ); $this->parent_theme_path = $this->getWPDirFullPath( 'parent-theme' ); $this->child_theme_path = $this->getWPDirFullPath( 'child-theme' ); $this->child_theme_active = $this->parent_theme_path !== $this->child_theme_path; $this->permalink_structure = get_option( 'permalink_structure' ); $this->wp_inc = '/' . WPINC; $this->wp_content = WP_CONTENT_DIR; $this->wp_uploads = str_replace( ABSPATH, '/', $this->wp_uploads_path ); $this->wp_plugins = str_replace( ABSPATH, '/', WP_PLUGIN_DIR ); $this->wp_themes = str_replace( ABSPATH, '/', get_theme_root() ); $this->wp_active_theme = str_replace( home_url(), '', get_template_directory_uri() ); $this->detect_base_url(); $this->subdirectory = $this->isSiteInstalledInSubDirectory(); $this->wp_site_subdir = $this->subdirectory; $this->wp_site_url = $this->site_url; $this->wp_site_path = $this->site_path; $this->wp_uploads_url = $this->uploads_url; $this->uploads_writable = $this->uploadsPathIsWritable(); $this->permalinks_set = $this->permalinksAreDefined(); $this->curl_enabled = $this->hasCurlSupport(); } public function isSiteInstalledInSubDirectory() { $parsed_site_url = parse_url( rtrim( $this->site_url, '/' ) ); if ( isset( $parsed_site_url['path'] ) ) { return $parsed_site_url['path']; } return false; } public function uploadsPathIsWritable() { return $this->wp_uploads_path && is_writable( $this->wp_uploads_path ); } public function hasCurlSupport() { return extension_loaded( 'curl' ); } public function permalinksAreDefined() { return strlen( get_option( 'permalink_structure' ) ); } public function detect_base_url() { $site_url = get_option( 'siteurl' ); $home = get_option( 'home' ); } public function getWPDirFullPath( $wp_dir ) { $full_path = ''; switch ( $wp_dir ) { case 'wp-content': $full_path = WP_CONTENT_DIR; break; case 'uploads': $upload_dir_info = wp_upload_dir(); $full_path = $upload_dir_info['basedir']; break; case 'wp-includes': $full_path = ABSPATH . WPINC; break; case 'plugins': $full_path = WP_PLUGIN_DIR; break; case 'theme-root': $full_path = get_theme_root(); break; case 'parent-theme': $full_path = get_template_directory(); break; case 'child-theme': $full_path = get_stylesheet_directory(); break; } return rtrim( $full_path, '/' ); } public function getWPDirNameOnly( $wp_dir ) { $wp_dir_name = ''; switch ( $wp_dir ) { case 'child-theme': case 'parent-theme': case 'wp-content': case 'wp-includes': case 'uploads': case 'theme-root': case 'plugins': $wp_dir_name = $this->getLastPathSegment( $this->getWPDirFullPath( $wp_dir ) ); break; } return rtrim( $wp_dir_name, '/' ); } public function getLastPathSegment( $path ) { $path_segments = explode( '/', $path ); return end( $path_segments ); } public function getWPContentSubDirectory() { $parsed_url = parse_url( $this->parent_theme_url ); $path_segments = explode( '/', $parsed_url['path'] ); if ( count( $path_segments ) === 5 ) { return $path_segments[1] . '/'; } else { return ''; } } }

View File

@ -0,0 +1,2 @@
<?php
class WsLog { public static function l( $text ) { $target_settings = array( 'general', 'wpenv', ); $wp_uploads_path = ''; $settings = ''; if ( defined( 'WP_CLI' ) ) { require_once dirname( __FILE__ ) . '/DBSettings.php'; $settings = WPSHO_DBSettings::get( $target_settings ); } else { require_once dirname( __FILE__ ) . '/PostSettings.php'; $settings = WPSHO_PostSettings::get( $target_settings ); } if ( ! isset( $settings['debug_mode'] ) ) { return; } $wp_uploads_path = $settings['wp_uploads_path']; $log_file_path = $wp_uploads_path . '/WP-STATIC-EXPORT-LOG.txt'; file_put_contents( $log_file_path, $text . PHP_EOL, FILE_APPEND | LOCK_EX ); chmod( $log_file_path, 0664 ); } }