Make WordPress Core

Ticket #22744: 22744.3.diff

File 22744.3.diff, 1.8 KB (added by DrewAPicture, 10 years ago)
  • src/wp-admin/includes/post.php

     
    10111011}
    10121012
    10131013/**
     1014 * Allow 'meta_query' to persist during wp().
     1015 *
     1016 * @since 4.0.0
     1017 * @access private
     1018 *
     1019 * @param array $q Query vars constructed during WP->parse_request().
     1020 * @return array The altered query vars.
     1021 */
     1022function _wp_filter_attachment_search( $q ) {
     1023        remove_filter( 'request', __FUNCTION__ );
     1024
     1025        $q['meta_query'] = array(
     1026                'relation' => 'OR',
     1027                array(
     1028                        'key' => '_wp_attached_file',
     1029                        'value' => $q['s'],
     1030                        'compare' => 'LIKE'
     1031                )
     1032        );
     1033        return $q;
     1034}
     1035
     1036/**
     1037 * Filter meta SQL to mark this search criteria as optional.
     1038 *
     1039 * @since 4.0.0
     1040 * @access private
     1041 *
     1042 * @param array $sql WHERE and JOIN SQL clauses.
     1043 * @return array The filtered clauses.
     1044 */
     1045function _wp_filter_attachment_meta_sql( $sql ) {
     1046        remove_filter( 'get_meta_sql', __FUNCTION__ );
     1047
     1048        /*
     1049         * Split AND-operated WHERE sub-clauses into an OR-operated clause group.
     1050         * This serves to mark the meta search criteria optional as a group.
     1051         */
     1052        if ( 0 === strpos( $sql['where'], ' AND (' ) ) {
     1053                list( $_, $clauses ) = explode( ' AND ', $sql['where'], 2 );
     1054                $sql['where'] = " OR ({$clauses})";
     1055        }
     1056        return $sql;
     1057}
     1058
     1059/**
    10141060 * Executes a query for attachments. An array of WP_Query arguments
    10151061 * can be passed in, which will override the arguments set by this function.
    10161062 *
     
    10541100        if ( isset($q['detached']) )
    10551101                $q['post_parent'] = 0;
    10561102
     1103        if ( ! empty( $q['s'] ) ) {
     1104                add_filter( 'request', '_wp_filter_attachment_search' );
     1105                add_filter( 'get_meta_sql', '_wp_filter_attachment_meta_sql' );
     1106        }
     1107
    10571108        wp( $q );
    10581109
    10591110        return array($post_mime_types, $avail_post_mime_types);