Skip to content

Instantly share code, notes, and snippets.

@nullvariable
Last active January 24, 2023 11:59
Show Gist options
  • Save nullvariable/56eab9ab40d9fa1f7acc4b1b6e6327e9 to your computer and use it in GitHub Desktop.
Save nullvariable/56eab9ab40d9fa1f7acc4b1b6e6327e9 to your computer and use it in GitHub Desktop.
Make annoying WordPress plugins that constantly perform license checks in the admin behave.
<?php
/**
* Plugin Name: Cache Aggressive License Checks
* Description: Cache the license checks for plugins that are lame and check on every request.
* Author: Doug Cone
* Author URI: http://dougcone.com
* Version: 0.0.1
*
*/
namespace nullvariable;
add_filter( 'pre_http_request', [ '\nullvariable\Cache_License_Checks', 'pre_http_request' ], 1, 3 );
add_filter( 'http_response', [ '\nullvariable\Cache_License_Checks', 'http_response' ], 1, 3 );
/**
* Cache_License_Checks class
*/
class Cache_License_Checks {
/**
* Domains to cache.
*
* @var $domains_to_cache array domains strings.
*/
public static $domains_to_cache = [
'example.com',
'www.anotherslowapidomain.com',
];
/**
* Pre_http_request filter for http class.
*
* Returns cache, if we have it.
*
* @param false|array|WP_Error $preempt Filtered content.
* @param array $args Request args.
* @param string $url Request url.
* @return false|array
*/
public static function pre_http_request( $preempt, $args, $url ) {
$domain = wp_parse_url( $url, PHP_URL_HOST );
if ( in_array( $domain, self::$domains_to_cache, true ) ) {
$preempt = wp_cache_get( md5( $url ), 'nv_license_cache' );
}
return $preempt;
}
/**
* Http_response filter for http class.
*
* Sets a cache value for requests we wish to cache.
*
* @param array $response HTTP response array.
* @param array $args HTTP request arguments.
* @param string $url The request URL.
* @return array Response array.
*/
public static function http_response( $response, $args, $url ) {
$domain = wp_parse_url( $url, PHP_URL_HOST );
if ( in_array( $domain, self::$domains_to_cache, true ) ) {
wp_cache_set( md5( $url ), $response, 'nv_license_cache', DAY_IN_SECONDS );
}
return $response;
}
}
@nullvariable
Copy link
Author

I use New Relic to find these bad actors, and then just add them to the array on Line 24.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment