LegacySelect2UsageTracker.php
433 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Tracks usage of WooCommerce's bundled legacy Select2 handles. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\LegacyAssets; |
| 9 | |
| 10 | use Automattic\Woocommerce_Analytics\WC_Analytics_Tracking; |
| 11 | use Automattic\WooCommerce\Internal\RegisterHooksInterface; |
| 12 | use WP_Scripts; |
| 13 | use WC_Site_Tracking; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * Tracks extensions that still depend on the legacy Select2 handles bundled by WooCommerce. |
| 19 | */ |
| 20 | class LegacySelect2UsageTracker implements RegisterHooksInterface { |
| 21 | |
| 22 | public const EVENT_NAME = 'legacy_select2_usage_detected'; |
| 23 | |
| 24 | private const CONTEXT_ADMIN = 'admin'; |
| 25 | private const CONTEXT_FRONTEND = 'frontend'; |
| 26 | |
| 27 | private const LEGACY_HANDLES = array( |
| 28 | 'select2', |
| 29 | 'wc-select2', |
| 30 | ); |
| 31 | |
| 32 | private const TRANSIENT_KEY_PREFIX = 'wc_legacy_select2_check_'; |
| 33 | |
| 34 | /** |
| 35 | * Register hook callbacks. |
| 36 | * |
| 37 | * @return void |
| 38 | * |
| 39 | * @since 11.0.0 |
| 40 | */ |
| 41 | public function register() { |
| 42 | add_action( 'wp_print_footer_scripts', array( $this, 'handle_wp_print_footer_scripts' ), PHP_INT_MAX ); |
| 43 | |
| 44 | if ( WC_Site_Tracking::is_tracking_enabled() ) { |
| 45 | add_action( 'admin_print_footer_scripts', array( $this, 'handle_admin_print_footer_scripts' ), PHP_INT_MAX ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Handle the admin_print_footer_scripts hook. |
| 51 | * |
| 52 | * @internal |
| 53 | * |
| 54 | * @return void |
| 55 | */ |
| 56 | public function handle_admin_print_footer_scripts(): void { |
| 57 | $this->track_usage( self::CONTEXT_ADMIN ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Handle the wp_print_footer_scripts hook. |
| 62 | * |
| 63 | * @internal |
| 64 | * |
| 65 | * @return void |
| 66 | */ |
| 67 | public function handle_wp_print_footer_scripts(): void { |
| 68 | if ( ! $this->is_frontend_tracking_available() ) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | $this->track_usage( self::CONTEXT_FRONTEND ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Build and record legacy Select2 usage events for the current request. |
| 77 | * |
| 78 | * @param string $context The request context. |
| 79 | * @return void |
| 80 | */ |
| 81 | private function track_usage( string $context ): void { |
| 82 | if ( ! $this->is_legacy_select2_printed() ) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | $event = $this->get_usage_event( $context ); |
| 87 | if ( empty( $event ) ) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | if ( $this->was_recently_checked( $event ) ) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | $this->mark_recently_checked( $event ); |
| 96 | |
| 97 | if ( self::CONTEXT_FRONTEND === $context ) { |
| 98 | $this->record_frontend_event( self::EVENT_NAME, $event ); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | $this->record_event( self::EVENT_NAME, $event ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get a legacy Select2 usage event for the current script registry. |
| 107 | * |
| 108 | * @internal |
| 109 | * |
| 110 | * @param string $context The request context. |
| 111 | * @return array<string, string> |
| 112 | */ |
| 113 | public function get_usage_event( string $context ): array { |
| 114 | $wp_scripts = wp_scripts(); |
| 115 | if ( ! $wp_scripts instanceof WP_Scripts ) { |
| 116 | return array(); |
| 117 | } |
| 118 | |
| 119 | // `done` includes dependencies that WordPress printed while resolving the queue. |
| 120 | // Keep only queued handles so dependents identify scripts explicitly enqueued for the page. |
| 121 | $printed_queued_scripts = array_intersect( $wp_scripts->queue, $wp_scripts->done ); |
| 122 | |
| 123 | $handles = array(); |
| 124 | $dependents = array(); |
| 125 | $dependents_sources = array(); |
| 126 | $plugins_path = wp_parse_url( plugins_url( '/' ), PHP_URL_PATH ); |
| 127 | |
| 128 | foreach ( $printed_queued_scripts as $handle ) { |
| 129 | $legacy_handles = $this->get_legacy_handles( $wp_scripts, $handle ); |
| 130 | |
| 131 | if ( empty( $legacy_handles ) ) { |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | $handles += array_fill_keys( $legacy_handles, true ); |
| 136 | $dependents[ $handle ] = true; |
| 137 | $source = isset( $wp_scripts->registered[ $handle ] ) ? $wp_scripts->registered[ $handle ]->src : ''; |
| 138 | $source = self::get_plugin_relative_source( $source, $plugins_path ); |
| 139 | |
| 140 | if ( '' !== $source ) { |
| 141 | $dependents_sources[] = $source; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if ( empty( $handles ) ) { |
| 146 | return array(); |
| 147 | } |
| 148 | |
| 149 | $handles = array_keys( $handles ); |
| 150 | $dependents = array_keys( $dependents ); |
| 151 | $dependents_sources = array_unique( $dependents_sources ); |
| 152 | $handles_sources = array(); |
| 153 | |
| 154 | foreach ( $handles as $handle ) { |
| 155 | $source = self::get_legacy_handle_source( $wp_scripts, $handle ); |
| 156 | $source = self::get_plugin_relative_source( $source, $plugins_path ); |
| 157 | |
| 158 | if ( '' !== $source ) { |
| 159 | $handles_sources[] = $source; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | $handles_sources = array_unique( $handles_sources ); |
| 164 | |
| 165 | sort( $handles ); |
| 166 | sort( $dependents ); |
| 167 | sort( $dependents_sources ); |
| 168 | sort( $handles_sources ); |
| 169 | |
| 170 | return array( |
| 171 | 'context' => $context, |
| 172 | 'page_type' => $this->get_current_page_type( $context ), |
| 173 | 'handles' => implode( ',', $handles ), |
| 174 | 'dependents' => implode( ',', $dependents ), |
| 175 | 'dependents_sources' => implode( ',', $dependents_sources ), |
| 176 | 'handles_sources' => implode( ',', $handles_sources ), |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Record a Tracks event. |
| 182 | * |
| 183 | * @param string $event_name Event name. |
| 184 | * @param array<string, string> $properties Event properties. |
| 185 | * @return void |
| 186 | * |
| 187 | * @since 11.0.0 |
| 188 | */ |
| 189 | protected function record_event( string $event_name, array $properties ): void { |
| 190 | if ( ! class_exists( 'WC_Tracks' ) ) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | \WC_Tracks::record_event( $event_name, $properties ); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Add a storefront event to the WooCommerce Analytics client queue. |
| 199 | * |
| 200 | * @param string $event_name Event name. |
| 201 | * @param array<string, string> $properties Event properties. |
| 202 | * @return void |
| 203 | * |
| 204 | * @since 11.0.0 |
| 205 | */ |
| 206 | protected function record_frontend_event( string $event_name, array $properties ): void { |
| 207 | if ( ! $this->is_frontend_tracking_available() ) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | WC_Analytics_Tracking::add_event_to_queue( $event_name, $properties ); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Whether the WooCommerce Analytics storefront tracker is initialized. |
| 216 | * |
| 217 | * @return bool |
| 218 | * |
| 219 | * @since 11.0.0 |
| 220 | */ |
| 221 | protected function is_frontend_tracking_available(): bool { |
| 222 | return did_action( 'woocommerce_analytics_init' ) |
| 223 | && class_exists( WC_Analytics_Tracking::class ) |
| 224 | && is_callable( array( WC_Analytics_Tracking::class, 'add_event_to_queue' ) ); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Whether this usage event was already checked recently. |
| 229 | * |
| 230 | * @param array<string, string> $event Usage event. |
| 231 | * @return bool |
| 232 | */ |
| 233 | private function was_recently_checked( array $event ): bool { |
| 234 | return false !== get_transient( $this->get_transient_key( $event ) ); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Mark this usage event as recently checked. |
| 239 | * |
| 240 | * @param array<string, string> $event Usage event. |
| 241 | * @return void |
| 242 | */ |
| 243 | private function mark_recently_checked( array $event ): void { |
| 244 | set_transient( $this->get_transient_key( $event ), 'yes', WEEK_IN_SECONDS ); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Check whether a legacy Select2 handle has been printed. |
| 249 | * |
| 250 | * @return bool |
| 251 | */ |
| 252 | private function is_legacy_select2_printed(): bool { |
| 253 | foreach ( self::LEGACY_HANDLES as $handle ) { |
| 254 | if ( wp_script_is( $handle, 'done' ) ) { |
| 255 | return true; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Get the transient key for a usage event. |
| 264 | * |
| 265 | * @param array<string, string> $event Usage event. |
| 266 | * @return string |
| 267 | */ |
| 268 | private function get_transient_key( array $event ): string { |
| 269 | ksort( $event ); |
| 270 | |
| 271 | $event_json = wp_json_encode( $event ); |
| 272 | |
| 273 | return self::TRANSIENT_KEY_PREFIX . md5( is_string( $event_json ) ? $event_json : '' ); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Get legacy Select2 handles for a printed top-level handle. |
| 278 | * |
| 279 | * @param WP_Scripts $wp_scripts WordPress scripts registry. |
| 280 | * @param string $handle Script handle. |
| 281 | * @return array<int, string> |
| 282 | */ |
| 283 | private function get_legacy_handles( WP_Scripts $wp_scripts, string $handle ): array { |
| 284 | $legacy_handles = array(); |
| 285 | |
| 286 | if ( in_array( $handle, self::LEGACY_HANDLES, true ) ) { |
| 287 | $legacy_handles[ $handle ] = true; |
| 288 | return array_keys( $legacy_handles ); |
| 289 | } |
| 290 | |
| 291 | if ( isset( $wp_scripts->registered[ $handle ] ) ) { |
| 292 | foreach ( $wp_scripts->registered[ $handle ]->deps as $dependency_handle ) { |
| 293 | if ( in_array( $dependency_handle, self::LEGACY_HANDLES, true ) ) { |
| 294 | $legacy_handles[ $dependency_handle ] = true; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | return array_keys( $legacy_handles ); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Get a script source relative to the plugins directory. |
| 304 | * |
| 305 | * @param string|false $source Script source. |
| 306 | * @param string|false|null $plugins_path Plugins directory URL path. |
| 307 | * @return string |
| 308 | */ |
| 309 | private static function get_plugin_relative_source( $source, $plugins_path ): string { |
| 310 | if ( ! is_string( $source ) || '' === $source || ! is_string( $plugins_path ) || '' === $plugins_path ) { |
| 311 | return ''; |
| 312 | } |
| 313 | |
| 314 | $source_path = wp_parse_url( $source, PHP_URL_PATH ); |
| 315 | if ( ! is_string( $source_path ) || ! str_starts_with( $source_path, $plugins_path ) ) { |
| 316 | return ''; |
| 317 | } |
| 318 | |
| 319 | return ltrim( substr( $source_path, strlen( $plugins_path ) ), '/' ); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Get the source for a legacy Select2 handle. |
| 324 | * |
| 325 | * @param WP_Scripts $wp_scripts WordPress scripts registry. |
| 326 | * @param string $handle Script handle. |
| 327 | * @return string |
| 328 | */ |
| 329 | private static function get_legacy_handle_source( WP_Scripts $wp_scripts, string $handle ): string { |
| 330 | $source_handle = 'select2' === $handle ? 'wc-select2' : $handle; |
| 331 | $registered_script = $wp_scripts->registered[ $source_handle ] ?? null; |
| 332 | |
| 333 | if ( null === $registered_script ) { |
| 334 | return ''; |
| 335 | } |
| 336 | |
| 337 | return is_string( $registered_script->src ) ? $registered_script->src : ''; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Get the current admin screen ID. |
| 342 | * |
| 343 | * @return string |
| 344 | */ |
| 345 | private function get_current_screen_id(): string { |
| 346 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 347 | return ''; |
| 348 | } |
| 349 | |
| 350 | $screen = get_current_screen(); |
| 351 | return $screen ? (string) $screen->id : ''; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Get the current page type. |
| 356 | * |
| 357 | * @param string $context The request context. |
| 358 | * @return string |
| 359 | */ |
| 360 | private function get_current_page_type( string $context ): string { |
| 361 | if ( self::CONTEXT_ADMIN === $context ) { |
| 362 | return $this->get_current_screen_id(); |
| 363 | } |
| 364 | |
| 365 | if ( self::CONTEXT_FRONTEND === $context ) { |
| 366 | return $this->get_current_frontend_page_type(); |
| 367 | } |
| 368 | |
| 369 | return ''; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Get the current frontend page type. |
| 374 | * |
| 375 | * @return string |
| 376 | */ |
| 377 | private function get_current_frontend_page_type(): string { |
| 378 | if ( is_cart() ) { |
| 379 | return 'cart'; |
| 380 | } |
| 381 | |
| 382 | if ( is_checkout() ) { |
| 383 | return 'checkout'; |
| 384 | } |
| 385 | |
| 386 | if ( is_account_page() ) { |
| 387 | return 'my_account'; |
| 388 | } |
| 389 | |
| 390 | if ( is_shop() ) { |
| 391 | return 'shop'; |
| 392 | } |
| 393 | |
| 394 | if ( is_product() ) { |
| 395 | return 'product'; |
| 396 | } |
| 397 | |
| 398 | if ( is_product_category() ) { |
| 399 | return 'product_category'; |
| 400 | } |
| 401 | |
| 402 | if ( is_product_tag() ) { |
| 403 | return 'product_tag'; |
| 404 | } |
| 405 | |
| 406 | if ( is_product_taxonomy() ) { |
| 407 | return 'product_taxonomy'; |
| 408 | } |
| 409 | |
| 410 | if ( is_front_page() ) { |
| 411 | return 'front_page'; |
| 412 | } |
| 413 | |
| 414 | if ( is_home() ) { |
| 415 | return 'home'; |
| 416 | } |
| 417 | |
| 418 | if ( is_search() ) { |
| 419 | return 'search'; |
| 420 | } |
| 421 | |
| 422 | if ( is_archive() ) { |
| 423 | return 'archive'; |
| 424 | } |
| 425 | |
| 426 | if ( is_singular() ) { |
| 427 | return 'singular'; |
| 428 | } |
| 429 | |
| 430 | return 'other'; |
| 431 | } |
| 432 | } |
| 433 |