-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmf2-feed.php
192 lines (168 loc) · 5.28 KB
/
mf2-feed.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* Plugin Name: MF2 Feed
* Plugin URI: https://github.com/indieweb/wordpress-mf2-feed/
* Description: Adds a Microformats2 JSON feed for every entry
* Version: 3.1.1
* Author: IndieWeb WordPress Outreach Club
* Author URI: https://indieweb.org/WordPress_Outreach_Club
* License: MIT
* License URI: http://opensource.org/licenses/MIT
* Text Domain: mf2-feed
* Domain Path: /languages
*/
add_action( 'init', array( 'Mf2Feed', 'init' ) );
// flush rewrite rules
register_activation_hook( __FILE__, array( 'Mf2Feed', 'activate' ) );
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
/**
* Mf2Feed class
*
* @author Matthias Pfefferle
*/
class Mf2Feed {
/**
* init function
*/
public static function init() {
self::setup_feeds();
// add 'json' as feed
add_action( 'do_feed_mf2', array( 'Mf2Feed', 'do_feed_mf2' ), 10, 1 );
add_action( 'do_feed_jf2', array( 'Mf2Feed', 'do_feed_jf2' ), 10, 1 );
add_action( 'wp_head', array( 'Mf2Feed', 'add_html_header' ), 5 );
add_filter( 'feed_content_type', array( 'Mf2Feed', 'feed_content_type' ), 10, 2 );
add_filter( 'template_include', array( 'Mf2Feed', 'render_json_template' ), 100 );
}
public static function activate() {
self::setup_feeds();
flush_rewrite_rules();
}
public static function setup_feeds() {
add_feed( 'mf2', array( 'Mf2Feed', 'do_feed_mf2' ) );
add_feed( 'jf2', array( 'Mf2Feed', 'do_feed_jf2' ) );
}
/**
* adds an MF2 JSON feed
*
* @param boolean $for_comments true if it is a comment-feed
*/
public static function do_feed_mf2( $for_comments ) {
if ( $for_comments ) {
load_template( dirname( __FILE__ ) . '/includes/feed-mf2-comments.php' );
} else {
load_template( dirname( __FILE__ ) . '/includes/feed-mf2.php' );
}
}
/**
* Prepares JSON for output
*
* @param array $json Associative array
* @return string $json_str JSON encoded string
*/
public static function encode_json( $json, $feed = 'mf2' ) {
$options = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES;
/*
* Options to be passed to json_encode()
*
* @param int $options The current options flags
*/
$options = apply_filters( '{$feed}_feed_options', $options ); // phpcs:ignore
return wp_json_encode( $json, $options );
}
/**
* adds an UF2 JSON feed
*
* @param boolean $for_comments true if it is a comment-feed
*/
public static function do_feed_jf2( $for_comments ) {
if ( $for_comments ) {
load_template( dirname( __FILE__ ) . '/includes/feed-jf2-comments.php' );
} else {
load_template( dirname( __FILE__ ) . '/includes/feed-jf2.php' );
}
}
/**
* Return a MF2/JF2 JSON version of an author, post or page.
*
* @param string $template The path to the template object.
*
* @return string The new path to the JSON template.
*/
public static function render_json_template( $template ) {
if ( ! is_singular() ) {
return $template;
}
global $wp_query;
if ( ! isset( $_SERVER['HTTP_ACCEPT'] ) ) {
return $template;
}
$accept_header = $_SERVER['HTTP_ACCEPT'];
if (
stristr( $accept_header, 'application/mf2+json' )
) {
return dirname( __FILE__ ) . '/includes/feed-mf2-comments.php';;
} elseif (
stristr( $accept_header, 'application/jf2+json' )
) {
return dirname( __FILE__ ) . '/includes/feed-jf2-comments.php';
}
// Accept header as an array.
$accept = explode( ',', trim( $accept_header ) );
if (
in_array( 'application/mf2+json', $accept, true )
) {
return dirname( __FILE__ ) . '/includes/feed-mf2-comments.php';;
} elseif (
in_array( 'application/jf2+json', $accept, true )
) {
return dirname( __FILE__ ) . '/includes/feed-jf2-comments.php';
}
return $template;
}
/**
* adds "mf2" content-type
*
* @param string $content_type the default content-type
* @param string $type the feed-type
* @return string the as1 content-type
*/
public static function feed_content_type( $content_type, $type ) {
switch ( $type ) {
case 'mf2':
return apply_filters( 'mf2_feed_content_type', 'application/mf2+json' );
break;
case 'jf2':
return apply_filters( 'jf2_feed_content_type', 'application/jf2+json' );
break;
case 'jf2feed':
return apply_filters( 'jf2_feed_content_type', 'application/jf2feed+json' );
break;
default:
return $content_type;
break;
}
}
/**
* Echos autodiscovery links
*/
public static function add_html_header() {
if ( is_singular() ) {
?>
<link rel="alternate" type="<?php echo esc_attr( feed_content_type( 'mf2' ) ); ?>" href="<?php echo esc_url( get_post_comments_feed_link( null, 'mf2' ) ); ?>" />
<link rel="alternate" type="<?php echo esc_attr( feed_content_type( 'jf2' ) ); ?>" href="<?php echo esc_url( get_post_comments_feed_link( null, 'jf2' ) ); ?>" />
<?php
} else {
?>
<link rel="alternate" type="<?php echo esc_attr( feed_content_type( 'mf2' ) ); ?>" href="<?php echo esc_url( get_feed_link( 'mf2' ) ); ?>" />
<link rel="alternate" type="<?php echo esc_attr( feed_content_type( 'jf2feed' ) ); ?>" href="<?php echo esc_url( get_feed_link( 'jf2' ) ); ?>" />
<?php
}
}
}
// Backcompat for function introduced in WordPress 5.3
if ( ! function_exists( 'get_self_link' ) ) {
function get_self_link() {
$host = @parse_url( home_url() );
return set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) );
}
}