PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 1.6.0
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v1.6.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.6.0, at inc/lib/bsf-analytics/modules/utm-analytics.php

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