| | 537 | |
| | 538 | |
| | 539 | /** |
| | 540 | * search for post content for for img tags, and add them to media_rss array |
| | 541 | * |
| | 542 | * @package WordPress |
| | 543 | * @subpackage Feed |
| | 544 | * @since 3.0 |
| | 545 | */ |
| | 546 | function wp_media_rss( $media_rss ){ |
| | 547 | |
| | 548 | global $mrss_gallery_lookup, $post; |
| | 549 | |
| | 550 | // Honor the feed settings. Don't include any media that isn't in the feed. |
| | 551 | if ( get_option( 'rss_use_excerpt' ) || !strlen( get_the_content() ) ) { |
| | 552 | $content = the_excerpt_rss(); |
| | 553 | } else { |
| | 554 | // If any galleries are processed, we need to capture the attachment IDs. |
| | 555 | add_filter( 'wp_get_attachment_link', 'media_rss_gallery_lookup', 10, 5 ); |
| | 556 | $content = apply_filters( 'the_content', get_the_content() ); |
| | 557 | remove_filter( 'wp_get_attachment_link', 'media_rss_gallery_lookup', 10, 5 ); |
| | 558 | $lookup = $mrss_gallery_lookup; |
| | 559 | unset( $mrss_gallery_lookup ); |
| | 560 | } |
| | 561 | |
| | 562 | if( !has_post_thumbnail( $post->ID ) ) |
| | 563 | return $media_rss; |
| | 564 | |
| | 565 | $image_id = get_post_thumbnail_id( $post->ID ); |
| | 566 | $image_data = wp_get_attachment_metadata( $image_id ); |
| | 567 | |
| | 568 | $item['content']['attr']['url'] = wp_get_attachment_url( $image_id ); |
| | 569 | $item['content']['attr']['medium'] = 'image'; |
| | 570 | if ( !empty( $image_data['image_meta']['title'] ) ) { |
| | 571 | $item['content']['children']['title']['attr']['type'] = 'plain'; |
| | 572 | $item['content']['children']['title']['children'][] = $image_data['image_meta']['title']; |
| | 573 | } elseif ( !empty( $image_data['image_meta']['caption'] ) ) { |
| | 574 | $item['content']['children']['title']['attr']['type'] = 'plain'; |
| | 575 | $item['content']['children']['title']['children'][] = $image_data['image_meta']['caption']; |
| | 576 | $item['content']['children']['description']['children'][] = $image_data['image_meta']['caption'] |
| | 577 | } |
| | 578 | if ( !empty( $image_data['sizes']['thumbnail ']) ) |
| | 579 | $item['content']['children']['thumbnail']['attr']['url'] = wp_get_attachment_thumb_url( $image_id ); |
| | 580 | |
| | 581 | $media_rss[] = $item; |
| | 582 | } |
| | 583 | |
| | 584 | return $media_rss; |
| | 585 | } |
| | 586 | add_filter( 'media_rss', 'wp_media_rss' ); |
| | 587 | |
| | 588 | function media_rss_url( $url ) { |
| | 589 | |
| | 590 | if ( preg_match( '!^https?://!', $url ) ) |
| | 591 | return $url; |
| | 592 | if ( $url{0} == '/' ) |
| | 593 | return rtrim( get_bloginfo('home'), '/' ) . $url; |
| | 594 | |
| | 595 | return get_bloginfo('home') . $url; |
| | 596 | } |
| | 597 | |
| | 598 | function media_rss_gallery_lookup( $link, $id, $size, $permalink, $icon ) { |
| | 599 | global $media_rss_gallery_lookup; |
| | 600 | |
| | 601 | preg_match( '/ src="(.*?)"/', $link, $matches ); |
| | 602 | $media_rss_gallery_lookup[$matches[1]] = $id; |
| | 603 | |
| | 604 | return $link; |
| | 605 | } |
| | 606 | |
| | 607 | /** |
| | 608 | * print media_rss array to generate mrss entries such as <media:content> |
| | 609 | * |
| | 610 | * @package WordPress |
| | 611 | * @subpackage Feed |
| | 612 | * @since 2.8.0 |
| | 613 | */ |
| | 614 | function media_print_rss( $media_rss ) { |
| | 615 | |
| | 616 | foreach( (array)$media_rss as $elements ) |
| | 617 | media_print_element( $elements ); |
| | 618 | |
| | 619 | echo "\n"; |
| | 620 | } |
| | 621 | |
| | 622 | function media_print_element( $elements, $indent = 2 ) { |
| | 623 | |
| | 624 | echo "\n"; |
| | 625 | foreach ( (array)$elements as $name => $data ) { |
| | 626 | |
| | 627 | echo str_repeat("\t", $indent) . "<media:$name"; |
| | 628 | if ( !empty($data['attr']) ) { |
| | 629 | foreach ( $data['attr'] as $attr => $value ) |
| | 630 | echo " $attr=\"" . ent2ncr($value) . "\""; |
| | 631 | } |
| | 632 | if ( !empty($data['children']) ) { |
| | 633 | $nl = false; |
| | 634 | echo ">"; |
| | 635 | foreach ( $data['children'] as $_name => $_data ) { |
| | 636 | |
| | 637 | if ( is_int($_name) ) { |
| | 638 | |
| | 639 | if ( !is_array( $_data ) ){ |
| | 640 | echo ent2ncr($_data); |
| | 641 | }else { |
| | 642 | //allow nested same level elements |
| | 643 | $nl = true; |
| | 644 | media_print_element( $_data, $indent + 1 ); |
| | 645 | } |
| | 646 | |
| | 647 | } else { |
| | 648 | $nl = true; |
| | 649 | media_print_element( array( $_name => $_data ), $indent + 1 ); |
| | 650 | } |
| | 651 | } |
| | 652 | |
| | 653 | if ( $nl ) |
| | 654 | echo str_repeat("\t", $indent); |
| | 655 | |
| | 656 | echo "</media:$name>\n"; |
| | 657 | } else { |
| | 658 | echo " />\n"; |
| | 659 | } |
| | 660 | } |
| | 661 | } |
| | 662 | No newline at end of file |