From 55a6c62e7665916920d6e6a0ab550693be9f0c24 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Wed, 21 Nov 2012 18:53:00 +0000 Subject: [PATCH] Support attachment_fields_to_save and attachment_fields_to_edit for attachments going through post.php. see #22186. see #21391. git-svn-id: http://core.svn.wordpress.org/trunk@22783 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/css/wp-admin-rtl.css | 5 ++++ wp-admin/css/wp-admin.css | 5 ++++ wp-admin/includes/media.php | 53 ++++++++++++++++++++--------------- wp-admin/includes/post.php | 3 ++ wp-includes/media.php | 2 +- 5 files changed, 45 insertions(+), 23 deletions(-) diff --git a/wp-admin/css/wp-admin-rtl.css b/wp-admin/css/wp-admin-rtl.css index a85a288560..f0977325c1 100644 --- a/wp-admin/css/wp-admin-rtl.css +++ b/wp-admin/css/wp-admin-rtl.css @@ -922,6 +922,11 @@ th.sorted a span { padding-right: 18px; } +.compat-attachment-fields th { + padding-right: 0; + padding-left: 10px; +} + /*------------------------------------------------------------------------------ 11.1 - Custom Fields ------------------------------------------------------------------------------*/ diff --git a/wp-admin/css/wp-admin.css b/wp-admin/css/wp-admin.css index ffb344fffb..556c8324f5 100644 --- a/wp-admin/css/wp-admin.css +++ b/wp-admin/css/wp-admin.css @@ -3400,6 +3400,11 @@ td.plugin-title p { display: none; } +.compat-attachment-fields th { + padding-top: 5px; + padding-right: 10px; +} + /*------------------------------------------------------------------------------ 11.2 - Post Revisions ------------------------------------------------------------------------------*/ diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index 6cc1b63693..65034fa7a4 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -1276,6 +1276,7 @@ function get_compat_media_markup( $attachment_id, $args = null ) { $default_args = array( 'errors' => null, + 'taxonomies' => false, ); $args = wp_parse_args( $args, $default_args ); @@ -1283,26 +1284,28 @@ function get_compat_media_markup( $attachment_id, $args = null ) { $form_fields = array(); - foreach ( get_attachment_taxonomies($post) as $taxonomy ) { - $t = (array) get_taxonomy($taxonomy); - if ( ! $t['public'] || ! $t['show_ui'] ) - continue; - if ( empty($t['label']) ) - $t['label'] = $taxonomy; - if ( empty($t['args']) ) - $t['args'] = array(); + if ( $args['taxonomies'] ) { + foreach ( get_attachment_taxonomies($post) as $taxonomy ) { + $t = (array) get_taxonomy($taxonomy); + if ( ! $t['public'] || ! $t['show_ui'] ) + continue; + if ( empty($t['label']) ) + $t['label'] = $taxonomy; + if ( empty($t['args']) ) + $t['args'] = array(); - $terms = get_object_term_cache($post->ID, $taxonomy); - if ( false === $terms ) - $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']); + $terms = get_object_term_cache($post->ID, $taxonomy); + if ( false === $terms ) + $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']); - $values = array(); + $values = array(); - foreach ( $terms as $term ) - $values[] = $term->slug; - $t['value'] = join(', ', $values); + foreach ( $terms as $term ) + $values[] = $term->slug; + $t['value'] = join(', ', $values); - $form_fields[$taxonomy] = $t; + $form_fields[$taxonomy] = $t; + } } // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default @@ -1332,6 +1335,7 @@ function get_compat_media_markup( $attachment_id, $args = null ) { continue; $name = "attachments[$attachment_id][$id]"; + $id_attr = "attachments-$attachment_id-$id"; if ( !empty( $field['tr'] ) ) { $item .= $field['tr']; @@ -1341,17 +1345,17 @@ function get_compat_media_markup( $attachment_id, $args = null ) { $field = array_merge( $defaults, $field ); if ( $field['input'] == 'hidden' ) { - $hidden_fields[$id] = $field['value']; + $hidden_fields[$name] = $field['value']; continue; } $required = $field['required'] ? '*' : ''; $aria_required = $field['required'] ? " aria-required='true' " : ''; - $class = 'compat-item-' . $name; + $class = 'compat-field-' . $id; $class .= $field['required'] ? ' form-required' : ''; $item .= "\t\t"; - $item .= "\t\t\t"; + $item .= "\t\t\t"; $item .= "\n\t\t\t"; if ( !empty( $field[ $field['input'] ] ) ) @@ -1361,9 +1365,9 @@ function get_compat_media_markup( $attachment_id, $args = null ) { // sanitize_post() skips the post_content when user_can_richedit $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES ); } - $item .= "'; + $item .= "'; } else { - $item .= ""; + $item .= ""; } if ( !empty( $field['helps'] ) ) $item .= "

" . join( "

\n

", array_unique( (array) $field['helps'] ) ) . '

'; @@ -1388,7 +1392,7 @@ function get_compat_media_markup( $attachment_id, $args = null ) { if ( !empty( $form_fields['_final'] ) ) $item .= "\t\t{$form_fields['_final']}\n"; if ( $item ) - $item = '' . $item . '
'; + $item = '' . $item . '
'; return array( 'item' => $item, @@ -2285,6 +2289,11 @@ function edit_form_image_editor() { ID ); + echo $extras['item']; + foreach ( $extras['hidden'] as $hidden_field => $value ) { + echo '' . "\n"; + } } /** diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index 1311068b7f..e4eca553cc 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -238,6 +238,9 @@ function edit_post( $post_data = null ) { // update_meta expects slashed update_post_meta( $post_ID, '_wp_attachment_image_alt', addslashes( $image_alt ) ); } + + if ( isset( $post_data['attachments'][ $post_ID ] ) ) + $post_data = apply_filters( 'attachment_fields_to_save', $post_data, $post_data['attachments'][ $post_ID ] ); } add_meta( $post_ID ); diff --git a/wp-includes/media.php b/wp-includes/media.php index d8475f18af..10ac1740e6 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -1293,7 +1293,7 @@ function wp_prepare_attachment_for_js( $attachment ) { } if ( function_exists('get_compat_media_markup') ) - $response['compat'] = get_compat_media_markup( $attachment->ID ); + $response['compat'] = get_compat_media_markup( $attachment->ID, array( 'taxonomies' => true ) ); return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta ); }