forked from manton/jsonfeed-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed-json-functions.php
206 lines (177 loc) · 6.34 KB
/
feed-json-functions.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
if ( ! function_exists( 'get_self_link' ) ) {
function get_self_link() {
$host = @wp_parse_url( home_url() );
return set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) );
}
}
function get_attachment_json_info() {
if ( post_password_required() ) {
return null;
}
$return = array();
foreach ( (array) get_post_custom() as $key => $val ) {
if ( 'enclosure' === $key ) {
foreach ( (array) $val as $enc ) {
$enclosure = explode( "\n", $enc );
// only get the first element, e.g. audio/mpeg from 'audio/mpeg mpga mp2 mp3'
$t = preg_split( '/[ \t]/', trim( $enclosure[2] ) );
$type = $t[0];
$return[] = array(
'url' => trim( $enclosure[0] ),
'mime_type' => $type,
'size_in_bytes' => (int) $enclosure[1],
);
}
}
}
return $return;
}
function get_link_from_json_feed( $link ) {
global $wp_rewrite;
$arg = $wp_rewrite->get_feed_permastruct();
// If empty this site does not have pretty permalinks enabled
if ( empty( $arg ) ) {
wp_parse_str( wp_parse_url( $link, PHP_URL_QUERY ), $query_args );
unset( $query_args['feed'] );
return add_query_arg( $query_args, home_url( '/' ) );
} else {
$arg = str_replace( '%feed%', 'json', $arg );
$arg = preg_replace( '#/+#', '/', "/$arg" );
$link = str_replace( $arg, '', $link );
}
return $link;
}
function get_json_feed_next_url() {
global $paged, $wp_query;
$max_page = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
$nextpage = ( ! $paged ) ? 2 : (int) $paged + 1;
if ( ! is_single() && ( $nextpage <= $max_page ) ) {
return add_query_arg( 'paged', $nextpage, get_self_link() );
}
return '';
}
function get_json_feed_data() {
$return = array(
'version' => 'https://jsonfeed.org/version/1.1',
// translators: 1. get_self_link URL
// 'user_comment' => sprintf( __( 'This feed allows you to read the posts from this site in any feed reader that supports the JSON Feed format. To add this feed to your reader, copy the following URL -- %1$s -- and add it your reader.', 'jsonfeed' ), get_self_link() ),
'next_url' => get_json_feed_next_url(),
// 'home_page_url' => get_link_from_json_feed( get_self_link() ),
'feed_url' => get_self_link(),
// 'language' => get_bloginfo( 'language' ),
// 'title' => get_wp_title_rss(),
// 'description' => get_bloginfo( 'description' ),
// 'icon' => get_site_icon_url(),
);
return array_filter( $return );
}
function get_json_comment_feed_item() {
$comment = get_comment();
$comment_post = get_post( $comment->comment_post_ID );
if ( 1 === (int) get_option( 'rss_use_excerpt' ) ) {
$content = get_comment_excerpt();
} else {
$content = get_comment_text();
}
if ( ! is_singular() ) {
$title = get_the_title( $comment_post->ID );
/** This filter is documented in wp-includes/feed.php */
$title = apply_filters( 'the_title_rss', $title );
/* translators: Individual comment title. 1: Post title, 2: Comment author name */
$title = sprintf( ent2ncr( __( 'Comment on %1$s by %2$s', 'jsonfeed' ) ), $title, get_comment_author_rss() );
} else {
$title = '';
}
$feed_item = array(
'id' => get_comment_link(),
'url' => get_comment_link(),
'title' => html_entity_decode( $title ),
// 'content_html' => $content,
// 'content_text' => wp_strip_all_tags( $content ),
'date_published' => get_comment_date( 'Y-m-d\TH:i:sP' ),
// 'authors' => array( get_json_comment_author() ),
// 'author' => get_json_comment_author(),
);
if ( ! is_singular() ) {
$feed_item['_parent_url'] = get_permalink( $comment_post );
}
// If anything is an empty string or null then remove it
$feed_item = array_filter( $feed_item );
return apply_filters( 'json_feed_comment_item', $feed_item, get_comment() );
}
function get_json_feed_item() {
if ( 1 === (int) get_option( 'rss_use_excerpt' ) ) {
$content = get_the_json_excerpt_feed();
} else {
$content = get_the_content_feed( 'json' );
}
$feed_item = array(
'id' => get_the_guid(),
'url' => get_permalink(),
'title' => html_entity_decode( get_the_title_rss() ),
// 'content_html' => $content,
// 'content_text' => wp_strip_all_tags( $content ),
'date_published' => get_the_date( 'Y-m-d\TH:i:sP' ),
'date_modified' => get_the_modified_date( 'Y-m-d\TH:i:sP' ),
// 'authors' => array( get_json_item_author() ),
// 'author' => get_json_item_author(),
'image' => get_the_post_thumbnail_url( null, 'full' ), // If there is a set featured image
'tags' => json_get_merged_tags(), // Tags is a merge of the category and the tags names
);
// Only add custom excerpts not generated ones
// if ( has_excerpt() ) {
$feed_item['summary'] = wp_strip_all_tags( get_the_excerpt() );
// }
// If anything is an empty string or null then remove it
$feed_item = array_filter( $feed_item );
$attachment = get_attachment_json_info();
if ( ! empty( $attachment ) ) {
$feed_item['attachments'] = $attachment;
}
return apply_filters( 'json_feed_item', $feed_item, get_post() );
}
function get_json_item_author() {
return array(
'name' => get_the_author(),
'url' => get_author_posts_url( get_the_author_meta( 'ID' ) ),
'avatar' => get_avatar_url( get_the_author_meta( 'ID' ), array( 'size' => 512 ) ),
);
}
function get_json_comment_author() {
return array(
'name' => get_comment_author(),
'url' => get_comment_author_url(),
'avatar' => get_avatar_url( get_comment(), array( 'size' => 512 ) ),
);
}
function json_get_merged_tags( $post_id = null ) {
$post = get_post( $post_id );
if ( ! $post ) {
return array();
}
$tags = get_the_terms( $post, 'post_tag' );
$categories = get_the_terms( $post, 'category' );
$tags = is_array( $tags ) ? $tags : array();
$categories = is_array( $categories ) ? $categories : array();
$tags = array_merge( $tags, $categories );
$tags = wp_list_pluck( $tags, 'name', 'slug' );
$return = array();
foreach ( $tags as $key => $value ) {
if ( 'uncategorized' !== $key ) {
$return[] = $value;
}
}
return $return;
}
function get_the_json_excerpt_feed() {
$output = get_the_excerpt();
/**
* Filters the post excerpt for a feed.
*
* @since 1.2.0
*
* @param string $output The current post excerpt.
*/
return apply_filters( 'the_excerpt_rss', $output );
}