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-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;
Note: See TracChangeset for help on using the changeset viewer.