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 |