Make WordPress Core


Ignore:
Timestamp:
12/02/2012 04:06:31 PM (13 years ago)
Author:
nacin
Message:

Allow the 'Uploaded to this post' view to be sorted, saving the resulting order as menu_order.

This functionality is designed to be backwards compatible with manual querying for attachments by menu_order.

props koopersmith.
see #22607.

File:
1 edited

Legend:

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

    r22954 r22967  
    19281928}
    19291929
     1930function wp_ajax_save_attachment_order() {
     1931    if ( ! isset( $_REQUEST['post_id'] ) )
     1932        wp_send_json_error();
     1933
     1934    if ( ! $post_id = absint( $_REQUEST['post_id'] ) )
     1935        wp_send_json_error();
     1936
     1937    if ( empty( $_REQUEST['attachments'] ) )
     1938        wp_send_json_error();
     1939
     1940    check_ajax_referer( 'update-post_' . $post_id, 'nonce' );
     1941
     1942    $attachments = $_REQUEST['attachments'];
     1943
     1944    if ( ! current_user_can( 'edit_post', $post_id ) )
     1945        wp_send_json_error();
     1946
     1947    $post = get_post( $post_id, ARRAY_A );
     1948
     1949    foreach ( $attachments as $attachment_id => $menu_order ) {
     1950        if ( ! current_user_can( 'edit_post', $attachment_id ) )
     1951            continue;
     1952        if ( ! $attachment = get_post( $attachment_id ) )
     1953            continue;
     1954        if ( 'attachment' != $attachment->post_type )
     1955            continue;
     1956
     1957        wp_update_post( array( 'ID' => $attachment_id, 'menu_order' => $menu_order ) );
     1958    }
     1959
     1960    wp_send_json_success();
     1961}
     1962
    19301963/**
    19311964 * Generates the HTML to send an attachment to the editor.
Note: See TracChangeset for help on using the changeset viewer.