From aa996d007449e50294142b5ba32449bf9dfa9e55 Mon Sep 17 00:00:00 2001 From: Oleg Iskusnyh Date: Sat, 25 Mar 2023 16:16:07 +0600 Subject: [PATCH] Add High-Performance Order Storage --- changelog.txt | 5 ++ composer.json | 2 +- ...class-wc-background-swedbank-pay-queue.php | 22 +------ ...class-wc-gateway-swedbank-pay-checkout.php | 27 ++------ includes/class-wc-swedbank-admin.php | 33 +++++----- .../class-wc-swedbank-pay-instant-capture.php | 7 +- .../class-wc-swedbank-pay-payment-url.php | 14 +++- includes/class-wc-swedbank-pay-refund.php | 60 ++++++++--------- includes/class-wc-swedbank-plugin.php | 1 + includes/class-wc-swedbank-subscriptions.php | 9 ++- includes/functions.php | 65 +++++++++++++++++++ package.json | 2 +- readme.md | 2 +- readme.txt | 2 +- swedbank-pay-woocommerce-checkout.php | 13 +++- 15 files changed, 156 insertions(+), 108 deletions(-) create mode 100644 includes/functions.php diff --git a/changelog.txt b/changelog.txt index b017370..023e8fb 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,8 @@ +Version 7.1.0 +* High-Performance Order Storage support +* Fixed critical error when a product have been deleted in the admin backend +* Session fixes + Version 7.0.1 * Fixed PaymentOrder.Payer.ConsumerProfileRef: Both ConsumerProfileRef and PayerReference cannot be set at the same time. diff --git a/composer.json b/composer.json index 50bc866..f6249f5 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "php": ">=7.0", "adci/full-name-parser": "^0.2.4", "ramsey/uuid": "^3.7", - "swedbank-pay/swedbank-pay-woocommerce-core": "~6.0.1" + "swedbank-pay/swedbank-pay-woocommerce-core": "~6.1.0" }, "require-dev": { "yoast/phpunit-polyfills": "^1.0.2" diff --git a/includes/class-wc-background-swedbank-pay-queue.php b/includes/class-wc-background-swedbank-pay-queue.php index 1b5c0c4..1483a14 100644 --- a/includes/class-wc-background-swedbank-pay-queue.php +++ b/includes/class-wc-background-swedbank-pay-queue.php @@ -175,7 +175,7 @@ protected function task( $item ) { // Get Order by Payment Id $transaction_id = $data['transaction']['number']; $payment_id = $data['payment']['id']; - $order_id = $this->get_post_id_by_meta( '_payex_payment_id', $payment_id ); + $order_id = sb_get_post_id_by_meta( '_payex_payment_id', $payment_id ); if ( ! $order_id ) { throw new \Exception( sprintf( 'Error: Failed to get order Id by Payment Id %s', $payment_id ) ); } @@ -235,24 +235,4 @@ public function dispatch_queue() { $this->save()->dispatch(); } } - - /** - * Get Post Id by Meta - * - * @param $key - * @param $value - * - * @return null|string - */ - private function get_post_id_by_meta( $key, $value ) { - global $wpdb; - - return $wpdb->get_var( - $wpdb->prepare( - "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = %s AND meta_value = %s;", - $key, - $value - ) - ); - } } diff --git a/includes/class-wc-gateway-swedbank-pay-checkout.php b/includes/class-wc-gateway-swedbank-pay-checkout.php index 28f6a1d..7ecb29c 100644 --- a/includes/class-wc-gateway-swedbank-pay-checkout.php +++ b/includes/class-wc-gateway-swedbank-pay-checkout.php @@ -1030,7 +1030,7 @@ public function process_payment( $order_id ) { } // Replace token - delete_post_meta( $order->get_id(), '_payment_tokens' ); + $order->delete_meta_data( '_payment_tokens' ); $order->add_payment_token( $token ); if ( self::wcs_is_payment_change() ) { @@ -1322,7 +1322,7 @@ public function return_handler() { $paymentorder_id = $data['paymentOrder']['id']; // Get Order by Order Payment Id - $order_id = $this->get_post_id_by_meta( '_payex_paymentorder_id', $paymentorder_id ); + $order_id = sb_get_post_id_by_meta( '_payex_paymentorder_id', $paymentorder_id ); // Get Order ID from payeeInfo if is not exist if ( empty( $order_id ) ) { @@ -1542,7 +1542,8 @@ public function delete_token( $token ) { * @throws Exception */ public function update_address( $order_id ) { - $paymentorder_id = get_post_meta( $order_id, '_payex_paymentorder_id', true ); + $order = wc_get_order( $order_id ); + $paymentorder_id = $order->get_meta( '_payex_paymentorder_id' ); if ( ! empty( $paymentorder_id ) ) { $result = $this->core->request( 'GET', $paymentorder_id . '/payers' ); @@ -1763,26 +1764,6 @@ public function update_consumer_address( $user_id, $type, $address ) { } } - /** - * Get Post Id by Meta - * - * @param $key - * @param $value - * - * @return null|string - */ - private function get_post_id_by_meta( $key, $value ) { - global $wpdb; - - return $wpdb->get_var( - $wpdb->prepare( - "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = %s AND meta_value = %s;", - $key, - $value - ) - ); - } - /** * Checks an order to see if it contains a subscription. * @see wcs_order_contains_subscription() diff --git a/includes/class-wc-swedbank-admin.php b/includes/class-wc-swedbank-admin.php index 174e2dc..39feb09 100644 --- a/includes/class-wc-swedbank-admin.php +++ b/includes/class-wc-swedbank-admin.php @@ -21,7 +21,7 @@ public function __construct() { ); // Add meta boxes - add_action( 'add_meta_boxes', __CLASS__ . '::add_meta_boxes' ); + add_action( 'add_meta_boxes', __CLASS__ . '::add_meta_boxes', 10, 2 ); // Add action buttons add_action( 'woocommerce_order_item_add_action_buttons', __CLASS__ . '::add_action_buttons', 10, 1 ); @@ -60,25 +60,25 @@ public function add_valid_order_statuses( $statuses, $order ) { /** * Add meta boxes in admin + * @param $screen_id + * @param WC_Order $order * @return void */ - public static function add_meta_boxes() { - global $post_id; - - $order = wc_get_order( $post_id ); - + public static function add_meta_boxes( $screen_id, $order ) { if ( $order ) { $payment_method = $order->get_payment_method(); if ( in_array( $payment_method, WC_Swedbank_Plugin::PAYMENT_METHODS, true ) ) { - $payment_id = get_post_meta( $post_id, '_payex_payment_id', true ); + $payment_id = $order->get_meta( '_payex_payment_id' ); if ( ! empty( $payment_id ) ) { + $screen = sb_is_hpos_enabled() ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order'; + add_meta_box( 'swedbank_payment_actions', __( 'Swedbank Pay Payments Actions', 'swedbank-pay-woocommerce-checkout' ), __CLASS__ . '::order_meta_box_payment_actions', - 'shop_order', + $screen, 'side', - 'default' + 'high' ); } } @@ -87,13 +87,11 @@ public static function add_meta_boxes() { /** * MetaBox for Payment Actions + * @param WC_Order $order * @return void */ - public static function order_meta_box_payment_actions() { - global $post_id; - - $order = wc_get_order( $post_id ); - $payment_id = get_post_meta( $post_id, '_payex_payment_id', true ); + public static function order_meta_box_payment_actions( $order ) { + $payment_id = $order->get_meta( '_payex_payment_id' ); if ( empty( $payment_id ) ) { return; } @@ -118,7 +116,7 @@ public static function order_meta_box_payment_actions() { array( 'gateway' => $gateway, 'order' => $order, - 'order_id' => $post_id, + 'order_id' => $order->get_id(), 'payment_id' => $payment_id, 'info' => $result, ), @@ -133,7 +131,7 @@ public static function order_meta_box_payment_actions() { * @param WC_Order $order */ public static function add_action_buttons( $order ) { - if (function_exists('wcs_is_subscription') && wcs_is_subscription($order)) { + if ( function_exists( 'wcs_is_subscription' ) && wcs_is_subscription( $order ) ) { // Buttons are available for orders only return; } @@ -168,7 +166,8 @@ public static function add_action_buttons( $order ) { * @return void */ public static function admin_enqueue_scripts( $hook ) { - if ( 'post.php' === $hook ) { + $hook_to_check = sb_is_hpos_enabled() ? wc_get_page_screen_id( 'shop-order' ) : 'post.php'; + if ( $hook_to_check === $hook ) { // Scripts $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; wp_register_script( diff --git a/includes/class-wc-swedbank-pay-instant-capture.php b/includes/class-wc-swedbank-pay-instant-capture.php index f8f22e1..62cafe0 100644 --- a/includes/class-wc-swedbank-pay-instant-capture.php +++ b/includes/class-wc-swedbank-pay-instant-capture.php @@ -175,12 +175,7 @@ private function get_instant_capture_items( $order ) { } // Get Product Class - $product_class = get_post_meta( - $order_item->get_product()->get_id(), - '_sb_product_class', - true - ); - + $product_class = $product->get_meta( '_sb_product_class' ); if ( empty( $product_class ) ) { $product_class = 'ProductGroup1'; } diff --git a/includes/class-wc-swedbank-pay-payment-url.php b/includes/class-wc-swedbank-pay-payment-url.php index c94470c..930c64c 100644 --- a/includes/class-wc-swedbank-pay-payment-url.php +++ b/includes/class-wc-swedbank-pay-payment-url.php @@ -39,7 +39,9 @@ public function __construct() { $this->payment_menu_style = isset( $this->settings['paymentMenuStyle'] ) ? $this->settings['paymentMenuStyle'] : $this->payment_menu_style; - add_action( 'init', array( $this, 'override_checkout_shortcode' ), 20 ); + if ( isset( $_GET['payment_url'] ) ) { // WPCS: input var ok, CSRF ok. + add_action( 'init', array( $this, 'override_checkout_shortcode' ), 100 ); + } } /** @@ -63,16 +65,22 @@ public function override_checkout_shortcode() */ public function shortcode_woocommerce_checkout( $atts ) { + // Check WC sessions + if ( ! WC()->session ) { + WC()->initialize_session(); + } + $payment_url = WC()->session->get( 'sb_payment_url' ); if ( empty( $payment_url ) ) { $order_id = absint( WC()->session->get( 'order_awaiting_payment' ) ); if ( $order_id > 0 ) { - $payment_url = get_post_meta( $order_id, '_sb_view_paymentorder', true ); + $order = wc_get_order( $order_id ); + $payment_url = $order->get_meta( '_sb_view_paymentorder' ); } } - if ( isset( $_GET['payment_url'] ) && ! empty( $payment_url ) ) { // WPCS: input var ok, CSRF ok. + if ( ! empty( $payment_url ) ) { wp_dequeue_script( 'featherlight' ); wp_dequeue_script( 'wc-sb-seamless-checkout' ); wp_dequeue_script( 'wc-sb-checkout' ); diff --git a/includes/class-wc-swedbank-pay-refund.php b/includes/class-wc-swedbank-pay-refund.php index 799168e..487e0f3 100644 --- a/includes/class-wc-swedbank-pay-refund.php +++ b/includes/class-wc-swedbank-pay-refund.php @@ -202,53 +202,55 @@ public static function refund( $gateway, $order, $amount, $reason ) { case 'line_item': /** @var WC_Order_Item_Product $item */ + $image = null; + $product_class = 'ProductGroup1'; + /** * @var WC_Product $product */ $product = $item->get_product(); + if ( $product ) { + // Get Product Sku + $reference = trim( + str_replace( + array( ' ', '.', ',' ), + '-', + $product->get_sku() + ) + ); - // Get Product Sku - $reference = trim( - str_replace( - array( ' ', '.', ',' ), - '-', - $product->get_sku() - ) - ); + // Get Product image + $image = wp_get_attachment_image_src( $product->get_image_id(), 'full' ); + if ( $image ) { + $image = array_shift( $image ); + } + + // Get Product Class + $product_class = $product->get_meta( '_sb_product_class' ); + + if ( empty( $product_class ) ) { + $product_class = apply_filters( + 'sb_product_class', + 'ProductGroup1', + $product + ); + } + } if ( empty( $reference ) ) { $reference = wp_generate_password( 12, false ); } - // Get Product image - $image = wp_get_attachment_image_src( $product->get_image_id(), 'full' ); - if ( $image ) { - $image = array_shift( $image ); - } else { + if ( empty( $image ) ) { $image = wc_placeholder_img_src( 'full' ); } if ( null === parse_url( $image, PHP_URL_SCHEME ) && - mb_substr( $image, 0, mb_strlen(WP_CONTENT_URL), 'UTF-8' ) === WP_CONTENT_URL + mb_substr( $image, 0, mb_strlen( WP_CONTENT_URL ), 'UTF-8' ) === WP_CONTENT_URL ) { $image = wp_guess_url() . $image; } - // Get Product Class - $product_class = get_post_meta( - $product->get_id(), - '_sb_product_class', - true - ); - - if ( empty( $product_class ) ) { - $product_class = apply_filters( - 'sb_product_class', - 'ProductGroup1', - $product - ); - } - // The field Reference must match the regular expression '[\\w-]*' $order_item[OrderItemInterface::FIELD_REFERENCE] = $reference; $order_item[OrderItemInterface::FIELD_TYPE] = OrderItemInterface::TYPE_PRODUCT; diff --git a/includes/class-wc-swedbank-plugin.php b/includes/class-wc-swedbank-plugin.php index 456518c..c1806cc 100644 --- a/includes/class-wc-swedbank-plugin.php +++ b/includes/class-wc-swedbank-plugin.php @@ -91,6 +91,7 @@ public function includes() { } } + require_once( dirname( __FILE__ ) . '/functions.php' ); require_once( dirname( __FILE__ ) . '/class-wc-swedbank-pay-transactions.php' ); require_once( dirname( __FILE__ ) . '/class-wc-swedbank-subscriptions.php' ); require_once( dirname( __FILE__ ) . '/class-wc-swedbank-pay-checkin.php' ); diff --git a/includes/class-wc-swedbank-subscriptions.php b/includes/class-wc-swedbank-subscriptions.php index 8362457..b8bdba4 100644 --- a/includes/class-wc-swedbank-subscriptions.php +++ b/includes/class-wc-swedbank-subscriptions.php @@ -150,7 +150,8 @@ public static function add_subscription_card_id( $order_id ) { */ public static function update_failing_payment_method( $subscription, $renewal_order ) { // Delete tokens - //delete_post_meta( $subscription->get_id(), '_payment_tokens' ); + //$subscription->delete_meta_data( '_payment_tokens' ); + //$subscription->save(); } /** @@ -164,7 +165,8 @@ public static function update_failing_payment_method( $subscription, $renewal_or */ public static function delete_resubscribe_meta( $resubscribe_order ) { // Delete tokens - delete_post_meta( $resubscribe_order->get_id(), '_payment_tokens' ); + $resubscribe_order->delete_meta_data( '_payment_tokens' ); + $resubscribe_order->save(); } /** @@ -270,7 +272,8 @@ public static function save_subscription_payment_meta( $subscription, $meta_tabl if ( 'swedbankpay_meta' === $meta_table && 'token_id' === $meta_key ) { // Delete tokens - delete_post_meta( $subscription->get_id(), '_payment_tokens' ); + $subscription->delete_meta_data( '_payment_tokens' ); + $subscription->save(); // Add tokens $tokens = explode( ',', $meta_value ); diff --git a/includes/functions.php b/includes/functions.php new file mode 100644 index 0000000..bb0d147 --- /dev/null +++ b/includes/functions.php @@ -0,0 +1,65 @@ +get_var( + $wpdb->prepare( + "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = %s AND meta_value = %s;", + $key, + $value + ) + ); + } + + $orders = wc_get_orders( + array( + 'return' => 'ids', + 'limit' => 1, + 'meta_query' => array( + array( + 'key' => $key, + 'value' => $value, + ), + ), + ) + ); + + if ( count( $orders ) > 0 ) { + $order = array_shift( $orders ); + + return $order->get_id(); + } + + return null; +} diff --git a/package.json b/package.json index 5b2083b..56835af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "swedbank-pay-woocommerce-checkout", - "version": "7.0.1", + "version": "7.1.0", "description": "", "main": "gulpfile.js", "dependencies": { diff --git a/readme.md b/readme.md index 4935b6e..23fdf31 100644 --- a/readme.md +++ b/readme.md @@ -16,7 +16,7 @@ This plugin provides the Swedbank Pay Checkout for WooCommerce. * Requires at least: 5.3 * Tested up to: 6.1.1 * Requires PHP: 7.0 -* Stable tag: 7.0.1 +* Stable tag: 7.1.0 * [License: Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0) ## Description diff --git a/readme.txt b/readme.txt index 8a49538..7dbbc1f 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: swedbankpay Tags: ecommerce, e-commerce, commerce, woothemes, wordpress ecommerce, swedbank, payex, payment gateway, woocommerce Requires at least: 5.3 Tested up to: 6.1.1 -Stable tag: 7.0.1 +Stable tag: 7.1.0 License: Apache License 2.0 License URI: http://www.apache.org/licenses/LICENSE-2.0 diff --git a/swedbank-pay-woocommerce-checkout.php b/swedbank-pay-woocommerce-checkout.php index 7447956..6b7daf8 100644 --- a/swedbank-pay-woocommerce-checkout.php +++ b/swedbank-pay-woocommerce-checkout.php @@ -7,11 +7,11 @@ * Author URI: https://profiles.wordpress.org/swedbankpay/ * License: Apache License 2.0 * License URI: http://www.apache.org/licenses/LICENSE-2.0 - * Version: 7.0.1 + * Version: 7.1.0 * Text Domain: swedbank-pay-woocommerce-checkout * Domain Path: /languages * WC requires at least: 5.5.1 - * WC tested up to: 7.4.1 + * WC tested up to: 7.5.1 */ use SwedbankPay\Checkout\WooCommerce\WC_Swedbank_Plugin; @@ -38,6 +38,15 @@ public function __construct() { // Actions add_action( 'plugins_loaded', array( $this, 'init' ), 0 ); add_action( 'woocommerce_loaded', array( $this, 'woocommerce_loaded' ), 30 ); + add_action( 'before_woocommerce_init', function() { + if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { + \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( + 'custom_order_tables', + __FILE__, + true + ); + } + } ); } /**