Making WordPress.org

Changeset 2298


Ignore:
Timestamp:
01/15/2016 06:07:53 PM (9 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/

Location:
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/bootstrap.php

    r2260 r2298  
    1616
    1717if ( is_admin() ) {
     18    require_once( __DIR__ . '/includes/wordcamp-budgets-dashboard.php' );
    1819    require_once( __DIR__ . '/includes/payment-requests-dashboard.php' );
    1920
  • 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}
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/payment-requests-list-table.php

    r2273 r2298  
    173173        $amount = get_post_meta( $request->ID, '_camppayments_payment_amount', true );
    174174
    175         $amount = preg_replace( '#[^\d.-]+#', '', $amount );
    176         $amount = floatval( $amount );
    177 
    178         if ( strpos( $currency, 'null' ) === false && $amount ) {
    179             $output = sprintf( '%s&nbsp;%s', esc_html( number_format( $amount, 2 ) ), esc_html( $currency ) );
    180 
    181             if ( $currency != 'USD' ) {
    182                 $usd_amount = Payment_Requests_Dashboard::convert_currency( $currency, 'usd', $amount );
    183                 if ( $usd_amount )
    184                     $output .= sprintf( '<br />~&nbsp;%s&nbsp;USD', esc_html( number_format( $usd_amount, 2 ) ) );
    185             }
    186 
    187             return $output;
    188         } elseif ( $amount ) {
    189             return esc_html( $amount );
    190         }
     175        return wp_kses(
     176            \WordCamp\Budgets_Dashboard\format_amount( $amount, $currency ),
     177            array( 'br' => array() )
     178        );
    191179    }
    192180
Note: See TracChangeset for help on using the changeset viewer.