-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathclass-tiny-settings.php
More file actions
614 lines (526 loc) · 16.7 KB
/
class-tiny-settings.php
File metadata and controls
614 lines (526 loc) · 16.7 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
<?php
/*
* Tiny Compress Images - WordPress plugin.
* Copyright (C) 2015-2016 Voormedia B.V.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
class Tiny_Settings extends Tiny_WP_Base {
const DUMMY_SIZE = '_tiny_dummy';
private $sizes;
private $tinify_sizes;
private $compressor;
private $notices;
private $_running = true;
public function __construct() {
parent::__construct();
$this->notices = new Tiny_Notices();
}
private function init_compressor() {
$this->compressor = Tiny_Compress::create(
$this->get_api_key(),
$this->get_method( 'after_compress_callback' )
);
}
public function pause () {
$this->_running = FALSE;
}
public function start () {
$this->_running = TRUE;
}
public function running () {
return $this->_running;
}
public function get_absolute_url() {
return get_admin_url( null, 'options-media.php#' . self::NAME );
}
public function xmlrpc_init() {
try {
$this->init_compressor();
} catch (Tiny_Exception $e) {
}
}
public function admin_init() {
if ( current_user_can( 'manage_options' ) && ! $this->get_api_key() ) {
$link = sprintf(
'<a href="options-media.php#%s">%s</a>', self::NAME,
esc_html__(
'Please register or provide an API key to start compressing images',
'tiny-compress-images'
)
);
$this->notices->show( 'setting', $link, 'error', false );
}
if ( current_user_can( 'manage_options' ) && ! Tiny_PHP::client_supported() ) {
$details = 'PHP ' . PHP_VERSION;
if ( extension_loaded( 'curl' ) ) {
$curlinfo = curl_version();
$details .= ' with curl ' . $curlinfo['version'];
} else {
$details .= ' without curl';
}
$message = esc_html__(
'You are using an outdated platform (' . $details .
') – some features are disabled', 'tiny-compress-images'
);
$this->notices->show( 'setting', $message, 'notice-warning', false );
}
try {
$this->init_compressor();
} catch (Tiny_Exception $e) {
$this->notices->show(
'compressor_exception',
esc_html__( $e->getMessage(), 'tiny-compress-images' ),
'error', false
);
}
$section = self::get_prefixed_name( 'settings' );
add_settings_section( $section,
__( 'PNG and JPEG optimization', 'tiny-compress-images' ),
$this->get_method( 'render_section' ),
'media'
);
$field = self::get_prefixed_name( 'api_key' );
register_setting( 'media', $field );
add_settings_field( $field,
__( 'TinyPNG account', 'tiny-compress-images' ),
$this->get_method( 'render_pending_status' ),
'media',
$section
);
$field = self::get_prefixed_name( 'api_key_automated' );
register_setting( 'media', $field );
$field = self::get_prefixed_name( 'sizes' );
register_setting( 'media', $field );
add_settings_field( $field,
__( 'File compression', 'tiny-compress-images' ),
$this->get_method( 'render_sizes' ),
'media',
$section
);
$field = self::get_prefixed_name( 'resize_original' );
register_setting( 'media', $field );
add_settings_field( $field,
__( 'Original image', 'tiny-compress-images' ),
$this->get_method( 'render_resize' ),
'media',
$section
);
$field = self::get_prefixed_name( 'preserve_data' );
register_setting( 'media', $field );
add_settings_section( 'section_end', '',
$this->get_method( 'render_section_end' ),
'media'
);
add_action(
'wp_ajax_tiny_image_sizes_notice',
$this->get_method( 'image_sizes_notice' )
);
add_action(
'wp_ajax_tiny_compress_status',
$this->get_method( 'connection_status' )
);
add_action(
'wp_ajax_tiny_settings_create_api_key',
$this->get_method( 'create_api_key' )
);
add_action(
'wp_ajax_tiny_settings_update_api_key',
$this->get_method( 'update_api_key' )
);
}
public function image_sizes_notice() {
$this->render_image_sizes_notice(
$_GET['image_sizes_selected'],
isset( $_GET['resize_original'] )
);
exit();
}
public function connection_status() {
$this->render_status();
exit();
}
public function get_compressor() {
return $this->compressor;
}
public function set_compressor($compressor) {
$this->compressor = $compressor;
}
public function get_status() {
return intval( get_option( self::get_prefixed_name( 'status' ) ) );
}
protected function get_api_key() {
if ( defined( 'TINY_API_KEY' ) ) {
return TINY_API_KEY;
} else {
return get_option( self::get_prefixed_name( 'api_key' ) );
}
}
protected static function get_intermediate_size($size) {
/* Inspired by
http://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes */
global $_wp_additional_image_sizes;
$width = get_option( $size . '_size_w' );
$height = get_option( $size . '_size_h' );
if ( $width && $height ) {
return array( $width, $height );
}
if ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
$sizes = $_wp_additional_image_sizes[ $size ];
return array(
isset( $sizes['width'] ) ? $sizes['width'] : null,
isset( $sizes['height'] ) ? $sizes['height'] : null,
);
}
return array( null, null );
}
public function get_sizes() {
if ( is_array( $this->sizes ) ) {
return $this->sizes;
}
$setting = get_option( self::get_prefixed_name( 'sizes' ) );
$size = Tiny_Image::ORIGINAL;
$this->sizes = array(
$size => array(
'width' => null,
'height' => null,
'tinify' => ! is_array( $setting ) ||
(isset( $setting[$size] ) && $setting[$size] === 'on'),
)
);
foreach ( get_intermediate_image_sizes() as $size ) {
if ( $size === self::DUMMY_SIZE ) {
continue;
}
list($width, $height) = self::get_intermediate_size( $size );
if ( $width || $height ) {
$this->sizes[ $size ] = array(
'width' => $width, 'height' => $height,
'tinify' => ! is_array( $setting ) ||
(isset( $setting[ $size ] ) && $setting[ $size ] === 'on'),
);
}
}
return $this->sizes;
}
public function get_active_tinify_sizes() {
if ( is_array( $this->tinify_sizes ) ) {
return $this->tinify_sizes;
}
$this->tinify_sizes = array();
foreach ( $this->get_sizes() as $size => $values ) {
if ( $values['tinify'] ) {
$this->tinify_sizes[] = $size;
}
}
return $this->tinify_sizes;
}
public function get_resize_enabled() {
$setting = get_option( self::get_prefixed_name( 'resize_original' ) );
return isset( $setting['enabled'] ) && $setting['enabled'] === 'on';
}
public function get_preserve_enabled($name) {
$setting = get_option( self::get_prefixed_name( 'preserve_data' ) );
return isset( $setting[ $name ] ) && $setting[ $name ] === 'on';
}
public function get_preserve_options( $size_name ) {
if ( ! Tiny_Image::is_original( $size_name ) ) {
return false;
}
$options = array();
$settings = get_option( self::get_prefixed_name( 'preserve_data' ) );
if ( $settings ) {
$keys = array_keys( $settings );
foreach ( $keys as &$key ) {
if ( 'on' === $settings[ $key ] ) {
array_push( $options, $key );
}
}
}
return $options;
}
public function get_resize_options( $size_name ) {
if ( ! Tiny_Image::is_original( $size_name ) ) {
return false;
}
if ( ! $this->get_resize_enabled() ) {
return false;
}
$setting = get_option( self::get_prefixed_name( 'resize_original' ) );
$width = intval( $setting['width'] );
$height = intval( $setting['height'] );
$method = $width > 0 && $height > 0 ? 'fit' : 'scale';
$options['method'] = $method;
if ( $width > 0 ) {
$options['width'] = $width;
}
if ( $height > 0 ) {
$options['height'] = $height;
}
return sizeof( $options ) >= 2 ? $options : false;
}
public function render_section_end() {
echo '</div>';
}
public function render_section() {
echo '<div class="' . self::NAME . '">';
echo '<span id="' . self::NAME . '"></span>';
}
public function render_sizes() {
echo '<p>';
esc_html_e(
'Choose sizes to compress. Remember each selected size counts as a compression.',
'tiny-compress-images'
);
echo '</p>';
echo '<input type="hidden" name="' .
self::get_prefixed_name( 'sizes[' . self::DUMMY_SIZE . ']' ) . '" value="on"/>';
foreach ( $this->get_sizes() as $size => $option ) {
$this->render_size_checkbox( $size, $option );
}
echo '<br>';
echo '<div id="tiny-image-sizes-notice">';
$this->render_image_sizes_notice(
count( self::get_active_tinify_sizes() ),
self::get_resize_enabled()
);
echo '</div>';
}
private function render_size_checkbox($size, $option) {
$id = self::get_prefixed_name( "sizes_$size" );
$name = self::get_prefixed_name( 'sizes[' . $size . ']' );
$checked = ( $option['tinify'] ? ' checked="checked"' : '' );
if ( Tiny_Image::is_original( $size ) ) {
$label = esc_html__( 'original', 'tiny-compress-images' ) . ' (' .
esc_html__( 'overwritten by compressed image', 'tiny-compress-images' ) . ')';
} else {
$label = $size . ' - ' . $option['width'] . 'x' . $option['height'];
}
echo '<p>';
echo '<input type="checkbox" id="' . $id . '" name="' . $name .
'" value="on" ' . $checked . '/>';
echo '<label for="' . $id . '">' . $label . '</label>';
echo '</p>';
}
public function render_image_sizes_notice($active_image_sizes_count, $resize_original_enabled) {
echo '<p>';
if ( $resize_original_enabled ) {
$active_image_sizes_count++;
}
if ( $active_image_sizes_count < 1 ) {
esc_html_e(
'With these settings no images will be compressed.',
'tiny-compress-images'
);
} else {
$free_images_per_month = floor(
Tiny_Config::MONTHLY_FREE_COMPRESSIONS / $active_image_sizes_count
);
printf( __(
'With these settings you can compress ' .
'<strong> at least %s images </strong> for free each month.',
'tiny-compress-images'
), $free_images_per_month );
}
echo '</p>';
}
public function render_resize() {
echo '<p class="tiny-resize-unavailable" style="display: none">';
esc_html_e(
'Enable compression of the original image size for more options.',
'tiny-compress-images'
);
echo '</p>';
$id = self::get_prefixed_name( 'resize_original_enabled' );
$name = self::get_prefixed_name( 'resize_original[enabled]' );
$checked = ( $this->get_resize_enabled() ? ' checked="checked"' : '' );
$label = esc_html__(
'Resize and compress the original image',
'tiny-compress-images'
);
echo '<p class="tiny-resize-available">';
echo '<input type="checkbox" id="' . $id . '" name="' . $name .
'" value="on" '. $checked . '/>';
echo '<label for="' . $id . '">' . $label . '</label>';
echo '<br>';
echo '</p>';
echo '<p class="tiny-resize-available tiny-resize-resolution">';
printf( '%s: ', esc_html__( 'Max Width', 'tiny-compress-images' ) );
$this->render_resize_input( 'width' );
printf( '%s: ', esc_html__( 'Max Height', 'tiny-compress-images' ) );
$this->render_resize_input( 'height' );
echo '</p>';
echo '<p class="tiny-resize-available tiny-resize-resolution">';
esc_html_e(
'Resizing takes 1 additional compression for each image that is larger.',
'tiny-compress-images'
);
echo '</p>';
echo '<br>';
$this->render_preserve_input(
'creation',
'Preserve creation date and time in the original image (JPEG only)'
);
$this->render_preserve_input(
'copyright',
'Preserve copyright information in the original image'
);
$this->render_preserve_input(
'location',
'Preserve GPS location in the original image (JPEG only)'
);
}
public function render_preserve_input($name, $description) {
echo '<p class="tiny-preserve">';
$id = sprintf( self::get_prefixed_name( 'preserve_data_%s' ), $name );
$field = sprintf( self::get_prefixed_name( 'preserve_data[%s]' ), $name );
$checked = ( $this->get_preserve_enabled( $name ) ? ' checked="checked"' : '' );
$label = esc_html__( $description, 'tiny-compress-images' );
echo '<input type="checkbox" id="' . $id . '" name="' . $field .
'" value="on" ' . $checked . '/>';
echo '<label for="' . $id . '">' . $label . '</label>';
echo '<br>';
echo '</p>';
}
public function render_resize_input($name) {
$id = sprintf( self::get_prefixed_name( 'resize_original_%s' ), $name );
$field = sprintf( self::get_prefixed_name( 'resize_original[%s]' ), $name );
$settings = get_option( self::get_prefixed_name( 'resize_original' ) );
$value = isset( $settings[ $name ] ) ? $settings[ $name ] : '2048';
echo '<input type="number" id="'. $id .'" name="' . $field .
'" value="' . $value . '" size="5" />';
}
public function get_compression_count() {
$field = self::get_prefixed_name( 'status' );
return get_option( $field );
}
public function after_compress_callback($compressor) {
if ( ! is_null( $count = $compressor->get_compression_count() ) ) {
$field = self::get_prefixed_name( 'status' );
update_option( $field, $count );
}
if ( $compressor->limit_reached() ) {
$link = '<a href="https://tinypng.com/developers" target="_blank">' .
esc_html__( 'TinyPNG API account', 'tiny-compress-images' ) . '</a>';
$this->notices->add('limit-reached',
sprintf(
esc_html__(
'You have reached your limit of %s compressions this month.',
'tiny-compress-images'
),
$count
) .
sprintf(
esc_html__(
'Upgrade your %s if you like to compress more images.',
'tiny-compress-images'
),
$link
)
);
} else {
$this->notices->remove( 'limit-reached' );
}
}
public function render_status() {
$key = $this->get_api_key();
if ( empty( $key ) ) {
include( dirname( __FILE__ ) . '/views/account-status-missing.php' );
} else {
$status = $this->compressor->get_status();
if ( ! $status->ok && $status->code == 401 ) {
$field = self::get_prefixed_name( 'api_key_automated' );
if ( get_option( $field ) ) {
$status->ok = true;
$status->message = (
'An email has been sent with a link to activate your account'
);
}
}
include( dirname( __FILE__ ) . '/views/account-status-connected.php' );
}
}
public function render_pending_status() {
include( dirname( __FILE__ ) . '/views/account-update-modal.php' );
$key = $this->get_api_key();
if ( empty( $key ) ) {
echo '<div id="tiny-compress-status" data-state="missing">';
include( dirname( __FILE__ ) . '/views/account-status-missing.php' );
echo '</div>';
} else {
echo '<div id="tiny-compress-status" data-state="pending">';
include( dirname( __FILE__ ) . '/views/account-status-pending.php' );
echo '</div>';
}
}
public function render_pending_savings() {
echo '<div id="tiny-compress-savings"><div class="spinner"></div></div>';
}
public function create_api_key() {
$compressor = $this->get_compressor();
if ( $compressor->can_create_key() ) {
try {
$site = str_replace( array( 'http://', 'https://' ), '', get_bloginfo( 'url' ) );
$identifier = 'WordPress plugin for ' . $site;
$link = $this->get_absolute_url();
$compressor->create_key($_POST['email'], array(
'name' => $_POST['name'],
'identifier' => $identifier,
'link' => $link,
));
update_option( self::get_prefixed_name( 'api_key_automated' ), true );
update_option( self::get_prefixed_name( 'api_key' ), $compressor->get_key() );
update_option( self::get_prefixed_name( 'status' ), 0 );
$status = (object) array(
'ok' => true,
'message' => null,
'key' => $compressor->get_key(),
);
} catch (Tiny_Exception $err) {
$status = (object) array(
'ok' => false,
'message' => $err->getMessage(),
);
}
} else {
$status = (object) array(
'ok' => false,
'message' => 'This feature is not available on your platform',
);
}
$status->message = __( $status->message, 'tiny-compress-images' );
echo json_encode( $status );
exit();
}
public function update_api_key() {
$key = $_POST['key'];
if ( empty( $key ) ) {
/* Always save if key is blank, so the key can be deleted. */
$status = (object) array(
'ok' => true,
'message' => null,
);
} else {
$status = Tiny_Compress::create( $key )->get_status();
}
if ( $status->ok ) {
update_option( self::get_prefixed_name( 'api_key_automated' ), false );
update_option( self::get_prefixed_name( 'api_key' ), $key );
}
$status->message = __( $status->message, 'tiny-compress-images' );
echo json_encode( $status );
exit();
}
}