Make WordPress Core

Changeset 22541


Ignore:
Timestamp:
11/11/2012 01:26:42 AM (12 years ago)
Author:
koopersmith
Message:

Media: Add backwards compatibility for attachment_fields_to_edit and attachment_fields_to_save. see #22186.

Location:
trunk
Files:
7 edited

Legend:

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

    r22173 r22541  
    5555    'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
    5656    'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment',
    57     'query-attachments', 'save-attachment',
     57    'query-attachments', 'save-attachment', 'save-attachment-compat',
    5858);
    5959
  • trunk/wp-admin/includes/ajax-actions.php

    r22532 r22541  
    18511851
    18521852    $changes = $_REQUEST['changes'];
    1853     $args    = array();
    1854 
    1855     if ( ! empty( $changes['title'] ) )
    1856         $args['post_title'] = $changes['title'];
    1857 
    1858     if ( ! empty( $changes['caption'] ) )
    1859         $args['post_excerpt'] = $changes['caption'];
    1860 
    1861     if ( ! empty( $changes['alt'] ) )
    1862         $args['_wp_attachment_image_alt'] = $changes['alt'];
    1863 
    1864     if ( $args )
    1865         edit_post( array_merge( $args, array( 'post_ID' => $id ) ) );
    1866 
     1853    $post    = get_post( $id, ARRAY_A );
     1854
     1855    if ( 'attachment' != $post['post_type'] )
     1856        wp_send_json_error();
     1857
     1858    if ( isset( $changes['title'] ) )
     1859        $post['post_title'] = $changes['title'];
     1860
     1861    if ( isset( $changes['caption'] ) )
     1862        $post['post_excerpt'] = $changes['caption'];
     1863
     1864    if ( isset( $changes['alt'] ) ) {
     1865        $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
     1866        $new_alt = stripslashes( $changes['alt'] );
     1867        if ( $alt != $new_alt ) {
     1868            $new_alt = wp_strip_all_tags( $new_alt, true );
     1869            update_post_meta( $id, '_wp_attachment_image_alt', addslashes( $new_alt ) );
     1870        }
     1871    }
     1872
     1873    wp_update_post( $post );
    18671874    wp_send_json_success();
    18681875}
     1876
     1877/**
     1878 * Save backwards compatible attachment attributes.
     1879 *
     1880 * @since 3.5.0
     1881 */
     1882function wp_ajax_save_attachment_compat() {
     1883    if ( ! isset( $_REQUEST['id'] ) )
     1884        wp_send_json_error();
     1885
     1886    if ( ! $id = absint( $_REQUEST['id'] ) )
     1887        wp_send_json_error();
     1888
     1889    if ( empty( $_REQUEST['attachments'] ) || empty( $_REQUEST['attachments'][ $id ] ) )
     1890        wp_send_json_error();
     1891    $attachment_data = $_REQUEST['attachments'][ $id ];
     1892
     1893    check_ajax_referer( 'save-attachment', 'nonce' );
     1894
     1895    if ( ! current_user_can( 'edit_post', $id ) )
     1896        wp_send_json_error();
     1897
     1898    $post = get_post( $id, ARRAY_A );
     1899
     1900    if ( 'attachment' != $post['post_type'] )
     1901        wp_send_json_error();
     1902
     1903    $post = apply_filters( 'attachment_fields_to_save', $post, $attachment_data );
     1904
     1905    if ( isset( $post['errors'] ) ) {
     1906        $errors = $post['errors']; // @todo return me and display me!
     1907        unset( $post['errors'] );
     1908    }
     1909
     1910    wp_update_post( $post );
     1911
     1912    foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) {
     1913        if ( isset( $attachment_data[ $taxonomy ] ) )
     1914            wp_set_object_terms( $id, array_map( 'trim', preg_split( '/,+/', $attachment_data[ $taxonomy ] ) ), $taxonomy, false );
     1915    }
     1916
     1917    if ( ! $attachment = wp_prepare_attachment_for_js( $id ) )
     1918        wp_send_json_error();
     1919
     1920    wp_send_json_success( $attachment );
     1921}
  • trunk/wp-admin/includes/media.php

    r22523 r22541  
    12701270}
    12711271
     1272function get_compat_media_markup( $attachment_id, $args = null ) {
     1273    $post = get_post( $attachment_id );
     1274
     1275    $default_args = array(
     1276        'errors' => null,
     1277    );
     1278
     1279    $args = wp_parse_args( $args, $default_args );
     1280    $args = apply_filters( 'get_media_item_args', $args );
     1281
     1282    $errors = $args['errors'];
     1283
     1284    $form_fields = get_attachment_fields_to_edit( $post, $errors );
     1285
     1286    $media_meta = apply_filters( 'media_meta', '', $post );
     1287
     1288    $defaults = array(
     1289        'input'      => 'text',
     1290        'required'   => false,
     1291        'value'      => '',
     1292        'extra_rows' => array(),
     1293    );
     1294
     1295    $hidden_fields = array();
     1296
     1297    unset( $form_fields['image-size'], $form_fields['align'], $form_fields['image_alt'],
     1298        $form_fields['post_title'], $form_fields['post_excerpt'], $form_fields['post_content'],
     1299        $form_fields['url'], $form_fields['menu_order'], $form_fields['image_url'] );
     1300
     1301    $item = '';
     1302    foreach ( $form_fields as $id => $field ) {
     1303        if ( $id[0] == '_' )
     1304            continue;
     1305
     1306        $name = "attachments[$attachment_id][$id]";
     1307
     1308        if ( !empty( $field['tr'] ) ) {
     1309            $item .= $field['tr'];
     1310            continue;
     1311        }
     1312
     1313        $field = array_merge( $defaults, $field );
     1314
     1315        if ( $field['input'] == 'hidden' ) {
     1316            $hidden_fields[$id] = $field['value'];
     1317            continue;
     1318        }
     1319
     1320        $required      = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
     1321        $aria_required = $field['required'] ? " aria-required='true' " : '';
     1322        $class  = 'compat-item-' . $name;
     1323        $class .= $field['required'] ? ' form-required' : '';
     1324
     1325        $item .= "\t\t<tr class='$class'>";
     1326        $item .= "\t\t\t<th valign='top' scope='row' class='label'><label for='$name'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label>";
     1327        $item .= "</th>\n\t\t\t<td class='field'>";
     1328
     1329        if ( !empty( $field[ $field['input'] ] ) )
     1330            $item .= $field[ $field['input'] ];
     1331        elseif ( $field['input'] == 'textarea' ) {
     1332            if ( 'post_content' == $id && user_can_richedit() ) {
     1333                // sanitize_post() skips the post_content when user_can_richedit
     1334                $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
     1335            }
     1336            $item .= "<textarea id='$name' name='$name' $aria_required>" . $field['value'] . '</textarea>';
     1337        } else {
     1338            $item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "' $aria_required />";
     1339        }
     1340        if ( !empty( $field['helps'] ) )
     1341            $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
     1342        $item .= "</td>\n\t\t</tr>\n";
     1343
     1344        $extra_rows = array();
     1345
     1346        if ( !empty( $field['errors'] ) )
     1347            foreach ( array_unique( (array) $field['errors'] ) as $error )
     1348                $extra_rows['error'][] = $error;
     1349
     1350        if ( !empty( $field['extra_rows'] ) )
     1351            foreach ( $field['extra_rows'] as $class => $rows )
     1352                foreach ( (array) $rows as $html )
     1353                    $extra_rows[$class][] = $html;
     1354
     1355        foreach ( $extra_rows as $class => $rows )
     1356            foreach ( $rows as $html )
     1357                $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
     1358    }
     1359
     1360    if ( !empty( $form_fields['_final'] ) )
     1361        $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
     1362    if ( $item )
     1363        $item = '<table>' . $item . '</table>';
     1364
     1365    return array(
     1366        'item'   => $item,
     1367        'hidden' => $hidden_fields,
     1368        'meta'   => $media_meta,
     1369    );
     1370}
     1371
    12721372/**
    12731373 * {@internal Missing Short Description}}
  • trunk/wp-includes/css/media-views.css

    r22537 r22541  
    201201
    202202.media-sidebar .setting span {
    203     float: left;
    204203    min-width: 30%;
    205     min-height: 24px;
    206204    margin-right: 4%;
     205}
     206
     207.media-sidebar .setting span,
     208.compat-item label span {
     209    float: left;
     210    min-height: 22px;
    207211    padding-top: 8px;
    208212    line-height: 16px;
    209213    text-align: right;
     214    font-weight: normal;
    210215    color: #999;
    211216    text-shadow: 0 1px 0 #fff;
     
    213218
    214219.media-sidebar .setting input,
    215 .media-sidebar .setting textarea {
    216     float: right;
    217     width: 65%;
     220.media-sidebar .setting textarea,
     221.compat-item .field input,
     222.compat-item .field textarea {
    218223    padding: 6px 8px;
    219224    line-height: 16px;
     225}
     226
     227.media-sidebar .setting input,
     228.media-sidebar .setting textarea {
     229    width: 65%;
     230    float: right;
     231}
     232
     233.media-sidebar .setting textarea,
     234.compat-item .field textarea {
     235    height: 62px;
    220236    resize: none;
    221237}
    222238
    223 .media-sidebar .setting textarea {
    224     height: 62px;
    225 }
    226 
    227 .media-sidebar .setting select {
     239.media-sidebar .setting select,
     240.compat-item .field select {
    228241    height: 28px;
    229242    line-height: 28px;
    230243    margin-top: 3px;
     244}
     245
     246.compat-item {
     247    float: left;
     248    width: 100%;
     249    overflow: hidden;
     250}
     251
     252.compat-item table {
     253    width: 100%;
     254    table-layout: fixed;
     255    border-spacing: 0;
     256    border: 0;
     257}
     258
     259.compat-item tr {
     260    padding: 2px 0;
     261    display: block;
     262    overflow: hidden;
     263}
     264
     265.compat-item .label,
     266.compat-item .field {
     267    display: block;
     268    margin: 0;
     269    padding: 0;
     270}
     271
     272.compat-item .label {
     273    min-width: 30%;
     274    margin-right: 4%;
     275    float: left;
     276    text-align: right;
     277}
     278.compat-item .label span {
     279    display: block;
     280    width: 100%;
     281}
     282
     283.compat-item .field {
     284    float: right;
     285    width: 65%;
     286    padding-right: 1px;
     287}
     288
     289.compat-item .field input {
     290    width: 100%;
     291    margin: 0;
    231292}
    232293
     
    856917    border-bottom: 1px solid #e5e5e5;
    857918    box-shadow: 0 1px 0 #fff;
    858     padding-bottom: 16px;
     919    padding-bottom: 11px;
    859920}
    860921
     
    870931    max-height: 120px;
    871932    margin-right: 10px;
     933    margin-bottom: 5px;
    872934}
    873935
     
    895957}
    896958
     959.attachment-info .compat-meta {
     960    float: left;
     961}
     962
    897963/**
    898964 * Attachment Display Settings
    899965 */
     966.attachment-display-settings {
     967    overflow: hidden;
     968    float: left;
     969}
    900970
    901971.attachment-display-settings h4 {
  • trunk/wp-includes/js/media-models.js

    r22539 r22541  
    246246            resp.modified = new Date( resp.modified );
    247247            return resp;
     248        },
     249
     250        saveCompat: function( data, options ) {
     251            var model = this;
     252
     253            return media.post( 'save-attachment-compat', _.defaults({
     254                id:     this.id,
     255                nonce:  l10n.saveAttachmentNonce
     256            }, data ) ).done( function( resp, status, xhr ) {
     257                model.set( model.parse( resp, xhr ), options );
     258            });
    248259        }
    249260    }, {
  • trunk/wp-includes/js/media-views.js

    r22537 r22541  
    833833
    834834        settingsSidebar: function( options ) {
     835            var single = this.state().get('selection').single(),
     836                views = {};
     837
     838            views.details = new media.view.Attachment.Details({
     839                controller: this,
     840                model:      single,
     841                priority:   80
     842            }).render();
     843
     844
     845            if ( single.get('compat') ) {
     846                views.compat = new media.view.AttachmentCompat({
     847                    controller: this,
     848                    model:      single,
     849                    priority:   120
     850                }).render();
     851            }
     852
    835853            this.sidebar.view( new media.view.Sidebar({
    836854                controller: this,
    837855                silent:     options && options.silent,
    838 
    839                 views: {
    840                     details: new media.view.Attachment.Details({
    841                         controller: this,
    842                         model:      this.state().get('selection').single(),
    843                         priority:   80
    844                     }).render()
    845                 }
     856                views:      views
    846857            }) );
    847858        },
     
    11461157                    model:        display[ single.cid ],
    11471158                    sizes:        single.get('sizes'),
    1148                     priority:     100,
     1159                    priority:     160,
    11491160                    userSettings: state.get('displayUserSettings')
    11501161                }).render()
     
    26402651
    26412652    /**
     2653     * wp.media.view.AttachmentCompat
     2654     */
     2655    media.view.AttachmentCompat = Backbone.View.extend({
     2656        tagName:   'form',
     2657        className: 'compat-item',
     2658
     2659        events: {
     2660            'submit':          'preventDefault',
     2661            'change input':    'save',
     2662            'change select':   'save',
     2663            'change textarea': 'save'
     2664        },
     2665
     2666        initialize: function() {
     2667            this.model.on( 'change:compat', this.render, this );
     2668        },
     2669
     2670        destroy: function() {
     2671            this.model.off( null, null, this );
     2672        },
     2673
     2674        render: function() {
     2675            var compat = this.model.get('compat');
     2676            if ( ! compat || ! compat.item )
     2677                return;
     2678
     2679            this.$el.html( compat.item );
     2680            return this;
     2681        },
     2682
     2683        preventDefault: function( event ) {
     2684            event.preventDefault();
     2685        },
     2686
     2687        save: function( event ) {
     2688            var data = {};
     2689
     2690            event.preventDefault();
     2691
     2692            _.each( this.$el.serializeArray(), function( pair ) {
     2693                data[ pair.name ] = pair.value;
     2694            });
     2695
     2696            this.model.saveCompat( data );
     2697        }
     2698    });
     2699
     2700    /**
    26422701     * wp.media.view.Iframe
    26432702     */
  • trunk/wp-includes/media.php

    r22537 r22541  
    12891289    }
    12901290
     1291    if ( function_exists('get_compat_media_markup') )
     1292        $response['compat'] = get_compat_media_markup( $attachment->ID );
     1293
    12911294    return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta );
    12921295}
     
    14541457                <# if ( 'image' === type ) { #>
    14551458                    <div class="dimensions">{{ width }} &times; {{ height }}</div>
     1459                <# } #>
     1460            </div>
     1461            <div class="compat-meta">
     1462                <# if ( compat && compat.meta ) { #>
     1463                    {{{ compat.meta }}}
    14561464                <# } #>
    14571465            </div>
Note: See TracChangeset for help on using the changeset viewer.