3 class OTGS_Installer_Site_Key_Ajax {
5 private $subscription_fetch;
8 private $subscription_factory;
10 public function __construct(
11 OTGS_Installer_Fetch_Subscription $subscription_fetch,
12 OTGS_Installer_Logger $logger,
13 OTGS_Installer_Repositories $repositories,
14 OTGS_Installer_Subscription_Factory $subscription_factory
16 $this->subscription_fetch = $subscription_fetch;
17 $this->logger = $logger;
18 $this->repositories = $repositories;
19 $this->subscription_factory = $subscription_factory;
22 public function add_hooks() {
23 add_action( 'wp_ajax_save_site_key', array( $this, 'save' ) );
24 add_action( 'wp_ajax_remove_site_key', array( $this, 'remove' ) );
25 add_action( 'wp_ajax_update_site_key', array( $this, 'update' ) );
26 add_action( 'wp_ajax_find_account', [ $this, 'find' ] );
29 public function save() {
30 $repository = isset( $_POST['repository_id'] ) && $_POST['repository_id'] ? sanitize_text_field( $_POST['repository_id'] ) : null;
31 $nonce = isset( $_POST['nonce'] ) && $_POST['nonce'] ? sanitize_text_field( $_POST['nonce'] ) : null;
32 $site_key = isset( $_POST[ 'site_key_' . $repository ] ) && $_POST[ 'site_key_' . $repository ] ? sanitize_text_field( $_POST[ 'site_key_' . $repository ] ) : null;
33 $site_key = preg_replace( '/[^A-Za-z0-9]/', '', $site_key );
36 if ( ! $repository || ! $nonce || ! $site_key || ! wp_verify_nonce( $nonce, 'save_site_key_' . $repository ) ) {
37 wp_send_json_error( esc_html__( 'Invalid request!', 'installer' ) );
41 $subscription = $this->subscription_fetch->get( $repository, $site_key, WP_Installer::SITE_KEY_VALIDATION_SOURCE_REGISTRATION );
42 if ( $subscription ) {
43 $subscription_data = $this->subscription_factory->create( array(
44 'data' => $subscription,
46 'site_url' => get_site_url(),
47 'registered_by' => get_current_user_id()
50 $repository = $this->repositories->get( $repository );
51 $repository->set_subscription( $subscription_data );
52 $this->repositories->save_subscription( $repository );
53 $this->repositories->refresh();
54 $this->clean_plugins_update_cache();
56 $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>';
58 } catch ( Exception $e ) {
59 $repository_data = $this->repositories->get( $repository );
60 $error = $this->get_error_message( $e, $repository_data );
63 $response = array( 'error' => $error );
65 if ( $this->logger->get_api_log() ) {
66 $response['debug'] = $this->logger->get_api_log();
69 wp_send_json_success( $response );
72 public function remove() {
73 $repository = isset( $_POST['repository_id'] ) ? sanitize_text_field( $_POST['repository_id'] ) : null;
74 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : null;
75 $nonce_action = 'remove_site_key_' . $repository;
77 if ( wp_verify_nonce( $nonce, $nonce_action ) ) {
78 $repository = $this->repositories->get( $repository );
79 $repository->set_subscription( null );
80 $this->repositories->save_subscription( $repository );
82 $this->clean_plugins_update_cache();
85 $this->repositories->refresh();
86 wp_send_json_success();
89 public function update() {
91 $nonce = isset( $_POST['nonce'] ) ? $_POST['nonce'] : null;
92 $repository = isset( $_POST['repository_id'] ) ? sanitize_text_field( $_POST['repository_id'] ) : null;
94 if ( $nonce && $repository && wp_verify_nonce( $nonce, 'update_site_key_' . $repository ) ) {
95 $repository_data = $this->repositories->get( $repository );
96 $site_key = $repository_data->get_subscription()->get_site_key();
100 $subscription = $this->subscription_fetch->get( $repository, $site_key, WP_Installer::SITE_KEY_VALIDATION_SOURCE_REGISTRATION );
102 if ( $subscription ) {
103 $subscription_data = $this->subscription_factory->create( array(
104 'data' => $subscription,
106 'site_url' => get_site_url(),
107 'registered_by' => get_current_user_id(),
109 $repository_data->set_subscription( $subscription_data );
111 $repository_data->set_subscription( null );
112 $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' );
115 $this->repositories->save_subscription( $repository_data );
116 $messages = $this->repositories->refresh( true );
118 if ( is_array( $messages ) ) {
119 $error .= implode( '', $messages );
123 $this->clean_plugins_update_cache();
124 } catch ( Exception $e ) {
125 $error = $this->get_error_message( $e, $repository_data );
131 wp_send_json_success( array( 'error' => $error ) );
134 public function find() {
135 $repository = isset( $_POST['repository_id'] ) ? sanitize_text_field( $_POST['repository_id'] ) : null;
136 $nonce = isset( $_POST['nonce'] ) ? $_POST['nonce'] : null;
137 $email = isset( $_POST['email'] ) ? sanitize_text_field( $_POST['email'] ) : null;
140 if ( $nonce && $repository && $email && wp_verify_nonce( $nonce, 'find_account_' . $repository ) ) {
141 $repository_data = $this->repositories->get( $repository );
142 $siteKey = $repository_data->get_subscription()->get_site_key();
145 'action' => 'user_email_exists',
146 'umail' => MD5( $email . $siteKey ),
147 'site_key' => $siteKey,
148 'site_url' => get_site_url()
151 $response = wp_remote_post( $repository_data->get_api_url(), $args );
153 $body = json_decode( wp_remote_retrieve_body( $response ) );
154 $success = isset( $body->success ) ? 'Success' === $body->success : false;
158 wp_send_json_success( [ 'found' => $success ] );
163 private function get_error_message( Exception $e, OTGS_Installer_Repository $repository_data ) {
164 $error = $e->getMessage();
165 if ( preg_match( '#Could not resolve host: (.*)#', $error, $matches ) || preg_match( '#Couldn\'t resolve host \'(.*)\'#', $error, $matches ) ) {
166 $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' ),
167 '<strong><i>' . $repository_data->get_product_name() . '</i></strong>',
168 '<strong><i>' . $matches[1] . '</i></strong>'
175 private function clean_plugins_update_cache() {
176 do_action( 'otgs_installer_clean_plugins_update_cache' );