Skip to content

Instantly share code, notes, and snippets.

@bamadesigner
Last active April 22, 2020 16:04
Show Gist options
  • Save bamadesigner/2147ac63258b137c6613 to your computer and use it in GitHub Desktop.
Save bamadesigner/2147ac63258b137c6613 to your computer and use it in GitHub Desktop.
Get Your Pocket Items (And Filter By Tag) to List On Your Website
<?php
HAS BEEN MOVED TO https://github.com/bamadesigner/pocket-to-wordpress.
/**
* 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 website.
*
* This was written for a WordPress site but only a
* few parts would need to be tweaked/removed and
* you could use it on any site.
*
* 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 ) {
// Convert to object
$item = (object) $item;
// Set the item ID
$item_id = isset( $item->item_id ) && ! empty( $item->item_id ) ? $item->item_id : ( isset( $item->resolved_id ) && ! empty( $item->resolved_id ) ? $item->resolved_id : NULL );
// Set the URL
$item_url = isset( $item->given_url ) && ! empty( $item->given_url ) ? $item->given_url : ( isset( $item->resolved_url ) && ! empty( $item->resolved_url ) ? $item->resolved_url : NULL );
// Set the title
$item_title = isset( $item->given_title ) && ! empty( $item->given_title ) ? $item->given_title : ( isset( $item->resolved_title ) && ! empty( $item->resolved_title ) ? $item->resolved_title : $item_url );
// Set the time added
$item_time_added = isset( $item->time_added ) ? $item->time_added : false;
// Set the time updated
//$item_time_updated = isset( $item->time_updated ) ? $item->time_updated : false;
// Do we have authors?
$item_authors = isset( $item->authors ) ? $item->authors : false;
// Do we have an image?
$item_has_image = isset( $item->has_image ) ? $item->has_image : false;
$item_image_src = $item_has_image && isset( $item->image ) && isset( $item->image[ 'src'] ) ? $item->image[ 'src' ] : false;
// What's the word count?
$item_word_count = isset( $item->word_count ) ? $item->word_count : false;
// Start the item
$pocket_html = '<div id="pocket-' . $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(' . $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="' . $item_url . '" target="_blank">' . $item_title . '</a>';
} else {
$pocket_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', $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="' . $author[ 'url' ] . '">' . $author[ 'name' ] . '</a>';
} else {
$pocket_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 .= "<p>{$item->excerpt}</p>";
$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>' . $tag . '</li>';
}
// End the list
$pocket_html .= '</ul>';
}
$pocket_html .= '</div>';
$pocket_html .= '</div>';
return $pocket_html;
}
/**
* 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
*/
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.
);
extract( wp_parse_args( $args, $defaults ), EXTR_OVERWRITE );
// Build request args
$pocket_request_args = array(
'consumer_key' => $key,
'access_token' => $token,
'tag' => $tag,
'detailType' => 'complete',
'state' => 'all',
'count' => $count,
);
// If we're set to store the items...
if ( $store_items !== false && $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 !== false && $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 );
// If we have results...
if ( isset( $pocket_request ) && is_array( $pocket_request ) && isset( $pocket_request[ 'list' ] ) && $pocket_request[ 'list' ] ) {
// If we're set to store items, store the data and args for the set transient time
if ( $store_items !== false && $store_items > 0 ) {
set_transient( $pocket_transient_name, $pocket_request[ 'list' ], $store_items );
set_transient( $pocket_transient_args_name, $pocket_request_args, $store_items );
}
// Return the items
return $pocket_request[ 'list' ];
}
return false;
}
/**
* Get a response from the Pocket API.
*
* @access public
* @param $url 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 );
}
@robneu
Copy link

robneu commented Jun 8, 2015

Sounds good! I wish gists had some way to send notifications or something... I noticed a couple of the item data points didn't make it into the data array when I was moving things around, so I updated my comment with the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment