PHP code example of frosty-media / wp-rest-cop

1. Go to this page and download the library: Download frosty-media/wp-rest-cop library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

frosty-media / wp-rest-cop example snippets




use FrostyMedia\WpRestCop\RestApi\Officer;

/**
 * Set the rate limit to 1000 requests every hour.
 */
add_action( 'wp_rest_cop_plugin_loaded', static function(Officer $officer): void {
    $officer
        ->setInterval(HOUR_IN_SECONDS)
        ->setLimit(1000);
});



use FrostyMedia\WpRestCop\RestApi\Officer;

/**
 * Skip the rate limit throttle on current route or other conditions.
 */
add_filter( 'wp_rest_cop_skip_throttle', static function(
    bool $skip,
    WP_REST_Request $request,
    WP_REST_Server $server): bool {
	
	if ($request->get_route() === 'some/route') {
	    $skip = true;
	}
	return $skip;
}, 10, 3);



use FrostyMedia\WpRestCop\RestApi\Rules\IpRulesInterface;
use FrostyMedia\WpRestCop\RestApi\Officer;

/**
 * Modify IP rules configuration via code.
 */
add_action( 'wp_rest_cop_rest_api_init', static function(Officer $officer, IpRulesInterface $ipRules): void {
    $ipRules->allow('192.168.50.4'); // Also accepts an array of IP addresses.
    
    // Or...
    $ipRules->deny('66.249.66.1'); // Also accepts an array of IP addresses.
}, 10, 2);



use FrostyMedia\WpRestCop\RestApi\Rules\IpRulesInterface;

/**
 * Register routes.
 */
add_action('rest_api_init', static function (): void {
    register_rest_route( 'myplugin/v1', '/internal/(?P<id>\d+)', [
        'methods' => 'GET',
        'callback' => 'my_awesome_expensive_func',
        IpRulesInterface::IPS => [
            IpRulesInterface::ALLOW => ['192.168.50.4'],
            IpRulesInterface::DENY => ['66.249.66.1'],
        ],
    ]);
});