-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathRSSImporterCommand.php
More file actions
235 lines (198 loc) · 5.84 KB
/
RSSImporterCommand.php
File metadata and controls
235 lines (198 loc) · 5.84 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
namespace Classifai\Command;
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
/**
* RSSImporterCommand provides basic support for importing RSS feeds
* into WordPress Posts.
*
* For development use only.
*/
// phpcs:ignore WordPressVIPMinimum.Classes.RestrictedExtendClasses.wp_cli
class RSSImporterCommand extends \WP_CLI_Command {
/**
* Import an RSS feed.
*
* @param array $args Arguments.
* @param array $opts Options.
*/
public function import( $args = [], $opts = [] ) {
$defaults = [
'limit' => 1,
];
$opts = wp_parse_args( $opts, $defaults );
$feeds = $this->get_rss_feeds();
$imported = 0;
$limit = $opts['limit'];
foreach ( $feeds as $feed ) {
$rss = $this->get_rss_feed( $feed );
$source = (string) $rss->channel->title;
$items = $rss->channel->item;
foreach ( $items as $item ) {
$item_info = $this->get_rss_item_info( $item );
$result = $this->import_post( $item_info, $source );
if ( $result && ++$imported >= $limit && ! empty( $limit ) ) {
break 2;
}
}
}
\WP_CLI::success( "Imported $imported posts." );
}
// Helpers.
/**
* Import a post.
*
* @param array $info The post info.
* @param string $source The post source.
*/
public function import_post( $info, $source ) {
if ( empty( $info['meta'] ) || empty( $info['meta']['content'] ) ) {
return false;
}
$post_date_gmt = gmdate( 'Y-m-d H:i:s', strtotime( $info['date'] ) );
$post = [
'post_title' => $info['title'],
'post_excerpt' => $info['description'],
'post_content' => $info['meta']['content'],
'post_status' => 'publish',
'post_date' => $post_date_gmt,
'post_date_gmt' => $post_date_gmt,
'post_author' => $this->get_post_author( $source ),
];
$result = wp_insert_post( $post, true );
if ( ! is_wp_error( $result ) ) {
if ( ! empty( $info['meta']['lead_image_url'] ) ) {
$this->import_thumbnail(
$result,
$info['meta']['lead_image_url'],
$info['meta']['excerpt']
);
}
update_post_meta( $result, 'imported_from_url', $info['link'] );
\WP_CLI::log( 'Imported: ' . $info['link'] );
return $result;
} else {
\WP_CLI::warning( 'Failed to import Post: ' . $result->get_error_message() );
return false;
}
}
/**
* Import a post thumbnail.
*
* @param int $post_id The post id.
* @param string $thumbnail URL of the image to download.
* @param string $description Description for the image.
*/
public function import_thumbnail( $post_id, $thumbnail, $description ) {
$attachment_id = media_sideload_image(
$thumbnail,
$post_id,
'',
'id'
);
if ( ! empty( $attachment_id ) ) {
$post = [
'ID' => $attachment_id,
'post_content' => $description,
];
wp_update_post( $post );
set_post_thumbnail( $post_id, $attachment_id );
}
}
/**
* Get information about an RSS item.
*
* @param object $item The item to get information on.
*/
public function get_rss_item_info( $item ) {
$info = [];
$info['title'] = (string) $item->title;
$info['description'] = (string) $item->description;
$info['date'] = (string) $item->pubDate; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$info['thumbnail'] = (string) $item->thumbnail->attributes['url'];
$info['link'] = (string) $item->link;
$info['meta'] = $this->get_url_meta( (string) $item->link );
return $info;
}
/**
* Get an RSS feed.
*
* @param string $feed_url The feed URL.
*/
public function get_rss_feed( $feed_url ) {
$rss = simplexml_load_file( $feed_url );
return $rss;
}
/**
* Get the RSS feeds.
*/
public function get_rss_feeds() {
return [
'http://feeds.bbci.co.uk/news/world/rss.xml',
'http://feeds.bbci.co.uk/news/business/rss.xml',
'http://feeds.bbci.co.uk/news/technology/rss.xml',
];
}
/**
* Get an author by name.
*
* @param string $name The author name.
*/
public function get_post_author( $name ) {
$slug = sanitize_title_with_dashes( $name );
$user = get_user_by( 'slug', $slug );
if ( $user ) {
return $user->ID;
} else {
$userdata = [
'display_name' => $name,
'user_login' => $slug,
'user_pass' => uniqid(),
];
$result = wp_insert_user( $userdata );
if ( ! is_wp_error( $result ) ) {
return $result;
} else {
\WP_CLI::log( 'Failed to insert user: ' . $result->get_error_message() );
return false;
}
}
}
/**
* Get meta for a URL.
*
* @param string $url The URL.
*/
public function get_url_meta( $url ) {
$options = [];
if ( empty( $options['headers'] ) ) {
$options['headers'] = [];
}
if ( defined( \MERCURY_PARSER_API_KEY ) && \MERCURY_PARSER_API_KEY ) {
$options['headers']['x-api-key'] = \MERCURY_PARSER_API_KEY;
}
$options['timeout'] = 60; // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
$request_url = 'https://mercury.postlight.com/parser?url=' . rawurlencode( $url );
$response = wp_remote_get( $request_url, $options ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
if ( ! is_wp_error( $response ) ) {
$body = wp_remote_retrieve_body( $response );
$json = json_decode( $body, true );
if ( json_last_error() === JSON_ERROR_NONE ) {
if ( ! empty( $json['content'] ) ) {
$allowed = wp_kses_allowed_html( 'post' );
unset( $allowed['img'] );
unset( $allowed['picture'] );
unset( $allowed['figure'] );
unset( $allowed['figurecaption'] );
$json['content'] = wp_kses( $json['content'], $allowed );
}
return $json;
} else {
return new \WP_Error( 'Invalid JSON: ' . json_last_error_msg(), $body );
}
} else {
return $response;
}
}
}