OSDN Git Service

add our own wp_die() handler
authorscribu <mail@scribu.net>
Fri, 8 Feb 2013 00:28:07 +0000 (02:28 +0200)
committerscribu <mail@scribu.net>
Fri, 8 Feb 2013 00:28:07 +0000 (02:28 +0200)
php/utils-wp.php
php/wp-settings-cli.php
tests/spec-core.php

index 0fb19ff..4609617 100644 (file)
@@ -21,6 +21,25 @@ function wp_debug_mode() {
        }
 }
 
+function replace_wp_die_handler() {
+       \remove_filter( 'wp_die_handler', '_default_wp_die_handler' );
+       \add_filter( 'wp_die_handler', function() { return __NAMESPACE__ . '\\' . 'wp_die_handler'; } );
+}
+
+function wp_die_handler( $message ) {
+       if ( is_wp_error( $message ) ) {
+               $message = $message->get_error_message();
+       }
+
+       if ( preg_match( '|^\<h1>(.+?)</h1>|', $message, $matches ) ) {
+               $message = $matches[1];
+       }
+
+       $message = html_entity_decode( $message );
+
+       \WP_CLI::error( $message );
+}
+
 function maybe_require( $since, $path ) {
        global $wp_version;
 
index 7855768..e58ea0d 100644 (file)
@@ -56,6 +56,8 @@ require( ABSPATH . WPINC . '/class-wp-error.php' );
 require( ABSPATH . WPINC . '/plugin.php' );
 require( ABSPATH . WPINC . '/pomo/mo.php' );
 
+Utils\replace_wp_die_handler();
+
 // Include the wpdb class and, if present, a db.php database drop-in.
 require_wp_db();
 
index 8913a44..95b3fce 100644 (file)
@@ -39,7 +39,7 @@ class CoreCommandSpec extends WP_CLI_Spec {
 
                        ->when( 'invoking', 'wp' )
                        ->then( 'return code should be', 1 )
-                       ->and( 'output should be', "Error: Can't connect to the database." )
+                       ->and( 'output should be', "Error: Can’t select database\n" )
 
                        ->when( 'invoking', 'db create' )
                        ->then( 'return code should be', 0 );