| 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 |
* Determines if this is a supported feed and to what degree we feel it's supported. |
| 27 |
* |
| 28 |
* @param string $url The url. |
| 29 |
* @param string $mime_type The mime type. |
| 30 |
* @param string $title The title. |
| 31 |
* @param string|null $content The content, it can't be assumed that it's always available. |
| 32 |
* |
| 33 |
* @return int Return 0 if unsupported, a positive value representing the confidence for the feed, use 10 if you're reasonably confident. |
| 34 |
*/ |
| 35 |
public function feed_support_confidence( $url, $mime_type, $title, $content = null ) { |
| 36 |
$confidence = 10; |
| 37 |
|
| 38 |
$rewritten = $this->rewrite_known_url( $url ); |
| 39 |
if ( $rewritten ) { |
| 40 |
$mime_type = $rewritten['type']; |
| 41 |
$confidence *= 2; |
| 42 |
} |
| 43 |
|
| 44 |
switch ( $mime_type ) { |
| 45 |
case 'application/feed+json': |
| 46 |
return $confidence; |
| 47 |
case 'application/json': |
| 48 |
if ( $content ) { |
| 49 |
return $confidence; |
| 50 |
|
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Rewrite known URLs to their RSS feeds. |
| 59 |
* |
| 60 |
* @param string $url The url. |
| 61 |
* |
| 62 |
* @return array An equivalent link array. |
| 63 |
*/ |
| 64 |
public function rewrite_known_url( $url ) { |
| 65 |
$host = parse_url( strtolower( $url ), PHP_URL_HOST ); |
| 66 |
|
| 67 |
switch ( $host ) { |
| 68 |
case 'micro.blog': |
| 69 |
if ( preg_match( '#/([^/]+)$#', $url, $m ) ) { |
| 70 |
return array( |
| 71 |
'title' => 'Micro.blog: ' . $m[1], |
| 72 |
'rel' => 'alternate', |
| 73 |
'type' => 'application/feed+json', |
| 74 |
'url' => 'https://micro.blog/posts/' . $m[1], |
| 75 |
); |
| 76 |
} |
| 77 |
return array(); |
| 78 |
} |
| 79 |
|
| 80 |
return array(); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Format the feed title and autoselect the posts feed. |
| 85 |
* |
| 86 |
* @param array $feed_details The feed details. |
| 87 |
* |
| 88 |
* @return array The (potentially) modified feed details. |
| 89 |
*/ |
| 90 |
public function update_feed_details( $feed_details ) { |
| 91 |
$rewritten = $this->rewrite_known_url( $feed_details['url'] ); |
| 92 |
if ( $rewritten ) { |
| 93 |
$feed_details = $rewritten; |
| 94 |
} |
| 95 |
|
| 96 |
return $feed_details; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Discover the feeds available at the URL specified. |
| 101 |
* |
| 102 |
* @param string $content The content for the URL is already provided here. |
| 103 |
* @param string $url The url to search. |
| 104 |
* |
| 105 |
* @return array A list of supported feeds at the URL. |
| 106 |
*/ |
| 107 |
public function discover_available_feeds( $content, $url ) { |
| 108 |
return array(); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Fetches a feed and returns the processed items. |
| 113 |
* |
| 114 |
* @param string $url The url. |
| 115 |
* @param User_Feed $user_feed The user feed. |
| 116 |
* |
| 117 |
* @return array An array of feed items. |
| 118 |
*/ |
| 119 |
public function fetch_feed( $url, User_Feed $user_feed = null ) { |
| 120 |
$args = array(); |
| 121 |
$res = wp_safe_remote_request( $url, $args ); |
| 122 |
|
| 123 |
if ( is_wp_error( $res ) ) { |
| 124 |
return $res; |
| 125 |
} |
| 126 |
|
| 127 |
$body = wp_remote_retrieve_body( $res ); |
| 128 |
$json = json_decode( $body ); |
| 129 |
|
| 130 |
return $this->parse_json( $json ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Parse the JSON object as a JSON Feed. |
| 135 |
* |
| 136 |
* @param object $json The parsed JSON. |
| 137 |
* |
| 138 |
* @return array An array of Feed items. |
| 139 |
*/ |
| 140 |
public function parse_json( $json ) { |
| 141 |
if ( ! isset( $json->items ) ) { |
| 142 |
return null; |
| 143 |
} |
| 144 |
|
| 145 |
$feed_items = array(); |
| 146 |
foreach ( $json->items as $item ) { |
| 147 |
$feed_item = new Feed_Item( |
| 148 |
array( |
| 149 |
'permalink' => $item->url, |
| 150 |
) |
| 151 |
); |
| 152 |
|
| 153 |
if ( isset( $item->content_html ) ) { |
| 154 |
$feed_item->content = $item->content_html; |
| 155 |
} elseif ( isset( $item->content_text ) ) { |
| 156 |
$feed_item->content = $item->content_text; |
| 157 |
} |
| 158 |
|
| 159 |
if ( isset( $item->title ) ) { |
| 160 |
$feed_item->title = $item->title; |
| 161 |
} |
| 162 |
if ( isset( $item->authors ) && isset( $item->authors[0] ) && is_object( $item->authors[0] ) ) { |
| 163 |
$feed_item->author = $item->authors[0]->name; |
| 164 |
} |
| 165 |
if ( isset( $item->date_published ) ) { |
| 166 |
$feed_item->date = $item->date_published; |
| 167 |
} |
| 168 |
if ( isset( $item->date_modified ) ) { |
| 169 |
$feed_item->updated_date = $item->date_modified; |
| 170 |
if ( ! isset( $feed_item->date ) ) { |
| 171 |
$feed_item->date = $feed_item->updated_date; |
| 172 |
} |
| 173 |
} |
| 174 |
$feed_items[] = $feed_item; |
| 175 |
} |
| 176 |
|
| 177 |
return $feed_items; |
| 178 |
} |
| 179 |
} |
| 180 |
|