Skip to content

Commit

Permalink
Admin: adds Thumbnail Format setting
Browse files Browse the repository at this point in the history
  • Loading branch information
cabrerahector committed Jun 23, 2024
1 parent 1473c9f commit c5ac648
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 45 deletions.
59 changes: 37 additions & 22 deletions src/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Admin/admin-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
15 changes: 15 additions & 0 deletions src/Admin/screen-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
echo '<p style="text-align: center;">' . esc_html(__('Sorry, you do not have enough permissions to do this. Please contact the site administrator for support.', 'wordpress-popular-posts')) . '</p>';
}
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');
?>
<div id="wpp_tools">
<h3 class="wmpp-subtitle"><?php esc_html_e('Thumbnails', 'wordpress-popular-posts'); ?></h3>
Expand Down Expand Up @@ -52,6 +55,18 @@
<p class="description"><?php esc_html_e('Tell WordPress Popular Posts where it should get thumbnails from', 'wordpress-popular-posts'); ?>.</p>
</td>
</tr>
<?php if ( $webp_support || $avif_support ) : ?>
<tr valign="top">
<th scope="row"><label for="thumb_format"><?php esc_html_e('Thumbnail format', 'wordpress-popular-posts'); ?>:</label></th>
<td>
<select name="thumb_format" id="thumb_format">
<option <?php if ($this->config['tools']['thumbnail']['format'] == 'original') { ?>selected="selected"<?php } ?> value="original"><?php esc_html_e('Use original format', 'wordpress-popular-posts'); ?></option>
<?php if ( $avif_support ) : ?><option <?php if ($this->config['tools']['thumbnail']['format'] == 'avif') { ?>selected="selected"<?php } ?> value="avif">avif</option><?php endif; ?>
<?php if ( $webp_support ) : ?><option <?php if ($this->config['tools']['thumbnail']['format'] == 'webp') { ?>selected="selected"<?php } ?> value="webp">webp</option><?php endif; ?>
</select>
</td>
</tr>
<?php endif; ?>
<tr valign="top">
<th scope="row"><label for="thumb_lazy_load"><?php esc_html_e('Lazy load', 'wordpress-popular-posts'); ?>:</label> <small>[<a href="https://github.com/cabrerahector/wordpress-popular-posts/wiki/7.-Performance#lazy-loading" target="_blank" title="<?php esc_attr_e('What is this?', 'wordpress-popular-posts'); ?>">?</a>]</small></th>
<td>
Expand Down
38 changes: 15 additions & 23 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class Settings {
],
'thumbnail' => [
'source' => 'featured',
'format' => 'original',
'field' => '',
'resize' => false,
'default' => '',
Expand Down

0 comments on commit c5ac648

Please sign in to comment.