|
3 | 3 | * Plugin Name: MF2 Feed |
4 | 4 | * Plugin URI: http://github.com/indieweb/wordpress-mf2-feed/ |
5 | 5 | * Description: Adds a Microformats2 JSON feed for every entry |
6 | | - * Version: 3.0.0 |
| 6 | + * Version: 3.1.0 |
7 | 7 | * Author: IndieWeb WordPress Outreach Club |
8 | 8 | * Author URI: https://indieweb.org/WordPress_Outreach_Club |
9 | 9 | * License: MIT |
@@ -35,6 +35,8 @@ public static function init() { |
35 | 35 |
|
36 | 36 | add_action( 'wp_head', array( 'Mf2Feed', 'add_html_header' ), 5 ); |
37 | 37 | add_filter( 'feed_content_type', array( 'Mf2Feed', 'feed_content_type' ), 10, 2 ); |
| 38 | + |
| 39 | + add_filter( 'template_include', array( 'Mf2Feed', 'render_json_template' ), 100 ); |
38 | 40 | } |
39 | 41 |
|
40 | 42 | public static function activate() { |
@@ -92,6 +94,52 @@ public static function do_feed_jf2( $for_comments ) { |
92 | 94 | } |
93 | 95 | } |
94 | 96 |
|
| 97 | + /** |
| 98 | + * Return a MF2/JF2 JSON version of an author, post or page. |
| 99 | + * |
| 100 | + * @param string $template The path to the template object. |
| 101 | + * |
| 102 | + * @return string The new path to the JSON template. |
| 103 | + */ |
| 104 | + public static function render_json_template( $template ) { |
| 105 | + if ( ! is_singular() ) { |
| 106 | + return $template; |
| 107 | + } |
| 108 | + |
| 109 | + global $wp_query; |
| 110 | + |
| 111 | + if ( ! isset( $_SERVER['HTTP_ACCEPT'] ) ) { |
| 112 | + return $template; |
| 113 | + } |
| 114 | + |
| 115 | + $accept_header = $_SERVER['HTTP_ACCEPT']; |
| 116 | + |
| 117 | + if ( |
| 118 | + stristr( $accept_header, 'application/mf2+json' ) |
| 119 | + ) { |
| 120 | + return dirname( __FILE__ ) . '/includes/feed-mf2-comments.php';; |
| 121 | + } elseif ( |
| 122 | + stristr( $accept_header, 'application/jf2+json' ) |
| 123 | + ) { |
| 124 | + return dirname( __FILE__ ) . '/includes/feed-jf2-comments.php'; |
| 125 | + } |
| 126 | + |
| 127 | + // Accept header as an array. |
| 128 | + $accept = explode( ',', trim( $accept_header ) ); |
| 129 | + |
| 130 | + if ( |
| 131 | + in_array( 'application/mf2+json', $accept, true ) |
| 132 | + ) { |
| 133 | + return dirname( __FILE__ ) . '/includes/feed-mf2-comments.php';; |
| 134 | + } elseif ( |
| 135 | + in_array( 'application/jf2+json', $accept, true ) |
| 136 | + ) { |
| 137 | + return dirname( __FILE__ ) . '/includes/feed-jf2-comments.php'; |
| 138 | + } |
| 139 | + |
| 140 | + return $template; |
| 141 | + } |
| 142 | + |
95 | 143 | /** |
96 | 144 | * adds "mf2" content-type |
97 | 145 | * |
|
0 commit comments