class-otgs-installer-site-key-ajax.php
8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
use OTGS\Installer\Subscription\SubscriptionManagerFactory;
class OTGS_Installer_Site_Key_Ajax {
private $logger;
private $repositories;
private $subscription_factory;
private $subscriptionManagerFactory;
public function __construct(
OTGS_Installer_Logger $logger,
OTGS_Installer_Repositories $repositories,
OTGS_Installer_Subscription_Factory $subscription_factory,
SubscriptionManagerFactory $subscriptionManagerFactory
) {
$this->logger = $logger;
$this->repositories = $repositories;
$this->subscription_factory = $subscription_factory;
$this->subscriptionManagerFactory = $subscriptionManagerFactory;
}
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() {
$repositoryId = 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_' . $repositoryId ] ) && $_POST[ 'site_key_' . $repositoryId ] ? sanitize_text_field( $_POST[ 'site_key_' . $repositoryId ] ) : 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 ( ! $repositoryId || ! $nonce || ! wp_verify_nonce( $nonce, 'save_site_key_' . $repositoryId ) ) {
wp_send_json_success( [ 'error' => esc_html__( 'Invalid request!', 'installer' ) ] );
return;
}
$repository = $this->repositories->get( $repositoryId );
try {
list ($subscription, $site_key_data) = $this->getSubscriptionData( $repositoryId, $repository, WP_Installer::SITE_KEY_VALIDATION_SOURCE_REGISTRATION, $site_key );
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->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 ) {
$error = $this->get_error_message( $e, $repository );
}
$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;
$repositoryId = isset( $_POST['repository_id'] ) ? sanitize_text_field( $_POST['repository_id'] ) : null;
if ( $nonce && $repositoryId && wp_verify_nonce( $nonce, 'update_site_key_' . $repositoryId ) ) {
$repository = $this->repositories->get( $repositoryId );
$site_key = $repository->get_subscription()->get_site_key();
if ( $site_key ) {
try {
list( $subscription, $site_key_data ) = $this->getSubscriptionData( $repositoryId, $repository, WP_Installer::SITE_KEY_VALIDATION_SOURCE_REVALIDATION, $site_key );
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->set_subscription( $subscription_data );
} else {
$repository->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 );
$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 );
}
}
}
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' );
}
/**
* @param $repositoryId
* @param OTGS_Installer_Repository $repository
* @param $site_key
*
* @return array
* @throws OTGS_Installer_Fetch_Subscription_Exception
* @throws \OTGS\Installer\Api\Exception\InvalidProductBucketUrl
*/
private function getSubscriptionData( $repositoryId, OTGS_Installer_Repository $repository, $source, $site_key ) {
$subscriptionManager = $this->subscriptionManagerFactory->create( $repositoryId, $repository->get_api_url() );
list ( $subscription, $site_key_data ) = $subscriptionManager->fetch( $site_key, $source );
return array( $subscription, $site_key_data );
}
}