mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 14:57:26 +08:00
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:
commit
51ad5404b3
9 changed files with 370 additions and 0 deletions
|
@ -29,6 +29,11 @@ use WooCommerce\PayPalCommerce\WcGateway\Endpoint\CaptureCardPayment;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint;
|
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Helper\CartCheckoutDetector;
|
use WooCommerce\PayPalCommerce\WcGateway\Helper\CartCheckoutDetector;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Helper\FeesUpdater;
|
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\WcSubscriptions\Helper\SubscriptionHelper;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer;
|
use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer;
|
||||||
|
@ -1762,4 +1767,62 @@ return array(
|
||||||
$container->get( 'woocommerce.logger.woocommerce' )
|
$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§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;
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -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 );
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
|
@ -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() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace WooCommerce\PayPalCommerce\WcGateway;
|
namespace WooCommerce\PayPalCommerce\WcGateway;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message;
|
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\WcGateway\Settings\SettingsRenderer;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\WcTasks\Registrar\TaskRegistrarInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class WcGatewayModule
|
* Class WcGatewayModule
|
||||||
|
@ -86,6 +88,7 @@ class WCGatewayModule implements ServiceModule, ExtendingModule, ExecutableModul
|
||||||
$this->register_order_functionality( $c );
|
$this->register_order_functionality( $c );
|
||||||
$this->register_columns( $c );
|
$this->register_columns( $c );
|
||||||
$this->register_checkout_paypal_address_preset( $c );
|
$this->register_checkout_paypal_address_preset( $c );
|
||||||
|
$this->register_wc_tasks( $c );
|
||||||
|
|
||||||
add_action(
|
add_action(
|
||||||
'woocommerce_sections_checkout',
|
'woocommerce_sections_checkout',
|
||||||
|
@ -831,4 +834,34 @@ class WCGatewayModule implements ServiceModule, ExtendingModule, ExecutableModul
|
||||||
2
|
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() );
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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_Payment_Gateway_CC.php';
|
||||||
require_once TESTS_ROOT_DIR . '/stubs/WC_Ajax.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/WC_Checkout.php';
|
||||||
|
require_once TESTS_ROOT_DIR . '/stubs/Task.php';
|
||||||
|
|
||||||
Hamcrest\Util::registerGlobalFunctions();
|
Hamcrest\Util::registerGlobalFunctions();
|
||||||
|
|
47
tests/stubs/Task.php
Normal file
47
tests/stubs/Task.php
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks;
|
||||||
|
|
||||||
|
use Automattic\WooCommerce\Internal\Admin\WCAdminUser;
|
||||||
|
|
||||||
|
abstract class Task
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param TaskList|null $task_list Parent task list.
|
||||||
|
*/
|
||||||
|
public function __construct( $task_list = null ) {
|
||||||
|
$this->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();
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue