PluginProbe
Affiliate Sales in Google Analytics and other tools / trunk
Affiliate Sales in Google Analytics and other tools vtrunk
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 trunk, at wecantrack.php

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