PluginProbe
Friends / 4.3.0
Friends v4.3.0
4.3.2 4.3.1 4.3.0 4.2.2 4.2.1 4.2.0 4.1.0 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.0 2.9.1 All 88 releases
friends / feed-parsers / class-feed-parser-json-feed.php

class-feed-parser-json-feed.php in Friends 4.3.0, at feed-parsers/class-feed-parser-json-feed.php

193 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Friends jsonfeed Parser
4 *
5 * With this parser, we can import RSS and Atom Feeds for a friend.
6 *
7 * @package Friends
8 */
9
10 namespace Friends;
11
12 /**
13 * This is the class for the feed part of the Friends Plugin.
14 *
15 * @since 1.0
16 *
17 * @package Friends
18 * @author Alex Kirk
19 */
20 class Feed_Parser_JSON_Feed extends Feed_Parser_V2 {
21 const NAME = 'JSON Feed';
22 const URL = 'https://www.jsonfeed.org/';
23 const SLUG = 'jsonfeed';
24
25 /**
26 * Get the badge for JSON feeds.
27 *
28 * @return array Badge info.
29 */
30 public function get_badge() {
31 return array(
32 'label' => 'JSON',
33 'color' => '#ee802f',
34 'title' => __( 'JSON Feed', 'friends' ),
35 );
36 }
37
38 /**
39 * Determines if this is a supported feed and to what degree we feel it's supported.
40 *
41 * @param string $url The url.
42 * @param string $mime_type The mime type.
43 * @param string $title The title.
44 * @param string|null $content The content, it can't be assumed that it's always available.
45 *
46 * @return int Return 0 if unsupported, a positive value representing the confidence for the feed, use 10 if you're reasonably confident.
47 */
48 public function feed_support_confidence( $url, $mime_type, $title, $content = null ) {
49 $confidence = 10;
50
51 $rewritten = $this->rewrite_known_url( $url );
52 if ( $rewritten ) {
53 $mime_type = $rewritten['type'];
54 $confidence *= 2;
55 }
56
57 switch ( $mime_type ) {
58 case 'application/feed+json':
59 return $confidence;
60 case 'application/json':
61 if ( $content ) {
62 return $confidence;
63
64 }
65 }
66
67 return 0;
68 }
69
70 /**
71 * Rewrite known URLs to their RSS feeds.
72 *
73 * @param string $url The url.
74 *
75 * @return array An equivalent link array.
76 */
77 public function rewrite_known_url( $url ) {
78 $host = wp_parse_url( strtolower( $url ), PHP_URL_HOST );
79
80 switch ( $host ) {
81 case 'micro.blog':
82 if ( preg_match( '#/([^/]+)$#', $url, $m ) ) {
83 return array(
84 'title' => 'Micro.blog: ' . $m[1],
85 'rel' => 'alternate',
86 'type' => 'application/feed+json',
87 'url' => 'https://micro.blog/posts/' . $m[1],
88 );
89 }
90 return array();
91 }
92
93 return array();
94 }
95
96 /**
97 * Format the feed title and autoselect the posts feed.
98 *
99 * @param array $feed_details The feed details.
100 *
101 * @return array The (potentially) modified feed details.
102 */
103 public function update_feed_details( $feed_details ) {
104 $rewritten = $this->rewrite_known_url( $feed_details['url'] );
105 if ( $rewritten ) {
106 $feed_details = $rewritten;
107 }
108
109 return $feed_details;
110 }
111
112 /**
113 * Discover the feeds available at the URL specified.
114 *
115 * @param string $content The content for the URL is already provided here.
116 * @param string $url The url to search.
117 *
118 * @return array A list of supported feeds at the URL.
119 */
120 public function discover_available_feeds( $content, $url ) {
121 return array();
122 }
123
124 /**
125 * Fetches a feed and returns the processed items.
126 *
127 * @param string $url The url.
128 * @param User_Feed $user_feed The user feed.
129 *
130 * @return array An array of feed items.
131 */
132 public function fetch_feed( $url, ?User_Feed $user_feed = null ) {
133 $args = array();
134 $res = wp_safe_remote_request( $url, $args );
135
136 if ( is_wp_error( $res ) ) {
137 return $res;
138 }
139
140 $body = wp_remote_retrieve_body( $res );
141 $json = json_decode( $body );
142
143 return $this->parse_json( $json );
144 }
145
146 /**
147 * Parse the JSON object as a JSON Feed.
148 *
149 * @param object $json The parsed JSON.
150 *
151 * @return array An array of Feed items.
152 */
153 public function parse_json( $json ) {
154 if ( ! isset( $json->items ) ) {
155 return null;
156 }
157
158 $feed_items = array();
159 foreach ( $json->items as $item ) {
160 $feed_item = new Feed_Item(
161 array(
162 'permalink' => $item->url,
163 )
164 );
165
166 if ( isset( $item->content_html ) ) {
167 $feed_item->content = $item->content_html;
168 } elseif ( isset( $item->content_text ) ) {
169 $feed_item->content = $item->content_text;
170 }
171
172 if ( isset( $item->title ) ) {
173 $feed_item->title = $item->title;
174 }
175 if ( isset( $item->authors ) && isset( $item->authors[0] ) && is_object( $item->authors[0] ) ) {
176 $feed_item->author = $item->authors[0]->name;
177 }
178 if ( isset( $item->date_published ) ) {
179 $feed_item->date = $item->date_published;
180 }
181 if ( isset( $item->date_modified ) ) {
182 $feed_item->updated_date = $item->date_modified;
183 if ( ! isset( $feed_item->date ) ) {
184 $feed_item->date = $feed_item->updated_date;
185 }
186 }
187 $feed_items[] = $feed_item;
188 }
189
190 return $feed_items;
191 }
192 }
193