Example #1
0
 /**
  * Set a specific user context for WordPress.
  *
  * @param array $assoc_args
  */
 private static function set_user($assoc_args)
 {
     if (isset($assoc_args['user'])) {
         $fetcher = new \WP_CLI\Fetchers\User();
         $user = $fetcher->get_check($assoc_args['user']);
         wp_set_current_user($user->ID);
     } else {
         kses_remove_filters();
     }
 }
Example #2
0
 /**
  * Set up hooks meant to run during the WordPress bootstrap process
  */
 private function setup_bootstrap_hooks()
 {
     if ($this->config['skip-plugins']) {
         $this->setup_skip_plugins_filters();
     }
     if ($this->config['skip-themes']) {
         WP_CLI::add_wp_hook('setup_theme', array($this, 'action_setup_theme_wp_cli_skip_themes'), 999);
     }
     WP_CLI::add_wp_hook('wp_die_handler', function () {
         return '\\WP_CLI\\Utils\\wp_die_handler';
     });
     // Prevent code from performing a redirect
     WP_CLI::add_wp_hook('wp_redirect', 'WP_CLI\\Utils\\wp_redirect_handler');
     WP_CLI::add_wp_hook('nocache_headers', function ($headers) {
         // WordPress might be calling nocache_headers() because of a dead db
         global $wpdb;
         if (!empty($wpdb->error)) {
             Utils\wp_die_handler($wpdb->error);
         }
         // Otherwise, WP might be calling nocache_headers() because WP isn't installed
         Utils\wp_not_installed();
         return $headers;
     });
     // ALTERNATE_WP_CRON might trigger a redirect, which we can't handle
     if (defined('ALTERNATE_WP_CRON') && ALTERNATE_WP_CRON) {
         WP_CLI::add_wp_hook('muplugins_loaded', function () {
             remove_action('init', 'wp_cron');
         });
     }
     // Get rid of warnings when converting single site to multisite
     if (defined('WP_INSTALLING') && $this->is_multisite()) {
         $values = array('ms_files_rewriting' => null, 'active_sitewide_plugins' => array(), '_site_transient_update_core' => null, '_site_transient_update_themes' => null, '_site_transient_update_plugins' => null, 'WPLANG' => '');
         foreach ($values as $key => $value) {
             WP_CLI::add_wp_hook("pre_site_option_{$key}", function () use($values, $key) {
                 return $values[$key];
             });
         }
     }
     // Always permit operations against sites, regardless of status
     WP_CLI::add_wp_hook('ms_site_check', '__return_true');
     // Always permit operations against WordPress, regardless of maintenance mode
     WP_CLI::add_wp_hook('enable_maintenance_mode', function () {
         return false;
     });
     // Use our own debug mode handling instead of WP core
     WP_CLI::add_wp_hook('enable_wp_debug_mode_checks', function ($ret) {
         Utils\wp_debug_mode();
         return false;
     });
     // Never load advanced-cache.php drop-in when WP-CLI is operating
     WP_CLI::add_wp_hook('enable_loading_advanced_cache_dropin', function () {
         return false;
     });
     // In a multisite install, die if unable to find site given in --url parameter
     if ($this->is_multisite()) {
         WP_CLI::add_wp_hook('ms_site_not_found', function ($current_site, $domain, $path) {
             WP_CLI::error("Site {$domain}{$path} not found.");
         }, 10, 3);
     }
     // The APC cache is not available on the command-line, so bail, to prevent cache poisoning
     WP_CLI::add_wp_hook('muplugins_loaded', function () {
         if ($GLOBALS['_wp_using_ext_object_cache'] && class_exists('APC_Object_Cache')) {
             WP_CLI::warning('Running WP-CLI while the APC object cache is activated can result in cache corruption.');
             WP_CLI::confirm('Given the consequences, do you wish to continue?');
         }
     }, 0);
     // Handle --user parameter
     if (!defined('WP_INSTALLING')) {
         $config = $this->config;
         WP_CLI::add_wp_hook('init', function () use($config) {
             if (isset($config['user'])) {
                 $fetcher = new \WP_CLI\Fetchers\User();
                 $user = $fetcher->get_check($config['user']);
                 wp_set_current_user($user->ID);
             } else {
                 add_action('init', 'kses_remove_filters', 11);
             }
         }, 0);
     }
     // Avoid uncaught exception when using wp_mail() without defined $_SERVER['SERVER_NAME']
     WP_CLI::add_wp_hook('wp_mail_from', function ($from_email) {
         if ('wordpress@' === $from_email) {
             $sitename = strtolower(parse_url(site_url(), PHP_URL_HOST));
             if (substr($sitename, 0, 4) == 'www.') {
                 $sitename = substr($sitename, 4);
             }
             $from_email = 'wordpress@' . $sitename;
         }
         return $from_email;
     });
 }
