diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 6ca2ecafe..a23de8457 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -29,6 +29,11 @@ use WooCommerce\PayPalCommerce\WcGateway\Endpoint\CaptureCardPayment; use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint; use WooCommerce\PayPalCommerce\WcGateway\Helper\CartCheckoutDetector; use WooCommerce\PayPalCommerce\WcGateway\Helper\FeesUpdater; +use WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Factory\SimpleRedirectTaskFactory; +use WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Factory\SimpleRedirectTaskFactoryInterface; +use WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Registrar\TaskRegistrar; +use WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Registrar\TaskRegistrarInterface; +use WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Tasks\SimpleRedirectTask; use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer; @@ -1762,4 +1767,62 @@ return array( $container->get( 'woocommerce.logger.woocommerce' ) ); }, + + 'wcgateway.settings.wc-tasks.simple-redirect-task-factory' => static function(): SimpleRedirectTaskFactoryInterface { + return new SimpleRedirectTaskFactory(); + }, + 'wcgateway.settings.wc-tasks.task-registrar' => static function(): TaskRegistrarInterface { + return new TaskRegistrar(); + }, + + /** + * A configuration for simple redirect wc tasks. + * + * @returns 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; + + $list_of_config = array(); + + if ( $container->get( 'paylater-configurator.is-available' ) ) { + $list_of_config[] = array( + 'id' => 'pay-later-messaging-task', + '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}" ), + ); + } + + return $list_of_config; + }, + + /** + * Retrieves the list of simple redirect task instances. + * + * @returns SimpleRedirectTask[] + */ + 'wcgateway.settings.wc-tasks.simple-redirect-tasks' => static function( ContainerInterface $container ): array { + $simple_redirect_tasks_config = $container->get( 'wcgateway.settings.wc-tasks.simple-redirect-tasks-config' ); + $simple_redirect_task_factory = $container->get( 'wcgateway.settings.wc-tasks.simple-redirect-task-factory' ); + assert( $simple_redirect_task_factory instanceof SimpleRedirectTaskFactoryInterface ); + + $simple_redirect_tasks = array(); + + foreach ( $simple_redirect_tasks_config as $config ) { + $id = $config['id'] ?? ''; + $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 ); + } + + 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 new file mode 100644 index 000000000..7f9d6e893 --- /dev/null +++ b/modules/ppcp-wc-gateway/src/Settings/WcTasks/Factory/SimpleRedirectTaskFactory.php @@ -0,0 +1,23 @@ +get_error_message() ); + } + } + } +} diff --git a/modules/ppcp-wc-gateway/src/Settings/WcTasks/Registrar/TaskRegistrarInterface.php b/modules/ppcp-wc-gateway/src/Settings/WcTasks/Registrar/TaskRegistrarInterface.php new file mode 100644 index 000000000..992b41150 --- /dev/null +++ b/modules/ppcp-wc-gateway/src/Settings/WcTasks/Registrar/TaskRegistrarInterface.php @@ -0,0 +1,25 @@ +id = $id; + $this->title = $title; + $this->description = $description; + $this->redirect_url = $redirect_url; + } + + /** + * The task ID. + * + * @return string + */ + public function get_id(): string { + return $this->id; + } + + /** + * The task title. + * + * @return string + */ + public function get_title(): string { + return $this->title; + } + + /** + * The task content. + * + * @return string + */ + public function get_content(): string { + return ''; + } + + /** + * The task time. + * + * @return string + */ + public function get_time(): string { + return $this->description; + } + + /** + * The task redirection URL. + * + * @return string + */ + public function get_action_url(): string { + return $this->redirect_url; + } + + /** + * The task completion. + * + * We need to set the task completed when the redirection happened for the first time. + * So this method of a parent class should be overridden. + * + * @return bool + */ + public function is_complete(): bool { + return parent::is_visited(); + } +} diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index 09cceda99..5a7ca2c62 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\WcGateway; +use Exception; use Psr\Log\LoggerInterface; use Throwable; use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message; @@ -55,6 +56,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsListener; use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsRenderer; use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; +use WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Registrar\TaskRegistrarInterface; /** * Class WcGatewayModule @@ -86,6 +88,7 @@ class WCGatewayModule implements ServiceModule, ExtendingModule, ExecutableModul $this->register_order_functionality( $c ); $this->register_columns( $c ); $this->register_checkout_paypal_address_preset( $c ); + $this->register_wc_tasks( $c ); add_action( 'woocommerce_sections_checkout', @@ -831,4 +834,34 @@ class WCGatewayModule implements ServiceModule, ExtendingModule, ExecutableModul 2 ); } + + /** + * Registers the tasks inside "Things to do next" WC section. + * + * @param ContainerInterface $container The container. + * @return void + */ + protected function register_wc_tasks( ContainerInterface $container ): void { + $simple_redirect_tasks = $container->get( 'wcgateway.settings.wc-tasks.simple-redirect-tasks' ); + if ( empty( $simple_redirect_tasks ) ) { + return; + } + + $task_registrar = $container->get( 'wcgateway.settings.wc-tasks.task-registrar' ); + assert( $task_registrar instanceof TaskRegistrarInterface ); + + $logger = $container->get( 'woocommerce.logger.woocommerce' ); + assert( $logger instanceof LoggerInterface ); + + add_action( + 'init', + static function () use ( $simple_redirect_tasks, $task_registrar, $logger ): void { + try { + $task_registrar->register( $simple_redirect_tasks ); + } catch ( Exception $exception ) { + $logger->error( "Failed to create a task in the 'Things to do next' section of WC. " . $exception->getMessage() ); + } + }, + ); + } } diff --git a/tests/PHPUnit/bootstrap.php b/tests/PHPUnit/bootstrap.php index 8910b0241..647473e12 100644 --- a/tests/PHPUnit/bootstrap.php +++ b/tests/PHPUnit/bootstrap.php @@ -10,5 +10,6 @@ require_once TESTS_ROOT_DIR . '/stubs/WC_Payment_Gateway.php'; require_once TESTS_ROOT_DIR . '/stubs/WC_Payment_Gateway_CC.php'; require_once TESTS_ROOT_DIR . '/stubs/WC_Ajax.php'; require_once TESTS_ROOT_DIR . '/stubs/WC_Checkout.php'; +require_once TESTS_ROOT_DIR . '/stubs/Task.php'; Hamcrest\Util::registerGlobalFunctions(); diff --git a/tests/stubs/Task.php b/tests/stubs/Task.php new file mode 100644 index 000000000..0a577cebe --- /dev/null +++ b/tests/stubs/Task.php @@ -0,0 +1,47 @@ +task_list = $task_list; + } + + /** + * ID. + * + * @return string + */ + abstract public function get_id(); + + /** + * Title. + * + * @return string + */ + abstract public function get_title(); + + /** + * Content. + * + * @return string + */ + abstract public function get_content(); + + /** + * Time. + * + * @return string + */ + abstract public function get_time(); +}