Skip to content

Commit 1f8eda5

Browse files
committed
Split feeds into feed template files and fix issues
1 parent 45a8d78 commit 1f8eda5

6 files changed

Lines changed: 116 additions & 98 deletions

File tree

includes/class-mf2-feed-entry.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ public function __construct( $post, $with_comments = false ) {
8888
* @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false.
8989
*/
9090
private function get_content_by_id( $post_id = 0, $more_link_text = null, $stripteaser = false ) {
91-
global $post;
92-
$post = get_post( $post_id );
93-
setup_postdata( $post, $more_link_text, $stripteaser );
91+
if ( ! $post_id ) {
92+
$post_id = get_the_ID();
93+
}
94+
$post = get_post( $post_id );
9495
$content = get_the_content();
95-
wp_reset_postdata( $post );
9696

9797
return $content;
9898
}
@@ -106,11 +106,11 @@ private function get_content_by_id( $post_id = 0, $more_link_text = null, $strip
106106
* @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false.
107107
*/
108108
private function get_excerpt_by_id( $post_id = 0, $more_link_text = null, $stripteaser = false ) {
109-
global $post;
110-
$post = get_post( $post_id );
111-
setup_postdata( $post, $more_link_text, $stripteaser );
109+
if ( ! $post_id ) {
110+
$post_id = get_the_ID();
111+
}
112+
$post = get_post( $post_id );
112113
$content = get_the_excerpt();
113-
wp_reset_postdata( $post );
114114

115115
return $content;
116116
}

includes/feed-jf2-comments.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* MF2 Feed Template for displaying an JF2 item.
4+
*
5+
* @package MF2 Feed
6+
*/
7+
8+
header( 'Content-Type: ' . feed_content_type( 'jf2' ), true );
9+
10+
require_once dirname( __FILE__ ) . '/class-mf2-feed-entry.php';
11+
$items = array();
12+
$item = new Mf2_Feed_Entry( get_the_ID() );
13+
$items = $item->to_jf2();
14+
15+
// filter output
16+
$items = apply_filters( 'jf2_feed_array', $items );
17+
echo Mf2Feed::encode_json( $items );

includes/feed-jf2.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* MF2 Feed Template for displaying an JF2 feed.
4+
*
5+
* @package MF2 Feed
6+
*/
7+
8+
header( 'Content-Type: ' . feed_content_type( 'jf2feed' ), true );
9+
10+
require_once dirname( __FILE__ ) . '/class-mf2-feed-entry.php';
11+
12+
$items = array( 'type' => 'feed' );
13+
while ( have_posts() ) {
14+
the_post();
15+
$item = new Mf2_Feed_Entry( get_the_ID() );
16+
$items['children'][] = $item->to_jf2();
17+
}
18+
19+
// filter output
20+
$items = apply_filters( 'jf2_feed_array', $items );
21+
echo Mf2Feed::encode_json( $items );

includes/feed-mf2-comments.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* MF2 Feed Template for displaying an MF2 item.
4+
*
5+
* @package MF2 Feed
6+
*/
7+
8+
header( 'Content-Type: ' . feed_content_type( 'mf2' ), true );
9+
10+
require_once dirname( __FILE__ ) . '/class-mf2-feed-entry.php';
11+
$items = array();
12+
$item = new Mf2_Feed_Entry( get_the_ID() );
13+
$item = $item->to_mf2();
14+
$items = array();
15+
$items['items'] = $item;
16+
17+
// filter output
18+
$items = apply_filters( 'mf2_feed_array', $items );
19+
echo Mf2Feed::encode_json( $items );

includes/feed-mf2.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* MF2 Feed Template for displaying an MF2 feed.
4+
*
5+
* @package MF2 Feed
6+
*/
7+
8+
header( 'Content-Type: ' . feed_content_type( 'mf2' ), true );
9+
10+
require_once dirname( __FILE__ ) . '/class-mf2-feed-entry.php';
11+
$items = array(
12+
'items' => array(
13+
array(
14+
'type' => array( 'h-feed' ),
15+
'properties' => array(
16+
'name' => array( get_bloginfo( 'name' ) ),
17+
'summary' => array( get_bloginfo( 'description' ) ),
18+
'url' => array( site_url( '/' ) ),
19+
),
20+
),
21+
),
22+
);
23+
24+
while ( have_posts() ) {
25+
the_post();
26+
$item = new Mf2_Feed_Entry( get_the_ID() );
27+
$items['items'][0]['children'][] = current( $item->to_mf2() );
28+
}
29+
30+
// filter output
31+
$items = apply_filters( 'mf2_feed_array', $items );
32+
echo Mf2Feed::encode_json( $items );

mf2-feed.php

Lines changed: 19 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)