Converter for Media – Optimize images | Convert WebP & AVIF - Version 1.2.5

Version Description

(2020-05-10) = * [Removed] Link to plugin settings on Network Admin Screen for WordPress Multisite * [Fixed] Path in RewriteRule for WordPress Multisite * [Changed] Error messages in administration panel * [Added] Support for disable_functions setting for using set_time_limit function * [Added] Support for blocked function file_get_contents

Download this release

Release Info

Developer mateuszgbiorczyk
Plugin Icon 128x128 Converter for Media – Optimize images | Convert WebP & AVIF
Version 1.2.5
Comparing to
See all releases

Code changes from version 1.2.4 to 1.2.5

app/Action/Convert.php CHANGED
@@ -35,8 +35,7 @@
35
 
36
  public function convertFilesByAttachment($postId)
37
  {
38
- $attachment = new Attachment();
39
- $paths = $attachment->getAttachmentPaths($postId);
40
  do_action('webpc_convert_paths', $paths);
41
  }
42
 
35
 
36
  public function convertFilesByAttachment($postId)
37
  {
38
+ $paths = (new Attachment())->getAttachmentPaths($postId);
 
39
  do_action('webpc_convert_paths', $paths);
40
  }
41
 
app/Action/Delete.php CHANGED
@@ -24,8 +24,7 @@
24
 
25
  private function deleteFileByPath($path)
26
  {
27
- $directory = new Directory();
28
- $source = $directory->getPath($path);
29
  if (is_writable($source) && (pathinfo($source, PATHINFO_EXTENSION) === 'webp')) unlink($source);
30
  }
31
  }
24
 
25
  private function deleteFileByPath($path)
26
  {
27
+ $source = (new Directory())->getPath($path);
 
28
  if (is_writable($source) && (pathinfo($source, PATHINFO_EXTENSION) === 'webp')) unlink($source);
29
  }
30
  }
