diff --git a/README.md b/README.md index 3c0122c..ff449ad 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ The following custom WP-CLI commands exist in this plugin: ## Plugins * [Writing Helper](https://github.com/Automattic/writing-helper) - "Helps you write your posts." This plugin is a feature on WordPress.com that allows posts to be copied and for feedback to be requested. This plugin can be disabled by calling `add_filter( 'wpcom_compat_enable_writing_helper', '__return_false' );` before loading the WordPress.com Compatibility mu-plugin. +* MediaRSS - This plugin adds compatibility for MediaRSS for RSS feeds, it's going to be merged in Jetpack at some point [Jetpack issue #11062](https://github.com/Automattic/jetpack/issues/11062), but for now it's a part of this compat plugin. ## Filters diff --git a/plugins/mrss.php b/plugins/mrss.php new file mode 100644 index 0000000..e8f0336 --- /dev/null +++ b/plugins/mrss.php @@ -0,0 +1,196 @@ +xmlns:media="http://search.yahoo.com/mrss/" + 'gallery_shortcode' ); + + if ( ! isset( $content ) ) { + $content = get_the_content(); + } + + $content = apply_filters( 'the_content_rss', $content ); + $content = do_shortcode( $content ); + $shortcode_tags = $_shortcode_tags; + + // img tags + if ( preg_match_all( '//', $content, $matches ) ) { + foreach ( $matches[1] as $attrs ) { + $media = $img = array(); + foreach ( wp_kses_hair( $attrs, array( 'http', 'https' ) ) as $attr ) { + $img[ $attr['name'] ] = $attr['value']; + } + if ( ! isset( $img['src'] ) || 0 !== strpos( $img['src'], 'http' ) ) { + continue; + } + $media['content']['attr']['url'] = esc_url( $img['src'] ); + $media['content']['attr']['medium'] = 'image'; + if ( ! empty( $img['title'] ) ) { + $media['content']['children']['title']['attr']['type'] = 'html'; + $media['content']['children']['title']['children'][] = $img['title']; + } elseif ( ! empty( $img['alt'] ) ) { + $media['content']['children']['title']['attr']['type'] = 'html'; + $media['content']['children']['title']['children'][] = $img['alt']; + } + $meds[] = $media; + } + } + + // audio players + if ( preg_match_all( '!\[audio (.+)\]!i', $content, $matches ) ) { + foreach ( $matches[1] as $url ) { + $media = array(); + + // New-style media player puts the audio in a src or mp3 attribute. + // If we see an attribute starting with a https? url use that instead of the compat stuff below + if ( preg_match( '!\w+="(https?://[^"]+)"!', $url, $attribute_match ) ) { + $url = $attribute_match[1]; + } else { + // Remove the audio player config args that start with the pipe symbol (|) + $url = preg_replace( '/\|.+/', '', $url ); + $url = html_entity_decode( $url ); + $url = preg_replace( '/[<>"\']/', '', $url ); + } + + $media['content']['attr']['url'] = esc_url( $url ); + $media['content']['attr']['medium'] = 'audio'; + $meds[] = $media; + } + } + + $meds = apply_filters( 'mrss_media', $meds ); + + if ( ! empty( $meds ) ) { + foreach ( $meds as $media ) { + mrss_print( $media ); + } + } + } + + add_filter( 'mrss_media', 'mrss_featured_image' ); + + /* Add featured image, as first item */ + function mrss_featured_image( $meds ) { + + if ( ! ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail() ) ) { + return $meds; + } + + $media = array(); + + $thumb_id = get_post_thumbnail_id(); + $thumb_url = wp_get_attachment_image_src( $thumb_id, 'full' ); + $thumb_url = $thumb_url[0]; + + if ( ! empty( $thumb_url ) ) { + $media['thumbnail']['attr']['url'] = esc_url( $thumb_url ); + $media['content']['attr']['url'] = esc_url( $thumb_url ); + $media['content']['attr']['medium'] = 'image'; + } + + $thumbnail = & get_post( $thumb_id ); + $title = trim( strip_tags( $thumbnail->post_title ) ); + + if ( empty( $title ) ) { + $title = trim( strip_tags( get_post_meta( $thumb_id, '_wp_attachment_image_alt', true ) ) ); + } + + if ( ! empty( $title ) ) { + $media['content']['children']['title']['attr']['type'] = 'html'; + $media['content']['children']['title']['children'][] = $title; + } + + // Remove from list if already there. + foreach ( $meds as $key => $med ) { + if ( $med['content']['attr']['url'] == $thumb_url ) { + unset( $meds[ $key ] ); + } + } + + // Add as first item + array_unshift( $meds, $media ); + + return $meds; + } + + function mrss_news_item() { + foreach ( get_post_custom() as $k => $v ) { + if ( $k == $v[0] && strpos( $k, ':' ) ) { + list( $blog_id, $post_id ) = explode( ':', $k ); + break; + } + } + $post = get_blog_post( $blog_id, $post_id ); + + mrss_item( $post->post_content ); + } + + function mrss_print( $element, $indent = 2 ) { + + echo "\n"; + + foreach ( (array) $element as $name => $data ) { + + echo str_repeat( "\t", $indent ) . " $value ) { + echo " $attr=\"" . ent2ncr( esc_attr( $value ) ) . '"'; + } + } + if ( ! empty( $data['children'] ) ) { + $nl = false; + echo '>'; + foreach ( $data['children'] as $_name => $_data ) { + + if ( is_int( $_name ) ) { + + if ( ! is_array( $_data ) ) { + echo ent2ncr( esc_html( $_data ) ); + } else { + // allow nested same level elements + $nl = true; + mrss_print( $_data, $indent + 1 ); + } + } else { + $nl = true; + mrss_print( array( $_name => $_data ), $indent + 1 ); + } + } + + if ( $nl ) { + echo str_repeat( "\t", $indent ); + } + + echo "\n"; + } else { + echo " />\n"; + } + } + } +} diff --git a/wpcom-plugins.php b/wpcom-plugins.php index 8a895f6..ffe75ec 100644 --- a/wpcom-plugins.php +++ b/wpcom-plugins.php @@ -26,3 +26,5 @@ function wpcom_vip_legacy_load_plugin( $plugin = false, $folder = false, $versio if ( true === apply_filters( 'wpcom_compat_enable_writing_helper', true ) && ! class_exists( 'Writing_Helper' ) ) { require __DIR__ . '/plugins/writing-helper/writing-helper.php'; } + +require_once __DIR__ . '/plugins/mrss.php';