Created
June 6, 2020 22:55
-
-
Save wpsmith/e72932af8acc1d4af246b4e32a7d3456 to your computer and use it in GitHub Desktop.
WP: MU Plugin for Optimizing AJAX calls.
This file contains hidden or 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 | |
/** | |
* Plugin Name: WPS AJAX MU Plugin | |
* Plugin URI: https://wpsmith.net | |
* Description: Improves the performance of AJAX requests. | |
* Author: Travis Smith <[email protected]> | |
* Author URI: https://wpsmith.net | |
* Version: 0.0.1 | |
* | |
* The main mu plugin file. | |
* | |
* This file is read by WordPress to generate the plugin information in the plugin | |
* admin area. This file also includes all of the dependencies used by the plugin, | |
* registers the activation and deactivation functions, and defines a function | |
* that starts the plugin. | |
* | |
* You may copy, distribute and modify the software as long as you track | |
* changes/dates in source files. Any modifications to or software including | |
* (via compiler) GPL-licensed code must also be made available under the GPL | |
* along with build & install instructions. | |
* | |
* PHP Version 7.2 | |
* | |
* @category WPS\WP\Plugins\Mu\AJAX | |
* @package WPS\WP\Plugins\Mu\AJAX | |
* @author Travis Smith <[email protected]> | |
* @copyright 2020 Travis Smith | |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License v2 | |
* @link https://wpsmith.net/ | |
* @since 0.0.1 | |
*/ | |
namespace WPS\WP\Plugins\Mu\AJAX; | |
add_filter( 'option_active_plugins', __NAMESPACE__ . '\ajax_disable_plugins', PHP_INT_MAX ); | |
/** | |
* Intercept `get_option( 'active_plugins' )` to only load certain plugins. | |
* | |
* @param array $plugins Array of active plugins. | |
* | |
* @return array Array of a plugins that should be activated. | |
*/ | |
function ajax_disable_plugins( $plugins ) { | |
// Do nothing if it's not AJAX or abiding by `fast_ajax` element. | |
// Load all plugins if (1) not in ajax mode, (2) fast_ajax is not set, & (3) fast_ajax is set to false. | |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
if ( ! defined( 'DOING_AJAX' ) || ! isset( $_REQUEST['fast_ajax'] ) || ( isset( $_REQUEST['fast_ajax'] ) && ! (bool) $_REQUEST['fast_ajax'] ) | |
) { | |
return $plugins; | |
} | |
// Disable all plugins if none are told to load by the load_plugins array. | |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
if ( ! isset( $_REQUEST['load_plugins'] ) || ( ! isset( $_REQUEST['load_plugins'] ) && ! (bool) $_REQUEST['load_plugins'] ) ) { | |
return array(); | |
} | |
// Unset plugins not included in the load_plugins array. | |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
return array_intersect( $plugins, array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['load_plugins'] ) ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment