PluginProbe
Blocks – Reusable Content, Shortcodes & Site Variables / trunk
Blocks – Reusable Content, Shortcodes & Site Variables vtrunk
26.09.03.15 26.08.31.21 26.08.22.20 26.08.22.22 26.08.22.17 26.08.22.13 26.08.21.19 26.08.07.23 26.07.19.14 26.07.13.21 26.07.13.17 26.07.12.13 026.07.07.21 026.07.05.18 026.06.26.20 026.06.26.21 026.06.08.20 026.05.13.14 026.04.29.10 trunk 026.02.22.22 026.03.16.23 026.04.23.13
blocks / src / Analytics / LegacyAnalyticsMigration.php

LegacyAnalyticsMigration.php in Blocks – Reusable Content, Shortcodes & Site Variables trunk, at src/Analytics/LegacyAnalyticsMigration.php

117 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace RenzoJohnson\Blocks\Analytics;
6
7 \defined( 'ABSPATH' ) || exit;
8
9 final class LegacyAnalyticsMigration {
10 public const SOURCE_OPTION = 'woocommerce_google_analytics_settings';
11 public const MARKER_OPTION = 'blocks_ga4_legacy_settings_migrated';
12
13 private const MISSING = '__blocks_missing__';
14
15 /**
16 * @var array<string, string>
17 */
18 private const BOOLEAN_OPTIONS = array(
19 'ga_support_display_advertising' => 'blocks_ga4_display_advertising_enabled',
20 'ga_404_tracking_enabled' => 'blocks_ga4_404_enabled',
21 'ga_linker_allow_incoming_enabled' => 'blocks_ga4_linker_allow_incoming_enabled',
22 'ga_ecommerce_tracking_enabled' => 'blocks_ga4_purchase_enabled',
23 'ga_event_tracking_enabled' => 'blocks_ga4_add_to_cart_enabled',
24 'ga_enhanced_remove_from_cart_enabled' => 'blocks_ga4_remove_from_cart_enabled',
25 'ga_enhanced_product_impression_enabled' => 'blocks_ga4_view_item_list_enabled',
26 'ga_enhanced_product_click_enabled' => 'blocks_ga4_select_content_enabled',
27 'ga_enhanced_product_detail_view_enabled' => 'blocks_ga4_view_item_enabled',
28 'ga_enhanced_checkout_process_enabled' => 'blocks_ga4_begin_checkout_enabled',
29 );
30
31 public function migrate(): void {
32 if ( self::MISSING !== \get_option( self::MARKER_OPTION, self::MISSING ) ) {
33 return;
34 }
35
36 $source = \get_option( self::SOURCE_OPTION, array() );
37 if ( ! \is_array( $source ) || array() === $source ) {
38 $this->mark_complete();
39 return;
40 }
41
42 $measurement_id = $this->measurement_id( $source['ga_id'] ?? '' );
43 $this->add_missing( 'blocks_ga4_measurement_id', $measurement_id );
44
45 if ( '' !== $measurement_id && $this->is_missing( 'blocks_ga4_ecommerce_enabled' ) ) {
46 \add_option( 'blocks_ga4_ecommerce_enabled', 1, '', false );
47 }
48
49 $identifier = \is_scalar( $source['ga_product_identifier'] ?? null )
50 ? \sanitize_text_field( (string) $source['ga_product_identifier'] )
51 : '';
52 $this->add_missing(
53 'blocks_ga4_product_identifier',
54 \in_array( $identifier, array( 'product_id', 'product_sku' ), true ) ? $identifier : ''
55 );
56
57 foreach ( self::BOOLEAN_OPTIONS as $source_key => $target_option ) {
58 if ( \array_key_exists( $source_key, $source ) ) {
59 $this->add_missing( $target_option, 'yes' === $source[ $source_key ] ? 1 : 0 );
60 }
61 }
62
63 if ( \array_key_exists( 'ga_linker_cross_domains', $source ) ) {
64 $this->add_missing( 'blocks_ga4_cross_domains', $this->domain_list( $source['ga_linker_cross_domains'] ) );
65 }
66
67 $this->mark_complete();
68 }
69
70 public function mark_fresh_install(): void {
71 $this->mark_complete();
72 }
73
74 private function add_missing( string $option_name, bool|int|string $value ): void {
75 if ( '' !== $value && $this->is_missing( $option_name ) ) {
76 \add_option( $option_name, $value, '', false );
77 }
78 }
79
80 private function is_missing( string $option_name ): bool {
81 return self::MISSING === \get_option( $option_name, self::MISSING );
82 }
83
84 private function mark_complete(): void {
85 \add_option( self::MARKER_OPTION, 1, '', false );
86 }
87
88 private function measurement_id( mixed $value ): string {
89 $measurement_id = \is_scalar( $value )
90 ? \strtoupper( \trim( \sanitize_text_field( (string) $value ) ) )
91 : '';
92
93 return 1 === \preg_match( '/^G-[A-Z0-9]+$/', $measurement_id ) ? $measurement_id : '';
94 }
95
96 private function domain_list( mixed $value ): string {
97 if ( ! \is_scalar( $value ) ) {
98 return '';
99 }
100
101 $domains = array();
102 foreach ( \explode( ',', (string) $value ) as $candidate ) {
103 $candidate = \strtolower( \trim( \sanitize_text_field( $candidate ) ) );
104 if ( '' === $candidate ) {
105 continue;
106 }
107
108 $host = \wp_parse_url( \str_contains( $candidate, '://' ) ? $candidate : 'https://' . $candidate, PHP_URL_HOST );
109 if ( \is_string( $host ) && 1 === \preg_match( '/^([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/', $host ) ) {
110 $domains[] = $host;
111 }
112 }
113
114 return \implode( ', ', \array_values( \array_unique( $domains ) ) );
115 }
116 }
117