Created
March 20, 2015 00:09
-
-
Save petenelson/b8348878f3713e5fc233 to your computer and use it in GitHub Desktop.
WordPress: Prevent specific plugins from loading during /api/ requests
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: GGA API Helper | |
* Description: Prevents certain plugins from being loaded during API requests to reduce overhead. | |
* Author: Pete Nelson (@GunGeekATX) | |
* Version: 1.0.0 | |
*/ | |
/* | |
drop this in your mu-plugins folder | |
Original code supplied by https://thomasgriffin.io/a-creative-approach-to-efficient-and-scalable-wordpress-api-endpoints/ | |
*/ | |
// Make sure we can target a valid URI endpoint. | |
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { | |
return; | |
} | |
// Make sure that the endpoint being requested matches a valid API endpoint. | |
if ( strpos( stripslashes( $_SERVER['REQUEST_URI'] ), '/api/' ) === false ) { | |
return; | |
} | |
// Define a constant that we can make use of in any API plugins we create. | |
define( 'GGA_API_REQUEST', true ); | |
// Now that we know this is an API request, let's filter available plugins. | |
add_filter( 'option_active_plugins', 'gga_remove_plugins_for_api_request' ); | |
function gga_remove_plugins_for_api_request( $plugins ) { | |
$all_plugins = filter_input( INPUT_GET, 'all-plugins' ); | |
if ( empty( $all_plugins ) ) { | |
$all_plugins = filter_input( INPUT_POST, 'all-plugins' ); | |
} | |
// allow all plugins to load if the API requests them | |
if ( ! empty( $all_plugins ) ) { | |
return $plugins; | |
} | |
$disallowed_plugins = gga_disallowed_api_plugins(); | |
if ( ! empty( $plugins ) ) { | |
$new_plugins = array(); | |
for ($i=0; $i < count($plugins); $i++) { | |
if ( ! in_array($plugins[ $i ], $disallowed_plugins) ) { | |
$new_plugins[] = $plugins[ $i ]; | |
} | |
} | |
$plugins = $new_plugins; | |
} | |
return $plugins; | |
} | |
if ( ! function_exists( 'gga_disallowed_api_plugins' ) ) { | |
function gga_disallowed_api_plugins() { | |
return array( | |
/* 'woocommerce/woocommerce.php', this needs to load to allow our API endpoints to work */ | |
'woocommerce-exporter/exporter.php', | |
'woocommerce-partial-orders/woocommerce-partial-orders.php', | |
'woocommerce-store-credit/store-credit.php', | |
'woothemes-updater/woothemes-updater.php', | |
'gravityforms/gravityforms.php', | |
'delete-expired-transients/delete-expired-transients.php', | |
'disqus-comment-system/disqus.php', | |
'duplicate-menu/duplicate-menu.php', | |
'easy-table/easy-table.php', | |
'query-monitor/query-monitor.php', | |
'gift-cards-for-woocommerce/giftcards.php', | |
'photo-gallery/photo-gallery.php', | |
'simple-custom-post-order/simple-custom-post-order.php', | |
'simple-page-ordering/simple-page-ordering.php', | |
'simple-popup-plugin/simple_popup_plugin.php', | |
'unattach/unattach.php', | |
'woocommerce-gateway-stripe/woocommerce-gateway-stripe.php', | |
'woocommerce-shipping-ups/shipping-ups.php', | |
'woothemes-updater-version-1.5.1/woothemes-updater.php', | |
'wordpress-popular-posts/wordpress-popular-posts.php', | |
'wp-memcached-manager/wp-memcached-manager.php', | |
'wp-missed-schedule/wp-missed-schedule.php', | |
'alligator-popup/alligator-popup.php', | |
'black-studio-tinymce-widget/black-studio-tinymce-widget.php', | |
'debug-bar-cron/debug-bar-cron.php', | |
'debug-bar/debug-bar.php', | |
'duplicate-post/duplicate-post.php', | |
'enable-media-replace/enable-media-replace.php', | |
'flexible-posts-widget/flexible-posts-widget.php', | |
'google-analytics-for-wordpress/googleanalytics.php', | |
'imsanity/imsanity.php', | |
'members/members.php', | |
'regenerate-thumbnails/regenerate-thumbnails.php', | |
'relevanssi/relevanssi.php', | |
'rewrite-rules-inspector/rewrite-rules-inspector.php', | |
'seo-image/seo-friendly-images.php', | |
'tabby-responsive-tabs/tabby-responsive-tabs.php', | |
'taxonomy-terms-order/taxonomy-terms-order.php', | |
'tinymce-advanced/tinymce-advanced.php', | |
'ubermenu/ubermenu.php', | |
'user-switching/user-switching.php', | |
'wordpress-importer/wordpress-importer.php', | |
'wordpress-seo/wp-seo.php', | |
'wp-columna/wp-columna.php', | |
'wp-optimize/wp-optimize.php', | |
'list-pages-shortcode/list-pages-shortcode.php', | |
'menu-social-icons/plugin.php', | |
'redirection/redirection.php', | |
'restrict-widgets/restrict-widgets.php', | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment