blob: 94e74b448e6d7f44f371f1d1c8630c4761df6f55 [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2
3if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5}
6
7/**
8 * Class that adds Inbox notifications.
9 *
10 * @since 4.5.4
11 */
12class WC_Stripe_Inbox_Notes {
13 const SUCCESS_NOTE_NAME = 'stripe-apple-pay-marketing-guide-holiday-2020';
14 const FAILURE_NOTE_NAME = 'stripe-apple-pay-domain-verification-needed';
15
16 const POST_SETUP_SUCCESS_ACTION = 'wc_stripe_apple_pay_post_setup_success';
17 const CAMPAIGN_2020_CLEANUP_ACTION = 'wc_stripe_apple_pay_2020_cleanup';
18
19 public function __construct() {
20 add_action( self::POST_SETUP_SUCCESS_ACTION, [ self::class, 'create_marketing_note' ] );
21 add_action( self::CAMPAIGN_2020_CLEANUP_ACTION, [ self::class, 'cleanup_campaign_2020' ] );
22 add_action( 'admin_init', [ self::class, 'create_upe_notes' ] );
23
24 // Schedule a 2020 holiday campaign cleanup action if needed.
25 // First, check to see if we are still before the cutoff.
26 // We don't need to (re)schedule this after the cutoff.
27 if ( time() < self::get_campaign_2020_cutoff() ) {
28 // If we don't have the clean up action scheduled, add it.
29 if ( ! wp_next_scheduled( self::CAMPAIGN_2020_CLEANUP_ACTION ) ) {
30 wp_schedule_single_event( self::get_campaign_2020_cutoff(), self::CAMPAIGN_2020_CLEANUP_ACTION );
31 }
32 }
33 }
34
35 public static function are_inbox_notes_supported() {
36 if ( ! class_exists( 'WC_Data_Store' ) ) {
37 return false;
38 }
39
40 try {
41 WC_Data_Store::load( 'admin-note' );
42 } catch ( Exception $e ) {
43 return false;
44 }
45
46 return trait_exists( 'Automattic\WooCommerce\Admin\Notes\NoteTraits' ) && class_exists( 'Automattic\WooCommerce\Admin\Notes\Note' );
47 }
48
49 public static function create_upe_notes() {
50 if ( ! self::are_inbox_notes_supported() ) {
51 return;
52 }
53
54 require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-availability-note.php';
55 WC_Stripe_UPE_Availability_Note::init();
56
57 require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-stripelink-note.php';
58 WC_Stripe_UPE_StripeLink_Note::init( WC_Stripe::get_instance()->get_main_stripe_gateway() );
59 }
60
61 public static function get_campaign_2020_cutoff() {
62 return strtotime( '22 December 2020' );
63 }
64
65 public static function get_success_title() {
66 if ( time() < self::get_campaign_2020_cutoff() ) {
67 return __( 'Boost sales this holiday season with Apple Pay!', 'woocommerce-gateway-stripe' );
68 }
69
70 return __( 'Boost sales with Apple Pay!', 'woocommerce-gateway-stripe' );
71 }
72
73 /**
74 * Manage notes to show after Apple Pay domain verification.
75 */
76 public static function notify_on_apple_pay_domain_verification( $verification_complete ) {
77 $admin_notes_class = WC_Stripe_Woo_Compat_Utils::get_notes_class();
78 if ( ! class_exists( $admin_notes_class ) || ! class_exists( 'WC_Data_Store' ) ) {
79 return;
80 }
81
82 try {
83 $data_store = WC_Data_Store::load( 'admin-note' );
84 $failure_note_ids = $data_store->get_notes_with_name( self::FAILURE_NOTE_NAME );
85 // Delete all previously created, soft deleted and unactioned failure notes (Legacy).
86 while ( ! empty( $failure_note_ids ) ) {
87 $note_id = array_pop( $failure_note_ids );
88 $note = $admin_notes_class::get_note( $note_id );
89 $note->delete();
90 }
91 } catch ( Exception $e ) {} // @codingStandardsIgnoreLine
92
93 if ( $verification_complete ) {
94 if ( self::should_show_marketing_note() && ! wp_next_scheduled( self::POST_SETUP_SUCCESS_ACTION ) ) {
95 wp_schedule_single_event( time() + DAY_IN_SECONDS, self::POST_SETUP_SUCCESS_ACTION );
96 }
97 } else {
98 // Create new note if verification failed.
99 self::create_failure_note();
100 }
101 }
102
103 /**
104 * Whether conditions are right for the marketing note.
105 */
106 public static function should_show_marketing_note() {
107 // Display to US merchants only.
108 $base_location = wc_get_base_location();
109 if ( ! $base_location || 'US' !== $base_location['country'] ) {
110 return false;
111 }
112
113 // Make sure Apple Pay is enabled and setup is successful.
114 $stripe_settings = get_option( 'woocommerce_stripe_settings', [] );
115 $stripe_enabled = isset( $stripe_settings['enabled'] ) && 'yes' === $stripe_settings['enabled'];
116 $button_enabled = isset( $stripe_settings['payment_request'] ) && 'yes' === $stripe_settings['payment_request'];
117 $verification_complete = isset( $stripe_settings['apple_pay_domain_set'] ) && 'yes' === $stripe_settings['apple_pay_domain_set'];
118 if ( ! $stripe_enabled || ! $button_enabled || ! $verification_complete ) {
119 return false;
120 }
121
122 // Make sure note doesn't already exist.
123 try {
124 $data_store = WC_Data_Store::load( 'admin-note' );
125 $success_note_ids = $data_store->get_notes_with_name( self::SUCCESS_NOTE_NAME );
126 if ( ! empty( $success_note_ids ) ) {
127 return false;
128 }
129 } catch ( Exception $e ) {
130 return false; // If unable to check, assume it shouldn't show note.
131 }
132
133 return true;
134 }
135
136 /**
137 * If conditions are right, show note promoting Apple Pay marketing guide.
138 */
139 public static function create_marketing_note() {
140 // Make sure conditions for this note still hold.
141 if ( ! self::should_show_marketing_note() || ! self::are_inbox_notes_supported() ) {
142 return;
143 }
144
145 try {
146 $admin_note_class = WC_Stripe_Woo_Compat_Utils::get_note_class();
147 $note = new $admin_note_class();
148 $note->set_title( self::get_success_title() );
149 $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' ) );
150 $note->set_type( $admin_note_class::E_WC_ADMIN_NOTE_MARKETING );
151 $note->set_name( self::SUCCESS_NOTE_NAME );
152 $note->set_source( 'woocommerce-gateway-stripe' );
153 $note->add_action(
154 'marketing-guide',
155 __( 'See marketing guide', 'woocommerce-gateway-stripe' ),
156 'https://developer.apple.com/apple-pay/marketing/'
157 );
158 $note->save();
159 } catch ( Exception $e ) {} // @codingStandardsIgnoreLine.
160 }
161
162 /**
163 * Show note indicating domain verification failure.
164 */
165 public static function create_failure_note() {
166 try {
167 $admin_note_class = WC_Stripe_Woo_Compat_Utils::get_note_class();
168 $note = new $admin_note_class();
169 $note->set_title( __( 'Apple Pay domain verification needed', 'woocommerce-gateway-stripe' ) );
170 $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' ) );
171 $note->set_type( $admin_note_class::E_WC_ADMIN_NOTE_INFORMATIONAL );
172 $note->set_name( self::FAILURE_NOTE_NAME );
173 $note->set_source( 'woocommerce-gateway-stripe' );
174 $note->add_action(
175 'learn-more',
176 __( 'Learn more', 'woocommerce-gateway-stripe' ),
177 'https://woocommerce.com/document/stripe/#apple-pay'
178 );
179 $note->save();
180 } catch ( Exception $e ) {} // @codingStandardsIgnoreLine.
181 }
182
183 /**
184 * Destroy unactioned inbox notes from the 2020 holiday campaign, replacing
185 * them with a non-holiday note promoting Apple Pay. This will be run once
186 * on/about 2020 Dec 22.
187 */
188 public static function cleanup_campaign_2020() {
189 if ( ! self::are_inbox_notes_supported() ) {
190 return;
191 }
192
193 $admin_notes_class = WC_Stripe_Woo_Compat_Utils::get_notes_class();
194 if ( ! class_exists( $admin_notes_class ) || ! class_exists( 'WC_Data_Store' ) ) {
195 return;
196 }
197
198 $note_ids = [];
199
200 try {
201 $data_store = WC_Data_Store::load( 'admin-note' );
202 $note_ids = $data_store->get_notes_with_name( self::SUCCESS_NOTE_NAME );
203 if ( empty( $note_ids ) ) {
204 return;
205 }
206 } catch ( Exception $e ) {
207 return;
208 }
209
210 $deleted_an_unactioned_note = false;
211
212 $admin_note_class = WC_Stripe_Woo_Compat_Utils::get_note_class();
213 foreach ( (array) $note_ids as $note_id ) {
214 try {
215 $note = new $admin_note_class( $note_id );
216 if ( $admin_note_class::E_WC_ADMIN_NOTE_UNACTIONED == $note->get_status() ) {
217 $note->delete();
218 $deleted_an_unactioned_note = true;
219 }
220 unset( $note );
221 } catch ( Exception $e ) {} // @codingStandardsIgnoreLine.
222 }
223
224 if ( $deleted_an_unactioned_note ) {
225 self::create_marketing_note();
226 }
227 }
228}
229
230new WC_Stripe_Inbox_Notes();