diff --git a/phpcs.xml b/phpcs.xml index 9d0d00d2..b56e261b 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -2,8 +2,24 @@ - - + + + + + + + + + + + + + + + + + + diff --git a/src/class-tiny-apache-rewrite.php b/src/class-tiny-apache-rewrite.php index e91f4c56..e6944d70 100644 --- a/src/class-tiny-apache-rewrite.php +++ b/src/class-tiny-apache-rewrite.php @@ -31,6 +31,7 @@ class Tiny_Apache_Rewrite extends Tiny_WP_Base { /** * seperator when rules are inserted + * * @var string */ const MARKER = 'tiny-compress-images'; @@ -40,9 +41,8 @@ class Tiny_Apache_Rewrite extends Tiny_WP_Base { * hooked into `update_option_tinypng_convert_format` * https://developer.wordpress.org/reference/hooks/update_option_option/ * - * - * @param mixed $old_value - * @param mixed $value + * @param mixed $old_value + * @param mixed $value * @param string $option * @return void */ diff --git a/src/class-tiny-bulk-optimization.php b/src/class-tiny-bulk-optimization.php index 52542ccb..a5d7fd15 100644 --- a/src/class-tiny-bulk-optimization.php +++ b/src/class-tiny-bulk-optimization.php @@ -42,7 +42,7 @@ public static function get_optimization_statistics( $settings, $result = null ) if ( isset( $last_image['ID'] ) ) { $last_image_id = $last_image['ID']; } - } while ( sizeof( $result ) == self::PAGING_SIZE ); + } while ( count( $result ) == self::PAGING_SIZE ); } else { $stats = self::populate_optimization_statistics( $settings, $result, $stats ); } @@ -104,7 +104,7 @@ private static function populate_optimization_statistics( $settings, $result, $s $active_tinify_sizes = $settings->get_active_tinify_sizes(); $conversion_enabled = $settings->get_conversion_enabled(); - for ( $i = 0; $i < sizeof( $result ); $i++ ) { + for ( $i = 0; $i < count( $result ); $i++ ) { $wp_metadata = unserialize( (string) $result[ $i ]['meta_value'] ); $tiny_metadata = isset( $result[ $i ]['tiny_meta_value'] ) ? unserialize( (string) $result[ $i ]['tiny_meta_value'] ) : diff --git a/src/class-tiny-cli.php b/src/class-tiny-cli.php index ba02f3af..d4f8a182 100644 --- a/src/class-tiny-cli.php +++ b/src/class-tiny-cli.php @@ -52,7 +52,6 @@ public static function register_command( $settings ) { * optimize all unprocessed images * wp tiny optimize * - * * @param array $args * @param array $assoc_args * @return void diff --git a/src/class-tiny-compress-client.php b/src/class-tiny-compress-client.php index 3acd24b8..2f3531d6 100644 --- a/src/class-tiny-compress-client.php +++ b/src/class-tiny-compress-client.php @@ -98,7 +98,7 @@ protected function validate() { } throw new Tiny_Exception( - $err->getMessage(), + esc_html( $err->getMessage() ), get_class( $err ), $err->status ); @@ -165,7 +165,7 @@ protected function compress( $input, $resize_opts, $preserve_opts, $convert_to ) ); throw new Tiny_Exception( - $err->getMessage(), + esc_html( $err->getMessage() ), get_class( $err ), $err->status ); @@ -184,7 +184,7 @@ public function create_key( $email, $options ) { $this->last_error_code = $err->status; throw new Tiny_Exception( - $err->getMessage(), + esc_html( $err->getMessage() ), get_class( $err ), $err->status ); @@ -192,7 +192,8 @@ public function create_key( $email, $options ) { } private function set_request_options( $client ) { - /* The client does not let us override cURL properties yet, so we have + /* + The client does not let us override cURL properties yet, so we have to use a reflection property. */ $property = new ReflectionProperty( $client, 'options' ); $property->setAccessible( true ); diff --git a/src/class-tiny-compress.php b/src/class-tiny-compress.php index d17f55c9..75ced00c 100644 --- a/src/class-tiny-compress.php +++ b/src/class-tiny-compress.php @@ -96,9 +96,9 @@ public function get_status() { /** * Compresses a single file * - * @param [type] $file - * @param array $resize_opts - * @param array $preserve_opts + * @param [type] $file + * @param array $resize_opts + * @param array $preserve_opts * @param array{ string } conversion options * @return void */ diff --git a/src/class-tiny-helpers.php b/src/class-tiny-helpers.php index f2140d3c..51c678c3 100644 --- a/src/class-tiny-helpers.php +++ b/src/class-tiny-helpers.php @@ -29,7 +29,7 @@ class Tiny_Helpers { * We can use mb_strlen & mb_substr as WordPress provides a compat function for * it if mbstring php module is not installed. * - * @param string $text the text + * @param string $text the text * @param integer $length the maximum length of the string * @return string the truncated string */ @@ -173,17 +173,17 @@ public static function get_wp_filesystem() { } /** - * Polyfill for `str_starts_with()` function added in PHP 8.0. - * - * Performs a case-sensitive check indicating if - * the haystack begins with needle. - * - * @since 5.9.0 - * - * @param string $haystack The string to search in. - * @param string $needle The substring to search for in the `$haystack`. - * @return bool True if `$haystack` starts with `$needle`, otherwise false. - */ + * Polyfill for `str_starts_with()` function added in PHP 8.0. + * + * Performs a case-sensitive check indicating if + * the haystack begins with needle. + * + * @since 5.9.0 + * + * @param string $haystack The string to search in. + * @param string $needle The substring to search for in the `$haystack`. + * @return bool True if `$haystack` starts with `$needle`, otherwise false. + */ public static function str_starts_with( $haystack, $needle ) { if ( function_exists( 'str_starts_with' ) ) { return str_starts_with( $haystack, $needle ); diff --git a/src/class-tiny-image.php b/src/class-tiny-image.php index a586f430..8c7ceaf1 100644 --- a/src/class-tiny-image.php +++ b/src/class-tiny-image.php @@ -51,7 +51,8 @@ private function parse_wp_metadata() { } if ( ! is_array( $this->wp_metadata ) || ! isset( $this->wp_metadata['file'] ) ) { - /* No file metadata found, this might be another plugin messing with + /* + No file metadata found, this might be another plugin messing with metadata. Simply ignore this! */ return; } @@ -63,7 +64,8 @@ private function parse_wp_metadata() { $path_prefix .= $path_info['dirname'] . '/'; } - /* Do not use pathinfo for getting the filename. + /* + Do not use pathinfo for getting the filename. It doesn't work when the filename starts with a special character. */ $path_parts = explode( '/', $this->wp_metadata['file'] ); $this->name = end( $path_parts ); @@ -471,7 +473,7 @@ public function get_savings( $stats ) { public function get_statistics( $active_sizes, $active_tinify_sizes ) { if ( $this->statistics ) { - error_log( 'Strangely the image statistics are asked for again.' ); + Tiny_Logger::error( 'Strangely the image statistics are asked for again.' ); return $this->statistics; } diff --git a/src/class-tiny-logger.php b/src/class-tiny-logger.php index 311eec79..b50da5be 100644 --- a/src/class-tiny-logger.php +++ b/src/class-tiny-logger.php @@ -22,7 +22,6 @@ /** * Handles logging of plugin events to file. * - * * @since 3.7.0 */ class Tiny_Logger { @@ -73,7 +72,7 @@ public static function init() { 'pre_update_option_tinypng_logging_enabled', 'Tiny_Logger::on_save_log_enabled', 10, - 3 + 2 ); } @@ -114,7 +113,7 @@ public function get_log_file_path() { * * @return bool true if enabled */ - public static function on_save_log_enabled( $log_enabled, $old, $option ) { + public static function on_save_log_enabled( $log_enabled, $old ) { $instance = self::get_instance(); $was_enabled = 'on' === $old; $is_now_enabled = 'on' === $log_enabled; @@ -190,6 +189,7 @@ private function log( $level, $message, $context = array() ) { $context_str = ! empty( $context ) ? ' ' . wp_json_encode( $context ) : ''; $log_entry = "[{$timestamp}] [{$level_str}] {$message}{$context_str}" . PHP_EOL; + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( $log_entry, 3, $this->log_file_path ); } diff --git a/src/class-tiny-php.php b/src/class-tiny-php.php index 91071da0..bbeb5818 100644 --- a/src/class-tiny-php.php +++ b/src/class-tiny-php.php @@ -37,8 +37,8 @@ public static function curl_exec_disabled() { } public static function client_supported() { - return Tiny_PHP::has_fully_supported_php() && - Tiny_PHP::curl_available() && - ! Tiny_PHP::curl_exec_disabled(); + return self::has_fully_supported_php() && + self::curl_available() && + ! self::curl_exec_disabled(); } } diff --git a/src/class-tiny-picture.php b/src/class-tiny-picture.php index ab318131..ae3f5e41 100644 --- a/src/class-tiny-picture.php +++ b/src/class-tiny-picture.php @@ -109,7 +109,7 @@ private function replace_picture_sources( $content ) { private function replace_img_sources( $content ) { $image_sources = $this->filter_images( $content ); foreach ( $image_sources as $image_source ) { - $content = Tiny_Picture::replace_image( $content, $image_source ); + $content = self::replace_image( $content, $image_source ); } return $content; } @@ -145,7 +145,7 @@ private function filter_pictures( $content ) { /** * Will add additional sourcesets to picture elements. * - * @param string $content the full page content + * @param string $content the full page content * @param Tiny_Source_Picture $source the picture element * * @return string the updated content including augmented picture elements @@ -159,7 +159,7 @@ private function replace_picture( $content, $source ) { /** * Will replace img elements with picture elements that (possibly) have additional formats. * - * @param string $content the full page content + * @param string $content the full page content * @param Tiny_Source_Image $source the picture element * * @return string the updated content including augmented picture elements diff --git a/src/class-tiny-plugin.php b/src/class-tiny-plugin.php index 820da686..91420d19 100644 --- a/src/class-tiny-plugin.php +++ b/src/class-tiny-plugin.php @@ -32,7 +32,8 @@ public static function jpeg_quality() { } public static function version() { - /* Avoid using get_plugin_data() because it is not loaded early enough + /* + Avoid using get_plugin_data() because it is not loaded early enough in xmlrpc.php. */ return self::VERSION; } @@ -111,7 +112,8 @@ public function ajax_init() { $this->get_method( 'mark_image_as_compressed' ) ); - /* When touching any functionality linked to image compressions when + /* + When touching any functionality linked to image compressions when uploading images make sure it also works with XML-RPC. See README. */ add_filter( 'wp_ajax_nopriv_tiny_rpc', @@ -788,7 +790,8 @@ public function add_dashboard_widget() { true ); - /* This might be deduplicated with the admin script localization, but + /* + This might be deduplicated with the admin script localization, but the order of including scripts is sometimes different. So in that case we need to make sure that the order of inclusion is correc.t */ wp_localize_script( @@ -850,6 +853,7 @@ public function friendly_user_name() { * Will clean up converted files (if any) when the original is deleted * * Hooked to the `delete_attachment` action. + * * @see https://developer.wordpress.org/reference/hooks/deleted_post/ * * @param [int] $post_id diff --git a/src/class-tiny-settings.php b/src/class-tiny-settings.php index d19e8765..281a2a00 100644 --- a/src/class-tiny-settings.php +++ b/src/class-tiny-settings.php @@ -48,6 +48,7 @@ public function xmlrpc_init() { try { $this->init_compressor(); } catch ( Tiny_Exception $e ) { + Tiny_Logger::error( $e->getMessage() ); } } @@ -55,6 +56,7 @@ public function cli_init() { try { $this->init_compressor(); } catch ( Tiny_Exception $e ) { + Tiny_Logger::error( $e->getMessage() ); } } @@ -62,6 +64,7 @@ public function ajax_init() { try { $this->init_compressor(); } catch ( Tiny_Exception $e ) { + Tiny_Logger::error( $e->getMessage() ); } add_action( @@ -89,6 +92,7 @@ public function rest_init() { try { $this->init_compressor(); } catch ( Tiny_Exception $e ) { + Tiny_Logger::error( $e->getMessage() ); } } @@ -98,7 +102,7 @@ public function admin_init() { } catch ( Tiny_Exception $e ) { $this->notices->show( 'compressor_exception', - esc_html( $e->getMessage(), 'tiny-compress-images' ), + esc_html( $e->getMessage() ), 'error', false ); @@ -221,7 +225,8 @@ protected function clear_api_key_pending() { } protected static function get_intermediate_size( $size ) { - /* Inspired by + /* + Inspired by http://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes */ global $_wp_additional_image_sizes; @@ -378,7 +383,7 @@ public function get_resize_options( $size_name ) { if ( $height > 0 ) { $options['height'] = $height; } - return sizeof( $options ) >= 2 ? $options : false; + return count( $options ) >= 2 ? $options : false; } /** @@ -686,8 +691,8 @@ public function render_compression_timing_radiobutton( } $id = sprintf( self::get_prefixed_name( 'compression_timing_%s' ), $value ); - $label = esc_html( $label, 'tiny-compress-images' ); - $desc = esc_html( $desc, 'tiny-compress-images' ); + $label = esc_html( $label ); + $desc = esc_html( $desc ); echo ''; echo ''; @@ -789,14 +794,12 @@ public function render_account_status() { if ( $this->get_api_key_pending() ) { $this->clear_api_key_pending(); } - } else { - if ( $this->get_api_key_pending() ) { + } elseif ( $this->get_api_key_pending() ) { $status->ok = true; $status->pending = true; $status->message = ( 'An email has been sent to activate your account' ); - } } include __DIR__ . '/views/account-status-connected.php'; } diff --git a/src/compatibility/as3cf/class-tiny-as3cf.php b/src/compatibility/as3cf/class-tiny-as3cf.php index 314f65e3..ca5af84a 100644 --- a/src/compatibility/as3cf/class-tiny-as3cf.php +++ b/src/compatibility/as3cf/class-tiny-as3cf.php @@ -44,11 +44,11 @@ public function __construct( $settings ) { * Will verify if either the Lite or Pro version of AS3CF is active. */ public static function is_active() { - return Tiny_AS3CF::pro_is_active() || Tiny_AS3CF::lite_is_active(); + return self::pro_is_active() || self::lite_is_active(); } public static function remove_local_files_setting_enabled() { - if ( ! Tiny_AS3CF::is_active() ) { + if ( ! self::is_active() ) { return false; } $settings = get_option( 'tantan_wordpress_s3' ); @@ -62,7 +62,7 @@ public static function remove_local_files_setting_enabled() { * Registers hooks required for the AS3CF integration. */ public function add_hooks() { - add_action( 'as3cf_pre_upload_object', array( $this, 'as3cf_before_offload' ), 10, 2 ); + add_action( 'as3cf_pre_upload_object', array( $this, 'as3cf_before_offload' ), 10, 1 ); } /** @@ -72,10 +72,9 @@ public function add_hooks() { * * Will handle file before file is possibly offloaded * - * @param Item $as3cf_item - * @param array $args + * @param mixed $as3cf_item */ - public function as3cf_before_offload( $as3cf_item, $args ) { + public function as3cf_before_offload( $as3cf_item ) { if ( ! $this->tiny_settings->auto_compress_enabled() ) { return; } diff --git a/src/views/account-status-connected.php b/src/views/account-status-connected.php index 4f8b1bff..085b1dca 100644 --- a/src/views/account-status-connected.php +++ b/src/views/account-status-connected.php @@ -4,7 +4,7 @@ ok ) { if ( isset( $status->message ) ) { - echo esc_html( $status->message, 'tiny-compress-images' ); + echo esc_html( $status->message ); } else { esc_html_e( 'Your account is connected', 'tiny-compress-images' ); } @@ -51,23 +51,21 @@ $compressions ); } - } else { - if ( isset( $status->message ) ) { + } elseif ( isset( $status->message ) ) { echo esc_html__( 'Error', 'tiny-compress-images' ) . ': '; - echo esc_html( $status->message, 'tiny-compress-images' ); - } else { - esc_html_e( - 'API status could not be checked, enable cURL for more information', - 'tiny-compress-images' - ); - } + echo esc_html( $status->message ); + } else { + esc_html_e( + 'API status could not be checked, enable cURL for more information', + 'tiny-compress-images' + ); } // End if(). ?>

- 0 && sizeof( $active_tinify_sizes ) > 0 && $stats['available-unoptimized-sizes'] > 0 ) { ?> + 0 && count( $active_tinify_sizes ) > 0 && $stats['available-unoptimized-sizes'] > 0 ) { ?>

'; - for ( $i = 0; $i < sizeof( $active_tinify_sizes ); ++$i ) { + for ( $i = 0; $i < count( $active_tinify_sizes ); ++$i ) { $name = $active_tinify_sizes[ $i ]; if ( '0' == $name ) { echo '

  • - ' . esc_html__( 'Original image', 'tiny-compress-images' ) . '
  • '; @@ -150,7 +150,7 @@ ?>

    - 0 ) { ?> + 0 ) { ?> - +
    @@ -166,7 +166,7 @@ echo ''; echo ''; echo ( Tiny_Image::is_original( $size_name ) ? esc_html__( 'Original', 'tiny-compress-images' ) : esc_html( ucfirst( rtrim( $size_name, '_wr2x' ) ) ) ); - echo '' . ' '; + echo ''; if ( ! array_key_exists( $size_name, $active_sizes ) && ! Tiny_Image::is_retina( $size_name ) ) { echo '' . esc_html__( '(not in use)', 'tiny-compress-images' ) . ''; } elseif ( $size->missing() && ( Tiny_Settings::wr2x_active() || ! Tiny_Image::is_retina( $size_name ) ) ) { diff --git a/src/views/optimization-chart.php b/src/views/optimization-chart.php index 28e3dca3..4013b5f3 100644 --- a/src/views/optimization-chart.php +++ b/src/views/optimization-chart.php @@ -33,7 +33,7 @@ @keyframes shwoosh { from { - stroke-dasharray: + stroke-dasharray: } to { stroke-dasharray: