From 215be466c31c7d40310da85afda60b839cd1e2b5 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Fri, 31 Aug 2012 02:58:51 +0000 Subject: [PATCH] Add get-attachment and query-attachments Ajax handlers. props koopersmith. see #21390. git-svn-id: https://develop.svn.wordpress.org/trunk@21681 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/admin-ajax.php | 2 +- wp-admin/includes/ajax-actions.php | 45 +++++++++++++++++++++++++++++- wp-includes/functions.php | 2 +- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/wp-admin/admin-ajax.php b/wp-admin/admin-ajax.php index db44df78c9..394e5c33b0 100644 --- a/wp-admin/admin-ajax.php +++ b/wp-admin/admin-ajax.php @@ -50,7 +50,7 @@ $core_actions_post = array( 'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink', 'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order', 'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post', - 'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', + 'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment', 'query-attachments', ); // Register core Ajax calls. diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index 014dd56fce..043621febb 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -1425,7 +1425,7 @@ function wp_ajax_inline_save_tax() { while ( $parent > 0 ) { $parent_tag = get_term( $parent, $taxonomy ); $parent = $parent_tag->parent; - $level++; + $level++; } echo $wp_list_table->single_row( $tag, $level ); wp_die(); @@ -1801,3 +1801,46 @@ function wp_ajax_dismiss_wp_pointer() { update_user_meta( get_current_user_id(), 'dismissed_wp_pointers', $dismissed ); wp_die( 1 ); } + +/** + * Get an attachment. + * + * @since 3.5.0 + */ +function wp_ajax_get_attachment() { + if ( ! isset( $_REQUEST['id'] ) ) + wp_send_json_error(); + + if ( ! $id = absint( $_REQUEST['id'] ) ) + wp_send_json_error(); + + if ( ! current_user_can( 'read_post', $id ) ) + wp_send_json_error(); + + if ( ! $attachment = wp_prepare_attachment_for_js( $id ) ) + wp_send_json_error(); + + wp_send_json_success( $attachment ); +} + +/** + * Query for attachments. + * + * @since 3.5.0 + */ +function wp_ajax_query_attachments() { + $qvs = isset( $_REQUEST['query'] ) ? (array) $_REQUEST['query'] : array(); + $qvs = array_intersect_key( $qvs, array_flip( array( 's', 'order', 'orderby', 'posts_per_page', 'paged' ) ) ); + + $query['post_type'] = 'attachment'; + $query['post_status'] = 'inherit'; + if ( current_user_can( get_post_type_object( 'attachment' )->cap->read_private_posts ) ) + $query['post_status'] .= ',private'; + + $query = new WP_Query( $query ); + + $posts = array_map( 'wp_prepare_attachment_for_js', $query->posts ); + $posts = array_filter( $posts ); + + wp_send_json_success( $posts ); +} diff --git a/wp-includes/functions.php b/wp-includes/functions.php index e369fff3a5..91314b5aab 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -2151,7 +2151,7 @@ function _scalar_wp_die_handler( $message = '' ) { */ function wp_send_json( $response ) { @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); - echo json_encode( $json ); + echo json_encode( $response ); if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) wp_die(); else