PluginProbe ʕ •ᴥ•ʔ
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.12.1
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.12.1
2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8 0.0.9 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.1.2 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.12.3 1.13.0 1.13.1 1.13.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.8.0 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.2 2.6.0
sureforms / inc / lib / bsf-analytics / modules / utm-analytics.php
sureforms / inc / lib / bsf-analytics / modules Last commit date
deactivation-survey 1 month ago utm-analytics.php 2 weeks ago
utm-analytics.php
171 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 if ( ! defined( 'BSF_UTM_ANALYTICS_REFERER' ) ) {
16 define( 'BSF_UTM_ANALYTICS_REFERER', 'bsf_product_referers' );
17 }
18
19 /**
20 * UTM Analytics class
21 *
22 * @since 1.1.10
23 */
24 class BSF_UTM_Analytics {
25
26 /**
27 * List of slugs of all the bsf products that will be referer, referring another product.
28 *
29 * @var array<string>
30 * @since 1.1.10
31 */
32 private static $bsf_product_slugs = [
33 'all-in-one-schemaorg-rich-snippets',
34 'astra',
35 'astra-portfolio',
36 'astra-sites',
37 'bb-ultimate-addon',
38 'cartflows',
39 'checkout-paypal-woo',
40 'checkout-plugins-stripe-woo',
41 'convertpro',
42 'header-footer-elementor',
43 'latepoint',
44 'modern-cart',
45 'power-coupons',
46 'presto-player',
47 'sigmize',
48 'spectra-blocks',
49 'surecart',
50 'surecontact',
51 'surecookie',
52 'suredash',
53 'suredonation',
54 'sureforms',
55 'suremails',
56 'surerank',
57 'suretriggers',
58 'ultimate-addons-for-beaver-builder-lite',
59 'ultimate-addons-for-gutenberg',
60 'ultimate-elementor',
61 'Ultimate_VC_Addons',
62 'variation-swatches-woo',
63 'woo-cart-abandonment-recovery',
64 'wp-schema-pro',
65 'zipwp'
66 ];
67
68 /**
69 * This function will help to determine if provided slug is a valid bsf product or not,
70 * This way we will maintain consistency through out all our products.
71 *
72 * @param string $slug unique slug of the product which can be used for referer, product.
73 * @since 1.1.10
74 * @return boolean
75 */
76 public static function is_valid_bsf_product_slug( $slug ) {
77 if ( empty( $slug ) || ! is_string( $slug ) ) {
78 return false;
79 }
80
81 return in_array( $slug, self::$bsf_product_slugs, true );
82 }
83
84 /**
85 * This function updates value of referer and product in option
86 * bsf_product_referer in form of key value pair as 'product' => 'referer'
87 *
88 * @param string $referer slug of the product which is refering another product.
89 * @param string $product slug of the product which is refered.
90 * @since 1.1.10
91 * @return void
92 */
93 public static function update_referer( $referer, $product ) {
94
95 $slugs = [
96 'referer' => $referer,
97 'product' => $product,
98 ];
99 $error_count = 0;
100
101 foreach ( $slugs as $type => $slug ) {
102 if ( ! self::is_valid_bsf_product_slug( $slug ) ) {
103 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.
104 $error_count++;
105 }
106 }
107
108 if ( $error_count > 0 ) {
109 return;
110 }
111
112 $slugs = array_map( 'sanitize_text_field', $slugs );
113
114 $bsf_product_referers = get_option( BSF_UTM_ANALYTICS_REFERER, [] );
115 if ( ! is_array( $bsf_product_referers ) ) {
116 $bsf_product_referers = [];
117 }
118
119 $bsf_product_referers[ $slugs['product'] ] = $slugs['referer'];
120
121 update_option( BSF_UTM_ANALYTICS_REFERER, $bsf_product_referers );
122 }
123
124 /**
125 * This function will add utm_args to pro link or purchase link
126 * added utm_source by default additional utm_args such as utm_medium etc can be provided to generate location specific links
127 *
128 * @param string $link Ideally this should be product site link where utm_params can be tracked.
129 * @param string $product Product slug whose utm_link need to be created.
130 * @param mixed $utm_args additional args to be passed ex: [ 'utm_medium' => 'dashboard'].
131 * @since 1.1.10
132 * @return string
133 */
134 public static function get_utm_ready_link( $link, $product, $utm_args = [] ) {
135
136 if ( false === wp_http_validate_url( $link ) ) {
137 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.
138 return $link;
139 }
140
141 if ( empty( $product ) || ! is_string( $product ) || ! self::is_valid_bsf_product_slug( $product ) ) {
142 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.
143 return $link;
144 }
145
146 $bsf_product_referers = get_option( BSF_UTM_ANALYTICS_REFERER, [] );
147
148 if ( ! is_array( $bsf_product_referers ) || empty( $bsf_product_referers[ $product ] ) ) {
149 return $link;
150 }
151
152 if ( ! self::is_valid_bsf_product_slug( $bsf_product_referers[ $product ] ) ) {
153 return $link;
154 }
155
156 if ( ! is_array( $utm_args ) ) {
157 $utm_args = [];
158 }
159
160 $utm_args['utm_source'] = $bsf_product_referers[ $product ];
161
162 $link = add_query_arg(
163 $utm_args,
164 $link
165 );
166
167 return $link;
168 }
169 }
170 }
171