-
Notifications
You must be signed in to change notification settings - Fork 2
/
pocket-to-wordpress.php
487 lines (390 loc) · 16.6 KB
/
pocket-to-wordpress.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
/**
* Plugin Name: Pocket to WordPress
* Plugin URI: https://wordpress.org/plugins/ @TODO update
* Description: Moves Pocket stuff to WordPress. @TODO update
* Version: 1.0
* Author: Rachel Carden
* Author URI: http://bamadesigner.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: pocket-to-wp
* Domain Path: /languages @TODO setup
*/
/**
* This adventure into the Pocket API was inspired
* because I wanted to get all of my Pocket items
* that were tagged "reading" and display them as
* a reading list on my WordPress site.
*
* Kudos to Rob Neu (@rob_neu) for some helpful tweaks.
*
* This code will cache the item data in a transient
* at a default time of 2 hours. You can tweak
* this time with the 'store_items' argument.
*
* You'll need a Pocket consumer key and access token
* in order to use their API. Visit https://getpocket.com/developer/
* to register an app and get a key.
*
* You can then use https://github.com/jshawl/pocket-oauth-php
* to get your access token.
*
* If you don't mind trusting some random site, you can use
* http://reader.fxneumann.de/plugins/oneclickpocket/auth.php
* to get your access token a lot quicker.
*
* Then use the shortcode [pocket_items] or the function
* get_pocket_items_html() to display your pocket items.
*
* Or you can use get_pocket_items() to retrieve the item
* data and display as you like.
*/
add_shortcode( 'pocket_items', 'get_pocket_items_html' );
/**
* Shortcode and function to print your Pocket items.
*
* Pass a 'tag' to get category-specific Pocket items.
* It seems they only allow one tag at a time.
*
* @TODO: Get your Pocket authentication info.
*
* You'll need a Pocket consumer key and access token
* in order to use their API. Visit https://getpocket.com/developer/
* to register an app and get a key.
*
* You can then use https://github.com/jshawl/pocket-oauth-php
* to get your access token.
*
* You can get more info in their docs: https://getpocket.com/developer/docs/authentication
*/
/**
* Returns markup for all pocket items.
*
* @access public
* @param array - $args - the arguments to create/format the list
* @return string - $pocket_html - the items markup
*/
function get_pocket_items_html( $args = array() ) {
// Handle the arguments
$defaults = array(
'key' => null,
'token' => null,
'tag' => NULL,
'store_items' => '7200' // Will cache the item for 2 hours by default. Set to false for no cache.
);
extract( wp_parse_args( $args, $defaults ), EXTR_OVERWRITE );
// Build html
$pocket_html = NULL;
// Get the items
$pocket_items = get_pocket_items( shortcode_atts( $defaults, $args ) );
// If we have no items
if ( ! ( isset( $pocket_items ) && is_array( $pocket_items ) ) )
return NULL;
// Start building the HTML
$pocket_html .= '<div class="pocket-items">';
// Loop through each item
foreach( $pocket_items as $item ) {
$pocket_html .= get_pocket_item_html( $item );
}
$pocket_html .= '</div>';
return $pocket_html;
}
/**
* Returns markup for individual pocket items.
*
* @access public
* @param array - $item - the data for the item to be displayed
* @return string - $pocket_html - the item's markup
*/
function get_pocket_item_html( $item ) {
// Make sure the item is formatted
if ( ! isset( $item->is_formatted ) )
$item = get_pocket_item_formatted_data( $item );
// Make sure we at least have an item ID and a title
if ( ! ( isset( $item->item_id ) && isset( $item->title ) ) )
return null;
// Start the item
$pocket_html = '<div id="pocket-' . $item->item_id . '" class="pocket-item' . ( $item->has_image && $item->image_src ? ' has-image' : NULL ) . '">';
// Add image
$pocket_html .= $item->has_image && $item->image_src ? '<div class="pocket-image" style="background-image: url(' . esc_url( $item->image_src ) . ');"></div>' : NULL;
// Add the main stuff
$pocket_html .= '<div class="pocket-main">';
// Add the title
$pocket_html .= '<h3 class="pocket-title">';
// Add the URL
if ( $item->url ) {
$pocket_html .= '<a href="' . esc_url( $item->url ) . '" target="_blank">' . esc_html( $item->title ) . '</a>';
} else {
$pocket_html .= esc_html( $item->title );
}
// Close the title
$pocket_html .= '</h3>';
// Add meta data
$pocket_html .= '<div class="pocket-meta">';
// Show when it was added
$pocket_html .= ! empty( $item->time_added ) ? '<span class="date-added meta-section">' . date( 'M\. j, Y', esc_attr( $item->time_added ) ) . '</span>' : NULL;
// Only add the first author
if ( $item->authors ) {
foreach( $item->authors as $author ) {
if ( isset( $author[ 'name' ] ) ) {
$pocket_html .= '<span class="author meta-section">';
// If we have a URL
if ( isset( $author[ 'url' ] ) ) {
$pocket_html .= '<a href="' . esc_url( $author[ 'url' ] ) . '">' . esc_html( $author[ 'name' ] ) . '</a>';
} else {
$pocket_html .= esc_html( $author[ 'name' ] );
}
$pocket_html .= '</span>';
break;
}
}
}
// Show the word count
$pocket_html .= $item->word_count > 0 ? '<span class="word-count meta-section">' . $item->word_count . ' words</span>' : NULL;
$pocket_html .= '</div>';
$pocket_html .= '<div class="pocket-excerpt">';
// Add excerpt
if ( isset( $item->excerpt ) && ! empty( $item->excerpt ) )
$pocket_html .= wpautop( $item->excerpt, false );
$pocket_html .= '</div>';
// Add tag list
if ( isset( $item->tags ) && ! empty( $item->tags ) ) {
// Start the list
$pocket_html .= '<ul class="pocket-item-tags">';
foreach( $item->tags as $tag => $tag_info ) {
$pocket_html .= '<li>' . esc_html( $tag ) . '</li>';
}
// End the list
$pocket_html .= '</ul>';
}
$pocket_html .= '</div> <!-- .pocket-main -->';
$pocket_html .= '</div><!-- #pocket-' . $item->item_id . ' -->';
return $pocket_html;
}
/**
* Inserts a Pocket item into a WordPress post.
*
* @access public
* @param array - $item - the item which holds the data we need to format
* @param array - $args - the arguments for creating the post
* @return boolean - whether or not the post was created
*/
function insert_pocket_item_to_post( $item, $args = array() ) {
global $wpdb;
// Handle the arguments
$defaults = array(
'post_type' => 'post',
'post_author' => null,
'post_status' => 'publish',
'tags_taxonomy_name'=> 'post_tag',
);
$args = wp_parse_args( $args, $defaults );
// Make sure the item is formatted
if ( ! isset( $item->is_formatted ) )
$item = get_pocket_item_formatted_data( $item );
// See if a post with this item ID already exists
$current_post_id = $wpdb->get_var( "SELECT posts.ID FROM {$wpdb->postmeta} pmeta INNER JOIN {$wpdb->posts} posts ON posts.ID = pmeta.post_id and posts.post_type = '{$args['post_type']}' AND posts.post_status = 'publish' WHERE pmeta.meta_key = '_pocket_item_id' AND pmeta.meta_value = '{$item->item_id}'" );
// If it already exists, get out of here
if ( $current_post_id > 0 )
return false;
// Create the post args
$post_args = array(
'post_type' => $args[ 'post_type' ],
'post_author' => $args[ 'post_author' ],
'post_status' => $args[ 'post_status' ],
'post_title' => apply_filters( 'pocket_item_post_title', $item->title, $item, $args ),
'post_name' => apply_filters( 'pocket_item_post_name', '', $item, $args ),
'post_content' => apply_filters( 'pocket_item_post_content', $item->excerpt, $item, $args ),
);
// When was the post added? - They send GMT
if ( isset( $item->time_added ) ) {
$post_args[ 'post_date_gmt' ] = date( 'Y-m-d H:i:s', $item->time_added );
}
// Filter the tags taxonomy name
$tags_taxonomy_name = apply_filters( 'pocket_item_tags_taxonomy_name', $args[ 'tags_taxonomy_name' ], $item, $args );
// Store the tags
if ( isset( $item->tags ) && is_array( $item->tags ) ) {
// Build tags array
$tags_array = array();
// Collect the tags
foreach( $item->tags as $tag ) {
$tags_array[] = $tag[ 'tag' ];
}
// Add tags
if ( ! empty( $tags_array ) ) {
$post_args[ 'tax_input' ] = array(
$tags_taxonomy_name => $tags_array,
);
}
}
// Insert the post
if ( $new_post_id = wp_insert_post( $post_args ) ) {
// @TODO
// Store authors separately?
// Store item post meta
$item_post_meta = array(
'_pocket_item_id' => $item->item_id,
'_pocket_item_url' => $item->url,
'_pocket_item_word_count' => $item->word_count,
'_pocket_item_time_read' => $item->time_read,
'_pocket_item_is_article' => $item->is_article ? true : false,
'_pocket_item_is_favorite' => $item->favorite ? true : false,
'_pocket_item_has_video' => $item->has_video ? true : false,
'_pocket_item_is_video' => $item->is_video ? true : false,
'_pocket_item_has_image' => $item->has_image ? true : false,
'_pocket_item_is_image' => $item->is_image ? true : false,
'_pocket_item_image_src' => $item->image_src,
'_pocket_item_data' => $item,
);
foreach( $item_post_meta as $meta_key => $meta_value ) {
add_post_meta( $new_post_id, $meta_key, $meta_value, true );
}
return true;
}
return false;
}
/**
* Returns a formatted array of data about a given Pocket item.
*
* @access public
* @param array - $item - the item which holds the data we need to format
* @return array - $data - the item's data attributes
*/
function get_pocket_item_formatted_data( $item ) {
// Convert to object
$item = (object) $item;
// Create the data
$data = (object) array(
'item_id' => ! empty( $item->item_id ) ? $item->item_id : null,
'resolved_id' => ! empty( $item->resolved_id ) ? $item->resolved_id : null,
'url' => ! empty( $item->given_url ) ? $item->given_url : null,
'resolved_url' => ! empty( $item->resolved_url ) ? $item->resolved_url : null,
'title' => ! empty( $item->given_title ) ? $item->given_title : null,
'resolved_title'=> ! empty( $item->resolved_title ) ? $item->resolved_title : null,
'time_added' => ! empty( $item->time_added ) ? $item->time_added : false,
'time_updated' => ! empty( $item->time_updated ) ? $item->time_updated : false,
'time_read' => ! empty( $item->time_read ) ? $item->time_read : false,
'time_favorited'=> ! empty( $item->time_favorited ) ? $item->time_favorited : false,
'favorite' => ! empty( $item->favorite ) ? $item->favorite : null,
'status' => ! empty( $item->status ) ? $item->status : null,
'word_count' => ! empty( $item->word_count ) ? $item->word_count : false,
'authors' => ! empty( $item->authors ) ? $item->authors : false,
'is_article' => ! empty( $item->is_article ) ? $item->is_article : false,
'has_video' => ! empty( $item->has_video ) ? $item->has_video : false,
'is_video' => ! empty( $item->has_video ) && 2 == $item->has_video ? true : false,
'has_image' => ! empty( $item->has_image ) ? $item->has_image : false,
'is_image' => ! empty( $item->has_image ) && 2 == $item->has_image ? true : false,
'excerpt' => ! empty( $item->excerpt ) ? $item->excerpt : null,
'tags' => ! empty( $item->tags ) ? $item->tags : false,
'image_src' => false,
'images' => false,
'is_formatted' => true,
);
// Make sure we have a title
if ( empty( $data->title ) ) {
if ( isset( $data->url ) ) {
$data->title = $data->url;
} else if ( isset( $data->resolved_url ) ) {
$data->title = $data->resolved_url;
}
}
// If we has_image, get the image src
if ( $data->has_image && ! empty( $item->image['src'] ) ) {
// Store image src
$data->image_src = $item->image[ 'src' ];
// Store images
if ( ! empty( $item->images ) )
$data->images = $item->images;
}
return $data;
}
/**
* Get Pocket items using Pocket's API.
*
* @TODO: Get your Pocket authentication info.
*
* You'll need a Pocket consumer key and access token
* in order to use their API. Visit https://getpocket.com/developer/
* to register an app and get a key.
*
* You can then use https://github.com/jshawl/pocket-oauth-php
* to get your access token.
*
* You can get more info in their docs: https://getpocket.com/developer/docs/authentication
*
* @access public
* @param $args - array - arguments to be passed to the API
* @return array - the items, false if something went wrong
*/
function get_pocket_items( $args = array() ) {
// Handle the arguments
$defaults = array(
'key' => null,
'token' => null,
'tag' => NULL, // Pass a tag to get category-specific Pocket items. It seems they only allow one tag at a time.
'count' => 10,
'store_items' => '7200' // Will cache the item for 2 hours by default. Set to false for no cache.
);
$args = wp_parse_args( $args, $defaults );
// Bail if we don't have the required access token and consumer key data.
if ( ! isset( $args[ 'key' ], $args[ 'token' ] ) ) {
return false;
}
// Build request args
$pocket_request_args = array(
'consumer_key' => $args[ 'key' ],
'access_token' => $args[ 'token' ],
'tag' => $args[ 'tag' ],
'detailType' => 'complete',
'state' => 'all',
'count' => $args[ 'count' ],
);
// If we're set to store the items...
if ( $args[ 'store_items' ] !== false && $args[ 'store_items' ] > 0 ) {
// See if we have data in our transient data and that it matches our args
$pocket_transient_name = 'my_pocket_reading_list';
$pocket_transient_args_name = 'my_pocket_reading_list_args';
// If we have cached Pocket items...
if ( ( $transient_pocket_items = get_transient( $pocket_transient_name ) )
&& $transient_pocket_items !== false ) {
// Check the args to see if they match...
if ( ( $transient_pocket_item_args = get_transient( $pocket_transient_args_name ) )
&& $transient_pocket_item_args == $pocket_request_args ) {
// Return the cached Pocket items
return $transient_pocket_items;
}
}
}
// Make our Pocket request
$pocket_request = get_pocket_response( 'https://getpocket.com/v3/get', $pocket_request_args );
// Bail if something went wrong with the request.
if ( ! $pocket_request || empty( $pocket_request['list'] ) ) {
return false;
}
// If we're set to store items, store the data and args for the set transient time
if ( $args[ 'store_items' ] !== false && $args[ 'store_items' ] > 0 ) {
set_transient( $pocket_transient_name, $pocket_request[ 'list' ], $args[ 'store_items' ] );
set_transient( $pocket_transient_args_name, $pocket_request_args, $args[ 'store_items' ] );
}
// Return the items
return $pocket_request[ 'list' ];
}
/**
* Get a response from the Pocket API.
*
* @access public
* @param $url - string - the API endpoint URL
* @param $post - array - data to be passed to the API
* @return mixed false if there's an error, otherwise an array of API data
*/
function get_pocket_response( $url, $post ) {
// Get the response
$response = wp_safe_remote_post( $url, array( 'body' => $post ) );
// Check for an error
if ( is_wp_error( $response ) ) {
return false;
}
// Return the response
return json_decode( wp_remote_retrieve_body( $response ), true );
}