Skip to content

Commit ddeee53

Browse files
Revamp settings screen API registration and key update.
1 parent d2f16fa commit ddeee53

18 files changed

Lines changed: 400 additions & 262 deletions

src/class-tiny-compress-client.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public function limit_reached() {
5858

5959
protected function validate() {
6060
try {
61-
$this->set_request_options( \Tinify\Tinify::getClient() );
6261
$this->last_error_code = 0;
62+
$this->set_request_options( \Tinify\Tinify::getClient() );
6363

6464
\Tinify\Tinify::getClient()->request( 'post', '/shrink' );
6565
return true;
@@ -81,8 +81,8 @@ protected function validate() {
8181

8282
protected function compress($input, $resize_opts, $preserve_opts) {
8383
try {
84-
$this->set_request_options( \Tinify\Tinify::getClient() );
8584
$this->last_error_code = 0;
85+
$this->set_request_options( \Tinify\Tinify::getClient() );
8686

8787
$source = \Tinify\fromBuffer( $input );
8888

@@ -126,8 +126,10 @@ protected function compress($input, $resize_opts, $preserve_opts) {
126126

127127
public function create_key($email, $options) {
128128
try {
129-
$this->set_request_options( \Tinify\Tinify::getClient() );
130129
$this->last_error_code = 0;
130+
$this->set_request_options(
131+
\Tinify\Tinify::getClient(\Tinify\Tinify::ANONYMOUS)
132+
);
131133

132134
\Tinify\createKey( $email, $options );
133135
} catch(\Tinify\Exception $err) {

src/class-tiny-compress.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function get_status() {
7474
if ( $err->get_status() == 401 ) {
7575
$message = 'The key that you have entered is not valid';
7676
} else {
77-
list($message) = explode( ' (HTTP', $err->getMessage(), 2 );
77+
list( $message ) = explode( ' (HTTP', $err->getMessage(), 2 );
7878
}
7979
}
8080

src/class-tiny-exception.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
*/
2020

2121
class Tiny_Exception extends Exception {
22+
protected $type;
23+
protected $status;
24+
2225
public function __construct($message, $type = null, $status = null) {
2326
if ( ! is_string( $message ) || ($type && ! is_string( $type )) ) {
2427
throw new InvalidArgumentException(

src/class-tiny-image.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ public function get_mime_type() {
141141
}
142142

143143
public function compress( $settings ) {
144-
145144
if ( $settings->get_compressor() === null || ! $this->file_type_allowed() ) {
146145
return;
147146
}
@@ -283,7 +282,6 @@ public function get_savings( $stats ) {
283282
}
284283

285284
public function get_statistics() {
286-
287285
if ( $this->statistics ) {
288286
error_log( 'Strangely the image statistics are asked for again.' );
289287
return $this->statistics;

src/class-tiny-notices.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020

2121
class Tiny_Notices extends Tiny_WP_Base {
22-
2322
private $notices;
2423
private $dismissals;
2524

src/class-tiny-settings.php

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,43 @@ public function xmlrpc_init() {
4949
}
5050

5151
public function admin_init() {
52-
if ( current_user_can( 'manage_options' ) && ! $this->get_api_key() ) {
53-
$link = sprintf(
54-
'<a href="options-media.php#%s">%s</a>', self::NAME,
55-
esc_html__(
52+
if ( current_user_can( 'manage_options' ) ) {
53+
if ( ! $this->get_api_key() ) {
54+
$notice_class = 'error';
55+
$notice = esc_html__(
5656
'Please register or provide an API key to start compressing images',
5757
'tiny-compress-images'
58-
)
59-
);
60-
$this->notices->show( 'setting', $link, 'error', false );
61-
}
58+
);
59+
} else if ( $this->get_api_key_pending() ) {
60+
$notice_class = 'notice-warning';
61+
$notice = esc_html__(
62+
'Please activate your account to start compressing images',
63+
'tiny-compress-images'
64+
);
65+
}
6266

63-
if ( current_user_can( 'manage_options' ) && ! Tiny_PHP::client_supported() ) {
64-
$details = 'PHP ' . PHP_VERSION;
65-
if ( extension_loaded( 'curl' ) ) {
66-
$curlinfo = curl_version();
67-
$details .= ' with curl ' . $curlinfo['version'];
68-
} else {
69-
$details .= ' without curl';
67+
if ( $notice ) {
68+
$link = sprintf(
69+
'<a href="options-media.php#%s">%s</a>', self::NAME, $notice
70+
);
71+
72+
$this->notices->show( 'setting', $link, $notice_class, false );
73+
}
74+
75+
if ( ! Tiny_PHP::client_supported() ) {
76+
$details = 'PHP ' . PHP_VERSION;
77+
if ( extension_loaded( 'curl' ) ) {
78+
$curlinfo = curl_version();
79+
$details .= ' with curl ' . $curlinfo['version'];
80+
} else {
81+
$details .= ' without curl';
82+
}
83+
$message = esc_html__(
84+
'You are using an outdated platform (' . $details .
85+
') – some features are disabled', 'tiny-compress-images'
86+
);
87+
$this->notices->show( 'deprecated', $message, 'notice-warning', false );
7088
}
71-
$message = esc_html__(
72-
'You are using an outdated platform (' . $details .
73-
') – some features are disabled', 'tiny-compress-images'
74-
);
75-
$this->notices->show( 'setting', $message, 'notice-warning', false );
7689
}
7790

7891
try {
@@ -95,13 +108,13 @@ public function admin_init() {
95108
$field = self::get_prefixed_name( 'api_key' );
96109
register_setting( 'media', $field );
97110
add_settings_field( $field,
98-
esc_html__( 'Connection status', 'tiny-compress-images' ),
111+
esc_html__( 'TinyPNG account', 'tiny-compress-images' ),
99112
$this->get_method( 'render_pending_status' ),
100113
'media',
101114
$section
102115
);
103116

104-
$field = self::get_prefixed_name( 'api_key_automated' );
117+
$field = self::get_prefixed_name( 'api_key_pending' );
105118
register_setting( 'media', $field );
106119

107120
$field = self::get_prefixed_name( 'sizes' );
@@ -136,8 +149,8 @@ public function admin_init() {
136149
);
137150

138151
add_action(
139-
'wp_ajax_tiny_compress_status',
140-
$this->get_method( 'connection_status' )
152+
'wp_ajax_tiny_account_status',
153+
$this->get_method( 'account_status' )
141154
);
142155

143156
add_action(
@@ -160,8 +173,8 @@ public function image_sizes_notice() {
160173
exit();
161174
}
162175

163-
public function connection_status() {
164-
$this->render_status();
176+
public function account_status() {
177+
$this->render_account_status();
165178
exit();
166179
}
167180

@@ -185,6 +198,14 @@ protected function get_api_key() {
185198
}
186199
}
187200

201+
protected function get_api_key_pending() {
202+
if ( defined( 'TINY_API_KEY' ) ) {
203+
return false;
204+
} else {
205+
return get_option( self::get_prefixed_name( 'api_key_pending' ) );
206+
}
207+
}
208+
188209
protected static function get_intermediate_size($size) {
189210
/* Inspired by
190211
http://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes */
@@ -480,6 +501,7 @@ public function after_compress_callback($compressor) {
480501
if ( $compressor->limit_reached() ) {
481502
$link = '<a href="https://tinypng.com/developers" target="_blank">' .
482503
esc_html__( 'TinyPNG API account', 'tiny-compress-images' ) . '</a>';
504+
483505
$this->notices->add('limit-reached',
484506
sprintf(
485507
esc_html__(
@@ -501,67 +523,71 @@ public function after_compress_callback($compressor) {
501523
}
502524
}
503525

504-
public function render_status() {
526+
public function render_account_status() {
505527
$key = $this->get_api_key();
506528
if ( empty( $key ) ) {
507529
include( dirname( __FILE__ ) . '/views/account-status-missing.php' );
508530
} else {
509531
$status = $this->compressor->get_status();
510-
if ( ! $status->ok && $status->code == 401 ) {
511-
$field = self::get_prefixed_name( 'api_key_automated' );
512-
if ( get_option( $field ) ) {
532+
$status->pending = false;
533+
534+
if ( $status->ok ) {
535+
if ( $this->get_api_key_pending() ) {
536+
delete_option( $pending_option );
537+
}
538+
} else {
539+
if ( $this->get_api_key_pending() ) {
513540
$status->ok = true;
541+
$status->pending = true;
514542
$status->message = (
515543
'An email has been sent with a link to activate your account'
516544
);
517545
}
518546
}
547+
548+
$name = trim( $current_user->user_firstname . ' ' . $current_user->user_lastname );
549+
$email = trim( $current_user->user_email );
550+
519551
include( dirname( __FILE__ ) . '/views/account-status-connected.php' );
520552
}
521553
}
522554

523555
public function render_pending_status() {
524-
include( dirname( __FILE__ ) . '/views/account-update-modal.php' );
525556
$key = $this->get_api_key();
526557
if ( empty( $key ) ) {
527-
echo '<div id="tiny-compress-status" data-state="missing">';
528558
include( dirname( __FILE__ ) . '/views/account-status-missing.php' );
529-
echo '</div>';
530559
} else {
531-
echo '<div id="tiny-compress-status" data-state="pending">';
532-
include( dirname( __FILE__ ) . '/views/account-status-pending.php' );
533-
echo '</div>';
560+
include( dirname( __FILE__ ) . '/views/account-status-loading.php' );
534561
}
535562
}
536563

537-
public function render_pending_savings() {
538-
echo '<div id="tiny-compress-savings"><div class="spinner"></div></div>';
539-
}
540-
541564
public function create_api_key() {
542565
$compressor = $this->get_compressor();
543566
if ( $compressor->can_create_key() ) {
544567
try {
545568
$site = str_replace( array( 'http://', 'https://' ), '', get_bloginfo( 'url' ) );
546569
$identifier = 'WordPress plugin for ' . $site;
547570
$link = $this->get_absolute_url();
548-
$compressor->create_key($_POST['email'], array(
571+
$compressor->create_key( $_POST['email'], array(
549572
'name' => $_POST['name'],
550573
'identifier' => $identifier,
551574
'link' => $link,
552-
));
553-
update_option( self::get_prefixed_name( 'api_key_automated' ), true );
575+
) );
576+
577+
update_option( self::get_prefixed_name( 'api_key_pending' ), true );
554578
update_option( self::get_prefixed_name( 'api_key' ), $compressor->get_key() );
555579
update_option( self::get_prefixed_name( 'status' ), 0 );
580+
556581
$status = (object) array(
557582
'ok' => true,
558583
'message' => null,
559584
'key' => $compressor->get_key(),
560585
);
561586
} catch (Tiny_Exception $err) {
587+
list( $message ) = explode( ' (HTTP', $err->getMessage(), 2 );
562588
$status = (object) array(
563589
'ok' => false,
564-
'message' => $err->getMessage(),
590+
'message' => $message,
565591
);
566592
}
567593
} else {
@@ -587,7 +613,7 @@ public function update_api_key() {
587613
$status = Tiny_Compress::create( $key )->get_status();
588614
}
589615
if ( $status->ok ) {
590-
update_option( self::get_prefixed_name( 'api_key_automated' ), false );
616+
update_option( self::get_prefixed_name( 'api_key_pending' ), false );
591617
update_option( self::get_prefixed_name( 'api_key' ), $key );
592618
}
593619
$status->message = esc_html__( $status->message, 'tiny-compress-images' );

0 commit comments

Comments
 (0)