Making WordPress.org


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

WordCamp QBO: Apply coding standards.

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

Legend:

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

    r6833 r6834  
    1616
    1717    public static function load_options() {
    18         if ( isset( self::$options ) )
     18        if ( isset( self::$options ) ) {
    1919            return self::$options;
     20        }
    2021
    2122        self::$options = wp_parse_args( get_option( 'wordcamp-qbo-client', array() ), array(
     
    3435        ) );
    3536
    36         foreach ( $init_options as $key => $value )
     37        foreach ( $init_options as $key => $value ) {
    3738            self::$$key = $value;
    38 
    39         if ( empty( self::$hmac_key ) )
    40             return;
     39        }
     40
     41        if ( empty( self::$hmac_key ) ) {
     42            return;
     43        }
    4144
    4245        add_action( 'admin_init', array( __CLASS__, 'admin_init' ), 20 );
     
    4649        $cap = is_multisite() ? 'manage_network' : 'manage_options';
    4750
    48         if ( ! current_user_can( $cap ) )
    49             return;
    50 
    51         if ( ! class_exists( 'WCP_Payment_Request' ) )
    52             return;
     51        if ( ! current_user_can( $cap ) ) {
     52            return;
     53        }
     54
     55        if ( ! class_exists( 'WCP_Payment_Request' ) ) {
     56            return;
     57        }
    5358
    5459        add_action( 'add_meta_boxes', array( __CLASS__, 'add_meta_boxes' ) );
     
    5964    public static function admin_notices() {
    6065        $screen = get_current_screen();
    61         if ( $screen->id != 'wcp_payment_request' )
    62             return;
     66        if ( $screen->id != 'wcp_payment_request' ) {
     67            return;
     68        }
    6369
    6470        $post = get_post();
    65         if ( $post->post_status == 'auto-draft' )
    66             return;
     71        if ( $post->post_status == 'auto-draft' ) {
     72            return;
     73        }
    6774
    6875        $data = get_post_meta( $post->ID, '_wordcamp-qbo-client-data', true );
    69         if ( empty( $data['last_error'] ) )
    70             return;
     76        if ( empty( $data['last_error'] ) ) {
     77            return;
     78        }
    7179
    7280        printf( '<div class="notice error is-dismissible"><p>QBO Sync Error: %s</p></div>', esc_html( $data['last_error'] ) );
     
    97105        self::load_options();
    98106
    99         $post = get_post();
     107        $post    = get_post();
    100108        $classes = self::get_classes();
    101         $data = get_post_meta( $post->ID, '_wordcamp-qbo-client-data', true );
     109        $data    = get_post_meta( $post->ID, '_wordcamp-qbo-client-data', true );
    102110
    103111        $selected_class = self::$options['default-class'];
    104         if ( ! empty( $data['class'] ) && array_key_exists( $data['class'], $classes ) )
     112        if ( ! empty( $data['class'] ) && array_key_exists( $data['class'], $classes ) ) {
    105113            $selected_class = $data['class'];
     114        }
    106115
    107116        ?>
     
    113122        <?php if ( empty( $data['transaction_id'] ) ) : ?>
    114123            <p>This request has not been synced with QuickBooks yet.</p>
    115         <?php else: ?>
     124        <?php else : ?>
    116125            <pre><?php echo esc_html( print_r( $data, true ) ); ?></pre>
    117126        <?php endif; ?>
     
    152161
    153162    public static function save_post( $post_id, $post ) {
    154         if ( $post->post_type !== WCP_Payment_Request::POST_TYPE )
    155             return;
    156 
    157         if ( empty( $_POST['wordcamp-qbo-client-nonce'] ) || empty( $_POST['wordcamp-qbo-client-post'] ) )
    158             return;
    159 
    160         if ( intval( $_POST['wordcamp-qbo-client-post'] ) !== $post->ID )
    161             return;
    162 
    163         if ( ! wp_verify_nonce( $_POST['wordcamp-qbo-client-nonce'], 'wordcamp-qbo-client-push-' . $post->ID ) )
     163        if ( $post->post_type !== WCP_Payment_Request::POST_TYPE ) {
     164            return;
     165        }
     166
     167        if ( empty( $_POST['wordcamp-qbo-client-nonce'] ) || empty( $_POST['wordcamp-qbo-client-post'] ) ) {
     168            return;
     169        }
     170
     171        if ( intval( $_POST['wordcamp-qbo-client-post'] ) !== $post->ID ) {
     172            return;
     173        }
     174
     175        if ( ! wp_verify_nonce( $_POST['wordcamp-qbo-client-nonce'], 'wordcamp-qbo-client-push-' . $post->ID ) ) {
    164176            wp_die( 'Could not verify QBO nonce. Please go back, refresh the page and try again.' );
     177        }
    165178
    166179        // No need to push.
    167         if ( empty( $_POST['wordcamp-qbo-client-push'] ) )
    168             return;
    169 
    170         if ( $post->post_status != 'paid' )
     180        if ( empty( $_POST['wordcamp-qbo-client-push'] ) ) {
     181            return;
     182        }
     183
     184        if ( $post->post_status != 'paid' ) {
    171185            wp_die( 'A request has to be marked as paid before it could be synced to QuickBooks.' );
    172 
    173         if ( empty( $_POST['wordcamp-qbo-client-class'] ) )
     186        }
     187
     188        if ( empty( $_POST['wordcamp-qbo-client-class'] ) ) {
    174189            wp_die( 'You need to set a QuickBooks class before you can sync this payment request.' );
     190        }
    175191
    176192        $class = $_POST['wordcamp-qbo-client-class'];
    177         if ( ! array_key_exists( $class, self::get_classes() ) )
     193        if ( ! array_key_exists( $class, self::get_classes() ) ) {
    178194            wp_die( 'The class you have picked does not exist.' );
    179 
    180         $data = get_post_meta( $post->ID, '_wordcamp-qbo-client-data', true );
     195        }
     196
     197        $data   = get_post_meta( $post->ID, '_wordcamp-qbo-client-data', true );
    181198        $txn_id = false;
    182199
    183         if ( ! is_array( $data ) )
     200        if ( ! is_array( $data ) ) {
    184201            $data = array();
     202        }
    185203
    186204        // This request has not been synced before.
    187         if ( ! empty( $data['transaction_id'] ) )
     205        if ( ! empty( $data['transaction_id'] ) ) {
    188206            $txn_id = $data['transaction_id'];
     207        }
    189208
    190209        $amount = get_post_meta( $post->ID, '_camppayments_payment_amount', true );
     
    193212
    194213        $currency = get_post_meta( $post->ID, '_camppayments_currency', true );
    195         if ( strtoupper( $currency ) != 'USD' )
     214
     215        if ( strtoupper( $currency ) != 'USD' ) {
    196216            wp_die( 'Non-USD payments sync to QuickBooks is not available yet.' );
     217        }
    197218
    198219        $description_chunks = array( $post->post_title );
    199         $description = get_post_meta( $post->ID, '_camppayments_description', true );
    200         if ( ! empty( $description ) )
     220        $description        = get_post_meta( $post->ID, '_camppayments_description', true );
     221
     222        if ( ! empty( $description ) ) {
    201223            $description_chunks[] = $description;
     224        }
    202225
    203226        $description_chunks[] = esc_url_raw( get_edit_post_link( $post->ID, 'raw' ) );
    204         $description = implode( "\n", $description_chunks );
     227        $description          = implode( "\n", $description_chunks );
     228
    205229        unset( $description_chunks );
    206230
    207231        $category = get_post_meta( $post->ID, '_camppayments_payment_category', true );
    208         $date = absint( get_post_meta( $post->ID, '_camppayments_date_vendor_paid', true ) );
     232        $date     = absint( get_post_meta( $post->ID, '_camppayments_date_vendor_paid', true ) );
    209233
    210234        $body = array(
    211             'id' => $txn_id,
    212             'date' => $date,
    213             'amount' => $amount,
    214             'category' => $category,
     235            'id'          => $txn_id,
     236            'date'        => $date,
     237            'amount'      => $amount,
     238            'category'    => $category,
    215239            'description' => $description,
    216             'class' => $class,
    217         );
    218 
    219         $body = json_encode( $body );
    220         $request_url = esc_url_raw( self::$api_base . '/expense/' );
     240            'class'       => $class,
     241        );
     242
     243        $body         = json_encode( $body );
     244        $request_url  = esc_url_raw( self::$api_base . '/expense/' );
    221245        $request_args = array(
    222246            'timeout' => self::REMOTE_REQUEST_TIMEOUT,
    223             'body' => $body,
     247            'body'    => $body,
    224248            'headers' => array(
    225                 'Content-Type' => 'application/json',
     249                'Content-Type'  => 'application/json',
    226250                'Authorization' => self::_get_auth_header( 'post', $request_url, $body ),
    227251            ),
    228252        );
    229         $response = wp_remote_post( $request_url, $request_args );
     253        $response     = wp_remote_post( $request_url, $request_args );
    230254
    231255        Logger\log( 'remote_request', compact( 'request_url', 'request_args', 'response' ) );
     
    242266                unset( $data['last_error'] );
    243267                $data['transaction_id'] = $body['transaction_id'];
    244                 $data['timestamp'] = time();
    245                 $data['class'] = $class;
     268                $data['timestamp']      = time();
     269                $data['class']          = $class;
    246270
    247271                // Remember this class for future reference.
     
    307331            'description'       => sanitize_text_field( $invoice_meta['_wcbsi_description'    ][0] ),
    308332
    309             'statement_memo' => sprintf(
     333            'statement_memo'    => sprintf(
    310334                'WordCamp.org Invoice: %s',
    311335                esc_url_raw( admin_url( sprintf( 'post.php?post=%s&action=edit', $invoice_id ) ) )
     
    340364
    341365        $args = array(
     366            'body'    => $body,
    342367            'timeout' => self::REMOTE_REQUEST_TIMEOUT,
    343368            'headers' => array(
     
    345370                'Content-Type'  => 'application/json',
    346371            ),
    347             'body' => $body,
    348372        );
    349373
     
    498522     * @param string $request_url The clean request URI, without any query arguments.
    499523     * @param string $body The payload body.
    500      * @param array $args The query arguments.
     524     * @param array  $args The query arguments.
    501525     *
    502526     * @return string A sha256 HMAC signature.
    503527     */
    504528    private static function _get_auth_header( $method, $request_url, $body = '', $args = array() ) {
    505         $signature = hash_hmac( 'sha256', json_encode( array( strtolower( $method ),
    506             strtolower( $request_url ), $body, $args ) ), self::$hmac_key );
     529        $signature = hash_hmac( 'sha256', json_encode( array(
     530            strtolower( $method ),
     531            strtolower( $request_url ),
     532            $body,
     533            $args,
     534        ) ), self::$hmac_key );
    507535
    508536        return 'wordcamp-qbo-hmac ' . $signature;
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo/class-wordcamp-qbo-oauth-client.php

    r2325 r6834  
    33 * WordCamp QBO Oauth Client
    44 *
    5  * Note: This is NOT a general-purpose OAuth client, it is only suitable
     5 * SECURITY WARNING: This is NOT a general-purpose OAuth client, it is only suitable
    66 * for the WordCamp QBO plugin.
    77 */
    88class WordCamp_QBO_OAuth_Client {
    9     private $consumer_key;
    10     private $consumer_secret;
    11     private $oauth_token;
    12     private $oauth_token_secret;
     9    private $consumer_key;
     10    private $consumer_secret;
     11    private $oauth_token;
     12    private $oauth_token_secret;
    1313
    14     /**
    15      * @param string $consumer_key The OAuth consumer key
    16     * @param string $consumer_secret The secret
    17     */
    18     public function __construct( $consumer_key, $consumer_secret ) {
    19         $this->consumer_key = $consumer_key;
    20         $this->consumer_secret = $consumer_secret;
    21     }
     14    /**
     15     * @param string $consumer_key    The OAuth consumer key
     16    * @param string $consumer_secret The secret
     17    */
     18    public function __construct( $consumer_key, $consumer_secret ) {
     19        $this->consumer_key    = $consumer_key;
     20        $this->consumer_secret = $consumer_secret;
     21    }
    2222
    23     /**
    24     * Set current OAuth token
    25     *
    26      * @param string $oauth_token An OAuth token.
    27     * @param string $oauth_token_secret The OAuth token secret.
    28     */
    29     public function set_token( $oauth_token, $oauth_token_secret ) {
    30         $this->oauth_token = $oauth_token;
    31         $this->oauth_token_secret = $oauth_token_secret;
    32     }
     23    /**
     24    * Set current OAuth token
     25    *
     26     * @param string $oauth_token        An OAuth token.
     27    * @param string $oauth_token_secret The OAuth token secret.
     28    */
     29    public function set_token( $oauth_token, $oauth_token_secret ) {
     30        $this->oauth_token        = $oauth_token;
     31        $this->oauth_token_secret = $oauth_token_secret;
     32    }
    3333
    34     /**
    35     * Get a request token.
    36     *
    37     * @param string $callback_url The URL to which a successful authentication will return.
    38     *
    39      * @return array An array with the tokens.
    40     */
    41     public function get_request_token( $request_url, $callback_url ) {
    42         $args = array_merge( $this->_get_default_args(), array(
    43             'oauth_callback' => $callback_url,
    44         ) );
     34    /**
     35    * Get a request token.
     36    *
     37    * @param string $callback_url The URL to which a successful authentication will return.
     38    *
     39     * @return array|WP_Error An array with the tokens.
     40    */
     41    public function get_request_token( $request_url, $callback_url ) {
     42        $args = array_merge( $this->_get_default_args(), array(
     43            'oauth_callback' => $callback_url,
     44        ) );
    4545
    46         $args['oauth_signature'] = $this->_get_signature( 'POST', $request_url, $args );
    47         $args = array_map( 'rawurlencode', $args );
     46        $args['oauth_signature'] = $this->_get_signature( 'POST', $request_url, $args );
     47        $args                    = array_map( 'rawurlencode', $args );
    4848
    49         $response = wp_remote_post( add_query_arg( $args, $request_url ) );
    50         if ( is_wp_error( $response ) )
    51             return $response;
     49        $response = wp_remote_post( add_query_arg( $args, $request_url ) );
    5250
    53         if ( wp_remote_retrieve_response_code( $response ) != 200 )
    54             return new WP_Error( 'error', 'Could not get OAuth request token.' );
     51        if ( is_wp_error( $response ) ) {
     52            return $response;
     53        }
    5554
    56         $result = wp_parse_args( wp_remote_retrieve_body( $response ), array(
    57             'oauth_token' => '',
    58             'oauth_token_secret' => '',
    59             'oauth_callback_confirmed' => '',
    60         ) );
     55        if ( wp_remote_retrieve_response_code( $response ) != 200 ) {
     56            return new WP_Error( 'error', 'Could not get OAuth request token.' );
     57        }
    6158
    62         return $result;
    63     }
     59        $result = wp_parse_args( wp_remote_retrieve_body( $response ), array(
     60            'oauth_token'              => '',
     61            'oauth_token_secret'       => '',
     62            'oauth_callback_confirmed' => '',
     63        ) );
    6464
    65     /**
    66      * Get an OAuth access token.
    67      *
    68      * @param string $verifier A verifier token from the authentication flow.
    69      *
    70      * @return array The access token.
    71      */
    72     public function get_access_token( $request_url, $verifier ) {
    73         $args = array_merge( $this->_get_default_args(), array(
    74             'oauth_verifier' => $verifier,
    75             'oauth_token' => $this->oauth_token,
    76         ) );
     65        return $result;
     66    }
    7767
    78         $args['oauth_signature'] = $this->_get_signature( 'POST', $request_url, $args );
    79         $args = array_map( 'rawurlencode', $args );
     68    /**
     69     * Get an OAuth access token.
     70     *
     71     * @param string $verifier A verifier token from the authentication flow.
     72     *
     73     * @return array|WP_Error The access token.
     74     */
     75    public function get_access_token( $request_url, $verifier ) {
     76        $args = array_merge( $this->_get_default_args(), array(
     77            'oauth_verifier' => $verifier,
     78            'oauth_token'    => $this->oauth_token,
     79        ) );
    8080
    81         $response = wp_remote_post( add_query_arg( $args, $request_url ) );
     81        $args['oauth_signature'] = $this->_get_signature( 'POST', $request_url, $args );
     82        $args                    = array_map( 'rawurlencode', $args );
    8283
    83         if ( is_wp_error( $response ) )
    84             return $response;
     84        $response = wp_remote_post( add_query_arg( $args, $request_url ) );
    8585
    86         if ( wp_remote_retrieve_response_code( $response ) != 200 )
    87             return new WP_Error( 'error', 'Could not get OAuth access token.' );
     86        if ( is_wp_error( $response ) ) {
     87            return $response;
     88        }
    8889
    89         $result = wp_parse_args( wp_remote_retrieve_body( $response ), array(
    90             'oauth_token' => '',
    91             'oauth_token_secret' => '',
    92         ) );
     90        if ( wp_remote_retrieve_response_code( $response ) != 200 ) {
     91            return new WP_Error( 'error', 'Could not get OAuth access token.' );
     92        }
    9393
    94         return $result;
    95     }
     94        $result = wp_parse_args( wp_remote_retrieve_body( $response ), array(
     95            'oauth_token'        => '',
     96            'oauth_token_secret' => '',
     97        ) );
    9698
    97     /**
    98      * Get a string suitable for the Authorization header.
    99      *
    100      * @see http://oauth.net/core/1.0a/#auth_header
    101      *
    102      * @param string $method The request method.
    103      * @param string $request_url The request URL (without query)
    104      * @param array|string $request_args Any additional query/body args.
    105      *
    106      * @return string An OAuth string ready for the Authorization header.
    107      */
    108     public function get_oauth_header( $method, $request_url, $request_args = array() ) {
    109         $oauth_args = array_merge( $this->_get_default_args(), array(
    110             'oauth_token' => $this->oauth_token,
    111         ) );
     99        return $result;
     100    }
    112101
    113         $all_args = $oauth_args;
    114         if ( is_array( $request_args ) && ! empty( $request_args ) )
    115             $all_args = array_merge( $oauth_args, $request_args );
     102    /**
     103     * Get a string suitable for the Authorization header.
     104     *
     105     * @see http://oauth.net/core/1.0a/#auth_header
     106     *
     107     * @param string       $method The request method.
     108     * @param string       $request_url The request URL (without query)
     109     * @param array|string $request_args Any additional query/body args.
     110     *
     111     * @return string An OAuth string ready for the Authorization header.
     112     */
     113    public function get_oauth_header( $method, $request_url, $request_args = array() ) {
     114        $oauth_args = array_merge( $this->_get_default_args(), array(
     115            'oauth_token' => $this->oauth_token,
     116        ) );
    116117
    117         $oauth_args['oauth_signature'] = $this->_get_signature( $method, $request_url, $all_args );
     118        $all_args = $oauth_args;
    118119
    119         $header_parts = array();
    120         foreach ( $oauth_args as $key => $value )
    121             $header_parts[] = sprintf( '%s="%s"', rawurlencode( $key ), rawurlencode( $value ) );
     120        if ( is_array( $request_args ) && ! empty( $request_args ) ) {
     121            $all_args = array_merge( $oauth_args, $request_args );
     122        }
    122123
    123         $header = 'OAuth ' . implode( ',', $header_parts );
    124         return $header;
    125     }
     124        $oauth_args['oauth_signature'] = $this->_get_signature( $method, $request_url, $all_args );
    126125
    127     /**
    128      * Get a default set of OAuth arguments.
    129      *
    130      * @return array Default OAuth arguments.
    131      */
    132     private function _get_default_args() {
    133         return array(
    134             'oauth_nonce' => md5( wp_generate_password( 12 ) ),
    135             'oauth_consumer_key' => $this->consumer_key,
    136             'oauth_signature_method' => 'HMAC-SHA1',
    137             'oauth_timestamp' => time(),
    138             'oauth_version' => '1.0',
    139         );
    140     }
     126        $header_parts = array();
    141127
    142     /**
    143      * Get an OAuth signature.
    144      *
    145      * @see http://oauth.net/core/1.0a/#signing_process
    146      *
    147      * @param string $method The request method, GET, POST, etc.
    148      * @param string $url The request URL (without any query)
    149      * @param array $args An optional array of query or body args.
    150      *
    151      * @return string A base64-encoded hmac-sha1 signature.
    152      */
    153     private function _get_signature( $method, $url, $args ) {
    154         ksort( $args );
     128        foreach ( $oauth_args as $key => $value ) {
     129            $header_parts[] = sprintf( '%s="%s"', rawurlencode( $key ), rawurlencode( $value ) );
     130        }
    155131
    156         // Don't sign a signature.
    157         unset( $args['oauth_signature'] );
     132        $header = 'OAuth ' . implode( ',', $header_parts );
     133        return $header;
     134    }
    158135
    159         $parameter_string = '';
    160         foreach ( $args as $key => $value )
    161             $parameter_string .= sprintf( '&%s=%s', rawurlencode( $key ), rawurlencode( $value ) );
     136    /**
     137     * Get a default set of OAuth arguments.
     138     *
     139     * @return array Default OAuth arguments.
     140     */
     141    private function _get_default_args() {
     142        return array(
     143            'oauth_nonce'            => md5( wp_generate_password( 12 ) ),
     144            'oauth_consumer_key'     => $this->consumer_key,
     145            'oauth_signature_method' => 'HMAC-SHA1',
     146            'oauth_timestamp'        => time(),
     147            'oauth_version'          => '1.0',
     148        );
     149    }
    162150
    163         $parameter_string = trim( $parameter_string, '&' );
    164         $signature_base = strtoupper( $method ) . '&' . rawurlencode( $url ) . '&' . rawurlencode( $parameter_string );
    165         $signing_key = rawurlencode( $this->consumer_secret ) . '&' . rawurlencode( $this->oauth_token_secret );
     151    /**
     152     * Get an OAuth signature.
     153     *
     154     * @see http://oauth.net/core/1.0a/#signing_process
     155     *
     156     * @param string $method The request method, GET, POST, etc.
     157     * @param string $url    The request URL (without any query)
     158     * @param array  $args   An optional array of query or body args.
     159     *
     160     * @return string A base64-encoded hmac-sha1 signature.
     161     */
     162    private function _get_signature( $method, $url, $args ) {
     163        ksort( $args );
    166164
    167         return base64_encode( hash_hmac( 'sha1', $signature_base, $signing_key, true ) );
    168     }
     165        // Don't sign a signature.
     166        unset( $args['oauth_signature'] );
     167
     168        $parameter_string = '';
     169
     170        foreach ( $args as $key => $value ) {
     171            $parameter_string .= sprintf( '&%s=%s', rawurlencode( $key ), rawurlencode( $value ) );
     172        }
     173
     174        $parameter_string = trim( $parameter_string, '&' );
     175        $signature_base   = strtoupper( $method ) . '&' . rawurlencode( $url ) . '&' . rawurlencode( $parameter_string );
     176        $signing_key      = rawurlencode( $this->consumer_secret ) . '&' . rawurlencode( $this->oauth_token_secret );
     177
     178        return base64_encode( hash_hmac( 'sha1', $signature_base, $signing_key, true ) );
     179    }
    169180}
  • 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.