Make WordPress Core

Changeset 21681


Ignore:
Timestamp:
08/31/2012 02:58:51 AM (12 years ago)
Author:
nacin
Message:

Add get-attachment and query-attachments Ajax handlers. props koopersmith. see #21390.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-ajax.php

    r21071 r21681  
    5151    'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
    5252    'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
    53     'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment',
     53    'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment', 'query-attachments',
    5454);
    5555
  • trunk/wp-admin/includes/ajax-actions.php

    r21597 r21681  
    14261426        $parent_tag = get_term( $parent, $taxonomy );
    14271427        $parent = $parent_tag->parent;
    1428         $level++; 
     1428        $level++;
    14291429    }
    14301430    echo $wp_list_table->single_row( $tag, $level );
     
    18021802    wp_die( 1 );
    18031803}
     1804
     1805/**
     1806 * Get an attachment.
     1807 *
     1808 * @since 3.5.0
     1809 */
     1810function wp_ajax_get_attachment() {
     1811    if ( ! isset( $_REQUEST['id'] ) )
     1812        wp_send_json_error();
     1813
     1814    if ( ! $id = absint( $_REQUEST['id'] ) )
     1815        wp_send_json_error();
     1816
     1817    if ( ! current_user_can( 'read_post', $id ) )
     1818        wp_send_json_error();
     1819
     1820    if ( ! $attachment = wp_prepare_attachment_for_js( $id ) )
     1821        wp_send_json_error();
     1822
     1823    wp_send_json_success( $attachment );
     1824}
     1825
     1826/**
     1827 * Query for attachments.
     1828 *
     1829 * @since 3.5.0
     1830 */
     1831function wp_ajax_query_attachments() {
     1832    $qvs = isset( $_REQUEST['query'] ) ? (array) $_REQUEST['query'] : array();
     1833    $qvs = array_intersect_key( $qvs, array_flip( array( 's', 'order', 'orderby', 'posts_per_page', 'paged' ) ) );
     1834
     1835    $query['post_type'] = 'attachment';
     1836    $query['post_status'] = 'inherit';
     1837    if ( current_user_can( get_post_type_object( 'attachment' )->cap->read_private_posts ) )
     1838        $query['post_status'] .= ',private';
     1839
     1840    $query = new WP_Query( $query );
     1841
     1842    $posts = array_map( 'wp_prepare_attachment_for_js', $query->posts );
     1843    $posts = array_filter( $posts );
     1844
     1845    wp_send_json_success( $posts );
     1846}
  • trunk/wp-includes/functions.php

    r21679 r21681  
    21522152function wp_send_json( $response ) {
    21532153    @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
    2154     echo json_encode( $json );
     2154    echo json_encode( $response );
    21552155    if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
    21562156        wp_die();
Note: See TracChangeset for help on using the changeset viewer.