From c5ac648e886444fcafdf7b59a6cadda197c2c28c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Cabrera?= Date: Sun, 23 Jun 2024 10:56:26 -0400 Subject: [PATCH] Admin: adds Thumbnail Format setting --- src/Admin/Admin.php | 59 ++++++++++++++++++++++++-------------- src/Admin/admin-page.php | 7 +++++ src/Admin/screen-tools.php | 15 ++++++++++ src/Image.php | 38 ++++++++++-------------- src/Settings.php | 1 + 5 files changed, 75 insertions(+), 45 deletions(-) diff --git a/src/Admin/Admin.php b/src/Admin/Admin.php index fa0ccad9..cafd0889 100644 --- a/src/Admin/Admin.php +++ b/src/Admin/Admin.php @@ -1287,36 +1287,51 @@ public function get_default_thumbnail() public function clear_thumbnails() { $wpp_uploads_dir = $this->thumbnail->get_plugin_uploads_dir(); + $token = isset($_POST['token']) ? $_POST['token'] : null; // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- This is a nonce - if ( is_array($wpp_uploads_dir) && ! empty($wpp_uploads_dir) ) { - $token = isset($_POST['token']) ? $_POST['token'] : null; // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- This is a nonce + if ( + current_user_can('edit_published_posts') + && wp_verify_nonce($token, 'wpp_nonce_reset_thumbnails') + ) { + echo $this->delete_thumbnails(); + } else { + echo 4; + } - if ( - current_user_can('edit_published_posts') - && wp_verify_nonce($token, 'wpp_nonce_reset_thumbnails') - ) { - if ( is_dir($wpp_uploads_dir['basedir']) ) { - $files = glob("{$wpp_uploads_dir['basedir']}/*"); // get all related images + wp_die(); + } - if ( is_array($files) && ! empty($files) ) { - foreach( $files as $file ){ // iterate files - if ( is_file($file) ) { - @unlink($file); // delete file - } - } - echo 1; - } else { - echo 2; + /** + * Deletes WPP thumbnails from the uploads/wordpress-popular-posts folder. + * + * @since 7.0.0 + * @return int 1 on success, 2 if no thumbnails were found, 3 if WPP's folder can't be reached + */ + private function delete_thumbnails() + { + $wpp_uploads_dir = $this->thumbnail->get_plugin_uploads_dir(); + + if ( + is_array($wpp_uploads_dir) + && ! empty($wpp_uploads_dir) + && is_dir($wpp_uploads_dir['basedir']) + ) { + $files = glob("{$wpp_uploads_dir['basedir']}/*"); + + if ( is_array($files) && ! empty($files) ) { + foreach( $files as $file ) { + if ( is_file($file) ) { + @unlink($file); // delete file } - } else { - echo 3; } - } else { - echo 4; + + return 1; } + + return 2; } - wp_die(); + return 3; } /** diff --git a/src/Admin/admin-page.php b/src/Admin/admin-page.php index c3e83a9d..233b969a 100644 --- a/src/Admin/admin-page.php +++ b/src/Admin/admin-page.php @@ -60,7 +60,14 @@ $this->flush_transients(); } + $thumbnail_format = sanitize_text_field($_POST['thumb_format']); + + if ( $thumbnail_format !== $this->config['tools']['thumbnail']['format'] ) { + $this->delete_thumbnails(); + } + $this->config['tools']['thumbnail']['source'] = sanitize_text_field($_POST['thumb_source']); + $this->config['tools']['thumbnail']['format'] = $thumbnail_format; $this->config['tools']['thumbnail']['field'] = ( ! empty($_POST['thumb_field']) ) ? sanitize_text_field($_POST['thumb_field']) : 'wpp_thumbnail'; $this->config['tools']['thumbnail']['default'] = ( ! empty($_POST['upload_thumb_src']) ) ? $_POST['upload_thumb_src'] : $this->config['tools']['thumbnail']['default']; $this->config['tools']['thumbnail']['resize'] = (bool) $_POST['thumb_field_resize']; diff --git a/src/Admin/screen-tools.php b/src/Admin/screen-tools.php index dacc0080..6902faa6 100644 --- a/src/Admin/screen-tools.php +++ b/src/Admin/screen-tools.php @@ -5,6 +5,9 @@ echo '

' . esc_html(__('Sorry, you do not have enough permissions to do this. Please contact the site administrator for support.', 'wordpress-popular-posts')) . '

'; } else { + // Image formats support + $webp_support = \WP_Image_Editor_GD::supports_mime_type('image/webp'); + $avif_support = \WP_Image_Editor_GD::supports_mime_type('image/avif'); ?>

@@ -52,6 +55,18 @@

.

+ + + + + + + + [?] diff --git a/src/Image.php b/src/Image.php index 5c317124..375e2fee 100644 --- a/src/Image.php +++ b/src/Image.php @@ -818,29 +818,21 @@ private function generate_thumbnail(string $path, string $filename, array $size, $mime_type = null; - /** - * Hook to tell WPP whether to generate thumbnails in the original format. - * @since 6.5.0 - */ - $keep_original_format = apply_filters('wpp_thumbnail_use_original_format', false); - - if ( ! $keep_original_format ) { - /** - * Let's try to generate an .avif/.webp thumbnail if server's image - * processing library (Imagick / GD) supports it. - * - * @since 6.5.0 - */ - // .avif support requires WP 6.5 or higher - if ( $image->supports_mime_type('image/avif') ) { - $filename = substr($filename, 0, strrpos($filename, '.')) . '.avif'; - $mime_type = 'image/avif'; - } - // .webp support requires WP 5.8 or higher - elseif ( $image->supports_mime_type('image/webp') ) { - $filename = substr($filename, 0, strrpos($filename, '.')) . '.webp'; - $mime_type = 'image/webp'; - } + // .webp thumbnails? + if ( + 'webp' === $this->admin_options['tools']['thumbnail']['format'] + && $image->supports_mime_type('image/webp') // .webp support requires WP 5.8 or higher + ) { + $filename = substr($filename, 0, strrpos($filename, '.')) . '.webp'; + $mime_type = 'image/webp'; + } + // .avif thumbnails? + elseif ( + 'avif' === $this->admin_options['tools']['thumbnail']['format'] + && $image->supports_mime_type('image/avif') // .avif support requires WP 6.5 or higher + ) { + $filename = substr($filename, 0, strrpos($filename, '.')) . '.avif'; + $mime_type = 'image/avif'; } $file_path = trailingslashit($this->get_plugin_uploads_dir()['basedir']) . $filename; diff --git a/src/Settings.php b/src/Settings.php index 14380fe7..3b6e82a4 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -106,6 +106,7 @@ class Settings { ], 'thumbnail' => [ 'source' => 'featured', + 'format' => 'original', 'field' => '', 'resize' => false, 'default' => '',