| 561 | |
| 562 | /** |
| 563 | * search for post content for for img tags, and add them to media_rss array |
| 564 | * |
| 565 | * @package WordPress |
| 566 | * @subpackage Feed |
| 567 | * @since 2.8.0 |
| 568 | */ |
| 569 | function wp_media_rss( $media_rss ){ |
| 570 | |
| 571 | global $mrss_gallery_lookup; |
| 572 | |
| 573 | // Honor the feed settings. Don't include any media that isn't in the feed. |
| 574 | if ( get_option( 'rss_use_excerpt' ) || !strlen( get_the_content() ) ) { |
| 575 | $content = the_excerpt_rss(); |
| 576 | } else { |
| 577 | // If any galleries are processed, we need to capture the attachment IDs. |
| 578 | add_filter( 'wp_get_attachment_link', 'media_rss_gallery_lookup', 10, 5 ); |
| 579 | $content = apply_filters( 'the_content', get_the_content() ); |
| 580 | remove_filter( 'wp_get_attachment_link', 'media_rss_gallery_lookup', 10, 5 ); |
| 581 | $lookup = $mrss_gallery_lookup; |
| 582 | unset( $mrss_gallery_lookup ); |
| 583 | } |
| 584 | |
| 585 | // img tags |
| 586 | $r = preg_match_all('/<img (.+?)>/', $content, $matches ); |
| 587 | |
| 588 | if ( $r === false || $r === 0 ) |
| 589 | return $media_rss; |
| 590 | |
| 591 | $num = 0; |
| 592 | foreach ( $matches[1] as $attrs ) { |
| 593 | $item = $img = array(); |
| 594 | |
| 595 | // Construct $img array from <img> attributes |
| 596 | foreach ( wp_kses_hair($attrs, array('http')) as $attr ) |
| 597 | $img[$attr['name']] = $attr['value']; |
| 598 | |
| 599 | if ( !isset($img['src']) ) |
| 600 | continue; |
| 601 | |
| 602 | $img['src'] = media_rss_url( $img['src'] ); |
| 603 | |
| 604 | // Skip emoticons |
| 605 | if ( isset( $img['class'] ) && false !== strpos( $img['class'], 'wp-smiley' ) ) |
| 606 | continue; |
| 607 | |
| 608 | if ( false !== strpos( $img['src'], '&' ) ) |
| 609 | continue; |
| 610 | |
| 611 | $id = false; |
| 612 | if ( isset( $lookup[$img['src']] ) ) { |
| 613 | $id = $lookup[$img['src']]; |
| 614 | } elseif ( isset( $img['class'] ) && preg_match( '/wp-image-(\d+)/', $img['class'], $match ) ) { |
| 615 | $id = $match[1]; |
| 616 | } |
| 617 | |
| 618 | if ( $id ) { |
| 619 | // It's an attachment, so we will get the URLs, title, and description from functions |
| 620 | $attachment =& get_post( $id ); |
| 621 | $src = wp_get_attachment_image_src( $id, 'full' ); |
| 622 | if ( !empty( $src[0] ) ) |
| 623 | $img['src'] = $src[0]; |
| 624 | |
| 625 | $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' ); |
| 626 | if ( !empty( $thumbnail[0] ) && $thumbnail[0] != $img['src'] ) |
| 627 | $img['thumbnail'] = $thumbnail[0]; |
| 628 | |
| 629 | $title = get_the_title( $id ); |
| 630 | if ( !empty( $title ) ) |
| 631 | $img['title'] = trim($title); |
| 632 | |
| 633 | $description = get_the_content( $id ); |
| 634 | if ( !empty( $attachment->post_excerpt ) ) |
| 635 | $img['description'] = trim($attachment->post_excerpt); |
| 636 | } |
| 637 | |
| 638 | // If this is the first image in the markup, make it the post thumbnail |
| 639 | if ( ++$num == 1 ) { |
| 640 | if ( isset( $img['thumbnail'] ) ) |
| 641 | $media_rss[]['thumbnail']['attr']['url'] = $img['thumbnail']; |
| 642 | else |
| 643 | $media_rss[]['thumbnail']['attr']['url'] = $img['src']; |
| 644 | } |
| 645 | |
| 646 | $item['content']['attr']['url'] = $img['src']; |
| 647 | $item['content']['attr']['medium'] = 'image'; |
| 648 | if ( !empty($img['title']) ) { |
| 649 | $item['content']['children']['title']['attr']['type'] = 'plain'; |
| 650 | $item['content']['children']['title']['children'][] = $img['title']; |
| 651 | } elseif ( !empty($img['alt']) ) { |
| 652 | $item['content']['children']['title']['attr']['type'] = 'plain'; |
| 653 | $item['content']['children']['title']['children'][] = $img['alt']; |
| 654 | } |
| 655 | if ( !empty($img['description']) ) { |
| 656 | $item['content']['children']['description']['attr']['type'] = 'html'; |
| 657 | $item['content']['children']['description']['children'][] = $img['description']; |
| 658 | } |
| 659 | if ( !empty($img['thumbnail']) ) |
| 660 | $item['content']['children']['thumbnail']['attr']['url'] = $img['thumbnail']; |
| 661 | |
| 662 | $media_rss[] = $item; |
| 663 | } |
| 664 | |
| 665 | return $media_rss; |
| 666 | } |
| 667 | add_filter( 'media_rss', 'wp_media_rss' ); |
| 668 | |
| 669 | function media_rss_url( $url ) { |
| 670 | |
| 671 | if ( preg_match( '!^https?://!', $url ) ) |
| 672 | return $url; |
| 673 | if ( $url{0} == '/' ) |
| 674 | return rtrim( get_bloginfo('home'), '/' ) . $url; |
| 675 | |
| 676 | return get_bloginfo('home') . $url; |
| 677 | } |
| 678 | |
| 679 | function media_rss_gallery_lookup( $link, $id, $size, $permalink, $icon ) { |
| 680 | global $media_rss_gallery_lookup; |
| 681 | |
| 682 | preg_match( '/ src="(.*?)"/', $link, $matches ); |
| 683 | $media_rss_gallery_lookup[$matches[1]] = $id; |
| 684 | |
| 685 | return $link; |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * print media_rss array to generate mrss entries such as <media:content> |
| 690 | * |
| 691 | * @package WordPress |
| 692 | * @subpackage Feed |
| 693 | * @since 2.8.0 |
| 694 | */ |
| 695 | function media_print_rss( $media_rss ) { |
| 696 | |
| 697 | foreach( (array)$media_rss as $elements ) |
| 698 | media_print_element( $elements ); |
| 699 | |
| 700 | echo "\n"; |
| 701 | } |
| 702 | |
| 703 | function media_print_element( $elements, $indent = 2 ) { |
| 704 | |
| 705 | echo "\n"; |
| 706 | foreach ( (array)$elements as $name => $data ) { |
| 707 | |
| 708 | echo str_repeat("\t", $indent) . "<media:$name"; |
| 709 | if ( !empty($data['attr']) ) { |
| 710 | foreach ( $data['attr'] as $attr => $value ) |
| 711 | echo " $attr=\"" . ent2ncr($value) . "\""; |
| 712 | } |
| 713 | if ( !empty($data['children']) ) { |
| 714 | $nl = false; |
| 715 | echo ">"; |
| 716 | foreach ( $data['children'] as $_name => $_data ) { |
| 717 | |
| 718 | if ( is_int($_name) ) { |
| 719 | |
| 720 | if ( !is_array( $_data ) ){ |
| 721 | echo ent2ncr($_data); |
| 722 | }else { |
| 723 | //allow nested same level elements |
| 724 | $nl = true; |
| 725 | media_print_element( $_data, $indent + 1 ); |
| 726 | } |
| 727 | |
| 728 | } else { |
| 729 | $nl = true; |
| 730 | media_print_element( array( $_name => $_data ), $indent + 1 ); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | if ( $nl ) |
| 735 | echo str_repeat("\t", $indent); |
| 736 | |
| 737 | echo "</media:$name>\n"; |
| 738 | } else { |
| 739 | echo " />\n"; |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | ?> |