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

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