Example #3
0
 /**
  * Set up hooks meant to run during the WordPress bootstrap process
  */
 private function setup_bootstrap_hooks()
 {
     if ($this->config['skip-plugins']) {
         $this->setup_skip_plugins_filters();
     }
     if ($this->config['skip-themes']) {
         $this->add_wp_hook('setup_theme', array($this, 'action_setup_theme_wp_cli_skip_themes'), 999);
     }
     $this->add_wp_hook('wp_die_handler', function () {
         return '\\WP_CLI\\Utils\\wp_die_handler';
     });
     // Prevent code from performing a redirect
     $this->add_wp_hook('wp_redirect', 'WP_CLI\\Utils\\wp_redirect_handler');
     $this->add_wp_hook('nocache_headers', function ($headers) {
         Utils\wp_not_installed();
         return $headers;
     });
     // Get rid of warnings when converting single site to multisite
     if (defined('WP_INSTALLING') && $this->is_multisite()) {
         $values = array('ms_files_rewriting' => null, 'active_sitewide_plugins' => array(), '_site_transient_update_core' => null, '_site_transient_update_themes' => null, '_site_transient_update_plugins' => null, 'WPLANG' => '');
         foreach ($values as $key => $value) {
             $this->add_wp_hook("pre_site_option_{$key}", function () use($values, $key) {
                 return $values[$key];
             });
         }
     }
     // Always permit operations against sites, regardless of status
     $this->add_wp_hook('ms_site_check', '__return_true');
     // Always permit operations against WordPress, regardless of maintenance mode
     $this->add_wp_hook('enable_maintenance_mode', function () {
         return false;
     });
     // Use our own debug mode handling instead of WP core
     $this->add_wp_hook('enable_wp_debug_mode_checks', function ($ret) {
         Utils\wp_debug_mode();
         // Check to see of wpdb is errored, instead of waiting for dead_db()
         require_wp_db();
         global $wpdb;
         if (!empty($wpdb->error)) {
             Utils\wp_die_handler($wpdb->error);
         }
         return false;
     });
     // Never load advanced-cache.php drop-in when WP-CLI is operating
     $this->add_wp_hook('enable_loading_advanced_cache_dropin', function () {
         return false;
     });
     // In a multisite install, die if unable to find site given in --url parameter
     if ($this->is_multisite()) {
         $this->add_wp_hook('ms_site_not_found', function ($current_site, $domain, $path) {
             WP_CLI::error("Site {$domain}{$path} not found.");
         }, 10, 3);
     }
     // The APC cache is not available on the command-line, so bail, to prevent cache poisoning
     $this->add_wp_hook('muplugins_loaded', function () {
         if ($GLOBALS['_wp_using_ext_object_cache'] && class_exists('APC_Object_Cache')) {
             WP_CLI::warning('Running WP-CLI while the APC object cache is activated can result in cache corruption.');
             WP_CLI::confirm('Given the consequences, do you wish to continue?');
         }
     }, 0);
     // Handle --user parameter
     if (!defined('WP_INSTALLING')) {
         $config = $this->config;
         $this->add_wp_hook('init', function () use($config) {
             if (isset($config['user'])) {
                 $fetcher = new \WP_CLI\Fetchers\User();
                 $user = $fetcher->get_check($config['user']);
                 wp_set_current_user($user->ID);
             } else {
                 add_action('init', 'kses_remove_filters', 11);
             }
         }, 0);
     }
 }
Example #4
0
File: Runner.php Project: nb/wp-cli
 private static function set_user($assoc_args)
 {
     if (!isset($assoc_args['user'])) {
         return;
     }
     $fetcher = new \WP_CLI\Fetchers\User();
     $user = $fetcher->get_check($assoc_args['user']);
     wp_set_current_user($user->ID);
 }