Merged in feature/MAW-855-import-code-into-aws (pull request #2)

code import from pantheon

* code import from pantheon
This commit is contained in:
Tony Volpe
2023-12-04 23:08:14 +00:00
parent 8c9b1312bc
commit 8f4b5efda6
4766 changed files with 185592 additions and 239967 deletions

View File

@@ -12,7 +12,8 @@ use Automattic\WooCommerce\Admin\PageController;
* @package Automattic\WooCommerce\Admin\Features
*/
class WcPayWelcomePage {
const TRANSIENT_NAME = 'wcpay_welcome_page_incentive';
const CACHE_TRANSIENT_NAME = 'wcpay_welcome_page_incentive';
const HAD_WCPAY_OPTION_NAME = 'wcpay_was_in_use';
/**
* Plugin instance.
@@ -44,6 +45,7 @@ class WcPayWelcomePage {
add_action( 'admin_menu', [ $this, 'register_payments_welcome_page' ] );
add_filter( 'woocommerce_admin_shared_settings', [ $this, 'shared_settings' ] );
add_filter( 'woocommerce_admin_allowed_promo_notes', [ $this, 'allowed_promo_notes' ] );
add_filter( 'woocommerce_admin_woopayments_onboarding_task_badge', [ $this, 'onboarding_task_badge' ] );
}
/**
@@ -52,6 +54,11 @@ class WcPayWelcomePage {
* @return boolean
*/
public function must_be_visible(): bool {
// The WooPayments plugin must not be active.
if ( $this->is_wcpay_active() ) {
return false;
}
// Suggestions not disabled via a setting.
if ( get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) === 'no' ) {
return false;
@@ -68,12 +75,7 @@ class WcPayWelcomePage {
return false;
}
// The WooPayments plugin must not be active.
if ( $this->is_wcpay_active() ) {
return false;
}
// Incentive is available.
// An incentive must be available.
if ( empty( $this->get_incentive() ) ) {
return false;
}
@@ -133,11 +135,17 @@ class WcPayWelcomePage {
}
// Add badge.
$badge = ' <span class="wcpay-menu-badge awaiting-mod count-1"><span class="plugin-count">1</span></span>';
foreach ( $menu as $index => $menu_item ) {
if ( 'wc-admin&path=/wc-pay-welcome-page' === $menu_item[2]
|| 'admin.php?page=wc-admin&path=/wc-pay-welcome-page' === $menu_item[2] ) {
//phpcs:ignore
$menu[ $index ][0] .= ' <span class="wcpay-menu-badge awaiting-mod count-1"><span class="plugin-count">1</span></span>';
// Only add the badge markup if not already present and the menu item is the WooPayments menu item.
if ( false === strpos( $menu_item[0], $badge )
&& ( 'wc-admin&path=/wc-pay-welcome-page' === $menu_item[2]
|| 'admin.php?page=wc-admin&path=/wc-pay-welcome-page' === $menu_item[2] )
) {
$menu[ $index ][0] .= $badge; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
// One menu item with a badge is more than enough.
break;
}
}
}
@@ -154,8 +162,8 @@ class WcPayWelcomePage {
return $settings;
}
// Return early if there is no eligible incentive.
if ( empty( $this->get_incentive() ) ) {
// Return early if the incentive must not be visible.
if ( ! $this->must_be_visible() ) {
return $settings;
}
@@ -171,8 +179,8 @@ class WcPayWelcomePage {
* @return array
*/
public function allowed_promo_notes( $promo_notes = [] ): array {
// Return early if there is no eligible incentive.
if ( empty( $this->get_incentive() ) ) {
// Return early if the incentive must not be visible.
if ( ! $this->must_be_visible() ) {
return $promo_notes;
}
@@ -183,20 +191,50 @@ class WcPayWelcomePage {
}
/**
* Check if the WooPayments payment gateway is active and set up,
* Adds the WooPayments incentive badge to the onboarding task.
*
* @param string $badge Current badge.
*
* @return string
*/
public function onboarding_task_badge( string $badge ): string {
// Return early if the incentive must not be visible.
if ( ! $this->must_be_visible() ) {
return $badge;
}
return $this->get_incentive()['task_badge'] ?? $badge;
}
/**
* Check if the WooPayments payment gateway is active and set up or was at some point,
* or there are orders processed with it, at some moment.
*
* @return boolean
*/
private function has_wcpay(): bool {
// First, get the stored value, if it exists.
// This way we avoid costly DB queries and API calls.
// Basically, we only want to know if WooPayments was in use in the past.
// Since the past can't be changed, neither can this value.
$had_wcpay = get_option( self::HAD_WCPAY_OPTION_NAME );
if ( false !== $had_wcpay ) {
return $had_wcpay === 'yes';
}
// We need to determine the value.
// Start with the assumption that the store didn't have WooPayments in use.
$had_wcpay = false;
// We consider the store to have WooPayments if there is meaningful account data in the WooPayments account cache.
// This implies that WooPayments is or was active at some point and that it was connected.
// This implies that WooPayments was active at some point and that it was connected.
// If WooPayments is active right now, we will not get to this point since the plugin is active check is done first.
if ( $this->has_wcpay_account_data() ) {
return true;
$had_wcpay = true;
}
// If there is at least one order processed with WooPayments, we consider the store to have WooPayments.
if ( ! empty(
if ( false === $had_wcpay && ! empty(
wc_get_orders(
[
'payment_method' => 'woocommerce_payments',
@@ -205,10 +243,13 @@ class WcPayWelcomePage {
]
)
) ) {
return true;
$had_wcpay = true;
}
return false;
// Store the value for future use.
update_option( self::HAD_WCPAY_OPTION_NAME, $had_wcpay ? 'yes' : 'no' );
return $had_wcpay;
}
/**
@@ -240,14 +281,21 @@ class WcPayWelcomePage {
* @return boolean
*/
private function is_incentive_dismissed(): bool {
$dismissed_incentives = get_option( 'wcpay_welcome_page_incentives_dismissed', [] );
// If there are no dismissed incentives, return early.
if ( empty( $dismissed_incentives ) ) {
return false;
}
// Return early if there is no eligible incentive.
if ( empty( $this->get_incentive() ) ) {
$incentive = $this->get_incentive();
if ( empty( $incentive ) ) {
return true;
}
$dismissed_incentives = get_option( 'wcpay_welcome_page_incentives_dismissed', [] );
if ( in_array( $this->get_incentive()['id'], $dismissed_incentives, true ) ) {
// Search the incentive ID in the dismissed incentives list.
if ( in_array( $incentive['id'], $dismissed_incentives, true ) ) {
return true;
}
@@ -265,6 +313,19 @@ class WcPayWelcomePage {
return $this->incentive;
}
// Get the cached data.
$cache = get_transient( self::CACHE_TRANSIENT_NAME );
// If the cached data is not expired and it's a WP_Error,
// it means there was an API error previously and we should not retry just yet.
if ( is_wp_error( $cache ) ) {
// Initialize the in-memory cache and return it.
$this->incentive = [];
return $this->incentive;
}
// Gather the store context data.
$store_context = [
// Store ISO-2 country code, e.g. `US`.
'country' => WC()->countries->get_base_country(),
@@ -293,25 +354,16 @@ class WcPayWelcomePage {
// Use the transient cached incentive if it exists, it is not expired,
// and the store context hasn't changed since we last requested from the WooPayments API (based on context hash).
$transient_cache = get_transient( self::TRANSIENT_NAME );
if ( false !== $transient_cache ) {
if ( is_null( $transient_cache ) ) {
// This means there was an error and we shouldn't retry just yet.
// Initialize the in-memory cache.
$this->incentive = [];
} elseif ( ! empty( $transient_cache['context_hash'] ) && is_string( $transient_cache['context_hash'] )
&& hash_equals( $store_context_hash, $transient_cache['context_hash'] ) ) {
if ( false !== $cache
&& ! empty( $cache['context_hash'] ) && is_string( $cache['context_hash'] )
&& hash_equals( $store_context_hash, $cache['context_hash'] ) ) {
// We have a store context hash and it matches with the current context one.
// We can use the cached incentive data.
// Store the incentive in the in-memory cache.
$this->incentive = $transient_cache['incentive'] ?? [];
}
// We have a store context hash and it matches with the current context one.
// We can use the cached incentive data.
// Store the incentive in the in-memory cache and return it.
$this->incentive = $cache['incentive'] ?? [];
// If the in-memory cache has been set, return it.
if ( isset( $this->incentive ) ) {
return $this->incentive;
}
return $this->incentive;
}
// By this point, we have an expired transient or the store context has changed.
@@ -323,16 +375,22 @@ class WcPayWelcomePage {
$response = wp_remote_get(
$url,
array(
[
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
)
]
);
// Return early if there is an error, waiting 6 hours before the next attempt.
if ( is_wp_error( $response ) ) {
// Store a null value in the transient so we know this is due to an API error.
set_transient( self::TRANSIENT_NAME, null, HOUR_IN_SECONDS * 6 );
// Initialize the in-memory cache.
// Store a trimmed down, lightweight error.
$error = new \WP_Error(
$response->get_error_code(),
$response->get_error_message(),
wp_remote_retrieve_response_code( $response )
);
// Store the error in the transient so we know this is due to an API error.
set_transient( self::CACHE_TRANSIENT_NAME, $error, HOUR_IN_SECONDS * 6 );
// Initialize the in-memory cache and return it.
$this->incentive = [];
return $this->incentive;
@@ -362,19 +420,21 @@ class WcPayWelcomePage {
// Skip transient cache if `cache-for` header equals zero.
if ( '0' === $cache_for ) {
// If we have a transient cache that is not expired, delete it so there are no leftovers.
if ( false !== $transient_cache ) {
delete_transient( self::TRANSIENT_NAME );
if ( false !== $cache ) {
delete_transient( self::CACHE_TRANSIENT_NAME );
}
return $this->incentive;
}
// Store incentive in transient cache (together with the context hash) for the given number of seconds or 24h.
// Store incentive in transient cache (together with the context hash) for the given number of seconds
// or 1 day in seconds. Also attach a timestamp to the transient data so we know when we last fetched.
set_transient(
self::TRANSIENT_NAME,
self::CACHE_TRANSIENT_NAME,
[
'incentive' => $this->incentive,
'context_hash' => $store_context_hash,
'timestamp' => time(),
],
! empty( $cache_for ) ? (int) $cache_for : DAY_IN_SECONDS
);