Last active
November 7, 2024 22:00
-
-
Save andrasguseo/7e06d64dced302e056cfea386f3c1e6d to your computer and use it in GitHub Desktop.
TEC > Block REST API requests coming from EA.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Block REST API requests coming from Event Aggregator. Basically blocks imports. | |
* It can block other sites trying to siphon events with EA through the "Other URL" source. | |
* | |
* Usage: Add the snippet with a plugin like Code Snippets or to your functions.php file. | |
* | |
* @author: Andras Guseo | |
* | |
* Plugins required: The Events Calendar | |
* | |
* @since November 7, 2024 Initial version. | |
*/ | |
add_filter( 'rest_pre_dispatch', function ( $result, $server, $request ) { | |
// List of IPs to block | |
$blocked_ips = [ '199.189.224.106' ]; | |
// Get the client IP address | |
$client_ip = $_SERVER['REMOTE_ADDR']; | |
// Check if this is a request to the events endpoint | |
if ( $request->get_route() === '/tribe/events/v1/events' ) { | |
// If the client IP is in the blocked list, deny access | |
if ( in_array( $client_ip, $blocked_ips ) ) { | |
return new WP_Error( | |
'rest_forbidden', | |
__( 'Access to this endpoint is blocked.' ), | |
array( 'status' => 403 ) | |
); | |
} | |
} | |
return $result; | |
}, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment