mirror of
https://ghproxy.net/https://github.com/elementor/wp2static-addon-netlify.git
synced 2025-10-04 04:21:51 +08:00
reinit from s3 addon
This commit is contained in:
parent
26305fa426
commit
625881c9eb
17 changed files with 1105 additions and 2180 deletions
0
LICENSE.txt
Executable file → Normal file
0
LICENSE.txt
Executable file → Normal file
8
README.txt
Executable file → Normal file
8
README.txt
Executable file → Normal file
|
@ -1,18 +1,18 @@
|
||||||
=== Plugin Name ===
|
=== Plugin Name ===
|
||||||
Contributors: leonstafford
|
Contributors: leonstafford
|
||||||
Donate link: https://leonstafford.netlify.io
|
Donate link: https://leonstafford.github.io
|
||||||
Tags: wp2static,netlify,static
|
Tags: wp2static,s3,static
|
||||||
Requires at least: 3.2
|
Requires at least: 3.2
|
||||||
Tested up to: 5.0.3
|
Tested up to: 5.0.3
|
||||||
Stable tag: 0.1
|
Stable tag: 0.1
|
||||||
License: Unlicense
|
License: Unlicense
|
||||||
License URI: http://unlicense.org
|
License URI: http://unlicense.org
|
||||||
|
|
||||||
Adds Netlify as a deployment option for WP2Static.
|
Adds AWS S3 as a deployment option for WP2Static.
|
||||||
|
|
||||||
== Description ==
|
== Description ==
|
||||||
|
|
||||||
Take advantage of the Netlify to host your WordPress
|
Take advantage of the S3 and optionally CloudFront to host your WordPress
|
||||||
powered static website.
|
powered static website.
|
||||||
|
|
||||||
== Installation ==
|
== Installation ==
|
||||||
|
|
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 );
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "wp2static/wp2static-addon-netlify",
|
"name": "wp2static/wp2static-addon-s3",
|
||||||
"description": "Netlify deployment Add-on for WP2Static.",
|
"description": "S3 deployment Add-on for WP2Static.",
|
||||||
"homepage": "https://wp2static.com",
|
"homepage": "https://wp2static.com",
|
||||||
"license": "UNLICENSE",
|
"license": "UNLICENSE",
|
||||||
"authors": [
|
"authors": [
|
||||||
|
@ -12,10 +12,10 @@
|
||||||
],
|
],
|
||||||
"type": "wordpress-plugin",
|
"type": "wordpress-plugin",
|
||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/WP2Static/wp2static-addon-netlify/issues",
|
"issues": "https://github.com/WP2Static/wp2static-addon-s3/issues",
|
||||||
"forum": "https://wp2static.com/community",
|
"forum": "https://wp2static.com/community",
|
||||||
"docs": "https://wp2static.com/documentation",
|
"docs": "https://wp2static.com/documentation",
|
||||||
"source": "https://github.com/WP2Static/wp2static-addon-netlify"
|
"source": "https://github.com/WP2Static/wp2static-addon-s3"
|
||||||
},
|
},
|
||||||
"repositories": [
|
"repositories": [
|
||||||
{
|
{
|
||||||
|
@ -24,7 +24,7 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.0"
|
"php": ">=7.2"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpstan/phpstan-shim": "^0.11.5",
|
"phpstan/phpstan-shim": "^0.11.5",
|
||||||
|
@ -37,13 +37,13 @@
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"WP2Static\\": "src/"
|
"WP2StaticS3\\": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"PHPStan\\WordPress\\": "tests/phpstan/",
|
"PHPStan\\WordPress\\": "tests/phpstan/",
|
||||||
"WP2Static\\": "src/"
|
"WP2StaticS3\\": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
|
@ -59,6 +59,7 @@
|
||||||
"phpstan": "php -d memory_limit=-1 ./vendor/bin/phpstan analyse",
|
"phpstan": "php -d memory_limit=-1 ./vendor/bin/phpstan analyse",
|
||||||
"phpcs": "vendor/bin/phpcs --standard=./tools/phpcs.xml --ignore=,*/tests/*,*/vendor/*,wp2static.php ./",
|
"phpcs": "vendor/bin/phpcs --standard=./tools/phpcs.xml --ignore=,*/tests/*,*/vendor/*,wp2static.php ./",
|
||||||
"phpunit": "vendor/bin/phpunit ./tests/unit/",
|
"phpunit": "vendor/bin/phpunit ./tests/unit/",
|
||||||
"lint": "vendor/bin/parallel-lint --exclude vendor ."
|
"lint": "vendor/bin/parallel-lint --exclude vendor .",
|
||||||
|
"build": "/bin/sh tools/build_release.sh"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
1789
composer.lock
generated
1789
composer.lock
generated
File diff suppressed because it is too large
Load diff
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,18 +0,0 @@
|
||||||
(function( $ ) {
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
$(function() {
|
|
||||||
deploy_options['netlify'] = {
|
|
||||||
exportSteps: [
|
|
||||||
'netlify_do_export',
|
|
||||||
'finalize_deployment'
|
|
||||||
],
|
|
||||||
required_fields: {
|
|
||||||
netlifyPersonalAccessToken: 'Please specify your Netlify personal access token in order to deploy to Netlify.',
|
|
||||||
netlifySiteID: 'Please specify the id of your Netlify site you want to deploy to.',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
status_descriptions['netlify_do_export'] = 'Deploying to Netlify';
|
|
||||||
}); // end DOM ready
|
|
||||||
|
|
||||||
})( jQuery );
|
|
452
src/Controller.php
Executable file
452
src/Controller.php
Executable file
|
@ -0,0 +1,452 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace WP2StaticS3;
|
||||||
|
|
||||||
|
class Controller {
|
||||||
|
const WP2STATIC_S3_VERSION = '0.1';
|
||||||
|
|
||||||
|
public function __construct() {}
|
||||||
|
|
||||||
|
public function run() {
|
||||||
|
// initialize options DB
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$table_name = $wpdb->prefix . 'wp2static_addon_s3_options';
|
||||||
|
|
||||||
|
$charset_collate = $wpdb->get_charset_collate();
|
||||||
|
|
||||||
|
$sql = "CREATE TABLE $table_name (
|
||||||
|
id mediumint(9) NOT NULL AUTO_INCREMENT,
|
||||||
|
name VARCHAR(255) NOT NULL,
|
||||||
|
value VARCHAR(255) NOT NULL,
|
||||||
|
label VARCHAR(255) NULL,
|
||||||
|
description VARCHAR(255) NULL,
|
||||||
|
PRIMARY KEY (id)
|
||||||
|
) $charset_collate;";
|
||||||
|
|
||||||
|
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||||
|
dbDelta( $sql );
|
||||||
|
|
||||||
|
// check for seed data
|
||||||
|
// if deployment_url option doesn't exist, create:
|
||||||
|
$options = $this->getOptions();
|
||||||
|
|
||||||
|
if ( ! isset( $options['s3Bucket'] ) ) {
|
||||||
|
$this->seedOptions();
|
||||||
|
}
|
||||||
|
|
||||||
|
add_filter( 'wp2static_add_menu_items', [ 'WP2StaticS3\Controller', 'addSubmenuPage' ] );
|
||||||
|
|
||||||
|
add_action(
|
||||||
|
'admin_post_wp2static_s3_save_options',
|
||||||
|
[ $this, 'saveOptionsFromUI' ],
|
||||||
|
15,
|
||||||
|
1);
|
||||||
|
|
||||||
|
add_action(
|
||||||
|
'wp2static_deploy',
|
||||||
|
[ $this, 'deploy' ],
|
||||||
|
15,
|
||||||
|
1);
|
||||||
|
|
||||||
|
add_action(
|
||||||
|
'wp2static_post_deploy_trigger',
|
||||||
|
[ 'WP2StaticS3\Deployer', 'cloudfront_invalidate' ],
|
||||||
|
15,
|
||||||
|
1);
|
||||||
|
|
||||||
|
// if ( defined( 'WP_CLI' ) ) {
|
||||||
|
// \WP_CLI::add_command(
|
||||||
|
// 'wp2static s3',
|
||||||
|
// [ 'WP2StaticS3\CLI', 's3' ]);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: is this needed? confirm slashing of destination URLs...
|
||||||
|
public function modifyWordPressSiteURL( $site_url ) {
|
||||||
|
return rtrim( $site_url, '/' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all add-on options
|
||||||
|
*
|
||||||
|
* @return mixed[] All options
|
||||||
|
*/
|
||||||
|
public static function getOptions() : array {
|
||||||
|
global $wpdb;
|
||||||
|
$options = [];
|
||||||
|
|
||||||
|
$table_name = $wpdb->prefix . 'wp2static_addon_s3_options';
|
||||||
|
|
||||||
|
$rows = $wpdb->get_results( "SELECT * FROM $table_name" );
|
||||||
|
|
||||||
|
foreach($rows as $row) {
|
||||||
|
$options[$row->name] = $row;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $options;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seed options
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static function seedOptions() : void {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$table_name = $wpdb->prefix . 'wp2static_addon_s3_options';
|
||||||
|
|
||||||
|
$query_string = "INSERT INTO $table_name (name, value, label, description) VALUES (%s, %s, %s, %s);";
|
||||||
|
$query = $wpdb->prepare(
|
||||||
|
$query_string,
|
||||||
|
'cfDistributionID',
|
||||||
|
'',
|
||||||
|
'CloudFront Distribution ID',
|
||||||
|
'If using CloudFront, set this to auto-invalidate cache');
|
||||||
|
|
||||||
|
$wpdb->query( $query );
|
||||||
|
|
||||||
|
$query = $wpdb->prepare(
|
||||||
|
$query_string,
|
||||||
|
's3Bucket',
|
||||||
|
'',
|
||||||
|
'Bucket name',
|
||||||
|
'');
|
||||||
|
|
||||||
|
$wpdb->query( $query );
|
||||||
|
|
||||||
|
$query = $wpdb->prepare(
|
||||||
|
$query_string,
|
||||||
|
's3AccessKeyID',
|
||||||
|
'',
|
||||||
|
'Access Key ID',
|
||||||
|
'');
|
||||||
|
|
||||||
|
$wpdb->query( $query );
|
||||||
|
|
||||||
|
$query = $wpdb->prepare(
|
||||||
|
$query_string,
|
||||||
|
's3SecretAccessKey',
|
||||||
|
'',
|
||||||
|
'Secret Access Key',
|
||||||
|
'');
|
||||||
|
|
||||||
|
$wpdb->query( $query );
|
||||||
|
|
||||||
|
$query = $wpdb->prepare(
|
||||||
|
$query_string,
|
||||||
|
'cfAccessKeyID',
|
||||||
|
'',
|
||||||
|
'Access Key ID',
|
||||||
|
'');
|
||||||
|
|
||||||
|
$wpdb->query( $query );
|
||||||
|
|
||||||
|
$query = $wpdb->prepare(
|
||||||
|
$query_string,
|
||||||
|
'cfSecretAccessKey',
|
||||||
|
'',
|
||||||
|
'Secret Access Key',
|
||||||
|
'');
|
||||||
|
|
||||||
|
$wpdb->query( $query );
|
||||||
|
|
||||||
|
|
||||||
|
$query = $wpdb->prepare(
|
||||||
|
$query_string,
|
||||||
|
's3Region',
|
||||||
|
'',
|
||||||
|
'Region',
|
||||||
|
'');
|
||||||
|
|
||||||
|
$wpdb->query( $query );
|
||||||
|
|
||||||
|
$query = $wpdb->prepare(
|
||||||
|
$query_string,
|
||||||
|
's3Profile',
|
||||||
|
'',
|
||||||
|
'Profile',
|
||||||
|
'');
|
||||||
|
|
||||||
|
$wpdb->query( $query );
|
||||||
|
|
||||||
|
$query = $wpdb->prepare(
|
||||||
|
$query_string,
|
||||||
|
'cfRegion',
|
||||||
|
'',
|
||||||
|
'Region',
|
||||||
|
'');
|
||||||
|
|
||||||
|
$wpdb->query( $query );
|
||||||
|
|
||||||
|
$query = $wpdb->prepare(
|
||||||
|
$query_string,
|
||||||
|
'cfProfile',
|
||||||
|
'',
|
||||||
|
'Profile',
|
||||||
|
'');
|
||||||
|
|
||||||
|
$wpdb->query( $query );
|
||||||
|
|
||||||
|
$query = $wpdb->prepare(
|
||||||
|
$query_string,
|
||||||
|
's3RemotePath',
|
||||||
|
'',
|
||||||
|
'Path prefix in bucket',
|
||||||
|
'Optionally, deploy to a subdirectory within bucket');
|
||||||
|
|
||||||
|
$wpdb->query( $query );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save options
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static function saveOption( $name, $value ) : void {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$table_name = $wpdb->prefix . 'wp2static_addon_s3_options';
|
||||||
|
|
||||||
|
$query_string = "INSERT INTO $table_name (name, value) VALUES (%s, %s);";
|
||||||
|
$query = $wpdb->prepare( $query_string, $name, $value );
|
||||||
|
|
||||||
|
$wpdb->query( $query );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function renderS3Page() : void {
|
||||||
|
$view = [];
|
||||||
|
$view['nonce_action'] = 'wp2static-s3-options';
|
||||||
|
$view['uploads_path'] = \WP2Static\SiteInfo::getPath('uploads');
|
||||||
|
$s3_path = \WP2Static\SiteInfo::getPath( 'uploads' ) . 'wp2static-processed-site.s3';
|
||||||
|
|
||||||
|
|
||||||
|
$view['options'] = self::getOptions();
|
||||||
|
|
||||||
|
$view['s3_url'] =
|
||||||
|
is_file( $s3_path ) ?
|
||||||
|
\WP2Static\SiteInfo::getUrl( 'uploads' ) . 'wp2static-processed-site.s3' : '#';
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../views/s3-page.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function deploy( $processed_site_path ) {
|
||||||
|
\WP2Static\WsLog::l('S3 Addon deploying');
|
||||||
|
|
||||||
|
$s3_deployer = new Deployer();
|
||||||
|
$s3_deployer->upload_files( $processed_site_path );
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Naive encypting/decrypting
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static function encrypt_decrypt($action, $string) {
|
||||||
|
$output = false;
|
||||||
|
$encrypt_method = "AES-256-CBC";
|
||||||
|
|
||||||
|
$secret_key =
|
||||||
|
defined( 'AUTH_KEY' ) ?
|
||||||
|
constant( 'AUTH_KEY' ) :
|
||||||
|
'LC>_cVZv34+W.P&_8d|ejfr]d31h)J?z5n(LB6iY=;P@?5/qzJSyB3qctr,.D$[L';
|
||||||
|
|
||||||
|
$secret_iv =
|
||||||
|
defined( 'AUTH_SALT' ) ?
|
||||||
|
constant( 'AUTH_SALT' ) :
|
||||||
|
'ec64SSHB{8|AA_ThIIlm:PD(Z!qga!/Dwll 4|i.?UkC§NNO}z?{Qr/q.KpH55K9';
|
||||||
|
|
||||||
|
$key = hash('sha256', $secret_key);
|
||||||
|
$variate = substr(hash('sha256', $secret_iv), 0, 16);
|
||||||
|
|
||||||
|
if ( $action == 'encrypt' ) {
|
||||||
|
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $variate);
|
||||||
|
$output = base64_encode($output);
|
||||||
|
} else if( $action == 'decrypt' ) {
|
||||||
|
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $variate);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function activate_for_single_site() : void {
|
||||||
|
error_log('activating WP2Static S3 Add-on');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function deactivate_for_single_site() : void {
|
||||||
|
error_log('deactivating WP2Static S3 Add-on, maintaining options');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function deactivate( bool $network_wide = null ) : void {
|
||||||
|
error_log('deactivating WP2Static S3 Add-on');
|
||||||
|
if ( $network_wide ) {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$query = 'SELECT blog_id FROM %s WHERE site_id = %d;';
|
||||||
|
|
||||||
|
$site_ids = $wpdb->get_col(
|
||||||
|
sprintf(
|
||||||
|
$query,
|
||||||
|
$wpdb->blogs,
|
||||||
|
$wpdb->siteid
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ( $site_ids as $site_id ) {
|
||||||
|
switch_to_blog( $site_id );
|
||||||
|
self::deactivate_for_single_site();
|
||||||
|
}
|
||||||
|
|
||||||
|
restore_current_blog();
|
||||||
|
} else {
|
||||||
|
self::deactivate_for_single_site();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function activate( bool $network_wide = null ) : void {
|
||||||
|
error_log('activating s3 addon');
|
||||||
|
if ( $network_wide ) {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$query = 'SELECT blog_id FROM %s WHERE site_id = %d;';
|
||||||
|
|
||||||
|
$site_ids = $wpdb->get_col(
|
||||||
|
sprintf(
|
||||||
|
$query,
|
||||||
|
$wpdb->blogs,
|
||||||
|
$wpdb->siteid
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ( $site_ids as $site_id ) {
|
||||||
|
switch_to_blog( $site_id );
|
||||||
|
self::activate_for_single_site();
|
||||||
|
}
|
||||||
|
|
||||||
|
restore_current_blog();
|
||||||
|
} else {
|
||||||
|
self::activate_for_single_site();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function addSubmenuPage( $submenu_pages ) {
|
||||||
|
$submenu_pages['s3'] = [ 'WP2StaticS3\Controller', 'renderS3Page' ];
|
||||||
|
|
||||||
|
return $submenu_pages;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function saveOptionsFromUI() {
|
||||||
|
check_admin_referer( 'wp2static-s3-options' );
|
||||||
|
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$table_name = $wpdb->prefix . 'wp2static_addon_s3_options';
|
||||||
|
|
||||||
|
$wpdb->update(
|
||||||
|
$table_name,
|
||||||
|
[ 'value' => sanitize_text_field( $_POST['cfDistributionID'] ) ],
|
||||||
|
[ 'name' => 'cfDistributionID' ]
|
||||||
|
);
|
||||||
|
|
||||||
|
$wpdb->update(
|
||||||
|
$table_name,
|
||||||
|
[ 'value' => sanitize_text_field( $_POST['s3Bucket'] ) ],
|
||||||
|
[ 'name' => 's3Bucket' ]
|
||||||
|
);
|
||||||
|
|
||||||
|
$wpdb->update(
|
||||||
|
$table_name,
|
||||||
|
[ 'value' => sanitize_text_field( $_POST['s3AccessKeyID'] ) ],
|
||||||
|
[ 'name' => 's3AccessKeyID' ]
|
||||||
|
);
|
||||||
|
|
||||||
|
$secret_access_key =
|
||||||
|
$_POST['s3SecretAccessKey'] ?
|
||||||
|
self::encrypt_decrypt(
|
||||||
|
'encrypt',
|
||||||
|
sanitize_text_field( $_POST['s3SecretAccessKey'] )
|
||||||
|
) : '';
|
||||||
|
|
||||||
|
$wpdb->update(
|
||||||
|
$table_name,
|
||||||
|
[ 'value' => $secret_access_key ],
|
||||||
|
[ 'name' => 's3SecretAccessKey' ]
|
||||||
|
);
|
||||||
|
|
||||||
|
$wpdb->update(
|
||||||
|
$table_name,
|
||||||
|
[ 'value' => sanitize_text_field( $_POST['cfAccessKeyID'] ) ],
|
||||||
|
[ 'name' => 'cfAccessKeyID' ]
|
||||||
|
);
|
||||||
|
|
||||||
|
$secret_access_key =
|
||||||
|
$_POST['cfSecretAccessKey'] ?
|
||||||
|
self::encrypt_decrypt(
|
||||||
|
'encrypt',
|
||||||
|
sanitize_text_field( $_POST['cfSecretAccessKey'] )
|
||||||
|
) : '';
|
||||||
|
|
||||||
|
$wpdb->update(
|
||||||
|
$table_name,
|
||||||
|
[ 'value' => $secret_access_key ],
|
||||||
|
[ 'name' => 'cfSecretAccessKey' ]
|
||||||
|
);
|
||||||
|
|
||||||
|
$wpdb->update(
|
||||||
|
$table_name,
|
||||||
|
[ 'value' => sanitize_text_field( $_POST['s3Region'] ) ],
|
||||||
|
[ 'name' => 's3Region' ]
|
||||||
|
);
|
||||||
|
|
||||||
|
$wpdb->update(
|
||||||
|
$table_name,
|
||||||
|
[ 'value' => sanitize_text_field( $_POST['cfRegion'] ) ],
|
||||||
|
[ 'name' => 'cfRegion' ]
|
||||||
|
);
|
||||||
|
|
||||||
|
$wpdb->update(
|
||||||
|
$table_name,
|
||||||
|
[ 'value' => sanitize_text_field( $_POST['s3Profile'] ) ],
|
||||||
|
[ 'name' => 's3Profile' ]
|
||||||
|
);
|
||||||
|
|
||||||
|
$wpdb->update(
|
||||||
|
$table_name,
|
||||||
|
[ 'value' => sanitize_text_field( $_POST['cfProfile'] ) ],
|
||||||
|
[ 'name' => 'cfProfile' ]
|
||||||
|
);
|
||||||
|
|
||||||
|
$wpdb->update(
|
||||||
|
$table_name,
|
||||||
|
[ 'value' => sanitize_text_field( $_POST['s3RemotePath'] ) ],
|
||||||
|
[ 'name' => 's3RemotePath' ]
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_safe_redirect( admin_url( 'admin.php?page=wp2static-s3' ) );
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get option value
|
||||||
|
*
|
||||||
|
* @return string option value
|
||||||
|
*/
|
||||||
|
public static function getValue( string $name ) : string {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$table_name = $wpdb->prefix . 'wp2static_addon_s3_options';
|
||||||
|
|
||||||
|
$sql = $wpdb->prepare(
|
||||||
|
"SELECT value FROM $table_name WHERE" . ' name = %s LIMIT 1',
|
||||||
|
$name
|
||||||
|
);
|
||||||
|
|
||||||
|
$option_value = $wpdb->get_var( $sql );
|
||||||
|
|
||||||
|
if ( ! is_string( $option_value ) ) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $option_value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
174
src/Deployer.php
Executable file
174
src/Deployer.php
Executable file
|
@ -0,0 +1,174 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace WP2StaticS3;
|
||||||
|
|
||||||
|
use RecursiveIteratorIterator;
|
||||||
|
use RecursiveDirectoryIterator;
|
||||||
|
use Aws\S3\S3Client;
|
||||||
|
use Aws\CloudFront\CloudFrontClient;
|
||||||
|
use Aws\Exception\AwsException;
|
||||||
|
|
||||||
|
// TODO: pull out of old core GuessMimeType( $local_file )
|
||||||
|
|
||||||
|
class Deployer {
|
||||||
|
|
||||||
|
// prepare deploy, if modifies URL structure, should be an action
|
||||||
|
// $this->prepareDeploy();
|
||||||
|
|
||||||
|
// options - load from addon's static methods
|
||||||
|
|
||||||
|
public function __construct() {}
|
||||||
|
|
||||||
|
public function upload_files ( $processed_site_path ) : void {
|
||||||
|
// check if dir exists
|
||||||
|
if ( ! is_dir( $processed_site_path ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$client_options = [
|
||||||
|
'profile' => Controller::getValue( 's3Profile' ),
|
||||||
|
'version' => 'latest',
|
||||||
|
'region' => Controller::getValue( 's3Region' ),
|
||||||
|
];
|
||||||
|
|
||||||
|
/*
|
||||||
|
If no credentials option, SDK attempts to load credentials from your environment in the following order:
|
||||||
|
|
||||||
|
- environment variables.
|
||||||
|
- a credentials .ini file.
|
||||||
|
- an IAM role.
|
||||||
|
*/
|
||||||
|
if (
|
||||||
|
Controller::getValue( 's3AccessKeyID' ) &&
|
||||||
|
Controller::getValue( 's3SecretAccessKey' )
|
||||||
|
) {
|
||||||
|
error_log('using supplied creds');
|
||||||
|
$client_options['credentials'] = [
|
||||||
|
'key' => Controller::getValue( 's3AccessKeyID' ),
|
||||||
|
'secret' => \WP2StaticS3\Controller::encrypt_decrypt(
|
||||||
|
'decrypt',
|
||||||
|
Controller::getValue( 's3SecretAccessKey' )
|
||||||
|
)
|
||||||
|
];
|
||||||
|
unset( $client_options['profile'] );
|
||||||
|
}
|
||||||
|
|
||||||
|
error_log(print_r($client_options, true));
|
||||||
|
|
||||||
|
// instantiate S3 client
|
||||||
|
$s3 = new \Aws\S3\S3Client( $client_options );
|
||||||
|
|
||||||
|
|
||||||
|
// iterate each file in ProcessedSite
|
||||||
|
$iterator = new RecursiveIteratorIterator(
|
||||||
|
new RecursiveDirectoryIterator(
|
||||||
|
$processed_site_path,
|
||||||
|
RecursiveDirectoryIterator::SKIP_DOTS
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ( $iterator as $filename => $file_object ) {
|
||||||
|
$base_name = basename( $filename );
|
||||||
|
if ( $base_name != '.' && $base_name != '..' ) {
|
||||||
|
$real_filepath = realpath( $filename );
|
||||||
|
|
||||||
|
// TODO: do filepaths differ when running from WP-CLI (non-chroot)?
|
||||||
|
|
||||||
|
// TODO: check if in DeployCache
|
||||||
|
if ( \WP2Static\DeployCache::fileisCached( $filename ) ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! $real_filepath ) {
|
||||||
|
$err = 'Trying to add unknown file to Zip: ' . $filename;
|
||||||
|
WsLog::l( $err );
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Standardise all paths to use / (Windows support)
|
||||||
|
$filename = str_replace( '\\', '/', $filename );
|
||||||
|
|
||||||
|
if ( ! is_string( $filename ) ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$key =
|
||||||
|
Controller::getValue( 's3RemotePath' ) ?
|
||||||
|
Controller::getValue( 's3RemotePath' ) . '/' . ltrim( str_replace( $processed_site_path, '', $filename ), '/' ) :
|
||||||
|
ltrim( str_replace( $processed_site_path, '', $filename ), '/' );
|
||||||
|
|
||||||
|
$result = $s3->putObject([
|
||||||
|
'Bucket' => Controller::getValue( 's3Bucket' ),
|
||||||
|
'Key' => $key,
|
||||||
|
'Body' => file_get_contents( $filename ),
|
||||||
|
'ACL' => 'public-read',
|
||||||
|
'ContentType' => mime_content_type( $filename ),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ( $result['@metadata']['statusCode'] === 200 ) {
|
||||||
|
\WP2Static\DeployCache::addFile( $filename );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function cloudfront_invalidate_all_items() {
|
||||||
|
if ( ! Controller::getValue( 'cfDistributionID' ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
\WsLog::l( 'Invalidating all CloudFront items' );
|
||||||
|
|
||||||
|
$client_options = [
|
||||||
|
'profile' => 'default',
|
||||||
|
'version' => 'latest',
|
||||||
|
'region' => Controller::getValue( 'cfRegion' ),
|
||||||
|
];
|
||||||
|
|
||||||
|
/*
|
||||||
|
If no credentials option, SDK attempts to load credentials from your environment in the following order:
|
||||||
|
|
||||||
|
- environment variables.
|
||||||
|
- a credentials .ini file.
|
||||||
|
- an IAM role.
|
||||||
|
*/
|
||||||
|
if (
|
||||||
|
Controller::getValue( 's3AccessKeyID' ) &&
|
||||||
|
Controller::getValue( 's3SecretAccessKey' )
|
||||||
|
) {
|
||||||
|
|
||||||
|
$credentials = new Aws\Credentials\Credentials(
|
||||||
|
Controller::getValue( 's3AccessKeyID' ),
|
||||||
|
\WP2StaticS3\Controller::encrypt_decrypt(
|
||||||
|
'decrypt',
|
||||||
|
Controller::getValue( 's3SecretAccessKey' )
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$client_options['credentials'] = $credentials;
|
||||||
|
}
|
||||||
|
|
||||||
|
$client = new Aws\CloudFront\CloudFrontClient( $client );
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = $client->createInvalidation([
|
||||||
|
'DistributionId' => Controller::getValue( 'cfDistributionID' ),
|
||||||
|
'InvalidationBatch' => [
|
||||||
|
'CallerReference' => 'WP2Static S3 Add-on',
|
||||||
|
'Paths' => [
|
||||||
|
'Items' => ['/*'],
|
||||||
|
'Quantity' => 1,
|
||||||
|
],
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
error_log(print_r($result, true));
|
||||||
|
|
||||||
|
} catch (AwsException $e) {
|
||||||
|
// output error message if fails
|
||||||
|
error_log($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
110
src/Netlify.php
110
src/Netlify.php
|
@ -1,110 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace WP2Static;
|
|
||||||
|
|
||||||
class Netlify extends SitePublisher {
|
|
||||||
|
|
||||||
public function __construct() {
|
|
||||||
// TODO: race condition in getting filtered options back
|
|
||||||
// use static method to access options, checking for an instance
|
|
||||||
// ie Option::getOption('netlifySiteID');
|
|
||||||
$plugin = Controller::getInstance();
|
|
||||||
|
|
||||||
$this->base_url = 'https://api.netlify.com';
|
|
||||||
|
|
||||||
$this->site_id =
|
|
||||||
$this->detectSiteID(
|
|
||||||
$plugin->options->getOption( 'netlifySiteID' )
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->access_token =
|
|
||||||
$plugin->options->getOption( 'netlifyPersonalAccessToken' );
|
|
||||||
}
|
|
||||||
|
|
||||||
public function detectSiteID( $site_id ) {
|
|
||||||
if ( strpos( $site_id, 'netlify.com' ) !== false ) {
|
|
||||||
return $site_id;
|
|
||||||
} elseif ( strpos( $site_id, '.' ) !== false ) {
|
|
||||||
return $site_id;
|
|
||||||
} elseif ( strlen( $site_id ) === 37 ) {
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
return $site_id .= '.netlify.com';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function deploy() {
|
|
||||||
WsLog::l('Deploying to Netlify');
|
|
||||||
|
|
||||||
$this->zip_archive_path = SiteInfo::getPath('uploads') .
|
|
||||||
'wp2static-exported-site.zip';
|
|
||||||
|
|
||||||
$zip_size = filesize( $this->zip_archive_path );
|
|
||||||
|
|
||||||
$zip_size = number_format( $zip_size / 1048576, 2) . ' MB';
|
|
||||||
|
|
||||||
WsLog::l("ZIP size: {$zip_size}");
|
|
||||||
|
|
||||||
$zip_deploy_endpoint = $this->base_url . '/api/v1/sites/' .
|
|
||||||
$this->site_id . '/deploys';
|
|
||||||
|
|
||||||
try {
|
|
||||||
$headers = array(
|
|
||||||
'Authorization: Bearer ' . $this->access_token,
|
|
||||||
'Content-Type: application/zip',
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->client = new Request();
|
|
||||||
|
|
||||||
$this->client->postWithFileStreamAndHeaders(
|
|
||||||
$zip_deploy_endpoint,
|
|
||||||
$this->zip_archive_path,
|
|
||||||
$headers
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->checkForValidResponses(
|
|
||||||
$this->client->status_code,
|
|
||||||
array( '100', '200', '201', '301', '302', '304' )
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->finalizeDeployment();
|
|
||||||
} catch ( Exception $e ) {
|
|
||||||
$this->handleException( $e );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test_netlify() {
|
|
||||||
$site_info_endpoint = $this->base_url . '/api/v1/sites/' .
|
|
||||||
$this->site_id;
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
$headers = array( 'Authorization: Bearer ' . $this->access_token );
|
|
||||||
|
|
||||||
$this->client = new Request();
|
|
||||||
|
|
||||||
$status_code = $this->client->getWithCustomHeaders(
|
|
||||||
$site_info_endpoint,
|
|
||||||
$headers
|
|
||||||
);
|
|
||||||
|
|
||||||
// NOTE: check for certain header, as response is always 200
|
|
||||||
if ( isset( $this->client->headers['x-ratelimit-limit'] ) ) {
|
|
||||||
if ( ! defined( 'WP_CLI' ) ) {
|
|
||||||
echo 'SUCCESS';
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
WsLog::l(
|
|
||||||
'BAD RESPONSE CODE FROM API (' . $status_code . ')'
|
|
||||||
);
|
|
||||||
|
|
||||||
http_response_code( $code );
|
|
||||||
|
|
||||||
echo 'Netlify test error';
|
|
||||||
wp_die();
|
|
||||||
}
|
|
||||||
} catch ( Exception $e ) {
|
|
||||||
$this->handleException( $e );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,116 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace WP2Static;
|
|
||||||
|
|
||||||
class NetlifyAddon {
|
|
||||||
|
|
||||||
public function __construct() {
|
|
||||||
if ( defined( 'PLUGIN_NAME_VERSION' ) ) {
|
|
||||||
$this->version = PLUGIN_NAME_VERSION;
|
|
||||||
} else {
|
|
||||||
$this->version = '0.1';
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->plugin_name = 'wp2static-addon-netlify';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function add_deployment_option_to_ui( $deploy_options ) {
|
|
||||||
$deploy_options['netlify'] = array('Netlify');
|
|
||||||
|
|
||||||
return $deploy_options;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function load_deployment_option_template( $templates ) {
|
|
||||||
$templates[] = __DIR__ . '/../views/netlify_settings_block.phtml';
|
|
||||||
|
|
||||||
return $templates;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function add_deployment_option_keys( $keys ) {
|
|
||||||
$new_keys = array(
|
|
||||||
'baseUrl-netlify',
|
|
||||||
'netlifyHeaders',
|
|
||||||
'netlifyPersonalAccessToken',
|
|
||||||
'netlifyRedirects',
|
|
||||||
'netlifySiteID',
|
|
||||||
);
|
|
||||||
|
|
||||||
$keys = array_merge(
|
|
||||||
$keys,
|
|
||||||
$new_keys
|
|
||||||
);
|
|
||||||
|
|
||||||
return $keys;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function whitelist_deployment_option_keys( $keys ) {
|
|
||||||
$whitelist_keys = array(
|
|
||||||
'baseUrl-netlify',
|
|
||||||
'netlifyHeaders',
|
|
||||||
'netlifyRedirects',
|
|
||||||
'netlifySiteID',
|
|
||||||
);
|
|
||||||
|
|
||||||
$keys = array_merge(
|
|
||||||
$keys,
|
|
||||||
$whitelist_keys
|
|
||||||
);
|
|
||||||
|
|
||||||
return $keys;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function add_post_and_db_keys( $keys ) {
|
|
||||||
$keys['netlify'] = array(
|
|
||||||
'baseUrl-netlify',
|
|
||||||
'netlifyHeaders',
|
|
||||||
'netlifyPersonalAccessToken',
|
|
||||||
'netlifyRedirects',
|
|
||||||
'netlifySiteID',
|
|
||||||
);
|
|
||||||
|
|
||||||
return $keys;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function netlify_load_js( $hook ) {
|
|
||||||
if ( $hook !== 'toplevel_page_wp2static' ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
wp_enqueue_script(
|
|
||||||
$this->plugin_name,
|
|
||||||
plugin_dir_url( __FILE__ ) .
|
|
||||||
'../js/wp2static-addon-netlify-admin.js',
|
|
||||||
array( 'jquery' ),
|
|
||||||
$this->version, false
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function run() {
|
|
||||||
add_action( 'admin_enqueue_scripts', [ $this, 'netlify_load_js' ] );
|
|
||||||
|
|
||||||
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']
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
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;
|
||||||
|
}
|
|
@ -1,81 +0,0 @@
|
||||||
<div class="netlify_settings_block" style="display:none;">
|
|
||||||
|
|
||||||
<?php if ( ! extension_loaded('zip') ) : ?>
|
|
||||||
|
|
||||||
<div class="notice notice-error inline wp2static-notice">
|
|
||||||
<h2 class="title">WARNING: ZIP extension missing</h2>
|
|
||||||
|
|
||||||
<p>This can be a common issue but easy to fix. It means that the installation of the PHP programming language on your web host does not currently have support for reading and generating compressed ZIP files. This is needed for the plugin to be able to generate a .zip archive of your static website. Here's some hosting company and platform specific guides on how to fix:</p>
|
|
||||||
|
|
||||||
<ul>
|
|
||||||
<li><a target="_blank" href="https://godaddy.com/help/enable-custom-php-modules-12036">GoDaddy</a></li>
|
|
||||||
<li><a target="_blank" href="https://stackoverflow.com/questions/23564138/how-to-enable-zip-dll-in-xampp">XAMPP</a></li>
|
|
||||||
<li><a target="_blank" href="https://stackoverflow.com/questions/38104348/install-php-zip-on-php-5-6-on-ubuntu">Ubuntu</a></li>
|
|
||||||
<li><a target="_blank" href="https://www.digitalocean.com/community/questions/php-7-0-ziparchive-library-is-missing-or-disabled">DigitalOcean</a></li>
|
|
||||||
<li><a target="_blank" href="http://lmgtfy.com/?q=how+to+get+zip+extension+php">Ask Dr. Google</a></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<p>After installing/enabling the ZIP extension for PHP, you will likely also need to restart your webserver (Apache or nginx) for it to be usable within WordPress and this plugin.</p>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<?php endif;?>
|
|
||||||
|
|
||||||
<table class="form-table">
|
|
||||||
<tbody>
|
|
||||||
<tr>
|
|
||||||
<th scope="row">
|
|
||||||
<label for="baseUrl-netlify"><?php echo __('Destination URL', 'static-html-output-plugin');?></label>
|
|
||||||
</th>
|
|
||||||
<td>
|
|
||||||
<?php $tpl->displayTextfield($this, 'baseUrl-netlify', 'http://mystaticsite.com', '', ''); ?><br>
|
|
||||||
|
|
||||||
<p><i><?php echo __("Set this to the URL you intend to host your static exported site on, ie http://mystaticsite.com. Do not set this to the same URL as the WordPress site you're currently using (the address in your browser above). This plugin will rewrite all URLs in the exported static html from your current WordPress URL to what you set here. Supports http, https and protocol relative URLs.", 'static-html-output-plugin');?></i></p>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<th scope="row">
|
|
||||||
<label for="netlifyPersonalAccessToken"><?php echo __('Personal Access Token', 'static-html-output-plugin');?></label>
|
|
||||||
</th>
|
|
||||||
<td>
|
|
||||||
<?php $tpl->displayTextfield($this, 'netlifyPersonalAccessToken', 'Netlify Personal Access Token', '<a href="https://app.netlify.com/account/applications/personal" target="_blank">How do I get this?</a>', 'password'); ?>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<th scope="row">
|
|
||||||
<label for="netlifySiteID"><?php echo __('Site ID', 'static-html-output-plugin');?></label>
|
|
||||||
</th>
|
|
||||||
<td>
|
|
||||||
<?php $tpl->displayTextfield($this, 'netlifySiteID', 'Netlify Site ID', 'supports: mystaticsite, mystaticsite.netlify.com, mydomain.com, 3970e0fe-8564-4903-9a55-c5f8de49fb8b'); ?>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<th scope="row">
|
|
||||||
<label for="netlifyRedirects"><?php echo __('Redirects File', 'static-html-output-plugin');?></label>
|
|
||||||
<p><a href="https://www.netlify.com/docs/redirects/" target="_blank">more info</a></p>
|
|
||||||
</th>
|
|
||||||
<td>
|
|
||||||
<textarea class="widefat" name="netlifyRedirects" id="netlifyRedirects" rows="5" cols="10"><?php echo $this->options->netlifyRedirects ? $this->options->netlifyRedirects : '' ?></textarea>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<th scope="row">
|
|
||||||
<label for="netlifyHeaders"><?php echo __('Headers File', 'static-html-output-plugin');?></label>
|
|
||||||
<p><a href="https://www.netlify.com/docs/headers-and-basic-auth/" target="_blank">more info</a></p>
|
|
||||||
</th>
|
|
||||||
<td>
|
|
||||||
<textarea class="widefat" name="netlifyHeaders" id="netlifyHeaders" rows="5" cols="10"><?php echo $this->options->netlifyHeaders ? $this->options->netlifyHeaders : '' ?></textarea>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<th scope="row">
|
|
||||||
<label for="netlify_test"><?php echo __('Test Netlify Settings', 'static-html-output-plugin');?></label>
|
|
||||||
</th>
|
|
||||||
<td>
|
|
||||||
<button id="netlify-test-button" type="button" class="btn-primary button">Test Netlify Settings</button>
|
|
||||||
<span id="netlify_test_results"></span>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
212
views/s3-page.php
Executable file
212
views/s3-page.php
Executable file
|
@ -0,0 +1,212 @@
|
||||||
|
<h2>S3 Deployment Options</h2>
|
||||||
|
|
||||||
|
<h3>S3</h3>
|
||||||
|
|
||||||
|
<form
|
||||||
|
name="wp2static-s3-save-options"
|
||||||
|
method="POST"
|
||||||
|
action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">
|
||||||
|
|
||||||
|
<?php wp_nonce_field( $view['nonce_action'] ); ?>
|
||||||
|
<input name="action" type="hidden" value="wp2static_s3_save_options" />
|
||||||
|
|
||||||
|
<table class="widefat striped">
|
||||||
|
<tbody>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td style="width:50%;">
|
||||||
|
<label
|
||||||
|
for="<?php echo $view['options']['s3Bucket']->name; ?>"
|
||||||
|
><?php echo $view['options']['s3Bucket']->label; ?></label>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
id="<?php echo $view['options']['s3Bucket']->name; ?>"
|
||||||
|
name="<?php echo $view['options']['s3Bucket']->name; ?>"
|
||||||
|
type="text"
|
||||||
|
value="<?php echo $view['options']['s3Bucket']->value !== '' ? $view['options']['s3Bucket']->value : ''; ?>"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td style="width:50%;">
|
||||||
|
<label
|
||||||
|
for="<?php echo $view['options']['s3Region']->name; ?>"
|
||||||
|
><?php echo $view['options']['s3Region']->label; ?></label>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
id="<?php echo $view['options']['s3Region']->name; ?>"
|
||||||
|
name="<?php echo $view['options']['s3Region']->name; ?>"
|
||||||
|
type="text"
|
||||||
|
value="<?php echo $view['options']['s3Region']->value !== '' ? $view['options']['s3Region']->value : ''; ?>"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td style="width:50%;">
|
||||||
|
<label
|
||||||
|
for="<?php echo $view['options']['s3AccessKeyID']->name; ?>"
|
||||||
|
><?php echo $view['options']['s3AccessKeyID']->label; ?></label>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
id="<?php echo $view['options']['s3AccessKeyID']->name; ?>"
|
||||||
|
name="<?php echo $view['options']['s3AccessKeyID']->name; ?>"
|
||||||
|
value="<?php echo $view['options']['s3AccessKeyID']->value !== '' ? $view['options']['s3AccessKeyID']->value : ''; ?>"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td style="width:50%;">
|
||||||
|
<label
|
||||||
|
for="<?php echo $view['options']['s3SecretAccessKey']->name; ?>"
|
||||||
|
><?php echo $view['options']['s3SecretAccessKey']->label; ?></label>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
id="<?php echo $view['options']['s3SecretAccessKey']->name; ?>"
|
||||||
|
name="<?php echo $view['options']['s3SecretAccessKey']->name; ?>"
|
||||||
|
type="password"
|
||||||
|
value="<?php echo $view['options']['s3SecretAccessKey']->value !== '' ?
|
||||||
|
\WP2StaticS3\Controller::encrypt_decrypt('decrypt', $view['options']['s3SecretAccessKey']->value) :
|
||||||
|
''; ?>"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td style="width:50%;">
|
||||||
|
<label
|
||||||
|
for="<?php echo $view['options']['s3Profile']->name; ?>"
|
||||||
|
><?php echo $view['options']['s3Profile']->label; ?></label>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
id="<?php echo $view['options']['s3Profile']->name; ?>"
|
||||||
|
name="<?php echo $view['options']['s3Profile']->name; ?>"
|
||||||
|
type="text"
|
||||||
|
value="<?php echo $view['options']['s3Profile']->value !== '' ? $view['options']['s3Profile']->value : ''; ?>"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td style="width:50%;">
|
||||||
|
<label
|
||||||
|
for="<?php echo $view['options']['s3RemotePath']->name; ?>"
|
||||||
|
><?php echo $view['options']['s3RemotePath']->label; ?></label>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
id="<?php echo $view['options']['s3RemotePath']->name; ?>"
|
||||||
|
name="<?php echo $view['options']['s3RemotePath']->name; ?>"
|
||||||
|
type="text"
|
||||||
|
value="<?php echo $view['options']['s3RemotePath']->value !== '' ? $view['options']['s3RemotePath']->value : ''; ?>"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
|
||||||
|
<h3>CloudFront</h3>
|
||||||
|
|
||||||
|
<table class="widefat striped">
|
||||||
|
<tbody>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td style="width:50%;">
|
||||||
|
<label
|
||||||
|
for="<?php echo $view['options']['cfRegion']->name; ?>"
|
||||||
|
><?php echo $view['options']['cfRegion']->label; ?></label>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
id="<?php echo $view['options']['cfRegion']->name; ?>"
|
||||||
|
name="<?php echo $view['options']['cfRegion']->name; ?>"
|
||||||
|
type="text"
|
||||||
|
value="<?php echo $view['options']['cfRegion']->value !== '' ? $view['options']['cfRegion']->value : ''; ?>"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td style="width:50%;">
|
||||||
|
<label
|
||||||
|
for="<?php echo $view['options']['cfAccessKeyID']->name; ?>"
|
||||||
|
><?php echo $view['options']['cfAccessKeyID']->label; ?></label>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
id="<?php echo $view['options']['cfAccessKeyID']->name; ?>"
|
||||||
|
name="<?php echo $view['options']['cfAccessKeyID']->name; ?>"
|
||||||
|
value="<?php echo $view['options']['cfAccessKeyID']->value !== '' ? $view['options']['cfAccessKeyID']->value : ''; ?>"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td style="width:50%;">
|
||||||
|
<label
|
||||||
|
for="<?php echo $view['options']['cfSecretAccessKey']->name; ?>"
|
||||||
|
><?php echo $view['options']['cfSecretAccessKey']->label; ?></label>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
id="<?php echo $view['options']['cfSecretAccessKey']->name; ?>"
|
||||||
|
name="<?php echo $view['options']['cfSecretAccessKey']->name; ?>"
|
||||||
|
type="password"
|
||||||
|
value="<?php echo $view['options']['cfSecretAccessKey']->value !== '' ?
|
||||||
|
\WP2StaticS3\Controller::encrypt_decrypt('decrypt', $view['options']['cfSecretAccessKey']->value) :
|
||||||
|
''; ?>"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td style="width:50%;">
|
||||||
|
<label
|
||||||
|
for="<?php echo $view['options']['cfProfile']->name; ?>"
|
||||||
|
><?php echo $view['options']['cfProfile']->label; ?></label>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
id="<?php echo $view['options']['cfProfile']->name; ?>"
|
||||||
|
name="<?php echo $view['options']['cfProfile']->name; ?>"
|
||||||
|
type="text"
|
||||||
|
value="<?php echo $view['options']['cfProfile']->value !== '' ? $view['options']['cfProfile']->value : ''; ?>"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td style="width:50%;">
|
||||||
|
<label
|
||||||
|
for="<?php echo $view['options']['cfDistributionID']->name; ?>"
|
||||||
|
><?php echo $view['options']['cfDistributionID']->label; ?></label>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
id="<?php echo $view['options']['cfDistributionID']->name; ?>"
|
||||||
|
name="<?php echo $view['options']['cfDistributionID']->name; ?>"
|
||||||
|
type="text"
|
||||||
|
value="<?php echo $view['options']['cfDistributionID']->value !== '' ? $view['options']['cfDistributionID']->value : ''; ?>"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
<button class="button btn-primary">Save S3 Options</button>
|
||||||
|
</form>
|
||||||
|
|
|
@ -1,54 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Plugin Name: WP2Static Add-on: Netlify
|
|
||||||
* Plugin URI: https://wp2static.com
|
|
||||||
* Description: AWS Netlify as a deployment option for WP2Static.
|
|
||||||
* Version: 0.1
|
|
||||||
* Author: Leon Stafford
|
|
||||||
* Author URI: https://ljs.dev
|
|
||||||
* License: Unlicense
|
|
||||||
* License URI: http://unlicense.org
|
|
||||||
* Text Domain: wp2static-addon-netlify
|
|
||||||
* Domain Path: /languages
|
|
||||||
*/
|
|
||||||
|
|
||||||
if ( ! defined( 'WPINC' ) ) {
|
|
||||||
die;
|
|
||||||
}
|
|
||||||
|
|
||||||
define( 'WP2STATIC_NETLIFY_PATH', plugin_dir_path( __FILE__ ) );
|
|
||||||
|
|
||||||
require WP2STATIC_NETLIFY_PATH . 'vendor/autoload.php';
|
|
||||||
|
|
||||||
// @codingStandardsIgnoreStart
|
|
||||||
$ajax_action = isset( $_POST['ajax_action'] ) ? $_POST['ajax_action'] : '';
|
|
||||||
// @codingStandardsIgnoreEnd
|
|
||||||
|
|
||||||
if ( $ajax_action == 'test_netlify' ) {
|
|
||||||
$netlify = new WP2Static\Netlify();
|
|
||||||
|
|
||||||
$netlify->test_netlify();
|
|
||||||
|
|
||||||
wp_die();
|
|
||||||
return null;
|
|
||||||
} elseif ( $ajax_action == 'netlify_do_export' ) {
|
|
||||||
$netlify = new WP2Static\Netlify();
|
|
||||||
|
|
||||||
$netlify->bootstrap();
|
|
||||||
$netlify->deploy();
|
|
||||||
|
|
||||||
wp_die();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
define( 'PLUGIN_NAME_VERSION', '0.1' );
|
|
||||||
|
|
||||||
function run_wp2static_addon_netlify() {
|
|
||||||
$plugin = new WP2Static\NetlifyAddon();
|
|
||||||
$plugin->run();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
run_wp2static_addon_netlify();
|
|
||||||
|
|
40
wp2static-addon-s3.php
Normal file
40
wp2static-addon-s3.php
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plugin Name: WP2Static Add-on: S3 Deployment
|
||||||
|
* Plugin URI: https://wp2static.com
|
||||||
|
* Description: AWS S3 deployment add-on for WP2Static.
|
||||||
|
* Version: 0.1
|
||||||
|
* Author: Leon Stafford
|
||||||
|
* Author URI: https://ljs.dev
|
||||||
|
* License: Unlicense
|
||||||
|
* License URI: http://unlicense.org
|
||||||
|
* Text Domain: wp2static-addon-s3
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ( ! defined( 'WPINC' ) ) {
|
||||||
|
die;
|
||||||
|
}
|
||||||
|
|
||||||
|
define( 'WP2STATIC_S3_PATH', plugin_dir_path( __FILE__ ) );
|
||||||
|
define( 'WP2STATIC_S3_VERSION', '0.1' );
|
||||||
|
|
||||||
|
require WP2STATIC_S3_PATH . 'vendor/autoload.php';
|
||||||
|
|
||||||
|
function run_wp2static_addon_s3() {
|
||||||
|
$controller = new WP2StaticS3\Controller();
|
||||||
|
$controller->run();
|
||||||
|
}
|
||||||
|
|
||||||
|
register_activation_hook(
|
||||||
|
__FILE__,
|
||||||
|
[ 'WP2StaticS3\Controller', 'activate' ]
|
||||||
|
);
|
||||||
|
|
||||||
|
register_deactivation_hook(
|
||||||
|
__FILE__,
|
||||||
|
[ 'WP2StaticS3\Controller', 'deactivate' ]
|
||||||
|
);
|
||||||
|
|
||||||
|
run_wp2static_addon_s3();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue