Optimus – WordPress Image Optimizer - Version 1.6.0

Version Description

  • Skip missing files
  • Added wpcli command for bulk optimization
  • Added option to append instead of replace file extension for webp files
Download this release

Release Info

Developer keycdn
Plugin Icon 128x128 Optimus – WordPress Image Optimizer
Version 1.6.0
Comparing to
See all releases

Code changes from version 1.5.0 to 1.6.0

inc/optimus.class.php CHANGED
@@ -342,6 +342,7 @@ class Optimus
342
  array(
343
  'copy_markers' => 0,
344
  'webp_convert' => 0,
 
345
  'keep_original' => 0,
346
  'secure_transport' => 0,
347
  'manual_optimize' => 0
342
  array(
343
  'copy_markers' => 0,
344
  'webp_convert' => 0,
345
+ 'webp_keeporigext' => 0,
346
  'keep_original' => 0,
347
  'secure_transport' => 0,
348
  'manual_optimize' => 0
inc/optimus_cli.class.php ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+
4
+ /* Quit */
5
+ defined('ABSPATH') OR exit;
6
+
7
+
8
+ /**
9
+ * Optimus WP-CLI
10
+ *
11
+ */
12
+
13
+ class Optimus_CLI extends WP_CLI_Command
14
+ {
15
+ /**
16
+ * Optimize command
17
+ *
18
+ * @since 1.6.0
19
+ */
20
+
21
+ public static function optimize( $args, $assoc_args ) {
22
+ $assoc_args = wp_parse_args( $assoc_args, array( 'format' => 'table', 'num' => 10 ) );
23
+
24
+ $assets = Optimus_Management::bulk_optimizer_assets();
25
+ $assets = array_slice($assets, 0, $assoc_args['num']);
26
+
27
+ foreach ($assets as $key=>$img) {
28
+ $assets[$key]['result'] = self::_optimize_image($img);
29
+
30
+ if ( $assets[$key]['result'] === false ) {
31
+ $assets[$key]['profit'] = 0;
32
+ $assets[$key]['quantity'] = 0;
33
+ $assets[$key]['webp'] = '';
34
+ } else {
35
+ $assets[$key]['profit'] = $assets[$key]['result']['optimus']['profit'];
36
+ $assets[$key]['quantity'] = $assets[$key]['result']['optimus']['quantity'];
37
+ $assets[$key]['webp'] = $assets[$key]['result']['optimus']['webp'];
38
+ }
39
+ }
40
+
41
+ $formatter = new \WP_CLI\Formatter( $assoc_args,
42
+ array(
43
+ 'ID',
44
+ 'post_title',
45
+ 'post_mime_type',
46
+ 'profit',
47
+ 'quantity',
48
+ 'webp'
49
+ ),
50
+ 'optimize' );
51
+ $formatter->display_items( $assets );
52
+ }
53
+
54
+ private static function _optimize_image($img) {
55
+ /* get metadata */
56
+ $metadata = wp_get_attachment_metadata($img['ID']);
57
+ if (!is_array($metadata)) {
58
+ // Metadata missing
59
+ return false;
60
+ }
61
+
62
+ /* optimize image */
63
+ $optimus_metadata = Optimus_Request::optimize_upload_images($metadata, $img['ID']);
64
+
65
+ if ( !empty($optimus_metadata['optimus']['error']) ) {
66
+ return false;
67
+ }
68
+
69
+ /* check if optimus array empty */
70
+ if ( empty($optimus_metadata['optimus']) ) {
71
+ return false;
72
+ }
73
+
74
+ /* update metadata */
75
+ update_post_meta($img['ID'], '_wp_attachment_metadata', $optimus_metadata);
76
+
77
+ return $optimus_metadata;
78
+ }
79
+
80
+ public static function add_commands() {
81
+ $cmd_optimize = function( $args, $assoc_args ) { self::optimize( $args, $assoc_args ); };
82
+ WP_CLI::add_command( 'optimus optimize', $cmd_optimize, array(
83
+ 'shortdesc' => 'Bulk optimize some images.',
84
+ 'synopsis' => array(
85
+ array(
86
+ 'type' => 'assoc',
87
+ 'name' => 'num',
88
+ 'description' => 'Batch size in number of images to process',
89
+ 'optional' => true,
90
+ 'default' => 10,
91
+ ),
92
+ array(
93
+ 'type' => 'assoc',
94
+ 'name' => 'format',
95
+ 'description' => 'Output results in format',
96
+ 'optional' => true,
97
+ 'default' => 'table',
98
+ 'options' => array( 'table', 'csv', 'json' ),
99
+ ),
100
+ ),
101
+ ));
102
+ }
103
+ }
104
+
inc/optimus_hq.class.php CHANGED
@@ -100,7 +100,8 @@ class Optimus_HQ
100
  if ( $expiration_time < time() ) {
101
 
102
  /* try to renew the licence once every 10 minutes */
103
- if ( empty(get_transient('optimus_renew_licence')) ) {
 
104
  set_transient('optimus_renew_licence', true, 600);
105
 
106
  $purchase_time_new = self::get_purchase_time(true);
100
  if ( $expiration_time < time() ) {
101
 
102
  /* try to renew the licence once every 10 minutes */
103
+ $transient = get_transient('optimus_renew_licence');
104
+ if ( empty($transient) ) {
105
  set_transient('optimus_renew_licence', true, 600);
106
 
107
  $purchase_time_new = self::get_purchase_time(true);
inc/optimus_management.class.php CHANGED
@@ -63,14 +63,13 @@ class Optimus_Management
63
 
64
 
65
  /**
66
- * Bulk optimizer page
67
  *
68
- * @since 1.3.8
69
- * @change 1.4.8
70
  *
71
  */
72
 
73
- public static function bulk_optimizer_page() {
74
  global $wpdb;
75
 
76
  /* Get plugin options */
@@ -110,7 +109,20 @@ class Optimus_Management
110
  $id_query
111
  ORDER BY $wpdb->posts.ID DESC";
112
 
113
- $assets = $wpdb->get_results($query, ARRAY_A);
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  $count = count($assets);
115
 
116
  echo '<div class="wrap" id="optimus-bulk-optimizer">';
63
 
64
 
65
  /**
66
+ * Bulk optimizer collect assets
67
  *
68
+ * @since 1.5.0
 
69
  *
70
  */
71
 
72
+ public static function bulk_optimizer_assets() {
73
  global $wpdb;
74
 
75
  /* Get plugin options */
109
  $id_query
110
  ORDER BY $wpdb->posts.ID DESC";
111
 
112
+ return $wpdb->get_results($query, ARRAY_A);
113
+ }
114
+
115
+
116
+ /**
117
+ * Bulk optimizer page
118
+ *
119
+ * @since 1.3.8
120
+ * @change 1.5.0
121
+ *
122
+ */
123
+
124
+ public static function bulk_optimizer_page() {
125
+ $assets = self::bulk_optimizer_assets();
126
  $count = count($assets);
127
 
128
  echo '<div class="wrap" id="optimus-bulk-optimizer">';
inc/optimus_request.class.php CHANGED
@@ -248,6 +248,11 @@ class Optimus_Request
248
  $upload_url_file = path_join($upload_url, $file);
249
  $upload_path_file = path_join($upload_path, $file);
250
 
 
 
 
 
 
251
  /* Get file size */
252
  $upload_filesize = (int)filesize($upload_path_file);
253
 
@@ -416,12 +421,18 @@ class Optimus_Request
416
  return __("Mime type not supported", "optimus");
417
  }
418
 
419
- /* Extension replace for WebP */
 
 
420
  if ( isset($args['webp']) ) {
421
- $file = self::_replace_file_extension(
422
- $file,
423
- 'webp'
424
- );
 
 
 
 
425
  }
426
 
427
  /* Rewrite image file */
@@ -638,11 +649,15 @@ class Optimus_Request
638
  @unlink($converted_file_retina);
639
  }
640
 
641
- /* Replace to webp extension */
642
- $converted_file = self::_replace_file_extension(
643
- $converted_file,
644
- 'webp'
645
- );
 
 
 
 
646
 
647
  /* Remove if exists */
648
  if ( file_exists($converted_file) ) {
248
  $upload_url_file = path_join($upload_url, $file);
249
  $upload_path_file = path_join($upload_path, $file);
250
 
251
+ /* skip loop iteration if file doesn't exist */
252
+ if ( ! file_exists($upload_path_file) ) {
253
+ continue;
254
+ }
255
+
256
  /* Get file size */
257
  $upload_filesize = (int)filesize($upload_path_file);
258
 
421
  return __("Mime type not supported", "optimus");
422
  }
423
 
424
+ $options = Optimus::get_options();
425
+
426
+ /* Replace to or append webp extension */
427
  if ( isset($args['webp']) ) {
428
+ if ( $options['webp_keeporigext'] == 1 ) {
429
+ $file = $file . ".webp";
430
+ } else {
431
+ $file = self::_replace_file_extension(
432
+ $file,
433
+ 'webp'
434
+ );
435
+ }
436
  }
437
 
438
  /* Rewrite image file */
649
  @unlink($converted_file_retina);
650
  }
651
 
652
+ /* Replace to or append webp extension */
653
+ if ( $options['webp_keeporigext'] == 1 ) {
654
+ $converted_file = $converted_file . ".webp";
655
+ } else {
656
+ $converted_file = self::_replace_file_extension(
657
+ $converted_file,
658
+ 'webp'
659
+ );
660
+ }
661
 
662
  /* Remove if exists */
663
  if ( file_exists($converted_file) ) {
inc/optimus_settings.class.php CHANGED
@@ -50,6 +50,7 @@ class Optimus_Settings
50
  return array(
51
  'copy_markers' => (int)(!empty($data['copy_markers'])),
52
  'webp_convert' => (int)(!empty($data['webp_convert'])),
 
53
  'keep_original' => (int)(!empty($data['keep_original'])),
54
  'secure_transport' => (int)(!empty($data['secure_transport'])),
55
  'manual_optimize' => (int)(!empty($data['manual_optimize']))
@@ -149,7 +150,7 @@ class Optimus_Settings
149
  <td>
150
  <fieldset>
151
  <label for="optimus_copy_markers">
152
- <input type="checkbox" name="optimus[copy_markers]" id="optimus_copy_markers" value="1" <?php checked(1, $options['copy_markers']); if ( Optimus_HQ::is_locked() ) { _e("onclick=\"return false;\" disabled=\"disabled\""); } ?> />
153
  <?php _e("No deletion of image metadata", "optimus"); ?>
154
  </label>
155
 
@@ -167,7 +168,7 @@ class Optimus_Settings
167
  <td>
168
  <fieldset>
169
  <label for="optimus_webp_convert">
170
- <input type="checkbox" name="optimus[webp_convert]" id="optimus_webp_convert" value="1" <?php checked(1, $options['webp_convert']); if ( Optimus_HQ::is_locked() ) { _e("onclick=\"return false;\" disabled=\"disabled\""); } ?> />
171
  <?php _e("Creation of WebP files", "optimus"); ?>
172
  </label>
173
 
@@ -175,6 +176,19 @@ class Optimus_Settings
175
  <?php _e("Only <a href=\"https://optimus.io\" target=\"_blank\">Optimus HQ</a>. It is recommended to use the <a href=\"https://wordpress.org/plugins/cache-enabler/\">Cache Enabler plugin</a> to integrate the WebP images. [<a href=\"https://optimus.keycdn.com/support/optimus-settings/#convert-to-webp\" target=\"_blank\">Details</a>]", "optimus"); ?>
176
  </p>
177
  </fieldset>
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  </td>
179
  </tr>
180
 
@@ -185,7 +199,7 @@ class Optimus_Settings
185
  <td>
186
  <fieldset>
187
  <label for="optimus_secure_transport">
188
- <input type="checkbox" name="optimus[secure_transport]" id="optimus_secure_transport" value="1" <?php checked(1, $options['secure_transport']); if ( Optimus_HQ::is_locked() ) { _e("onclick=\"return false;\" disabled=\"disabled\""); } ?> />
189
  <?php _e("Transfer images using TLS encryption", "optimus"); ?>
190
  </label>
191
 
50
  return array(
51
  'copy_markers' => (int)(!empty($data['copy_markers'])),
52
  'webp_convert' => (int)(!empty($data['webp_convert'])),
53
+ 'webp_keeporigext' => (int)(!empty($data['webp_keeporigext'])),
54
  'keep_original' => (int)(!empty($data['keep_original'])),
55
  'secure_transport' => (int)(!empty($data['secure_transport'])),
56
  'manual_optimize' => (int)(!empty($data['manual_optimize']))
150
  <td>
151
  <fieldset>
152
  <label for="optimus_copy_markers">
153
+ <input type="checkbox" name="optimus[copy_markers]" id="optimus_copy_markers" value="1" <?php checked(1, $options['copy_markers']); echo Optimus_HQ::is_locked() ? "onclick=\"return false;\" disabled=\"disabled\"" : ""; ?> />
154
  <?php _e("No deletion of image metadata", "optimus"); ?>
155
  </label>
156
 
168
  <td>
169
  <fieldset>
170
  <label for="optimus_webp_convert">
171
+ <input type="checkbox" name="optimus[webp_convert]" id="optimus_webp_convert" value="1" <?php checked(1, $options['webp_convert']); echo Optimus_HQ::is_locked() ? "onclick=\"return false;\" disabled=\"disabled\"" : ""; ?> />
172
  <?php _e("Creation of WebP files", "optimus"); ?>
173
  </label>
174
 
176
  <?php _e("Only <a href=\"https://optimus.io\" target=\"_blank\">Optimus HQ</a>. It is recommended to use the <a href=\"https://wordpress.org/plugins/cache-enabler/\">Cache Enabler plugin</a> to integrate the WebP images. [<a href=\"https://optimus.keycdn.com/support/optimus-settings/#convert-to-webp\" target=\"_blank\">Details</a>]", "optimus"); ?>
177
  </p>
178
  </fieldset>
179
+
180
+ <br>
181
+
182
+ <fieldset>
183
+ <label for="optimus_webp_keeporigext">
184
+ <input type="checkbox" name="optimus[webp_keeporigext]" id="optimus_webp_keeporigext" value="1" <?php checked(1, $options['webp_keeporigext']); echo Optimus_HQ::is_locked() ? "onclick=\"return false;\" disabled=\"disabled\"" : ""; ?> />
185
+ <?php _e("Append .webp extension", "optimus"); ?>
186
+ </label>
187
+
188
+ <p class="description">
189
+ <?php _e("Append .webp extension instead of replacing the original one (e.g. <i>test.jpg.webp</i>)", "optimus"); ?>
190
+ </p>
191
+ </fieldset>
192
  </td>
193
  </tr>
194
 
199
  <td>
200
  <fieldset>
201
  <label for="optimus_secure_transport">
202
+ <input type="checkbox" name="optimus[secure_transport]" id="optimus_secure_transport" value="1" <?php checked(1, $options['secure_transport']); echo Optimus_HQ::is_locked() ? "onclick=\"return false;\" disabled=\"disabled\"" : ""; ?> />
203
  <?php _e("Transfer images using TLS encryption", "optimus"); ?>
204
  </label>
205
 
optimus.php CHANGED
@@ -7,7 +7,7 @@ Author: KeyCDN
7
  Author URI: https://www.keycdn.com
8
  Plugin URI: https://optimus.io
9
  License: GPLv2 or later
10
- Version: 1.5.0
11
  */
12
 
13
  /*
@@ -76,6 +76,18 @@ if ( is_admin() OR (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) ) {
76
  );
77
  }
78
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  /* Uninstall */
81
  register_uninstall_hook(
7
  Author URI: https://www.keycdn.com
8
  Plugin URI: https://optimus.io
9
  License: GPLv2 or later
10
+ Version: 1.6.0
11
  */
12
 
13
  /*
76
  );
77
  }
78
 
79
+ /* WP-CLI */
80
+ if ( defined( 'WP_CLI' ) && WP_CLI ) {
81
+ require_once(OPTIMUS_DIR."/inc/optimus_cli.class.php");
82
+
83
+ add_action(
84
+ 'init',
85
+ array(
86
+ 'Optimus_CLI',
87
+ 'add_commands'
88
+ )
89
+ );
90
+ }
91
 
92
  /* Uninstall */
93
  register_uninstall_hook(
readme.txt CHANGED
@@ -88,6 +88,11 @@ Optimus supports the conversion of images to the new [*WebP* image format](https
88
 
89
  == Changelog ==
90
 
 
 
 
 
 
91
  = 1.5.0 =
92
  * Unsupported image types are now gracefully skipped
93
  * Renamed setting names and greyed out inapplicable settings to avoid confusion
88
 
89
  == Changelog ==
90
 
91
+ = 1.6.0 =
92
+ * Skip missing files
93
+ * Added wpcli command for bulk optimization
94
+ * Added option to append instead of replace file extension for webp files
95
+
96
  = 1.5.0 =
97
  * Unsupported image types are now gracefully skipped
98
  * Renamed setting names and greyed out inapplicable settings to avoid confusion