Make WordPress Core

Ticket #22607: 22607.2.diff

File 22607.2.diff, 5.3 KB (added by koopersmith, 13 years ago)
  • wp-admin/admin-ajax.php

     
    5656        'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
    5757        'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment',
    5858        'query-attachments', 'save-attachment', 'save-attachment-compat', 'send-link-to-editor',
    59         'send-attachment-to-editor',
     59        'send-attachment-to-editor', 'save-attachment-order',
    6060);
    6161
    6262// Register core Ajax calls.
  • wp-admin/includes/ajax-actions.php

     
    19271927        wp_send_json_success( $attachment );
    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.
    19321965 * Backwards compatible with the media_send_to_editor filter and the chain
  • wp-includes/js/media-models.js

     
    523523                _requery: function() {
    524524                        if ( this.props.get('query') )
    525525                                this.mirror( Query.get( this.props.toJSON() ) );
     526                },
     527
     528                // If this collection is sorted by `menuOrder`, recalculates and saves
     529                // the menu order to the database.
     530                saveMenuOrder: function() {
     531                        if ( 'menuOrder' !== this.props.get('orderby') )
     532                                return;
     533
     534                        // Removes any uploading attachments, updates each attachment's
     535                        // menu order, and returns an object with an { id: menuOrder }
     536                        // mapping to pass to the request.
     537                        var attachments = this.chain().filter( function( attachment ) {
     538                                return ! _.isUndefined( attachment.id );
     539                        }).map( function( attachment, index ) {
     540                                // Indices start at 1.
     541                                index = index + 1;
     542                                attachment.set( 'menuOrder', index );
     543                                return [ attachment.id, index ];
     544                        }).object().value();
     545
     546                        if ( _.isEmpty( attachments ) )
     547                                return;
     548
     549                        return media.post( 'save-attachment-order', {
     550                                nonce:       media.model.settings.updatePostNonce,
     551                                post_id:     media.model.settings.postId,
     552                                attachments: attachments
     553                        });
    526554                }
    527555        }, {
    528556                comparator: function( a, b, options ) {
  • wp-includes/js/media-views.js

     
    1414
    1515        // Copy the `postId` setting over to the model settings.
    1616        media.model.settings.postId = media.view.settings.postId;
     17        media.model.settings.updatePostNonce = media.view.settings.nonce.updatePost;
    1718
    1819        // Check if the browser supports CSS 3.0 transitions
    1920        $.support.transition = (function(){
     
    267268                        content:    'browse',
    268269                        searchable: true,
    269270                        filterable: false,
    270                         uploads:    true
     271                        uploads:    true,
     272                        sortable:   true
    271273                },
    272274
    273275                initialize: function() {
     
    27682770                                                at:     ui.item.index(),
    27692771                                                silent: true
    27702772                                        });
     2773
     2774                                        // If the collection is sorted by menu order,
     2775                                        // update the menu order.
     2776                                        collection.saveMenuOrder();
    27712777                                }
    27722778                        });
    27732779
     
    27832789                                return;
    27842790
    27852791                        // If the `collection` has a `comparator`, disable sorting.
    2786                         this.$el.sortable( 'option', 'disabled', !! this.collection.comparator );
     2792                        var collection = this.collection,
     2793                                orderby = collection.props.get('orderby'),
     2794                                enabled = 'menuOrder' === orderby || ! collection.comparator;
     2795
     2796                        this.$el.sortable( 'option', 'disabled', ! enabled );
    27872797                },
    27882798
    27892799                createAttachmentView: function( attachment ) {
     
    30493059                                }).render() );
    30503060                        }
    30513061
    3052                         if ( this.options.sortable ) {
     3062                        if ( this.options.sortable && ! this.options.filters ) {
    30533063                                this.toolbar.set( 'dragInfo', new media.View({
    30543064                                        el: $( '<div class="instructions">' + l10n.dragInfo + '</div>' )[0],
    30553065                                        priority: -40
  • wp-includes/media.php

     
    14331433        if ( isset( $args['post'] ) ) {
    14341434                $post = get_post( $args['post'] );
    14351435                $settings['postId'] = $post->ID;
     1436                $settings['nonce']['updatePost'] = wp_create_nonce( 'update-post_' . $post->ID );
    14361437        }
    14371438
    14381439        $hier = $post && is_post_type_hierarchical( $post->post_type );