Making WordPress.org


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

WordCamp QBO: Apply coding standards.

File:
1 edited

Legend:

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

    r6833 r6834  
    2323
    2424    public static function load_options() {
    25         if ( isset( self::$options ) )
     25        if ( isset( self::$options ) ) {
    2626            return self::$options;
     27        }
    2728
    2829        self::$options = wp_parse_args( get_option( 'wordcamp-qbo', array() ), array(
     
    5051
    5152        $init_options = wp_parse_args( apply_filters( 'wordcamp_qbo_options', array() ), array(
    52             'app_token' => '',
    53             'consumer_key' => '',
     53            'app_token'       => '',
     54            'consumer_key'    => '',
    5455            'consumer_secret' => '',
    55             'hmac_key' => '',
    56 
    57             'categories_map' => array(),
     56            'hmac_key'        => '',
     57
     58            'categories_map'  => array(),
    5859        ) );
    5960
    60         foreach ( $init_options as $key => $value )
     61        foreach ( $init_options as $key => $value ) {
    6162            self::$$key = $value;
     63        }
    6264
    6365        // There's no point in doing anything if we don't have the secrets.
    64         if ( empty( self::$consumer_key ) )
     66        if ( empty( self::$consumer_key ) ) {
    6567            return;
     68        }
    6669
    6770        self::$api_base_url = sprintf(
     
    8790    public static function rest_api_init() {
    8891        register_rest_route( 'wordcamp-qbo/v1', '/expense', array(
    89             'methods' => 'GET, POST',
     92            'methods'  => 'GET, POST',
    9093            'callback' => array( __CLASS__, 'rest_callback_expense' ),
    9194        ) );
    9295
    9396        register_rest_route( 'wordcamp-qbo/v1', '/invoice', array(
    94             'methods' => 'GET, POST',
     97            'methods'  => 'GET, POST',
    9598            'callback' => array( __CLASS__, 'rest_callback_invoice' ),
    9699        ) );
    97100
    98101        register_rest_route( 'wordcamp-qbo/v1', '/invoice_pdf', array(
    99             'methods' => 'GET',
     102            'methods'  => 'GET',
    100103            'callback' => array( __CLASS__, 'rest_callback_invoice_pdf' ),
    101104        ) );
    102105
    103106        register_rest_route( 'wordcamp-qbo/v1', '/paid_invoices', array(
    104             'methods' => 'GET',
     107            'methods'  => 'GET',
    105108            'callback' => array( __CLASS__, 'rest_callback_paid_invoices' ),
    106109        ) );
     
    113116     */
    114117    public static function rest_callback_expense( $request ) {
    115         if ( ! self::_is_valid_request( $request ) )
     118        if ( ! self::_is_valid_request( $request ) ) {
    116119            return new WP_Error( 'unauthorized', 'Unauthorized', array( 'status' => 401 ) );
     120        }
    117121
    118122        self::load_options();
     
    121125
    122126        $amount = floatval( $request->get_param( 'amount' ) );
    123         if ( ! $amount )
     127        if ( ! $amount ) {
    124128            return new WP_Error( 'error', 'An amount was not given.' );
     129        }
    125130
    126131        $description = $request->get_param( 'description' );
    127         if ( empty( $description ) )
     132        if ( empty( $description ) ) {
    128133            return new WP_Error( 'error', 'The expense description can not be empty.' );
     134        }
    129135
    130136        $category = $request->get_param( 'category' );
    131         if ( empty( $category ) || ! array_key_exists( $category, self::$categories_map ) )
     137        if ( empty( $category ) || ! array_key_exists( $category, self::$categories_map ) ) {
    132138            return new WP_Error( 'error', 'The category you have picked is invalid.' );
     139        }
    133140
    134141        $date = $request->get_param( 'date' );
    135         if ( empty( $date ) )
     142        if ( empty( $date ) ) {
    136143            return new WP_Error( 'error', 'The expense date can not be empty.' );
     144        }
    137145
    138146        $date = absint( $date );
    139147
    140148        $class = $request->get_param( 'class' );
    141         if ( empty( $class ) )
     149        if ( empty( $class ) ) {
    142150            return new WP_Error( 'error', 'You need to set a class.' );
     151        }
    143152
    144153        $classes = self::_get_classes();
    145         if ( ! array_key_exists( $class, $classes ) )
     154        if ( ! array_key_exists( $class, $classes ) ) {
    146155            return new WP_Error( 'error', 'Unknown class.' );
     156        }
    147157
    148158        $class = array(
    149159            'value' => $class,
    150             'name' => $classes[ $class ],
     160            'name'  => $classes[ $class ],
    151161        );
    152162
    153163        $payload = array(
    154             'AccountRef' => self::$account,
    155             'TxnDate' => gmdate( 'Y-m-d', $date ),
     164            'AccountRef'  => self::$account,
     165            'TxnDate'     => gmdate( 'Y-m-d', $date ),
    156166            'PaymentType' => 'Cash',
    157             'Line' => array(
     167            'Line'        => array(
    158168                array(
    159                     'Id' => 1,
    160                     'Description' => $description,
    161                     'Amount' => $amount,
    162                     'DetailType' => 'AccountBasedExpenseLineDetail',
     169                    'Id'                            => 1,
     170                    'Description'                   => $description,
     171                    'Amount'                        => $amount,
     172                    'DetailType'                    => 'AccountBasedExpenseLineDetail',
    163173                    'AccountBasedExpenseLineDetail' => array(
    164                         'ClassRef' => $class,
     174                        'ClassRef'   => $class,
    165175                        'AccountRef' => self::$categories_map[ $category ],
    166176                    ),
     
    176186
    177187            $oauth_header = $oauth->get_oauth_header( 'GET', $request_url );
    178             $response = wp_remote_get( $request_url, array(
     188            $response     = wp_remote_get( $request_url, array(
    179189                'timeout' => self::REMOTE_REQUEST_TIMEOUT,
    180190                'headers' => array(
    181191                    'Authorization' => $oauth_header,
    182                     'Accept' => 'application/json',
     192                    'Accept'        => 'application/json',
    183193                ),
    184194            ) );
    185195            Logger\log( 'remote_request_sync_token', compact( 'response' ) );
    186196
    187             if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 )
     197            if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) {
    188198                return new WP_Error( 'error', 'Could not find purchase to update.' );
     199            }
    189200
    190201            $body = json_decode( wp_remote_retrieve_body( $response ), true );
    191             if ( ! isset( $body['Purchase']['SyncToken'] ) )
     202            if ( ! isset( $body['Purchase']['SyncToken'] ) ) {
    192203                return new WP_Error( 'error', 'Could not decode purchase for update.' );
     204            }
    193205
    194206            $payload['SyncToken'] = $body['Purchase']['SyncToken'];
     
    196208        }
    197209
    198         $payload = json_encode( $payload );
     210        $payload     = json_encode( $payload );
    199211        $request_url = esc_url_raw( sprintf( '%s/v3/company/%d/purchase',
    200212            self::$api_base_url, self::$options['auth']['realmId'] ) );
    201213
    202214        $oauth_header = $oauth->get_oauth_header( 'POST', $request_url, $payload );
    203         $response = wp_remote_post( $request_url, array(
     215        $response     = wp_remote_post( $request_url, array(
    204216            'timeout' => self::REMOTE_REQUEST_TIMEOUT,
    205217            'headers' => array(
    206218                'Authorization' => $oauth_header,
    207                 'Accept' => 'application/json',
    208                 'Content-Type' => 'application/json',
    209             ),
    210             'body' => $payload,
     219                'Accept'        => 'application/json',
     220                'Content-Type'  => 'application/json',
     221            ),
     222            'body'    => $payload,
    211223        ) );
    212224        Logger\log( 'remote_request_create_expense', compact( 'payload', 'response' ) );
    213225
    214         if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 )
     226        if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) {
    215227            return new WP_Error( 'error', 'Could not create purchase.' );
     228        }
    216229
    217230        $body = json_decode( wp_remote_retrieve_body( $response ), true );
    218         if ( empty( $body ) )
     231        if ( empty( $body ) ) {
    219232            return new WP_Error( 'error', 'Could not decode create purchase result.' );
     233        }
    220234
    221235        return array(
     
    248262
    249263        $args = array(
    250             'query' => 'SELECT * FROM Class MAXRESULTS 1000',
     264            'query'        => 'SELECT * FROM Class MAXRESULTS 1000',
    251265            'minorversion' => 4,
    252266        );
     
    256270
    257271        $oauth_header = $oauth->get_oauth_header( 'GET', $request_url, $args );
    258         $response = wp_remote_get(
     272        $response     = wp_remote_get(
    259273            esc_url_raw( add_query_arg( $args, $request_url ) ),
    260274            array(
     
    262276                'headers' => array(
    263277                    'Authorization' => $oauth_header,
    264                     'Accept' => 'application/json',
    265                 )
     278                    'Accept'        => 'application/json',
     279                ),
    266280            )
    267281        );
     
    475489         */
    476490        $description_limit = abs( 995 - strlen( $payment_instructions ) );
    477         $customer_memo = sprintf(
     491        $customer_memo     = sprintf(
    478492            "%s\n\n%s",
    479493            substr( $description, 0, $description_limit ),
     
    502516            'Line' => array(
    503517                array(
    504                     'Amount'      => $amount,
    505                     'Description' => $line_description,
    506                     'DetailType'  => 'SalesItemLineDetail',
     518                    'Amount'              => $amount,
     519                    'Description'         => $line_description,
     520                    'DetailType'          => 'SalesItemLineDetail',
    507521
    508522                    'SalesItemLineDetail' => array(
    509                         'ItemRef' => array(
     523                        'ItemRef'   => array(
    510524                            'value' => '20', // Sponsorship
    511525                        ),
    512526
    513                         'ClassRef' => array(
     527                        'ClassRef'  => array(
    514528                            'value' => $class_id,
    515529                        ),
     
    517531                        'UnitPrice' => $amount,
    518532                        'Qty'       => 1,
    519                     )
    520                 )
    521             ),
    522 
    523             'CustomerRef' => array(
     533                    ),
     534                ),
     535            ),
     536
     537            'CustomerRef'  => array(
    524538                'value' => $customer_id,
    525539            ),
     
    533547            ),
    534548
    535             'BillEmail' => array(
     549            'BillEmail'    => array(
    536550                'Address' => $customer_email,
    537551            ),
     
    566580                'Content-Type'  => 'application/json',
    567581            ),
    568             'body' => $payload,
     582            'body'    => $payload,
    569583        );
    570584
    571585        return array(
    572586            'url'  => $request_url,
    573             'args' => $args
     587            'args' => $args,
    574588        );
    575589    }
     
    630644                'Content-Type'  => 'application/octet-stream',
    631645            ),
    632             'body' => '',
     646            'body'    => '',
    633647        );
    634648
     
    717731                'Content-Type'  => 'application/pdf',
    718732            ),
    719             'body' => '',
     733            'body'    => '',
    720734        );
    721735
     
    946960
    947961            'CurrencyRef' => array(
    948                 'value' => $currency_code
    949             ),
    950 
    951             'PreferredDeliveryMethod' =>'Email',
    952 
    953             'GivenName'        => $sponsor['first-name'],
    954             'FamilyName'       => $sponsor['last-name'],
    955             'CompanyName'      => $sponsor['company-name'],
    956             'DisplayName'      => sprintf( '%s - %s', $sponsor['company-name'], $currency_code ),
    957             'PrintOnCheckName' => $sponsor['company-name'],
    958 
    959             'PrimaryPhone' => array(
     962                'value' => $currency_code,
     963            ),
     964
     965            'PreferredDeliveryMethod' => 'Email',
     966
     967            'GivenName'               => $sponsor['first-name'],
     968            'FamilyName'              => $sponsor['last-name'],
     969            'CompanyName'             => $sponsor['company-name'],
     970            'DisplayName'             => sprintf( '%s - %s', $sponsor['company-name'], $currency_code ),
     971            'PrintOnCheckName'        => $sponsor['company-name'],
     972
     973            'PrimaryPhone'            => array(
    960974                'FreeFormNumber' => $sponsor['phone-number'],
    961975            ),
    962976
    963             'PrimaryEmailAddr' => array(
     977            'PrimaryEmailAddr'        => array(
    964978                'Address' => $sponsor['email-address'],
    965979            ),
     
    985999                'Content-Type'  => 'application/json',
    9861000            ),
    987             'body' => $payload,
     1001            'body'    => $payload,
    9881002        );
    9891003
     
    11021116     */
    11031117    private static function _is_valid_request( $request ) {
    1104         if ( ! $request->get_header( 'authorization' ) )
     1118        if ( ! $request->get_header( 'authorization' ) ) {
    11051119            return false;
    1106 
    1107         if ( ! preg_match( '#^wordcamp-qbo-hmac (.+)$#', $request->get_header( 'authorization' ), $matches ) )
     1120        }
     1121
     1122        if ( ! preg_match( '#^wordcamp-qbo-hmac (.+)$#', $request->get_header( 'authorization' ), $matches ) ) {
    11081123            return false;
    1109 
    1110         $given_hmac = $matches[1];
     1124        }
     1125
     1126        $given_hmac  = $matches[1];
    11111127        $request_url = esc_url_raw( home_url( parse_url( home_url( $_SERVER['REQUEST_URI'] ), PHP_URL_PATH ) ) );
    1112         $payload = json_encode( array( strtolower( $request->get_method() ), strtolower( $request_url ),
    1113             $request->get_body(), $request->get_query_params() ) );
     1128        $payload     = json_encode( array(
     1129            strtolower( $request->get_method() ),
     1130            strtolower( $request_url ),
     1131            $request->get_body(),
     1132            $request->get_query_params(),
     1133        ) );
    11141134
    11151135        return hash_equals( hash_hmac( 'sha256', $payload, self::$hmac_key ), $given_hmac );
     
    11281148     */
    11291149    public static function maybe_oauth_request() {
    1130         if ( empty( $_GET['wordcamp-qbo-oauth-request'] ) )
     1150        if ( empty( $_GET['wordcamp-qbo-oauth-request'] ) ) {
    11311151            return;
    1132 
    1133         if ( empty( $_GET['wordcamp-qbo-oauth-nonce'] ) || ! wp_verify_nonce( $_GET['wordcamp-qbo-oauth-nonce'], 'oauth-request' ) )
     1152        }
     1153
     1154        if ( empty( $_GET['wordcamp-qbo-oauth-nonce'] ) || ! wp_verify_nonce( $_GET['wordcamp-qbo-oauth-nonce'], 'oauth-request' ) ) {
    11341155            wp_die( 'Could not verify nonce.' );
     1156        }
    11351157
    11361158        self::load_options();
     
    11401162
    11411163            // We don't have an access token yet.
    1142             $request_url = 'https://oauth.intuit.com/oauth/v1/get_request_token';
     1164            $request_url  = 'https://oauth.intuit.com/oauth/v1/get_request_token';
    11431165            $callback_url = esc_url_raw( add_query_arg( array(
    11441166                'wordcamp-qbo-oauth-request' => 1,
    1145                 'wordcamp-qbo-oauth-nonce' => wp_create_nonce( 'oauth-request' ),
     1167                'wordcamp-qbo-oauth-nonce'   => wp_create_nonce( 'oauth-request' ),
    11461168            ), admin_url() ) );
    11471169
    11481170            $request_token = $oauth->get_request_token( $request_url, $callback_url );
    1149             if ( is_wp_error( $request_token ) )
     1171            if ( is_wp_error( $request_token ) ) {
    11501172                wp_die( $request_token->get_error_message() );
     1173            }
    11511174
    11521175            update_user_meta( get_current_user_id(), 'wordcamp-qbo-oauth', $request_token );
     
    11611184            $request_token = get_user_meta( get_current_user_id(), 'wordcamp-qbo-oauth', true );
    11621185
    1163             if ( $request_token['oauth_token'] != $_GET['oauth_token'] )
     1186            if ( $request_token['oauth_token'] != $_GET['oauth_token'] ) {
    11641187                wp_die( 'Could not verify OAuth token.' );
    1165 
    1166             if ( empty( $_GET['oauth_verifier'] ) )
     1188            }
     1189
     1190            if ( empty( $_GET['oauth_verifier'] ) ) {
    11671191                wp_die( 'Could not obtain OAuth verifier.' );
     1192            }
    11681193
    11691194            $oauth->set_token( $request_token['oauth_token'], $request_token['oauth_token_secret'] );
     
    11721197            $access_token = $oauth->get_access_token( $request_url, $_GET['oauth_verifier'] );
    11731198
    1174             if ( is_wp_error( $access_token ) )
     1199            if ( is_wp_error( $access_token ) ) {
    11751200                wp_die( 'Could not obtain an access token.' );
     1201            }
    11761202
    11771203            // We have an access token.
    11781204            $data = array(
    1179                 'oauth_token' => $access_token['oauth_token'],
     1205                'oauth_token'        => $access_token['oauth_token'],
    11801206                'oauth_token_secret' => $access_token['oauth_token_secret'],
    1181                 'realmId' => $_GET['realmId'],
     1207                'realmId'            => $_GET['realmId'],
    11821208            );
    11831209
     
    11851211
    11861212            $oauth->set_token( self::$options['auth']['oauth_token'], self::$options['auth']['oauth_token_secret'] );
    1187             $request_url = sprintf( '%s/v3/company/%d/companyinfo/%d',
    1188                 self::$api_base_url, self::$options['auth']['realmId'], self::$options['auth']['realmId'] );
     1213
     1214            $request_url = sprintf(
     1215                '%s/v3/company/%d/companyinfo/%d',
     1216                self::$api_base_url,
     1217                self::$options['auth']['realmId'],
     1218                self::$options['auth']['realmId']
     1219            );
    11891220
    11901221            $oauth_header = $oauth->get_oauth_header( 'GET', $request_url );
    1191             $response = wp_remote_get( $request_url, array(
     1222            $response     = wp_remote_get( $request_url, array(
    11921223                'timeout' => self::REMOTE_REQUEST_TIMEOUT,
    11931224                'headers' => array(
    11941225                    'Authorization' => $oauth_header,
    1195                     'Accept' => 'application/json',
    1196                 )
     1226                    'Accept'        => 'application/json',
     1227                ),
    11971228            ) );
    11981229            Logger\log( 'remote_request', compact( 'response' ) );
     
    12081239            $company_name = $body['CompanyInfo']['CompanyName'];
    12091240
    1210             self::$options['auth']['name'] = $company_name;
     1241            self::$options['auth']['name']      = $company_name;
    12111242            self::$options['auth']['timestamp'] = time();
    12121243            self::update_options();
Note: See TracChangeset for help on using the changeset viewer.