[custom_search]
<add_shortcode( 'pocket_items', 'pocket_items_shortcode' );

function pocket_items_shortcode( $atts = array() ) {

    $defaults = array(
        'key'         => '84232-1c30b51dfc7eb5b29a0b2b90',
        'token'       => 'a4ed6383-e2bf-794f-8ebf-511100',
        'tag'         => today,
        'store_items' => '7200',
    );

    $pocket_items = get_pocket_items( shortcode_atts( $defaults, $atts ) );


    if ( empty( $pocket_items ) ) {
        return null;
    }

    $output = '<div class="pocket-items">';

    foreach ( $pocket_items as $item ) {
        $output .= get_pocket_item_html( get_pocket_item_data( $item ) );
    }

    $output .= '</div><!-- .pocket-items -->';

    return $output;
}

function get_pocket_item_data( $item ) {
    $item = (object) $item;

    $data = array(
        'id'         => ! empty( $item->resolved_id )    ? $item->resolved_id:    null,
        'url'        => ! empty( $item->resolved_url )   ? $item->resolved_url:   null,
        'title'      => ! empty( $item->resolved_title ) ? $item->resolved_title: null,
        'time_added' => ! empty( $item->time_added )     ? $item->time_added:     false,
        'authors'    => ! empty( $item->authors )        ? $item->authors:        false,
        'word_count' => ! empty( $item->word_count )     ? $item->word_count:     false,
        'has_image'  => ! empty( $item->has_image )      ? $item->has_image:      false,
        'excerpt'    => ! empty( $item->excerpt )        ? $item->excerpt:        false,
        'tags'       => ! empty( $item->tags )           ? $item->tags:           false,
        'image_src'  => false,
    );

    if ( ! empty( $item->item_id ) ) {
        $data['id'] = $item->item_id;
    }

    if ( ! empty( $item->given_url ) ) {
        $data['url'] = $item->given_url;
    }

    if ( ! empty( $item->given_title ) ) {
        $data['title'] = $item->given_title;
    }

    if ( empty( $data['title'] ) ) {
        $data['title'] = $data['url'];
    }

    if ( ! empty( $item->image['src'] ) ) {
        $data['image_src'] = $item->image['src'];
    }

    return $data;
}

function get_pocket_item_html( $data ) {
    $img_src = $data['image_src'] ? $data['image_src'] : false;
    $class   = 'pocket-item';
    $title   = $data['title'];

    if ( $img_src ) {
        $class .= ' has-image';
    }

    if ( $url = $data['url'] ) {
        $title = '<a href="' . esc_url( $url ) . '" target="_blank">' . esc_html( $title ) . '</a>';
    }

    $output = '<div id="pocket-' . absint( $data['id'] ) . '" class="' . $class . '">';

    if ( $img_src ) {
        $output .= '<div class="pocket-image" style="background-image: url(' . esc_url( $img_src ) . ');"></div>';
    }

    $output .= '<div class="pocket-main">';

    $output .= '<h3 class="pocket-title">' . $title . '</h3>';

    $output .= '<div class="pocket-meta">';

    if ( $time = $data['time_added'] ) {
        $output .= '<span class="date-added meta-section">';
        $output .= date( 'M\. j, Y', esc_attr( $time ) );
        $output .= '</span>';
    }

    if ( $authors = $data['authors'] ) {
        foreach ( (array) $authors as $author ) {
            if ( isset( $author['name'] ) ) {

                $name = $author['name'];
                if ( isset( $author['url'] ) ) {
                    $name = '<a href="' . esc_url( $author['url'] ) . '">' . esc_html( $name ) . '</a>';
                }

                $output .= '<span class="author meta-section">' . $name . '</span>';

                break;
            }
        }
    }

    if ( $word_count = $data['word_count'] ) {
        $output .= '<span class="word-count meta-section">';
        $output .= absint( $word_count ) . ' words';
        $output .= '</span>';
    }

    $output .= '</div><!-- .pocket-meta -->';

    if ( $excerpt = $data['excerpt'] ) {
        $output .= '<div class="pocket-excerpt">';
        $output .= wpautop( $excerpt, false );
        $output .= '</div><!-- .pocket-excerpt -->';
    }

    if ( $tags = $data['tags'] ) {
        $output .= '<ul class="pocket-item-tags">';
        foreach ( $tags as $tag => $tag_info ) {
            $output .= '<li>' . esc_html( $tag ) . '</li>';
        }
        $output .= '</ul>';
    }

    $output .= '</div><!-- .pocket-main -->';

    $output .= '</div><!-- #pocket-' . absint( $data['id'] ) . ' -->';

    return $output;
}

function get_pocket_items( $args = array() ) {
    $defaults = array(
        'key'         => null,
        'token'       => null,
        'tag'         => null,
        'count'       => 10,
        'store_items' => '7200',
    );

    $args = wp_parse_args( $args, $defaults );

    if ( ! isset( $args['key'], $args['token'] ) ) {
        return false;
    }

   
    $request_args = array(
        'consumer_key' => $args['key'],
        'access_token' => $args['token'],
        'tag'          => $args['tag'],
        'detailType'   => 'complete',
        'state'        => 'all',
        'count'        => $args['count'],
    );

    $cache_tag      = 'my_pocket_reading_list';
    $cache_args_tag = 'my_pocket_reading_list_args';

    if ( ! empty( $args['store_items'] ) ) {
        $cache      = get_transient( $cache_tag );
        $cache_args = get_transient( $cache_args_tag );

   
        if ( ! empty( $cache ) && $cache_args === $request_args ) {
            return $cache;
        }
    }

    $request = get_pocket_response( 'https://getpocket.com/v3/get', $request_args );

    if ( ! $request || empty( $request['list'] ) ) {
        return false;
    }

    if ( ! empty( $args['store_items'] ) ) {
        set_transient( $cache_tag, $request['list'], $args['store_items'] );
        set_transient( $cache_args_tag, $request_args, $args['store_items'] );
    }

    return $request['list'];
}


function get_pocket_response( $url, $post ) {
    $response = wp_safe_remote_post( $url, array( 'body' => $post ) );

    if ( is_wp_error( $response ) ) {
        return false;
    }

    return json_decode( wp_remote_retrieve_body( $response ), true );
}
Facebook
Twitter
Email
Print