Skip to content

Commit

Permalink
Fixed regression with sql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal committed Jan 22, 2025
1 parent 72a3ea0 commit fffaa9d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
17 changes: 6 additions & 11 deletions inc/classes/class-imagify-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,33 +236,28 @@ public static function get_required_wp_metadata_exist_clause( $id_field = 'p.ID'
'test' => false,
) );

$first = true;
$initial_clause = '';
$first = true;

foreach ( self::get_required_wp_metadata_aliases() as $meta_name => $alias ) {
if ( $first ) {
$first = false;
$initial_clause = "
OR NOT EXISTS(
SELECT 1 FROM $wpdb->postmeta AS $alias WHERE
$alias.post_id = $id_field AND $alias.meta_key = '$meta_name' $special_join_conditions
)
OR EXISTS(
$clause .= "
EXISTS(
SELECT 1 FROM $wpdb->postmeta AS $alias WHERE
$alias.post_id = $id_field AND $alias.meta_key = '$meta_name' $special_join_conditions
$alias.post_id = $id_field AND $alias.meta_key = '$meta_name'
$additional_clause
)
";
continue;
}

$clause .= "
NOT EXISTS (
OR NOT EXISTS (
SELECT 1 FROM $wpdb->postmeta AS $alias WHERE
$alias.post_id = $id_field AND $alias.meta_key = '$meta_name')";
}

return "AND( $clause $initial_clause )";
return "AND( $clause )";
}

/**
Expand Down
27 changes: 20 additions & 7 deletions inc/functions/admin-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ 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('p.ID', true, true,
"AND p.post_mime_type IN ( $mime_types )
$nodata_join = '';
$nodata_where = '';
if ( ! imagify_has_attachments_without_required_metadata() ) {
$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();
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
}
$count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
"
SELECT COUNT( p.ID )
Expand Down Expand Up @@ -91,8 +95,12 @@ function imagify_count_error_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_where = Imagify_DB::get_required_wp_metadata_where_clause();
$nodata_join = '';
$nodata_where = '';
if ( ! imagify_has_attachments_without_required_metadata() ) {
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
}
$count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
"
SELECT COUNT(*)
Expand Down Expand Up @@ -145,8 +153,13 @@ function imagify_count_optimized_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_where = Imagify_DB::get_required_wp_metadata_where_clause();
$nodata_join = '';
$nodata_where = '';
if ( ! imagify_has_attachments_without_required_metadata() ) {
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
}

$count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
"
SELECT COUNT( DISTINCT p.ID )
Expand Down

0 comments on commit fffaa9d

Please sign in to comment.