Skip to content

Commit f51e759

Browse files
Various improvements and integration test coverage.
1 parent 6c2e100 commit f51e759

13 files changed

Lines changed: 390 additions & 138 deletions

src/class-tiny-settings.php

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ protected function get_api_key_pending() {
205205
}
206206
}
207207

208+
protected function clear_api_key_pending() {
209+
delete_option( self::get_prefixed_name( 'api_key_pending' ) );
210+
}
211+
208212
protected static function get_intermediate_size($size) {
209213
/* Inspired by
210214
http://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes */
@@ -434,9 +438,7 @@ public function render_resize() {
434438
'tiny-compress-images'
435439
);
436440

437-
echo '</p>';
438-
439-
echo '<br>';
441+
echo '</p><br>';
440442

441443
$this->render_preserve_input(
442444
'creation',
@@ -525,14 +527,19 @@ public function after_compress_callback($compressor) {
525527
public function render_account_status() {
526528
$key = $this->get_api_key();
527529
if ( empty( $key ) ) {
528-
include( dirname( __FILE__ ) . '/views/account-status-missing.php' );
530+
$compressor = $this->get_compressor();
531+
if ( $compressor->can_create_key() ) {
532+
include( dirname( __FILE__ ) . '/views/account-status-create-advanced.php' );
533+
} else {
534+
include( dirname( __FILE__ ) . '/views/account-status-create-simple.php' );
535+
}
529536
} else {
530537
$status = $this->compressor->get_status();
531538
$status->pending = false;
532539

533540
if ( $status->ok ) {
534541
if ( $this->get_api_key_pending() ) {
535-
delete_option( $pending_option );
542+
$this->clear_api_key_pending();
536543
}
537544
} else {
538545
if ( $this->get_api_key_pending() ) {
@@ -544,17 +551,19 @@ public function render_account_status() {
544551
}
545552
}
546553

547-
$name = trim( $current_user->user_firstname . ' ' . $current_user->user_lastname );
548-
$email = trim( $current_user->user_email );
549-
550554
include( dirname( __FILE__ ) . '/views/account-status-connected.php' );
551555
}
552556
}
553557

554558
public function render_pending_status() {
555559
$key = $this->get_api_key();
556560
if ( empty( $key ) ) {
557-
include( dirname( __FILE__ ) . '/views/account-status-missing.php' );
561+
$compressor = $this->get_compressor();
562+
if ( $compressor->can_create_key() ) {
563+
include( dirname( __FILE__ ) . '/views/account-status-create-advanced.php' );
564+
} else {
565+
include( dirname( __FILE__ ) . '/views/account-status-create-simple.php' );
566+
}
558567
} else {
559568
include( dirname( __FILE__ ) . '/views/account-status-loading.php' );
560569
}
@@ -563,6 +572,28 @@ public function render_pending_status() {
563572
public function create_api_key() {
564573
$compressor = $this->get_compressor();
565574
if ( $compressor->can_create_key() ) {
575+
if ( ! isset( $_POST['name'] ) || ! $_POST['name'] ) {
576+
$status = (object) array(
577+
'ok' => false,
578+
'message' => __(
579+
'Please enter your name', 'tiny-compress-images'
580+
),
581+
);
582+
echo json_encode( $status );
583+
exit();
584+
}
585+
586+
if ( ! isset( $_POST['email'] ) || ! $_POST['email'] ) {
587+
$status = (object) array(
588+
'ok' => false,
589+
'message' => __(
590+
'Please enter your email address', 'tiny-compress-images'
591+
),
592+
);
593+
echo json_encode( $status );
594+
exit();
595+
}
596+
566597
try {
567598
$site = str_replace( array( 'http://', 'https://' ), '', get_bloginfo( 'url' ) );
568599
$identifier = 'WordPress plugin for ' . $site;
@@ -580,7 +611,6 @@ public function create_api_key() {
580611
$status = (object) array(
581612
'ok' => true,
582613
'message' => null,
583-
'key' => $compressor->get_key(),
584614
);
585615
} catch (Tiny_Exception $err) {
586616
list( $message ) = explode( ' (HTTP', $err->getMessage(), 2 );
@@ -595,7 +625,8 @@ public function create_api_key() {
595625
'message' => 'This feature is not available on your platform',
596626
);
597627
}
598-
$status->message = esc_html__( $status->message, 'tiny-compress-images' );
628+
629+
$status->message = __( $status->message, 'tiny-compress-images' );
599630
echo json_encode( $status );
600631
exit();
601632
}
@@ -615,7 +646,7 @@ public function update_api_key() {
615646
update_option( self::get_prefixed_name( 'api_key_pending' ), false );
616647
update_option( self::get_prefixed_name( 'api_key' ), $key );
617648
}
618-
$status->message = esc_html__( $status->message, 'tiny-compress-images' );
649+
$status->message = __( $status->message, 'tiny-compress-images' );
619650
echo json_encode( $status );
620651
exit();
621652
}

src/css/admin.css

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ div.misc-pub-section.tiny-compress-images {
66
/* Settings */
77
div.tiny-account-status {
88
box-sizing: border-box;
9-
display: inline-block;
9+
display: table-cell;
1010
width: 500px;
1111
padding: 22px 28px;
1212
border: 1px solid #e1e1e1;
@@ -94,6 +94,7 @@ div.tiny-account-status p.status {
9494

9595
div.tiny-account-status.wide {
9696
width: auto;
97+
max-width: 700px;
9798
}
9899

99100
div.tiny-account-status div.failure input {
@@ -109,16 +110,36 @@ div.tiny-account-status div.update {
109110
}
110111

111112
div.tiny-account-status.wide div.create {
112-
float: left;
113-
width: 280px;
113+
box-sizing: border-box;
114+
display: table-cell;
115+
width: 50%;
114116
padding-right: 30px;
115117
border-right: 1px solid #e5e5e5;
116118
}
117119

118120
div.tiny-account-status.wide div.update {
119-
float: left;
120-
width: 280px;
121-
padding-left: 30px;
121+
box-sizing: border-box;
122+
display: table-cell;
123+
width: 50%;
124+
padding-left: 29px;
125+
}
126+
127+
@media screen and (max-width: 600px) {
128+
div.tiny-account-status.wide div.create {
129+
display: block;
130+
width: auto;
131+
padding-right: 0;
132+
padding-bottom: 20px;
133+
border-right: 0;
134+
border-bottom: 1px solid #e5e5e5;
135+
}
136+
137+
div.tiny-account-status.wide div.update {
138+
display: block;
139+
width: auto;
140+
padding-top: 29px;
141+
padding-left: 0;
142+
}
122143
}
123144

124145
div.tiny-account-status.wide div.create input, div.tiny-account-status.wide div.update input {

src/js/admin.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@
158158
})
159159
}
160160

161-
if (adminpage === 'upload-php') {
162-
161+
switch (adminpage) {
162+
case 'upload-php':
163163
eventOn('click', 'button.tiny-compress', compressImage)
164164

165165
if (typeof jQuery.fn.prop === 'function') {
@@ -170,13 +170,11 @@
170170

171171
jQuery('<option>').val('tiny_bulk_action').text(tinyCompress.L10nBulkAction).appendTo('select[name=action]')
172172
jQuery('<option>').val('tiny_bulk_action').text(tinyCompress.L10nBulkAction).appendTo('select[name=action2]')
173-
174-
} else if (adminpage === 'post-php') {
175-
173+
break
174+
case 'post-php':
176175
eventOn('click', 'button.tiny-compress', compressImage)
177-
178-
} else if (adminpage === 'options-media-php') {
179-
176+
break
177+
case 'options-media-php':
180178
changeEnterKeyTarget('div.tiny-account-status create', '[data-tiny-action=create-key]')
181179
changeEnterKeyTarget('div.tiny-account-status update', '[data-tiny-action=update-key]')
182180

@@ -191,7 +189,8 @@
191189
}
192190

193191
jQuery('input[name*=tinypng_sizes], input#tinypng_resize_original_enabled').on('click', function() {
194-
// Unfortunately, we need some additional information to display the correct notice.
192+
/* Unfortunately, we need some additional information to display
193+
the correct notice. */
195194
totalSelectedSizes = jQuery('input[name*=tinypng_sizes]:checked').length
196195
var image_count_url = ajaxurl + '?action=tiny_image_sizes_notice&image_sizes_selected=' + totalSelectedSizes
197196
if (jQuery('input#tinypng_resize_original_enabled').prop('checked') && jQuery('input#tinypng_sizes_0').prop('checked')) {
@@ -209,5 +208,4 @@
209208
jQuery(function() {
210209
jQuery('.tiny-notice.is-dismissible button').unbind('click').click(dismissNotice)
211210
})
212-
213211
}).call()

src/views/account-status-connected.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
), $link );
7171
?></p>
7272

73-
<input class="tiny-update-account-input" type="text" id="tinypng_api_key"
73+
<input type="text" id="tinypng_api_key"
7474
name="tinypng_api_key" size="35" spellcheck="false"
7575
value="<?php echo esc_attr( $key ); ?>">
7676

@@ -80,7 +80,7 @@
8080

8181
<p class="message"></p>
8282

83-
<p><a href="#" onclick="jQuery('div.tiny-account-status div.update').toggle(); jQuery('div.tiny-account-status div.status').toggle(); return false"><?php
83+
<p><a href="#" onclick="jQuery('div.tiny-account-status div.update').toggle(); jQuery('div.tiny-account-status div.status').toggle(); return false"><?php
8484
echo esc_html__( 'Cancel' );
8585
?></a></p>
8686
</div>

src/views/account-status-missing.php renamed to src/views/account-status-create-advanced.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
<div class="tiny-account-status wide" id="tiny-account-status" data-state="missing">
1+
<?php
2+
3+
$user = wp_get_current_user();
4+
$name = trim( $user->user_firstname . ' ' . $user->user_lastname );
5+
$email = trim( $user->user_email );
6+
7+
?><div class="tiny-account-status wide" id="tiny-account-status" data-state="missing">
28
<div class="create">
39
<h4><?php
410
echo esc_html_e( 'Register new account', 'tiny-compress-images' );
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<div class="tiny-account-status" id="tiny-account-status" data-state="missing">
2+
<div class="update">
3+
<h4><?php echo esc_html__( 'Configure your account', 'tiny-compress-images' ); ?></h4>
4+
<p class="introduction"><?php
5+
$link = sprintf( '<a href="https://tinypng.com/developers" target="_blank">%s</a>',
6+
esc_html__( 'TinyPNG developer section', 'tiny-compress-images' )
7+
);
8+
9+
echo esc_html__( 'Enter your API key.', 'tiny-compress-images' );
10+
echo ' ';
11+
12+
printf( esc_html__(
13+
'If you have lost your key, go to the %s to retrieve it.',
14+
'tiny-compress-images'
15+
), $link );
16+
?></p>
17+
18+
<input type="text" id="tinypng_api_key"
19+
name="tinypng_api_key" size="35" spellcheck="false"
20+
value="<?php echo esc_attr( $key ); ?>">
21+
22+
<button class="button button-primary" data-tiny-action="update-key"><?php
23+
echo esc_html__( 'Save', 'tiny-compress-images' );
24+
?></button>
25+
26+
<p class="message"></p>
27+
</div>
28+
</div>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<div class="tiny-account-status" id="tiny-account-status" data-state="missing">
2+
<div class="update">
3+
<h4><?php echo esc_html__( 'Change your API key', 'tiny-compress-images' ); ?></h4>
4+
<p class="introduction"><?php
5+
$link = sprintf( '<a href="https://tinypng.com/developers" target="_blank">%s</a>',
6+
esc_html__( 'TinyPNG developer section', 'tiny-compress-images' )
7+
);
8+
9+
echo esc_html__( 'Enter your API key.', 'tiny-compress-images' );
10+
echo ' ';
11+
12+
printf( esc_html__(
13+
'If you have lost your key, go to the %s to retrieve it.',
14+
'tiny-compress-images'
15+
), $link );
16+
?></p>
17+
18+
<input type="text" id="tinypng_api_key"
19+
name="tinypng_api_key" size="35" spellcheck="false"
20+
value="<?php echo esc_attr( $key ); ?>">
21+
22+
<button class="button button-primary" data-tiny-action="update-key"><?php
23+
echo esc_html__( 'Save', 'tiny-compress-images' );
24+
?></button>
25+
26+
<p class="message"></p>
27+
28+
<p><a href="#" onclick="jQuery('div.tiny-account-status div.update').toggle(); jQuery('div.tiny-account-status div.status').toggle(); return false"><?php
29+
echo esc_html__( 'Cancel', 'tiny-compress-images' );
30+
?></a></p>
31+
</div>
32+
</div>

src/views/compress-details-processing.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?php
2-
?><div class="details-container">
1+
<div class="details-container">
32
<div class="details">
43
<span class="icon spinner"></span>
54
<span class="message">

test/integration/IntegrationTestCase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ protected function visit($path) {
2727
self::$driver->get( wordpress( $path ) );
2828
}
2929

30+
protected function refresh() {
31+
self::$driver->navigate()->refresh();
32+
}
33+
3034
protected function find($selector, $base = null) {
3135
if ( ! $base ) {
3236
$base = self::$driver;

test/integration/PluginIntegrationTest.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class PluginIntegrationTest extends IntegrationTestCase {
88
public function set_up() {
99
parent::set_up();
10-
self::$driver->get( wordpress( '/wp-admin/plugins.php' ) );
10+
$this->visit( '/wp-admin/plugins.php' );
1111
}
1212

1313
public function tear_down() {
@@ -16,11 +16,9 @@ public function tear_down() {
1616
}
1717

1818
public function test_plugin_list_should_include_title() {
19-
$element = self::$driver->findElement(
20-
WebDriverBy::cssSelector(
21-
'tr[data-slug=tiny-compress-images] td.plugin-title strong, ' . /* WP4.5+ */
22-
'tr#compress-jpeg-png-images td.plugin-title strong'
23-
)
19+
$element = $this->find(
20+
'tr[data-slug=tiny-compress-images] td.plugin-title strong, ' . /* WP4.5+ */
21+
'tr#compress-jpeg-png-images td.plugin-title strong'
2422
);
2523

2624
$this->assertEquals(
@@ -30,11 +28,9 @@ public function test_plugin_list_should_include_title() {
3028
}
3129

3230
public function test_plugin_list_should_include_settings_link() {
33-
$element = self::$driver->findElement(
34-
WebDriverBy::cssSelector(
35-
'tr[data-slug=tiny-compress-images] span.settings a, ' . /* WP4.5+ */
36-
'tr#compress-jpeg-png-images span.settings a'
37-
)
31+
$element = $this->find(
32+
'tr[data-slug=tiny-compress-images] span.settings a, ' . /* WP4.5+ */
33+
'tr#compress-jpeg-png-images span.settings a'
3834
);
3935

4036
$this->assertStringEndsWith(

0 commit comments

Comments
 (0)