From 15a8a0d9d0e515973190cbc09c2e472cdc662699 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Wed, 11 Sep 2024 20:18:46 +0400 Subject: [PATCH] Add condition to check if pay later task should be enabled --- modules/ppcp-wc-gateway/services.php | 9 +++++--- .../Factory/SimpleRedirectTaskFactory.php | 4 ++-- .../SimpleRedirectTaskFactoryInterface.php | 3 ++- .../WcTasks/Registrar/TaskRegistrar.php | 5 +++++ .../WcTasks/Tasks/SimpleRedirectTask.php | 21 ++++++++++++++++++- 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index f487818c5..4bacab63a 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -1782,10 +1782,11 @@ return array( * id: string, * title: string, * description: string, - * redirect_url: string + * redirect_url: string, + * is_enabled: bool * }> */ - 'wcgateway.settings.wc-tasks.simple-redirect-tasks-config' => static function(): array { + 'wcgateway.settings.wc-tasks.simple-redirect-tasks-config' => static function( ContainerInterface $container ): array { $section_id = PayPalGateway::ID; $pay_later_tab_id = Settings::PAY_LATER_TAB_ID; @@ -1795,6 +1796,7 @@ return array( 'title' => __( 'Configure PayPal Pay Later messaging', 'woocommerce-paypal-payments' ), 'description' => __( 'Decide where you want dynamic Pay Later messaging to show up and how you want it to look on your site.', 'woocommerce-paypal-payments' ), 'redirect_url' => admin_url("admin.php?page=wc-settings&tab=checkout§ion={$section_id}&ppcp-tab={$pay_later_tab_id}"), + 'is_enabled' => $container->get('paylater-configurator.is-available'), ), ); }, @@ -1816,7 +1818,8 @@ return array( $title = $config['title'] ?? ''; $description = $config['description'] ?? ''; $redirect_url = $config['redirect_url'] ?? ''; - $simple_redirect_tasks[] = $simple_redirect_task_factory->create_task( $id, $title, $description, $redirect_url ); + $is_enabled = $config['is_enabled'] ?? false; + $simple_redirect_tasks[] = $simple_redirect_task_factory->create_task( $id, $title, $description, $redirect_url, $is_enabled ); } return $simple_redirect_tasks; diff --git a/modules/ppcp-wc-gateway/src/Settings/WcTasks/Factory/SimpleRedirectTaskFactory.php b/modules/ppcp-wc-gateway/src/Settings/WcTasks/Factory/SimpleRedirectTaskFactory.php index 7f9d6e893..be21d6cfa 100644 --- a/modules/ppcp-wc-gateway/src/Settings/WcTasks/Factory/SimpleRedirectTaskFactory.php +++ b/modules/ppcp-wc-gateway/src/Settings/WcTasks/Factory/SimpleRedirectTaskFactory.php @@ -17,7 +17,7 @@ class SimpleRedirectTaskFactory implements SimpleRedirectTaskFactoryInterface { /** * {@inheritDoc} */ - public function create_task( string $id, string $title, string $description, string $redirect_url ): SimpleRedirectTask { - return new SimpleRedirectTask( $id, $title, $description, $redirect_url ); + public function create_task( string $id, string $title, string $description, string $redirect_url, bool $is_enabled ): SimpleRedirectTask { + return new SimpleRedirectTask( $id, $title, $description, $redirect_url, $is_enabled ); } } diff --git a/modules/ppcp-wc-gateway/src/Settings/WcTasks/Factory/SimpleRedirectTaskFactoryInterface.php b/modules/ppcp-wc-gateway/src/Settings/WcTasks/Factory/SimpleRedirectTaskFactoryInterface.php index 8931622d4..f8d17b57f 100644 --- a/modules/ppcp-wc-gateway/src/Settings/WcTasks/Factory/SimpleRedirectTaskFactoryInterface.php +++ b/modules/ppcp-wc-gateway/src/Settings/WcTasks/Factory/SimpleRedirectTaskFactoryInterface.php @@ -20,7 +20,8 @@ interface SimpleRedirectTaskFactoryInterface { * @param string $title The task title. * @param string $description The task description. * @param string $redirect_url The redirection URL. + * @param bool $is_enabled Whether the task is enabled. * @return SimpleRedirectTask The task. */ - public function create_task( string $id, string $title, string $description, string $redirect_url ): SimpleRedirectTask; + public function create_task( string $id, string $title, string $description, string $redirect_url, bool $is_enabled ): SimpleRedirectTask; } diff --git a/modules/ppcp-wc-gateway/src/Settings/WcTasks/Registrar/TaskRegistrar.php b/modules/ppcp-wc-gateway/src/Settings/WcTasks/Registrar/TaskRegistrar.php index 174e60e6b..c710c7a09 100644 --- a/modules/ppcp-wc-gateway/src/Settings/WcTasks/Registrar/TaskRegistrar.php +++ b/modules/ppcp-wc-gateway/src/Settings/WcTasks/Registrar/TaskRegistrar.php @@ -9,6 +9,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Registrar; use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists; use RuntimeException; +use WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Tasks\SimpleRedirectTask; use WP_Error; /** @@ -23,6 +24,10 @@ class TaskRegistrar implements TaskRegistrarInterface { */ public function register( array $tasks ): void { foreach ( $tasks as $task ) { + if ( $task instanceof SimpleRedirectTask && ! $task->is_enabled() ) { + return; + } + $added_task = TaskLists::add_task( 'extended', $task ); if ( $added_task instanceof WP_Error ) { throw new RuntimeException( $added_task->get_error_message() ); diff --git a/modules/ppcp-wc-gateway/src/Settings/WcTasks/Tasks/SimpleRedirectTask.php b/modules/ppcp-wc-gateway/src/Settings/WcTasks/Tasks/SimpleRedirectTask.php index b187f6b17..104a9be6d 100644 --- a/modules/ppcp-wc-gateway/src/Settings/WcTasks/Tasks/SimpleRedirectTask.php +++ b/modules/ppcp-wc-gateway/src/Settings/WcTasks/Tasks/SimpleRedirectTask.php @@ -44,6 +44,14 @@ class SimpleRedirectTask extends Task { */ protected string $redirect_url; + /** + * Whether the task is enabled. + * + * @var bool + */ + protected bool $is_enabled; + + /** * SimpleRedirectTask constructor. * @@ -51,14 +59,16 @@ class SimpleRedirectTask extends Task { * @param string $title The task title. * @param string $description The task description. * @param string $redirect_url The redirection URL. + * @param bool $is_enabled Whether the task is enabled. */ - public function __construct( string $id, string $title, string $description, string $redirect_url ) { + public function __construct( string $id, string $title, string $description, string $redirect_url, bool $is_enabled ) { parent::__construct(); $this->id = $id; $this->title = $title; $this->description = $description; $this->redirect_url = $redirect_url; + $this->is_enabled = $is_enabled; } /** @@ -106,6 +116,15 @@ class SimpleRedirectTask extends Task { return $this->redirect_url; } + /** + * Whether the task is enabled. + * + * @return bool + */ + public function is_enabled(): bool { + return $this->is_enabled; + } + /** * The task completion. *