initial
This commit is contained in:
+44
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Migration;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Config\Connector_Endpoints_Config;
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Connector_Base;
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Connector_Service_Provider;
|
||||
use Gravity_Forms\Gravity_SMTP\Gravity_SMTP;
|
||||
use Gravity_Forms\Gravity_Tools\Providers\Config_Service_Provider;
|
||||
use Gravity_Forms\Gravity_Tools\Service_Container;
|
||||
use Gravity_Forms\Gravity_SMTP\Apps\Migration\Endpoints\Migrate_Settings_Endpoint;
|
||||
use Gravity_Forms\Gravity_SMTP\Migration\Config\Migration_Endpoints_Config;
|
||||
|
||||
class Migration_Service_Provider extends Config_Service_Provider {
|
||||
|
||||
const MIGRATOR_COLLECTION = 'migrator_collection';
|
||||
const WP_SMTP_PRO_MIGRATOR = 'wp_smtp_pro_migrator';
|
||||
const MIGRATE_ENDPOINTS_CONFIG = 'migrate_endpoints_config';
|
||||
|
||||
const MIGRATE_SETTINGS_ENDPOINT = 'migrate_settings_endpoint';
|
||||
|
||||
protected $configs = array(
|
||||
self::MIGRATE_ENDPOINTS_CONFIG => Migration_Endpoints_Config::class,
|
||||
);
|
||||
|
||||
public function register( Service_Container $container ) {
|
||||
parent::register( $container );
|
||||
|
||||
$container->add( self::MIGRATOR_COLLECTION, function() {
|
||||
return new Migrator_Collection();
|
||||
});
|
||||
|
||||
$container->add( self::MIGRATE_SETTINGS_ENDPOINT, function () use ( $container ) {
|
||||
return new Migrate_Settings_Endpoint( $container->get( Connector_Service_Provider::CONNECTOR_FACTORY ), $container->get( Connector_Service_Provider::DATA_STORE_OPTS ), __NAMESPACE__, $container->get( Connector_Service_Provider::REGISTERED_CONNECTORS ) );
|
||||
} );
|
||||
}
|
||||
|
||||
public function init( \Gravity_Forms\Gravity_Tools\Service_Container $container ) {
|
||||
add_action( 'wp_ajax_' . Migrate_Settings_Endpoint::ACTION_NAME, function () use ( $container ) {
|
||||
$container->get( self::MIGRATE_SETTINGS_ENDPOINT )->handle();
|
||||
} );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Migration;
|
||||
|
||||
class Migration {
|
||||
|
||||
private $og_identifier;
|
||||
private $new_identifier;
|
||||
|
||||
public function __construct( $og_identifier, $new_identifier ) {
|
||||
$this->og_identifier = $og_identifier;
|
||||
$this->new_identifier = $new_identifier;
|
||||
}
|
||||
|
||||
public function migrate() {
|
||||
$original_value = $this->get_original_value();
|
||||
$new_value = $this->store_new_value( $original_value );
|
||||
|
||||
return $new_value;
|
||||
}
|
||||
|
||||
private function get_original_value() {
|
||||
if ( is_string( $this->og_identifier ) ) {
|
||||
return get_option( $this->og_identifier, null );
|
||||
}
|
||||
|
||||
return call_user_func( $this->og_identifier );
|
||||
}
|
||||
|
||||
private function store_new_value( $original_value ) {
|
||||
if ( is_string( $this->new_identifier ) ) {
|
||||
update_option( $this->new_identifier, $original_value );
|
||||
}
|
||||
|
||||
call_user_func( $this->new_identifier, $original_value );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Migration;
|
||||
|
||||
class Migrator_Collection {
|
||||
|
||||
/**
|
||||
* @var Migrator[]
|
||||
*/
|
||||
private $migrators = array();
|
||||
|
||||
public function add( $key, $migrator ) {
|
||||
$this->migrators[ $key ] = $migrator;
|
||||
}
|
||||
|
||||
public function remove( $key ) {
|
||||
unset( $this->migrators[ $key ] );
|
||||
}
|
||||
|
||||
public function get( $key ) {
|
||||
return isset( $this->migrators[ $key ] ) ? $this->migrators[ $key ] : null;
|
||||
}
|
||||
|
||||
public function run_all() {
|
||||
foreach( $this->migrators as $key => $migrator ) {
|
||||
$migrator->migrate();
|
||||
}
|
||||
}
|
||||
|
||||
public function run( $key ) {
|
||||
if ( ! isset( $this->migrators[ $key ] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->migrators[ $key ]->migrate();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Migration;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Connector_Service_Provider;
|
||||
use Gravity_Forms\Gravity_SMTP\Data_Store\Opts_Data_Store;
|
||||
use Gravity_Forms\Gravity_SMTP\Data_Store\Plugin_Opts_Data_Store;
|
||||
use Gravity_Forms\Gravity_SMTP\Gravity_SMTP;
|
||||
|
||||
class Migrator {
|
||||
|
||||
/**
|
||||
* @var Migration[][]
|
||||
*/
|
||||
private $connector = array();
|
||||
|
||||
/**
|
||||
* @var Migration[]
|
||||
*/
|
||||
private $plugin = array();
|
||||
|
||||
/**
|
||||
* @var Migration[]
|
||||
*/
|
||||
private $user = array();
|
||||
|
||||
public function add_connector_migration( $key, $og_id, $new_id ) {
|
||||
$migration = new Migration( $og_id, $new_id );
|
||||
$this->connector[ $key ][] = $migration;
|
||||
}
|
||||
|
||||
public function add_plugin_migration( $key, $og_id, $new_id ) {
|
||||
$migration = new Migration( $og_id, $new_id );
|
||||
$this->plugin[ $key ] = $migration;
|
||||
}
|
||||
|
||||
public function add_user_migration( $key, $og_id, $new_id, $user_id = 0 ) {
|
||||
if ( is_string( $new_id ) ) {
|
||||
$new_id = $this->default_user_action( $new_id, $user_id );
|
||||
}
|
||||
|
||||
$migration = new Migration( $og_id, $new_id );
|
||||
$this->user[ $key ] = $migration;
|
||||
}
|
||||
|
||||
public function migrate() {
|
||||
foreach ( $this->connector as $connector => $migrations ) {
|
||||
foreach( $migrations as $conn_migration ) {
|
||||
$conn_migration->migrate();
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $this->plugin as $key => $plugin_migration ) {
|
||||
$plugin_migration->migrate();
|
||||
}
|
||||
|
||||
foreach ( $this->user as $key => $user_migration ) {
|
||||
$user_migration->migrate();
|
||||
}
|
||||
}
|
||||
|
||||
public function migrate_single( $type, $key ) {
|
||||
if ( ! isset( $this->$type[ $key ] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->$type[ $key ]->migrate();
|
||||
}
|
||||
|
||||
public function migrate_connections() {
|
||||
foreach ( $this->connector as $key => $conn_migration ) {
|
||||
$conn_migration->migrate();
|
||||
}
|
||||
}
|
||||
|
||||
public function migrate_plugins() {
|
||||
foreach ( $this->plugin as $key => $plugin_migration ) {
|
||||
$plugin_migration->migrate();
|
||||
}
|
||||
}
|
||||
|
||||
public function migrate_users() {
|
||||
foreach ( $this->user as $key => $user_migration ) {
|
||||
$user_migration->migrate();
|
||||
}
|
||||
}
|
||||
|
||||
private function default_user_action( $new_option_name, $user_id ) {
|
||||
return function ( $new_value ) use ( $new_option_name, $user_id ) {
|
||||
update_user_meta( $user_id, $new_option_name, $new_value );
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Migration\Config;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Apps\Migration\Endpoints\Migrate_Settings_Endpoint;
|
||||
use Gravity_Forms\Gravity_Tools\Config;
|
||||
|
||||
class Migration_Endpoints_Config extends Config {
|
||||
|
||||
protected $script_to_localize = 'gravitysmtp_scripts_admin';
|
||||
protected $name = 'gravitysmtp_admin_config';
|
||||
|
||||
public function should_enqueue() {
|
||||
return is_admin();
|
||||
}
|
||||
|
||||
public function data() {
|
||||
return array(
|
||||
'common' => array(
|
||||
'endpoints' => array(
|
||||
Migrate_Settings_Endpoint::ACTION_NAME => array(
|
||||
'action' => array(
|
||||
'value' => Migrate_Settings_Endpoint::ACTION_NAME,
|
||||
'default' => 'mock_endpoint',
|
||||
),
|
||||
'nonce' => array(
|
||||
'value' => wp_create_nonce( Migrate_Settings_Endpoint::ACTION_NAME ),
|
||||
'default' => 'nonce',
|
||||
),
|
||||
),
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Migration\Data;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Connector_Base;
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Connector_Service_Provider;
|
||||
use Gravity_Forms\Gravity_SMTP\Gravity_SMTP;
|
||||
use Gravity_Forms\Gravity_SMTP\Migration\Migrator;
|
||||
use Gravity_Forms\Gravity_SMTP\Migration\Migrator_Collection;
|
||||
|
||||
class Migration_Data_Gravityforms {
|
||||
|
||||
protected $connector_types = array(
|
||||
'postmark',
|
||||
'sendgrid',
|
||||
'mailgun',
|
||||
);
|
||||
|
||||
public function get_migrations() {
|
||||
/**
|
||||
* @var Migrator_Collection $collection
|
||||
*/
|
||||
$collection = new Migrator_Collection();
|
||||
|
||||
$gf_migrator = new Migrator();
|
||||
|
||||
foreach( $this->connector_types as $connector_name ) {
|
||||
$gf_migrator = $this->get_migrations_for_connector( $gf_migrator, $connector_name );
|
||||
}
|
||||
|
||||
$collection->add( 'gravityforms', $gf_migrator );
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
private function get_migrations_for_connector( Migrator $gf_migrator, $connector_name ) {
|
||||
$factory = Gravity_SMTP::container()->get( Connector_Service_Provider::CONNECTOR_FACTORY );
|
||||
$connector = $factory->create( $connector_name );
|
||||
$map = $connector->migration_map();
|
||||
|
||||
if ( empty( $map ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
foreach ( $map as $migration_data ) {
|
||||
$og_callback = function() use ( $migration_data ) {
|
||||
$original_key = isset( $migration_data['original_key'] ) ? $migration_data['original_key'] : '';
|
||||
$sub_key = isset( $migration_data['sub_key'] ) ? $migration_data['sub_key'] : false;
|
||||
$transform = isset( $migration_data['transform'] ) ? $migration_data['transform'] : false;
|
||||
|
||||
if ( empty( $original_key ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$value = get_option( $original_key );
|
||||
|
||||
if ( $sub_key && is_array( $value ) ) {
|
||||
$value = rgars( $value, $sub_key );
|
||||
}
|
||||
|
||||
if ( empty( $value ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ( $transform ) {
|
||||
$value = call_user_func( $transform, $value );
|
||||
}
|
||||
|
||||
return $value;
|
||||
};
|
||||
|
||||
$new_key = isset( $migration_data['new_key'] ) ? $migration_data['new_key'] : '';
|
||||
|
||||
$gf_migrator->add_connector_migration( $connector_name, $og_callback, $this->default_connector_action( $new_key, $connector_name ) );
|
||||
}
|
||||
|
||||
return $gf_migrator;
|
||||
}
|
||||
|
||||
private function default_connector_action( $new_option_name, $connector ) {
|
||||
return function ( $new_value ) use ( $new_option_name, $connector ) {
|
||||
/**
|
||||
* @var Opts_Data_Store $data
|
||||
*/
|
||||
$data = Gravity_SMTP::container()->get( Connector_Service_Provider::DATA_STORE_OPTS );
|
||||
$data->save( $new_option_name, $new_value, $connector );
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Migration\Data;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Connector_Base;
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Connector_Service_Provider;
|
||||
use Gravity_Forms\Gravity_SMTP\Gravity_SMTP;
|
||||
use Gravity_Forms\Gravity_SMTP\Migration\Migrator;
|
||||
use Gravity_Forms\Gravity_SMTP\Migration\Migrator_Collection;
|
||||
use Gravity_Forms\Gravity_SMTP\Utils\Booliesh;
|
||||
|
||||
class Migration_Data_Wpmailsmtp {
|
||||
|
||||
protected $names_map = array(
|
||||
'sendinblue' => 'brevo',
|
||||
'amazonses' => 'amazon-ses',
|
||||
'gmail' => 'google',
|
||||
'outlook' => 'microsoft',
|
||||
'smtp' => 'generic',
|
||||
);
|
||||
|
||||
protected $wp_smtp_pro_settings_map = array(
|
||||
"sendinblue" => array(
|
||||
"api_key" => "",
|
||||
),
|
||||
"amazonses" => array(
|
||||
"client_id" => "",
|
||||
"client_secret" => ""
|
||||
),
|
||||
"gmail" => array(
|
||||
"client_id" => "",
|
||||
"client_secret" => "",
|
||||
),
|
||||
"mailgun" => array(
|
||||
"api_key" => "",
|
||||
"domain" => "",
|
||||
"region" => "__transform_strtolower",
|
||||
),
|
||||
"outlook" => array(
|
||||
"client_id" => "",
|
||||
"client_secret" => "",
|
||||
),
|
||||
"postmark" => array(
|
||||
"server_api_token" => "",
|
||||
),
|
||||
"sendgrid" => array(
|
||||
"api_key" => "",
|
||||
),
|
||||
"sparkpost" => array(
|
||||
"api_key" => "",
|
||||
"region" => ""
|
||||
),
|
||||
"zoho" => array(
|
||||
"domain" => "",
|
||||
"client_id" => "",
|
||||
"client_secret" => ""
|
||||
),
|
||||
'smtp' => array(
|
||||
'host' => "",
|
||||
'port' => "",
|
||||
'encryption' => "__transform_strtolower||encryption_type",
|
||||
'autotls' => "__transform_booleish||auto_tls",
|
||||
'auth' => "__transform_booleish",
|
||||
'user' => "username",
|
||||
),
|
||||
'elasticemail' => array(
|
||||
'api_key' => '',
|
||||
),
|
||||
'smtp2go' => array(
|
||||
'api_key' => '',
|
||||
),
|
||||
);
|
||||
|
||||
public function get_migrations() {
|
||||
/**
|
||||
* @var Migrator_Collection $collection
|
||||
*/
|
||||
$collection = new Migrator_Collection();
|
||||
|
||||
$wp_smtp_pro_migrator = new Migrator();
|
||||
$wp_smtp_pro_options = get_option( 'wp_mail_smtp' );
|
||||
|
||||
foreach ( $this->wp_smtp_pro_settings_map as $connector => $values ) {
|
||||
$og_connector = $connector;
|
||||
if ( isset( $this->names_map[ $connector ] ) ) {
|
||||
$connector = $this->names_map[ $connector ];
|
||||
}
|
||||
|
||||
foreach ( $values as $og_id => $new_id ) {
|
||||
$transform = false;
|
||||
|
||||
if ( strpos( $new_id, '__transform_' ) !== false ) {
|
||||
$parts = explode( '||', $new_id );
|
||||
$transform = str_replace( '__transform_', '', $parts[0] );
|
||||
$new_id = isset( $parts[1] ) ? $parts[1] : '';
|
||||
}
|
||||
|
||||
if ( $transform === 'booleish' ) {
|
||||
$transform = array( Booliesh::class, 'get' );
|
||||
}
|
||||
|
||||
if ( empty( $new_id ) ) {
|
||||
$new_id = $og_id;
|
||||
}
|
||||
|
||||
$og_callback = function () use ( $wp_smtp_pro_options, $og_id, $connector, $og_connector, $transform ) {
|
||||
$value = $wp_smtp_pro_options[ $og_connector ][ $og_id ];
|
||||
|
||||
if ( $transform ) {
|
||||
$value = call_user_func( $transform, $value );
|
||||
}
|
||||
|
||||
return $value;
|
||||
};
|
||||
|
||||
$wp_smtp_pro_migrator->add_connector_migration( $connector, $og_callback, $this->default_connector_action( $new_id, $connector ) );
|
||||
}
|
||||
}
|
||||
|
||||
$plugin_to_connector_map = array(
|
||||
'from_email' => Connector_Base::SETTING_FROM_EMAIL,
|
||||
'from_name' => Connector_Base::SETTING_FROM_NAME,
|
||||
'from_email_force' => Connector_Base::SETTING_FORCE_FROM_EMAIL,
|
||||
'from_name_force' => Connector_Base::SETTING_FORCE_FROM_NAME,
|
||||
);
|
||||
|
||||
$connector_name_map = Gravity_SMTP::container()->get( Connector_Service_Provider::NAME_MAP );
|
||||
|
||||
if ( empty( $connector_name_map ) ) {
|
||||
$collection->add( 'wpmailsmtp', $wp_smtp_pro_migrator );
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
foreach ( $plugin_to_connector_map as $og_key => $new_key ) {
|
||||
$og_callback = function () use ( $wp_smtp_pro_options, $og_key ) {
|
||||
return $wp_smtp_pro_options['mail'][ $og_key ];
|
||||
};
|
||||
|
||||
foreach ( $connector_name_map as $connector => $label ) {
|
||||
$wp_smtp_pro_migrator->add_connector_migration( $connector, $og_callback, $this->default_connector_action( $new_key, $connector ) );
|
||||
}
|
||||
}
|
||||
|
||||
$collection->add( 'wpmailsmtp', $wp_smtp_pro_migrator );
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
private function default_connector_action( $new_option_name, $connector ) {
|
||||
return function ( $new_value ) use ( $new_option_name, $connector ) {
|
||||
/**
|
||||
* @var Opts_Data_Store $data
|
||||
*/
|
||||
$data = Gravity_SMTP::container()->get( Connector_Service_Provider::DATA_STORE_OPTS );
|
||||
$data->save( $new_option_name, $new_value, $connector );
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Apps\Migration\Endpoints;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Connector_Factory;
|
||||
use Gravity_Forms\Gravity_SMTP\Data_Store\Opts_Data_Store;
|
||||
use Gravity_Forms\Gravity_SMTP\Gravity_SMTP;
|
||||
use Gravity_Forms\Gravity_SMTP\Migration\Migrator_Collection;
|
||||
use Gravity_Forms\Gravity_Tools\Endpoints\Endpoint;
|
||||
use Gravity_Forms\Gravity_Tools\License\License_Statuses;
|
||||
use Gravity_Forms\Gravity_Tools\Updates\Updates_Service_Provider;
|
||||
use Gravity_Forms\Gravity_SMTP\Migration\Data;
|
||||
use Gravity_Forms\Gravity_SMTP\Users\Roles;
|
||||
|
||||
class Migrate_Settings_Endpoint extends Endpoint {
|
||||
|
||||
const PARAM_PLUGIN_TO_MIGRATE = 'plugin_to_migrate';
|
||||
const ACTION_NAME = 'migrate_settings';
|
||||
|
||||
protected $minimum_cap = Roles::EDIT_INTEGRATIONS;
|
||||
|
||||
/**
|
||||
* @var Connector_Factory $connector_factory
|
||||
*/
|
||||
protected $connector_factory;
|
||||
|
||||
/**
|
||||
* @var Opts_Data_Store
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $base_namespace;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $connectors;
|
||||
|
||||
protected $required_params = array(
|
||||
self::PARAM_PLUGIN_TO_MIGRATE,
|
||||
);
|
||||
|
||||
public function __construct( $connector_factory, $data_store, $base_namespace, $connectors ) {
|
||||
$this->connector_factory = $connector_factory;
|
||||
$this->data = $data_store;
|
||||
$this->base_namespace = $base_namespace;
|
||||
$this->connectors = $connectors;
|
||||
}
|
||||
|
||||
protected function get_nonce_name() {
|
||||
return self::ACTION_NAME;
|
||||
}
|
||||
|
||||
public function handle() {
|
||||
if ( ! $this->validate() ) {
|
||||
wp_send_json_error( 'Missing required parameters.', 400 );
|
||||
}
|
||||
|
||||
$plugin_to_migrate = filter_input( INPUT_POST, self::PARAM_PLUGIN_TO_MIGRATE, FILTER_DEFAULT );
|
||||
$allowed = array( 'Gravityforms', 'Wpmailsmtp' );
|
||||
$handler_classname = $this->base_namespace . '\\Data\\Migration_Data_' . ucfirst( strtolower( $plugin_to_migrate ) );
|
||||
|
||||
if ( ! in_array( ucfirst( strtolower( $plugin_to_migrate ) ), $allowed ) || ! class_exists( $handler_classname ) ) {
|
||||
/* translators: %1$s: plugin name */
|
||||
wp_send_json_error( sprintf( __( 'Could not find handler for migration type: %1$s', 'gravitysmtp' ), $plugin_to_migrate ), 400 );
|
||||
}
|
||||
|
||||
$handler = new $handler_classname();
|
||||
|
||||
/**
|
||||
* @var Migrator_Collection $migrations
|
||||
*/
|
||||
$migrations = $handler->get_migrations();
|
||||
$migrations->run( $plugin_to_migrate );
|
||||
|
||||
$response = array();
|
||||
|
||||
foreach ( $this->connectors as $connector_name => $connector_class ) {
|
||||
$instance = $this->connector_factory->create( $connector_name );
|
||||
$connector_data = $instance->get_data();
|
||||
$response[ strtolower( $connector_name ) ] = $connector_data['data'];
|
||||
}
|
||||
|
||||
wp_send_json_success( $response );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user