Changeset 3310533
- Timestamp:
- 06/12/2025 01:03:06 PM (10 months ago)
- Location:
- bestseller-lists-from-new-york-times
- Files:
-
- 5 deleted
- 3 edited
- 9 copied
-
tags/2.5.0 (copied) (copied from bestseller-lists-from-new-york-times/trunk)
-
tags/2.5.0/admin-page.php (copied) (copied from bestseller-lists-from-new-york-times/trunk/admin-page.php)
-
tags/2.5.0/bestseller-lists-from-new-york-times.php (copied) (copied from bestseller-lists-from-new-york-times/trunk/bestseller-lists-from-new-york-times.php) (2 diffs)
-
tags/2.5.0/blocks (deleted)
-
tags/2.5.0/build (copied) (copied from bestseller-lists-from-new-york-times/trunk/build)
-
tags/2.5.0/build/lists/render.php (copied) (copied from bestseller-lists-from-new-york-times/trunk/build/lists/render.php)
-
tags/2.5.0/class-nytbestsellerlistings.php (copied) (copied from bestseller-lists-from-new-york-times/trunk/class-nytbestsellerlistings.php) (7 diffs)
-
tags/2.5.0/functions.php (copied) (copied from bestseller-lists-from-new-york-times/trunk/functions.php)
-
tags/2.5.0/index.php (deleted)
-
tags/2.5.0/nytBestsellerListings.class.php (deleted)
-
tags/2.5.0/plugin-updates.php (copied) (copied from bestseller-lists-from-new-york-times/trunk/plugin-updates.php)
-
tags/2.5.0/plugin_updates.php (deleted)
-
tags/2.5.0/public-assets (deleted)
-
tags/2.5.0/readme.txt (copied) (copied from bestseller-lists-from-new-york-times/trunk/readme.txt) (1 diff)
-
trunk/bestseller-lists-from-new-york-times.php (modified) (2 diffs)
-
trunk/class-nytbestsellerlistings.php (modified) (7 diffs)
-
trunk/readme.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
bestseller-lists-from-new-york-times/tags/2.5.0/bestseller-lists-from-new-york-times.php
r3242199 r3310533 4 4 Plugin URI: https://jakeparis.com/wordpress-plugins/ 5 5 Description: Integrate bestseller lists from the New York Times into your own site with a user-friendly interface. 6 Version: 2. 4.26 Version: 2.5.0 7 7 Requires PHP: 7.4 8 8 Requires at least: 5.4 9 Tested up to: 6. 7.29 Tested up to: 6.8.1 10 10 Author: Jake Paris 11 11 Author URI: https://jakeparis.com/ … … 15 15 defined( 'ABSPATH' ) || die( 'Not allowed' ); 16 16 17 define( 'BSLNYT_PLUGIN_VERSION', '2. 4.2' );17 define( 'BSLNYT_PLUGIN_VERSION', '2.5.0' ); 18 18 define( 'BSLNYT_PLUGIN_PATH', plugin_dir_path( __FILE__ ) ); 19 19 define( 'BSLNYT_PLUGIN_URL', plugins_url( '/', __FILE__ ) ); -
bestseller-lists-from-new-york-times/tags/2.5.0/class-nytbestsellerlistings.php
r2967544 r3310533 6 6 private static $options_name = 'nyt_bestseller_listings'; 7 7 8 public static function setApiKey ( $v ) {8 public static function setApiKey ( $v ) { 9 9 $v = sanitize_text_field( $v ); 10 10 11 return update_option( self::$options_name . '_apiKey', $v ); 11 12 } 12 public static function getApiKey() { 13 14 public static function getApiKey () { 13 15 return get_option( self::$options_name . '_apiKey', false ); 14 16 } 15 17 16 public static function getDefaultList () {18 public static function getDefaultList () { 17 19 return 'hardcover-fiction'; 18 20 } 19 21 20 public static function setCatalogLinkFormat ( $v ) {21 if ( ! $v ) 22 public static function setCatalogLinkFormat ( $v ) { 23 if ( ! $v ) { 22 24 return delete_option( self::$options_name . '_CatalogLinkFormat' ); 25 } 23 26 24 27 $v = sanitize_text_field( $v ); 28 25 29 return update_option( self::$options_name . '_CatalogLinkFormat', $v ); 26 30 } 27 public static function getCatalogLinkFormat() { 31 32 public static function getCatalogLinkFormat () { 28 33 return get_option( self::$options_name . '_CatalogLinkFormat' ); 29 34 } 30 public static function formatCatalogLink( $args = [] ) { 35 36 public static function formatCatalogLink ( $args = [] ) { 31 37 $clink = self::getCatalogLinkFormat(); 32 if ( ! $clink ) 38 if ( ! $clink ) { 33 39 return false; 34 if ( ! count( $args ) ) 40 } 41 if ( ! count( $args ) ) { 35 42 return false; 43 } 36 44 foreach ( $args as $field => $value ) { 37 45 $clink = str_replace( '{' . $field . '}', $value, $clink ); 38 46 } 47 39 48 return $clink; 40 49 } 41 50 42 public static function getApiBase () {51 public static function getApiBase () { 43 52 return 'https://api.nytimes.com/svc/books/v3/lists/'; 44 53 } 45 54 46 public static function getApiUrl ( $endpoint, $params= [] ) {55 public static function getApiUrl ( $endpoint, $params = [] ) { 47 56 $endpoint = ltrim( $endpoint, '/' ); 48 57 $url = self::getApiBase(); 49 58 $url .= $endpoint; 50 if ( count( $params ) ) 59 if ( count( $params ) ) { 51 60 $url .= '?' . http_build_query( $params ); 61 } 62 52 63 return $url; 53 64 } … … 55 66 /** 56 67 * 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 * 62 75 * @return mixed object if $json_decode=true, otherwise a string 63 76 */ 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 ) { 65 78 $api_key = self::getApiKey(); 66 if ( ! $api_key ) 79 if ( ! $api_key ) { 67 80 return false; 68 $params = array_merge($params, [ 81 } 82 $params = array_merge( $params, [ 69 83 '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 ) ); 72 86 $stored_results = get_transient( $transient_name ); 73 87 74 88 if ( ! empty( $stored_results ) && $use_cache ) { 75 89 $result_body = $stored_results; … … 85 99 } 86 100 87 if ( $json_decode ) 101 if ( $json_decode ) { 88 102 $result_body = json_decode( $result_body ); 103 } 89 104 90 105 return $result_body; 91 106 } 92 107 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 } 93 133 94 134 /** … … 101 141 * if failure to fetch) 102 142 */ 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 ) ) { 111 147 $selected_list = self::getDefaultList(); 148 } 149 112 150 $out = '<div class="nyt-bestseller-listings-changeListWrap">Change List 113 151 <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 ) 116 154 ? ' selected ' 117 155 : ''; 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"; 119 157 } 120 158 $out .= '</select> 121 159 </div>'; 160 122 161 return $out; 123 162 } … … 126 165 * Get the book list from NYT api 127 166 * 128 * @param string $listthe slug for the list129 * @param boolean $use_cachewhether to use the api-call cache (default:true)130 * @param boolean $display_imagesshould 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 * 132 171 * @return string The html for the list of bestsellers 133 172 */ 134 public static function getBookList ( $list, $use_cache = true, $display_images=true ) {173 public static function getBookList ( $list, $use_cache = true, $display_images = true ) { 135 174 $endpoint = "current/{$list}.json"; 136 175 $results = self::getResults( $endpoint, $use_cache ); … … 143 182 144 183 foreach ( $results->results->books as $book ) { 145 146 184 $links = self::getBookLinks( $book ); 147 185 … … 171 209 } 172 210 173 $out .= '</div>211 $out .= '</div> 174 212 ' . $image_html . ' 175 213 </div>'; 176 177 } 214 } 215 178 216 return $out; 179 217 } 180 218 181 public static function getBookLinks ( $book ) {219 public static function getBookLinks ( $book ) { 182 220 $links = []; 183 221 184 if ( $book->sunday_review_link ) 222 if ( $book->sunday_review_link ) { 185 223 $links['New York Times Sunday Review'] = $book->sunday_review_link; 186 if ( $book->book_review_link ) 224 } 225 if ( $book->book_review_link ) { 187 226 $links['New York Times Book Review'] = $book->book_review_link; 188 if ( $book->first_chapter_link ) 227 } 228 if ( $book->first_chapter_link ) { 189 229 $links['First Chapter'] = $book->first_chapter_link; 190 $catalog_link = self::formatCatalogLink([ 230 } 231 $catalog_link = self::formatCatalogLink( [ 191 232 'isbn' => $book->primary_isbn13, 192 233 'author' => $book->author, 193 234 'title' => strtolower( $book->title ), 194 ] );195 if ( $catalog_link ) 235 ] ); 236 if ( $catalog_link ) { 196 237 $links['Library Catalog'] = $catalog_link; 238 } 197 239 198 240 return $links; -
bestseller-lists-from-new-york-times/tags/2.5.0/readme.txt
r3242199 r3310533 46 46 47 47 == 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 48 53 49 54 = 2.4.2 = -
bestseller-lists-from-new-york-times/trunk/bestseller-lists-from-new-york-times.php
r3242199 r3310533 4 4 Plugin URI: https://jakeparis.com/wordpress-plugins/ 5 5 Description: Integrate bestseller lists from the New York Times into your own site with a user-friendly interface. 6 Version: 2. 4.26 Version: 2.5.0 7 7 Requires PHP: 7.4 8 8 Requires at least: 5.4 9 Tested up to: 6. 7.29 Tested up to: 6.8.1 10 10 Author: Jake Paris 11 11 Author URI: https://jakeparis.com/ … … 15 15 defined( 'ABSPATH' ) || die( 'Not allowed' ); 16 16 17 define( 'BSLNYT_PLUGIN_VERSION', '2. 4.2' );17 define( 'BSLNYT_PLUGIN_VERSION', '2.5.0' ); 18 18 define( 'BSLNYT_PLUGIN_PATH', plugin_dir_path( __FILE__ ) ); 19 19 define( 'BSLNYT_PLUGIN_URL', plugins_url( '/', __FILE__ ) ); -
bestseller-lists-from-new-york-times/trunk/class-nytbestsellerlistings.php
r2967544 r3310533 6 6 private static $options_name = 'nyt_bestseller_listings'; 7 7 8 public static function setApiKey ( $v ) {8 public static function setApiKey ( $v ) { 9 9 $v = sanitize_text_field( $v ); 10 10 11 return update_option( self::$options_name . '_apiKey', $v ); 11 12 } 12 public static function getApiKey() { 13 14 public static function getApiKey () { 13 15 return get_option( self::$options_name . '_apiKey', false ); 14 16 } 15 17 16 public static function getDefaultList () {18 public static function getDefaultList () { 17 19 return 'hardcover-fiction'; 18 20 } 19 21 20 public static function setCatalogLinkFormat ( $v ) {21 if ( ! $v ) 22 public static function setCatalogLinkFormat ( $v ) { 23 if ( ! $v ) { 22 24 return delete_option( self::$options_name . '_CatalogLinkFormat' ); 25 } 23 26 24 27 $v = sanitize_text_field( $v ); 28 25 29 return update_option( self::$options_name . '_CatalogLinkFormat', $v ); 26 30 } 27 public static function getCatalogLinkFormat() { 31 32 public static function getCatalogLinkFormat () { 28 33 return get_option( self::$options_name . '_CatalogLinkFormat' ); 29 34 } 30 public static function formatCatalogLink( $args = [] ) { 35 36 public static function formatCatalogLink ( $args = [] ) { 31 37 $clink = self::getCatalogLinkFormat(); 32 if ( ! $clink ) 38 if ( ! $clink ) { 33 39 return false; 34 if ( ! count( $args ) ) 40 } 41 if ( ! count( $args ) ) { 35 42 return false; 43 } 36 44 foreach ( $args as $field => $value ) { 37 45 $clink = str_replace( '{' . $field . '}', $value, $clink ); 38 46 } 47 39 48 return $clink; 40 49 } 41 50 42 public static function getApiBase () {51 public static function getApiBase () { 43 52 return 'https://api.nytimes.com/svc/books/v3/lists/'; 44 53 } 45 54 46 public static function getApiUrl ( $endpoint, $params= [] ) {55 public static function getApiUrl ( $endpoint, $params = [] ) { 47 56 $endpoint = ltrim( $endpoint, '/' ); 48 57 $url = self::getApiBase(); 49 58 $url .= $endpoint; 50 if ( count( $params ) ) 59 if ( count( $params ) ) { 51 60 $url .= '?' . http_build_query( $params ); 61 } 62 52 63 return $url; 53 64 } … … 55 66 /** 56 67 * 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 * 62 75 * @return mixed object if $json_decode=true, otherwise a string 63 76 */ 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 ) { 65 78 $api_key = self::getApiKey(); 66 if ( ! $api_key ) 79 if ( ! $api_key ) { 67 80 return false; 68 $params = array_merge($params, [ 81 } 82 $params = array_merge( $params, [ 69 83 '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 ) ); 72 86 $stored_results = get_transient( $transient_name ); 73 87 74 88 if ( ! empty( $stored_results ) && $use_cache ) { 75 89 $result_body = $stored_results; … … 85 99 } 86 100 87 if ( $json_decode ) 101 if ( $json_decode ) { 88 102 $result_body = json_decode( $result_body ); 103 } 89 104 90 105 return $result_body; 91 106 } 92 107 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 } 93 133 94 134 /** … … 101 141 * if failure to fetch) 102 142 */ 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 ) ) { 111 147 $selected_list = self::getDefaultList(); 148 } 149 112 150 $out = '<div class="nyt-bestseller-listings-changeListWrap">Change List 113 151 <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 ) 116 154 ? ' selected ' 117 155 : ''; 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"; 119 157 } 120 158 $out .= '</select> 121 159 </div>'; 160 122 161 return $out; 123 162 } … … 126 165 * Get the book list from NYT api 127 166 * 128 * @param string $listthe slug for the list129 * @param boolean $use_cachewhether to use the api-call cache (default:true)130 * @param boolean $display_imagesshould 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 * 132 171 * @return string The html for the list of bestsellers 133 172 */ 134 public static function getBookList ( $list, $use_cache = true, $display_images=true ) {173 public static function getBookList ( $list, $use_cache = true, $display_images = true ) { 135 174 $endpoint = "current/{$list}.json"; 136 175 $results = self::getResults( $endpoint, $use_cache ); … … 143 182 144 183 foreach ( $results->results->books as $book ) { 145 146 184 $links = self::getBookLinks( $book ); 147 185 … … 171 209 } 172 210 173 $out .= '</div>211 $out .= '</div> 174 212 ' . $image_html . ' 175 213 </div>'; 176 177 } 214 } 215 178 216 return $out; 179 217 } 180 218 181 public static function getBookLinks ( $book ) {219 public static function getBookLinks ( $book ) { 182 220 $links = []; 183 221 184 if ( $book->sunday_review_link ) 222 if ( $book->sunday_review_link ) { 185 223 $links['New York Times Sunday Review'] = $book->sunday_review_link; 186 if ( $book->book_review_link ) 224 } 225 if ( $book->book_review_link ) { 187 226 $links['New York Times Book Review'] = $book->book_review_link; 188 if ( $book->first_chapter_link ) 227 } 228 if ( $book->first_chapter_link ) { 189 229 $links['First Chapter'] = $book->first_chapter_link; 190 $catalog_link = self::formatCatalogLink([ 230 } 231 $catalog_link = self::formatCatalogLink( [ 191 232 'isbn' => $book->primary_isbn13, 192 233 'author' => $book->author, 193 234 'title' => strtolower( $book->title ), 194 ] );195 if ( $catalog_link ) 235 ] ); 236 if ( $catalog_link ) { 196 237 $links['Library Catalog'] = $catalog_link; 238 } 197 239 198 240 return $links; -
bestseller-lists-from-new-york-times/trunk/readme.txt
r3242199 r3310533 46 46 47 47 == 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 48 53 49 54 = 2.4.2 =
Note: See TracChangeset
for help on using the changeset viewer.