PluginProbe
Affiliate Sales in Google Analytics and other tools / 5.4.0
Affiliate Sales in Google Analytics and other tools v5.4.0
5.4.1 5.5.0 5.4.0 5.3.0 5.1.0 5.1.1 4.0.1 4.0.2 5.0.0 5.0.1 5.0.2 trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 All 55 releases
wecantrack / wecantrack.php

wecantrack.php in Affiliate Sales in Google Analytics and other tools 5.4.0, at wecantrack.php

349 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: WeCanTrack
4 * Plugin URI: https://wecantrack.com/wordpress
5 * Description: Integrate all your affiliate sales into Google Analytics, Google Ads, Facebook, Data Studio, and more!
6 * Version: 5.4.0
7 * Author: WeCanTrack
8 * Author URI: https://wecantrack.com
9 * Requires PHP: 7.4
10 * Requires at least: 5.0
11 * Tested up to: 7.0
12 * License: GPLv3
13 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
14 * Text Domain: wecantrack
15 * Domain Path: /languages
16 */
17
18 if (!defined('ABSPATH')) { die('You are not allowed to call this page directly.'); }
19
20 define('WECANTRACK_VERSION', '5.4.0');
21 define('WECANTRACK_PLUGIN_NAME', 'wecantrack');
22 define('WECANTRACK_PATH', plugin_dir_path(__FILE__));
23 define('WECANTRACK_URL', plugin_dir_url(__FILE__));
24 // Overridable for local development (e.g. in wp-config.php via .wp-env.override.json);
25 // the shipped default always points at production.
26 if (!defined('WECANTRACK_API_BASE_URL')) {
27 define('WECANTRACK_API_BASE_URL', 'https://api.wecantrack.com');
28 }
29
30 // Load core classes
31 require_once(WECANTRACK_PATH . '/WecantrackHelper.php');
32
33 if (is_admin() || defined('WP_CLI')) {
34 require_once(WECANTRACK_PATH . '/WecantrackAdmin.php');
35 new WecantrackAdmin();
36 } else if ((! defined('DOING_CRON') || ! DOING_CRON) && filter_input(INPUT_SERVER, 'REQUEST_URI') !== '/wp-login.php') {
37 // Do not enqueue our JS scripts in Thrive Architect's iframe.
38 $thriveIsActive = defined('TVE_PLUGIN_FILE') && strpos(filter_input(INPUT_SERVER, 'REQUEST_URI'), 'tve=true') !== false;
39 $elementorIsActive = defined('ELEMENTOR_VERSION') && strpos(filter_input(INPUT_SERVER, 'REQUEST_URI'), 'elementor-preview=') !== false;
40 $diviIsActive = defined('ET_CORE_VERSION') && strpos(filter_input(INPUT_SERVER, 'REQUEST_URI'), 'et_fb=') !== false;
41
42 if (! $thriveIsActive && ! $elementorIsActive && ! $diviIsActive) {
43 require_once(WECANTRACK_PATH . '/WecantrackApp.php');
44
45 if (! empty($_GET['afflink']) && ! empty($_GET['data'])) {
46 add_action('template_redirect', 'wecantrack_handle_deprecated_go_redirect');
47 }
48
49 new WecantrackApp();
50 }
51 }
52
53 /**
54 * Installation/Uninstall process
55 */
56
57 register_activation_hook(__FILE__, 'wecantrack_plugin_activation');
58 register_deactivation_hook(__FILE__, 'wecantrack_plugin_deactivation');
59 register_uninstall_hook(__FILE__, 'wecantrack_plugin_uninstall');
60 add_action('upgrader_process_complete', 'wecantrack_plugin_upgraded', 10, 2);
61 add_action('wecantrack_cron_refresh', 'wecantrack_refresh_website_options');
62
63 // Ensure cron is scheduled for existing installs that updated without reactivating
64 if (is_admin() && !wp_next_scheduled('wecantrack_cron_refresh')) {
65 wp_schedule_event(time(), 'hourly', 'wecantrack_cron_refresh');
66 }
67
68 if (!function_exists('wecantrack_refresh_website_options')) {
69 /**
70 * Cron callback that refreshes website options and tracking code from the WeCanTrack API.
71 *
72 * @return void
73 */
74 function wecantrack_refresh_website_options()
75 {
76 $api_key = get_option('wecantrack_api_key');
77 if (empty($api_key)) {
78 return;
79 }
80
81 try {
82 WecantrackHelper::refresh_config_with_candidates($api_key, WecantrackHelper::get_candidate_site_urls());
83 } catch (\Exception $e) {
84 error_log('[WeCanTrack] Cron refresh error: ' . $e->getMessage());
85 }
86 }
87 }
88
89 if (!function_exists('wecantrack_plugin_activation')) {
90 /**
91 * Runs on plugin activation.
92 *
93 * Registers default plugin options in the WordPress database.
94 *
95 * @return void
96 */
97 function wecantrack_plugin_activation()
98 {
99 add_option('wecantrack_api_key', null, null);
100 add_option('wecantrack_plugin_status', 0, null);
101 add_option('wecantrack_fetch_expiration', null, null);
102 add_option('wecantrack_snippet', 1, null);
103 add_option('wecantrack_session_enabler', null, null);
104 add_option('wecantrack_snippet_version', null, null);
105 add_option('wecantrack_domain_patterns', null, null);
106 add_option('wecantrack_custom_redirect_html', null, null);
107 add_option('wecantrack_redirect_options', null, null);
108 add_option('wecantrack_website_options', null, null);
109 add_option('wecantrack_version', null, null);
110 add_option('wecantrack_storage', null, null);
111 add_option('wecantrack_referrer_cookie_status', 0, null);
112 add_option('wecantrack_website_override', null, null);
113
114 if (!wp_next_scheduled('wecantrack_cron_refresh')) {
115 wp_schedule_event(time(), 'hourly', 'wecantrack_cron_refresh');
116 }
117 }
118 }
119
120 if (!function_exists('wecantrack_plugin_deactivation')) {
121 /**
122 * Runs on plugin deactivation.
123 *
124 * Disables plugin functionality without deleting saved options.
125 *
126 * @return void
127 */
128 function wecantrack_plugin_deactivation()
129 {
130 update_option('wecantrack_plugin_status', 0);
131 wp_clear_scheduled_hook('wecantrack_cron_refresh');
132 }
133 }
134
135 if (!function_exists('wecantrack_plugin_uninstall')) {
136 /**
137 * Runs on plugin uninstall.
138 *
139 * Deletes all plugin options from the WordPress database.
140 * This is a full cleanup to ensure no leftover data remains.
141 *
142 * @return void
143 */
144 function wecantrack_plugin_uninstall()
145 {
146 delete_option('wecantrack_api_key');
147 delete_option('wecantrack_plugin_status');
148 delete_option('wecantrack_fetch_expiration');
149 delete_option('wecantrack_snippet');
150 delete_option('wecantrack_session_enabler');
151 delete_option('wecantrack_snippet_version');
152 delete_option('wecantrack_domain_patterns');
153 delete_option('wecantrack_custom_redirect_html');
154 delete_option('wecantrack_redirect_options');
155 delete_option('wecantrack_website_options');
156 delete_option('wecantrack_version');
157 delete_option('wecantrack_storage');
158 delete_option('wecantrack_referrer_cookie_status');
159 delete_option('wecantrack_website_override');
160 }
161 }
162
163 if (!function_exists('wecantrack_plugin_upgraded')) {
164 /**
165 * Callback triggered after plugin is updated.
166 *
167 * If this plugin was updated, it refreshes the WeCanTrack tracking code
168 * and website information, then clears all known cache layers.
169 *
170 * @param WP_Upgrader $upgrader_object The upgrader instance.
171 * @param array $options Array of options for the upgrade action.
172 *
173 * @return void
174 */
175 function wecantrack_plugin_upgraded($upgrader_object, $options) {
176 $current_plugin_path_name = plugin_basename( __FILE__ );
177
178 if ($options['action'] == 'upgrade' && $options['type'] == 'plugin') {
179 foreach($options['plugins'] as $each_plugin) {
180 if ($each_plugin == $current_plugin_path_name) {
181 $api_key = get_option('wecantrack_api_key');
182
183 if (empty($api_key)) {
184 return;
185 }
186
187 // refetch the wecantrack script
188 try {
189 WecantrackHelper::refresh_config_with_candidates($api_key, WecantrackHelper::get_candidate_site_urls());
190 } catch (\Exception $e) {
191 error_log('[WeCanTrack] Error occurred during plugin upgrade. Message: ' . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine());
192 }
193
194 wecantrack_clear_all_known_caches();
195 }
196 }
197 }
198 }
199 }
200
201 if (!function_exists('wecantrack_clear_all_known_caches')) {
202 /**
203 * Clears cache from known WordPress caching plugins and platforms.
204 *
205 * Attempts to programmatically flush caches for:
206 * - WP Super Cache
207 * - W3 Total Cache
208 * - WP Rocket
209 * - LiteSpeed
210 * - SiteGround
211 * - Autoptimize
212 * - Swift Performance
213 * - Hummingbird
214 * - Comet Cache
215 * - Breeze
216 * - WP Fastest Cache
217 * - WP-Optimize
218 * - Cache Enabler
219 * - Kinsta
220 * - Hyper Cache
221 * - Simple Cache
222 * - Cachify
223 *
224 * @return void
225 */
226 function wecantrack_clear_all_known_caches() {
227 // WP Super Cache
228 if (function_exists('wp_cache_clear_cache')) {
229 wp_cache_clear_cache();
230 }
231
232 // W3 Total Cache
233 if (class_exists('W3_Plugin_TotalCacheAdmin')) {
234 if (function_exists('w3_instance')) {
235 $w3_plugin_totalcacheadmin = w3_instance('W3_Plugin_TotalCacheAdmin');
236 if (method_exists($w3_plugin_totalcacheadmin, 'flush_all')) {
237 $w3_plugin_totalcacheadmin->flush_all();
238 }
239 }
240 }
241
242 // WP Rocket
243 if (function_exists('rocket_clean_domain')) {
244 rocket_clean_domain();
245 }
246
247 // LiteSpeed Cache
248 do_action('litespeed_purge_all');
249
250 // SiteGround Optimizer
251 if (class_exists('SG_CachePress_Supercacher')) {
252 $sg_cache = new SG_CachePress_Supercacher();
253 $sg_cache->purge_cache();
254 }
255
256 // Autoptimize (Clears only its own cache)
257 if (class_exists('autoptimizeCache')) {
258 autoptimizeCache::clearall();
259 }
260
261 // Swift Performance
262 if (class_exists('Swift_Performance_Cache')) {
263 Swift_Performance_Cache::clear_all_cache();
264 }
265
266 // Hummingbird (by WPMU DEV)
267 do_action('wphb_clear_cache');
268
269 // Comet Cache (Zencache)
270 if (class_exists('comet_cache')) {
271 comet_cache::clear();
272 }
273
274 // Breeze (by Cloudways)
275 if (function_exists('breeze_clear_cache')) {
276 breeze_clear_cache();
277 }
278
279 // WP Fastest Cache
280 if (function_exists('wpfc_clear_all_cache')) {
281 // true = clear minified CSS/JS as well; omit/false if you only need the page cache.
282 wpfc_clear_all_cache(true);
283 }
284
285 // WP-Optimize
286 if (function_exists('wpo_cache_flush')) {
287 wpo_cache_flush();
288 }
289
290 // Cache Enabler
291 if (function_exists('cache_enabler_clear_total_cache')) {
292 cache_enabler_clear_total_cache();
293 }
294
295 // Kinsta MU Cache
296 do_action('kinsta_cache_purge_all');
297
298 // Hyper Cache
299 if (function_exists('hyper_cache_clean')) {
300 hyper_cache_clean();
301 }
302
303 // Simple Cache
304 if (function_exists('sc_clear_cache')) {
305 sc_clear_cache();
306 }
307
308 // Cachify
309 if (function_exists('cachify_flush_total_cache')) {
310 cachify_flush_total_cache();
311 }
312 }
313 }
314
315 if (!function_exists('wecantrack_handle_deprecated_go_redirect')) {
316 /**
317 * Handles deprecated /go redirects.
318 * This function sends a 410 Gone status and displays a message indicating that the redirect method is no longer supported.
319 * Additionally, it optionally emails the admin once per hour if the deprecated link is accessed.
320 *
321 * @return void Terminates script execution with exit().
322 */
323 function wecantrack_handle_deprecated_go_redirect() {
324 status_header(410); // Gone
325 header('Content-Type: text/html; charset=utf-8');
326
327 echo '<html>';
328 echo '<head>';
329 echo '<meta name="robots" content="noindex,nofollow">';
330 echo '<meta charset="UTF-8">';
331 echo '<title>Redirect Not Available</title>';
332 echo '<style>body{font-family:sans-serif;padding:2em;max-width:600px;margin:auto;}</style>';
333 echo '</head>';
334
335 echo '<body>';
336 echo '<h1>Link no longer available</h1>';
337 echo '<p>This affiliate link redirect method is no longer supported.</p>';
338 echo '<p>If you are the site admin, please clear your website and CDN caches to resolve issues with outdated or broken outgoing links.</p>';
339 if (!empty($_GET['afflink'])) {
340 echo '<p><a href="' . esc_url($_GET['afflink']) . '">Go to offer</a></p>';
341 }
342 echo '<p><a href="' . esc_url(home_url()) . '">Go back to homepage</a></p>';
343
344 echo '</body>';
345 echo '</html>';
346 exit;
347 }
348 }
349