Update Payments menu item display and linking behavior (#51541)

* Minor cleanup

* refact: Rename must_be_visible to is_incentive_visible for clarity

* refact: Rename register_payments_welcome_page to register_menu_and_page for clarity

* Don't show the incentive page if the user can't manage WC

* Introduce menu item when no incentive

* Don't recommend additional gateways when WCPay is present

* Render gateway suggestions on the WCPay task page, also

* Add changelog entry

* Remove short array syntax

* Lint fixes

* Simplify Payments task view logic

* Simplify WooPayments task view logic

* Improve WooPayments task complete logic

* WooPayments can't be connected or onboarded if it is not active

* refact: Rename private static method

* Improve logic to determine the Payments task slug by factoring in the extended task list also

* docs: Add details about our use of the Payments task logic

* Add type hint

* Do not recommend offline methods when WooPayments is supported

* On suggestion enablement redirect to the same task page

* Lint fixes

* test: Fix unit tests

* Maintain deprecated navigation logic
This commit is contained in:
Vlad Olaru 2024-09-20 15:53:10 +03:00 committed by GitHub
parent ad2d18200f
commit 1ff28fd174
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 249 additions and 131 deletions

View File

@ -13,7 +13,7 @@ import { recordEvent } from '@woocommerce/tracks';
import { useMemo, useCallback, useEffect } from '@wordpress/element'; import { useMemo, useCallback, useEffect } from '@wordpress/element';
import { registerPlugin } from '@wordpress/plugins'; import { registerPlugin } from '@wordpress/plugins';
import { WooOnboardingTask } from '@woocommerce/onboarding'; import { WooOnboardingTask } from '@woocommerce/onboarding';
import { getNewPath } from '@woocommerce/navigation'; import { getNewPath, getQuery } from '@woocommerce/navigation';
import { Button } from '@wordpress/components'; import { Button } from '@wordpress/components';
import ExternalIcon from 'gridicons/dist/external'; import ExternalIcon from 'gridicons/dist/external';
@ -91,11 +91,15 @@ export const PaymentGatewaySuggestions = ( { onComplete, query } ) => {
// gateway variable doesn't have hasPlugins property. // gateway variable doesn't have hasPlugins property.
! paymentGateways.get( id )?.hasPlugins ! paymentGateways.get( id )?.hasPlugins
? { ? {
redirectPath: getNewPath( // If we are already on a task page, don't redirect.
{ task: 'payments' }, // Otherwise, redirect to Payments task page.
{}, redirectPath: getQuery()?.task
'/' ? getNewPath(
), { task: getQuery().task },
{},
'/'
)
: getNewPath( { task: 'payments' }, {}, '/' ),
} }
: {} : {}
); );
@ -245,7 +249,8 @@ export const PaymentGatewaySuggestions = ( { onComplete, query } ) => {
<List <List
heading={ additionalSectionHeading } heading={ additionalSectionHeading }
headingDescription={ additionalSectionHeadingDescription } headingDescription={ additionalSectionHeadingDescription }
recommendation={ recommendation } // No recommendation if WooPayments is supported (and displayed).
recommendation={ isWCPaySupported ? false : recommendation }
paymentGateways={ additionalGateways } paymentGateways={ additionalGateways }
markConfigured={ markConfigured } markConfigured={ markConfigured }
footerLink={ footerLink={
@ -265,7 +270,8 @@ export const PaymentGatewaySuggestions = ( { onComplete, query } ) => {
const offlineSection = !! offlineGateways.length && ( const offlineSection = !! offlineGateways.length && (
<List <List
heading={ __( 'Offline payment methods', 'woocommerce' ) } heading={ __( 'Offline payment methods', 'woocommerce' ) }
recommendation={ recommendation } // No recommendation if WooPayments is supported (and displayed).
recommendation={ isWCPaySupported ? false : recommendation }
paymentGateways={ offlineGateways } paymentGateways={ offlineGateways }
markConfigured={ markConfigured } markConfigured={ markConfigured }
/> />

View File

@ -1,7 +1,7 @@
/** /**
* External dependencies * External dependencies
*/ */
import React, { useEffect } from 'react'; import React from 'react';
import { registerPlugin } from '@wordpress/plugins'; import { registerPlugin } from '@wordpress/plugins';
import { import {
WooOnboardingTaskListItem, WooOnboardingTaskListItem,
@ -9,12 +9,12 @@ import {
} from '@woocommerce/onboarding'; } from '@woocommerce/onboarding';
import { PLUGINS_STORE_NAME } from '@woocommerce/data'; import { PLUGINS_STORE_NAME } from '@woocommerce/data';
import { useDispatch } from '@wordpress/data'; import { useDispatch } from '@wordpress/data';
import { Spinner } from '@woocommerce/components';
/** /**
* Internal dependencies * Internal dependencies
*/ */
import { installActivateAndConnectWcpay } from './PaymentGatewaySuggestions/components/WCPay'; import { installActivateAndConnectWcpay } from './PaymentGatewaySuggestions/components/WCPay';
import { PaymentGatewaySuggestions } from './PaymentGatewaySuggestions';
const WoocommercePaymentsTaskItem = () => { const WoocommercePaymentsTaskItem = () => {
const { installAndActivatePlugins } = useDispatch( PLUGINS_STORE_NAME ); const { installAndActivatePlugins } = useDispatch( PLUGINS_STORE_NAME );
@ -52,42 +52,21 @@ registerPlugin( 'woocommerce-admin-task-wcpay', {
render: WoocommercePaymentsTaskItem, render: WoocommercePaymentsTaskItem,
} ); } );
const ReadyWcPay = () => { // Shows up at http://host/wp-admin/admin.php?page=wc-admin&task=woocommerce-payments which is the default url for woocommerce-payments task
const { installAndActivatePlugins } = useDispatch( PLUGINS_STORE_NAME );
const { createNotice } = useDispatch( 'core/notices' );
useEffect( () => {
new Promise( ( resolve, reject ) => {
return installActivateAndConnectWcpay(
reject,
createNotice,
installAndActivatePlugins
);
} );
}, [ createNotice, installAndActivatePlugins ] );
return (
<div
style={ {
height: '70vh',
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
} }
>
<Spinner />
<div style={ { marginTop: '1rem' } }>
Preparing payment settings...
</div>
</div>
);
};
// shows up at http://host/wp-admin/admin.php?page=wc-admin&task=woocommerce-payments which is the default url for woocommerce-payments task
const WoocommercePaymentsTaskPage = () => ( const WoocommercePaymentsTaskPage = () => (
<WooOnboardingTask id="woocommerce-payments"> <WooOnboardingTask id="woocommerce-payments">
<ReadyWcPay /> { ( {
onComplete,
query,
}: {
onComplete: () => void;
query: { id: string };
} ) => (
<PaymentGatewaySuggestions
onComplete={ onComplete }
query={ query }
/>
) }
</WooOnboardingTask> </WooOnboardingTask>
); );

View File

@ -0,0 +1,5 @@
Significance: patch
Type: update
Comment: Detach the Payment menu item and page from an active incentive.

View File

@ -4,8 +4,6 @@
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks; namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Admin\Features\Features; use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\Payments;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\WooCommercePayments;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\Init; use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\Init;
/** /**
@ -98,7 +96,7 @@ class AdditionalPayments extends Payments {
return $this->can_view_result; return $this->can_view_result;
} }
// Show task if woocommerce-payments is connected or if there are any suggested gateways in other category enabled. // Show task if WooPayments is connected or if there are any suggested gateways in other category enabled.
$this->can_view_result = ( $this->can_view_result = (
WooCommercePayments::is_connected() || WooCommercePayments::is_connected() ||
self::has_enabled_other_category_gateways() self::has_enabled_other_category_gateways()

View File

@ -76,13 +76,13 @@ class Payments extends Task {
* @return bool * @return bool
*/ */
public function can_view() { public function can_view() {
$woocommerce_payments = $this->task_list->get_task( 'woocommerce-payments' ); // The task is visible if WooPayments is not supported in the current store location country.
// Make sure the task is mutually exclusive with the WooPayments task. // Otherwise, the WooPayments task will be shown.
return Features::is_enabled( 'payment-gateway-suggestions' ) && ! $woocommerce_payments->can_view(); return Features::is_enabled( 'payment-gateway-suggestions' ) && ! WooCommercePayments::is_supported();
} }
/** /**
* Check if the store has any enabled gateways, other than WooPayments. * Check if the store has any enabled gateways.
* *
* @return bool * @return bool
*/ */
@ -91,8 +91,7 @@ class Payments extends Task {
$enabled_gateways = array_filter( $enabled_gateways = array_filter(
$gateways, $gateways,
function( $gateway ) { function( $gateway ) {
// Filter out any WooPayments gateways as this task is mutually exclusive with the WooPayments task. return 'yes' === $gateway->enabled;
return 'yes' === $gateway->enabled && 0 !== strpos( $gateway->id, 'woocommerce_payments' );
} }
); );
@ -117,7 +116,7 @@ class Payments extends Task {
} }
// Check if there is an active WooPayments incentive via the welcome page. // Check if there is an active WooPayments incentive via the welcome page.
if ( WcPayWelcomePage::instance()->must_be_visible() ) { if ( WcPayWelcomePage::instance()->is_incentive_visible() ) {
// Point to the WooPayments welcome page. // Point to the WooPayments welcome page.
return add_query_arg( 'from', 'WCADMIN_PAYMENT_TASK', admin_url( 'admin.php?page=wc-admin&path=/wc-pay-welcome-page' ) ); return add_query_arg( 'from', 'WCADMIN_PAYMENT_TASK', admin_url( 'admin.php?page=wc-admin&path=/wc-pay-welcome-page' ) );
} }

View File

@ -105,7 +105,9 @@ class WooCommercePayments extends Task {
*/ */
public function is_complete() { public function is_complete() {
if ( null === $this->is_complete_result ) { if ( null === $this->is_complete_result ) {
$this->is_complete_result = self::is_connected() && ! self::is_account_partially_onboarded(); // This task is complete if there are other ecommerce gateways enabled (offline payment methods are excluded),
// or if WooPayments is active and has a connected, fully onboarded account.
$this->is_complete_result = self::has_other_ecommerce_gateways() || ( self::is_connected() && ! self::is_account_partially_onboarded() );
} }
return $this->is_complete_result; return $this->is_complete_result;
@ -117,10 +119,7 @@ class WooCommercePayments extends Task {
* @return bool * @return bool
*/ */
public function can_view() { public function can_view() {
$payments = $this->task_list->get_task( 'payments' ); return self::is_supported();
return ! $payments->is_complete() && // Do not re-display the task if the general "Payments" task has already been completed.
self::is_supported();
} }
/** /**
@ -162,7 +161,11 @@ class WooCommercePayments extends Task {
* @return bool * @return bool
*/ */
public static function is_connected() { public static function is_connected() {
$wc_payments_gateway = self::get_woo_payments_gateway(); if ( ! self::is_wcpay_active() ) {
return false;
}
$wc_payments_gateway = self::get_gateway();
if ( $wc_payments_gateway && method_exists( $wc_payments_gateway, 'is_connected' ) ) { if ( $wc_payments_gateway && method_exists( $wc_payments_gateway, 'is_connected' ) ) {
return $wc_payments_gateway->is_connected(); return $wc_payments_gateway->is_connected();
} }
@ -177,7 +180,11 @@ class WooCommercePayments extends Task {
* @return bool * @return bool
*/ */
public static function is_account_partially_onboarded() { public static function is_account_partially_onboarded() {
$wc_payments_gateway = self::get_woo_payments_gateway(); if ( ! self::is_wcpay_active() ) {
return false;
}
$wc_payments_gateway = self::get_gateway();
if ( $wc_payments_gateway && method_exists( $wc_payments_gateway, 'is_account_partially_onboarded' ) ) { if ( $wc_payments_gateway && method_exists( $wc_payments_gateway, 'is_account_partially_onboarded' ) ) {
return $wc_payments_gateway->is_account_partially_onboarded(); return $wc_payments_gateway->is_account_partially_onboarded();
} }
@ -212,11 +219,33 @@ class WooCommercePayments extends Task {
* *
* @return \WC_Payments|null * @return \WC_Payments|null
*/ */
private static function get_woo_payments_gateway() { private static function get_gateway() {
$payment_gateways = WC()->payment_gateways->payment_gateways(); $payment_gateways = WC()->payment_gateways->payment_gateways();
if ( isset( $payment_gateways['woocommerce_payments'] ) ) { if ( isset( $payment_gateways['woocommerce_payments'] ) ) {
return $payment_gateways['woocommerce_payments']; return $payment_gateways['woocommerce_payments'];
} }
return null; return null;
} }
/**
* Check if the store has any enabled ecommerce gateways, other than WooPayments.
*
* We exclude offline payment methods from this check.
*
* @return bool
*/
public static function has_other_ecommerce_gateways(): bool {
$gateways = WC()->payment_gateways->get_available_payment_gateways();
$enabled_gateways = array_filter(
$gateways,
function ( $gateway ) {
// Filter out any WooPayments-related or offline gateways.
return 'yes' === $gateway->enabled
&& 0 !== strpos( $gateway->id, 'woocommerce_payments' )
&& ! in_array( $gateway->id, array( 'bacs', 'cheque', 'cod' ), true );
}
);
return ! empty( $enabled_gateways );
}
} }

View File

@ -38,8 +38,8 @@ class PaymentsMoreInfoNeeded {
* @return bool * @return bool
*/ */
public static function should_display_note() { public static function should_display_note() {
// WCPay welcome page must not be visible. // WooPayments incentive must not be visible.
if ( WcPayWelcomePage::instance()->must_be_visible() ) { if ( WcPayWelcomePage::instance()->is_incentive_visible() ) {
return false; return false;
} }

View File

@ -38,8 +38,8 @@ class PaymentsRemindMeLater {
* @return bool * @return bool
*/ */
public static function should_display_note() { public static function should_display_note() {
// WCPay welcome page must be visible. // WooPayments incentive must be visible.
if ( ! WcPayWelcomePage::instance()->must_be_visible() ) { if ( ! WcPayWelcomePage::instance()->is_incentive_visible() ) {
return false; return false;
} }

View File

@ -2,6 +2,8 @@
namespace Automattic\WooCommerce\Internal\Admin; namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists;
use Automattic\WooCommerce\Admin\WCAdminHelper; use Automattic\WooCommerce\Admin\WCAdminHelper;
use Automattic\WooCommerce\Admin\PageController; use Automattic\WooCommerce\Admin\PageController;
use WC_Abstract_Order; use WC_Abstract_Order;
@ -43,7 +45,7 @@ class WcPayWelcomePage {
* WCPayWelcomePage constructor. * WCPayWelcomePage constructor.
*/ */
public function __construct() { public function __construct() {
add_action( 'admin_menu', array( $this, 'register_payments_welcome_page' ) ); add_action( 'admin_menu', array( $this, 'register_menu_and_page' ) );
add_filter( 'woocommerce_admin_shared_settings', array( $this, 'shared_settings' ) ); add_filter( 'woocommerce_admin_shared_settings', array( $this, 'shared_settings' ) );
add_filter( 'woocommerce_admin_allowed_promo_notes', array( $this, 'allowed_promo_notes' ) ); add_filter( 'woocommerce_admin_allowed_promo_notes', array( $this, 'allowed_promo_notes' ) );
add_filter( 'woocommerce_admin_woopayments_onboarding_task_badge', array( $this, 'onboarding_task_badge' ) ); add_filter( 'woocommerce_admin_woopayments_onboarding_task_badge', array( $this, 'onboarding_task_badge' ) );
@ -51,18 +53,23 @@ class WcPayWelcomePage {
} }
/** /**
* Whether the WooPayments welcome page should be visible. * Whether the WooPayments incentive should be visible.
* *
* @param bool $skip_wcpay_active Whether to skip the check for the WooPayments plugin being active. * @param bool $skip_wcpay_active Whether to skip the check for the WooPayments plugin being active.
* *
* @return boolean * @return boolean
*/ */
public function must_be_visible( $skip_wcpay_active = false ): bool { public function is_incentive_visible( bool $skip_wcpay_active = false ): bool {
// The WooPayments plugin must not be active. // The WooPayments plugin must not be active.
if ( ! $skip_wcpay_active && $this->is_wcpay_active() ) { if ( ! $skip_wcpay_active && $this->is_wcpay_active() ) {
return false; return false;
} }
// The current WP user must have the capabilities required to set up WooPayments.
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return false;
}
// Suggestions not disabled via a setting. // Suggestions not disabled via a setting.
if ( get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) === 'no' ) { if ( get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) === 'no' ) {
return false; return false;
@ -95,61 +102,89 @@ class WcPayWelcomePage {
/** /**
* Registers the WooPayments welcome page. * Registers the WooPayments welcome page.
*/ */
public function register_payments_welcome_page() { public function register_menu_and_page() {
global $menu; global $menu;
if ( ! $this->must_be_visible() ) { // The WooPayments plugin must not be active.
if ( $this->is_wcpay_active() ) {
return; return;
} }
$menu_icon = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI4NTIiIGhlaWdodD0iNjg0Ij48cGF0aCBmaWxsPSIjYTJhYWIyIiBkPSJNODIgODZ2NTEyaDY4NFY4NlptMCA1OThjLTQ4IDAtODQtMzgtODQtODZWODZDLTIgMzggMzQgMCA4MiAwaDY4NGM0OCAwIDg0IDM4IDg0IDg2djUxMmMwIDQ4LTM2IDg2LTg0IDg2em0zODQtNTU2djQ0aDg2djg0SDM4MnY0NGgxMjhjMjQgMCA0MiAxOCA0MiA0MnYxMjhjMCAyNC0xOCA0Mi00MiA0MmgtNDR2NDRoLTg0di00NGgtODZ2LTg0aDE3MHYtNDRIMzM4Yy0yNCAwLTQyLTE4LTQyLTQyVjIxNGMwLTI0IDE4LTQyIDQyLTQyaDQ0di00NHoiLz48L3N2Zz4='; $menu_title = esc_html__( 'Payments', 'woocommerce' );
$menu_icon = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI4NTIiIGhlaWdodD0iNjg0Ij48cGF0aCBmaWxsPSIjYTJhYWIyIiBkPSJNODIgODZ2NTEyaDY4NFY4NlptMCA1OThjLTQ4IDAtODQtMzgtODQtODZWODZDLTIgMzggMzQgMCA4MiAwaDY4NGM0OCAwIDg0IDM4IDg0IDg2djUxMmMwIDQ4LTM2IDg2LTg0IDg2em0zODQtNTU2djQ0aDg2djg0SDM4MnY0NGgxMjhjMjQgMCA0MiAxOCA0MiA0MnYxMjhjMCAyNC0xOCA0Mi00MiA0MmgtNDR2NDRoLTg0di00NGgtODZ2LTg0aDE3MHYtNDRIMzM4Yy0yNCAwLTQyLTE4LTQyLTQyVjIxNGMwLTI0IDE4LTQyIDQyLTQyaDQ0di00NHoiLz48L3N2Zz4=';
$menu_data = [ // If an incentive is visible, we register the WooPayments welcome/incentives page.
'id' => 'wc-calypso-bridge-payments-welcome-page', // Otherwise, we register a menu item that links to the Payments task page.
'title' => esc_html__( 'Payments', 'woocommerce' ), if ( $this->is_incentive_visible() ) {
'path' => '/wc-pay-welcome-page', $page_id = 'wc-calypso-bridge-payments-welcome-page';
'position' => '56', $page_options = array(
'nav_args' => [ 'id' => $page_id,
'title' => esc_html__( 'WooPayments', 'woocommerce' ), 'title' => $menu_title,
'is_category' => false, 'capability' => 'manage_woocommerce',
'menuId' => 'plugins', 'path' => '/wc-pay-welcome-page',
'is_top_level' => true, 'position' => '56',
], 'nav_args' => array(
'icon' => $menu_icon, 'title' => 'WooPayments', // Do not translate the brand name.
]; 'is_category' => false,
'menuId' => 'plugins',
'is_top_level' => true,
),
'icon' => $menu_icon,
);
wc_admin_register_page( $menu_data ); wc_admin_register_page( $page_options );
// Registering a top level menu via wc_admin_register_page doesn't work when the new $menu_path = PageController::get_instance()->get_path_from_id( $page_id );
// nav is enabled. The new nav disabled everything, except the 'WooCommerce' menu.
// We need to register this menu via add_menu_page so that it doesn't become a child of // Registering a top level menu via wc_admin_register_page doesn't work when the new
// WooCommerce menu. // nav is enabled. The new nav disabled everything, except the 'WooCommerce' menu.
if ( get_option( 'woocommerce_navigation_enabled', 'no' ) === 'yes' ) { // We need to register this menu via add_menu_page so that it doesn't become a child of
$menu_with_nav_data = [ // WooCommerce menu.
esc_html__( 'Payments', 'woocommerce' ), if ( get_option( 'woocommerce_navigation_enabled', 'no' ) === 'yes' ) {
esc_html__( 'Payments', 'woocommerce' ), $menu_path = 'admin.php?page=wc-admin&path=/wc-pay-welcome-page';
'view_woocommerce_reports', $menu_with_nav_data = array(
'admin.php?page=wc-admin&path=/wc-pay-welcome-page', $menu_title,
$menu_title,
'manage_woocommerce',
$menu_path,
null,
$menu_icon,
56,
);
call_user_func_array( 'add_menu_page', $menu_with_nav_data );
}
} else {
// Determine the path to the active Payments task page.
$menu_path = 'admin.php?page=wc-admin&task=' . $this->get_active_payments_task_slug();
add_menu_page(
$menu_title,
$menu_title,
'manage_woocommerce',
$menu_path,
null, null,
$menu_icon, $menu_icon,
56, 56,
]; );
call_user_func_array( 'add_menu_page', $menu_with_nav_data );
} }
// Add badge. // Maybe add a badge to the menu.
$badge = ' <span class="wcpay-menu-badge awaiting-mod count-1"><span class="plugin-count">1</span></span>'; // If the main Payments task is not complete, we add a badge to the Payments menu item.
foreach ( $menu as $index => $menu_item ) { // We use the complete logic of the main Payments task because it is the most general one.
// Only add the badge markup if not already present and the menu item is the WooPayments menu item. if ( ! empty( $this->get_payments_task() ) && ! $this->is_payments_task_complete() ) {
if ( false === strpos( $menu_item[0], $badge ) $badge = ' <span class="wcpay-menu-badge awaiting-mod count-1"><span class="plugin-count">1</span></span>';
&& ( 'wc-admin&path=/wc-pay-welcome-page' === $menu_item[2] foreach ( $menu as $index => $menu_item ) {
|| 'admin.php?page=wc-admin&path=/wc-pay-welcome-page' === $menu_item[2] ) // Only add the badge markup if not already present and the menu item is the Payments menu item.
) { if ( 0 === strpos( $menu_item[0], $menu_title )
$menu[ $index ][0] .= $badge; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited && $menu_path === $menu_item[2]
&& false === strpos( $menu_item[0], $badge ) ) {
// One menu item with a badge is more than enough. $menu[ $index ][0] .= $badge; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
break;
// One menu item with a badge is more than enough.
break;
}
} }
} }
} }
@ -167,7 +202,7 @@ class WcPayWelcomePage {
} }
// Return early if the incentive must not be visible. // Return early if the incentive must not be visible.
if ( ! $this->must_be_visible() ) { if ( ! $this->is_incentive_visible() ) {
return $settings; return $settings;
} }
@ -182,11 +217,11 @@ class WcPayWelcomePage {
* @param array $promo_notes Allowed promo notes. * @param array $promo_notes Allowed promo notes.
* @return array * @return array
*/ */
public function allowed_promo_notes( $promo_notes = [] ): array { public function allowed_promo_notes( $promo_notes = array() ): array {
// Note: We need to disregard if WooPayments is active when adding the promo note to the list of // Note: We need to disregard if WooPayments is active when adding the promo note to the list of
// allowed promo notes. The AJAX call that adds the promo note happens after WooPayments is installed and activated. // allowed promo notes. The AJAX call that adds the promo note happens after WooPayments is installed and activated.
// Return early if the incentive page must not be visible, without checking if WooPayments is active. // Return early if the incentive page must not be visible, without checking if WooPayments is active.
if ( ! $this->must_be_visible( true ) ) { if ( ! $this->is_incentive_visible( true ) ) {
return $promo_notes; return $promo_notes;
} }
@ -205,7 +240,7 @@ class WcPayWelcomePage {
*/ */
public function onboarding_task_badge( string $badge ): string { public function onboarding_task_badge( string $badge ): string {
// Return early if the incentive must not be visible. // Return early if the incentive must not be visible.
if ( ! $this->must_be_visible() ) { if ( ! $this->is_incentive_visible() ) {
return $badge; return $badge;
} }
@ -221,7 +256,7 @@ class WcPayWelcomePage {
*/ */
public function onboarding_task_additional_data( ?array $additional_data ): ?array { public function onboarding_task_additional_data( ?array $additional_data ): ?array {
// Return early if the incentive must not be visible. // Return early if the incentive must not be visible.
if ( ! $this->must_be_visible() ) { if ( ! $this->is_incentive_visible() ) {
return $additional_data; return $additional_data;
} }
@ -266,11 +301,11 @@ class WcPayWelcomePage {
// If there is at least one order processed with WooPayments, we consider the store to have WooPayments. // If there is at least one order processed with WooPayments, we consider the store to have WooPayments.
if ( false === $had_wcpay && ! empty( if ( false === $had_wcpay && ! empty(
wc_get_orders( wc_get_orders(
[ array(
'payment_method' => 'woocommerce_payments', 'payment_method' => 'woocommerce_payments',
'return' => 'ids', 'return' => 'ids',
'limit' => 1, 'limit' => 1,
] )
) )
) ) { ) ) {
$had_wcpay = true; $had_wcpay = true;
@ -402,13 +437,13 @@ class WcPayWelcomePage {
// it means there was an API error previously and we should not retry just yet. // it means there was an API error previously and we should not retry just yet.
if ( is_wp_error( $cache ) ) { if ( is_wp_error( $cache ) ) {
// Initialize the in-memory cache and return it. // Initialize the in-memory cache and return it.
$this->incentive = []; $this->incentive = array();
return $this->incentive; return $this->incentive;
} }
// Gather the store context data. // Gather the store context data.
$store_context = [ $store_context = array(
// Store ISO-2 country code, e.g. `US`. // Store ISO-2 country code, e.g. `US`.
'country' => WC()->countries->get_base_country(), 'country' => WC()->countries->get_base_country(),
// Store locale, e.g. `en_US`. // Store locale, e.g. `en_US`.
@ -419,7 +454,7 @@ class WcPayWelcomePage {
// Whether the store has at least one payment gateway enabled. // Whether the store has at least one payment gateway enabled.
'has_payments' => ! empty( WC()->payment_gateways()->get_available_payment_gateways() ), 'has_payments' => ! empty( WC()->payment_gateways()->get_available_payment_gateways() ),
'has_wcpay' => $this->has_wcpay(), 'has_wcpay' => $this->has_wcpay(),
]; );
// Fingerprint the store context through a hash of certain entries. // Fingerprint the store context through a hash of certain entries.
$store_context_hash = $this->generate_context_hash( $store_context ); $store_context_hash = $this->generate_context_hash( $store_context );
@ -447,9 +482,9 @@ class WcPayWelcomePage {
$response = wp_remote_get( $response = wp_remote_get(
$url, $url,
[ array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ), 'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
] )
); );
// Return early if there is an error, waiting 6 hours before the next attempt. // Return early if there is an error, waiting 6 hours before the next attempt.
@ -470,11 +505,11 @@ class WcPayWelcomePage {
$cache_for = wp_remote_retrieve_header( $response, 'cache-for' ); $cache_for = wp_remote_retrieve_header( $response, 'cache-for' );
// Initialize the in-memory cache. // Initialize the in-memory cache.
$this->incentive = []; $this->incentive = array();
if ( 200 === wp_remote_retrieve_response_code( $response ) ) { if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
// Decode the results, falling back to an empty array. // Decode the results, falling back to an empty array.
$results = json_decode( wp_remote_retrieve_body( $response ), true ) ?? []; $results = json_decode( wp_remote_retrieve_body( $response ), true ) ?? array();
// Find all `welcome_page` incentives. // Find all `welcome_page` incentives.
$incentives = array_filter( $incentives = array_filter(
@ -486,7 +521,7 @@ class WcPayWelcomePage {
// Use the first found matching incentive or empty array if none was found. // Use the first found matching incentive or empty array if none was found.
// Store incentive in the in-memory cache. // Store incentive in the in-memory cache.
$this->incentive = empty( $incentives ) ? [] : reset( $incentives ); $this->incentive = empty( $incentives ) ? array() : reset( $incentives );
} }
// Skip transient cache if `cache-for` header equals zero. // Skip transient cache if `cache-for` header equals zero.
@ -503,11 +538,11 @@ class WcPayWelcomePage {
// or 1 day in seconds. Also attach a timestamp to the transient data so we know when we last fetched. // or 1 day in seconds. Also attach a timestamp to the transient data so we know when we last fetched.
set_transient( set_transient(
self::CACHE_TRANSIENT_NAME, self::CACHE_TRANSIENT_NAME,
[ array(
'incentive' => $this->incentive, 'incentive' => $this->incentive,
'context_hash' => $store_context_hash, 'context_hash' => $store_context_hash,
'timestamp' => time(), 'timestamp' => time(),
], ),
! empty( $cache_for ) ? (int) $cache_for : DAY_IN_SECONDS ! empty( $cache_for ) ? (int) $cache_for : DAY_IN_SECONDS
); );
@ -527,14 +562,76 @@ class WcPayWelcomePage {
// Entries like `active_for` have no place in the hash generation since they change automatically. // Entries like `active_for` have no place in the hash generation since they change automatically.
return md5( return md5(
wp_json_encode( wp_json_encode(
[ array(
'country' => $context['country'] ?? '', 'country' => $context['country'] ?? '',
'locale' => $context['locale'] ?? '', 'locale' => $context['locale'] ?? '',
'has_orders' => $context['has_orders'] ?? false, 'has_orders' => $context['has_orders'] ?? false,
'has_payments' => $context['has_payments'] ?? false, 'has_payments' => $context['has_payments'] ?? false,
'has_wcpay' => $context['has_wcpay'] ?? false, 'has_wcpay' => $context['has_wcpay'] ?? false,
] )
) )
); );
} }
/**
* Get the slug of the active payments task.
*
* It can be either 'woocommerce-payments' or 'payments'.
*
* @return string Either 'woocommerce-payments' or 'payments'. Empty string if no task is found.
*/
private function get_active_payments_task_slug(): string {
$setup_task_list = TaskLists::get_list( 'setup' );
$extended_task_list = TaskLists::get_list( 'extended' );
if ( empty( $setup_task_list ) && empty( $extended_task_list ) ) {
return '';
}
$payments_task = $setup_task_list->get_task( 'payments' );
if ( ! empty( $payments_task ) && $payments_task->can_view() ) {
return 'payments';
}
$payments_task = $extended_task_list->get_task( 'payments' );
if ( ! empty( $payments_task ) && $payments_task->can_view() ) {
return 'payments';
}
$woopayments_task = $setup_task_list->get_task( 'woocommerce-payments' );
if ( ! empty( $woopayments_task ) && $woopayments_task->can_view() ) {
return 'woocommerce-payments';
}
return '';
}
/**
* Get the WooCommerce setup task list Payments task instance.
*
* @return Task|null The Payments task instance. null if the task is not found.
*/
private function get_payments_task(): ?Task {
$task_list = TaskLists::get_list( 'setup' );
if ( empty( $task_list ) ) {
return null;
}
$payments_task = $task_list->get_task( 'payments' );
if ( empty( $payments_task ) ) {
return null;
}
return $payments_task;
}
/**
* Determine if the WooCommerce setup task list Payments task is complete.
*
* @return bool True if the Payments task is complete, false otherwise.
*/
private function is_payments_task_complete(): bool {
$payments_task = $this->get_payments_task();
return ! empty( $payments_task ) && $payments_task->is_complete();
}
} }

View File

@ -7,8 +7,6 @@
declare(strict_types=1); declare(strict_types=1);
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskList; use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskList;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\WooCommercePayments; use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\WooCommercePayments;
@ -167,3 +165,10 @@ class Fake_WC_Payments_Gateway extends WC_Payment_Gateway {
$this->partially_onboarded = $partially_onboarded; $this->partially_onboarded = $partially_onboarded;
} }
} }
if ( ! class_exists( '\WC_Payments' ) ) {
/**
* Fake WooPayments main class for testing.
*/
class WC_Payments {}
}