Edit File by line
/home/zeestwma/ajeebong.../wp-conte.../plugins/image-op.../modules/optimiza.../classes
File: validate-image.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace ImageOptimization\Modules\Optimization\Classes;
[2] Fix | Delete
[3] Fix | Delete
use ImageOptimization\Classes\Image\{
[4] Fix | Delete
Exceptions\Invalid_Image_Exception,
[5] Fix | Delete
Image,
[6] Fix | Delete
Image_Meta,
[7] Fix | Delete
WP_Image_Meta,
[8] Fix | Delete
};
[9] Fix | Delete
use ImageOptimization\Classes\File_System\Exceptions\File_System_Operation_Error;
[10] Fix | Delete
use ImageOptimization\Classes\File_System\File_System;
[11] Fix | Delete
use ImageOptimization\Classes\File_Utils;
[12] Fix | Delete
use ImageOptimization\Modules\Optimization\Classes\Exceptions\Image_Validation_Error;
[13] Fix | Delete
use ImageOptimization\Plugin;
[14] Fix | Delete
[15] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[16] Fix | Delete
exit; // Exit if accessed directly.
[17] Fix | Delete
}
[18] Fix | Delete
[19] Fix | Delete
class Validate_Image {
[20] Fix | Delete
public const MAX_FILE_SIZE = 10 * 1024 * 1024;
[21] Fix | Delete
public const MAX_BIG_FILE_SIZE = 25 * 1024 * 1024;
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Returns true if $image_id provided associated with an image that can be optimized.
[25] Fix | Delete
*
[26] Fix | Delete
* @param int $image_id Attachment id.
[27] Fix | Delete
*
[28] Fix | Delete
* @return true
[29] Fix | Delete
* @throws Image_Validation_Error
[30] Fix | Delete
* @throws Invalid_Image_Exception
[31] Fix | Delete
*/
[32] Fix | Delete
public static function is_valid( int $image_id ): bool {
[33] Fix | Delete
$attachment_object = get_post( $image_id );
[34] Fix | Delete
[35] Fix | Delete
if ( ! $attachment_object ) {
[36] Fix | Delete
throw new Image_Validation_Error(
[37] Fix | Delete
esc_html__( 'Can\'t optimize this file. If the issue persists, Contact Support', 'image-optimization' )
[38] Fix | Delete
);
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
if ( ! wp_attachment_is_image( $attachment_object ) ||
[42] Fix | Delete
! in_array( $attachment_object->post_mime_type, Image::get_supported_mime_types(), true ) ||
[43] Fix | Delete
(
[44] Fix | Delete
in_array( $attachment_object->post_mime_type, Image::get_mime_types_cannot_be_optimized(), true ) &&
[45] Fix | Delete
! get_post_meta( $image_id, Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, true )
[46] Fix | Delete
)
[47] Fix | Delete
) {
[48] Fix | Delete
throw new Image_Validation_Error( esc_html( self::prepare_supported_formats_list_error() ) );
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
if ( ! file_exists( get_attached_file( $image_id ) ) ) {
[52] Fix | Delete
throw new Image_Validation_Error(
[53] Fix | Delete
esc_html__( 'File is missing. Verify the upload', 'image-optimization' )
[54] Fix | Delete
);
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
self::validate_file_size( $image_id );
[58] Fix | Delete
[59] Fix | Delete
return true;
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* Prepares the error message for the unsupported file formats.
[64] Fix | Delete
*
[65] Fix | Delete
* @return string The error message.
[66] Fix | Delete
*/
[67] Fix | Delete
private static function prepare_supported_formats_list_error(): string {
[68] Fix | Delete
$formats = Image::get_supported_formats();
[69] Fix | Delete
$formats = array_filter(
[70] Fix | Delete
$formats,
[71] Fix | Delete
fn ( $format ) => ! in_array( $format, Image::get_formats_cannot_be_optimized(), true )
[72] Fix | Delete
);
[73] Fix | Delete
$last_item = strtoupper( array_pop( $formats ) );
[74] Fix | Delete
[75] Fix | Delete
$formats_list = join( ', ', array_map( 'strtoupper', $formats ) );
[76] Fix | Delete
[77] Fix | Delete
return sprintf(
[78] Fix | Delete
__( 'Unsupported file format. Only %1$s, or %2$s are supported', 'image-optimization' ),
[79] Fix | Delete
$formats_list,
[80] Fix | Delete
$last_item
[81] Fix | Delete
);
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
/**
[85] Fix | Delete
* @throws Invalid_Image_Exception
[86] Fix | Delete
* @throws Image_Validation_Error
[87] Fix | Delete
*/
[88] Fix | Delete
public static function validate_file_size( int $image_id, string $image_size = Image::SIZE_FULL ) {
[89] Fix | Delete
try {
[90] Fix | Delete
$wp_meta = new WP_Image_Meta( $image_id );
[91] Fix | Delete
$image_size = $wp_meta->get_file_size( $image_size )
[92] Fix | Delete
?? File_System::size( ( new Image( $image_id ) )->get_file_path( $image_size ) );
[93] Fix | Delete
} catch ( File_System_Operation_Error $e ) {
[94] Fix | Delete
throw new Image_Validation_Error(
[95] Fix | Delete
esc_html__( 'File is missing. Verify the upload', 'image-optimization' )
[96] Fix | Delete
);
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
if ( $image_size > self::MAX_FILE_SIZE ) {
[100] Fix | Delete
if ( ! self::are_big_files_supported() ) {
[101] Fix | Delete
throw new Image_Validation_Error(
[102] Fix | Delete
esc_html(
[103] Fix | Delete
sprintf(
[104] Fix | Delete
__( 'File is too large. Max size is %s', 'image-optimization' ),
[105] Fix | Delete
File_Utils::format_file_size( self::MAX_FILE_SIZE, 0 ),
[106] Fix | Delete
)
[107] Fix | Delete
)
[108] Fix | Delete
);
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
if ( $image_size > self::MAX_BIG_FILE_SIZE ) {
[112] Fix | Delete
throw new Image_Validation_Error(
[113] Fix | Delete
esc_html(
[114] Fix | Delete
sprintf(
[115] Fix | Delete
__( 'File is too large. Max size is %s', 'image-optimization' ),
[116] Fix | Delete
File_Utils::format_file_size( self::MAX_BIG_FILE_SIZE, 0 ),
[117] Fix | Delete
)
[118] Fix | Delete
)
[119] Fix | Delete
);
[120] Fix | Delete
}
[121] Fix | Delete
}
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* Returns the max file size allowed by the current plan.
[126] Fix | Delete
*
[127] Fix | Delete
* @return int File size in bytes.
[128] Fix | Delete
*/
[129] Fix | Delete
public static function get_max_file_size(): int {
[130] Fix | Delete
return self::are_big_files_supported() ? self::MAX_BIG_FILE_SIZE : self::MAX_FILE_SIZE;
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
public static function are_big_files_supported(): bool {
[134] Fix | Delete
static $is_allowed = null;
[135] Fix | Delete
$connect_manager = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
[136] Fix | Delete
[137] Fix | Delete
if ( null === $is_allowed ) {
[138] Fix | Delete
if ( ! $connect_manager->connect_instance->is_connected() ) {
[139] Fix | Delete
$is_allowed = false;
[140] Fix | Delete
return $is_allowed;
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
$plan_data = $connect_manager->connect_instance->get_connect_status();
[144] Fix | Delete
[145] Fix | Delete
if ( ! isset( $plan_data->subscription_plan->features->large_upload_allowed ) ) {
[146] Fix | Delete
$is_allowed = false;
[147] Fix | Delete
} else {
[148] Fix | Delete
$is_allowed = (bool) $plan_data->subscription_plan->features->large_upload_allowed;
[149] Fix | Delete
}
[150] Fix | Delete
}
[151] Fix | Delete
[152] Fix | Delete
return $is_allowed;
[153] Fix | Delete
}
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function