Changeset 6833 for sites/trunk/wordcamp.org/public_html/wp-content/plugins
- Timestamp:
- 03/07/2018 01:45:38 AM (7 years ago)
- Location:
- sites/trunk/wordcamp.org/public_html/wp-content/plugins
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo-client/wordcamp-qbo-client.php
r6505 r6833 91 91 */ 92 92 public static function get_classes() { 93 $cache_key = md5( 'wordcamp-qbo-client:classes' ); 94 $cache = get_transient( $cache_key ); 95 96 if ( $cache !== false ) 97 return $cache; 98 99 $request_url = esc_url_raw( self::$api_base . '/classes/' ); 100 $request_args = array( 101 'timeout' => self::REMOTE_REQUEST_TIMEOUT, 102 'headers' => array( 103 'Content-Type' => 'application/json', 104 'Authorization' => self::_get_auth_header( 'get', $request_url ), 105 ), 106 ); 107 $response = wp_remote_get( $request_url, $request_args ); 108 109 Logger\log( 'remote_request', compact( 'request_url', 'request_args', 'response' ) ); 110 111 $classes = array(); 112 113 if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) == 200 ) { 114 $body = json_decode( wp_remote_retrieve_body( $response ), true ); 115 if ( ! empty( $body ) && is_array( $body ) ) 116 $classes = $body; 117 } 118 119 if ( $classes ) { 120 set_transient( $cache_key, $classes, 12 * HOUR_IN_SECONDS ); 121 } 122 123 return $classes; 93 return get_site_option( 'wordcamp_qbo_classes' ); 124 94 } 125 95 -
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo/wordcamp-qbo.php
r5588 r6833 36 36 public static function load() { 37 37 add_action( 'plugins_loaded', array( __CLASS__, 'plugins_loaded' ) ); 38 add_action( 'wcqbo_prime_classes_cache', array( __CLASS__, 'prime_classes_cache' ) ); 39 40 if ( ! wp_next_scheduled( 'wcqbo_prime_classes_cache' ) ) { 41 wp_schedule_event( time(), 'hourly', 'wcqbo_prime_classes_cache' ); 42 } 38 43 } 39 44 … … 84 89 'methods' => 'GET, POST', 85 90 'callback' => array( __CLASS__, 'rest_callback_expense' ), 86 ) );87 88 register_rest_route( 'wordcamp-qbo/v1', '/classes', array(89 'methods' => 'GET',90 'callback' => array( __CLASS__, 'rest_callback_classes' ),91 91 ) ); 92 92 … … 225 225 226 226 /** 227 * REST: /classes 228 * 229 * @param WP_REST_Request $request 230 */ 231 public static function rest_callback_classes( $request ) { 232 if ( ! self::_is_valid_request( $request ) ) 233 return new WP_Error( 'unauthorized', 'Unauthorized', array( 'status' => 401 ) ); 234 235 return self::_get_classes(); 236 } 237 238 /** 239 * Get an array of available QBO classes. 240 * 241 * @uses get_transient, set_transient 242 * 243 * @return array An array of class IDs as keys, names as values. 244 */ 245 private static function _get_classes() { 246 $cache_key = md5( 'wordcamp-qbo:classes' ); 247 $cache = get_transient( $cache_key ); 248 249 if ( $cache !== false ) 250 return $cache; 227 * Update the cached QBO classes ("communities"). 228 * 229 * These are stored in an option rather than a transient, because: 230 * 231 * 1) The user shouldn't have to wait for an external HTTP request to complete before the page loads. 232 * 2) The connection is QBO is not reliable enough. For example, it expires every 180 days, and needs to be 233 * manually reconnected. When the transient expires during that timeframe, it cannot be renewed until the 234 * connection is re-established, and any functionality relying on this would be broken. 235 */ 236 public static function prime_classes_cache() { 237 /* 238 * This isn't strictly needed right now, but it's future-proofing for when we eventually remove 239 * `wordcamp-qbo-client` and network-activate `wordcamp-qbo`. 240 */ 241 if ( ! is_main_site() ) { 242 return; 243 } 251 244 252 245 self::load_options(); … … 276 269 277 270 if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) { 278 return new WP_Error( 'error', 'Could not fetch classes.' );271 return; 279 272 } 280 273 281 274 $body = json_decode( wp_remote_retrieve_body( $response ), true ); 282 275 if ( empty( $body ) ) { 283 return new WP_Error( 'error', 'Could not fetch classes body.' );276 return; 284 277 } 285 278 … … 289 282 } 290 283 284 if ( empty ( $class ) ) { 285 return; 286 } 287 291 288 asort( $classes ); 292 289 293 set_transient( $cache_key, $classes, 12 * HOUR_IN_SECONDS ); 294 return $classes; 290 update_site_option( 'wordcamp_qbo_classes', $classes ); 295 291 } 296 292
Note: See TracChangeset
for help on using the changeset viewer.