-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-key.php
390 lines (351 loc) · 10.7 KB
/
api-key.php
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
class Memberful_Updater_API_Key {
/**
* The one instance of Memberful_Updater_API_Key.
*
* @since 1.0.0.
*
* @var Memberful_Updater_API_Key
*/
private static $instance;
/**
* Instantiate or return the one Memberful_Updater_API_Key instance.
*
* @since 1.0.0.
*
* @return Memberful_Updater_API_Key
*/
public static function instance() {
if ( is_null( self::$instance ) )
self::$instance = new self();
return self::$instance;
}
/**
* Initiate the actions.
*
* @since 1.0.0.
*
* @return Memberful_Updater_API_Key
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_tool_page' ) );
add_action( 'admin_init', array( $this, 'add_settings' ) );
add_filter( 'wp_redirect', array( $this, 'handle_redirect_after_save' ), 99, 2 );
add_action( 'admin_footer', array( $this, 'display_message' ) );
}
/**
* Add a new admin page under the Appearance tab.
*
* @since 1.0.0.
*
* @return void
*/
public function add_tool_page() {
add_submenu_page(
'tools.php',
__( 'Updater API Key', 'memberful-updater' ),
__( 'Updater API Key', 'memberful-updater' ),
'manage_options',
'updater_auth_page',
array( $this, 'render_updater_auth_page' )
);
}
/**
* Render the theme page wrapper.
*
* @since 1.0.0.
*
* @return void
*/
public function render_updater_auth_page() {
$is_redirect = ( isset( $_GET['action'] ) && ( isset( $_GET['plugin'] ) || isset( $_GET['theme'] ) ) );
?>
<div class="wrap">
<h2><?php _e( 'Updater Authorization', 'memberful-updater' ); ?></h2>
<?php if ( $msg = get_transient( 'updater-auth-' . get_current_user_id() ) ) : ?>
<?php $status = ( isset( $msg['status'] ) ) ? $msg['status'] : ''; ?>
<div id="setting-error-settings_updated" class="<?php echo esc_attr( $status ); ?> settings-error">
<?php if ( isset( $msg['message'] ) ) : ?>
<p>
<?php echo esc_html( $msg['message'] ); ?>
</p>
<?php endif; ?>
<?php if ( isset( $msg['details'] ) ) : ?>
<ul>
<?php foreach ( $msg['details'] as $detail ) : ?>
<li>
- <?php echo esc_html( $detail->msg ); ?> (<?php echo esc_html( $detail->code ); ?>)
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<?php endif; ?>
<form action="options.php" method="post">
<?php settings_fields( 'memberful-api-key' ); ?>
<?php do_settings_sections( 'updater_auth_page' ); ?>
<p class="submit" class="updater-toggle">
<?php $label = ( true === $is_redirect ) ? __( 'Authorize Site and Complete Update', 'memberful-updater' ) : __( 'Authorize Site', 'memberful-updater' ); ?>
<input type="submit" name="submit" id="submit" class="button button-primary" value="<?php echo $label; ?>">
</p>
<?php if ( true === $is_redirect ) : ?>
<?php if ( isset( $_GET['plugin'] ) ) : ?>
<input type="hidden" name="redirect-plugin" value="<?php echo esc_attr( $_GET['plugin'] ); ?>">
<?php elseif ( isset( $_GET['theme'] ) ) : ?>
<input type="hidden" name="redirect-theme" value="<?php echo esc_attr( $_GET['theme'] ); ?>">
<?php endif; ?>
<input type="hidden" name="redirect-action" value="<?php echo esc_attr( $_GET['action'] ); ?>">
<?php wp_nonce_field( 'redirect', 'redirect-nonce' ); ?>
<?php endif; ?>
</div>
</form>
</div>
<script type="text/javascript">
(function ($) {
var $reAuth = $('.updater-re-auth'),
$formWrapper = $('.updater-form-wrapper');
$reAuth.on('click', function(evt) {
evt.preventDefault();
$formWrapper.show();
})
})(jQuery);
</script>
<?php delete_transient( 'updater-auth-' . get_current_user_id() ); ?>
<?php
}
/**
* Register the settings, sections, and field
*
* @since 1.0.0.
*
* @return void
*/
public function add_settings() {
// Register an option to flag whether or not the site is authorized
register_setting(
'memberful-api-key',
'memberful-api-key',
array( $this, 'authorize_theme_and_domain' )
);
// Add the settings section to hold the interface
add_settings_section(
'updater_settings',
'',
array( $this, 'render_input_section' ),
'updater_auth_page'
);
// Add the username field
add_settings_field(
'updater_render_email',
__( 'E-mail Address', 'memberful-updater' ),
array( $this, 'render_email' ),
'updater_auth_page',
'updater_settings'
);
// Add the password field
add_settings_field(
'updater_render_password',
__( 'Password', 'memberful-updater' ),
array( $this, 'render_password' ),
'updater_auth_page',
'updater_settings'
);
}
/**
* Render a heading for the section.
*
* @since 1.0.0.
*
* @return void
*/
public function render_input_section() {
$api_key = get_option( 'memberful-api-key', false );
if ( ! empty( $api_key ) ) {
printf(
'<h3 class="title updater-authed">%1$s <strong>%2$s</strong></h3>',
__( 'API Key:', 'memberful-updater' ),
$api_key
);
printf(
'<p>%s</p>',
__( 'Problems? <a href="#" class="updater-re-auth">Try authorizing again</a>.', 'memberful-updater' )
);
} else {
printf(
'<p>%s</p>',
__( 'Please authorize your theme for automatic updates with the same credentials you use to sign into our website. The authorization process will send your current domain and product information to authorize this site.', 'memberful-updater' )
);
}
?>
<div class="updater-form-wrapper"<?php if ( ! empty( $api_key) ) : ?> style="display:none;"<?php endif; ?>>
<?php
}
/**
* Render the email input.
*
* @since 1.0.0.
*
* @return void
*/
public function render_email() {
echo '<input type="text" name="updater-email" class="regular-text" value="" autofocus />';
}
/**
* Render the password input.
*
* @since 1.0.0.
*
* @return void
*/
public function render_password() {
echo '<input type="password" name="updater-password" class="regular-text" value="" />';
}
/**
* Authorize the theme and save the auth status on submission.
*
* @since 1.0.0.
*
* @param mixed $value The input value.
* @return bool The auth status.
*/
public function authorize_theme_and_domain( $value ) {
if ( ! isset( $_POST['updater-email'] ) || ! isset( $_POST['updater-password'] ) ) {
return 0;
}
// Grab the auth details
$email = $_POST['updater-email'];
$password = $_POST['updater-password'];
// Set the endpoint
$auth_endpoint = 'https://yourwebsite.com/key/auth';
// Sometimes HTTP requests need a little extra time locally
$timeout = ( defined( 'WP_DEBUG' ) && true === WP_DEBUG ) ? 20 : 5;
// Make the request
$response = wp_remote_post(
$auth_endpoint,
array(
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded',
),
'body' => array(
'email' => $email,
'password' => $password,
'domain' => str_replace( array( 'http://', 'https://' ), array( '', '' ), get_option( 'siteurl' ) ),
'item' => sanitize_title_with_dashes( memberful_get_updater()->config['slug'] ),
),
'timeout' => $timeout,
)
);
$response_code = (int) wp_remote_retrieve_response_code( $response );
$response_body = json_decode( wp_remote_retrieve_body( $response ) );
if ( 200 === $response_code && isset( $response_body->key ) ) {
$msg = array(
'status' => 'updated',
'message' => __( 'Your site is authorized for automatic updates from The Theme Foundry.', 'memberful-updater' ),
);
// Record the key
$key = $response_body->key;
if ( $key === preg_replace( '/[^a-z0-9]/i', '', $key ) ) {
$this->add_msg( $msg );
return $key;
} else {
$new_error = new stdClass();
$new_error->code = '513';
$new_error->msg = 'invalid key';
$errors[] = $new_error;
$msg = array(
'status' => 'error',
'message' => __( 'There was an error authorizing your site for automatic updates.', 'memberful-updater' ),
'details' => $errors,
);
$this->add_msg( $msg );
return false;
}
} else {
if ( is_wp_error( $response ) ) {
$errors = array();
foreach ( $response as $error ) {
foreach ( $error as $code => $data ) {
$new_error = new stdClass();
$new_error->code = $code;
$new_error->msg = $data[0];
$errors[] = $new_error;
}
}
} else {
$errors = (array) $response_body->errors;
}
$msg = array(
'status' => 'error',
'message' => __( 'There was an error authorizing your site for automatic updates.', 'memberful-updater' ),
'details' => $errors,
);
$this->add_msg( $msg );
return false;
}
}
/**
* Detect when authorizing for updates and handle redirects properly.
*
* @since 1.0.0.
*
* @param string $location The path to redirect to.
* @param int $status Status code to use.
* @return string The new URL to redirect to.
*/
public function handle_redirect_after_save( $location, $status ) {
// Is this screen accessed as part of the update process
if ( ! ( isset( $_POST['redirect-plugin'] ) || isset( $_POST['redirect-theme'] ) ) || ! isset( $_POST['redirect-action'] ) || ! wp_verify_nonce( $_POST['redirect-nonce'], 'redirect' ) ) {
return $location;
}
// Set up the GET args for the redirection
$args = array(
'action' => $_POST['redirect-action'],
);
if ( isset( $_POST['redirect-plugin'] ) ) {
$args['plugin'] = $_POST['redirect-plugin'];
$args['_wpnonce'] = wp_create_nonce( 'upgrade-plugin_' . $_POST['redirect-plugin'] );
} else if ( isset( $_POST['redirect-theme'] ) ) {
$args['theme'] = $_POST['redirect-theme'];
$args['_wpnonce'] = wp_create_nonce( 'upgrade-theme_' . $_POST['redirect-theme'] );
}
if ( 'upgrade-plugin' === $_POST['redirect-action'] || 'upgrade-theme' === $_POST['redirect-action'] ) {
return add_query_arg(
$args,
admin_url( 'update.php' )
);
} else {
$this->add_msg( 'You are now authorized for updates from The Theme Foundry. Please try your update again.' );
return admin_url( 'update-core.php' );
}
}
/**
* Output a message if necessary.
*
* @since 1.0.1.
*
* @return void
*/
public function display_message() {
global $pagenow;
if ( 'update-core.php' === $pagenow && $msg = get_transient( 'updater-auth-' . get_current_user_id() ) ) : ?>
<div id="api-auth-message" class="updated settings-error">
<p>
<?php echo esc_html( $msg ); ?>
</p>
</div>
<?php delete_transient( 'updater-auth-' . get_current_user_id() );
endif;
}
/**
* Save a message to display later.
*
* @since 1.0.0.
*
* @param array $message Notice details.
* @return void
*/
public function add_msg( $message ) {
set_transient( 'updater-auth-' . get_current_user_id(), $message, 30 );
}
}
Memberful_Updater_API_Key::instance();