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

WC_Stripe_Inbox_Notes::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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 function __construct() {
19
		add_action( 'wc_stripe_apple_pay_post_setup_success', array( self::class, 'create_marketing_note' ) );
20
	}
21
22
	/**
23
	 * Manage notes to show after Apple Pay domain verification.
24
	 */
25
	public static function notify_on_apple_pay_domain_verification( $verification_complete ) {
26
		if ( ! class_exists( 'Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes' ) ) {
27
			return;
28
		}
29
30
		if ( ! class_exists( 'WC_Data_Store' ) ) {
31
			return;
32
		}
33
34
		if ( $verification_complete ) {
35
			if ( self::should_show_marketing_note() && ! wp_next_scheduled( 'wc_stripe_apple_pay_post_setup_success' ) ) {
36
				wp_schedule_single_event( time() + DAY_IN_SECONDS, 'wc_stripe_apple_pay_post_setup_success' );
37
			}
38
39
			// If the domain verification completed after failure note was created, make sure it's marked as actioned.
40
			$data_store       = WC_Data_Store::load( 'admin-note' );
41
			$failure_note_ids = $data_store->get_notes_with_name( self::FAILURE_NOTE_NAME );
42
			if ( ! empty( $failure_note_ids ) ) {
43
				$note_id = array_pop( $failure_note_ids );
44
				$note    = WC_Admin_Notes::get_note( $note_id );
45
				if ( false !== $note && WC_Admin_Note::E_WC_ADMIN_NOTE_ACTIONED !== $note->get_status() ) {
46
					$note->set_status( WC_Admin_Note::E_WC_ADMIN_NOTE_ACTIONED );
47
					$note->save();
48
				}
49
			}
50
		} else {
51
			if ( empty( $failure_note_ids ) ) {
0 ignored issues
show
Bug introduced by
The variable $failure_note_ids seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
52
				self::create_failure_note();
53
			}
54
		}
55
	}
56
57
	/**
58
	 * Whether conditions are right for the marketing note.
59
	 */
60
	public static function should_show_marketing_note() {
61
		// Display to US merchants only.
62
		$base_location = wc_get_base_location();
63
		if ( ! $base_location || 'US' !== $base_location['country'] ) {
64
			return false;
65
		}
66
67
		// Make sure Apple Pay is enabled and setup is successful.
68
		$stripe_settings       = get_option( 'woocommerce_stripe_settings', array() );
69
		$stripe_enabled        = isset( $stripe_settings['enabled'] ) && 'yes' === $stripe_settings['enabled'];
70
		$button_enabled        = isset( $stripe_settings['payment_request'] ) && 'yes' === $stripe_settings['payment_request'];
71
		$verification_complete = isset( $stripe_settings['apple_pay_domain_set'] ) && 'yes' === $stripe_settings['apple_pay_domain_set'];
72
		if ( ! $stripe_enabled || ! $button_enabled || ! $verification_complete ) {
73
			return false;
74
		}
75
76
		// Make sure note doesn't already exist.
77
		$data_store       = WC_Data_Store::load( 'admin-note' );
78
		$success_note_ids = $data_store->get_notes_with_name( self::SUCCESS_NOTE_NAME );
79
		if ( ! empty( $success_note_ids ) ) {
80
			return false;
81
		}
82
83
		return true;
84
	}
85
86
	/**
87
	 * If conditions are right, show note promoting Apple Pay marketing guide.
88
	 */
89 View Code Duplication
	public static function create_marketing_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...
90
		// Make sure conditions for this note still hold.
91
		if ( ! self::should_show_marketing_note() ) {
92
			return;
93
		}
94
95
		$note = new WC_Admin_Note();
96
		$note->set_title( __( 'Boost sales this holiday season with Apple Pay!', 'woocommerce-gateway-stripe' ) );
97
		$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' ) );
98
		$note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_MARKETING );
99
		$note->set_name( self::SUCCESS_NOTE_NAME );
100
		$note->set_source( 'woocommerce-gateway-stripe' );
101
		$note->add_action(
102
			'marketing-guide',
103
			__( 'See marketing guide', 'woocommerce-gateway-stripe' ),
104
			'https://developer.apple.com/apple-pay/marketing/'
105
		);
106
		$note->save();
107
	}
108
109
	/**
110
	 * Show note indicating domain verification failure.
111
	 */
112 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...
113
		$note = new WC_Admin_Note();
114
		$note->set_title( __( 'Apple Pay domain verification needed', 'woocommerce-gateway-stripe' ) );
115
		$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' ) );
116
		$note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
117
		$note->set_name( self::FAILURE_NOTE_NAME );
118
		$note->set_source( 'woocommerce-gateway-stripe' );
119
		$note->add_action(
120
			'learn-more',
121
			__( 'Learn more', 'woocommerce-gateway-stripe' ),
122
			'https://docs.woocommerce.com/document/stripe/#apple-pay'
123
		);
124
		$note->save();
125
	}
126
}
127
128
new WC_Stripe_Inbox_Notes();
129