]> _ Git - physioassist-wordpress.git/blob
3027fc080810151f152fbe50e793a60090a5b134
[physioassist-wordpress.git] /
1 <?php
2
3 class OTGS_Installer_Site_Key_Ajax {
4
5         private $subscription_fetch;
6         private $logger;
7         private $repositories;
8         private $subscription_factory;
9
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
15         ) {
16                 $this->subscription_fetch   = $subscription_fetch;
17                 $this->logger               = $logger;
18                 $this->repositories         = $repositories;
19                 $this->subscription_factory = $subscription_factory;
20         }
21
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' ] );
27         }
28
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 );
34                 $error      = '';
35
36                 if ( ! $repository || ! $nonce || ! $site_key || ! wp_verify_nonce( $nonce, 'save_site_key_' . $repository ) ) {
37                         wp_send_json_error( esc_html__( 'Invalid request!', 'installer' ) );
38                 }
39
40                 try {
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,
45                                         'key'           => $site_key,
46                                         'site_url'      => get_site_url(),
47                                         'registered_by' => get_current_user_id()
48                                 ) );
49
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();
55                         } else {
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>';
57                         }
58                 } catch ( Exception $e ) {
59                         $repository_data = $this->repositories->get( $repository );
60                         $error           = $this->get_error_message( $e, $repository_data );
61                 }
62
63                 $response = array( 'error' => $error );
64
65                 if ( $this->logger->get_api_log() ) {
66                         $response['debug'] = $this->logger->get_api_log();
67                 }
68
69                 wp_send_json_success( $response );
70         }
71
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;
76
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 );
81
82                         $this->clean_plugins_update_cache();
83                 }
84
85                 $this->repositories->refresh();
86                 wp_send_json_success();
87         }
88
89         public function update() {
90                 $error      = '';
91                 $nonce      = isset( $_POST['nonce'] ) ? $_POST['nonce'] : null;
92                 $repository = isset( $_POST['repository_id'] ) ? sanitize_text_field( $_POST['repository_id'] ) : null;
93
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();
97
98                         if ( $site_key ) {
99                                 try {
100                                         $subscription = $this->subscription_fetch->get( $repository, $site_key, WP_Installer::SITE_KEY_VALIDATION_SOURCE_REGISTRATION );
101
102                                         if ( $subscription ) {
103                                                 $subscription_data = $this->subscription_factory->create( array(
104                                                         'data'          => $subscription,
105                                                         'key'           => $site_key,
106                                                         'site_url'      => get_site_url(),
107                                                         'registered_by' => get_current_user_id(),
108                                                 ) );
109                                                 $repository_data->set_subscription( $subscription_data );
110                                         } else {
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' );
113                                         }
114
115                                         $this->repositories->save_subscription( $repository_data );
116                                         $messages = $this->repositories->refresh( true );
117
118                                         if ( is_array( $messages ) ) {
119                                                 $error .= implode( '', $messages );
120                                         }
121
122
123                                         $this->clean_plugins_update_cache();
124                                 } catch ( Exception $e ) {
125                                         $error = $this->get_error_message( $e, $repository_data );
126                                 }
127                         }
128
129                 }
130
131                 wp_send_json_success( array( 'error' => $error ) );
132         }
133
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;
138                 $success    = false;
139
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();
143
144                         $args['body'] = [
145                                 'action'   => 'user_email_exists',
146                                 'umail'    => MD5( $email . $siteKey ),
147                                 'site_key' => $siteKey,
148                                 'site_url' => get_site_url()
149                         ];
150
151                         $response = wp_remote_post( $repository_data->get_api_url(), $args );
152                         if ( $response ) {
153                                 $body    = json_decode( wp_remote_retrieve_body( $response ) );
154                                 $success = isset( $body->success ) ? 'Success' === $body->success : false;
155                         }
156                 }
157
158                 wp_send_json_success( [ 'found' => $success ] );
159
160         }
161
162
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>'
169                         );
170                 }
171
172                 return $error;
173         }
174
175         private function clean_plugins_update_cache() {
176                 do_action( 'otgs_installer_clean_plugins_update_cache' );
177         }
178 }