d AND post_meta_ignore.meta_key= %s WHERE post_meta_ignore.meta_value IS NULL AND post_meta_error.meta_key = %s ORDER BY post_meta_error.post_id DESC LIMIT %d;", Media_Item::IGNORED_META_KEY, Media_Item_Optimizer::ERROR_META_KEY, $limit ); /** * Due to performance, we do not join with table wp_posts to exclude the deleted attachments, * leave a filter for third-party custom it. */ $query = apply_filters( 'wp_smush_query_get_last_optimize_errors', $query ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared return $wpdb->get_col( $query ); } private static function get_last_media_item_errors( $limit ) { $error_list_ids = Global_Stats::get()->get_error_list()->get_ids(); $last_errors = array(); if ( empty( $error_list_ids ) ) { return $last_errors; } foreach( $error_list_ids as $attachment_id ) { $media_item = Media_Item_Cache::get_instance()->get( $attachment_id ); if ( ! $media_item->has_errors() ) { continue; } $last_errors[ $attachment_id ] = self::get_error( $media_item->get_errors(), $media_item ); if ( count( $last_errors ) >= $limit ) { break; } } return $last_errors; } private static function get_last_optimize_errors( $limit ) { $last_errors_ids = self::get_last_optimization_error_ids( $limit ); $last_errors = array(); if ( empty( $last_errors_ids ) ) { return $last_errors; } foreach( $last_errors_ids as $attachment_id ) { $media_item = Media_Item_Cache::get_instance()->get( $attachment_id ); $optimizer = new Media_Item_Optimizer($media_item); if( ! $optimizer->has_errors() ) { continue; } $last_errors[ $attachment_id ] = self::get_error( $optimizer->get_errors(), $media_item ); if ( count( $last_errors ) >= $limit ) { break; } } return $last_errors; } /** * Get latest errors. * * @param int $limit Limit number of errors to return. * @return array */ public static function get_last_errors( $limit = 10 ) { $last_errors = self::get_last_media_item_errors( $limit ); $no_item_errors = count( $last_errors ); if ( $no_item_errors >= $limit ) { return $last_errors; } $optimize_errors = self::get_last_optimize_errors( $limit - $no_item_errors ); return $last_errors + $optimize_errors; } /** * @return array */ public static function get_error( WP_Error $errors, Media_Item $media_item ) { $thumbnail = $media_item->get_size('thumbnail' ); $media_item_size = $media_item->get_scaled_or_full_size(); return array( 'error_code' => $errors->get_error_code(), 'error_message' => $errors->get_error_message(), 'file_name' => $media_item_size ? $media_item_size->get_file_name() : '', 'thumbnail' => $thumbnail ? $thumbnail->get_file_url() : false, ); } /** * Get error messages. * * @return array */ private static function get_default_error_messages() { return apply_filters( 'wp_smush_error_messages', array( 'missing_id' => esc_html__( 'No attachment ID was received.', 'wp-smushit' ), 'ignored' => esc_html__( 'Skip ignored file.', 'wp-smushit' ), 'animated' => esc_html__( 'Skipped animated file.', 'wp-smushit' ), 'in_progress' => esc_html__( 'File processing is in progress.', 'wp-smushit' ), 'no_file_meta' => esc_html__( 'No file data found in image meta', 'wp-smushit' ), 'skipped_filter' => esc_html__( 'Skipped with wp_smush_image filter', 'wp-smushit' ), 'empty_path' => esc_html__( 'File path is empty', 'wp-smushit' ), 'empty_response' => esc_html__( 'Webp no response was received.', 'wp-smushit' ), 'not_processed' => esc_html__( 'Not processed', 'wp-smushit' ), /* translators: %s: image size */ 'size_limit' => __( 'Skipped (%s), file size limit of 5mb exceeded', 'wp-smushit' ), /* translators: %s: image size */ 'size_pro_limit' => __( 'Skipped (%s), file size limit of 256mb exceeded', 'wp-smushit' ), /* translators: %s: Directory path */ 'not_writable' => __( '%s is not writable', 'wp-smushit' ), /* translators: %s: File path */ 'file_not_found' => __( 'Skipped (%s), File not found.', 'wp-smushit' ), ) ); } }