Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(media-credit): refactor clauses for credit search #2691

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 12 additions & 104 deletions includes/class-newspack-image-credits.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@ class Newspack_Image_Credits {
*/
const MEDIA_CREDIT_CAN_DISTRIBUTE_META = '_navis_media_can_distribute';

/**
* The SQL generated by the WP_Meta_Query object when we are doing a search via the wp admin ajax call.
*
* @var ?array
*/
private static $meta_search_sql;

/**
* The term being searched for in the wp admin ajax call.
*
* @var ?string
*/
private static $search_string;

/**
* Hook actions and filters.
*/
Expand Down Expand Up @@ -459,105 +445,27 @@ public static function filter_ajax_query_attachments( $query ) {
if ( empty( $query['s'] ) ) {
return $query;
}

global $wpdb;

$meta_query = new WP_Meta_Query(
[
'relation' => 'OR',
[
'key' => self::MEDIA_CREDIT_META,
'value' => $query['s'],
'compare' => 'LIKE',
],
[
'key' => self::MEDIA_CREDIT_URL_META,
'value' => $query['s'],
'compare' => 'LIKE',
],
[
'key' => self::MEDIA_CREDIT_ORG_META,
'value' => $query['s'],
'compare' => 'LIKE',
],
]
);

self::$meta_search_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' );
self::$search_string = $query['s'];

add_filter( 'posts_search', [ __CLASS__, 'filter_posts_search' ], 10, 2 );
add_filter( 'posts_join', [ __CLASS__, 'filter_posts_join' ], 10, 2 );
add_filter( 'posts_distinct', [ __CLASS__, 'filter_posts_distinct' ], 10, 2 );

add_filter( 'posts_clauses', [ __CLASS__, 'filter_posts_clauses' ], 10, 2 );
return $query;
}

/**
* Filters the posts join clause to include the relationthip with post_meta
* Filter posts query clauses to include media credit meta search.
*
* @param string $sql The current join SQL.
* @param array $clauses The current query clauses.
* @param \WP_Query $query The current WP_Query object.
* @return string
*/
public static function filter_posts_join( $sql, $query ) {

if (
empty( self::$meta_search_sql ) ||
empty( self::$search_string ) ||
! $query->is_search() ||
'attachment' !== $query->query['post_type'] ||
empty( $query->query['s'] ) ||
self::$search_string !== $query->query['s']
) {
return $sql;
}
return $sql . self::$meta_search_sql['join'] . ' ';
}

/**
* Filters the search SQL to include posts with matching image credits.
*
* @param string $search_sql The current search SQL.
* @param \WP_Query $query The current WP_Query object.
* @return string
*/
public static function filter_posts_search( $search_sql, $query ) {
if (
! empty( self::$meta_search_sql ) &&
$query->is_search() &&
'attachment' === $query->query['post_type'] &&
self::$search_string === $query->query['s'] &&
' AND ((' === substr( $search_sql, 0, 7 ) &&
' AND (' === substr( self::$meta_search_sql['where'], 0, 6 )
) {
// removes ' AND (' from the beginning and ')' from the end.
$meta_sql = substr( self::$meta_search_sql['where'], 6, strlen( self::$meta_search_sql['where'] ) - 7 );
$search_sql = ' AND ((' . $meta_sql . ' OR ' . substr( $search_sql, 7 );
}
return $search_sql;
}

/**
* Filters the search SQL to include posts with matching image credits.
*
* @param string $distinct The distinct string for the current query.
* @param \WP_Query $query The current WP_Query object.
* @return string
* @return array
*/
public static function filter_posts_distinct( $distinct, $query ) {
if (
empty( self::$meta_search_sql ) ||
empty( self::$search_string ) ||
! $query->is_search() ||
'attachment' !== $query->query['post_type'] ||
empty( $query->query['s'] ) ||
self::$search_string !== $query->query['s']
) {
return $distinct;
public static function filter_posts_clauses( $clauses, $query ) {
if ( empty( $query->get( 's' ) ) ) {
return $clauses;
}
return 'DISTINCT';
global $wpdb;
$clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS media_credit_meta ON {$wpdb->posts}.ID = media_credit_meta.post_id";
$clauses['where'] .= " OR ( media_credit_meta.meta_key IN ( '_media_credit', '_media_credit_url', '_navis_media_credit_org' ) AND media_credit_meta.meta_value LIKE '%" . esc_sql( $query->get( 's' ) ) . "%' )";
$clauses['groupby'] = "{$wpdb->posts}.ID";
return $clauses;
}

}
Newspack_Image_Credits::init();