Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.