WC_Stripe_Inbox_Notes::get_success_title()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 7
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
	const POST_SETUP_SUCCESS_ACTION    = 'wc_stripe_apple_pay_post_setup_success';
19
	const CAMPAIGN_2020_CLEANUP_ACTION = 'wc_stripe_apple_pay_2020_cleanup';
20
21
	public function __construct() {
22
		add_action( self::POST_SETUP_SUCCESS_ACTION, array( self::class, 'create_marketing_note' ) );
23
		add_action( self::CAMPAIGN_2020_CLEANUP_ACTION, array( self::class, 'cleanup_campaign_2020' ) );
24
25
		// Schedule a 2020 holiday campaign cleanup action if needed.
26
		// First, check to see if we are still before the cutoff.
27
		// We don't need to (re)schedule this after the cutoff.
28
		if ( current_time( 'timestamp', true ) < self::get_campaign_2020_cutoff() ) {
29
			// If we don't have the clean up action scheduled, add it.
30
			if ( ! wp_next_scheduled( self::CAMPAIGN_2020_CLEANUP_ACTION ) ) {
31
				wp_schedule_single_event( self::get_campaign_2020_cutoff(), self::CAMPAIGN_2020_CLEANUP_ACTION );
32
			}
33
		}
34
	}
35
36
	public static function get_campaign_2020_cutoff() {
37
		return strtotime( '22 December 2020' );
38
	}
39
40
	public static function get_success_title() {
41
		if ( current_time( 'timestamp', true ) < self::get_campaign_2020_cutoff() ) {
42
			return __( 'Boost sales this holiday season with Apple Pay!', 'woocommerce-gateway-stripe' );
43
		}
44
45
		return __( 'Boost sales with Apple Pay!', 'woocommerce-gateway-stripe' );
46
	}
47
48
	/**
49
	 * Manage notes to show after Apple Pay domain verification.
50
	 */
51
	public static function notify_on_apple_pay_domain_verification( $verification_complete ) {
52
		if ( ! class_exists( 'Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes' ) ) {
53
			return;
54
		}
55
56
		if ( ! class_exists( 'WC_Data_Store' ) ) {
57
			return;
58
		}
59
60
		if ( $verification_complete ) {
61
			if ( self::should_show_marketing_note() && ! wp_next_scheduled( self::POST_SETUP_SUCCESS_ACTION ) ) {
62
				wp_schedule_single_event( time() + DAY_IN_SECONDS, self::POST_SETUP_SUCCESS_ACTION );
63
			}
64
65
			// If the domain verification completed after failure note was created, make sure it's marked as actioned.
66
			try {
67
				$data_store       = WC_Data_Store::load( 'admin-note' );
68
				$failure_note_ids = $data_store->get_notes_with_name( self::FAILURE_NOTE_NAME );
69
				if ( ! empty( $failure_note_ids ) ) {
70
					$note_id = array_pop( $failure_note_ids );
71
					$note    = WC_Admin_Notes::get_note( $note_id );
72
					if ( false !== $note && WC_Admin_Note::E_WC_ADMIN_NOTE_ACTIONED !== $note->get_status() ) {
73
						$note->set_status( WC_Admin_Note::E_WC_ADMIN_NOTE_ACTIONED );
74
						$note->save();
75
					}
76
				}
77
			} catch ( Exception $e ) {}  // @codingStandardsIgnoreLine.
78
		} else {
79
			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...
80
				self::create_failure_note();
81
			}
82
		}
83
	}
84
85
	/**
86
	 * Whether conditions are right for the marketing note.
87
	 */
