Changeset 6834 for sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo-client/wordcamp-qbo-client.php
- Timestamp:
- 03/07/2018 01:45:42 AM (7 years ago)
- 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 16 16 17 17 public static function load_options() { 18 if ( isset( self::$options ) ) 18 if ( isset( self::$options ) ) { 19 19 return self::$options; 20 } 20 21 21 22 self::$options = wp_parse_args( get_option( 'wordcamp-qbo-client', array() ), array( … … 34 35 ) ); 35 36 36 foreach ( $init_options as $key => $value ) 37 foreach ( $init_options as $key => $value ) { 37 38 self::$$key = $value; 38 39 if ( empty( self::$hmac_key ) ) 40 return; 39 } 40 41 if ( empty( self::$hmac_key ) ) { 42 return; 43 } 41 44 42 45 add_action( 'admin_init', array( __CLASS__, 'admin_init' ), 20 ); … … 46 49 $cap = is_multisite() ? 'manage_network' : 'manage_options'; 47 50 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 } 53 58 54 59 add_action( 'add_meta_boxes', array( __CLASS__, 'add_meta_boxes' ) ); … … 59 64 public static function admin_notices() { 60 65 $screen = get_current_screen(); 61 if ( $screen->id != 'wcp_payment_request' ) 62 return; 66 if ( $screen->id != 'wcp_payment_request' ) { 67 return; 68 } 63 69 64 70 $post = get_post(); 65 if ( $post->post_status == 'auto-draft' ) 66 return; 71 if ( $post->post_status == 'auto-draft' ) { 72 return; 73 } 67 74 68 75 $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 } 71 79 72 80 printf( '<div class="notice error is-dismissible"><p>QBO Sync Error: %s</p></div>', esc_html( $data['last_error'] ) ); … … 97 105 self::load_options(); 98 106 99 $post = get_post();107 $post = get_post(); 100 108 $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 ); 102 110 103 111 $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 ) ) { 105 113 $selected_class = $data['class']; 114 } 106 115 107 116 ?> … … 113 122 <?php if ( empty( $data['transaction_id'] ) ) : ?> 114 123 <p>This request has not been synced with QuickBooks yet.</p> 115 <?php else : ?>124 <?php else : ?> 116 125 <pre><?php echo esc_html( print_r( $data, true ) ); ?></pre> 117 126 <?php endif; ?> … … 152 161 153 162 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 ) ) { 164 176 wp_die( 'Could not verify QBO nonce. Please go back, refresh the page and try again.' ); 177 } 165 178 166 179 // 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' ) { 171 185 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'] ) ) { 174 189 wp_die( 'You need to set a QuickBooks class before you can sync this payment request.' ); 190 } 175 191 176 192 $class = $_POST['wordcamp-qbo-client-class']; 177 if ( ! array_key_exists( $class, self::get_classes() ) ) 193 if ( ! array_key_exists( $class, self::get_classes() ) ) { 178 194 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 ); 181 198 $txn_id = false; 182 199 183 if ( ! is_array( $data ) ) 200 if ( ! is_array( $data ) ) { 184 201 $data = array(); 202 } 185 203 186 204 // This request has not been synced before. 187 if ( ! empty( $data['transaction_id'] ) ) 205 if ( ! empty( $data['transaction_id'] ) ) { 188 206 $txn_id = $data['transaction_id']; 207 } 189 208 190 209 $amount = get_post_meta( $post->ID, '_camppayments_payment_amount', true ); … … 193 212 194 213 $currency = get_post_meta( $post->ID, '_camppayments_currency', true ); 195 if ( strtoupper( $currency ) != 'USD' ) 214 215 if ( strtoupper( $currency ) != 'USD' ) { 196 216 wp_die( 'Non-USD payments sync to QuickBooks is not available yet.' ); 217 } 197 218 198 219 $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 ) ) { 201 223 $description_chunks[] = $description; 224 } 202 225 203 226 $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 205 229 unset( $description_chunks ); 206 230 207 231 $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 ) ); 209 233 210 234 $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, 215 239 '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/' ); 221 245 $request_args = array( 222 246 'timeout' => self::REMOTE_REQUEST_TIMEOUT, 223 'body' => $body,247 'body' => $body, 224 248 'headers' => array( 225 'Content-Type' => 'application/json',249 'Content-Type' => 'application/json', 226 250 'Authorization' => self::_get_auth_header( 'post', $request_url, $body ), 227 251 ), 228 252 ); 229 $response = wp_remote_post( $request_url, $request_args );253 $response = wp_remote_post( $request_url, $request_args ); 230 254 231 255 Logger\log( 'remote_request', compact( 'request_url', 'request_args', 'response' ) ); … … 242 266 unset( $data['last_error'] ); 243 267 $data['transaction_id'] = $body['transaction_id']; 244 $data['timestamp'] = time();245 $data['class'] = $class;268 $data['timestamp'] = time(); 269 $data['class'] = $class; 246 270 247 271 // Remember this class for future reference. … … 307 331 'description' => sanitize_text_field( $invoice_meta['_wcbsi_description' ][0] ), 308 332 309 'statement_memo' => sprintf(333 'statement_memo' => sprintf( 310 334 'WordCamp.org Invoice: %s', 311 335 esc_url_raw( admin_url( sprintf( 'post.php?post=%s&action=edit', $invoice_id ) ) ) … … 340 364 341 365 $args = array( 366 'body' => $body, 342 367 'timeout' => self::REMOTE_REQUEST_TIMEOUT, 343 368 'headers' => array( … … 345 370 'Content-Type' => 'application/json', 346 371 ), 347 'body' => $body,348 372 ); 349 373 … … 498 522 * @param string $request_url The clean request URI, without any query arguments. 499 523 * @param string $body The payload body. 500 * @param array $args The query arguments.524 * @param array $args The query arguments. 501 525 * 502 526 * @return string A sha256 HMAC signature. 503 527 */ 504 528 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 ); 507 535 508 536 return 'wordcamp-qbo-hmac ' . $signature;
Note: See TracChangeset
for help on using the changeset viewer.