bentleykf

WP 3.9 Ajax without loading Specific Plugins

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