Merge pull request #2586 from woocommerce/PCP-3659-hook-into-things-to-do-next

Hook into the "Things to do next" section to add custom tasks (3659)
This commit is contained in:
Emili Castells 2024-09-13 12:05:43 +02:00 committed by GitHub
commit 51ad5404b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 370 additions and 0 deletions

View file

@ -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<array{
* id: string,
* title: string,
* description: string,
* redirect_url: string
* }>
*/
'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&section={$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;
},
);

View file

@ -0,0 +1,23 @@
<?php
/**
* A factory to create simple redirect task.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Settings
*/
namespace WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Factory;
use WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Tasks\SimpleRedirectTask;
/**
* A factory to create simple redirect task.
*/
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 );
}
}

View file

@ -0,0 +1,26 @@
<?php
/**
* Responsible for creating the simple redirect task.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Settings
*/
declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Factory;
use WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Tasks\SimpleRedirectTask;
interface SimpleRedirectTaskFactoryInterface {
/**
* Creates the simple redirect task.
*
* @param string $id The task ID.
* @param string $title The task title.
* @param string $description The task description.
* @param string $redirect_url The redirection URL.
* @return SimpleRedirectTask The task.
*/
public function create_task( string $id, string $title, string $description, string $redirect_url ): SimpleRedirectTask;
}

View file

@ -0,0 +1,32 @@
<?php
/**
* Registers the tasks inside the "Things to do next" WC section.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Settings
*/
namespace WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Registrar;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists;
use RuntimeException;
use WP_Error;
/**
* Registers the tasks inside the "Things to do next" WC section.
*/
class TaskRegistrar implements TaskRegistrarInterface {
/**
* {@inheritDoc}
*
* @throws RuntimeException If problem registering.
*/
public function register( array $tasks ): void {
foreach ( $tasks as $task ) {
$added_task = TaskLists::add_task( 'extended', $task );
if ( $added_task instanceof WP_Error ) {
throw new RuntimeException( $added_task->get_error_message() );
}
}
}
}

View file

@ -0,0 +1,25 @@
<?php
/**
* Responsible for registering the tasks inside the "Things to do next" WC section.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Settings
*/
declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Registrar;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
use RuntimeException;
interface TaskRegistrarInterface {
/**
* Registers the tasks inside "Things to do next" WC section.
*
* @param Task[] $tasks The list of tasks.
* @return void
* @throws RuntimeException If problem registering.
*/
public function register( array $tasks ): void;
}

View file

@ -0,0 +1,120 @@
<?php
/**
* Represents the Task for simple redirection. See "Things to do next" WC section.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Settings
*/
declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Tasks;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
/**
* Class SimpleRedirectTask
*/
class SimpleRedirectTask extends Task {
/**
* The task ID.
*
* @var string
*/
protected string $id;
/**
* The task title.
*
* @var string
*/
protected string $title;
/**
* The task description.
*
* @var string
*/
protected string $description;
/**
* The redirection URL.
*
* @var string
*/
protected string $redirect_url;
/**
* SimpleRedirectTask constructor.
*
* @param string $id The task ID.
* @param string $title The task title.
* @param string $description The task description.
* @param string $redirect_url The redirection URL.
*/
public function __construct( string $id, string $title, string $description, string $redirect_url ) {
parent::__construct();
$this->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();
}
}

View file

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