-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracking-blocker.php
80 lines (73 loc) · 2.22 KB
/
tracking-blocker.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
<?php
/**
* Plugin Name: Tracking Blocker for WooCommerce
* Description: Blocks outbound tracking requests to tracking.woocommerce.com and logs the data.
* Version: 1.0
* Author: Your Name
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Blocks requests to tracking.woocommerce.com and logs the data
*
* @param array $preempt Whether to preempt an HTTP request's return value. Default false.
* @param array $parsed_args HTTP request arguments.
* @param string $url The request URL.
* @return array|false
*/
function tbwc_block_tracking_requests( $preempt, $parsed_args, $url ) {
if ( strpos( $url, 'tracking.woocommerce.com/v1/' ) !== false ) {
// Log the data that was supposed to be sent
error_log( 'Blocked WooCommerce tracking request: ' . print_r( $parsed_args, true ) );
// Return a response indicating the request was blocked
return [
'headers' => [],
'body' => '',
'response' => [
'code' => 403,
'message' => 'Forbidden'
],
'cookies' => [],
'filename' => null
];
}
return $preempt;
}
add_filter( 'pre_http_request', 'tbwc_block_tracking_requests', 10, 3 );
/**
* Enqueue admin styles
*/
function tbwc_enqueue_admin_styles() {
$css = "
/* Mobile-first styles */
.tbwc-notice {
background-color: #f8d7da;
border-color: #f5c6cb;
color: #721c24;
padding: 15px;
margin: 20px 0;
border: 1px solid transparent;
border-radius: .25rem;
}
@media (min-width: 768px) {
.tbwc-notice {
padding: 20px;
}
}
";
wp_add_inline_style( 'wp-admin', $css );
}
add_action( 'admin_enqueue_scripts', 'tbwc_enqueue_admin_styles' );
/**
* Display admin notice
*/
function tbwc_admin_notice() {
?>
<div class="notice notice-error tbwc-notice">
<p><?php esc_html_e( 'Tracking Blocker for WooCommerce is active. Outbound tracking requests are blocked.', 'tracking-blocker-woocommerce' ); ?></p>
</div>
<?php
}
add_action( 'admin_notices', 'tbwc_admin_notice' );