| @@ -10,9 +10,8 @@ | ||
| 10 | 10 | exit; // Exit if accessed directly. |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | if ( ! class_exists( 'BSF_UTM_Analytics' ) ) { |
| 14 | - | |
| 15 | 14 | |
| 16 | 15 | if ( ! defined( 'BSF_UTM_ANALYTICS_REFERER' ) ) { |
| 17 | 16 | define( 'BSF_UTM_ANALYTICS_REFERER', 'bsf_product_referers' ); |
| 18 | 17 | } |
| @@ -42,10 +41,17 @@ | ||
| 42 | 41 | 'convertpro', |
| 43 | 42 | 'header-footer-elementor', |
| 44 | 43 | 'latepoint', |
| 45 | 44 | 'modern-cart', |
| 45 | + 'power-coupons', | |
| 46 | 46 | 'presto-player', |
| 47 | + 'sigmize', | |
| 48 | + 'spectra-blocks', | |
| 47 | 49 | 'surecart', |
| 50 | + 'surecontact', | |
| 51 | + 'surecookie', | |
| 52 | + 'suredash', | |
| 53 | + 'suredonation', | |
| 48 | 54 | 'sureforms', |
| 49 | 55 | 'suremails', |
| 50 | 56 | 'surerank', |
| 51 | 57 | 'suretriggers', |
| @@ -57,10 +63,9 @@ | ||
| 57 | 63 | 'woo-cart-abandonment-recovery', |
| 58 | 64 | 'wp-schema-pro', |
| 59 | 65 | 'zipwp' |
| 60 | 66 | ]; |
| 61 | - | |
| 62 | - | |
| 67 | + | |
| 63 | 68 | /** |
| 64 | 69 | * This function will help to determine if provided slug is a valid bsf product or not, |
| 65 | 70 | * This way we will maintain consistency through out all our products. |
| 66 | 71 | * |
| @@ -71,12 +76,12 @@ | ||
| 71 | 76 | public static function is_valid_bsf_product_slug( $slug ) { |
| 72 | 77 | if ( empty( $slug ) || ! is_string( $slug ) ) { |
| 73 | 78 | return false; |
| 74 | 79 | } |
| 75 | - | |
| 80 | + | |
| 76 | 81 | return in_array( $slug, self::$bsf_product_slugs, true ); |
| 77 | 82 | } |
| 78 | - | |
| 83 | + | |
| 79 | 84 | /** |
| 80 | 85 | * This function updates value of referer and product in option |
| 81 | 86 | * bsf_product_referer in form of key value pair as 'product' => 'referer' |
| 82 | 87 | * |
| @@ -85,15 +90,15 @@ | ||
| 85 | 90 | * @since 1.1.10 |
| 86 | 91 | * @return void |
| 87 | 92 | */ |
| 88 | 93 | public static function update_referer( $referer, $product ) { |
| 89 | - | |
| 94 | + | |
| 90 | 95 | $slugs = [ |
| 91 | 96 | 'referer' => $referer, |
| 92 | 97 | 'product' => $product, |
| 93 | 98 | ]; |
| 94 | 99 | $error_count = 0; |
| 95 | - | |
| 100 | + | |
| 96 | 101 | foreach ( $slugs as $type => $slug ) { |
| 97 | 102 | if ( ! self::is_valid_bsf_product_slug( $slug ) ) { |
| 98 | 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. |
| 99 | 104 | $error_count++; |
| @@ -98,25 +103,25 @@ | ||
| 98 | 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. |
| 99 | 104 | $error_count++; |
| 100 | 105 | } |
| 101 | 106 | } |
| 102 | - | |
| 107 | + | |
| 103 | 108 | if ( $error_count > 0 ) { |
| 104 | 109 | return; |
| 105 | 110 | } |
| 106 | - | |
| 111 | + | |
| 107 | 112 | $slugs = array_map( 'sanitize_text_field', $slugs ); |
| 108 | - | |
| 113 | + | |
| 109 | 114 | $bsf_product_referers = get_option( BSF_UTM_ANALYTICS_REFERER, [] ); |
| 110 | 115 | if ( ! is_array( $bsf_product_referers ) ) { |
| 111 | 116 | $bsf_product_referers = []; |
| 112 | 117 | } |
| 113 | - | |
| 118 | + | |
| 114 | 119 | $bsf_product_referers[ $slugs['product'] ] = $slugs['referer']; |
| 115 | - | |
| 120 | + | |
| 116 | 121 | update_option( BSF_UTM_ANALYTICS_REFERER, $bsf_product_referers ); |
| 117 | 122 | } |
| 118 | - | |
| 123 | + | |
| 119 | 124 | /** |
| 120 | 125 | * This function will add utm_args to pro link or purchase link |
| 121 | 126 | * added utm_source by default additional utm_args such as utm_medium etc can be provided to generate location specific links |
| 122 | 127 | * |
| @@ -126,42 +131,40 @@ | ||
| 126 | 131 | * @since 1.1.10 |
| 127 | 132 | * @return string |
| 128 | 133 | */ |
| 129 | 134 | public static function get_utm_ready_link( $link, $product, $utm_args = [] ) { |
| 130 | - | |
| 135 | + | |
| 131 | 136 | if ( false === wp_http_validate_url( $link ) ) { |
| 132 | 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. |
| 133 | 138 | return $link; |
| 134 | 139 | } |
| 135 | - | |
| 140 | + | |
| 136 | 141 | if ( empty( $product ) || ! is_string( $product ) || ! self::is_valid_bsf_product_slug( $product ) ) { |
| 137 | 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. |
| 138 | 143 | return $link; |
| 139 | 144 | } |
| 140 | - | |
| 145 | + | |
| 141 | 146 | $bsf_product_referers = get_option( BSF_UTM_ANALYTICS_REFERER, [] ); |
| 142 | - | |
| 147 | + | |
| 143 | 148 | if ( ! is_array( $bsf_product_referers ) || empty( $bsf_product_referers[ $product ] ) ) { |
| 144 | 149 | return $link; |
| 145 | 150 | } |
| 146 | - | |
| 151 | + | |
| 147 | 152 | if ( ! self::is_valid_bsf_product_slug( $bsf_product_referers[ $product ] ) ) { |
| 148 | 153 | return $link; |
| 149 | 154 | } |
| 150 | - | |
| 155 | + | |
| 151 | 156 | if ( ! is_array( $utm_args ) ) { |
| 152 | 157 | $utm_args = []; |
| 153 | 158 | } |
| 154 | - | |
| 159 | + | |
| 155 | 160 | $utm_args['utm_source'] = $bsf_product_referers[ $product ]; |
| 156 | - | |
| 161 | + | |
| 157 | 162 | $link = add_query_arg( |
| 158 | 163 | $utm_args, |
| 159 | 164 | $link |
| 160 | 165 | ); |
| 161 | - | |
| 166 | + | |
| 162 | 167 | return $link; |
| 163 | 168 | } |
| 164 | 169 | } |
| 165 | 170 | } |
| 166 | - | |
| 167 | - | |