| | 711 | |
| | 712 | /** |
| | 713 | * Search for post content for for img tags, and add them to media_rss array |
| | 714 | * |
| | 715 | * @since 4.8.0 |
| | 716 | * @param array $media_rss An array of media tags. |
| | 717 | */ |
| | 718 | function wp_media_rss( $media_rss ) { |
| | 719 | |
| | 720 | global $mrss_gallery_lookup, $post; |
| | 721 | |
| | 722 | // Honor the feed settings. Don't include any media that isn't in the feed. |
| | 723 | if ( get_option( 'rss_use_excerpt' ) || !strlen( get_the_content() ) ) { |
| | 724 | $content = the_excerpt_rss(); |
| | 725 | } else { |
| | 726 | // If any galleries are processed, we need to capture the attachment IDs. |
| | 727 | add_filter( 'wp_get_attachment_link', 'media_rss_gallery_lookup', 10, 5 ); |
| | 728 | $content = apply_filters( 'the_content', get_the_content() ); |
| | 729 | remove_filter( 'wp_get_attachment_link', 'media_rss_gallery_lookup', 10, 5 ); |
| | 730 | $lookup = $mrss_gallery_lookup; |
| | 731 | unset( $mrss_gallery_lookup ); |
| | 732 | } |
| | 733 | |
| | 734 | if( ! has_post_thumbnail( $post->ID ) ) |
| | 735 | return $media_rss; |
| | 736 | |
| | 737 | $image_id = get_post_thumbnail_id( $post->ID ); |
| | 738 | $image_data = wp_get_attachment_metadata( $image_id ); |
| | 739 | |
| | 740 | $item['content']['attr']['url'] = wp_get_attachment_url( $image_id ); |
| | 741 | $item['content']['attr']['medium'] = 'image'; |
| | 742 | if ( !empty( $image_data['image_meta']['title'] ) ) { |
| | 743 | $item['content']['children']['title']['attr']['type'] = 'plain'; |
| | 744 | $item['content']['children']['title']['children'][] = $image_data['image_meta']['title']; |
| | 745 | } elseif ( !empty( $image_data['image_meta']['caption'] ) ) { |
| | 746 | $item['content']['children']['title']['attr']['type'] = 'plain'; |
| | 747 | $item['content']['children']['title']['children'][] = $image_data['image_meta']['caption']; |
| | 748 | $item['content']['children']['description']['children'][] = $image_data['image_meta']['caption']; |
| | 749 | } |
| | 750 | if ( !empty( $image_data['sizes']['thumbnail ']) ) |
| | 751 | $item['content']['children']['thumbnail']['attr']['url'] = wp_get_attachment_thumb_url( $image_id ); |
| | 752 | |
| | 753 | $media_rss[] = $item; |
| | 754 | |
| | 755 | return $media_rss; |
| | 756 | } |
| | 757 | add_filter( 'media_rss', 'wp_media_rss' ); |
| | 758 | |
| | 759 | function media_rss_url( $url ) { |
| | 760 | |
| | 761 | if ( preg_match( '!^https?://!', $url ) ) |
| | 762 | return $url; |
| | 763 | if ( $url{0} == '/' ) |
| | 764 | return rtrim( get_bloginfo('home'), '/' ) . $url; |
| | 765 | |
| | 766 | return get_bloginfo('home') . $url; |
| | 767 | } |
| | 768 | |
| | 769 | function media_rss_gallery_lookup( $link, $id, $size, $permalink, $icon ) { |
| | 770 | global $media_rss_gallery_lookup; |
| | 771 | |
| | 772 | preg_match( '/ src="(.*?)"/', $link, $matches ); |
| | 773 | $media_rss_gallery_lookup[$matches[1]] = $id; |
| | 774 | |
| | 775 | return $link; |
| | 776 | } |
| | 777 | |
| | 778 | /** |
| | 779 | * print media_rss array to generate mrss entries such as <media:content> |
| | 780 | * |
| | 781 | * @package WordPress |
| | 782 | * @subpackage Feed |
| | 783 | * @since 2.8.0 |
| | 784 | */ |
| | 785 | function media_print_rss( $media_rss ) { |
| | 786 | |
| | 787 | foreach( (array)$media_rss as $elements ) |
| | 788 | media_print_element( $elements ); |
| | 789 | |
| | 790 | echo "\n"; |
| | 791 | } |
| | 792 | |
| | 793 | function media_print_element( $elements, $indent = 2 ) { |
| | 794 | |
| | 795 | echo "\n"; |
| | 796 | foreach ( (array)$elements as $name => $data ) { |
| | 797 | |
| | 798 | echo str_repeat("\t", $indent) . "<media:$name"; |
| | 799 | if ( !empty($data['attr']) ) { |
| | 800 | foreach ( $data['attr'] as $attr => $value ) |
| | 801 | echo " $attr=\"" . ent2ncr($value) . "\""; |
| | 802 | } |
| | 803 | if ( !empty($data['children']) ) { |
| | 804 | $nl = false; |
| | 805 | echo ">"; |
| | 806 | foreach ( $data['children'] as $_name => $_data ) { |
| | 807 | |
| | 808 | if ( is_int($_name) ) { |
| | 809 | |
| | 810 | if ( !is_array( $_data ) ){ |
| | 811 | echo ent2ncr($_data); |
| | 812 | }else { |
| | 813 | //allow nested same level elements |
| | 814 | $nl = true; |
| | 815 | media_print_element( $_data, $indent + 1 ); |
| | 816 | } |
| | 817 | |
| | 818 | } else { |
| | 819 | $nl = true; |
| | 820 | media_print_element( array( $_name => $_data ), $indent + 1 ); |
| | 821 | } |
| | 822 | } |
| | 823 | |
| | 824 | if ( $nl ) |
| | 825 | echo str_repeat("\t", $indent); |
| | 826 | |
| | 827 | echo "</media:$name>\n"; |
| | 828 | } else { |
| | 829 | echo " />\n"; |
| | 830 | } |
| | 831 | } |
| | 832 | } |