PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.2.11
LatePoint – Calendar Booking Plugin for Appointments and Events v5.2.11
5.6.7 5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / kit / bsf-analytics / modules / utm-analytics.php
latepoint / lib / kit / bsf-analytics / modules Last commit date
deactivation-survey 4 months ago utm-analytics.php 4 months ago
utm-analytics.php
168 lines
1 <?php
2 /**
3 * UTM Analytics class
4 *
5 * @package bsf-analytics
6 */
7
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly.
11 }
12
13 if ( ! class_exists( 'BSF_UTM_Analytics' ) ) {
14
15
16 if ( ! defined( 'BSF_UTM_ANALYTICS_REFERER' ) ) {
17 define( 'BSF_UTM_ANALYTICS_REFERER', 'bsf_product_referers' );
18 }
19
20 /**
21 * UTM Analytics class
22 *
23 * @since 1.1.10
24 */
25 class BSF_UTM_Analytics {
26
27 /**
28 * List of slugs of all the bsf products that will be referer, referring another product.
29 *
30 * @var array<string>
31 * @since 1.1.10
32 */
33 private static $bsf_product_slugs = [
34 'all-in-one-schemaorg-rich-snippets',
35 'astra',
36 'astra-portfolio',
37 'astra-sites',
38 'bb-ultimate-addon',
39 'cartflows',
40 'checkout-paypal-woo',
41 'checkout-plugins-stripe-woo',
42 'convertpro',
43 'header-footer-elementor',
44 'latepoint',
45 'modern-cart',
46 'presto-player',
47 'surecart',
48 'sureforms',
49 'suremails',
50 'surerank',
51 'suretriggers',
52 'ultimate-addons-for-beaver-builder-lite',
53 'ultimate-addons-for-gutenberg',
54 'ultimate-elementor',
55 'Ultimate_VC_Addons',
56 'variation-swatches-woo',
57 'woo-cart-abandonment-recovery',
58 'wp-schema-pro',
59 'zipwp'
60 ];
61
62
63 /**
64 * This function will help to determine if provided slug is a valid bsf product or not,
65 * This way we will maintain consistency through out all our products.
66 *
67 * @param string $slug unique slug of the product which can be used for referer, product.
68 * @since 1.1.10
69 * @return boolean
70 */
71 public static function is_valid_bsf_product_slug( $slug ) {
72 if ( empty( $slug ) || ! is_string( $slug ) ) {
73 return false;
74 }
75
76 return in_array( $slug, self::$bsf_product_slugs, true );
77 }
78
79 /**
80 * This function updates value of referer and product in option
81 * bsf_product_referer in form of key value pair as 'product' => 'referer'
82 *
83 * @param string $referer slug of the product which is refering another product.
84 * @param string $product slug of the product which is refered.
85 * @since 1.1.10
86 * @return void
87 */
88 public static function update_referer( $referer, $product ) {
89
90 $slugs = [
91 'referer' => $referer,
92 'product' => $product,
93 ];
94 $error_count = 0;
95
96 foreach ( $slugs as $type => $slug ) {
97 if ( ! self::is_valid_bsf_product_slug( $slug ) ) {
98 error_log( sprintf( 'Invalid %1$s slug provided "%2$s", does not match bsf_product_slugs', $type, $slug ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- adding logs in case of failure will help in debugging.
99 $error_count++;
100 }
101 }
102
103 if ( $error_count > 0 ) {
104 return;
105 }
106
107 $slugs = array_map( 'sanitize_text_field', $slugs );
108
109 $bsf_product_referers = get_option( BSF_UTM_ANALYTICS_REFERER, [] );
110 if ( ! is_array( $bsf_product_referers ) ) {
111 $bsf_product_referers = [];
112 }
113
114 $bsf_product_referers[ $slugs['product'] ] = $slugs['referer'];
115
116 update_option( BSF_UTM_ANALYTICS_REFERER, $bsf_product_referers );
117 }
118
119 /**
120 * This function will add utm_args to pro link or purchase link
121 * added utm_source by default additional utm_args such as utm_medium etc can be provided to generate location specific links
122 *
123 * @param string $link Ideally this should be product site link where utm_params can be tracked.
124 * @param string $product Product slug whose utm_link need to be created.
125 * @param mixed $utm_args additional args to be passed ex: [ 'utm_medium' => 'dashboard'].
126 * @since 1.1.10
127 * @return string
128 */
129 public static function get_utm_ready_link( $link, $product, $utm_args = [] ) {
130
131 if ( false === wp_http_validate_url( $link ) ) {
132 error_log( 'Invalid url passed to get_utm_ready_link function' ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- adding logs in case of failure will help in debugging.
133 return $link;
134 }
135
136 if ( empty( $product ) || ! is_string( $product ) || ! self::is_valid_bsf_product_slug( $product ) ) {
137 error_log( sprintf( 'Invalid product slug provided "%1$s", does not match bsf_product_slugs', $product ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- adding logs in case of failure will help in debugging.
138 return $link;
139 }
140
141 $bsf_product_referers = get_option( BSF_UTM_ANALYTICS_REFERER, [] );
142
143 if ( ! is_array( $bsf_product_referers ) || empty( $bsf_product_referers[ $product ] ) ) {
144 return $link;
145 }
146
147 if ( ! self::is_valid_bsf_product_slug( $bsf_product_referers[ $product ] ) ) {
148 return $link;
149 }
150
151 if ( ! is_array( $utm_args ) ) {
152 $utm_args = [];
153 }
154
155 $utm_args['utm_source'] = $bsf_product_referers[ $product ];
156
157 $link = add_query_arg(
158 $utm_args,
159 $link
160 );
161
162 return $link;
163 }
164 }
165 }
166
167
168