PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 1.0.0
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v1.0.0
1.6.0 1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
suredonation / inc / lib / bsf-analytics / modules / utm-analytics.php

utm-analytics.php in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management 1.0.0, at inc/lib/bsf-analytics/modules/utm-analytics.php

169 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'surecart',
49 'surecontact',
50 'surecookie',
51 'suredash',
52 'sureforms',
53 'suremails',
54 'surerank',
55 'suretriggers',
56 'ultimate-addons-for-beaver-builder-lite',
57 'ultimate-addons-for-gutenberg',
58 'ultimate-elementor',
59 'Ultimate_VC_Addons',
60 'variation-swatches-woo',
61 'woo-cart-abandonment-recovery',
62 'wp-schema-pro',
63 'zipwp'
64 ];
65
66 /**
67 * This function will help to determine if provided slug is a valid bsf product or not,
68 * This way we will maintain consistency through out all our products.
69 *
70 * @param string $slug unique slug of the product which can be used for referer, product.
71 * @since 1.1.10
72 * @return boolean
73 */
74 public static function is_valid_bsf_product_slug( $slug ) {
75 if ( empty( $slug ) || ! is_string( $slug ) ) {
76 return false;
77 }
78
79 return in_array( $slug, self::$bsf_product_slugs, true );
80 }
81
82 /**
83 * This function updates value of referer and product in option
84 * bsf_product_referer in form of key value pair as 'product' => 'referer'
85 *
86 * @param string $referer slug of the product which is refering another product.
87 * @param string $product slug of the product which is refered.
88 * @since 1.1.10
89 * @return void
90 */
91 public static function update_referer( $referer, $product ) {
92
93 $slugs = [
94 'referer' => $referer,
95 'product' => $product,
96 ];
97 $error_count = 0;
98
99 foreach ( $slugs as $type => $slug ) {
100 if ( ! self::is_valid_bsf_product_slug( $slug ) ) {
101 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.
102 $error_count++;
103 }
104 }
105
106 if ( $error_count > 0 ) {
107 return;
108 }
109
110 $slugs = array_map( 'sanitize_text_field', $slugs );
111
112 $bsf_product_referers = get_option( BSF_UTM_ANALYTICS_REFERER, [] );
113 if ( ! is_array( $bsf_product_referers ) ) {
114 $bsf_product_referers = [];
115 }
116
117 $bsf_product_referers[ $slugs['product'] ] = $slugs['referer'];
118
119 update_option( BSF_UTM_ANALYTICS_REFERER, $bsf_product_referers );
120 }
121
122 /**
123 * This function will add utm_args to pro link or purchase link
124 * added utm_source by default additional utm_args such as utm_medium etc can be provided to generate location specific links
125 *
126 * @param string $link Ideally this should be product site link where utm_params can be tracked.
127 * @param string $product Product slug whose utm_link need to be created.
128 * @param mixed $utm_args additional args to be passed ex: [ 'utm_medium' => 'dashboard'].
129 * @since 1.1.10
130 * @return string
131 */
132 public static function get_utm_ready_link( $link, $product, $utm_args = [] ) {
133
134 if ( false === wp_http_validate_url( $link ) ) {
135 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.
136 return $link;
137 }
138
139 if ( empty( $product ) || ! is_string( $product ) || ! self::is_valid_bsf_product_slug( $product ) ) {
140 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.
141 return $link;
142 }
143
144 $bsf_product_referers = get_option( BSF_UTM_ANALYTICS_REFERER, [] );
145
146 if ( ! is_array( $bsf_product_referers ) || empty( $bsf_product_referers[ $product ] ) ) {
147 return $link;
148 }
149
150 if ( ! self::is_valid_bsf_product_slug( $bsf_product_referers[ $product ] ) ) {
151 return $link;
152 }
153
154 if ( ! is_array( $utm_args ) ) {
155 $utm_args = [];
156 }
157
158 $utm_args['utm_source'] = $bsf_product_referers[ $product ];
159
160 $link = add_query_arg(
161 $utm_args,
162 $link
163 );
164
165 return $link;
166 }
167 }
168 }
169