From 88b3b3e590db764be51b61807f62e8e6cdc5a49d Mon Sep 17 00:00:00 2001 From: Nick Green Date: Wed, 27 Sep 2023 12:08:05 -0700 Subject: [PATCH 1/2] Add new feature to hide if non-admin users on front end --- includes/class-wc-gateway-dummy.php | 14 ++++++++++---- woocommerce-gateway-dummy.php | 6 +++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/includes/class-wc-gateway-dummy.php b/includes/class-wc-gateway-dummy.php index b6c3a88..89ebddc 100644 --- a/includes/class-wc-gateway-dummy.php +++ b/includes/class-wc-gateway-dummy.php @@ -47,9 +47,10 @@ public function __construct() { $this->init_settings(); // Define user set variables. - $this->title = $this->get_option( 'title' ); - $this->description = $this->get_option( 'description' ); - $this->instructions = $this->get_option( 'instructions', $this->description ); + $this->title = $this->get_option( 'title' ); + $this->description = $this->get_option( 'description' ); + $this->instructions = $this->get_option( 'instructions', $this->description ); + $this->hide_for_non_admin_users = $this->get_option( 'hide_for_non_admin_users' ); // Actions. add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); @@ -66,7 +67,12 @@ public function init_form_fields() { 'title' => __( 'Enable/Disable', 'woocommerce-gateway-dummy' ), 'type' => 'checkbox', 'label' => __( 'Enable Dummy Payments', 'woocommerce-gateway-dummy' ), - 'default' => 'yes' + 'default' => 'yes', + ), + 'hide_for_non_admin_users' => array( + 'type' => 'checkbox', + 'label' => __( 'Hide at checkout for non-admin users', 'woocommerce-gateway-dummy' ), + 'default' => 'no', ), 'title' => array( 'title' => __( 'Title', 'woocommerce-gateway-dummy' ), diff --git a/woocommerce-gateway-dummy.php b/woocommerce-gateway-dummy.php index f7382e7..344bbf4 100644 --- a/woocommerce-gateway-dummy.php +++ b/woocommerce-gateway-dummy.php @@ -53,7 +53,11 @@ public static function init() { * @param array */ public static function add_gateway( $gateways ) { - $gateways[] = 'WC_Gateway_Dummy'; + $options = get_option( 'woocommerce_dummy_settings', array() ); + $hide_for_non_admin_users = $options['hide_for_non_admin_users']; + if ( ( 'yes' === $hide_for_non_admin_users && current_user_can( 'manage_options' ) ) || 'no' === $hide_for_non_admin_users ) { + $gateways[] = 'WC_Gateway_Dummy'; + } return $gateways; } From 0820b6b86780173112b20c2027a33ad90981f6a3 Mon Sep 17 00:00:00 2001 From: Nick Green Date: Fri, 29 Sep 2023 14:36:38 -0700 Subject: [PATCH 2/2] Allow for backwards compatibility --- woocommerce-gateway-dummy.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/woocommerce-gateway-dummy.php b/woocommerce-gateway-dummy.php index 344bbf4..f0f236f 100644 --- a/woocommerce-gateway-dummy.php +++ b/woocommerce-gateway-dummy.php @@ -53,8 +53,15 @@ public static function init() { * @param array */ public static function add_gateway( $gateways ) { - $options = get_option( 'woocommerce_dummy_settings', array() ); - $hide_for_non_admin_users = $options['hide_for_non_admin_users']; + + $options = get_option( 'woocommerce_dummy_settings', array() ); + + if ( isset( $options['hide_for_non_admin_users'] ) ) { + $hide_for_non_admin_users = $options['hide_for_non_admin_users']; + } else { + $hide_for_non_admin_users = 'no'; + } + if ( ( 'yes' === $hide_for_non_admin_users && current_user_can( 'manage_options' ) ) || 'no' === $hide_for_non_admin_users ) { $gateways[] = 'WC_Gateway_Dummy'; } @@ -100,7 +107,7 @@ public static function woocommerce_gateway_dummy_woocommerce_block_support() { add_action( 'woocommerce_blocks_payment_method_type_registration', function( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) { - $payment_method_registry->register( new WC_Gateway_Dummy_Blocks_Support ); + $payment_method_registry->register( new WC_Gateway_Dummy_Blocks_Support() ); } ); }