PluginProbe
WooCommerce / 11.1.0
WooCommerce v11.1.0
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Internal / Admin / Settings / PaymentsController.php
PaymentsController.php
377 lines 12.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare( strict_types=1 );
3
4 namespace Automattic\WooCommerce\Internal\Admin\Settings;
5
6 use Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders\WooPayments\WooPaymentsService;
7 use Automattic\WooCommerce\Internal\Logging\SafeGlobalFunctionProxy;
8 use Throwable;
9 use WC_Gateway_BACS;
10 use WC_Gateway_Cheque;
11 use WC_Gateway_COD;
12
13 defined( 'ABSPATH' ) || exit;
14 /**
15 * Payments settings controller class.
16 *
17 * Use this class for hooks and actions related to the Payments settings page.
18 *
19 * @internal
20 */
21 class PaymentsController {
22
23 const TRANSIENT_HAS_PROVIDERS_WITH_INCENTIVE_KEY = 'woocommerce_admin_settings_payments_has_providers_with_incentive';
24
25 /**
26 * The payment service.
27 *
28 * @var Payments
29 */
30 private Payments $payments;
31
32 /**
33 * Register hooks.
34 */
35 public function register() {
36 add_action( 'admin_menu', array( $this, 'add_menu' ) );
37 add_filter( 'admin_body_class', array( $this, 'add_body_classes' ), 20 );
38 add_filter( 'woocommerce_admin_shared_settings', array( $this, 'preload_settings' ) );
39 add_filter( 'woocommerce_admin_allowed_promo_notes', array( $this, 'add_allowed_promo_notes' ) );
40 add_filter( 'woocommerce_get_sections_checkout', array( $this, 'handle_sections' ), 20 );
41 add_action( 'woocommerce_admin_payments_extension_suggestion_incentive_dismissed', array( $this, 'handle_incentive_dismissed' ) );
42 }
43
44 /**
45 * Initialize the class instance.
46 *
47 * @param Payments $payments The payments service.
48 *
49 * @internal
50 */
51 final public function init( Payments $payments ): void {
52 $this->payments = $payments;
53 }
54
55 /**
56 * Adds the Payments top-level menu item.
57 */
58 public function add_menu() {
59 global $menu;
60
61 // When the WooPayments account is onboarded, WooPayments will own the Payments menu item since it is the native Woo payments solution.
62 if ( $this->is_woopayments_account_onboarded() ) {
63 return;
64 } else {
65 // Otherwise, remove the Payments menu item linking to the Connect page to avoid Payments menu item duplication.
66 remove_menu_page( 'wc-admin&path=/payments/connect' );
67 }
68
69 $menu_title = esc_html__( 'Payments', 'woocommerce' );
70 $menu_icon = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI4NTIiIGhlaWdodD0iNjg0Ij48cGF0aCBmaWxsPSIjYTJhYWIyIiBkPSJNODIgODZ2NTEyaDY4NFY4NlptMCA1OThjLTQ4IDAtODQtMzgtODQtODZWODZDLTIgMzggMzQgMCA4MiAwaDY4NGM0OCAwIDg0IDM4IDg0IDg2djUxMmMwIDQ4LTM2IDg2LTg0IDg2em0zODQtNTU2djQ0aDg2djg0SDM4MnY0NGgxMjhjMjQgMCA0MiAxOCA0MiA0MnYxMjhjMCAyNC0xOCA0Mi00MiA0MmgtNDR2NDRoLTg0di00NGgtODZ2LTg0aDE3MHYtNDRIMzM4Yy0yNCAwLTQyLTE4LTQyLTQyVjIxNGMwLTI0IDE4LTQyIDQyLTQyaDQ0di00NHoiLz48L3N2Zz4=';
71 // Link to the Payments settings page.
72 $menu_path = 'admin.php?page=wc-settings&tab=checkout&from=' . Payments::FROM_PAYMENTS_MENU_ITEM;
73
74 add_menu_page(
75 $menu_title,
76 $menu_title,
77 'manage_woocommerce', // Capability required to see the menu item.
78 $menu_path,
79 null,
80 $menu_icon,
81 56, // Position after WooCommerce Product menu item.
82 );
83
84 // If there are providers with an active incentive, add a notice badge to the Payments menu item.
85 if ( $this->store_has_providers_with_incentive() ) {
86 $badge = ' <span class="wcpay-menu-badge awaiting-mod count-1"><span class="plugin-count">1</span></span>';
87 foreach ( $menu as $index => $menu_item ) {
88 // Only add the badge markup if not already present, and the menu item is the Payments menu item.
89 if ( 0 === strpos( $menu_item[0], $menu_title )
90 && $menu_path === $menu_item[2]
91 && false === strpos( $menu_item[0], $badge ) ) {
92
93 $menu[ $index ][0] .= $badge; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
94
95 // One menu item with a badge is more than enough.
96 break;
97 }
98 }
99 }
100 }
101
102 /**
103 * Adds body classes when in the Payments Settings admin area.
104 *
105 * @param string $classes The existing body classes for the admin area.
106 *
107 * @return string The modified body classes for the admin area.
108 */
109 public function add_body_classes( $classes = '' ) {
110 global $current_tab;
111
112 // Bail if the type is invalid.
113 if ( ! is_string( $classes ) ) {
114 return $classes;
115 }
116
117 if ( 'checkout' === $current_tab && ! str_contains( 'woocommerce-settings-payments-tab', $classes ) ) {
118 $classes = "$classes woocommerce-settings-payments-tab";
119 }
120
121 return $classes;
122 }
123
124 /**
125 * Preload settings to make them available to the Payments settings page frontend logic.
126 *
127 * Added keys will be available in the window.wcSettings.admin object.
128 *
129 * @param array $settings The settings array.
130 *
131 * @return array Settings array with additional settings added.
132 */
133 public function preload_settings( $settings = array() ) {
134 // We only preload settings in the WP admin.
135 if ( ! is_admin() ) {
136 return $settings;
137 }
138
139 // Reset the received value if the type is invalid.
140 if ( ! is_array( $settings ) ) {
141 $settings = array();
142 }
143
144 // Add the business location country to the settings.
145 if ( ! isset( $settings[ Payments::PAYMENTS_NOX_PROFILE_KEY ] ) ) {
146 $settings[ Payments::PAYMENTS_NOX_PROFILE_KEY ] = array();
147 }
148 $settings[ Payments::PAYMENTS_NOX_PROFILE_KEY ]['business_country_code'] = $this->payments->get_country();
149
150 return $settings;
151 }
152
153 /**
154 * Adds promo note IDs to the list of allowed ones.
155 *
156 * @param array $promo_notes Allowed promo note IDs.
157 *
158 * @return array The updated list of allowed promo note IDs.
159 */
160 public function add_allowed_promo_notes( $promo_notes = array() ): array {
161 // Reset the value if the type is invalid.
162 if ( ! is_array( $promo_notes ) ) {
163 $promo_notes = array();
164 }
165
166 try {
167 $providers = $this->payments->get_payment_providers( $this->payments->get_country(), false );
168 } catch ( Throwable $e ) {
169 // Catch everything since we don't want to break all the WP admin pages.
170 // Log so we can investigate.
171 SafeGlobalFunctionProxy::wc_get_logger()->error(
172 'Failed to get payment providers: ' . $e->getMessage(),
173 array(
174 'source' => 'settings-payments',
175 )
176 );
177
178 return $promo_notes;
179 }
180
181 // Add all incentive promo IDs to the allowed promo notes list.
182 foreach ( $providers as $provider ) {
183 if ( ! empty( $provider['_incentive']['promo_id'] ) ) {
184 $promo_notes[] = $provider['_incentive']['promo_id'];
185 }
186 }
187
188 return $promo_notes;
189 }
190
191 /**
192 * Alter the Payments tab sections under certain conditions.
193 *
194 * @param array $sections The payments/checkout tab sections.
195 *
196 * @return array The filtered sections.
197 */
198 public function handle_sections( $sections = array() ): array {
199 global $current_section;
200
201 // Reset the value if the type is invalid.
202 if ( ! is_array( $sections ) ) {
203 $sections = array();
204 }
205
206 // Bail if the current section global is empty or of the wrong type.
207 if ( empty( $current_section ) || ! is_string( $current_section ) ) {
208 return $sections;
209 }
210
211 // For WooPayments and offline payment methods settings pages, we don't want any section navigation.
212 if ( in_array( $current_section, array( WooPaymentsService::GATEWAY_ID, WC_Gateway_BACS::ID, WC_Gateway_Cheque::ID, WC_Gateway_COD::ID ), true ) ) {
213 return array();
214 }
215
216 return $sections;
217 }
218
219 /**
220 * Handle the payments extension suggestion incentive dismissed event.
221 *
222 * @return void
223 */
224 public function handle_incentive_dismissed(): void {
225 // Clear the transient to force a new check for providers with an incentive.
226 delete_transient( self::TRANSIENT_HAS_PROVIDERS_WITH_INCENTIVE_KEY );
227 }
228
229 /**
230 * Check if the store has any enabled gateways (including offline payment methods).
231 *
232 * @return bool True if the store has any enabled gateways, false otherwise.
233 */
234 private function store_has_enabled_gateways(): bool {
235 $gateways = WC()->payment_gateways->get_available_payment_gateways();
236 $enabled_gateways = array_filter(
237 $gateways,
238 function ( $gateway ) {
239 return 'yes' === $gateway->enabled;
240 }
241 );
242
243 return ! empty( $enabled_gateways );
244 }
245
246 /**
247 * Check if the store has any payment providers that have an active incentive.
248 *
249 * @return bool True if the store has providers with an active incentive.
250 */
251 private function store_has_providers_with_incentive(): bool {
252 // First, try to use the transient value.
253 $transient = get_transient( self::TRANSIENT_HAS_PROVIDERS_WITH_INCENTIVE_KEY );
254 if ( false !== $transient ) {
255 return filter_var( $transient, FILTER_VALIDATE_BOOLEAN );
256 }
257
258 try {
259 $providers = $this->payments->get_payment_providers( $this->payments->get_country(), false );
260 } catch ( Throwable $e ) {
261 // Catch everything since we don't want to break all the WP admin pages.
262 // Log so we can investigate.
263 SafeGlobalFunctionProxy::wc_get_logger()->error(
264 'Failed to get payment providers: ' . $e->getMessage(),
265 array(
266 'source' => 'settings-payments',
267 )
268 );
269
270 // In case of an error, default to false.
271 // Set the transient to avoid repeated errors.
272 set_transient( self::TRANSIENT_HAS_PROVIDERS_WITH_INCENTIVE_KEY, 'no', HOUR_IN_SECONDS );
273 return false;
274 }
275
276 $has_providers_with_incentive = false;
277 // Go through the providers and check if any of them have a "prominently" visible incentive (i.e., modal or banner).
278 foreach ( $providers as $provider ) {
279 if ( empty( $provider['_incentive'] ) ) {
280 continue;
281 }
282
283 $dismissals = $provider['_incentive']['_dismissals'] ?? array();
284
285 // If there are no dismissals at all, the incentive is prominently visible.
286 if ( empty( $dismissals ) ) {
287 $has_providers_with_incentive = true;
288 break;
289 }
290
291 // First, we check to see if the incentive was dismissed in the banner context.
292 // The banner context has the lowest priority, so if it was dismissed, we don't need to check the modal context.
293 // If the banner is dismissed, there is no prominent incentive.
294 $is_dismissed_banner = ! empty(
295 array_filter(
296 $dismissals,
297 function ( $dismissal ) {
298 return isset( $dismissal['context'] ) && 'wc_settings_payments__banner' === $dismissal['context'];
299 }
300 )
301 );
302 if ( $is_dismissed_banner ) {
303 continue;
304 }
305
306 // In case an incentive uses the modal surface also (like the WooPayments Switch incentive),
307 // we rely on the fact that the modal falls back to the banner, once dismissed, after 30 days.
308 // @see here's its frontend "brother" in client/admin/client/settings-payments/settings-payments-main.tsx.
309 $is_dismissed_modal = ! empty(
310 array_filter(
311 $dismissals,
312 function ( $dismissal ) {
313 return isset( $dismissal['context'] ) && 'wc_settings_payments__modal' === $dismissal['context'];
314 }
315 )
316 );
317 // If there are no modal dismissals, the incentive is still visible.
318 if ( ! $is_dismissed_modal ) {
319 $has_providers_with_incentive = true;
320 break;
321 }
322
323 $is_dismissed_modal_more_than_30_days_ago = ! empty(
324 array_filter(
325 $dismissals,
326 function ( $dismissal ) {
327 return isset( $dismissal['context'], $dismissal['timestamp'] ) &&
328 'wc_settings_payments__modal' === $dismissal['context'] &&
329 $dismissal['timestamp'] < strtotime( '-30 days' );
330 }
331 )
332 );
333 // If the modal was dismissed less than 30 days ago, there is no prominent incentive (aka the banner is not shown).
334 if ( ! $is_dismissed_modal_more_than_30_days_ago ) {
335 continue;
336 }
337
338 // The modal was dismissed more than 30 days ago, so the banner is visible.
339 $has_providers_with_incentive = true;
340 break;
341 }
342
343 // Save the value in a transient to avoid unnecessary processing throughout the WP admin.
344 // Incentives don't change frequently, so it is safe to cache the value for 1 hour.
345 set_transient( self::TRANSIENT_HAS_PROVIDERS_WITH_INCENTIVE_KEY, $has_providers_with_incentive ? 'yes' : 'no', HOUR_IN_SECONDS );
346
347 return $has_providers_with_incentive;
348 }
349
350 /**
351 * Check if the WooPayments account is onboarded.
352 *
353 * @return boolean
354 */
355 private function is_woopayments_account_onboarded(): bool {
356 // Sanity check: the WooPayments extension must be active.
357 if ( ! class_exists( '\WC_Payments' ) ) {
358 return false;
359 }
360
361 $account_data = get_option( 'wcpay_account_data', array() );
362
363 // The account ID must be present.
364 if ( empty( $account_data['data']['account_id'] ) ) {
365 return false;
366 }
367
368 // We consider the store to have an onboarded WooPayments account if account data in the WooPayments account cache
369 // contains a details_submitted = true entry. This implies that WooPayments is also connected.
370 if ( empty( $account_data['data']['details_submitted'] ) ) {
371 return false;
372 }
373
374 return filter_var( $account_data['data']['details_submitted'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ) ?? false;
375 }
376 }
377