From 2a421e399e51f1c4e1399c062ea54307b6eb5c33 Mon Sep 17 00:00:00 2001 From: Ivan Petak Date: Tue, 16 Jul 2024 19:06:13 +0200 Subject: [PATCH 1/3] Add checkout block support --- assets/src/js/kekspay-blocks.js | 20 ++++++++ assets/src/scss/kekspay.scss | 1 + .../core/class-kekspay-block-checkout.php | 50 +++++++++++++++++++ .../core/class-kekspay-payment-gateway.php | 25 +++++++++- includes/utilities/class-kekspay-data.php | 2 +- webpack.mix.js | 1 + woocommerce-gateway-kekspay.php | 29 +++++++++-- 7 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 assets/src/js/kekspay-blocks.js create mode 100644 includes/core/class-kekspay-block-checkout.php diff --git a/assets/src/js/kekspay-blocks.js b/assets/src/js/kekspay-blocks.js new file mode 100644 index 0000000..51f1e13 --- /dev/null +++ b/assets/src/js/kekspay-blocks.js @@ -0,0 +1,20 @@ +const settings = window.wc.wcSettings.getSetting( kekspayBlocksData.kekspayID + '_data', {} ); +const label = window.wp.htmlEntities.decodeEntities( settings.title ); + +const Content = () => { + return window.wp.htmlEntities.decodeEntities( settings.description ); +}; + +const Kekspay_Gateway = { + name: kekspayBlocksData.kekspayID, + label: label, + content: Object( window.wp.element.createElement )( Content, null ), + edit: Object( window.wp.element.createElement )( Content, null ), + canMakePayment: () => true, + ariaLabel: label, + supports: { + features: settings.supports, + }, +}; + +window.wc.wcBlocksRegistry.registerPaymentMethod( Kekspay_Gateway ); diff --git a/assets/src/scss/kekspay.scss b/assets/src/scss/kekspay.scss index 28ff13c..2a125a3 100644 --- a/assets/src/scss/kekspay.scss +++ b/assets/src/scss/kekspay.scss @@ -51,6 +51,7 @@ } .kekspay-logo { + display: block; width: 300px; height: 129px; margin: 0 auto; diff --git a/includes/core/class-kekspay-block-checkout.php b/includes/core/class-kekspay-block-checkout.php new file mode 100644 index 0000000..f11b246 --- /dev/null +++ b/includes/core/class-kekspay-block-checkout.php @@ -0,0 +1,50 @@ +settings = Kekspay_Data::get_settings(); + $this->gateway = Kekspay_Payment_Gateway::get_instance(); + } + + public function is_active() { + return $this->gateway->is_available(); + } + + public function get_payment_method_script_handles() { + wp_register_script( + 'kekspay-blocks-script', + KEKSPAY_DIR_URL . 'assets/dist/js/kekspay-blocks.js', + [ + 'wc-blocks-registry', + 'wc-settings', + 'wp-element', + 'wp-html-entities', + 'wp-i18n', + ], + KEKSPAY_PLUGIN_VERSION, + true + ); + + wp_localize_script( + 'kekspay-blocks-script', + 'kekspayBlocksData', + [ + 'kekspayID' => KEKSPAY_PLUGIN_ID, + ] + ); + + return [ 'kekspay-blocks-script' ]; + } + + public function get_payment_method_data() { + return [ + 'title' => $this->gateway->method_title, + 'description' => $this->gateway->method_description, + ]; + } +} diff --git a/includes/core/class-kekspay-payment-gateway.php b/includes/core/class-kekspay-payment-gateway.php index 6b3ab65..238944b 100644 --- a/includes/core/class-kekspay-payment-gateway.php +++ b/includes/core/class-kekspay-payment-gateway.php @@ -12,6 +12,12 @@ * Kekspay_Payment_Gateway class */ class Kekspay_Payment_Gateway extends WC_Payment_Gateway { + /** + * Instance of the current class, null before first usage. + * + * @var WC_Kekspay + */ + protected static $instance = null; /** * App data handler. @@ -42,6 +48,8 @@ public function __construct() { $this->title = esc_attr( Kekspay_Data::get_settings( 'title' ) ); $this->add_hooks(); + + self::$instance = $this; } /** @@ -49,7 +57,10 @@ public function __construct() { */ private function add_hooks() { add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ] ); - add_action( 'woocommerce_receipt_' . $this->id, [ $this, 'do_receipt_page' ] ); + + if ( ! self::$instance ) { + add_action( 'woocommerce_receipt_' . $this->id, [ $this, 'do_receipt_page' ] ); + } } /** @@ -217,5 +228,17 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { return Kekspay_Connector::refund( $order, $amount ); } + /** + * Return class instance. + * + * @static + * @return Kekspay_Payment_Gateway + */ + public static function get_instance() { + if ( is_null( self::$instance ) ) { + self::$instance = new self(); + } + return self::$instance; + } } } diff --git a/includes/utilities/class-kekspay-data.php b/includes/utilities/class-kekspay-data.php index e23d939..2ff9353 100644 --- a/includes/utilities/class-kekspay-data.php +++ b/includes/utilities/class-kekspay-data.php @@ -105,7 +105,7 @@ public static function test_mode() { self::load_settings(); } - return 'yes' === self::$settings['in-test-mode']; + return isset( self::$settings['in-test-mode'] ) && 'yes' === self::$settings['in-test-mode']; } /** diff --git a/webpack.mix.js b/webpack.mix.js index 8ecbad3..421617c 100644 --- a/webpack.mix.js +++ b/webpack.mix.js @@ -16,6 +16,7 @@ if (!mix.inProduction()) { } mix.setPublicPath("./"); // set because of this issue: https://github.com/JeffreyWay/laravel-mix/issues/1126 +mix.js("assets/src/js/kekspay-blocks.js", "assets/dist/js/kekspay-blocks.js"); mix.js("assets/src/js/kekspay-admin.js", "assets/dist/js/kekspay-admin.js"); mix.js("assets/src/js/kekspay.js", "assets/dist/js/kekspay.js"); diff --git a/woocommerce-gateway-kekspay.php b/woocommerce-gateway-kekspay.php index 9dd704c..f675f7e 100644 --- a/woocommerce-gateway-kekspay.php +++ b/woocommerce-gateway-kekspay.php @@ -51,17 +51,19 @@ function kekspay_admin_notice_missing_woocommerce() { } /** - * Declare Kekspay plugin compatible with HPOS. + * Declare Kekspay plugin compatibility for certain WooCommerce features: + * - HPOS + * - Checkout Blocks * * @return void */ -function kekspay_hpos_compatible() { +function kekspay_wc_features_compatibility() { if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); + \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true ); } } -add_action( 'before_woocommerce_init', 'kekspay_hpos_compatible' ); - +add_action( 'before_woocommerce_init', 'kekspay_wc_features_compatibility' ); if ( ! class_exists( 'WC_Kekspay' ) ) { /** @@ -106,6 +108,7 @@ protected function __construct() { add_action( 'admin_init', [ $this, 'check_for_other_kekspay_gateways' ], 1 ); add_action( 'activated_plugin', [ $this, 'set_kekspay_plugins_check_required' ] ); add_action( 'woocommerce_admin_field_payment_gateways', [ $this, 'set_kekspay_plugins_check_required' ] ); + add_action( 'woocommerce_blocks_loaded', [ $this, 'register_checkout_block_gateway' ] ); } /** @@ -243,6 +246,24 @@ public static function set_kekspay_plugins_check_required() { update_option( 'kekspay_plugins_check_required', 'yes' ); } + /** + * Register Kekspay method for block checkout. + */ + public function register_checkout_block_gateway() { + if ( ! class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) { + return; + } + + require_once KEKSPAY_DIR_PATH . 'includes/core/class-kekspay-block-checkout.php'; + + add_action( + 'woocommerce_blocks_payment_method_type_registration', + function( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) { + $payment_method_registry->register( new Kekspay_Block_Checkout() ); + } + ); + } + /** * Deactivate plugin. */ From b849cdfcafff9509dc6c63a89c728dca715ff82b Mon Sep 17 00:00:00 2001 From: Ivan Petak Date: Thu, 18 Jul 2024 17:03:01 +0200 Subject: [PATCH 2/3] Bump version numbers --- readme.txt | 2 +- woocommerce-gateway-kekspay.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/readme.txt b/readme.txt index 06ae7cf..7f8f8be 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Tags: kekspay, woocommerce, gateway, payment Requires at least: 6.3 Tested up to: 6.5 Requires PHP: 7.4 -Stable tag: 2.0.0 +Stable tag: 2.0.1 License: GPL v3 or later License URI: https://www.gnu.org/licenses/gpl-3.0.html diff --git a/woocommerce-gateway-kekspay.php b/woocommerce-gateway-kekspay.php index f675f7e..e5cd36a 100644 --- a/woocommerce-gateway-kekspay.php +++ b/woocommerce-gateway-kekspay.php @@ -3,7 +3,7 @@ * Plugin Name: KEKS Pay for WooCommerce * Plugin URI: https://www.kekspay.hr/ * Description: Incredibly fast and user friendly payment method created by Erste Bank Croatia. - * Version: 2.0.0 + * Version: 2.0.1 * Requires at least: 6.3 * Requires PHP: 7.4 * Author: Erste bank Croatia @@ -119,7 +119,7 @@ public static function register_constants() { define( 'KEKSPAY_PLUGIN_ID', 'erste-kekspay-woocommerce' ); } if ( ! defined( 'KEKSPAY_PLUGIN_VERSION' ) ) { - define( 'KEKSPAY_PLUGIN_VERSION', '2.0.0' ); + define( 'KEKSPAY_PLUGIN_VERSION', '2.0.1' ); } if ( ! defined( 'KEKSPAY_DIR_PATH' ) ) { define( 'KEKSPAY_DIR_PATH', plugin_dir_path( __FILE__ ) ); From 68e168acc249d97f722acf01ba5d7ef08ad63692 Mon Sep 17 00:00:00 2001 From: Ivan Petak Date: Tue, 22 Oct 2024 10:15:40 +0200 Subject: [PATCH 3/3] Bump version and tested numbers --- readme.txt | 7 +++++-- woocommerce-gateway-kekspay.php | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/readme.txt b/readme.txt index 7f8f8be..0a91893 100644 --- a/readme.txt +++ b/readme.txt @@ -2,9 +2,9 @@ Contributors: erstebank Tags: kekspay, woocommerce, gateway, payment Requires at least: 6.3 -Tested up to: 6.5 +Tested up to: 6.6 Requires PHP: 7.4 -Stable tag: 2.0.1 +Stable tag: 2.1.0 License: GPL v3 or later License URI: https://www.gnu.org/licenses/gpl-3.0.html @@ -49,6 +49,9 @@ For more installation options check the [official WordPress documentation](https == Changelog == += 2.1.0 = +* Add WooCommerce checkout blocks support. + = 2.0.0 = * Drop PHP 7.2 support. diff --git a/woocommerce-gateway-kekspay.php b/woocommerce-gateway-kekspay.php index e5cd36a..6bdd207 100644 --- a/woocommerce-gateway-kekspay.php +++ b/woocommerce-gateway-kekspay.php @@ -3,7 +3,7 @@ * Plugin Name: KEKS Pay for WooCommerce * Plugin URI: https://www.kekspay.hr/ * Description: Incredibly fast and user friendly payment method created by Erste Bank Croatia. - * Version: 2.0.1 + * Version: 2.1.0 * Requires at least: 6.3 * Requires PHP: 7.4 * Author: Erste bank Croatia @@ -14,7 +14,7 @@ * Domain Path: /languages * * WC requires at least: 8.2 - * WC tested up to: 8.8 + * WC tested up to: 9.3 */ if ( ! defined( 'ABSPATH' ) ) { @@ -119,7 +119,7 @@ public static function register_constants() { define( 'KEKSPAY_PLUGIN_ID', 'erste-kekspay-woocommerce' ); } if ( ! defined( 'KEKSPAY_PLUGIN_VERSION' ) ) { - define( 'KEKSPAY_PLUGIN_VERSION', '2.0.1' ); + define( 'KEKSPAY_PLUGIN_VERSION', '2.1.0' ); } if ( ! defined( 'KEKSPAY_DIR_PATH' ) ) { define( 'KEKSPAY_DIR_PATH', plugin_dir_path( __FILE__ ) );