/var/www/html/wp-content/plugins/header-footer-elementor/inc/lib/utm-analytics/inc/utils.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
 * Utils class
 *
 * @package bsf-utm-analytics
 */

namespace BSF_UTM_Analytics\Inc;

if ( ! 
defined'ABSPATH' ) ) {
    exit; 
// Exit if accessed directly.
}

/**
 * Utils class
 *
 * @since 0.0.1
 */
class Utils {

    
/**
     * List of slugs of all the bsf products that will be referer, referring another product.
     *
     * @var array<string>
     * @since 0.0.1
     */
    
private static $bsf_product_slugs = [
        
'all-in-one-schemaorg-rich-snippets',
        
'astra',
        
'astra-portfolio',
        
'astra-sites',
        
'bb-ultimate-addon',
        
'cartflows',
        
'checkout-paypal-woo',
        
'checkout-plugins-stripe-woo',
        
'convertpro',
        
'header-footer-elementor',
        
'presto-player',
        
'project-huddle',
        
'surecart',
        
'sureforms',
        
'suremails',
        
'surerank',
        
'suretriggers',
        
'ultimate-addons-for-beaver-builder-lite',
        
'ultimate-addons-for-gutenberg',
        
'ultimate-elementor',
        
'variation-swatches-woo',
        
'woo-cart-abandonment-recovery',
        
'wp-schema-pro',
        
'zipwp',
    ];


    
/**
     * This function will help to determine if provided slug is a valid bsf product or not,
     * This way we will maintain consistency through out all our products.
     *
     * @param string $slug unique slug of the product which can be used for referer, product.
     * @since 0.0.1
     * @return boolean
     */
    
public static function is_valid_bsf_product_slug$slug ) {
        if ( empty( 
$slug ) || ! is_string$slug ) ) {
            return 
false;
        }

        return 
in_array$slugself::$bsf_product_slugstrue );
    }

    
/**
     * This function updates value of referer and product in option
     * bsf_product_referer in form of key value pair as 'product' => 'referer'
     *
     * @param string $referer slug of the product which is refering another product.
     * @param string $product slug of the product which is refered.
     * @since 0.0.1
     * @return void
     */
    
public static function update_referer$referer$product ) {

        
$slugs       = [
            
'referer' => $referer,
            
'product' => $product,
        ];
        
$error_count 0;

        foreach ( 
$slugs as $type => $slug ) {
            if ( ! 
self::is_valid_bsf_product_slug$slug ) ) {
                
error_logsprintf'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.
                
$error_count++;
            }
        }

        if ( 
$error_count ) {
            return;
        }

        
$slugs array_map'sanitize_text_field'$slugs );

        
$bsf_product_referers get_optionBSF_UTM_ANALYTICS_REFERER_OPTION, [] );
        if ( ! 
is_array$bsf_product_referers ) ) {
            
$bsf_product_referers = [];
        }

        
$bsf_product_referers$slugs['product'] ] = $slugs['referer'];

        
update_optionBSF_UTM_ANALYTICS_REFERER_OPTION$bsf_product_referers );
    }

    
/**
     * This function will  add utm_args to pro link or purchase link
     * added utm_source by default additional utm_args such as utm_medium etc can be provided to generate location specific links
     *
     * @param string $link Ideally this should be product site link where utm_params can be tracked.
     * @param string $product Product slug whose utm_link need to be created.
     * @param mixed  $utm_args additional args to be passed ex: [ 'utm_medium' => 'dashboard'].
     * @since 0.0.1
     * @return string
     */
    
public static function get_utm_ready_link$link$product$utm_args = [] ) {

        if ( 
false === wp_http_validate_url$link ) ) {
            
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.
            
return $link;
        }

        if ( empty( 
$product ) || ! is_string$product ) || ! self::is_valid_bsf_product_slug$product ) ) {
            
error_logsprintf'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.
            
return $link;
        }

        
$bsf_product_referers get_optionBSF_UTM_ANALYTICS_REFERER_OPTION, [] );

        if ( ! 
is_array$bsf_product_referers ) || empty( $bsf_product_referers$product ] ) ) {
            return 
$link;
        }

        if ( ! 
self::is_valid_bsf_product_slug$bsf_product_referers$product ] ) ) {
            return 
$link;
        }

        if ( ! 
is_array$utm_args ) ) {
            
$utm_args = [];
        }

        
$utm_args['utm_source'] = $bsf_product_referers$product ];

        
$link add_query_arg(
            
$utm_args,
            
$link
        
);

        return 
$link;
    }
}