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

git-svn-id: http://core.svn.wordpress.org/trunk@21681 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2012-08-31 02:58:51 +00:00
parent 5546df59d3
commit 75052955cc
3 changed files with 46 additions and 3 deletions

View File

@ -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.

View File

@ -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 );
}

View File

@ -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