88
	public static function should_show_marketing_note() {
89
		// Display to US merchants only.
90
		$base_location = wc_get_base_location();
91
		if ( ! $base_location || 'US' !== $base_location['country'] ) {
92
			return false;
93
		}
94
95
		// Make sure Apple Pay is enabled and setup is successful.
96
		$stripe_settings       = get_option( 'woocommerce_stripe_settings', array() );
97
		$stripe_enabled        = isset( $stripe_settings['enabled'] ) && 'yes' === $stripe_settings['enabled'];
98
		$button_enabled        = isset( $stripe_settings['payment_request'] ) && 'yes' === $stripe_settings['payment_request'];
99
		$verification_complete = isset( $stripe_settings['apple_pay_domain_set'] ) && 'yes' === $stripe_settings['apple_pay_domain_set'];
100
		if ( ! $stripe_enabled || ! $button_enabled || ! $verification_complete ) {
101
			return false;
102
		}
103
104
		// Make sure note doesn't already exist.
105
		try {
106
			$data_store       = WC_Data_Store::load( 'admin-note' );
107
			$success_note_ids = $data_store->get_notes_with_name( self::SUCCESS_NOTE_NAME );
108
			if ( ! empty( $success_note_ids ) ) {
109
				return false;
110
			}
111
		} catch ( Exception $e ) {
112
			return false; // If unable to check, assume it shouldn't show note.
113
		}
114
115
		return true;
116
	}
117
118
	/**
119
	 * If conditions are right, show note promoting Apple Pay marketing guide.
120
	 */
121
	public static function create_marketing_note() {
122
		// Make sure conditions for this note still hold.
123
		if ( ! self::should_show_marketing_note() ) {
124
			return;
125
		}
126
127
		try {
128
			$note = new WC_Admin_Note();
129
			$note->set_title( self::get_success_title() );
130
			$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' ) );
131
			$note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_MARKETING );
132
			$note->set_name( self::SUCCESS_NOTE_NAME );
133
			$note->set_source( 'woocommerce-gateway-stripe' );
134
			$note->add_action(
135
				'marketing-guide',
136
				__( 'See marketing guide', 'woocommerce-gateway-stripe' ),
137
				'https://developer.apple.com/apple-pay/marketing/'
138
			);
139
			$note->save();
140
		} catch ( Exception $e ) {} // @codingStandardsIgnoreLine.
141
	}
142
143
	/**
144
	 * Show note indicating domain verification failure.
145
	 */
146
	public static function create_failure_note() {
147
		try {
148
			$note = new WC_Admin_Note();
149
			$note->set_title( __( 'Apple Pay domain verification needed', 'woocommerce-gateway-stripe' ) );
150
			$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' ) );
151
			$note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
152
			$note->set_name( self::FAILURE_NOTE_NAME );
153
			$note->set_source( 'woocommerce-gateway-stripe' );
154
			$note->add_action(
155
				'learn-more',
156
				__( 'Learn more', 'woocommerce-gateway-stripe' ),
157
				'https://docs.woocommerce.com/document/stripe/#apple-pay'
158
			);
159
			$note->save();
160
		} catch ( Exception $e ) {} // @codingStandardsIgnoreLine.
161
	}
162
163
	/**
164
	 * Destroy unactioned inbox notes from the 2020 holiday campaign, replacing
165
	 * them with a non-holiday note promoting Apple Pay. This will be run once
166
	 * on/about 2020 Dec 22.
167
	 */
168
	public static function cleanup_campaign_2020() {
169
		if ( ! class_exists( 'Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes') ) {
170
			return;
171
		}
172
173
		if ( ! class_exists( 'WC_Data_Store' ) ) {
174
			return;
175
		}
176
177
		$note_ids = array();
178
179
		try {
180
			$data_store = WC_Data_Store::load( 'admin-note' );
181
			$note_ids   = $data_store->get_notes_with_name( self::SUCCESS_NOTE_NAME );
182
			if ( empty( $note_ids ) ) {
183
				return;
184
			}
185
		} catch ( Exception $e ) {
186
			return;
187
		}
188
189
		$deleted_an_unactioned_note = false;
190
191
		foreach ( (array) $note_ids as $note_id ) {
192
			try {
193
				$note = new WC_Admin_Note( $note_id );
194
				if ( WC_Admin_Note::E_WC_ADMIN_NOTE_UNACTIONED == $note->get_status() ) {
195
					$note->delete();
196
					$deleted_an_unactioned_note = true;
197
				}
198
				unset( $note );
199
			} catch ( Exception $e ) {} // @codingStandardsIgnoreLine.
200
		}
201
202
		if ( $deleted_an_unactioned_note ) {
203
			self::create_marketing_note();
204
		}
205
	}
206
}
207
208
new WC_Stripe_Inbox_Notes();
209