app/Admin/Plugin.php CHANGED
@@ -6,21 +6,27 @@
6
  {
7
  public function __construct()
8
  {
9
- add_filter('network_admin_plugin_action_links_' . WEBPC_NAME, [$this, 'addPluginLinks']);
10
- add_filter('plugin_action_links_' . WEBPC_NAME, [$this, 'addPluginLinks']);
 
11
  }
12
 
13
  /* ---
14
  Functions
15
  --- */
16
 
17
- public function addPluginLinks($links)
18
  {
19
  array_unshift($links, sprintf(
20
  __('%sSettings%s', 'webp-converter'),
21
  '<a href="' . menu_page_url('webpc_admin_page', false) . '">',
22
  '</a>'
23
  ));
 
 
 
 
 
24
  $links[] = sprintf(
25
  __('%sProvide us a coffee%s', 'webp-converter'),
26
  '<a href="https://ko-fi.com/gbiorczyk/" target="_blank">',
6
  {
7
  public function __construct()
8
  {
9
+ add_filter('plugin_action_links_' . WEBPC_NAME, [$this, 'addLinkToSettings']);
10
+ add_filter('plugin_action_links_' . WEBPC_NAME, [$this, 'addLinkToDonate']);
11
+ add_filter('network_admin_plugin_action_links_' . WEBPC_NAME, [$this, 'addLinkToDonate']);
12
  }
13
 
14
  /* ---
15
  Functions
16
  --- */
17
 
18
+ public function addLinkToSettings($links)
19
  {
20
  array_unshift($links, sprintf(
21
  __('%sSettings%s', 'webp-converter'),
22
  '<a href="' . menu_page_url('webpc_admin_page', false) . '">',
23
  '</a>'
24
  ));
25
+ return $links;
26
+ }
27
+
28
+ public function addLinkToDonate($links)
29
+ {
30
  $links[] = sprintf(
31
  __('%sProvide us a coffee%s', 'webp-converter'),
32
  '<a href="https://ko-fi.com/gbiorczyk/" target="_blank">',
app/Convert/Gd.php CHANGED
@@ -10,8 +10,7 @@
10
 
11
  public function convertImage($path, $quality)
12
  {
13
- ini_set('memory_limit', '1G');
14
- set_time_limit(120);
15
 
16
  try {
17
  if (!is_writable($path)) {
@@ -102,9 +101,7 @@
102
  private function convertToWebp($image, $path, $quality)
103
  {
104
  try {
105
- $directory = new Directory();
106
- $output = $directory->getPath($path, true);
107
-
108
  if (!$output) {
109
  throw new \Exception(sprintf('An error occurred creating destination directory for "%s" file.', $path));
110
  } else if (!function_exists('imagewebp')) {
10
 
11
  public function convertImage($path, $quality)
12
  {
13
+ (new Server())->setSettings();
 
14
 
15
  try {
16
  if (!is_writable($path)) {
101
  private function convertToWebp($image, $path, $quality)
102
  {
103
  try {
104
+ $output = (new Directory())->getPath($path, true);
 
 
105
  if (!$output) {
106
  throw new \Exception(sprintf('An error occurred creating destination directory for "%s" file.', $path));
107
  } else if (!function_exists('imagewebp')) {
app/Convert/Imagick.php CHANGED
@@ -10,8 +10,7 @@
10
 
11
  public function convertImage($path, $quality)
12
  {
13
- ini_set('memory_limit', '1G');
14
- set_time_limit(120);
15
 
16
  try {
17
  if (!is_writable($path)) {
@@ -65,8 +64,7 @@
65
  private function convertToWebp($image, $path, $quality)
66
  {
67
  try {
68
- $directory = new Directory();
69
- $output = $directory->getPath($path, true);
70
  if (!$output) {
71
  throw new \Exception(sprintf('An error occurred creating destination directory for "%s" file.', $path));
72
  } else if (!in_array('WEBP', $image->queryFormats())) {
10
 
11
  public function convertImage($path, $quality)
12
  {
13
+ (new Server())->setSettings();
 
14
 
15
  try {
16
  if (!is_writable($path)) {
64
  private function convertToWebp($image, $path, $quality)
65
  {
66
  try {
67
+ $output = (new Directory())->getPath($path, true);
 
68
  if (!$output) {
69
  throw new \Exception(sprintf('An error occurred creating destination directory for "%s" file.', $path));
70
  } else if (!in_array('WEBP', $image->queryFormats())) {
app/Convert/Server.php ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace WebpConverter\Convert;
4
+
5
+ class Server
6
+ {
7
+ /* ---
8
+ Functions
9
+ --- */
10
+
11
+ public function setSettings()
12
+ {
13
+ ini_set('memory_limit', '1G');
14
+
15
+ if (strpos(ini_get('disable_functions'), 'set_time_limit') === false) {
16
+ set_time_limit(120);
17
+ }
18
+ }
19
+ }
app/Media/Htaccess.php CHANGED
@@ -50,7 +50,7 @@
50
  $content = '';
51
  if (!$settings['extensions']) return $content;
52
 
53
- $path = apply_filters('webpc_uploads_prefix', parse_url(site_url('/'), PHP_URL_PATH));
54
  $path .= apply_filters('webpc_uploads_webp', '', true);
55
 
56
  $content .= '<IfModule mod_rewrite.c>' . PHP_EOL;
50
  $content = '';
51
  if (!$settings['extensions']) return $content;
52
 
53
+ $path = apply_filters('webpc_uploads_prefix', '/');
54
  $path .= apply_filters('webpc_uploads_webp', '', true);
55
 
56
  $content .= '<IfModule mod_rewrite.c>' . PHP_EOL;
app/Regenerate/Endpoints.php CHANGED
@@ -71,8 +71,7 @@
71
  $params = $request->get_params();
72
  if ($params['skip_converted']) new Skip();
73
 
74
- $api = new Paths();
75
- $data = $api->getPaths();
76
  if ($data !== false) return new \WP_REST_Response($data, 200);
77
  else return new \WP_Error('webpc_rest_api_error', null, ['status' => 405]);
78
  }
@@ -80,8 +79,7 @@
80
  public function convertImages($request)
81
  {
82
  $params = $request->get_params();
83
- $api = new Regenerate();
84
- $data = $api->convertImages($params['paths']);
85
  if ($data !== false) return new \WP_REST_Response($data, 200);
86
  else return new \WP_Error('webpc_rest_api_error', null, ['status' => 405]);
87
  }
71
  $params = $request->get_params();
72
  if ($params['skip_converted']) new Skip();
73
 
74
+ $data = (new Paths())->getPaths();
 
75
  if ($data !== false) return new \WP_REST_Response($data, 200);
76
  else return new \WP_Error('webpc_rest_api_error', null, ['status' => 405]);
77
  }
79
  public function convertImages($request)
80
  {
81
  $params = $request->get_params();
82
+ $data = (new Regenerate())->convertImages($params['paths']);
 
83
  if ($data !== false) return new \WP_REST_Response($data, 200);
84
  else return new \WP_Error('webpc_rest_api_error', null, ['status' => 405]);
85
  }
app/Settings/Errors.php CHANGED
@@ -76,9 +76,9 @@
76
 
77
  private function ifBypassingApacheIsActive()
78
  {
79
- $pngUrl = WEBPC_URL . 'public/img/icon-before.png';
80
- $png2Url = WEBPC_URL . 'public/img/icon-before.png2';
81
 
82
- return (strlen(file_get_contents($pngUrl)) >= strlen(file_get_contents($png2Url)));
83
  }
84
  }
76
 
77
  private function ifBypassingApacheIsActive()
78
  {
79
+ $filePng = @file_get_contents(WEBPC_URL . 'public/img/icon-before.png');
80
+ $filePng2 = @file_get_contents(WEBPC_URL . 'public/img/icon-before.png2');
81
 
82
+ return (strlen($filePng) >= strlen($filePng2));
83
  }
84
  }
app/Settings/Paths.php CHANGED
@@ -9,10 +9,11 @@
9
 
10
  public function __construct()
11
  {
12
- add_filter('webpc_uploads_path', [$this, 'getSourcePath'], 0, 2);
13
- add_filter('webpc_uploads_webp', [$this, 'getOutputPath'], 0, 2);
14
- add_filter('webpc_uploads_path', [$this, 'parsePath'], 100, 2);
15
- add_filter('webpc_uploads_webp', [$this, 'parsePath'], 100, 2);
 
16
  }
17
 
18
  /* ---
@@ -36,4 +37,14 @@
36
  $path = apply_filters('webpc_uploads_root', ABSPATH);
37
  return $path . '/' . trim($value, '\/');
38
  }
 
 
 
 
 
 
 
 
 
 
39
  }
9
 
10
  public function __construct()
11
  {
12
+ add_filter('webpc_uploads_path', [$this, 'getSourcePath'], 0, 2);
13
+ add_filter('webpc_uploads_webp', [$this, 'getOutputPath'], 0, 2);
14
+ add_filter('webpc_uploads_path', [$this, 'parsePath'], 100, 2);
15
+ add_filter('webpc_uploads_webp', [$this, 'parsePath'], 100, 2);
16
+ add_filter('webpc_uploads_prefix', [$this, 'getPrefixPath'], 0);
17
  }
18
 
19
  /* ---
37
  $path = apply_filters('webpc_uploads_root', ABSPATH);
38
  return $path . '/' . trim($value, '\/');
39
  }
40
+
41
+ public function getPrefixPath($value)
42
+ {
43
+ $docDir = realpath($_SERVER['DOCUMENT_ROOT']);
44
+ $wpDir = realpath(ABSPATH);
45
+ $diffDir = trim(str_replace($docDir, '', $wpDir), '\/');
46
+ $diffPath = sprintf('/%s/', $diffDir);
47
+
48
+ return str_replace('//', '/', $diffPath);
49
+ }
50
  }
readme.txt CHANGED
@@ -150,6 +150,18 @@ Please remember that **Safari and Internet Explorer do not support the WebP form
150
 
151
  You can find more about WebP support by browsers [here](https://caniuse.com/#feat=webp).
152
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  = What to do after installing plugin? =
154
 
155
  After installing the plugin, you should convert all existing images.
@@ -187,6 +199,12 @@ If the converted image in WebP format is larger than the original, the browser w
187
 
188
  If you want to force the use of WebP files, uncheck the `Automatic removal of WebP files larger than original` option in the plugin settings. Then click on the `Regenerate All` button to convert all images again.
189
 
 
 
 
 
 
 
190
  = Where are converted images stored? =
191
 
192
  All WebP images are stored in the `/wp-content/uploads-webpc/` directory. Inside the directory there is the same structure as in the original `uploads` directory. The files have original extensions in the name along with the new `.webp`.
@@ -220,7 +238,7 @@ Directory path with converted WebP files *(relative to the root directory)*:
220
  Prefix in URL of `/wp-content/` directory or equivalent *(used in .htaccess)*:
221
 
222
  `add_filter('webpc_uploads_prefix', function($prefix) {
223
- return parse_url(site_url('/'), PHP_URL_PATH);
224
  });`
225
 
226
  For the following sample custom WordPress structure:
@@ -358,7 +376,7 @@ and add below code in this file:
358
  `server {`
359
  ` # ...
360
 
361
- location ~ /wp-content/uploads/(?<path>.+)\.(?<ext>jp?g|png|gif)$ {
362
  if ($http_accept !~* "image/webp") {
363
  break;
364
  }
@@ -424,6 +442,13 @@ This is all very important to us and allows us to do even better things for you!
424
 
425
  == Changelog ==
426
 
 
 
 
 
 
 
 
427
  = 1.2.4 (2020-04-24) =
428
  * `[Changed]` Error messages in administration panel
429
  * `[Added]` Action `webpc_delete_paths` to delete images by paths
150
 
151
  You can find more about WebP support by browsers [here](https://caniuse.com/#feat=webp).
152
 
153
+ = Forced rewrites to WebP =
154
+
155
+ The plugin uses rules in the .htaccess file to redirect from the original image to an image in WebP format. Verifies whether the WebP file exists and whether your browser supports the WebP format. It does this every time you try to load an image.
156
+
157
+ When you enter from a WebP supporting device, it will redirect. However, when someone uses a browser that does not support WebP, the redirection will not work and you get the original file.
158
+
159
+ However, if you see corrupted images on browsers that do not support WebP, it means that your server uses cache for rewrites from the .htaccess file. Then the plugin will not work properly because the server instead of reading the rules from the .htaccess file every time uses its cache and does the redirection automatically.
160
+
161
+ How can you check it? When you turn off the plugin, the rewrites from the .htaccess file are removed, which means you should see the original images on every browser. If this is not the case and you see forced redirects to WebP files, it means that your server remembers the redirects you made earlier and uses cache.
162
+
163
+ In this situation, please contact your server administrator. Each configuration is different. It can be e.g. module `mod_pagespeed` or other similar. This functionality must be turned off so that the server reads and executes the rules from the .htaccess file each time the images are loaded. This cannot be ignored because it will cause problems.
164
+
165
  = What to do after installing plugin? =
166
 
167
  After installing the plugin, you should convert all existing images.
199
 
200
  If you want to force the use of WebP files, uncheck the `Automatic removal of WebP files larger than original` option in the plugin settings. Then click on the `Regenerate All` button to convert all images again.
201
 
202
+ In addition, images may not be displayed in WebP format if they are files downloaded from the `/themes` directory or from a directory of another plugin that is not compatible.
203
+
204
+ Remember that this plugin supports images from the `/uploads` directory, i.e. files downloaded from the media library. Similarly, if your images are downloaded from another domain, i.e. from an external service.
205
+
206
+ When checking the operation of the plugin, e.g. in Dev Tools, pay attention to the path from which the files are downloaded.
207
+
208
  = Where are converted images stored? =
209
 
210
  All WebP images are stored in the `/wp-content/uploads-webpc/` directory. Inside the directory there is the same structure as in the original `uploads` directory. The files have original extensions in the name along with the new `.webp`.
238
  Prefix in URL of `/wp-content/` directory or equivalent *(used in .htaccess)*:
239
 
240
  `add_filter('webpc_uploads_prefix', function($prefix) {
241
+ return '/';
242
  });`
243
 
244
  For the following sample custom WordPress structure:
376
  `server {`
377
  ` # ...
378
 
379
+ location ~ /wp-content/uploads/(?<path>.+)\.(?<ext>jpe?g|png|gif)$ {
380
  if ($http_accept !~* "image/webp") {
381
  break;
382
  }
442
 
443
  == Changelog ==
444
 
445
+ = 1.2.5 (2020-05-10) =
446
+ * `[Removed]` Link to plugin settings on Network Admin Screen for WordPress Multisite
447
+ * `[Fixed]` Path in RewriteRule for WordPress Multisite
448
+ * `[Changed]` Error messages in administration panel
449
+ * `[Added]` Support for `disable_functions` setting for using `set_time_limit` function
450
+ * `[Added]` Support for blocked function `file_get_contents`
451
+
452
  = 1.2.4 (2020-04-24) =
453
  * `[Changed]` Error messages in administration panel
454
  * `[Added]` Action `webpc_delete_paths` to delete images by paths
resources/components/widgets/errors.php CHANGED
@@ -7,9 +7,10 @@
7
  <?php if (in_array('path_uploads', $errors)) : ?>
8
  <p>
9
  <?= sprintf(
10
- __('The path for /uploads files does not exist %s(function is_dir() returns false)%s. Please use the %s filter to set the correct path. The current using path is: %s.', 'webp-converter'),
11
  '<em>',
12
  '</em>',
 
13
  '<strong>webpc_uploads_path</strong>',
14
  '<strong>' . apply_filters('webpc_uploads_path', '') . '</strong>'
15
  ); ?>
@@ -28,9 +29,11 @@
28
  <?php if (in_array('path_webp', $errors)) : ?>
29
  <p>
30
  <?= sprintf(
31
- __('The path for saving converted WebP files does not exist and cannot be created %s(function is_writable() returns false)%s. Check your server configuration and try again. The current using path is: %s. Please contact your server administrator.', 'webp-converter'),
32
  '<em>',
33
  '</em>',
 
 
34
  '<strong>' . apply_filters('webpc_uploads_webp', '') . '</strong>'
35
  ); ?>
36
  </p>
7
  <?php if (in_array('path_uploads', $errors)) : ?>
8
  <p>
9
  <?= sprintf(
10
+ __('The path for /uploads files does not exist %s(function is_dir() returns false)%s. Use filters %s or %s to set the correct path. The current using path is: %s. Please read the plugin FAQ to learn more.', 'webp-converter'),
11
  '<em>',
12
  '</em>',
13
+ '<strong>webpc_uploads_root</strong>',
14
  '<strong>webpc_uploads_path</strong>',
15
  '<strong>' . apply_filters('webpc_uploads_path', '') . '</strong>'
16
  ); ?>
29
  <?php if (in_array('path_webp', $errors)) : ?>
30
  <p>
31
  <?= sprintf(
32
+ __('The path for saving converted WebP files does not exist and cannot be created %s(function is_writable() returns false)%s. Use filters %s or %s to set the correct path. The current using path is: %s. Please read the plugin FAQ to learn more.', 'webp-converter'),
33
  '<em>',
34
  '</em>',
35
+ '<strong>webpc_uploads_root</strong>',
36
+ '<strong>webpc_uploads_webp</strong>',
37
  '<strong>' . apply_filters('webpc_uploads_webp', '') . '</strong>'
38
  ); ?>
39
  </p>
webp-converter-for-media.php CHANGED
@@ -3,13 +3,13 @@
3
  /*
4
  Plugin Name: WebP Converter for Media
5
  Description: Speed up your website by serving WebP images instead of standard formats JPEG, PNG and GIF.
6
- Version: 1.2.4
7
  Author: Mateusz Gbiorczyk
8
  Author URI: https://gbiorczyk.pl/
9
  Text Domain: webp-converter
10
  */
11
 
12
- define('WEBPC_VERSION', '1.2.4');
13
  define('WEBPC_FILE', __FILE__);
14
  define('WEBPC_NAME', plugin_basename(__FILE__));
15
  define('WEBPC_PATH', plugin_dir_path(__FILE__));
3
  /*
4
  Plugin Name: WebP Converter for Media
5
  Description: Speed up your website by serving WebP images instead of standard formats JPEG, PNG and GIF.
6
+ Version: 1.2.5
7
  Author: Mateusz Gbiorczyk
8
  Author URI: https://gbiorczyk.pl/
9
  Text Domain: webp-converter
10
  */
11
 
12
+ define('WEBPC_VERSION', '1.2.5');
13
  define('WEBPC_FILE', __FILE__);
14
  define('WEBPC_NAME', plugin_basename(__FILE__));
15
  define('WEBPC_PATH', plugin_dir_path(__FILE__));