Analysis result
Scanned at : 2023-10-14
Analyzed by : Coding Standard PSR12
Your PHP Code Snippet
<?php
namespace WebAvifConverter\Hooks;
use WebAvifConverter\Utils;
use WebAvifConverter\GeneratorUtils;
// function p($a, $die = 0){
// echo '<pre>';
// echo print_r($a);
// echo '</pre>';
// if ($die) { wp_die(); }
// }
apply_filters('jepg_quality', get_option('wac_quality_jpeg', DEFAULT_QUALITY));
add_filter('wp_generate_attachment_metadata', 'WebAvifConverter\Hooks\convert_images_on_generate_attachment_metadata', 10, 2);
function convert_images_on_generate_attachment_metadata($metadata, $attachment_id)
{
if (!isset($metadata['file'])) { return $metadata; }
$extension = pathinfo($metadata['file'], PATHINFO_EXTENSION);
if (!in_array($extension, ['jpg', 'jpeg', 'png'])) { return $metadata; }
$metadata['quality_jpeg'] = get_option('wac_quality_jpeg', DEFAULT_QUALITY);
wp_update_attachment_metadata($attachment_id, $metadata);
// Generating JPEGs with adjusted quality is handled by jepg_quality filter
GeneratorUtils\generate_webp_sizes($attachment_id, get_option('wac_quality_webp', DEFAULT_QUALITY));
GeneratorUtils\generate_avif_sizes($attachment_id, get_option('wac_quality_avif', DEFAULT_QUALITY));
return wp_get_attachment_metadata($attachment_id);
}
add_action('edit_attachment', 'WebAvifConverter\Hooks\save_image_quality_metadata');
function save_image_quality_metadata($attachment_id)
{
$metadata = wp_get_attachment_metadata($attachment_id);
$filed_names = ['quality_jpeg', 'quality_avif', 'quality_webp' ];
foreach ($filed_names as $filed_name) {
if (empty($_REQUEST['attachments'][$attachment_id][$filed_name])) { continue; }
$quality = intval($_REQUEST['attachments'][$attachment_id][$filed_name]);
if ($quality < 1 || $quality > 100 || isset($metadata[$filed_name]) && $quality === $metadata[$filed_name]) { continue; }
if ($filed_name === 'quality_jpeg') { GeneratorUtils\generate_jpeg_sizes($attachment_id, $quality); }
if ($filed_name === 'quality_webp') { GeneratorUtils\generate_webp_sizes($attachment_id, $quality); }
if ($filed_name === 'quality_avif') { GeneratorUtils\generate_avif_sizes($attachment_id, $quality); }
}
}
function update_attachment_quality($attachment_id, $quality_webp = DEFAULT_QUALITY, $quality_avif = DEFAULT_QUALITY, $quality_jpeg = DEFAULT_QUALITY)
{
$metadata = wp_get_attachment_metadata($attachment_id);
if (empty($metadata) || !isset($metadata['file']) || !isset($metadata['sizes'])) {
return; // No metadata found for this attachment.
}
if(!isset($metadata['quality_jpeg']) || intval($metadata['quality_jpeg']) !== $quality_jpeg){
GeneratorUtils\generate_jpeg_sizes($attachment_id, $quality_jpeg);
}
if(!isset($metadata['quality_webp']) || intval($metadata['quality_webp']) !== $quality_webp){
GeneratorUtils\generate_webp_sizes($attachment_id, $quality_webp);
}
if(!isset($metadata['quality_avif']) || intval($metadata['quality_avif']) !== $quality_avif){
GeneratorUtils\generate_avif_sizes($attachment_id, $quality_avif);
}
}
function update_all_attachments_quality($quality_webp, $quality_avif, $quality_jpeg)
{
Utils\update_progress_bar(0);
$attachments = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'exclude' => get_post_thumbnail_id()
));
$total_attachments = count($attachments);
$progress = 0;
foreach ($attachments as $attachment) {
$progress++;
$percent = intval($progress / $total_attachments * 100);
Utils\update_progress_bar($percent);
update_attachment_quality($attachment->ID, $quality_webp, $quality_avif, $quality_jpeg);
}
}
add_filter('delete_attachment', 'WebAvifConverter\Hooks\delete_attachment_files');
function delete_attachment_files($attachment_id)
{
$metadata = wp_get_attachment_metadata($attachment_id);
if (empty($metadata)) { return; }
$upload_dir = wp_upload_dir()['basedir'];
if (array_key_exists('file_webp', $metadata)) {
Utils\safe_unlink($upload_dir . '/' . $metadata['file_webp']);
unset($metadata['file_webp']);
}
if (array_key_exists('file_avif', $metadata)) {
Utils\safe_unlink($upload_dir . '/' . $metadata['file_avif']);
unset($metadata['file_avif']);
}
$attachment_subdir = dirname($metadata['file']);
$attachment_files_dir = $upload_dir . '/' . $attachment_subdir . '/';
if (array_key_exists('sizes_avif', $metadata)) {
foreach ($metadata['sizes_avif'] as $size) {
Utils\safe_unlink($attachment_files_dir . $size['file']);
}
unset($metadata['sizes_avif']);
}
if (array_key_exists('quality_webp', $metadata)){
unset($metadata['quality_webp']);
}
if (array_key_exists('quality_avif', $metadata)){
unset($metadata['quality_avif']);
}
if (array_key_exists('sizes_webp', $metadata)) {
foreach ($metadata['sizes_webp'] as $size) {
Utils\safe_unlink($attachment_files_dir . $size['file']);
}
unset($metadata['sizes_webp']);
}wp_update_attachment_metadata($attachment_id, $metadata);
}
function delete_all_attachments_avif_and_webp()
{
Utils\update_progress_bar(0);
$attachments = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'exclude' => get_post_thumbnail_id()
));
$total_attachments = count($attachments);
$progress = 0;
foreach ($attachments as $attachment) {
delete_attachment_files($attachment->ID);
$progress++;
$percent = intval($progress / $total_attachments * 100) . "%";
Utils\update_progress_bar($percent);
}
}
Thanks for your like.
Your like has been removed.
Your dislike has been saved.
Your dislike has been removed.
An error occurred: Unable to save your like or dislike.
Errors: 40
Warning: 4
Severity | Type | Message | Location |
---|---|---|---|
5 | WARNING | A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 19 and the first side effect is on line 16. | Line 1, column 1 |
5 | ERROR | End of line character is invalid; expected "\n" but found "\r\n" | Line 1, column 1 |
5 | WARNING | Line exceeds 120 characters; contains 126 characters | Line 18, column 126 |
5 | ERROR | Newline required after opening brace | Line 21, column 36 |
5 | ERROR | Closing brace must be on a line by itself | Line 21, column 56 |
5 | ERROR | Newline required after opening brace | Line 24, column 56 |
5 | ERROR | Closing brace must be on a line by itself | Line 24, column 76 |
5 | ERROR | Whitespace found at end of line | Line 25, column 1 |
5 | ERROR | Whitespace found at end of line | Line 41, column 2 |
5 | ERROR | Newline required after opening brace | Line 47, column 75 |
5 | ERROR | Closing brace must be on a line by itself | Line 47, column 87 |
5 | ERROR | Newline required after opening brace | Line 51, column 117 |
5 | WARNING | Line exceeds 120 characters; contains 129 characters | Line 51, column 129 |
5 | ERROR | Closing brace must be on a line by itself | Line 51, column 129 |
5 | ERROR | Newline required after opening brace | Line 53, column 45 |
5 | ERROR | Closing brace must be on a line by itself | Line 53, column 109 |
5 | ERROR | Newline required after opening brace | Line 54, column 45 |
5 | ERROR | Closing brace must be on a line by itself | Line 54, column 109 |
5 | ERROR | Newline required after opening brace | Line 55, column 45 |
5 | ERROR | Closing brace must be on a line by itself | Line 55, column 109 |
5 | ERROR | Whitespace found at end of line | Line 56, column 6 |
5 | WARNING | Line exceeds 120 characters; contains 149 characters | Line 59, column 149 |
5 | ERROR | Expected 1 space(s) after IF keyword; 0 found | Line 67, column 5 |
5 | ERROR | Expected 1 space(s) after closing parenthesis; found 0 | Line 67, column 96 |
5 | ERROR | Blank line found at start of control structure | Line 67, column 97 |
5 | ERROR | Whitespace found at end of line | Line 68, column 1 |
5 | ERROR | Expected 1 space(s) after IF keyword; 0 found | Line 72, column 5 |
5 | ERROR | Expected 1 space(s) after closing parenthesis; found 0 | Line 72, column 96 |
5 | ERROR | Blank line found at start of control structure | Line 72, column 97 |
5 | ERROR | Whitespace found at end of line | Line 73, column 1 |
5 | ERROR | Whitespace found at end of line | Line 75, column 1 |
5 | ERROR | Whitespace found at end of line | Line 78, column 1 |
5 | ERROR | Expected 1 space(s) after IF keyword; 0 found | Line 79, column 5 |
5 | ERROR | Expected 1 space(s) after closing parenthesis; found 0 | Line 79, column 96 |
5 | ERROR | Whitespace found at end of line | Line 97, column 1 |
5 | ERROR | Newline required after opening brace | Line 110, column 27 |
5 | ERROR | Closing brace must be on a line by itself | Line 110, column 37 |
5 | ERROR | Whitespace found at end of line | Line 133, column 1 |
5 | ERROR | Expected 1 space(s) after closing parenthesis; found 0 | Line 134, column 52 |
5 | ERROR | Whitespace found at end of line | Line 137, column 1 |
5 | ERROR | Expected 1 space(s) after closing parenthesis; found 0 | Line 138, column 52 |
5 | ERROR | Blank line found at start of control structure | Line 138, column 53 |
5 | ERROR | Whitespace found at end of line | Line 167, column 50 |
5 | ERROR | Expected 1 newline at end of file; 0 found | Line 172, column 1 |