WcPayWelcomePage.php
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\Admin; |
| 5 | |
| 6 | use Automattic\WooCommerce\Admin\Features\Features; |
| 7 | use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task; |
| 8 | use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists; |
| 9 | use Automattic\WooCommerce\Admin\PageController; |
| 10 | use Automattic\WooCommerce\Internal\Admin\Suggestions\PaymentsExtensionSuggestionIncentives; |
| 11 | use Automattic\WooCommerce\Internal\Admin\Suggestions\PaymentsExtensionSuggestions; |
| 12 | use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 13 | |
| 14 | /** |
| 15 | * Class WCPayWelcomePage |
| 16 | * |
| 17 | * @deprecated 9.9.0 The WooPayments welcome page is deprecated and will be removed in a future version of WooCommerce. |
| 18 | */ |
| 19 | class WcPayWelcomePage { |
| 20 | /** |
| 21 | * The incentive type for the WooPayments welcome page. |
| 22 | */ |
| 23 | const INCENTIVE_TYPE = 'welcome_page'; |
| 24 | |
| 25 | /** |
| 26 | * The suggestion incentives instance. |
| 27 | * |
| 28 | * @var PaymentsExtensionSuggestionIncentives |
| 29 | */ |
| 30 | private PaymentsExtensionSuggestionIncentives $suggestion_incentives; |
| 31 | |
| 32 | /** |
| 33 | * Class instance. |
| 34 | * |
| 35 | * @var ?WcPayWelcomePage |
| 36 | */ |
| 37 | protected static ?WcPayWelcomePage $instance = null; |
| 38 | |
| 39 | /** |
| 40 | * Get class instance. |
| 41 | * |
| 42 | * @return ?WcPayWelcomePage |
| 43 | */ |
| 44 | public static function instance(): ?WcPayWelcomePage { |
| 45 | self::$instance = is_null( self::$instance ) ? new self() : self::$instance; |
| 46 | |
| 47 | return self::$instance; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * WCPayWelcomePage constructor. |
| 52 | */ |
| 53 | public function __construct() { |
| 54 | $this->suggestion_incentives = wc_get_container()->get( PaymentsExtensionSuggestionIncentives::class ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Check if we have an incentive available to show. |
| 59 | * |
| 60 | * @param bool $skip_wcpay_active Whether to skip the check for the WooPayments plugin being active. |
| 61 | * |
| 62 | * @return bool Whether we have an incentive available to show. |
| 63 | */ |
| 64 | public function has_incentive( bool $skip_wcpay_active = false ): bool { |
| 65 | // The WooPayments plugin must not be active. |
| 66 | if ( ! $skip_wcpay_active && $this->is_wcpay_active() ) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | // Suggestions not disabled via a setting. |
| 71 | if ( get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) === 'no' ) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Filter allow marketplace suggestions. |
| 77 | * |
| 78 | * User can disable all suggestions via filter. |
| 79 | * |
| 80 | * @since 3.6.0 |
| 81 | */ |
| 82 | if ( ! apply_filters( 'woocommerce_allow_marketplace_suggestions', true ) ) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | $incentive = $this->get_incentive(); |
| 87 | if ( empty( $incentive ) ) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | if ( $this->is_incentive_dismissed( $incentive ) ) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | return $this->suggestion_incentives->is_incentive_visible( |
| 96 | $incentive['id'], |
| 97 | PaymentsExtensionSuggestions::WOOPAYMENTS, |
| 98 | WC()->countries->get_base_country(), |
| 99 | $skip_wcpay_active |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get the WooPayments incentive details, if available. |
| 105 | * |
| 106 | * @return array|null The incentive details. Null if there is no incentive available. |
| 107 | */ |
| 108 | private function get_incentive(): ?array { |
| 109 | return $this->suggestion_incentives->get_incentive( |
| 110 | PaymentsExtensionSuggestions::WOOPAYMENTS, |
| 111 | WC()->countries->get_base_country(), |
| 112 | self::INCENTIVE_TYPE, |
| 113 | true |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Check if the WooPayments plugin is active. |
| 119 | * |
| 120 | * @return boolean |
| 121 | */ |
| 122 | private function is_wcpay_active(): bool { |
| 123 | return class_exists( '\WC_Payments' ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Check if the current incentive has been manually dismissed. |
| 128 | * |
| 129 | * @param array $incentive The incentive details. |
| 130 | * |
| 131 | * @return boolean |
| 132 | */ |
| 133 | private function is_incentive_dismissed( array $incentive ): bool { |
| 134 | /* |
| 135 | * First, check the legacy option. |
| 136 | */ |
| 137 | $dismissed_incentives = get_option( 'wcpay_welcome_page_incentives_dismissed', array() ); |
| 138 | if ( ! empty( $dismissed_incentives ) ) { |
| 139 | // Search the incentive ID in the dismissed incentives list. |
| 140 | if ( in_array( $incentive['id'], $dismissed_incentives, true ) ) { |
| 141 | return true; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Second, use the new logic. |
| 147 | */ |
| 148 | return $this->suggestion_incentives->is_incentive_dismissed( |
| 149 | $incentive['id'], |
| 150 | PaymentsExtensionSuggestions::WOOPAYMENTS, |
| 151 | 'wc_payments_task' |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get the slug of the active payments task. |
| 157 | * |
| 158 | * It can be either 'woocommerce-payments' or 'payments'. |
| 159 | * |
| 160 | * @return string Either 'woocommerce-payments' or 'payments'. Empty string if no task is found. |
| 161 | */ |
| 162 | private function get_active_payments_task_slug(): string { |
| 163 | $setup_task_list = TaskLists::get_list( 'setup' ); |
| 164 | $extended_task_list = TaskLists::get_list( 'extended' ); |
| 165 | |
| 166 | // The task pages are not available if the task lists don't exist or are not visible. |
| 167 | // Bail early if we have no task to work with. |
| 168 | if ( |
| 169 | ( empty( $setup_task_list ) || ! $setup_task_list->is_visible() ) && |
| 170 | ( empty( $extended_task_list ) || ! $extended_task_list->is_visible() ) |
| 171 | ) { |
| 172 | return ''; |
| 173 | } |
| 174 | |
| 175 | // The Payments task in the setup task list. |
| 176 | if ( ! empty( $setup_task_list ) && $setup_task_list->is_visible() ) { |
| 177 | $payments_task = $setup_task_list->get_task( 'payments' ); |
| 178 | if ( ! empty( $payments_task ) && $payments_task->can_view() ) { |
| 179 | return 'payments'; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // The Additional Payments task in the extended task list. |
| 184 | if ( ! empty( $extended_task_list ) && $extended_task_list->is_visible() ) { |
| 185 | $payments_task = $extended_task_list->get_task( 'payments' ); |
| 186 | if ( ! empty( $payments_task ) && $payments_task->can_view() ) { |
| 187 | return 'payments'; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // The WooPayments task in the setup task list. |
| 192 | if ( ! empty( $setup_task_list ) && $setup_task_list->is_visible() ) { |
| 193 | $payments_task = $setup_task_list->get_task( 'woocommerce-payments' ); |
| 194 | if ( ! empty( $payments_task ) && $payments_task->can_view() ) { |
| 195 | return 'woocommerce-payments'; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return ''; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Get the WooCommerce setup task list Payments task instance. |
| 204 | * |
| 205 | * @return Task|null The Payments task instance. null if the task is not found. |
| 206 | */ |
| 207 | private function get_payments_task(): ?Task { |
| 208 | $task_list = TaskLists::get_list( 'setup' ); |
| 209 | if ( empty( $task_list ) ) { |
| 210 | return null; |
| 211 | } |
| 212 | |
| 213 | $payments_task = $task_list->get_task( 'payments' ); |
| 214 | if ( empty( $payments_task ) ) { |
| 215 | return null; |
| 216 | } |
| 217 | |
| 218 | return $payments_task; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Determine if the WooCommerce setup task list Payments task is complete. |
| 223 | * |
| 224 | * @return bool True if the Payments task is complete, false otherwise. |
| 225 | */ |
| 226 | private function is_payments_task_complete(): bool { |
| 227 | $payments_task = $this->get_payments_task(); |
| 228 | |
| 229 | return ! empty( $payments_task ) && $payments_task->is_complete(); |
| 230 | } |
| 231 | } |
| 232 |