diff --git a/inc/classes/class-imagify-db.php b/inc/classes/class-imagify-db.php index 6a12cd066..b457c2145 100644 --- a/inc/classes/class-imagify-db.php +++ b/inc/classes/class-imagify-db.php @@ -159,18 +159,20 @@ public static function get_post_statuses() { * It returns an empty string if the database has no attachments without the required metadada. * It also triggers Imagify_DB::unlimit_joins(). * - * @since 1.7 - * @access public - * @author Grégory Viguier - * - * @param string $id_field An ID field to match the metadata ID against in the JOIN clause. + * @param string $id_field An ID field to match the metadata ID against in the JOIN clause. * Default is the posts table `ID` field, using the `p` alias: `p.ID`. * In case of "false" value or PEBKAC, fallback to the same field without alias. - * @param bool $matching Set to false to get a query to fetch metas NOT matching the file extensions. - * @param bool $test Test if the site has attachments without required metadata before returning the query. False to bypass the test and get the query anyway. + * @param bool $matching Set to false to get a query to fetch metas NOT matching the file extensions. + * @param bool $test Test if the site has attachments without required metadata before returning the query. False to bypass the test and get the query anyway. + * @param string $special_join_conditions Special conditions to apply on the join. + * * @return string + * @author Grégory Viguier + * + * @since 1.7 + * @access public */ - public static function get_required_wp_metadata_join_clause( $id_field = 'p.ID', $matching = true, $test = true ) { + public static function get_required_wp_metadata_join_clause( $id_field = 'p.ID', $matching = true, $test = true, $special_join_conditions = '' ) { global $wpdb; if ( $test && ! imagify_has_attachments_without_required_metadata() ) { @@ -186,7 +188,16 @@ public static function get_required_wp_metadata_join_clause( $id_field = 'p.ID', $join = $matching ? 'INNER' : 'LEFT'; + $first = true; + foreach ( self::get_required_wp_metadata_aliases() as $meta_name => $alias ) { + if ( $first ) { + $first = false; + $clause .= " + $join JOIN $wpdb->postmeta AS $alias + ON ( $id_field = $alias.post_id AND $alias.meta_key = '$meta_name' $special_join_conditions )"; + continue; + } $clause .= " $join JOIN $wpdb->postmeta AS $alias ON ( $id_field = $alias.post_id AND $alias.meta_key = '$meta_name' )"; @@ -299,6 +310,9 @@ public static function get_extensions_where_clause( $args = false ) { $extensions = array_keys( imagify_get_mime_types() ); $extensions = implode( '|', $extensions ); $extensions = explode( '|', $extensions ); + $extensions = array_map(function ( $ex ) { + return strrev( $ex ); + }, $extensions); } if ( ! $alias ) { @@ -312,10 +326,12 @@ public static function get_extensions_where_clause( $args = false ) { return $prepared ? str_replace( '%', '%%', $query[ $key ] ) : $query[ $key ]; } + $regex = '^' . implode( '\..*|^', $extensions ) . '\..*'; + if ( $matching ) { - $query[ $key ] = "AND ( LOWER( $alias.meta_value ) LIKE '%." . implode( "' OR LOWER( $alias.meta_value ) LIKE '%.", $extensions ) . "' )"; + $query[ $key ] = "AND REVERSE (LOWER( $alias.meta_value )) REGEXP '$regex'"; } else { - $query[ $key ] = "OR ( LOWER( $alias.meta_value ) NOT LIKE '%." . implode( "' AND LOWER( $alias.meta_value ) NOT LIKE '%.", $extensions ) . "' )"; + $query[ $key ] = "AND REVERSE (LOWER( $alias.meta_value )) NOT REGEXP '$regex'"; } return $prepared ? str_replace( '%', '%%', $query[ $key ] ) : $query[ $key ]; diff --git a/inc/functions/admin-stats.php b/inc/functions/admin-stats.php index 95f1bb69d..84d534de9 100755 --- a/inc/functions/admin-stats.php +++ b/inc/functions/admin-stats.php @@ -33,7 +33,10 @@ function imagify_count_attachments() { $mime_types = Imagify_DB::get_mime_types(); $statuses = Imagify_DB::get_post_statuses(); - $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause(); + $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause('p.ID', true, true, + "AND p.post_mime_type IN ( $mime_types ) + AND p.post_type = 'attachment' + AND p.post_status IN ( $statuses )"); $nodata_where = Imagify_DB::get_required_wp_metadata_where_clause(); $count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok. " diff --git a/inc/functions/attachments.php b/inc/functions/attachments.php index f24999cc4..af361d74a 100755 --- a/inc/functions/attachments.php +++ b/inc/functions/attachments.php @@ -115,7 +115,14 @@ function imagify_has_attachments_without_required_metadata() { $mime_types = Imagify_DB::get_mime_types(); $statuses = Imagify_DB::get_post_statuses(); - $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause( 'p.ID', false, false ); + $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause( + 'p.ID', + false, + false, + "AND p.post_mime_type IN ( $mime_types ) + AND p.post_type = 'attachment' + AND p.post_status IN ( $statuses )" + ); $nodata_where = Imagify_DB::get_required_wp_metadata_where_clause( array( 'matching' => false, 'test' => false,