Making WordPress.org


Ignore:
Timestamp:
01/15/2016 06:07:53 PM (10 years ago)
Author:
iandunn
Message:

WordCamp Budgets Dashboard: Centralize currency conversion and formatting.

These will be used by upcoming modules in addition to the current one.

Note that escaping was removed from format_amount(), since that should be done as late as possible.

wp_kses() is performant in this situation. See https://www.tollmanz.com/wp-kses-performance/

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/payment-requests-dashboard.php

    r2273 r2298  
    489489        }
    490490    }
    491 
    492     /**
    493      * Currency Conversion
    494      *
    495      * @param string $from What currency are we selling.
    496      * @param string $to What currency are we buying.
    497      * @param float $amount How much we're selling.
    498      *
    499      * @return float Converted amount.
    500      */
    501     public static function convert_currency( $from, $to, $amount ) {
    502         global $wpdb;
    503 
    504         $from = strtolower( $from );
    505         $to = strtolower( $to );
    506         $cache_key = md5( sprintf( 'wcp-exchange-rate-%s:%s', $from, $to ) );
    507 
    508         $rate = 0;
    509         if ( false === ( $rate = get_transient( $cache_key ) ) ) {
    510             $url = 'https://query.yahooapis.com/v1/public/yql';
    511             $url = add_query_arg( 'format', 'json', $url );
    512             $url = add_query_arg( 'env', rawurlencode( 'store://datatables.org/alltableswithkeys' ), $url );
    513             $url = add_query_arg( 'q', rawurlencode( $wpdb->prepare( 'select * from yahoo.finance.xchange where pair = %s', $from . $to ) ), $url );
    514 
    515             $request = wp_remote_get( esc_url_raw( $url ) );
    516             $body = json_decode( wp_remote_retrieve_body( $request ), true );
    517 
    518             if ( ! empty( $body['query']['results']['rate']['Ask'] ) ) {
    519                 $rate = floatval( $body['query']['results']['rate']['Ask'] );
    520             }
    521 
    522             set_transient( $cache_key, $rate, 24 * HOUR_IN_SECONDS );
    523         }
    524 
    525         if ( $rate < 0.0000000001 )
    526             return 0;
    527 
    528         return $amount * $rate;
    529     }
    530491}
Note: See TracChangeset for help on using the changeset viewer.