Completed
Pull Request — master (#1365)
by
unknown
01:49
created

WC_Stripe_Inbox_Notes::create_failure_note()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
use Automattic\WooCommerce\Admin\Notes\WC_Admin_Note;
7
use Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes;
8
9
/**
10
 * Class that adds Inbox notifications.
11
 *
12
 * @since 4.5.4
13
 */
14
class WC_Stripe_Inbox_Notes {
15
	const SUCCESS_NOTE_NAME = 'stripe-apple-pay-marketing-guide-holiday-2020';
16
	const FAILURE_NOTE_NAME = 'stripe-apple-pay-domain-verification-needed';
17
18
	public static function notify_on_apple_pay_domain_verification() {
19
		if ( ! class_exists( 'Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes' ) ) {
20
			return;
21
		}
22
23
		if ( ! class_exists( 'WC_Data_Store' ) ) {
24
			return;
25
		}
26
27
		$stripe_settings       = get_option( 'woocommerce_stripe_settings', array() );
28
		$domain_flag_key       = 'apple_pay_domain_set';
29
		$verification_complete = isset( $stripe_settings[ $domain_flag_key ] ) && 'yes' === $stripe_settings[ $domain_flag_key ];
30
31
		$data_store = WC_Data_Store::load( 'admin-note' );
32
33
		$success_note_ids = $data_store->get_notes_with_name( self::SUCCESS_NOTE_NAME );
34
		$failure_note_ids = $data_store->get_notes_with_name( self::FAILURE_NOTE_NAME );
35
36
		if ( $verification_complete ) {
37
			// Display success note to US merchants only.
38
			$base_location = wc_get_base_location();
39
			if ( is_array( $base_location ) && 'US' === $base_location['country'] ) {
40
				if ( empty( $success_note_ids ) ) {
41
					self::create_success_note();
42
				}
43
			}
44
45
			if ( ! empty( $failure_note_ids ) ) {
46
				$note_id = array_pop( $failure_note_ids );
47
				$note    = WC_Admin_Notes::get_note( $note_id );
48
				if ( false === $note ) {
49
					return;
50
				}
51
52
				// If the domain verification completed after failure note was created, make sure it's marked as actioned.
53
				if ( WC_Admin_Note::E_WC_ADMIN_NOTE_ACTIONED !== $note->get_status() ) {
54
					$note->set_status( WC_Admin_Note::E_WC_ADMIN_NOTE_ACTIONED );
55
					$note->save();
56
				}
57
			}
58
		} else {
59
			if ( empty( $failure_note_ids ) ) {
60
				self::create_failure_note();
61
			}
62
		}
63
	}
64
65 View Code Duplication
	public static function create_success_note() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
		$note = new WC_Admin_Note();
67
		$note->set_title( __( 'Boost sales this holiday season with Apple Pay!', 'woocommerce-gateway-stripe' ) );
68
		$note->set_content( __( 'Now that you accept Apple Pay® with Stripe, you can increase conversion rates by letting your customers know that Apple Pay is available. Here’s a marketing guide to help you get started.', 'woocommerce-gateway-stripe' ) );
69
		$note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_MARKETING );
70
		$note->set_name( self::SUCCESS_NOTE_NAME );
71
		$note->set_source( 'woocommerce-gateway-stripe' );
72
		$note->add_action(
73
			'marketing-guide',
74
			__( 'See marketing guide', 'woocommerce-gateway-stripe' ),
75
			'https://developer.apple.com/apple-pay/marketing/'
76
		);
77
		$note->save();
78
	}
79
80 View Code Duplication
	public static function create_failure_note() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
		$note = new WC_Admin_Note();
82
		$note->set_title( __( 'Apple Pay domain verification needed', 'woocommerce-gateway-stripe' ) );
83
		$note->set_content( __( 'The WooCommerce Stripe Gateway extension attempted to perform domain verification on behalf of your store, but was unable to do so. This must be resolved before Apple Pay can be offered to your customers.', 'woocommerce-gateway-stripe' ) );
84
		$note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
85
		$note->set_name( self::FAILURE_NOTE_NAME );
86
		$note->set_source( 'woocommerce-gateway-stripe' );
87
		$note->add_action(
88
			'learn-more',
89
			__( 'Learn more', 'woocommerce-gateway-stripe' ),
90
			'https://docs.woocommerce.com/document/stripe/#apple-pay'
91
		);
92
		$note->save();
93
	}
94
}
95