Completed
Push — master ( 949330...bc25c6 )
by
unknown
13s queued 10s
created

WC_Stripe_Inbox_Notes::cleanup_campaign_2020()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 11
nop 0
dl 0
loc 36
rs 8.0995
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
		$note = new WC_Admin_Note();
128
		$note->set_title( self::get_success_title() );
129
		$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' ) );
130
		$note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_MARKETING );
131
		$note->set_name( self::SUCCESS_NOTE_NAME );
132
		$note->set_source( 'woocommerce-gateway-stripe' );
133
		$note->add_action(
134
			'marketing-guide',
135
			__( 'See marketing guide', 'woocommerce-gateway-stripe' ),
136
			'https://developer.apple.com/apple-pay/marketing/'
137
		);
138
		$note->save();
139
	}
140
141
	/**
142
	 * Show note indicating domain verification failure.
143
	 */
144
	public static function create_failure_note() {
145
		$note = new WC_Admin_Note();
146
		$note->set_title( __( 'Apple Pay domain verification needed', 'woocommerce-gateway-stripe' ) );
147
		$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' ) );
148
		$note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
149
		$note->set_name( self::FAILURE_NOTE_NAME );
150
		$note->set_source( 'woocommerce-gateway-stripe' );
151
		$note->add_action(
152
			'learn-more',
153
			__( 'Learn more', 'woocommerce-gateway-stripe' ),
154
			'https://docs.woocommerce.com/document/stripe/#apple-pay'
155
		);
156
		$note->save();
157
	}
158
159
	/**
160
	 * Destroy unactioned inbox notes from the 2020 holiday campaign, replacing
161
	 * them with a non-holiday note promoting Apple Pay. This will be run once
162
	 * on/about 2020 Dec 22.
163
	 */
164
	public static function cleanup_campaign_2020() {
165
		if ( ! class_exists( 'Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes') ) {
166
			return;
167
		}
168
169
		if ( ! class_exists( 'WC_Data_Store' ) ) {
170
			return;
171
		}
172
173
		$note_ids = array();
174
175
		try {
176
			$data_store = WC_Data_Store::load( 'admin-note' );
177
			$note_ids   = $data_store->get_notes_with_name( self::SUCCESS_NOTE_NAME );
178
			if ( empty( $note_ids ) ) {
179
				return;
180
			}
181
		} catch ( Exception $e ) {
182
			return;
183
		}
184
185
		$deleted_an_unactioned_note = false;
186
187
		foreach ( (array) $note_ids as $note_id ) {
188
			$note = new WC_Admin_Note( $note_id );
189
			if ( WC_Admin_Note::E_WC_ADMIN_NOTE_UNACTIONED == $note->get_status() ) {
190
				$note->delete();
191
				$deleted_an_unactioned_note = true;
192
			}
193
			unset( $note );
194
		}
195
196
		if ( $deleted_an_unactioned_note ) {
197
			self::create_marketing_note();
198
		}
199
	}
200
}
201
202
new WC_Stripe_Inbox_Notes();
203