Skip to content

Commit

Permalink
Issue #842: Handle 'Closure' callbacks in get_plugin().
Browse files Browse the repository at this point in the history
Also, address a Travis error.
Instead of testing for the presence of 'amp-wp,'
simply make it 'amp.'
This plugin seems to be in the directory 'amp-wp'
in the Travis tests.
  • Loading branch information
Ryan Kienstra committed Feb 19, 2018
1 parent 0d5e50d commit f0c864c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
11 changes: 6 additions & 5 deletions includes/utils/class-amp-validation-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,21 +358,22 @@ public static function callback_wrappers() {
* @return string|null $plugin The plugin to which the callback belongs, or null.
*/
public static function get_plugin( $callback ) {
// The $callback is a function or static method.
if ( is_string( $callback ) && is_callable( $callback ) ) {
// The $callback is a function or static method.
$exploded_callback = explode( '::', $callback );
if ( count( $exploded_callback ) > 1 ) {
$reflection = new ReflectionClass( $exploded_callback[0] );
$file = $reflection->getFileName();
} else {
$reflection = new ReflectionFunction( $callback );
$file = $reflection->getFileName();
}
} elseif ( isset( $callback[0], $callback[1] ) && method_exists( $callback[0], $callback[1] ) ) {
} elseif ( is_array( $callback ) && isset( $callback[0], $callback[1] ) && method_exists( $callback[0], $callback[1] ) ) {
// The $callback is a method.
$reflection = new ReflectionClass( $callback[0] );
$file = $reflection->getFileName();
} elseif ( is_object( $callback ) && ( 'Closure' === get_class( $callback ) ) ) {
$reflection = new ReflectionFunction( $callback );
}

$file = isset( $reflection ) ? $reflection->getFileName() : null;
if ( ! isset( $file ) ) {
return null;
}
Expand Down
17 changes: 9 additions & 8 deletions tests/test-class-amp-validation-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,16 +436,16 @@ public function test_callback_wrappers() {
do_action( $action_no_argument );
$output = ob_get_clean();
$this->assertContains( '<div></div>', $output );
$this->assertContains( '<!--before:amp-->', $output );
$this->assertContains( '<!--after:amp-->', $output );
$this->assertContains( '<!--before:amp', $output );
$this->assertContains( '<!--after:amp', $output );

ob_start();
do_action( $action_one_argument, $notice );
$output = ob_get_clean();
$this->assertContains( $notice, $output );
$this->assertContains( sprintf( '<div class="notice notice-warning"><p>%s</p></div>', $notice ), $output );
$this->assertContains( '<!--before:amp-->', $output );
$this->assertContains( '<!--after:amp-->', $output );
$this->assertContains( '<!--before:amp', $output );
$this->assertContains( '<!--after:amp', $output );

ob_start();
do_action( $action_two_arguments, $notice, get_the_ID() );
Expand All @@ -454,8 +454,8 @@ public function test_callback_wrappers() {
self::output_message( $notice, get_the_ID() );
$expected_output = ob_get_clean();
$this->assertContains( $expected_output, $output );
$this->assertContains( '<!--before:amp-->', $output );
$this->assertContains( '<!--after:amp-->', $output );
$this->assertContains( '<!--before:amp', $output );
$this->assertContains( '<!--after:amp', $output );

// This action's callback isn't from a plugin, so it shouldn't be wrapped in comments.
ob_start();
Expand Down Expand Up @@ -501,10 +501,11 @@ public function test_wrapped_callback() {
call_user_func( $wrapped_callback );
$output = ob_get_clean();

$this->assertEquals( 'Closure', get_class( $wrapped_callback ) );
$this->assertTrue( is_object( $wrapped_callback ) );
$this->assertContains( strval( get_the_ID() ), $output );
$this->assertContains( '<!--before:amp-->', $output );
$this->assertContains( '<!--after:amp-->', $output );
$this->assertContains( '<!--before:amp', $output );
$this->assertContains( '<!--after:amp', $output );
unset( $post );
}
/**
Expand Down

0 comments on commit f0c864c

Please sign in to comment.