Make WordPress Core

Changeset 20608


Ignore:
Timestamp:
04/26/2012 08:56:52 PM (14 years ago)
Author:
ryan
Message:

XML-RPC featured image and media preparation cleanup.

  • Introduce _prepare_media_item().
  • Use it to prepare post_thumbnail info in _prepare_post().
  • Also use _prepare_media_item() in wp_getMediaLibrary() and wp_getMediaItem() so that all media is prepared consistently and uses the same filters.

Props maxcutler
Fixes #20409

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-wp-xmlrpc-server.php

    r20607 r20608  
    595595                );
    596596
    597                 //
    598                 $post_fields['post_thumbnail']     = get_post_meta( $post['ID'], '_thumbnail_id', true );
    599                 $post_fields['post_thumbnail_url'] = wp_get_attachment_url( $post_fields['post_thumbnail'] );
     597                // Thumbnail
     598                $post_fields['post_thumbnail'] = array();
     599                $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
     600                if ( $thumbnail_id ) {
     601                        $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
     602                        $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
     603                }
    600604
    601605                // Consider future posts as published
     
    699703
    700704                return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
     705        }
     706
     707        /**
     708         * Prepares media item data for return in an XML-RPC object.
     709         *
     710         * @access protected
     711         *
     712         * @param object $media_item The unprepared media item data
     713         * @param string $size The image size to use for the thumbnail URL
     714         * @return array The prepared media item data
     715         */
     716        protected function _prepare_media_item( $media_item, $thumbnail_size='thumbnail' ) {
     717                $_media_item = array(
     718                        'attachment_id'    => strval( $media_item->ID ),
     719                        'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
     720                        'parent'           => $media_item->post_parent,
     721                        'link'             => wp_get_attachment_url( $media_item->ID ),
     722                        'title'            => $media_item->post_title,
     723                        'caption'          => $media_item->post_excerpt,
     724                        'description'      => $media_item->post_content,
     725                        'metadata'         => wp_get_attachment_metadata( $media_item->ID ),
     726                );
     727
     728                $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
     729                if ( $thumbnail_src )
     730                        $_media_item['thumbnail'] = $thumbnail_src[0];
     731                else
     732                        $_media_item['thumbnail'] = $_media_item['link'];
     733
     734                return apply_filters( 'xmlrpc__prepare_media_item', $_media_item, $media_item, $thumbnail_size );
    701735        }
    702736
     
    28192853                        return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
    28202854
    2821                 // Format page date.
    2822                 $attachment_date = $this->_convert_date( $attachment->post_date );
    2823                 $attachment_date_gmt = $this->_convert_date_gmt( $attachment->post_date_gmt, $attachment->post_date );
    2824 
    2825                 $link = wp_get_attachment_url($attachment->ID);
    2826                 $thumbnail_link = wp_get_attachment_thumb_url($attachment->ID);
    2827 
    2828                 $attachment_struct = array(
    2829                         'date_created_gmt'              => $attachment_date_gmt,
    2830                         'parent'                                => $attachment->post_parent,
    2831                         'link'                                  => $link,
    2832                         'thumbnail'                             => $thumbnail_link,
    2833                         'title'                                 => $attachment->post_title,
    2834                         'caption'                               => $attachment->post_excerpt,
    2835                         'description'                   => $attachment->post_content,
    2836                         'metadata'                              => wp_get_attachment_metadata($attachment->ID),
    2837                         'attachment_id'                 => (string) $attachment->ID
    2838                 );
    2839 
    2840                 return $attachment_struct;
     2855                return $this->_prepare_media_item( $attachment );
    28412856        }
    28422857
     
    28652880         */
    28662881        function wp_getMediaLibrary($args) {
    2867                 $raw_args = $args;
    28682882                $this->escape($args);
    28692883
     
    28872901
    28882902                $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
    2889                 $num_attachments = count($attachments);
    2890 
    2891                 if ( ! $num_attachments )
    2892                         return array();
    28932903
    28942904                $attachments_struct = array();
    28952905
    28962906                foreach ($attachments as $attachment )
    2897                         $attachments_struct[] = $this->wp_getMediaItem( array( $raw_args[0], $raw_args[1], $raw_args[2], $attachment->ID ) );
     2907                        $attachments_struct[] = $this->_prepare_media_item( $attachment );
    28982908
    28992909                return $attachments_struct;
     
    42094219                        if ( !empty($enclosure) ) $resp['enclosure'] = $enclosure;
    42104220
    4211                         $resp['wp_post_thumbnail'] = get_post_meta( $postdata['ID'], '_thumbnail_id', true );
    4212                         $resp['wp_post_thumbnail_url'] = wp_get_attachment_url( $resp['wp_post_thumbnail'] );
     4221                        $resp['wp_post_thumbnail'] = get_post_thumbnail_id( $postdata['ID'] );
    42134222
    42144223                        return $resp;
     
    43214330
    43224331                        $entry_index = count( $struct ) - 1;
    4323                         $struct[ $entry_index ][ 'wp_post_thumbnail' ]     = get_post_meta( $entry['ID'], '_thumbnail_id', true );
    4324                         $struct[ $entry_index ][ 'wp_post_thumbnail_url' ] = wp_get_attachment_url( $struct[ $entry_index ][ 'wp_post_thumbnail' ] );
     4332                        $struct[ $entry_index ][ 'wp_post_thumbnail' ] = get_post_thumbnail_id( $entry['ID'] );
    43254333                }
    43264334
Note: See TracChangeset for help on using the changeset viewer.