From 1b829a0533185e64daa109d5b9dbea59e1b500c6 Mon Sep 17 00:00:00 2001 From: willian-soaresferreira Date: Thu, 17 Sep 2020 11:23:18 -0300 Subject: [PATCH] fix direct calls to order object properties --- .../views/html-notice-missing-woocommerce.php | 15 +++++++-- includes/class-wc-pagarme-api.php | 33 ++++++++++--------- ...lass-wc-pagarme-banking-ticket-gateway.php | 6 ++-- .../class-wc-pagarme-credit-card-gateway.php | 6 ++-- includes/class-wc-pagarme-my-account.php | 4 +-- woocommerce-pagarme.php | 25 ++++++++++---- 6 files changed, 56 insertions(+), 33 deletions(-) diff --git a/includes/admin/views/html-notice-missing-woocommerce.php b/includes/admin/views/html-notice-missing-woocommerce.php index dfecd61..8b89170 100644 --- a/includes/admin/views/html-notice-missing-woocommerce.php +++ b/includes/admin/views/html-notice-missing-woocommerce.php @@ -10,19 +10,28 @@ } $is_installed = false; +$is_active = false; +$has_required_version = false; if ( function_exists( 'get_plugins' ) ) { $all_plugins = get_plugins(); $is_installed = ! empty( $all_plugins['woocommerce/woocommerce.php'] ); + $is_active = is_plugin_active( 'woocommerce/woocommerce.php' ); + + if ( $is_installed && defined( 'WC_VERSION' ) ) { + // Minimum Woocommerce version required. + $has_required_version = version_compare( WC_VERSION, '3.0', '>=' ); + } } ?>
-

- - +

+

+ +

diff --git a/includes/class-wc-pagarme-api.php b/includes/class-wc-pagarme-api.php index d4a287c..cb57056 100644 --- a/includes/class-wc-pagarme-api.php +++ b/includes/class-wc-pagarme-api.php @@ -246,8 +246,8 @@ public function generate_transaction_data( $order, $posted ) { 'amount' => $order->get_total() * 100, 'postback_url' => WC()->api_request_url( get_class( $this->gateway ) ), 'customer' => array( - 'name' => trim( $order->billing_first_name . ' ' . $order->billing_last_name ), - 'email' => $order->billing_email, + 'name' => trim( $order->get_formatted_billing_full_name() ), + 'email' => $order->get_billing_email(), ), 'metadata' => array( 'order_number' => $order->get_order_number(), @@ -255,8 +255,8 @@ public function generate_transaction_data( $order, $posted ) { ); // Phone. - if ( ! empty( $order->billing_phone ) ) { - $phone = $this->only_numbers( $order->billing_phone ); + if ( $order->get_billing_phone() ) { + $phone = $this->only_numbers( $order->get_billing_phone() ); $data['customer']['phone'] = array( 'ddd' => substr( $phone, 0, 2 ), @@ -265,11 +265,11 @@ public function generate_transaction_data( $order, $posted ) { } // Address. - if ( ! empty( $order->billing_address_1 ) ) { + if ( $order->get_billing_address_1() ) { $data['customer']['address'] = array( - 'street' => $order->billing_address_1, - 'complementary' => $order->billing_address_2, - 'zipcode' => $this->only_numbers( $order->billing_postcode ), + 'street' => $order->get_billing_address_1(), + 'complementary' => $order->get_billing_address_2(), + 'zipcode' => $this->only_numbers( $order->get_billing_postcode() ), ); // Non-WooCommerce default address fields. @@ -549,7 +549,7 @@ public function do_transaction( $order, $args, $token = '' ) { */ public function do_refund( $order_id, $amount ) { $order = wc_get_order( $order_id ); - $transaction_id = get_post_meta( $order_id, '_wc_pagarme_transaction_id', true ); + $transaction_id = $order->get_meta( '_wc_pagarme_transaction_id' ); $endpoint = 'transactions/' . $transaction_id . '/refund'; $data = array ( 'api_key' => $this->gateway->api_key, @@ -862,15 +862,16 @@ public function process_successful_ipn( $posted ) { $order = wc_get_order( $order_id ); $status = sanitize_text_field( $posted['current_status'] ); - if ( $order && $order->id === $order_id ) { + if ( $order && $order->get_id() === $order_id ) { $this->process_order_status( $order, $status ); } // Async transactions will only send the boleto_url on IPN. - if ( ! empty( $posted['transaction']['boleto_url'] ) && 'pagarme-banking-ticket' === $order->payment_method ) { - $post_data = get_post_meta( $order->id, '_wc_pagarme_transaction_data', true ); + if ( ! empty( $posted['transaction']['boleto_url'] ) && 'pagarme-banking-ticket' === $order->get_payment_method() ) { + $post_data = $order->get_meta( '_wc_pagarme_transaction_data' ); $post_data['boleto_url'] = sanitize_text_field( $posted['transaction']['boleto_url'] ); - update_post_meta( $order->id, '_wc_pagarme_transaction_data', $post_data ); + $order->update_meta_data( '_wc_pagarme_transaction_data', $post_data ); + $order->save(); } } @@ -893,7 +894,7 @@ public function process_order_status( $order, $status ) { break; case 'pending_review': - $transaction_id = get_post_meta( $order->id, '_wc_pagarme_transaction_id', true ); + $transaction_id = $order->get_meta( '_wc_pagarme_transaction_id' ); $transaction_url = 'https://dashboard.pagar.me/#/transactions/' . intval( $transaction_id ) . ''; /* translators: %s transaction details url */ @@ -920,7 +921,7 @@ public function process_order_status( $order, $status ) { case 'refused' : $order->update_status( 'failed', __( 'Pagar.me: The transaction was rejected by the card company or by fraud.', 'woocommerce-pagarme' ) ); - $transaction_id = get_post_meta( $order->id, '_wc_pagarme_transaction_id', true ); + $transaction_id = $order->get_meta( '_wc_pagarme_transaction_id' ); $transaction_url = 'https://dashboard.pagar.me/#/transactions/' . intval( $transaction_id ) . ''; $this->send_email( @@ -933,7 +934,7 @@ public function process_order_status( $order, $status ) { case 'refunded' : $order->update_status( 'refunded', __( 'Pagar.me: The transaction was refunded/canceled.', 'woocommerce-pagarme' ) ); - $transaction_id = get_post_meta( $order->id, '_wc_pagarme_transaction_id', true ); + $transaction_id = $order->get_meta( '_wc_pagarme_transaction_id' ); $transaction_url = 'https://dashboard.pagar.me/#/transactions/' . intval( $transaction_id ) . ''; $this->send_email( diff --git a/includes/class-wc-pagarme-banking-ticket-gateway.php b/includes/class-wc-pagarme-banking-ticket-gateway.php index d607a2f..15dcee1 100644 --- a/includes/class-wc-pagarme-banking-ticket-gateway.php +++ b/includes/class-wc-pagarme-banking-ticket-gateway.php @@ -175,7 +175,7 @@ public function process_payment( $order_id ) { */ public function thankyou_page( $order_id ) { $order = wc_get_order( $order_id ); - $data = get_post_meta( $order_id, '_wc_pagarme_transaction_data', true ); + $data = $order->get_meta( '_wc_pagarme_transaction_data' ); if ( isset( $data['boleto_url'] ) && in_array( $order->get_status(), array( 'processing', 'on-hold' ), true ) ) { $template = 'no' === $this->async ? 'payment' : 'async'; @@ -201,11 +201,11 @@ public function thankyou_page( $order_id ) { * @return string Payment instructions. */ public function email_instructions( $order, $sent_to_admin, $plain_text = false ) { - if ( $sent_to_admin || ! in_array( $order->get_status(), array( 'processing', 'on-hold' ), true ) || $this->id !== $order->payment_method ) { + if ( $sent_to_admin || ! in_array( $order->get_status(), array( 'processing', 'on-hold' ), true ) || $this->id !== $order->get_payment_method() ) { return; } - $data = get_post_meta( $order->id, '_wc_pagarme_transaction_data', true ); + $data = $order->get_meta( '_wc_pagarme_transaction_data' ); if ( isset( $data['boleto_url'] ) ) { $email_type = $plain_text ? 'plain' : 'html'; diff --git a/includes/class-wc-pagarme-credit-card-gateway.php b/includes/class-wc-pagarme-credit-card-gateway.php index 8aee1bd..151b62c 100644 --- a/includes/class-wc-pagarme-credit-card-gateway.php +++ b/includes/class-wc-pagarme-credit-card-gateway.php @@ -335,7 +335,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { */ public function thankyou_page( $order_id ) { $order = wc_get_order( $order_id ); - $data = get_post_meta( $order_id, '_wc_pagarme_transaction_data', true ); + $data = $order->get_meta( '_wc_pagarme_transaction_data' ); if ( isset( $data['installments'] ) && in_array( $order->get_status(), array( 'processing', 'on-hold' ), true ) ) { wc_get_template( @@ -360,11 +360,11 @@ public function thankyou_page( $order_id ) { * @return string Payment instructions. */ public function email_instructions( $order, $sent_to_admin, $plain_text = false ) { - if ( $sent_to_admin || ! in_array( $order->get_status(), array( 'processing', 'on-hold' ), true ) || $this->id !== $order->payment_method ) { + if ( $sent_to_admin || ! in_array( $order->get_status(), array( 'processing', 'on-hold' ), true ) || $this->id !== $order->get_payment_method() ) { return; } - $data = get_post_meta( $order->id, '_wc_pagarme_transaction_data', true ); + $data = $order->get_meta( '_wc_pagarme_transaction_data' ); if ( isset( $data['installments'] ) ) { $email_type = $plain_text ? 'plain' : 'html'; diff --git a/includes/class-wc-pagarme-my-account.php b/includes/class-wc-pagarme-my-account.php index 73e688c..5520771 100644 --- a/includes/class-wc-pagarme-my-account.php +++ b/includes/class-wc-pagarme-my-account.php @@ -30,7 +30,7 @@ public function __construct() { * @return array */ public function my_orders_banking_ticket_link( $actions, $order ) { - if ( 'pagarme-banking-ticket' !== $order->payment_method ) { + if ( 'pagarme-banking-ticket' !== $order->get_payment_method() ) { return $actions; } @@ -38,7 +38,7 @@ public function my_orders_banking_ticket_link( $actions, $order ) { return $actions; } - $data = get_post_meta( $order->id, '_wc_pagarme_transaction_data', true ); + $data = $order->get_meta( '_wc_pagarme_transaction_data' ); if ( ! empty( $data['boleto_url'] ) ) { $actions[] = array( 'url' => $data['boleto_url'], diff --git a/woocommerce-pagarme.php b/woocommerce-pagarme.php index c380aea..6aee022 100644 --- a/woocommerce-pagarme.php +++ b/woocommerce-pagarme.php @@ -44,15 +44,28 @@ class WC_Pagarme { private function __construct() { // Load plugin text domain. add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); + $is_installed = false; + $has_required_version = false; - // Checks with WooCommerce is installed. + // Checks if WooCommerce is installed. if ( class_exists( 'WC_Payment_Gateway' ) ) { - $this->upgrade(); - $this->includes(); + $is_installed = true; - add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateway' ) ); - add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) ); - } else { + // Checks if minimum Woocommerce version is installed. + if ( defined( 'WC_VERSION' ) ) { + $has_required_version = version_compare( WC_VERSION, '3.0', '>=' ); + } + + if ( $has_required_version ) { + $this->upgrade(); + $this->includes(); + + add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateway' ) ); + add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) ); + } + } + + if ( ! $is_installed || ! $has_required_version ) { add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) ); } }