bentleykf

WP Ajax without loading Specific Plugins

Apr 23rd, 2014
1,738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.80 KB | None | 0 0
  1. // This is a combination of the ajax approach by raz ohad
  2. // (see https://coderwall.com/p/of7y2q)
  3. // and the wp-settings.php file after 'if ( SHORTINIT ) return false;'
  4.  
  5. // I'm excluding specific plugins from loading when we loop
  6. // through the plugin array returned by wp_get_active_and_valid_plugins().
  7.  
  8. //mimic the actuall admin-ajax
  9. define('DOING_AJAX', true);
  10.  
  11. if (!isset( $_REQUEST['action']))
  12.     die('-1');
  13.  
  14. ini_set('html_errors', 0);
  15.    
  16. //make sure we skip most of the loading which we might not need
  17. //http://core.trac.wordpress.org/browser/branches/3.4/wp-settings.php#L99
  18. define('SHORTINIT', true);
  19.  
  20. //make sure you update this line
  21. //to the relative location of the wp-load.php
  22. require_once('../../../../wp-load.php');
  23.  
  24. //Typical headers
  25. header('Content-Type: text/html');
  26. send_nosniff_header();
  27. //Disable caching
  28. header('Cache-Control: no-cache');
  29. header('Pragma: no-cache');
  30.  
  31. // from wp-settings.php after SHORTINIT
  32.  
  33. // Load the L10n library.
  34. require_once( ABSPATH . WPINC . '/l10n.php' );
  35.  
  36. // Run the installer if WordPress is not installed.
  37. wp_not_installed();
  38.  
  39. // Load most of WordPress.
  40. require( ABSPATH . WPINC . '/class-wp-walker.php' );
  41. require( ABSPATH . WPINC . '/class-wp-ajax-response.php' );
  42. require( ABSPATH . WPINC . '/formatting.php' );
  43. require( ABSPATH . WPINC . '/capabilities.php' );
  44. require( ABSPATH . WPINC . '/query.php' );
  45. require( ABSPATH . WPINC . '/date.php' );
  46. require( ABSPATH . WPINC . '/theme.php' );
  47. require( ABSPATH . WPINC . '/class-wp-theme.php' );
  48. require( ABSPATH . WPINC . '/template.php' );
  49. require( ABSPATH . WPINC . '/user.php' );
  50. require( ABSPATH . WPINC . '/meta.php' );
  51. require( ABSPATH . WPINC . '/general-template.php' );
  52. require( ABSPATH . WPINC . '/link-template.php' );
  53. require( ABSPATH . WPINC . '/author-template.php' );
  54. require( ABSPATH . WPINC . '/post.php' );
  55. require( ABSPATH . WPINC . '/post-template.php' );
  56. require( ABSPATH . WPINC . '/revision.php' );
  57. require( ABSPATH . WPINC . '/post-formats.php' );
  58. require( ABSPATH . WPINC . '/post-thumbnail-template.php' );
  59. require( ABSPATH . WPINC . '/category.php' );
  60. require( ABSPATH . WPINC . '/category-template.php' );
  61. require( ABSPATH . WPINC . '/comment.php' );
  62. require( ABSPATH . WPINC . '/comment-template.php' );
  63. require( ABSPATH . WPINC . '/rewrite.php' );
  64. require( ABSPATH . WPINC . '/feed.php' );
  65. require( ABSPATH . WPINC . '/bookmark.php' );
  66. require( ABSPATH . WPINC . '/bookmark-template.php' );
  67. require( ABSPATH . WPINC . '/kses.php' );
  68. require( ABSPATH . WPINC . '/cron.php' );
  69. require( ABSPATH . WPINC . '/deprecated.php' );
  70. require( ABSPATH . WPINC . '/script-loader.php' );
  71. require( ABSPATH . WPINC . '/taxonomy.php' );
  72. require( ABSPATH . WPINC . '/update.php' );
  73. require( ABSPATH . WPINC . '/canonical.php' );
  74. require( ABSPATH . WPINC . '/shortcodes.php' );
  75. require( ABSPATH . WPINC . '/class-wp-embed.php' );
  76. require( ABSPATH . WPINC . '/media.php' );
  77. require( ABSPATH . WPINC . '/http.php' );
  78. require( ABSPATH . WPINC . '/class-http.php' );
  79. require( ABSPATH . WPINC . '/widgets.php' );
  80. require( ABSPATH . WPINC . '/nav-menu.php' );
  81. require( ABSPATH . WPINC . '/nav-menu-template.php' );
  82. require( ABSPATH . WPINC . '/admin-bar.php' );
  83.  
  84. // Load multisite-specific files.
  85. if ( is_multisite() ) {
  86.     require( ABSPATH . WPINC . '/ms-functions.php' );
  87.     require( ABSPATH . WPINC . '/ms-default-filters.php' );
  88.     require( ABSPATH . WPINC . '/ms-deprecated.php' );
  89. }
  90.  
  91. // Define constants that rely on the API to obtain the default value.
  92. // Define must-use plugin directory constants, which may be overridden in the sunrise.php drop-in.
  93. wp_plugin_directory_constants();
  94.  
  95. // Load must-use plugins.
  96. foreach ( wp_get_mu_plugins() as $mu_plugin ) {
  97.     include_once( $mu_plugin );
  98. }
  99. unset( $mu_plugin );
  100.  
  101. // Load network activated plugins.
  102. if ( is_multisite() ) {
  103.     foreach( wp_get_active_network_plugins() as $network_plugin ) {
  104.         include_once( $network_plugin );
  105.     }
  106.     unset( $network_plugin );
  107. }
  108.  
  109. /**
  110.  * Fires once all must-use and network-activated plugins have loaded.
  111.  *
  112.  * @since 2.8.0
  113.  */
  114. do_action( 'muplugins_loaded' );
  115.  
  116. if ( is_multisite() )
  117.     ms_cookie_constants(  );
  118.  
  119. // Define constants after multisite is loaded. Cookie-related constants may be overridden in ms_network_cookies().
  120. wp_cookie_constants();
  121.  
  122. // Define and enforce our SSL constants
  123. wp_ssl_constants();
  124.  
  125. // Create common globals.
  126. require( ABSPATH . WPINC . '/vars.php' );
  127.  
  128. // Make taxonomies and posts available to plugins and themes.
  129. // @plugin authors: warning: these get registered again on the init hook.
  130. create_initial_taxonomies();
  131. create_initial_post_types();
  132.  
  133. // Register the default theme directory root
  134. register_theme_directory( get_theme_root() );
  135.  
  136. // Load active plugins...
  137. //  well we don't need to load all our plugins for ajax calls
  138. // as you can abstract from the loop, wp_get_active_and_valid_plugins() is the
  139. // server side path to the plugin file, so just evaluate against that...eg:
  140. //
  141. //  [8] => /wp-content/plugins/facebook-awd/AWD_facebook.php
  142. //
  143.  
  144. $ldd_plugins = array();
  145. foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
  146.     if(strpos($plugin, 'ewww-image-optimizer') !== false)       continue;
  147.     if(strpos($plugin, 'facebook-awd') !== false)           continue;
  148.     if(strpos($plugin, 'instagrabber') !== false)           continue;
  149.     if(strpos($plugin, 'w3-total-cache') !== false)         continue;
  150.     if(strpos($plugin, 'wp-pagenavi') !== false)            continue;
  151.  
  152.     include_once( $plugin );
  153.     $ldd_plugins[] = $plugin;
  154. }
  155. //print_r($ldd_plugins);
  156. unset($ldd_plugins);
  157. unset( $plugin );
  158.  
  159. // Load pluggable functions.
  160. require( ABSPATH . WPINC . '/pluggable.php' );
  161. require( ABSPATH . WPINC . '/pluggable-deprecated.php' );
  162.  
  163. // Set internal encoding.
  164. wp_set_internal_encoding();
  165.  
  166. // Run wp_cache_postload() if object cache is enabled and the function exists.
  167. if ( WP_CACHE && function_exists( 'wp_cache_postload' ) )
  168.     wp_cache_postload();
  169.  
  170. /**
  171.  * Fires once activated plugins have loaded.
  172.  *
  173.  * Pluggable functions are also available at this point in the loading order.
  174.  *
  175.  * @since 1.5.0
  176.  */
  177. do_action( 'plugins_loaded' );
  178.  
  179. // Define constants which affect functionality if not already defined.
  180. wp_functionality_constants();
  181.  
  182. // Add magic quotes and set up $_REQUEST ( $_GET + $_POST )
  183. wp_magic_quotes();
  184.  
  185. /**
  186.  * Fires when comment cookies are sanitized.
  187.  *
  188.  * @since 2.0.11
  189.  */
  190. do_action( 'sanitize_comment_cookies' );
  191.  
  192. /**
  193.  * WordPress Query object
  194.  * @global object $wp_the_query
  195.  * @since 2.0.0
  196.  */
  197. $wp_the_query = new WP_Query();
  198.  
  199. /**
  200.  * Holds the reference to @see $wp_the_query
  201.  * Use this global for WordPress queries
  202.  * @global object $wp_query
  203.  * @since 1.5.0
  204.  */
  205. $wp_query = $wp_the_query;
  206.  
  207. /**
  208.  * Holds the WordPress Rewrite object for creating pretty URLs
  209.  * @global object $wp_rewrite
  210.  * @since 1.5.0
  211.  */
  212. $GLOBALS['wp_rewrite'] = new WP_Rewrite();
  213.  
  214. /**
  215.  * WordPress Object
  216.  * @global object $wp
  217.  * @since 2.0.0
  218.  */
  219. $wp = new WP();
  220.  
  221. /**
  222.  * WordPress Widget Factory Object
  223.  * @global object $wp_widget_factory
  224.  * @since 2.8.0
  225.  */
  226. $GLOBALS['wp_widget_factory'] = new WP_Widget_Factory();
  227.  
  228. /**
  229.  * WordPress User Roles
  230.  * @global object $wp_roles
  231.  * @since 2.0.0
  232.  */
  233. $GLOBALS['wp_roles'] = new WP_Roles();
  234.  
  235. /**
  236.  * Fires before the theme is loaded.
  237.  *
  238.  * @since 2.6.0
  239.  */
  240. do_action( 'setup_theme' );
  241.  
  242. // Define the template related constants.
  243. wp_templating_constants(  );
  244.  
  245. // Load the default text localization domain.
  246. load_default_textdomain();
  247.  
  248. $locale = get_locale();
  249. $locale_file = WP_LANG_DIR . "/$locale.php";
  250. if ( ( 0 === validate_file( $locale ) ) && is_readable( $locale_file ) )
  251.     require( $locale_file );
  252. unset( $locale_file );
  253.  
  254. // Pull in locale data after loading text domain.
  255. require_once( ABSPATH . WPINC . '/locale.php' );
  256.  
  257. /**
  258.  * WordPress Locale object for loading locale domain date and various strings.
  259.  * @global object $wp_locale
  260.  * @since 2.1.0
  261.  */
  262. $GLOBALS['wp_locale'] = new WP_Locale();
  263.  
  264. // Load the functions for the active theme, for both parent and child theme if applicable.
  265. if ( ! defined( 'WP_INSTALLING' ) || 'wp-activate.php' === $pagenow ) {
  266.     if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) )
  267.         include( STYLESHEETPATH . '/functions.php' );
  268.     if ( file_exists( TEMPLATEPATH . '/functions.php' ) )
  269.         include( TEMPLATEPATH . '/functions.php' );
  270. }
  271.  
  272. /**
  273.  * Fires after the theme is loaded.
  274.  *
  275.  * @since 3.0.0
  276.  */
  277. do_action( 'after_setup_theme' );
  278.  
  279. // Set up current user.
  280. $wp->init();
  281.  
  282. /**
  283.  * Fires after WordPress has finished loading but before any headers are sent.
  284.  *
  285.  * Most of WP is loaded at this stage, and the user is authenticated. WP continues
  286.  * to load on the init hook that follows (e.g. widgets), and many plugins instantiate
  287.  * themselves on it for all sorts of reasons (e.g. they need a user, a taxonomy, etc.).
  288.  *
  289.  * If you wish to plug an action once WP is loaded, use the wp_loaded hook below.
  290.  *
  291.  * @since 1.5.0
  292.  */
  293. do_action( 'init' );
  294.  
  295. // Check site status
  296. if ( is_multisite() ) {
  297.     if ( true !== ( $file = ms_site_check() ) ) {
  298.         require( $file );
  299.         die();
  300.     }
  301.     unset($file);
  302. }
  303.  
  304. /**
  305.  * This hook is fired once WP, all plugins, and the theme are fully loaded and instantiated.
  306.  *
  307.  * AJAX requests should use wp-admin/admin-ajax.php. admin-ajax.php can handle requests for
  308.  * users not logged in.
  309.  *
  310.  * @link http://codex.wordpress.org/AJAX_in_Plugins
  311.  *
  312.  * @since 3.0.0
  313.  */
  314. do_action( 'wp_loaded' );
  315.  
  316.  
  317. $action = esc_attr(trim($_POST['action']));
  318.  
  319. //A bit of security
  320. $allowed_actions = array(
  321.     'my_allow_action_1',
  322.     'my_allow_action_2',
  323.     'my_allow_action_3',
  324.     'my_allow_action_4',
  325.     'my_allow_action_5'
  326. );
  327.  
  328. if(in_array($action, $allowed_actions)){
  329.     if(is_user_logged_in())
  330.         do_action('MY_AJAX_HANDLER_'.$action);
  331.     else
  332.         do_action('MY_AJAX_HANDLER_nopriv_'.$action);
  333. }
  334. else{
  335.     die('-1');
  336. }
Advertisement
Add Comment
Please, Sign In to add comment