ClassicVariationGalleryAdmin.php
1 month ago
LegacyVariationGalleryCompatibility.php
1 month ago
Migration.php
1 month ago
Package.php
1 month ago
Telemetry.php
1 month ago
Telemetry.php
149 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Telemetry for the variation gallery feature. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\VariationGallery; |
| 9 | |
| 10 | use Automattic\WooCommerce\Internal\RegisterHooksInterface; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Tracker snapshot + Tracks events for the merged variation gallery feature. |
| 16 | */ |
| 17 | class Telemetry implements RegisterHooksInterface { |
| 18 | |
| 19 | public const EVENT_SAVE_SUCCEEDED = 'variation_gallery_save_succeeded'; |
| 20 | public const EVENT_SAVE_FAILED = 'variation_gallery_save_failed'; |
| 21 | public const EVENT_MIGRATION_COMPLETED = 'variation_gallery_migration_completed'; |
| 22 | |
| 23 | private const LEGACY_PLUGIN_FILE = 'woocommerce-additional-variation-images/woocommerce-additional-variation-images.php'; |
| 24 | |
| 25 | /** |
| 26 | * Register the tracker snapshot filter. |
| 27 | * |
| 28 | * @return void |
| 29 | */ |
| 30 | public function register() { |
| 31 | add_filter( 'woocommerce_tracker_data', array( $this, 'add_snapshot_to_tracker_data' ), 10, 1 ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Append the variation gallery snapshot fields to WC_Tracker's payload. |
| 36 | * |
| 37 | * @param array $data The aggregated tracker data. |
| 38 | * @return array |
| 39 | */ |
| 40 | public function add_snapshot_to_tracker_data( array $data ): array { |
| 41 | $data['variation_gallery'] = self::collect_snapshot(); |
| 42 | return $data; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Collect the variation gallery snapshot fields. |
| 47 | * |
| 48 | * @return array<string, mixed> |
| 49 | */ |
| 50 | public static function collect_snapshot(): array { |
| 51 | global $wpdb; |
| 52 | |
| 53 | $option_value = get_option( Package::ENABLE_OPTION_NAME, '' ); |
| 54 | $variant_assignment = (int) get_option( 'woocommerce_remote_variant_assignment', 0 ); |
| 55 | $cohort = ( $variant_assignment > 0 && $variant_assignment <= 5 ) ? 'treatment' : 'control'; |
| 56 | $legacy_plugin_active = self::is_legacy_plugin_active(); |
| 57 | $legacy_plugin_file = WP_PLUGIN_DIR . '/' . self::LEGACY_PLUGIN_FILE; |
| 58 | |
| 59 | $migrated_variation_count = (int) $wpdb->get_var( |
| 60 | $wpdb->prepare( |
| 61 | "SELECT COUNT(post_id) FROM {$wpdb->postmeta} WHERE meta_key = %s", |
| 62 | LegacyVariationGalleryCompatibility::get_core_managed_meta_key() |
| 63 | ) |
| 64 | ); |
| 65 | |
| 66 | $variation_gallery_rows = $wpdb->get_col( |
| 67 | "SELECT pm.meta_value |
| 68 | FROM {$wpdb->postmeta} pm |
| 69 | INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id |
| 70 | WHERE pm.meta_key = '_product_image_gallery' |
| 71 | AND pm.meta_value <> '' |
| 72 | AND p.post_type = 'product_variation'" |
| 73 | ); |
| 74 | |
| 75 | $authored_variation_count = 0; |
| 76 | $total_image_count = 0; |
| 77 | $single_image_variation_count = 0; |
| 78 | $multi_image_variation_count = 0; |
| 79 | |
| 80 | foreach ( $variation_gallery_rows as $meta_value ) { |
| 81 | $image_ids = wp_parse_id_list( $meta_value ); |
| 82 | $count = count( $image_ids ); |
| 83 | if ( 0 === $count ) { |
| 84 | continue; |
| 85 | } |
| 86 | ++$authored_variation_count; |
| 87 | $total_image_count += $count; |
| 88 | if ( 1 === $count ) { |
| 89 | ++$single_image_variation_count; |
| 90 | } else { |
| 91 | ++$multi_image_variation_count; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | $authored_without_legacy_count = (int) $wpdb->get_var( |
| 96 | "SELECT COUNT(*) FROM {$wpdb->postmeta} core_pm |
| 97 | INNER JOIN {$wpdb->posts} p ON p.ID = core_pm.post_id |
| 98 | LEFT JOIN {$wpdb->postmeta} legacy_pm |
| 99 | ON legacy_pm.post_id = core_pm.post_id |
| 100 | AND legacy_pm.meta_key = '_wc_additional_variation_images' |
| 101 | WHERE core_pm.meta_key = '_product_image_gallery' |
| 102 | AND core_pm.meta_value <> '' |
| 103 | AND p.post_type = 'product_variation' |
| 104 | AND legacy_pm.post_id IS NULL" |
| 105 | ); |
| 106 | |
| 107 | return array( |
| 108 | 'feature_enabled' => 'yes' === $option_value ? 'yes' : 'no', |
| 109 | 'feature_option_explicit' => '' === $option_value ? 'no' : 'yes', |
| 110 | 'remote_variant_cohort' => $cohort, |
| 111 | 'legacy_avi_plugin_active' => $legacy_plugin_active ? 'yes' : 'no', |
| 112 | 'legacy_avi_plugin_installed' => file_exists( $legacy_plugin_file ) ? 'yes' : 'no', |
| 113 | 'migrated_variation_count' => $migrated_variation_count, |
| 114 | 'authored_variation_count' => $authored_variation_count, |
| 115 | 'authored_without_legacy_count' => $authored_without_legacy_count, |
| 116 | 'total_image_count' => $total_image_count, |
| 117 | 'single_image_variation_count' => $single_image_variation_count, |
| 118 | 'multi_image_variation_count' => $multi_image_variation_count, |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Record a Tracks event. |
| 124 | * |
| 125 | * @param string $event_name One of the `EVENT_*` class constants. |
| 126 | * @param array<string, mixed> $properties Event properties to attach. |
| 127 | * @return void |
| 128 | */ |
| 129 | public static function record_event( string $event_name, array $properties = array() ): void { |
| 130 | if ( ! function_exists( 'wc_admin_record_tracks_event' ) ) { |
| 131 | return; |
| 132 | } |
| 133 | wc_admin_record_tracks_event( $event_name, $properties ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Whether the legacy Additional Variation Images extension is currently |
| 138 | * active. |
| 139 | * |
| 140 | * @return bool |
| 141 | */ |
| 142 | private static function is_legacy_plugin_active(): bool { |
| 143 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 144 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 145 | } |
| 146 | return is_plugin_active( self::LEGACY_PLUGIN_FILE ); |
| 147 | } |
| 148 | } |
| 149 |