Making WordPress.org

Changeset 7521


Ignore:
Timestamp:
07/30/2018 03:41:33 AM (8 years ago)
Author:
obenland
Message:

Gutenberg: Make ajax handler available to logged out users.

See #3703.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/gutenberg/functions.php

    r7520 r7521  
    6666        }
    6767
    68 
    6968        // Disable use XML-RPC
    7069        add_filter( 'xmlrpc_enabled', '__return_false' );
     
    7776        }
    7877        add_filter( 'wp_headers', 'disable_x_pingback' );
     78});
     79
     80/**
     81 * Ajax handler for querying attachments for logged-out users.
     82 *
     83 * @since 3.5.0
     84 */
     85function frontenberg_wp_ajax_nopriv_query_attachments() {
     86        if ( 97589 !== absint( $_REQUEST['post_id'] ) ) {
     87                wp_send_json_error();
     88        }
     89        $query = isset( $_REQUEST['query'] ) ? (array) $_REQUEST['query'] : array();
     90        $keys = array(
     91                's', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type',
     92                'post_parent', 'post__in', 'post__not_in', 'year', 'monthnum'
     93        );
     94        foreach ( get_taxonomies_for_attachments( 'objects' ) as $t ) {
     95                if ( $t->query_var && isset( $query[ $t->query_var ] ) ) {
     96                        $keys[] = $t->query_var;
     97                }
     98        }
     99
     100        $query = array_intersect_key( $query, array_flip( $keys ) );
     101        $query['post_type'] = 'attachment';
     102        if ( MEDIA_TRASH
     103                && ! empty( $_REQUEST['query']['post_status'] )
     104                && 'trash' === $_REQUEST['query']['post_status'] ) {
     105                $query['post_status'] = 'trash';
     106        } else {
     107                $query['post_status'] = 'inherit';
     108        }
     109
     110        // Filter query clauses to include filenames.
     111        if ( isset( $query['s'] ) ) {
     112                add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     113        }
    79114
    80115        /**
    81          * Ajax handler for querying attachments.
     116         * Filters the arguments passed to WP_Query during an Ajax
     117         * call for querying attachments.
    82118         *
    83          * @since 3.5.0
     119         * @since 3.7.0
     120         *
     121         * @see WP_Query::parse_query()
     122         *
     123         * @param array $query An array of query variables.
    84124         */
    85         function frontenberg_wp_ajax_nopriv_query_attachments() {
    86                 $query = isset( $_REQUEST['query'] ) ? (array) $_REQUEST['query'] : array();
    87                 $keys = array(
    88                         's', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type',
    89                         'post_parent', 'post__in', 'post__not_in', 'year', 'monthnum'
    90                 );
    91                 foreach ( get_taxonomies_for_attachments( 'objects' ) as $t ) {
    92                         if ( $t->query_var && isset( $query[ $t->query_var ] ) ) {
    93                                 $keys[] = $t->query_var;
    94                         }
    95                 }
    96 
    97                 $query = array_intersect_key( $query, array_flip( $keys ) );
    98                 $query['post_type'] = 'attachment';
    99                 if ( MEDIA_TRASH
    100                         && ! empty( $_REQUEST['query']['post_status'] )
    101                         && 'trash' === $_REQUEST['query']['post_status'] ) {
    102                         $query['post_status'] = 'trash';
    103                 } else {
    104                         $query['post_status'] = 'inherit';
    105                 }
    106 
    107                 // Filter query clauses to include filenames.
    108                 if ( isset( $query['s'] ) ) {
    109                         add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
    110                 }
    111 
    112                 /**
    113                  * Filters the arguments passed to WP_Query during an Ajax
    114                  * call for querying attachments.
    115                  *
    116                  * @since 3.7.0
    117                  *
    118                  * @see WP_Query::parse_query()
    119                  *
    120                  * @param array $query An array of query variables.
    121                  */
    122                 $query = apply_filters( 'ajax_query_attachments_args', $query );
    123                 $query = new WP_Query( $query );
    124 
    125                 $posts = array_map( 'wp_prepare_attachment_for_js', $query->posts );
    126                 $posts = array_filter( $posts );
    127 
    128                 wp_send_json_success( $posts );
    129         }
    130         add_action( 'wp_ajax_nopriv_query-attachments', 'frontenberg_wp_ajax_nopriv_query_attachments' );
    131 });
    132 
    133 
     125        $query = apply_filters( 'ajax_query_attachments_args', $query );
     126        $query = new WP_Query( $query );
     127
     128        $posts = array_map( 'wp_prepare_attachment_for_js', $query->posts );
     129        $posts = array_filter( $posts );
     130
     131        wp_send_json_success( $posts );
     132}
     133add_action( 'wp_ajax_nopriv_query-attachments', 'frontenberg_wp_ajax_nopriv_query_attachments' );
    134134
    135135if ( ! function_exists( 'gutenbergtheme_setup' ) ) :
Note: See TracChangeset for help on using the changeset viewer.