Making WordPress.org

Changeset 6833


Ignore:
Timestamp:
03/07/2018 01:45:38 AM (8 years ago)
Author:
iandunn
Message:

WordCamp QBO: Store classes in site option for reliability.

Storing it in a transient often causes the data to be empty when the QBO connection is unavailable.

This is also a good opportunity to remove some of the unnecessary "middle man" code from the wordcamp-qbo-client experiment, now that it's clear that the tangible drawbacks (lots of unnecessary, duplicated, unintuitive code) are worse than the intangible benefits (future-proofing for the unlikely event that we'll switch to a decentralized hosting model in the future).

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  
    9191         */
    9292        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' );
    12494        }
    12595
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo/wordcamp-qbo.php

    r5588 r6833  
    3636        public static function load() {
    3737                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                }
    3843        }
    3944
     
    8489                        'methods' => 'GET, POST',
    8590                        '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' ),
    9191                ) );
    9292
     
    225225
    226226        /**
    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                }
    251244
    252245                self::load_options();
     
    276269
    277270                if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
    278                         return new WP_Error( 'error', 'Could not fetch classes.' );
     271                        return;
    279272                }
    280273
    281274                $body = json_decode( wp_remote_retrieve_body( $response ), true );
    282275                if ( empty( $body ) ) {
    283                         return new WP_Error( 'error', 'Could not fetch classes body.' );
     276                        return;
    284277                }
    285278
     
    289282                }
    290283
     284                if ( empty ( $class ) ) {
     285                        return;
     286                }
     287
    291288                asort( $classes );
    292289
    293                 set_transient( $cache_key, $classes, 12 * HOUR_IN_SECONDS );
    294                 return $classes;
     290                update_site_option( 'wordcamp_qbo_classes', $classes );
    295291        }
    296292
Note: See TracChangeset for help on using the changeset viewer.