Add condition to check if pay later task should be enabled

This commit is contained in:
Narek Zakarian 2024-09-11 20:18:46 +04:00
parent a25c86477c
commit 15a8a0d9d0
No known key found for this signature in database
GPG key ID: 07AFD7E7A9C164A7
5 changed files with 35 additions and 7 deletions

View file

@ -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&section={$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;

View file

@ -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 );
}
}

View file

@ -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;
}

View file

@ -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() );

View file

@ -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.
*