Plugin Directory

Changeset 3310533


Ignore:
Timestamp:
06/12/2025 01:03:06 PM (10 months ago)
Author:
jakeparis
Message:

updates for nyt api & version bump for wp 6.8.1

Location:
bestseller-lists-from-new-york-times
Files:
5 deleted
3 edited
9 copied

Legend:

Unmodified
Added
Removed
  • bestseller-lists-from-new-york-times/tags/2.5.0/bestseller-lists-from-new-york-times.php

    r3242199 r3310533  
    44Plugin URI:  https://jakeparis.com/wordpress-plugins/
    55Description: Integrate bestseller lists from the New York Times into your own site with a user-friendly interface.
    6 Version:     2.4.2
     6Version:     2.5.0
    77Requires PHP: 7.4
    88Requires at least: 5.4
    9 Tested up to: 6.7.2
     9Tested up to: 6.8.1
    1010Author:      Jake Paris
    1111Author URI:  https://jakeparis.com/
     
    1515defined( 'ABSPATH' ) || die( 'Not allowed' );
    1616
    17 define( 'BSLNYT_PLUGIN_VERSION', '2.4.2' );
     17define( 'BSLNYT_PLUGIN_VERSION', '2.5.0' );
    1818define( 'BSLNYT_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
    1919define( 'BSLNYT_PLUGIN_URL', plugins_url( '/', __FILE__ ) );
  • bestseller-lists-from-new-york-times/tags/2.5.0/class-nytbestsellerlistings.php

    r2967544 r3310533  
    66    private static $options_name = 'nyt_bestseller_listings';
    77
    8     public static function setApiKey( $v ) {
     8    public static function setApiKey ( $v ) {
    99        $v = sanitize_text_field( $v );
     10
    1011        return update_option( self::$options_name . '_apiKey', $v );
    1112    }
    12     public static function getApiKey() {
     13
     14    public static function getApiKey () {
    1315        return get_option( self::$options_name . '_apiKey', false );
    1416    }
    1517
    16     public static function getDefaultList() {
     18    public static function getDefaultList () {
    1719        return 'hardcover-fiction';
    1820    }
    1921
    20     public static function setCatalogLinkFormat( $v ) {
    21         if ( ! $v )
     22    public static function setCatalogLinkFormat ( $v ) {
     23        if ( ! $v ) {
    2224            return delete_option( self::$options_name . '_CatalogLinkFormat' );
     25        }
    2326
    2427        $v = sanitize_text_field( $v );
     28
    2529        return update_option( self::$options_name . '_CatalogLinkFormat', $v );
    2630    }
    27     public static function getCatalogLinkFormat() {
     31
     32    public static function getCatalogLinkFormat () {
    2833        return get_option( self::$options_name . '_CatalogLinkFormat' );
    2934    }
    30     public static function formatCatalogLink( $args = [] ) {
     35
     36    public static function formatCatalogLink ( $args = [] ) {
    3137        $clink = self::getCatalogLinkFormat();
    32         if ( ! $clink )
     38        if ( ! $clink ) {
    3339            return false;
    34         if ( ! count( $args ) )
     40        }
     41        if ( ! count( $args ) ) {
    3542            return false;
     43        }
    3644        foreach ( $args as $field => $value ) {
    3745            $clink = str_replace( '{' . $field . '}', $value, $clink );
    3846        }
     47
    3948        return $clink;
    4049    }
    4150
    42     public static function getApiBase() {
     51    public static function getApiBase () {
    4352        return 'https://api.nytimes.com/svc/books/v3/lists/';
    4453    }
    4554
    46     public static function getApiUrl( $endpoint, $params= [] ) {
     55    public static function getApiUrl ( $endpoint, $params = [] ) {
    4756        $endpoint = ltrim( $endpoint, '/' );
    4857        $url = self::getApiBase();
    4958        $url .= $endpoint;
    50         if ( count( $params ) )
     59        if ( count( $params ) ) {
    5160            $url .= '?' . http_build_query( $params );
     61        }
     62
    5263        return $url;
    5364    }
     
    5566    /**
    5667     * Get results from the NYT api
    57      * @param  string  $endpoint   endpoint to hit on the api
    58      * @param bool $use_cache  Whether to use cached results for this call, if any exist
    59      * @param  array   $params     extra query params to send to the api
    60      * @param  boolean $json_decode set to false to leave results as a json-encoded
    61      *                             string
     68     *
     69     * @param string  $endpoint     endpoint to hit on the api
     70     * @param bool    $use_cache    Whether to use cached results for this call, if any exist
     71     * @param array   $params       extra query params to send to the api
     72     * @param boolean $json_decode  set to false to leave results as a json-encoded
     73     *                              string
     74     *
    6275     * @return mixed               object if $json_decode=true, otherwise a string
    6376     */
    64     public static function getResults( $endpoint, $use_cache=true, $params=[], $json_decode=true ) {
     77    public static function getResults ( $endpoint, $use_cache = true, $params = [], $json_decode = true ) {
    6578        $api_key = self::getApiKey();
    66         if ( ! $api_key )
     79        if ( ! $api_key ) {
    6780            return false;
    68         $params = array_merge($params, [
     81        }
     82        $params         = array_merge( $params, [
    6983            'api-key' => $api_key,
    70         ]);
    71         $transient_name = "nyt_bestseller_lists_{$endpoint}_" . md5( serialize($params) );
     84        ] );
     85        $transient_name = "nyt_bestseller_lists_{$endpoint}_" . md5( serialize( $params ) );
    7286        $stored_results = get_transient( $transient_name );
    73        
     87
    7488        if ( ! empty( $stored_results ) && $use_cache ) {
    7589            $result_body = $stored_results;
     
    8599        }
    86100
    87         if ( $json_decode )
     101        if ( $json_decode ) {
    88102            $result_body = json_decode( $result_body );
     103        }
    89104
    90105        return $result_body;
    91106    }
    92107
     108    public static function get_all_lists ( bool $use_cache = true ) {
     109        $cache_key = 'nyt_bestseller_lists';
     110        $lists     = get_transient( $cache_key );
     111        if ( ! $lists || ! $use_cache ) {
     112            $results = self::getResults( 'overview.json', false );
     113            if ( ! $results ) {
     114                return array();
     115            }
     116            if ( ! empty( $results->errors ) || $results?->status === 'ERROR' ) {
     117                return array();
     118            }
     119            $lists = $results?->results?->lists ?? [];
     120            $lists = array_map( function ( $list ) {
     121                return [
     122                    'display_name'      => $list->display_name,
     123                    'list_name'         => $list->list_name,
     124                    'list_name_encoded' => $list->list_name_encoded,
     125                    'list_id'           => $list->list_id,
     126                ];
     127            }, $lists );
     128            set_transient( $cache_key, $lists, WEEK_IN_SECONDS );
     129        }
     130
     131        return $lists;
     132    }
    93133
    94134    /**
     
    101141     *  if failure to fetch)
    102142     */
    103     public static function getListsDropdown( $selected_list = null, $use_cache = true ) {
    104         $results = self::getResults( 'names.json', $use_cache );
    105 
    106         if ( ! $results ) {
    107             return '';
    108         }
    109 
    110         if ( is_null( $selected_list ) )
     143    public static function getListsDropdown ( $selected_list = null, $use_cache = true ) {
     144        $lists = self::get_all_lists( $use_cache );
     145
     146        if ( is_null( $selected_list ) ) {
    111147            $selected_list = self::getDefaultList();
     148        }
     149
    112150        $out = '<div class="nyt-bestseller-listings-changeListWrap">Change List
    113151        <select class="js-nyt-bestseller-listings_changeList">';
    114         foreach ( $results->results as $list ) {
    115             $selected = ( $list->list_name_encoded == $selected_list )
     152        foreach ( $lists as $list ) {
     153            $selected = ( $list['list_name_encoded'] == $selected_list )
    116154                ? ' selected '
    117155                : '';
    118             $out .= "<option value='{$list->list_name_encoded}' {$selected}>{$list->list_name}</option>\n";
     156            $out      .= "<option value='{$list['list_name_encoded']}' {$selected}>{$list['list_name']}</option>\n";
    119157        }
    120158        $out .= '</select>
    121159        </div>';
     160
    122161        return $out;
    123162    }
     
    126165     * Get the book list from NYT api
    127166     *
    128      * @param  string  $list          the slug for the list
    129      * @param  boolean $use_cache whether to use the api-call cache (default:true)
    130      * @param  boolean $display_images should we display images (default:true)
    131      * 
     167     * @param string  $list            the slug for the list
     168     * @param boolean $use_cache      whether to use the api-call cache (default:true)
     169     * @param boolean $display_images should we display images (default:true)
     170     *
    132171     * @return string  The html for the list of bestsellers
    133172     */
    134     public static function getBookList( $list, $use_cache = true, $display_images=true ) {
     173    public static function getBookList ( $list, $use_cache = true, $display_images = true ) {
    135174        $endpoint = "current/{$list}.json";
    136175        $results = self::getResults( $endpoint, $use_cache );
     
    143182
    144183        foreach ( $results->results->books as $book ) {
    145 
    146184            $links = self::getBookLinks( $book );
    147185
     
    171209            }
    172210
    173                 $out .= '</div>
     211            $out .= '</div>
    174212                    ' . $image_html . '             
    175213            </div>';
    176 
    177         }
     214        }
     215
    178216        return $out;
    179217    }
    180218
    181     public static function getBookLinks( $book ) {
     219    public static function getBookLinks ( $book ) {
    182220        $links = [];
    183221
    184         if ( $book->sunday_review_link )
     222        if ( $book->sunday_review_link ) {
    185223            $links['New York Times Sunday Review'] = $book->sunday_review_link;
    186         if ( $book->book_review_link )
     224        }
     225        if ( $book->book_review_link ) {
    187226            $links['New York Times Book Review'] = $book->book_review_link;
    188         if ( $book->first_chapter_link )
     227        }
     228        if ( $book->first_chapter_link ) {
    189229            $links['First Chapter'] = $book->first_chapter_link;
    190         $catalog_link = self::formatCatalogLink([
     230        }
     231        $catalog_link = self::formatCatalogLink( [
    191232            'isbn' => $book->primary_isbn13,
    192233            'author' => $book->author,
    193234            'title' => strtolower( $book->title ),
    194         ]);
    195         if ( $catalog_link )
     235        ] );
     236        if ( $catalog_link ) {
    196237            $links['Library Catalog'] = $catalog_link;
     238        }
    197239
    198240        return $links;
  • bestseller-lists-from-new-york-times/tags/2.5.0/readme.txt

    r3242199 r3310533  
    4646
    4747== Changelog ==
     48
     49= 2.5.0 =
     50
     51* Update api calls to match changes in the New York Times API.
     52* Bump version compatibility to 6.8.1
    4853
    4954= 2.4.2 =
  • bestseller-lists-from-new-york-times/trunk/bestseller-lists-from-new-york-times.php

    r3242199 r3310533  
    44Plugin URI:  https://jakeparis.com/wordpress-plugins/
    55Description: Integrate bestseller lists from the New York Times into your own site with a user-friendly interface.
    6 Version:     2.4.2
     6Version:     2.5.0
    77Requires PHP: 7.4
    88Requires at least: 5.4
    9 Tested up to: 6.7.2
     9Tested up to: 6.8.1
    1010Author:      Jake Paris
    1111Author URI:  https://jakeparis.com/
     
    1515defined( 'ABSPATH' ) || die( 'Not allowed' );
    1616
    17 define( 'BSLNYT_PLUGIN_VERSION', '2.4.2' );
     17define( 'BSLNYT_PLUGIN_VERSION', '2.5.0' );
    1818define( 'BSLNYT_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
    1919define( 'BSLNYT_PLUGIN_URL', plugins_url( '/', __FILE__ ) );
  • bestseller-lists-from-new-york-times/trunk/class-nytbestsellerlistings.php

    r2967544 r3310533  
    66    private static $options_name = 'nyt_bestseller_listings';
    77
    8     public static function setApiKey( $v ) {
     8    public static function setApiKey ( $v ) {
    99        $v = sanitize_text_field( $v );
     10
    1011        return update_option( self::$options_name . '_apiKey', $v );
    1112    }
    12     public static function getApiKey() {
     13
     14    public static function getApiKey () {
    1315        return get_option( self::$options_name . '_apiKey', false );
    1416    }
    1517
    16     public static function getDefaultList() {
     18    public static function getDefaultList () {
    1719        return 'hardcover-fiction';
    1820    }
    1921
    20     public static function setCatalogLinkFormat( $v ) {
    21         if ( ! $v )
     22    public static function setCatalogLinkFormat ( $v ) {
     23        if ( ! $v ) {
    2224            return delete_option( self::$options_name . '_CatalogLinkFormat' );
     25        }
    2326
    2427        $v = sanitize_text_field( $v );
     28
    2529        return update_option( self::$options_name . '_CatalogLinkFormat', $v );
    2630    }
    27     public static function getCatalogLinkFormat() {
     31
     32    public static function getCatalogLinkFormat () {
    2833        return get_option( self::$options_name . '_CatalogLinkFormat' );
    2934    }
    30     public static function formatCatalogLink( $args = [] ) {
     35
     36    public static function formatCatalogLink ( $args = [] ) {
    3137        $clink = self::getCatalogLinkFormat();
    32         if ( ! $clink )
     38        if ( ! $clink ) {
    3339            return false;
    34         if ( ! count( $args ) )
     40        }
     41        if ( ! count( $args ) ) {
    3542            return false;
     43        }
    3644        foreach ( $args as $field => $value ) {
    3745            $clink = str_replace( '{' . $field . '}', $value, $clink );
    3846        }
     47
    3948        return $clink;
    4049    }
    4150
    42     public static function getApiBase() {
     51    public static function getApiBase () {
    4352        return 'https://api.nytimes.com/svc/books/v3/lists/';
    4453    }
    4554
    46     public static function getApiUrl( $endpoint, $params= [] ) {
     55    public static function getApiUrl ( $endpoint, $params = [] ) {
    4756        $endpoint = ltrim( $endpoint, '/' );
    4857        $url = self::getApiBase();
    4958        $url .= $endpoint;
    50         if ( count( $params ) )
     59        if ( count( $params ) ) {
    5160            $url .= '?' . http_build_query( $params );
     61        }
     62
    5263        return $url;
    5364    }
     
    5566    /**
    5667     * Get results from the NYT api
    57      * @param  string  $endpoint   endpoint to hit on the api
    58      * @param bool $use_cache  Whether to use cached results for this call, if any exist
    59      * @param  array   $params     extra query params to send to the api
    60      * @param  boolean $json_decode set to false to leave results as a json-encoded
    61      *                             string
     68     *
     69     * @param string  $endpoint     endpoint to hit on the api
     70     * @param bool    $use_cache    Whether to use cached results for this call, if any exist
     71     * @param array   $params       extra query params to send to the api
     72     * @param boolean $json_decode  set to false to leave results as a json-encoded
     73     *                              string
     74     *
    6275     * @return mixed               object if $json_decode=true, otherwise a string
    6376     */
    64     public static function getResults( $endpoint, $use_cache=true, $params=[], $json_decode=true ) {
     77    public static function getResults ( $endpoint, $use_cache = true, $params = [], $json_decode = true ) {
    6578        $api_key = self::getApiKey();
    66         if ( ! $api_key )
     79        if ( ! $api_key ) {
    6780            return false;
    68         $params = array_merge($params, [
     81        }
     82        $params         = array_merge( $params, [
    6983            'api-key' => $api_key,
    70         ]);
    71         $transient_name = "nyt_bestseller_lists_{$endpoint}_" . md5( serialize($params) );
     84        ] );
     85        $transient_name = "nyt_bestseller_lists_{$endpoint}_" . md5( serialize( $params ) );
    7286        $stored_results = get_transient( $transient_name );
    73        
     87
    7488        if ( ! empty( $stored_results ) && $use_cache ) {
    7589            $result_body = $stored_results;
     
    8599        }
    86100
    87         if ( $json_decode )
     101        if ( $json_decode ) {
    88102            $result_body = json_decode( $result_body );
     103        }
    89104
    90105        return $result_body;
    91106    }
    92107
     108    public static function get_all_lists ( bool $use_cache = true ) {
     109        $cache_key = 'nyt_bestseller_lists';
     110        $lists     = get_transient( $cache_key );
     111        if ( ! $lists || ! $use_cache ) {
     112            $results = self::getResults( 'overview.json', false );
     113            if ( ! $results ) {
     114                return array();
     115            }
     116            if ( ! empty( $results->errors ) || $results?->status === 'ERROR' ) {
     117                return array();
     118            }
     119            $lists = $results?->results?->lists ?? [];
     120            $lists = array_map( function ( $list ) {
     121                return [
     122                    'display_name'      => $list->display_name,
     123                    'list_name'         => $list->list_name,
     124                    'list_name_encoded' => $list->list_name_encoded,
     125                    'list_id'           => $list->list_id,
     126                ];
     127            }, $lists );
     128            set_transient( $cache_key, $lists, WEEK_IN_SECONDS );
     129        }
     130
     131        return $lists;
     132    }
    93133
    94134    /**
     
    101141     *  if failure to fetch)
    102142     */
    103     public static function getListsDropdown( $selected_list = null, $use_cache = true ) {
    104         $results = self::getResults( 'names.json', $use_cache );
    105 
    106         if ( ! $results ) {
    107             return '';
    108         }
    109 
    110         if ( is_null( $selected_list ) )
     143    public static function getListsDropdown ( $selected_list = null, $use_cache = true ) {
     144        $lists = self::get_all_lists( $use_cache );
     145
     146        if ( is_null( $selected_list ) ) {
    111147            $selected_list = self::getDefaultList();
     148        }
     149
    112150        $out = '<div class="nyt-bestseller-listings-changeListWrap">Change List
    113151        <select class="js-nyt-bestseller-listings_changeList">';
    114         foreach ( $results->results as $list ) {
    115             $selected = ( $list->list_name_encoded == $selected_list )
     152        foreach ( $lists as $list ) {
     153            $selected = ( $list['list_name_encoded'] == $selected_list )
    116154                ? ' selected '
    117155                : '';
    118             $out .= "<option value='{$list->list_name_encoded}' {$selected}>{$list->list_name}</option>\n";
     156            $out      .= "<option value='{$list['list_name_encoded']}' {$selected}>{$list['list_name']}</option>\n";
    119157        }
    120158        $out .= '</select>
    121159        </div>';
     160
    122161        return $out;
    123162    }
     
    126165     * Get the book list from NYT api
    127166     *
    128      * @param  string  $list          the slug for the list
    129      * @param  boolean $use_cache whether to use the api-call cache (default:true)
    130      * @param  boolean $display_images should we display images (default:true)
    131      * 
     167     * @param string  $list            the slug for the list
     168     * @param boolean $use_cache      whether to use the api-call cache (default:true)
     169     * @param boolean $display_images should we display images (default:true)
     170     *
    132171     * @return string  The html for the list of bestsellers
    133172     */
    134     public static function getBookList( $list, $use_cache = true, $display_images=true ) {
     173    public static function getBookList ( $list, $use_cache = true, $display_images = true ) {
    135174        $endpoint = "current/{$list}.json";
    136175        $results = self::getResults( $endpoint, $use_cache );
     
    143182
    144183        foreach ( $results->results->books as $book ) {
    145 
    146184            $links = self::getBookLinks( $book );
    147185
     
    171209            }
    172210
    173                 $out .= '</div>
     211            $out .= '</div>
    174212                    ' . $image_html . '             
    175213            </div>';
    176 
    177         }
     214        }
     215
    178216        return $out;
    179217    }
    180218
    181     public static function getBookLinks( $book ) {
     219    public static function getBookLinks ( $book ) {
    182220        $links = [];
    183221
    184         if ( $book->sunday_review_link )
     222        if ( $book->sunday_review_link ) {
    185223            $links['New York Times Sunday Review'] = $book->sunday_review_link;
    186         if ( $book->book_review_link )
     224        }
     225        if ( $book->book_review_link ) {
    187226            $links['New York Times Book Review'] = $book->book_review_link;
    188         if ( $book->first_chapter_link )
     227        }
     228        if ( $book->first_chapter_link ) {
    189229            $links['First Chapter'] = $book->first_chapter_link;
    190         $catalog_link = self::formatCatalogLink([
     230        }
     231        $catalog_link = self::formatCatalogLink( [
    191232            'isbn' => $book->primary_isbn13,
    192233            'author' => $book->author,
    193234            'title' => strtolower( $book->title ),
    194         ]);
    195         if ( $catalog_link )
     235        ] );
     236        if ( $catalog_link ) {
    196237            $links['Library Catalog'] = $catalog_link;
     238        }
    197239
    198240        return $links;
  • bestseller-lists-from-new-york-times/trunk/readme.txt

    r3242199 r3310533  
    4646
    4747== Changelog ==
     48
     49= 2.5.0 =
     50
     51* Update api calls to match changes in the New York Times API.
     52* Bump version compatibility to 6.8.1
    4853
    4954= 2.4.2 =
Note: See TracChangeset for help on using the changeset viewer.