@@ -34,7 +34,6 @@ public static function init() {
3434 add_action ( 'do_feed_jf2 ' , array ( 'Mf2Feed ' , 'do_feed_jf2 ' ), 10 , 1 );
3535
3636 add_action ( 'wp_head ' , array ( 'Mf2Feed ' , 'add_html_header ' ), 5 );
37- add_filter ( 'query_vars ' , array ( 'Mf2Feed ' , 'query_vars ' ) );
3837 add_filter ( 'feed_content_type ' , array ( 'Mf2Feed ' , 'feed_content_type ' ), 10 , 2 );
3938 }
4039
@@ -55,59 +54,29 @@ public static function setup_feeds() {
5554 * @param boolean $for_comments true if it is a comment-feed
5655 */
5756 public static function do_feed_mf2 ( $ for_comments ) {
58- require_once dirname ( __FILE__ ) . '/includes/class-mf2-feed-entry.php ' ;
59-
6057 if ( $ for_comments ) {
61- $ post = new Mf2_Feed_Entry ( get_the_ID () );
62-
63- $ post = $ post ->to_mf2 ();
64-
65- $ items = array ();
66- $ items ['items ' ] = $ post ;
58+ load_template ( dirname ( __FILE__ ) . '/includes/feed-mf2-comments.php ' );
6759 } else {
68- $ items = array (
69- 'items ' => array (
70- array (
71- 'type ' => array ( 'h-feed ' ),
72- 'properties ' => array (
73- 'name ' => array ( get_bloginfo ( 'name ' ) ),
74- 'summary ' => array ( get_bloginfo ( 'description ' ) ),
75- 'url ' => array ( site_url ( '/ ' ) ),
76- ),
77- ),
78- ),
79- );
80-
81- while ( have_posts () ) {
82- the_post ();
83-
84- $ post = new Mf2_Feed_Entry ( get_the_ID () );
85-
86- $ items ['items ' ][0 ]['children ' ][] = current ( $ post ->to_mf2 () );
87- }
88- }
89-
90- // filter output
91- $ json = apply_filters ( 'mf2_feed_array ' , $ items );
92-
93- header ( 'Content-Type: ' . feed_content_type ( 'mf2 ' ) . '; charset= ' . get_option ( 'blog_charset ' ), true );
94-
95- $ options = 0 ;
96- // JSON_PRETTY_PRINT added in PHP 5.4
97- if ( get_query_var ( 'pretty ' ) ) {
98- $ options |= JSON_PRETTY_PRINT ;
60+ load_template ( dirname ( __FILE__ ) . '/includes/feed-mf2.php ' );
9961 }
62+ }
10063
64+ /**
65+ * Prepares JSON for output
66+ *
67+ * @param array $json Associative array
68+ * @return string $json_str JSON encoded string
69+ */
70+ public static function encode_json ( $ json , $ feed = 'mf2 ' ) {
71+ $ options |= JSON_PRETTY_PRINT ;
10172 /*
10273 * Options to be passed to json_encode()
10374 *
10475 * @param int $options The current options flags
10576 */
106- $ options = apply_filters ( 'mf2_feed_options ' , $ options );
77+ $ options = apply_filters ( '{$feed}_feed_options ' , $ options ); // phpcs:ignore
10778
108- $ json_str = wp_json_encode ( $ json , $ options );
109-
110- echo $ json_str ;
79+ return wp_json_encode ( $ json , $ options );
11180 }
11281
11382 /**
@@ -116,44 +85,12 @@ public static function do_feed_mf2( $for_comments ) {
11685 * @param boolean $for_comments true if it is a comment-feed
11786 */
11887 public static function do_feed_jf2 ( $ for_comments ) {
119- require_once dirname ( __FILE__ ) . '/includes/class-mf2-feed-entry.php ' ;
120-
12188 if ( $ for_comments ) {
122- $ post = new Mf2_Feed_Entry ( get_the_ID (), $ for_comments );
123- $ items = $ post ->to_jf2 ();
89+ load_template ( dirname ( __FILE__ ) . '/includes/feed-jf2-comments.php ' );
12490 } else {
125- $ items = array ( 'type ' => 'feed ' );
126-
127- while ( have_posts () ) {
128- the_post ();
129-
130- $ post = new Mf2_Feed_Entry ( get_the_ID () );
131- $ items ['children ' ][] = $ post ->to_jf2 ();
132- }
91+ load_template ( dirname ( __FILE__ ) . '/includes/feed-jf2.php ' );
13392 }
134-
135- // filter output
136- $ json = apply_filters ( 'jf2_feed_array ' , $ items );
137-
138- header ( 'Content-Type: ' . feed_content_type ( 'jf2 ' ) . '; charset= ' . get_option ( 'blog_charset ' ), true );
139-
140- $ options = 0 ;
141-
142- // JSON_PRETTY_PRINT added in PHP 5.4
143- if ( get_query_var ( 'pretty ' ) ) {
144- $ options |= JSON_PRETTY_PRINT ;
145- }
146-
147- /*
148- * Options to be passed to json_encode()
149- *
150- * @param int $options The current options flags
151- */
152- $ options = apply_filters ( 'jf2_feed_options ' , $ options );
153-
154- $ json_str = wp_json_encode ( $ json , $ options );
155-
156- echo $ json_str ;
93+ require_once dirname ( __FILE__ ) . '/includes/class-mf2-feed-entry.php ' ;
15794 }
15895
15996 /**
@@ -171,21 +108,13 @@ public static function feed_content_type( $content_type, $type ) {
171108 if ( 'jf2 ' === $ type || 'jf2 ' === $ type ) {
172109 return apply_filters ( 'jf2_feed_content_type ' , 'application/jf2+json ' );
173110 }
111+ if ( 'jf2feed ' === $ type || 'jf2feed ' === $ type ) {
112+ return apply_filters ( 'jf2_feed_content_type ' , 'application/jf2feed+json ' );
113+ }
174114
175115 return $ content_type ;
176116 }
177117
178- /**
179- * add 'pretty' as a valid query variables.
180- *
181- * @param array $vars
182- * @return array
183- */
184- public static function query_vars ( $ vars ) {
185- $ vars [] = 'pretty ' ;
186- return $ vars ;
187- }
188-
189118 /**
190119 * Echos autodiscovery links
191120 */
0 commit comments