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.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / admin / class-wc-admin-marketplace-promotions.php
woocommerce / includes / admin Last commit date
helper 5 months ago importers 1 year ago list-tables 5 months ago marketplace-suggestions 10 months ago meta-boxes 5 months ago notes 5 months ago plugin-updates 2 years ago reports 8 months ago settings 5 months ago views 5 months ago class-wc-admin-addons.php 8 months ago class-wc-admin-api-keys-table-list.php 2 years ago class-wc-admin-api-keys.php 10 months ago class-wc-admin-assets.php 7 months ago class-wc-admin-attributes.php 3 years ago class-wc-admin-brands.php 5 months ago class-wc-admin-customize.php 5 years ago class-wc-admin-dashboard-setup.php 10 months ago class-wc-admin-dashboard.php 5 months ago class-wc-admin-duplicate-product.php 5 months ago class-wc-admin-exporters.php 1 year ago class-wc-admin-help.php 2 years ago class-wc-admin-importers.php 10 months ago class-wc-admin-log-table-list.php 1 year ago class-wc-admin-marketplace-promotions.php 1 year ago class-wc-admin-menus.php 5 months ago class-wc-admin-meta-boxes.php 1 year ago class-wc-admin-notices.php 5 months ago class-wc-admin-permalink-settings.php 5 years ago class-wc-admin-pointers.php 3 years ago class-wc-admin-post-types.php 1 year ago class-wc-admin-profile.php 1 year ago class-wc-admin-reports.php 1 year ago class-wc-admin-settings.php 5 months ago class-wc-admin-setup-wizard.php 5 months ago class-wc-admin-status.php 1 year ago class-wc-admin-taxonomies.php 7 months ago class-wc-admin-upload-downloadable-product.php 2 years ago class-wc-admin-webhooks-table-list.php 1 year ago class-wc-admin-webhooks.php 10 months ago class-wc-admin.php 7 months ago wc-admin-functions.php 7 months ago wc-meta-box-functions.php 1 year ago woocommerce-legacy-reports.php 1 year ago
class-wc-admin-marketplace-promotions.php
411 lines
1 <?php
2 /**
3 * Addons Page
4 *
5 * @package WooCommerce\Admin
6 * @version 2.5.0
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * WC_Admin_Marketplace_Promotions class.
15 */
16 class WC_Admin_Marketplace_Promotions {
17
18 const CRON_NAME = 'woocommerce_marketplace_cron_fetch_promotions';
19 const TRANSIENT_NAME = 'woocommerce_marketplace_promotions_v2';
20 const TRANSIENT_LIFE_SPAN = DAY_IN_SECONDS;
21 const PROMOTIONS_API_URL = 'https://woocommerce.com/wp-json/wccom-extensions/3.0/promotions';
22
23 /**
24 * The user's locale, for example en_US.
25 *
26 * @var string
27 */
28 public static string $locale;
29
30 /**
31 * On all admin pages, try go get Marketplace promotions every day.
32 * Shows notice and adds menu badge to WooCommerce Extensions item
33 * if the promotions API requests them.
34 *
35 * WC_Admin calls this method when it is instantiated during
36 * is_admin requests.
37 *
38 * @return void
39 */
40 public static function init() {
41 // A legacy hook that can be triggered by action scheduler.
42 add_action( 'woocommerce_marketplace_fetch_promotions', array( __CLASS__, 'clear_deprecated_action' ) );
43 add_action(
44 'woocommerce_marketplace_fetch_promotions_clear',
45 array(
46 __CLASS__,
47 'clear_deprecated_scheduled_event',
48 )
49 );
50
51 // Fetch promotions from the API and store them in a transient.
52 add_action( self::CRON_NAME, array( __CLASS__, 'update_promotions' ) );
53
54 if (
55 defined( 'DOING_AJAX' ) && DOING_AJAX
56 || defined( 'DOING_CRON' ) && DOING_CRON
57 || defined( 'WP_CLI' ) && WP_CLI
58 ) {
59 return;
60 }
61
62 if ( ! is_admin() ) {
63 return;
64 }
65
66 self::schedule_cron_event();
67
68 register_deactivation_hook( WC_PLUGIN_FILE, array( __CLASS__, 'clear_cron_event' ) );
69
70 self::$locale = ( self::$locale ?? get_user_locale() ) ?? 'en_US';
71 self::maybe_show_bubble_promotions();
72 }
73
74 /**
75 * Schedule a daily cron event to fetch promotions.
76 *
77 * @version 9.5.0
78 *
79 * @return void
80 */
81 private static function schedule_cron_event() {
82 if ( ! wp_next_scheduled( self::CRON_NAME ) ) {
83 wp_schedule_event( time(), 'twicedaily', self::CRON_NAME );
84 }
85 }
86
87 /**
88 * Fetch promotions from the API and store them in a transient.
89 *
90 * @return void
91 */
92 public static function update_promotions() {
93 // Fetch promotions from the API.
94 $promotions = self::fetch_marketplace_promotions();
95 set_transient( self::TRANSIENT_NAME, $promotions, self::TRANSIENT_LIFE_SPAN );
96 }
97
98 /**
99 * Get active Marketplace promotions from the transient.
100 * Use `woocommerce_marketplace_suppress_promotions` filter to suppress promotions.
101 *
102 * @since 9.0
103 */
104 public static function get_active_promotions() {
105 /**
106 * Filter to suppress the requests for and showing of marketplace promotions.
107 *
108 * @since 8.8
109 */
110 if ( apply_filters( 'woocommerce_marketplace_suppress_promotions', false ) ) {
111 return array();
112 }
113
114 $promotions = get_transient( self::TRANSIENT_NAME );
115 if ( ! $promotions ) {
116 return array();
117 }
118
119 $promotions = self::merge_promos( $promotions );
120
121 return self::filter_out_inactive_promotions( $promotions );
122 }
123
124 /**
125 * Get promotions to show in the Woo in-app marketplace and load them into a transient
126 * with a 12-hour life. Run as a recurring scheduled action.
127 *
128 * @return array
129 */
130 private static function fetch_marketplace_promotions() {
131 /**
132 * Filter to suppress the requests for and showing of marketplace promotions.
133 *
134 * @since 8.8
135 */
136 if ( apply_filters( 'woocommerce_marketplace_suppress_promotions', false ) ) {
137 return array();
138 }
139
140 // Fetch promotions from the API.
141 $fetch_options = array(
142 'auth' => true,
143 'country' => true,
144 );
145 $raw_promotions = WC_Admin_Addons::fetch( self::PROMOTIONS_API_URL, $fetch_options );
146
147 // phpcs:disable WordPress.NamingConventions.ValidHookName.UseUnderscores
148 if ( is_wp_error( $raw_promotions ) ) {
149 /**
150 * Allows connection error to be handled.
151 *
152 * @since 8.7
153 */
154 do_action( 'woocommerce_page_wc-addons_connection_error', $raw_promotions->get_error_message() );
155 }
156
157 $response_code = (int) wp_remote_retrieve_response_code( $raw_promotions );
158 if ( 200 !== $response_code ) {
159 /**
160 * Allows connection error to be handled.
161 *
162 * @since 8.7
163 */
164 do_action( 'woocommerce_page_wc-addons_connection_error', $response_code );
165 }
166
167 $promotions = json_decode( wp_remote_retrieve_body( $raw_promotions ), true );
168
169 if ( ! is_array( $promotions ) ) {
170 $promotions = array();
171
172 /**
173 * Allows connection error to be handled.
174 *
175 * @since 8.7
176 */
177 do_action( 'woocommerce_page_wc-addons_connection_error', 'Malformed response' );
178 }
179 // phpcs:enable WordPress.NamingConventions.ValidHookName.UseUnderscores
180
181 return $promotions;
182 }
183
184 /**
185 * If there's an active promotion of the format `menu_bubble`,
186 * add a filter to show a bubble on the Extensions item in the
187 * WooCommerce menu.
188 *
189 * Use `woocommerce_marketplace_suppress_promotions` filter to suppress the bubble.
190 *
191 * @return void
192 * @throws Exception If we are unable to create a DateTime from the date_to_gmt.
193 */
194 private static function maybe_show_bubble_promotions() {
195 /**
196 * Filter to suppress the requests for and showing of marketplace promotions.
197 *
198 * @since 8.8
199 */
200 if ( apply_filters( 'woocommerce_marketplace_suppress_promotions', false ) ) {
201 return;
202 }
203
204 $promotions = get_transient( self::TRANSIENT_NAME );
205 if ( ! $promotions ) {
206 return;
207 }
208
209 $bubble_promotions = self::get_promotions_of_format( $promotions, 'menu_bubble' );
210 if ( empty( $bubble_promotions ) ) {
211 return;
212 }
213
214 $now_date_time = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
215
216 // Let's make absolutely sure the promotion is still active.
217 foreach ( $bubble_promotions as $promotion ) {
218 if ( ! isset( $promotion['date_to_gmt'] ) ) {
219 continue;
220 }
221
222 try {
223 $date_to_gmt = new DateTime( $promotion['date_to_gmt'], new DateTimeZone( 'UTC' ) );
224 } catch ( \Exception $ex ) {
225 continue;
226 }
227
228 if ( $now_date_time < $date_to_gmt ) {
229 add_filter(
230 'woocommerce_marketplace_menu_items',
231 function ( $marketplace_pages ) use ( $promotion ) {
232 return self::filter_marketplace_menu_items( $marketplace_pages, $promotion );
233 }
234 );
235
236 break;
237 }
238 }
239 }
240
241 /**
242 * From the array of promotions, select those of a given format.
243 *
244 * @param ?array $promotions Array of data about promotions of all formats.
245 * @param ?string $format Format we want to filter for.
246 *
247 * @return array
248 */
249 private static function get_promotions_of_format( $promotions = array(), $format = '' ): array {
250 if ( empty( $promotions ) || empty( $format ) ) {
251 return array();
252 }
253
254 return array_filter(
255 $promotions,
256 function( $promotion ) use ( $format ) {
257 return isset( $promotion['format'] ) && $format === $promotion['format'];
258 }
259 );
260 }
261
262 /**
263 * Find promotions that are still active – they have a date range that
264 * includes the current date.
265 *
266 * @param ?array $promotions Data about current promotions.
267 *
268 * @return array
269 */
270 private static function filter_out_inactive_promotions( $promotions = array() ) {
271 $now_date_time = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
272 $active_promotions = array();
273
274 foreach ( $promotions as $promotion ) {
275 if ( ! isset( $promotion['date_from_gmt'] ) || ! isset( $promotion['date_to_gmt'] ) ) {
276 continue;
277 }
278
279 try {
280 $date_from_gmt = new DateTime( $promotion['date_from_gmt'], new DateTimeZone( 'UTC' ) );
281 $date_to_gmt = new DateTime( $promotion['date_to_gmt'], new DateTimeZone( 'UTC' ) );
282 } catch ( \Exception $ex ) {
283 continue;
284 }
285
286 if ( $now_date_time >= $date_from_gmt && $now_date_time <= $date_to_gmt ) {
287 $active_promotions[] = $promotion;
288 }
289 }
290
291 // Sort promotions so the ones starting more recently are at the top.
292 usort(
293 $active_promotions,
294 function ( $a, $b ) {
295 return $b['date_from_gmt'] <=> $a['date_from_gmt'];
296 }
297 );
298
299 return $active_promotions;
300 }
301
302 /**
303 * Promos arrive in the array of promotions as an array of arrays with the key 'promos'.
304 * We merge them into the main array.
305 *
306 * @param ?array $promotions Promotions data received from WCCOM.
307 * May have an element with the key 'promos', which contains an array.
308 *
309 * @return array
310 * */
311 private static function merge_promos( ?array $promotions = array() ): array {
312 if (
313 ! empty( $promotions['promos'] )
314 && is_array( $promotions['promos'] )
315 ) {
316 $promotions = array_merge( $promotions, $promotions['promos'] );
317 unset( $promotions['promos'] );
318 }
319
320 return $promotions;
321 }
322
323 /**
324 * Callback for the `woocommerce_marketplace_menu_items` filter
325 * in `Automattic\WooCommerce\Internal\Admin\Marketplace::get_marketplace_pages`.
326 * At the moment, the Extensions page is the only page in `$menu_items`.
327 * Adds a bubble to the menu item.
328 *
329 * @param array $menu_items Arrays representing items in nav menu.
330 * @param ?array $promotion Data about a promotion from the WooCommerce.com API.
331 *
332 * @return array
333 */
334 public static function filter_marketplace_menu_items( $menu_items, $promotion = array() ): array {
335 if ( ! isset( $promotion['menu_item_id'] ) || ! isset( $promotion['content'] ) ) {
336 return $menu_items;
337 }
338 foreach ( $menu_items as $index => $menu_item ) {
339 if (
340 'woocommerce' === $menu_item['parent']
341 && $promotion['menu_item_id'] === $menu_item['id']
342 ) {
343 $bubble_text = $promotion['content'][ self::$locale ] ?? ( $promotion['content']['en_US'] ?? __( 'Sale', 'woocommerce' ) );
344 $menu_items[ $index ]['title'] = self::append_bubble( $menu_item['title'], $bubble_text );
345
346 break;
347 }
348 }
349
350 return $menu_items;
351 }
352
353 /**
354 * Return the markup for a menu item bubble with a given text.
355 *
356 * @param string $menu_item_text Text of menu item we want to change.
357 * @param string $bubble_text Text of bubble.
358 *
359 * @return string
360 */
361 private static function append_bubble( string $menu_item_text, string $bubble_text ): string {
362 // Strip out update count bubble added by Marketplace::get_marketplace_update_count_html.
363 $menu_item_text = preg_replace( '|<span class="update-plugins count-[\d]+">[A-z0-9 <>="-]+</span>|', '', $menu_item_text );
364
365 return $menu_item_text
366 . '<span class="awaiting-mod update-plugins remaining-tasks-badge woocommerce-task-list-remaining-tasks-badge">'
367 . esc_html( $bubble_text )
368 . '</span>';
369 }
370
371 /**
372 * When WooCommerce is disabled, clear the WP Cron event we use to fetch promotions.
373 *
374 * @version 9.5.0
375 *
376 * @return void
377 */
378 public static function clear_cron_event() {
379 $timestamp = wp_next_scheduled( self::CRON_NAME );
380 wp_unschedule_event( $timestamp, self::CRON_NAME );
381 }
382
383 /**
384 * Clear deprecated scheduled action that was used to fetch promotions in WooCommerce 8.8.
385 * Replaced with a transient in WooCommerce 9.0.
386 *
387 * @return void
388 */
389 public static function clear_deprecated_scheduled_event() {
390 if ( function_exists( 'as_unschedule_all_actions' ) ) {
391 as_unschedule_all_actions( 'woocommerce_marketplace_fetch_promotions' );
392 }
393 }
394
395 /**
396 * We can't clear deprecated action from AS when it's running,
397 * so we schedule a new single action to clear the deprecated
398 * `woocommerce_marketplace_fetch_promotions` action.
399 */
400 public static function clear_deprecated_action() {
401 if ( function_exists( 'as_schedule_single_action' ) ) {
402 as_schedule_single_action( time(), 'woocommerce_marketplace_fetch_promotions_clear' );
403 }
404 }
405 }
406
407 // Fetch list of promotions from WooCommerce.com for WooCommerce admin UI.
408 if ( ! has_action( 'init', array( 'WC_Admin_Marketplace_Promotions', 'init' ) ) ) {
409 add_action( 'init', array( 'WC_Admin_Marketplace_Promotions', 'init' ), 11 );
410 }
411