mirror of
https://ghproxy.net/https://github.com/elementor/wp2static-addon-github.git
synced 2025-10-03 23:29:25 +08:00
initial copy from s3
This commit is contained in:
commit
a80054c0be
10 changed files with 922 additions and 0 deletions
24
LICENSE.txt
Normal file
24
LICENSE.txt
Normal file
|
@ -0,0 +1,24 @@
|
|||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
distribute this software, either in source code form or as a compiled
|
||||
binary, for any purpose, commercial or non-commercial, and by any
|
||||
means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors
|
||||
of this software dedicate any and all copyright interest in the
|
||||
software to the public domain. We make this dedication for the benefit
|
||||
of the public at large and to the detriment of our heirs and
|
||||
successors. We intend this dedication to be an overt act of
|
||||
relinquishment in perpetuity of all present and future rights to this
|
||||
software under copyright law.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
For more information, please refer to <http://unlicense.org>
|
29
README.txt
Normal file
29
README.txt
Normal file
|
@ -0,0 +1,29 @@
|
|||
=== Plugin Name ===
|
||||
Contributors: leonstafford
|
||||
Donate link: https://leonstafford.github.io
|
||||
Tags: wp2static,s3,static
|
||||
Requires at least: 3.2
|
||||
Tested up to: 5.0.3
|
||||
Stable tag: 0.1
|
||||
License: Unlicense
|
||||
License URI: http://unlicense.org
|
||||
|
||||
Adds AWS S3 as a deployment option for WP2Static.
|
||||
|
||||
== Description ==
|
||||
|
||||
Take advantage of the S3 and optionally CloudFront to host your WordPress
|
||||
powered static website.
|
||||
|
||||
== Installation ==
|
||||
|
||||
Upload the ZIP to your WordPress plugins page within your dashboard.
|
||||
|
||||
Activate the plugin, then navigate to your WP2Static main plugin page to see
|
||||
the new deployment option available.
|
||||
|
||||
== Changelog ==
|
||||
|
||||
= 0.1 =
|
||||
|
||||
First release
|
418
S3Deployer.php
Executable file
418
S3Deployer.php
Executable file
|
@ -0,0 +1,418 @@
|
|||
<?php
|
||||
|
||||
class WP2Static_S3 extends WP2Static_SitePublisher {
|
||||
|
||||
public function __construct() {
|
||||
// calling outside WP chain, need to specify this
|
||||
// Add-on's option keys
|
||||
$deploy_keys = array(
|
||||
's3',
|
||||
array(
|
||||
'baseUrl-s3',
|
||||
'cfDistributionId',
|
||||
's3Bucket',
|
||||
's3Key',
|
||||
's3Region',
|
||||
's3RemotePath',
|
||||
's3Secret',
|
||||
),
|
||||
);
|
||||
|
||||
$this->loadSettings( 's3', $deploy_keys );
|
||||
|
||||
$this->previous_hashes_path =
|
||||
$this->settings['wp_uploads_path'] .
|
||||
'/WP2STATIC-S3-PREVIOUS-HASHES.txt';
|
||||
|
||||
if ( defined( 'WP_CLI' ) ) {
|
||||
return; }
|
||||
|
||||
switch ( $_POST['ajax_action'] ) {
|
||||
case 'test_s3':
|
||||
$this->test_s3();
|
||||
break;
|
||||
case 's3_prepare_export':
|
||||
$this->bootstrap();
|
||||
$this->loadArchive();
|
||||
$this->prepareDeploy();
|
||||
break;
|
||||
case 's3_transfer_files':
|
||||
$this->bootstrap();
|
||||
$this->loadArchive();
|
||||
$this->upload_files();
|
||||
break;
|
||||
case 'cloudfront_invalidate_all_items':
|
||||
$this->cloudfront_invalidate_all_items();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public function upload_files() {
|
||||
$this->files_remaining = $this->getRemainingItemsCount();
|
||||
|
||||
if ( $this->files_remaining < 0 ) {
|
||||
echo 'ERROR';
|
||||
die(); }
|
||||
|
||||
$batch_size = $this->settings['deployBatchSize'];
|
||||
|
||||
if ( $batch_size > $this->files_remaining ) {
|
||||
$batch_size = $this->files_remaining;
|
||||
}
|
||||
|
||||
$lines = $this->getItemsToDeploy( $batch_size );
|
||||
|
||||
$this->openPreviousHashesFile();
|
||||
|
||||
require_once dirname( __FILE__ ) .
|
||||
'/../static-html-output-plugin' .
|
||||
'/plugin/WP2Static/MimeTypes.php';
|
||||
|
||||
foreach ( $lines as $line ) {
|
||||
list($local_file, $this->target_path) = explode( ',', $line );
|
||||
|
||||
$local_file = $this->archive->path . $local_file;
|
||||
|
||||
if ( ! is_file( $local_file ) ) {
|
||||
continue; }
|
||||
|
||||
if ( isset( $this->settings['s3RemotePath'] ) ) {
|
||||
$this->target_path =
|
||||
$this->settings['s3RemotePath'] . '/' . $this->target_path;
|
||||
}
|
||||
|
||||
$this->logAction(
|
||||
"Uploading {$local_file} to {$this->target_path} in S3"
|
||||
);
|
||||
|
||||
$this->local_file_contents = file_get_contents( $local_file );
|
||||
|
||||
$this->hash_key = $this->target_path . basename( $local_file );
|
||||
|
||||
if ( isset( $this->file_paths_and_hashes[ $this->hash_key ] ) ) {
|
||||
$prev = $this->file_paths_and_hashes[ $this->hash_key ];
|
||||
$current = crc32( $this->local_file_contents );
|
||||
|
||||
if ( $prev != $current ) {
|
||||
$this->logAction(
|
||||
"{$this->hash_key} differs from previous deploy cache "
|
||||
);
|
||||
|
||||
try {
|
||||
$this->put_s3_object(
|
||||
$this->target_path .
|
||||
basename( $local_file ),
|
||||
$this->local_file_contents,
|
||||
GuessMimeType( $local_file )
|
||||
);
|
||||
|
||||
} catch ( Exception $e ) {
|
||||
$this->handleException( $e );
|
||||
}
|
||||
} else {
|
||||
$this->logAction(
|
||||
"Skipping {$this->hash_key} as identical " .
|
||||
'to deploy cache'
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$this->logAction(
|
||||
"{$this->hash_key} not found in deploy cache "
|
||||
);
|
||||
|
||||
try {
|
||||
$this->put_s3_object(
|
||||
$this->target_path .
|
||||
basename( $local_file ),
|
||||
$this->local_file_contents,
|
||||
GuessMimeType( $local_file )
|
||||
);
|
||||
|
||||
} catch ( Exception $e ) {
|
||||
$this->handleException( $e );
|
||||
}
|
||||
}
|
||||
|
||||
$this->recordFilePathAndHashInMemory(
|
||||
$this->hash_key,
|
||||
$this->local_file_contents
|
||||
);
|
||||
}
|
||||
|
||||
$this->writeFilePathAndHashesToFile();
|
||||
|
||||
$this->pauseBetweenAPICalls();
|
||||
|
||||
if ( $this->uploadsCompleted() ) {
|
||||
$this->finalizeDeployment();
|
||||
}
|
||||
}
|
||||
|
||||
public function test_s3() {
|
||||
try {
|
||||
$this->put_s3_object(
|
||||
'.tmp_wp2static.txt',
|
||||
'Test WP2Static connectivity',
|
||||
'text/plain'
|
||||
);
|
||||
|
||||
if ( ! defined( 'WP_CLI' ) ) {
|
||||
echo 'SUCCESS';
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
require_once dirname( __FILE__ ) .
|
||||
'/../static-html-output-plugin' .
|
||||
'/plugin/WP2Static/WsLog.php';
|
||||
|
||||
WsLog::l( 'S3 ERROR RETURNED: ' . $e );
|
||||
echo "There was an error testing S3.\n";
|
||||
}
|
||||
}
|
||||
|
||||
public function put_s3_object( $s3_path, $content, $content_type ) {
|
||||
// NOTE: quick fix for #287
|
||||
$s3_path = str_replace( '@', '%40', $s3_path );
|
||||
|
||||
$this->logAction( "PUT'ing file to {$s3_path} in S3" );
|
||||
|
||||
$host_name = $this->settings['s3Bucket'] . '.s3.' .
|
||||
$this->settings['s3Region'] . '.amazonaws.com';
|
||||
|
||||
$this->logAction( "Using S3 Endpoint {$host_name}" );
|
||||
|
||||
//$content_acl = 'public-read';
|
||||
$content_title = $s3_path;
|
||||
$aws_service_name = 's3';
|
||||
$timestamp = gmdate( 'Ymd\THis\Z' );
|
||||
$date = gmdate( 'Ymd' );
|
||||
|
||||
// HTTP request headers as key & value
|
||||
$request_headers = array();
|
||||
$request_headers['Content-Type'] = $content_type;
|
||||
$request_headers['Date'] = $timestamp;
|
||||
$request_headers['Host'] = $host_name;
|
||||
//$request_headers['x-amz-acl'] = $content_acl;
|
||||
$request_headers['x-amz-content-sha256'] = hash( 'sha256', $content );
|
||||
|
||||
if ( ! empty( $this->settings[ 's3CacheControl' ] ) ) {
|
||||
$max_age = $this->settings[ 's3CacheControl' ];
|
||||
$request_headers['Cache-Control'] = 'max-age=' . $max_age;
|
||||
}
|
||||
|
||||
// Sort it in ascending order
|
||||
ksort( $request_headers );
|
||||
|
||||
$canonical_headers = array();
|
||||
|
||||
foreach ( $request_headers as $key => $value ) {
|
||||
$canonical_headers[] = strtolower( $key ) . ':' . $value;
|
||||
}
|
||||
|
||||
$canonical_headers = implode( "\n", $canonical_headers );
|
||||
|
||||
$signed_headers = array();
|
||||
|
||||
foreach ( $request_headers as $key => $value ) {
|
||||
$signed_headers[] = strtolower( $key );
|
||||
}
|
||||
|
||||
$signed_headers = implode( ';', $signed_headers );
|
||||
|
||||
$canonical_request = array();
|
||||
$canonical_request[] = 'PUT';
|
||||
$canonical_request[] = '/' . $content_title;
|
||||
$canonical_request[] = '';
|
||||
$canonical_request[] = $canonical_headers;
|
||||
$canonical_request[] = '';
|
||||
$canonical_request[] = $signed_headers;
|
||||
$canonical_request[] = hash( 'sha256', $content );
|
||||
$canonical_request = implode( "\n", $canonical_request );
|
||||
$hashed_canonical_request = hash( 'sha256', $canonical_request );
|
||||
|
||||
$scope = array();
|
||||
$scope[] = $date;
|
||||
$scope[] = $this->settings['s3Region'];
|
||||
$scope[] = $aws_service_name;
|
||||
$scope[] = 'aws4_request';
|
||||
|
||||
$string_to_sign = array();
|
||||
$string_to_sign[] = 'AWS4-HMAC-SHA256';
|
||||
$string_to_sign[] = $timestamp;
|
||||
$string_to_sign[] = implode( '/', $scope );
|
||||
$string_to_sign[] = $hashed_canonical_request;
|
||||
$string_to_sign = implode( "\n", $string_to_sign );
|
||||
|
||||
// Signing key
|
||||
$k_secret = 'AWS4' . $this->settings['s3Secret'];
|
||||
$k_date = hash_hmac( 'sha256', $date, $k_secret, true );
|
||||
$k_region =
|
||||
hash_hmac( 'sha256', $this->settings['s3Region'], $k_date, true );
|
||||
$k_service = hash_hmac( 'sha256', $aws_service_name, $k_region, true );
|
||||
$k_signing = hash_hmac( 'sha256', 'aws4_request', $k_service, true );
|
||||
|
||||
$signature = hash_hmac( 'sha256', $string_to_sign, $k_signing );
|
||||
|
||||
$authorization = [
|
||||
'Credential=' . $this->settings['s3Key'] . '/' .
|
||||
implode( '/', $scope ),
|
||||
'SignedHeaders=' . $signed_headers,
|
||||
'Signature=' . $signature,
|
||||
];
|
||||
|
||||
$authorization =
|
||||
'AWS4-HMAC-SHA256' . ' ' . implode( ',', $authorization );
|
||||
|
||||
$curl_headers = [ 'Authorization: ' . $authorization ];
|
||||
|
||||
foreach ( $request_headers as $key => $value ) {
|
||||
$curl_headers[] = $key . ': ' . $value;
|
||||
}
|
||||
|
||||
$url = 'http://' . $host_name . '/' . $content_title;
|
||||
|
||||
$this->logAction( "S3 URL: {$url}" );
|
||||
|
||||
$ch = curl_init( $url );
|
||||
|
||||
curl_setopt( $ch, CURLOPT_HEADER, false );
|
||||
curl_setopt( $ch, CURLOPT_HTTPHEADER, $curl_headers );
|
||||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
|
||||
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
|
||||
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
|
||||
// curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
|
||||
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 0 );
|
||||
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
|
||||
curl_setopt( $ch, CURLOPT_USERAGENT, 'WP2Static.com' );
|
||||
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
|
||||
curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
|
||||
curl_setopt( $ch, CURLOPT_TIMEOUT, 30 );
|
||||
curl_setopt( $ch, CURLOPT_POSTFIELDS, $content );
|
||||
|
||||
$output = curl_exec( $ch );
|
||||
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
|
||||
|
||||
if ( ! $output ) {
|
||||
$this->logAction( "No response from API request, printing cURL error" );
|
||||
$response = curl_error( $ch );
|
||||
$this->logAction( stripslashes( $response ) );
|
||||
|
||||
throw new Exception(
|
||||
'No response from API request, check Debug Log'
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! $http_code ) {
|
||||
$this->logAction( "No response code from API, printing cURL info" );
|
||||
$this->logAction( print_r( curl_getinfo( $ch ), true ) );
|
||||
|
||||
throw new Exception(
|
||||
'No response code from API, check Debug Log'
|
||||
);
|
||||
}
|
||||
|
||||
$this->logAction( "API response code: {$http_code}" );
|
||||
$this->logAction( "API response body: {$output}" );
|
||||
|
||||
// TODO: pass $ch to checkForValidResponses
|
||||
$this->checkForValidResponses(
|
||||
$http_code,
|
||||
array( '100', '200' )
|
||||
);
|
||||
|
||||
curl_close( $ch );
|
||||
}
|
||||
|
||||
public function cloudfront_invalidate_all_items() {
|
||||
$this->logAction( 'Invalidating all CloudFront items' );
|
||||
|
||||
if ( ! isset( $this->settings['cfDistributionId'] ) ) {
|
||||
$this->logAction(
|
||||
'No CloudFront distribution ID set, skipping invalidation'
|
||||
);
|
||||
|
||||
if ( ! defined( 'WP_CLI' ) ) {
|
||||
echo 'SUCCESS'; }
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$distribution = $this->settings['cfDistributionId'];
|
||||
$access_key = $this->settings['s3Key'];
|
||||
$secret_key = $this->settings['s3Secret'];
|
||||
|
||||
$epoch = date( 'U' );
|
||||
|
||||
$xml = <<<EOD
|
||||
<InvalidationBatch>
|
||||
<Path>/*</Path>
|
||||
<CallerReference>{$distribution}{$epoch}</CallerReference>
|
||||
</InvalidationBatch>
|
||||
EOD;
|
||||
|
||||
$len = strlen( $xml );
|
||||
$date = gmdate( 'D, d M Y G:i:s T' );
|
||||
$sig = base64_encode(
|
||||
hash_hmac( 'sha1', $date, $secret_key, true )
|
||||
);
|
||||
$msg = 'POST /2010-11-01/distribution/';
|
||||
$msg .= "{$distribution}/invalidation HTTP/1.0\r\n";
|
||||
$msg .= "Host: cloudfront.amazonaws.com\r\n";
|
||||
$msg .= "Date: {$date}\r\n";
|
||||
$msg .= "Content-Type: text/xml; charset=UTF-8\r\n";
|
||||
$msg .= "Authorization: AWS {$access_key}:{$sig}\r\n";
|
||||
$msg .= "Content-Length: {$len}\r\n\r\n";
|
||||
$msg .= $xml;
|
||||
|
||||
$context = stream_context_create([
|
||||
'ssl' => [
|
||||
'verify_peer' => false,
|
||||
'verify_peer_name' => false
|
||||
]
|
||||
]);
|
||||
|
||||
$hostname = 'ssl://cloudfront.amazonaws.com:443';
|
||||
$fp = stream_socket_client(
|
||||
$hostname,
|
||||
$errno,
|
||||
$errstr,
|
||||
ini_get("default_socket_timeout"),
|
||||
STREAM_CLIENT_CONNECT,
|
||||
$context
|
||||
);
|
||||
|
||||
|
||||
//$fp = fsockopen(
|
||||
// 'ssl://cloudfront.amazonaws.com',
|
||||
// 443,
|
||||
// $errno,
|
||||
// $errstr,
|
||||
// 30
|
||||
//);
|
||||
|
||||
if ( ! $fp ) {
|
||||
require_once dirname( __FILE__ ) .
|
||||
'/../static-html-output-plugin' .
|
||||
'/plugin/WP2Static/WsLog.php';
|
||||
|
||||
WsLog::l( "CLOUDFRONT CONNECTION ERROR: {$errno} {$errstr}" );
|
||||
die( "Connection failed: {$errno} {$errstr}\n" );
|
||||
}
|
||||
|
||||
fwrite( $fp, $msg );
|
||||
$resp = '';
|
||||
|
||||
while ( ! feof( $fp ) ) {
|
||||
$resp .= fgets( $fp, 1024 );
|
||||
}
|
||||
|
||||
$this->logAction( "CloudFront response body: {$resp}" );
|
||||
|
||||
fclose( $fp );
|
||||
|
||||
if ( ! defined( 'WP_CLI' ) ) {
|
||||
echo 'SUCCESS';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$s3 = new WP2Static_S3();
|
17
admin/class-wp2static-addon-s3-admin.php
Normal file
17
admin/class-wp2static-addon-s3-admin.php
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
class Wp2static_Addon_S3_Admin {
|
||||
|
||||
private $plugin_name;
|
||||
private $version;
|
||||
|
||||
public function __construct( $plugin_name, $version ) {
|
||||
$this->plugin_name = $plugin_name;
|
||||
$this->version = $version;
|
||||
|
||||
}
|
||||
|
||||
public function enqueue_scripts() {
|
||||
wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wp2static-addon-s3-admin.js', array( 'jquery' ), $this->version, false );
|
||||
}
|
||||
}
|
24
admin/js/wp2static-addon-s3-admin.js
Normal file
24
admin/js/wp2static-addon-s3-admin.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
(function( $ ) {
|
||||
'use strict';
|
||||
|
||||
$(function() {
|
||||
deploy_options['s3'] = {
|
||||
exportSteps: [
|
||||
's3_prepare_export',
|
||||
's3_transfer_files',
|
||||
'cloudfront_invalidate_all_items',
|
||||
'finalize_deployment'
|
||||
],
|
||||
required_fields: {
|
||||
s3Key: 'Please input an S3 Key in order to authenticate when using the S3 deployment method.',
|
||||
s3Secret: 'Please input an S3 Secret in order to authenticate when using the S3 deployment method.',
|
||||
s3Bucket: 'Please input the name of the S3 bucket you are trying to deploy to.',
|
||||
}
|
||||
};
|
||||
|
||||
status_descriptions['s3_prepare_export'] = 'Preparing files for S3 deployment';
|
||||
status_descriptions['s3_transfer_files'] = 'Deploying files to S3';
|
||||
status_descriptions['cloudfront_invalidate_all_items'] = 'Invalidating CloudFront cache';
|
||||
}); // end DOM ready
|
||||
|
||||
})( jQuery );
|
43
includes/class-wp2static-addon-s3-loader.php
Normal file
43
includes/class-wp2static-addon-s3-loader.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
class Wp2static_Addon_S3_Loader {
|
||||
|
||||
protected $actions;
|
||||
protected $filters;
|
||||
|
||||
public function __construct() {
|
||||
$this->actions = array();
|
||||
$this->filters = array();
|
||||
|
||||
}
|
||||
|
||||
public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
|
||||
$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
|
||||
}
|
||||
|
||||
public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
|
||||
$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
|
||||
}
|
||||
|
||||
private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
|
||||
$hooks[] = array(
|
||||
'hook' => $hook,
|
||||
'component' => $component,
|
||||
'callback' => $callback,
|
||||
'priority' => $priority,
|
||||
'accepted_args' => $accepted_args
|
||||
);
|
||||
|
||||
return $hooks;
|
||||
}
|
||||
|
||||
public function run() {
|
||||
foreach ( $this->filters as $hook ) {
|
||||
add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
|
||||
}
|
||||
|
||||
foreach ( $this->actions as $hook ) {
|
||||
add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
|
||||
}
|
||||
}
|
||||
}
|
142
includes/class-wp2static-addon-s3.php
Normal file
142
includes/class-wp2static-addon-s3.php
Normal file
|
@ -0,0 +1,142 @@
|
|||
<?php
|
||||
|
||||
class Wp2static_Addon_S3 {
|
||||
|
||||
protected $loader;
|
||||
protected $plugin_name;
|
||||
protected $version;
|
||||
|
||||
public function __construct() {
|
||||
if ( defined( 'PLUGIN_NAME_VERSION' ) ) {
|
||||
$this->version = PLUGIN_NAME_VERSION;
|
||||
} else {
|
||||
$this->version = '1.0.0';
|
||||
}
|
||||
$this->plugin_name = 'wp2static-addon-s3';
|
||||
|
||||
$this->load_dependencies();
|
||||
$this->define_admin_hooks();
|
||||
}
|
||||
|
||||
private function load_dependencies() {
|
||||
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wp2static-addon-s3-loader.php';
|
||||
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wp2static-addon-s3-admin.php';
|
||||
|
||||
$this->loader = new Wp2static_Addon_S3_Loader();
|
||||
}
|
||||
|
||||
private function define_admin_hooks() {
|
||||
$plugin_admin = new Wp2static_Addon_S3_Admin( $this->get_plugin_name(), $this->get_version() );
|
||||
|
||||
if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'wp2static')) {
|
||||
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
|
||||
}
|
||||
}
|
||||
|
||||
public function add_deployment_option_to_ui( $deploy_options ) {
|
||||
$deploy_options['s3'] = array('Amazon S3');
|
||||
|
||||
return $deploy_options;
|
||||
}
|
||||
|
||||
public function load_deployment_option_template( $templates ) {
|
||||
$templates[] = __DIR__ . '/../views/s3_settings_block.phtml';
|
||||
|
||||
return $templates;
|
||||
}
|
||||
|
||||
public function add_deployment_option_keys( $keys ) {
|
||||
$new_keys = array(
|
||||
'baseUrl-s3',
|
||||
'cfDistributionId',
|
||||
's3Bucket',
|
||||
's3CacheControl',
|
||||
's3Key',
|
||||
's3Region',
|
||||
's3RemotePath',
|
||||
's3Secret',
|
||||
);
|
||||
|
||||
$keys = array_merge(
|
||||
$keys,
|
||||
$new_keys
|
||||
);
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
public function whitelist_deployment_option_keys( $keys ) {
|
||||
$whitelist_keys = array(
|
||||
'baseUrl-s3',
|
||||
'cfDistributionId',
|
||||
's3Bucket',
|
||||
's3CacheControl',
|
||||
's3Key',
|
||||
's3Region',
|
||||
's3RemotePath',
|
||||
);
|
||||
|
||||
$keys = array_merge(
|
||||
$keys,
|
||||
$whitelist_keys
|
||||
);
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
public function add_post_and_db_keys( $keys ) {
|
||||
$keys['s3'] = array(
|
||||
'baseUrl-s3',
|
||||
'cfDistributionId',
|
||||
's3Bucket',
|
||||
's3CacheControl',
|
||||
's3Key',
|
||||
's3Region',
|
||||
's3RemotePath',
|
||||
's3Secret',
|
||||
);
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
public function run() {
|
||||
$this->loader->run();
|
||||
|
||||
add_filter(
|
||||
'wp2static_add_deployment_method_option_to_ui',
|
||||
[$this, 'add_deployment_option_to_ui']
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'wp2static_load_deploy_option_template',
|
||||
[$this, 'load_deployment_option_template']
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'wp2static_add_option_keys',
|
||||
[$this, 'add_deployment_option_keys']
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'wp2static_whitelist_option_keys',
|
||||
[$this, 'whitelist_deployment_option_keys']
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'wp2static_add_post_and_db_keys',
|
||||
[$this, 'add_post_and_db_keys']
|
||||
);
|
||||
}
|
||||
|
||||
public function get_plugin_name() {
|
||||
return $this->plugin_name;
|
||||
}
|
||||
|
||||
public function get_loader() {
|
||||
return $this->loader;
|
||||
}
|
||||
|
||||
public function get_version() {
|
||||
return $this->version;
|
||||
}
|
||||
}
|
31
uninstall.php
Normal file
31
uninstall.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Fired when the plugin is uninstalled.
|
||||
*
|
||||
* When populating this file, consider the following flow
|
||||
* of control:
|
||||
*
|
||||
* - This method should be static
|
||||
* - Check if the $_REQUEST content actually is the plugin name
|
||||
* - Run an admin referrer check to make sure it goes through authentication
|
||||
* - Verify the output of $_GET makes sense
|
||||
* - Repeat with other user roles. Best directly by using the links/query string parameters.
|
||||
* - Repeat things for multisite. Once for a single site in the network, once sitewide.
|
||||
*
|
||||
* This file may be updated more in future version of the Boilerplate; however, this is the
|
||||
* general skeleton and outline for how the file should work.
|
||||
*
|
||||
* For more information, see the following discussion:
|
||||
* https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate/pull/123#issuecomment-28541913
|
||||
*
|
||||
* @link https://leonstafford.github.io
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package Wp2static_Addon_S3
|
||||
*/
|
||||
|
||||
// If uninstall not called from WordPress, then exit.
|
||||
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
|
||||
exit;
|
||||
}
|
122
views/s3_settings_block.phtml
Executable file
122
views/s3_settings_block.phtml
Executable file
|
@ -0,0 +1,122 @@
|
|||
<div class="s3_settings_block" style="display:none;">
|
||||
|
||||
<table class="form-table">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="baseUrl-s3"><?php echo __('Destination URL', 'static-html-output-plugin');?></label>
|
||||
</th>
|
||||
<td>
|
||||
<?php $tpl->displayTextfield($this, 'baseUrl-s3', 'http://mystaticsite.com', '', ''); ?><br>
|
||||
|
||||
<p><i>Set this to the URL your visitors will use to access your site. For an S3 hosted website, you have a few options which will influence what you put in this field. </i></p>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="s3Key"><?php echo __('Access Key ID', 'static-html-output-plugin');?></label>
|
||||
</th>
|
||||
<td>
|
||||
<?php $tpl->displayTextfield($this, 's3Key', 'Access Key ID', 'ie, AKIAIOSFODNN7EXAMPLE'); ?><br>
|
||||
|
||||
<p><i>Your S3 user will need permissions to put objects into the bucket. Check that the user whose Key you are using has the correct permissions for S3. You may attach the 'AmazonS3FullAccess' to the user or give more fine grained permissions control via <a href="https://aws.amazon.com/iam/" target="_blank">AWS's IAM</a>.</i></p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="s3Key"><?php echo __('Secret Access Key', 'static-html-output-plugin');?></label>
|
||||
</th>
|
||||
<td>
|
||||
<?php $tpl->displayTextfield($this, 's3Secret', 'Secret Access Key', 'ie, wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY', 'password'); ?>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="s3Region"><?php echo __('Region', 'static-html-output-plugin');?></label>
|
||||
</th>
|
||||
<td>
|
||||
<?php
|
||||
|
||||
// TODO: shift to S3 model/helper
|
||||
$s3_regions = array(
|
||||
"us-east-1" => "US East (N. Virginia)",
|
||||
"us-east-2" => "US East (Ohio)",
|
||||
"us-west-1" => "US West (N. California)",
|
||||
"us-west-2" => "US West (Oregon)",
|
||||
"ca-central-1" => "Canada (Central)",
|
||||
"ap-south-1" => "Asia Pacific (Mumbai)",
|
||||
"ap-northeast-2" => "Asia Pacific (Seoul)",
|
||||
"ap-southeast-1" => "Asia Pacific (Singapore)",
|
||||
"ap-southeast-2" => "Asia Pacific (Sydney)",
|
||||
"ap-northeast-1" => "Asia Pacific (Tokyo)",
|
||||
"ap-northeast-3" => "Asia Pacific (Osaka-Local)",
|
||||
"eu-central-1" => "EU (Frankfurt)",
|
||||
"eu-west-1" => "EU (Ireland)",
|
||||
"eu-west-2" => "EU (London)",
|
||||
"eu-west-3" => "EU (Paris)",
|
||||
"eu-north-1" => "EU (Stockholm)",
|
||||
"sa-east-1" => "South America (São Paulo)",
|
||||
"cn-north-1" => "China (Beijing)",
|
||||
"cn-northwest-1" => "China (Ningxia)",
|
||||
);
|
||||
?>
|
||||
|
||||
<?php $tpl->displaySelectMenu($this, $s3_regions, 's3Region', 'Region', 'ie, my-static-site'); ?><br>
|
||||
|
||||
<span class="description">choose the AWS region your bucket was created in</span>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="s3Bucket"><?php echo __('Bucket', 'static-html-output-plugin');?></label>
|
||||
</th>
|
||||
<td>
|
||||
<?php $tpl->displayTextfield($this, 's3Bucket', 'S3 Bucket', 'ie, my-static-site'); ?><br>
|
||||
|
||||
<p><i>Your bucket name as it appears in your <a href="https://s3.console.aws.amazon.com/s3/home" target="_blank">AWS Console for S3</a>.</i></p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="s3RemotePath"><?php echo __('Subdirectory', 'static-html-output-plugin');?></label>
|
||||
</th>
|
||||
<td>
|
||||
<?php $tpl->displayTextfield($this, 's3RemotePath', 'Subfolder in your bucket', 'ie, static-website'); ?><br>
|
||||
|
||||
<p><i>In case you want to put your site in a sub directory of a bucket, this will deploy all the static website files into the folder name you specify here.</i></p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="s3CacheControl"><?php echo __('Cache-Control', 'static-html-output-plugin');?></label>
|
||||
</th>
|
||||
<td>
|
||||
<?php $tpl->displayTextfield($this, 's3CacheControl', 'Cache-Control', 'ie, 86400'); ?><br>
|
||||
|
||||
<p><i>Sets the cache-control max-age value applied to S3 objects. If you are using CloudFront it will use this value to determine how often to refresh the CDN for a particular URL. Reduces CDN Transfer costs. You will need to redeploy the entire site if this value is changed.</a>.</i></p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="cfDistributionId"><?php echo __('CloudFront Cache Invalidation', 'static-html-output-plugin');?></label>
|
||||
</th>
|
||||
<td>
|
||||
<?php $tpl->displayTextfield($this, 'cfDistributionId', 'CloudFront Distribution ID', 'ie, ABC123DEFX'); ?><br>
|
||||
|
||||
<p><i>If using CloudFront in your S3 static website setup, enter the CloudFront Distribution ID here and it will create an invalidation request for all files at the end of the deployment process. The invalidation usually happens within a few minutes. You can check any pending invalidations in your <a href="https://console.aws.amazon.com/cloudfront/home" target="_blank">AWS Console's CloudFront page</a>. You AWS user will need to have the CloudFrontFullAccess permissions or a more controlled policy, that includes the ability to send CloudFront invalidation requests.</i></p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="s3_test"><?php echo __('Test S3 Settings', 'static-html-output-plugin');?></label>
|
||||
</th>
|
||||
<td>
|
||||
<button id="s3-test-button" type="button" class="btn-primary button">Test S3 Settings</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<a href="https://docs.wp2static.com/en/deploying/deployment-options/amazon-s3/" target="_blank">Read the docs</a> for more on deploying your WordPress site to S3
|
||||
</div>
|
72
wp2static-addon-s3.php
Normal file
72
wp2static-addon-s3.php
Normal file
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Plugin Name: WP2Static Add-on: S3
|
||||
* Plugin URI: https://wp2static.com
|
||||
* Description: AWS S3 as a deployment option for WP2Static.
|
||||
* Version: 0.1
|
||||
* Author: Leon Stafford
|
||||
* Author URI: https://leonstafford.github.io
|
||||
* License: Unlicense
|
||||
* License URI: http://unlicense.org
|
||||
* Text Domain: wp2static-addon-s3
|
||||
* Domain Path: /languages
|
||||
*/
|
||||
|
||||
if ( ! defined( 'WPINC' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
$ajax_action = isset( $_POST['ajax_action'] ) ? $_POST['ajax_action'] : '';
|
||||
// @codingStandardsIgnoreEnd
|
||||
|
||||
$wp2static_core_dir =
|
||||
dirname( __FILE__ ) . '/../static-html-output-plugin';
|
||||
|
||||
$add_on_dir = dirname( __FILE__ );
|
||||
|
||||
// NOTE: bypass instantiating plugin for specific AJAX requests
|
||||
if ( $ajax_action == 'test_s3' ) {
|
||||
require_once $wp2static_core_dir .
|
||||
'/plugin/WP2Static/SitePublisher.php';
|
||||
require_once $add_on_dir . '/S3Deployer.php';
|
||||
|
||||
wp_die();
|
||||
return null;
|
||||
} elseif ( $ajax_action == 's3_prepare_export' ) {
|
||||
require_once $wp2static_core_dir .
|
||||
'/plugin/WP2Static/SitePublisher.php';
|
||||
require_once $add_on_dir . '/S3Deployer.php';
|
||||
|
||||
wp_die();
|
||||
return null;
|
||||
} elseif ( $ajax_action == 's3_transfer_files' ) {
|
||||
require_once $wp2static_core_dir .
|
||||
'/plugin/WP2Static/SitePublisher.php';
|
||||
require_once $add_on_dir . '/S3Deployer.php';
|
||||
|
||||
wp_die();
|
||||
return null;
|
||||
} elseif ( $ajax_action == 'cloudfront_invalidate_all_items' ) {
|
||||
require_once $wp2static_core_dir .
|
||||
'/plugin/WP2Static/SitePublisher.php';
|
||||
require_once $add_on_dir . '/S3Deployer.php';
|
||||
|
||||
wp_die();
|
||||
return null;
|
||||
}
|
||||
|
||||
define( 'PLUGIN_NAME_VERSION', '0.1' );
|
||||
|
||||
require plugin_dir_path( __FILE__ ) . 'includes/class-wp2static-addon-s3.php';
|
||||
|
||||
function run_wp2static_addon_s3() {
|
||||
|
||||
$plugin = new Wp2static_Addon_S3();
|
||||
$plugin->run();
|
||||
|
||||
}
|
||||
|
||||
run_wp2static_addon_s3();
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue