OSDN Git Service

Simulate and announce a /wp-admin/ page load
authorscribu <mail@scribu.net>
Tue, 23 Apr 2013 13:42:50 +0000 (16:42 +0300)
committerscribu <mail@scribu.net>
Tue, 23 Apr 2013 13:58:49 +0000 (16:58 +0300)
Most of the built-in commands need access to code from
wp-admin/includes/ so it makes sense to load it upfront.

Since we're already doing that, it seems like a good idea to also
announce this fact to plugins, by setting WP_ADMIN to true.

And since we're setting WP_ADMIN to true, we don't need to set up the
global $wp_query instance anymore, because the only admin screen where
it's set is the post list screen (wp-admin/edit.php).

See #385

features/core.feature
php/WP_CLI/Runner.php
php/utils-wp.php
php/wp-cli.php

index 87d0687..6092329 100644 (file)
@@ -69,6 +69,20 @@ Feature: Manage WordPress installation
     When I run `wp core is-installed`
     Then it should run without errors
 
+    When I run `wp eval 'var_export( is_admin() );'`
+    Then it should run without errors
+    And STDOUT should be:
+      """
+      true
+      """ 
+
+    When I run `wp eval 'var_export( function_exists( 'media_handle_upload' ) );'`
+    Then it should run without errors
+    And STDOUT should be:
+      """
+      true
+      """ 
+
   Scenario: Custom wp-content directory
     Given a WP install
     And a custom wp-content directory
index a41f34b..4af9554 100644 (file)
@@ -347,9 +347,6 @@ class Runner {
 
                Utils\set_user( $this->config );
 
-               if ( !defined( 'WP_INSTALLING' ) && isset( $this->config['url'] ) )
-                       Utils\set_wp_query();
-
                if ( isset( $this->config['require'] ) )
                        require $this->config['require'];
 
index 2153d3f..1821f7f 100644 (file)
@@ -65,13 +65,6 @@ function set_user( $assoc_args ) {
        }
 }
 
-function set_wp_query() {
-       if ( isset( $GLOBALS['wp_query'] ) && isset( $GLOBALS['wp'] ) ) {
-               $GLOBALS['wp']->parse_request();
-               $GLOBALS['wp_query']->query($GLOBALS['wp']->query_vars);
-       }
-}
-
 function get_upgrader( $class ) {
        if ( !class_exists( '\WP_Upgrader' ) )
                require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
index a3bc6cd..449a19e 100644 (file)
@@ -17,18 +17,22 @@ WP_CLI::init();
 
 WP_CLI::$runner->before_wp_load();
 
-// Load WordPress, in the global scope
+// Load wp-config.php code, in the global scope
 eval( WP_CLI::$runner->get_wp_config_code() );
 
 WP_CLI::$runner->after_wp_config_load();
 
+// Load main WordPress code, in the global scope
 require WP_CLI_ROOT . 'wp-settings-cli.php';
 
 // Fix memory limit. See http://core.trac.wordpress.org/ticket/14889
 @ini_set( 'memory_limit', -1 );
 
-// Load all admin utilities
+// Simulate a /wp-admin/ page load
+$_SERVER['PHP_SELF'] = '/wp-admin/index.php';
+define( 'WP_ADMIN', true );
 require ABSPATH . 'wp-admin/includes/admin.php';
+do_action( 'admin_init' );
 
 WP_CLI::$runner->after_wp_load();