first commit
This commit is contained in:
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
class OTGS_Installer_Fetch_Subscription {
|
||||
|
||||
private $package_source_factory;
|
||||
private $plugin_finder;
|
||||
private $repositories;
|
||||
private $logger;
|
||||
private $log_factory;
|
||||
|
||||
public function __construct(
|
||||
OTGS_Installer_Source_Factory $package_source_factory,
|
||||
OTGS_Installer_Plugin_Finder $plugin_finder,
|
||||
OTGS_Installer_Repositories $repositories,
|
||||
OTGS_Installer_Logger $logger,
|
||||
OTGS_Installer_Log_Factory $log_factory
|
||||
) {
|
||||
$this->package_source_factory = $package_source_factory;
|
||||
$this->plugin_finder = $plugin_finder;
|
||||
$this->repositories = $repositories;
|
||||
$this->logger = $logger;
|
||||
$this->log_factory = $log_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $repository_id
|
||||
* @param string $site_key
|
||||
* @param string $source
|
||||
*
|
||||
* @return array
|
||||
* @throws OTGS_Installer_Fetch_Subscription_Exception
|
||||
*/
|
||||
public function get( $repository_id, $site_key, $source ) {
|
||||
if ( ! $repository_id || ! $site_key || ! $source ) {
|
||||
throw new OTGS_Installer_Fetch_Subscription_Exception( 'Repository, site key and source are required fields.' );
|
||||
}
|
||||
|
||||
$subscription_data = false;
|
||||
$site_key_data = false;
|
||||
|
||||
$args['body'] = array(
|
||||
'action' => 'site_key_validation',
|
||||
'site_key' => $site_key,
|
||||
'site_url' => $this->get_installer_site_url( $repository_id ),
|
||||
'source' => $source
|
||||
);
|
||||
|
||||
if ( $repository_id === 'wpml' ) {
|
||||
$args['body']['using_icl'] = function_exists( 'wpml_site_uses_icl' ) && wpml_site_uses_icl();
|
||||
$args['body']['wpml_version'] = defined( 'ICL_SITEPRESS_VERSION' ) ? ICL_SITEPRESS_VERSION : '';
|
||||
}
|
||||
|
||||
$args['body']['installer_version'] = WP_INSTALLER_VERSION;
|
||||
$args['body']['theme'] = wp_get_theme()->get( 'Name' );
|
||||
$args['body']['site_name'] = get_bloginfo( 'name' );
|
||||
$args['body']['repository_id'] = $repository_id;
|
||||
$args['body']['versions'] = $this->get_local_product_versions();
|
||||
$args['timeout'] = 45;
|
||||
|
||||
$package_source = $this->package_source_factory->create()->get();
|
||||
|
||||
// Add extra parameters for custom Installer packages
|
||||
if ( $package_source ) {
|
||||
$extra = $this->get_extra_url_parameters( $package_source );
|
||||
if ( ! empty( $extra['repository'] ) && $extra['repository'] == $repository_id ) {
|
||||
unset( $extra['repository'] );
|
||||
foreach ( $extra as $key => $val ) {
|
||||
$args['body'][ $key ] = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$repository = $this->repositories->get( $repository_id );
|
||||
|
||||
$valid_response = null;
|
||||
$valid_body = null;
|
||||
$api_url = null;
|
||||
|
||||
foreach ( array( $repository->get_api_url(), $repository->get_api_url( false ) ) as $api_url ) {
|
||||
$valid_response = false;
|
||||
$valid_body = false;
|
||||
|
||||
$response = wp_remote_post(
|
||||
$api_url,
|
||||
apply_filters( 'installer_fetch_subscription_data_request', $args )
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$valid_response = true;
|
||||
|
||||
$body = trim( wp_remote_retrieve_body( $response ) );
|
||||
|
||||
if ( ! $body || ! is_serialized( $body ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$valid_body = true;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
$this->logger->add_api_log( "POST {$api_url}" );
|
||||
$this->logger->add_api_log( $args );
|
||||
|
||||
$this->logger->add_log( "POST {$api_url} - fetch subscription data" );
|
||||
|
||||
if ( $valid_response ) {
|
||||
if ( $valid_body ) {
|
||||
$data = unserialize( $body );
|
||||
$this->logger->add_api_log( $data );
|
||||
|
||||
if ( isset( $data->subscription_data ) && $data->subscription_data ) {
|
||||
$subscription_data = $data->subscription_data;
|
||||
} else {
|
||||
$this->store_log( $args, $api_url, isset( $data->error ) ? $data->error : '' );
|
||||
}
|
||||
|
||||
if ( isset( $data->site_key ) && $data->site_key ) {
|
||||
$site_key_data = $data->site_key;
|
||||
}
|
||||
|
||||
do_action( 'installer_fetched_subscription_data', $data, $repository_id );
|
||||
} else {
|
||||
if ( is_wp_error( $response ) ) {
|
||||
$response_message = $response->get_error_message();
|
||||
} else {
|
||||
$response_message = wp_remote_retrieve_response_message( $response );
|
||||
}
|
||||
$this->store_log( $args, $api_url, $response_message );
|
||||
$this->logger->add_api_log( $body );
|
||||
}
|
||||
|
||||
} else {
|
||||
$this->store_log( $args, $api_url, $response->get_error_message() );
|
||||
$this->logger->add_api_log( $response );
|
||||
throw new OTGS_Installer_Fetch_Subscription_Exception( $response->get_error_message() );
|
||||
}
|
||||
|
||||
return [$subscription_data, $site_key_data];
|
||||
}
|
||||
|
||||
private function store_log( $args, $request_url, $response ) {
|
||||
$log = $this->log_factory->create();
|
||||
$log->set_request_args( $args )
|
||||
->set_request_url( $request_url )
|
||||
->set_response( $response )
|
||||
->set_component( OTGS_Installer_Logger_Storage::COMPONENT_SUBSCRIPTION );
|
||||
|
||||
$this->logger->save_log( $log );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function get_local_product_versions() {
|
||||
$installed_plugins = $this->plugin_finder->get_otgs_installed_plugins();
|
||||
$versions = array();
|
||||
|
||||
foreach ( $installed_plugins as $plugin ) {
|
||||
$versions[ $plugin->get_slug() ] = $plugin->get_installed_version();
|
||||
}
|
||||
|
||||
return $versions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $source
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_extra_url_parameters( $source ) {
|
||||
if ( $source ) {
|
||||
$parameters = $source;
|
||||
}
|
||||
|
||||
$parameters['installer_version'] = WP_INSTALLER_VERSION;
|
||||
$parameters['theme'] = wp_get_theme()->get( 'Name' );
|
||||
$parameters['site_name'] = get_bloginfo( 'name' );
|
||||
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $repository_id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_installer_site_url( $repository_id = false ) {
|
||||
global $current_site;
|
||||
|
||||
$site_url = get_site_url();
|
||||
|
||||
if ( $repository_id && is_multisite() && $this->repositories->get_all() ) {
|
||||
$network_settings = maybe_unserialize( get_site_option( 'wp_installer_network' ) );
|
||||
|
||||
if ( isset( $network_settings[ $repository_id ] ) ) {
|
||||
$site_url = get_site_url( $current_site->blog_id );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$filtered_site_url = filter_var( apply_filters( 'otgs_installer_site_url', $site_url ), FILTER_SANITIZE_URL );
|
||||
|
||||
return $filtered_site_url ? $filtered_site_url : $site_url;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
|
||||
class OTGS_Installer_Site_Key_Ajax {
|
||||
|
||||
private $subscription_fetch;
|
||||
private $logger;
|
||||
private $repositories;
|
||||
private $subscription_factory;
|
||||
|
||||
public function __construct(
|
||||
OTGS_Installer_Fetch_Subscription $subscription_fetch,
|
||||
OTGS_Installer_Logger $logger,
|
||||
OTGS_Installer_Repositories $repositories,
|
||||
OTGS_Installer_Subscription_Factory $subscription_factory
|
||||
) {
|
||||
$this->subscription_fetch = $subscription_fetch;
|
||||
$this->logger = $logger;
|
||||
$this->repositories = $repositories;
|
||||
$this->subscription_factory = $subscription_factory;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_action( 'wp_ajax_save_site_key', array( $this, 'save' ) );
|
||||
add_action( 'wp_ajax_remove_site_key', array( $this, 'remove' ) );
|
||||
add_action( 'wp_ajax_update_site_key', array( $this, 'update' ) );
|
||||
add_action( 'wp_ajax_find_account', [ $this, 'find' ] );
|
||||
}
|
||||
|
||||
public function save() {
|
||||
$repository = isset( $_POST['repository_id'] ) && $_POST['repository_id'] ? sanitize_text_field( $_POST['repository_id'] ) : null;
|
||||
$nonce = isset( $_POST['nonce'] ) && $_POST['nonce'] ? sanitize_text_field( $_POST['nonce'] ) : null;
|
||||
$site_key = isset( $_POST[ 'site_key_' . $repository ] ) && $_POST[ 'site_key_' . $repository ] ? sanitize_text_field( $_POST[ 'site_key_' . $repository ] ) : null;
|
||||
$site_key = preg_replace( '/[^A-Za-z0-9]/', '', $site_key );
|
||||
$error = '';
|
||||
|
||||
if ( ! $site_key ) {
|
||||
wp_send_json_success( [ 'error' => esc_html__( 'Empty site key!', 'installer' ) ] );
|
||||
return;
|
||||
}
|
||||
if ( ! $repository || ! $nonce || ! wp_verify_nonce( $nonce, 'save_site_key_' . $repository ) ) {
|
||||
wp_send_json_success( [ 'error' => esc_html__( 'Invalid request!', 'installer' ) ] );
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
list ($subscription, $site_key_data) = $this->subscription_fetch->get( $repository, $site_key, WP_Installer::SITE_KEY_VALIDATION_SOURCE_REGISTRATION );
|
||||
if ( $subscription ) {
|
||||
$subscription_data = $this->subscription_factory->create( array(
|
||||
'data' => $subscription,
|
||||
'key' => $site_key,
|
||||
'key_type' => isset($site_key_data['type'])
|
||||
? (int) $site_key_data['type'] : OTGS_Installer_Subscription::SITE_KEY_TYPE_PRODUCTION,
|
||||
'site_url' => get_site_url(),
|
||||
'registered_by' => get_current_user_id()
|
||||
) );
|
||||
|
||||
$repository = $this->repositories->get( $repository );
|
||||
$repository->set_subscription( $subscription_data );
|
||||
$this->repositories->save_subscription( $repository );
|
||||
$this->repositories->refresh();
|
||||
$this->clean_plugins_update_cache();
|
||||
do_action( 'otgs_installer_site_key_update', $repository->get_id() );
|
||||
} else {
|
||||
$error = __( 'Invalid site key for the current site.', 'installer' ) . '<br /><div class="installer-footnote">' . __( 'Please note that the site key is case sensitive.', 'installer' ) . '</div>';
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
$repository_data = $this->repositories->get( $repository );
|
||||
$error = $this->get_error_message( $e, $repository_data );
|
||||
}
|
||||
|
||||
$response = array( 'error' => $error );
|
||||
|
||||
if ( $this->logger->get_api_log() ) {
|
||||
$response['debug'] = $this->logger->get_api_log();
|
||||
}
|
||||
|
||||
wp_send_json_success( $response );
|
||||
}
|
||||
|
||||
public function remove() {
|
||||
$repository = isset( $_POST['repository_id'] ) ? sanitize_text_field( $_POST['repository_id'] ) : null;
|
||||
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : null;
|
||||
$nonce_action = 'remove_site_key_' . $repository;
|
||||
|
||||
if ( wp_verify_nonce( $nonce, $nonce_action ) ) {
|
||||
$repository = $this->repositories->get( $repository );
|
||||
$repository->set_subscription( null );
|
||||
$this->repositories->save_subscription( $repository );
|
||||
|
||||
$this->clean_plugins_update_cache();
|
||||
do_action( 'otgs_installer_site_key_update', $repository->get_id() );
|
||||
}
|
||||
|
||||
$this->repositories->refresh();
|
||||
wp_send_json_success();
|
||||
}
|
||||
|
||||
public function update() {
|
||||
$error = '';
|
||||
$nonce = isset( $_POST['nonce'] ) ? $_POST['nonce'] : null;
|
||||
$repository = isset( $_POST['repository_id'] ) ? sanitize_text_field( $_POST['repository_id'] ) : null;
|
||||
|
||||
if ( $nonce && $repository && wp_verify_nonce( $nonce, 'update_site_key_' . $repository ) ) {
|
||||
$repository_data = $this->repositories->get( $repository );
|
||||
$site_key = $repository_data->get_subscription()->get_site_key();
|
||||
|
||||
if ( $site_key ) {
|
||||
try {
|
||||
list ($subscription, $site_key_data) = $this->subscription_fetch->get( $repository, $site_key, WP_Installer::SITE_KEY_VALIDATION_SOURCE_REGISTRATION );
|
||||
|
||||
if ( $subscription ) {
|
||||
$subscription_data = $this->subscription_factory->create( array(
|
||||
'data' => $subscription,
|
||||
'key' => $site_key,
|
||||
'key_type' => isset($site_key_data['type'])
|
||||
? (int) $site_key_data['type'] : OTGS_Installer_Subscription::SITE_KEY_TYPE_PRODUCTION,
|
||||
'site_url' => get_site_url(),
|
||||
'registered_by' => get_current_user_id(),
|
||||
) );
|
||||
$repository_data->set_subscription( $subscription_data );
|
||||
} else {
|
||||
$repository_data->set_subscription( null );
|
||||
$error = __( 'Invalid site key for the current site. If the error persists, try to un-register first and then register again with the same site key.', 'installer' );
|
||||
}
|
||||
|
||||
$this->repositories->save_subscription( $repository_data );
|
||||
$messages = $this->repositories->refresh( true );
|
||||
|
||||
if ( is_array( $messages ) ) {
|
||||
$error .= implode( '', $messages );
|
||||
}
|
||||
|
||||
|
||||
$this->clean_plugins_update_cache();
|
||||
} catch ( Exception $e ) {
|
||||
$error = $this->get_error_message( $e, $repository_data );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
wp_send_json_success( array( 'error' => $error ) );
|
||||
}
|
||||
|
||||
public function find() {
|
||||
$repository = isset( $_POST['repository_id'] ) ? sanitize_text_field( $_POST['repository_id'] ) : null;
|
||||
$nonce = isset( $_POST['nonce'] ) ? $_POST['nonce'] : null;
|
||||
$email = isset( $_POST['email'] ) ? sanitize_text_field( $_POST['email'] ) : null;
|
||||
$success = false;
|
||||
|
||||
if ( $nonce && $repository && $email && wp_verify_nonce( $nonce, 'find_account_' . $repository ) ) {
|
||||
$repository_data = $this->repositories->get( $repository );
|
||||
$siteKey = $repository_data->get_subscription()->get_site_key();
|
||||
|
||||
$args['body'] = [
|
||||
'action' => 'user_email_exists',
|
||||
'umail' => MD5( $email . $siteKey ),
|
||||
'site_key' => $siteKey,
|
||||
'site_url' => get_site_url()
|
||||
];
|
||||
|
||||
$response = wp_remote_post( $repository_data->get_api_url(), $args );
|
||||
if ( $response ) {
|
||||
$body = json_decode( wp_remote_retrieve_body( $response ) );
|
||||
$success = isset( $body->success ) ? 'Success' === $body->success : false;
|
||||
}
|
||||
}
|
||||
|
||||
wp_send_json_success( [ 'found' => $success ] );
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function get_error_message( Exception $e, OTGS_Installer_Repository $repository_data ) {
|
||||
$error = $e->getMessage();
|
||||
if ( preg_match( '#Could not resolve host: (.*)#', $error, $matches ) || preg_match( '#Couldn\'t resolve host \'(.*)\'#', $error, $matches ) ) {
|
||||
$error = sprintf( __( "%s cannot access %s to register. Try again to see if it's a temporary problem. If the problem continues, make sure that this site has access to the Internet. You can still use the plugin without registration, but you will not receive automated updates.", 'installer' ),
|
||||
'<strong><i>' . $repository_data->get_product_name() . '</i></strong>',
|
||||
'<strong><i>' . $matches[1] . '</i></strong>'
|
||||
);
|
||||
}
|
||||
|
||||
return $error;
|
||||
}
|
||||
|
||||
private function clean_plugins_update_cache() {
|
||||
do_action( 'otgs_installer_clean_plugins_update_cache' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user