Skip to content

Commit

Permalink
Merge pull request #1212 from Automattic/fix/widget-source
Browse files Browse the repository at this point in the history
Fix obtaining source for widgets
  • Loading branch information
westonruter committed Jun 14, 2018
2 parents 9461da6 + b811dca commit 014e0a0
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion includes/validation/class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ public static function wrap_widget_callbacks() {
continue;
}
$source['widget_id'] = $widget_id;
unset( $source['reflection'] ); // Omit from stored source.

$function = $registered_widget['callback'];
$accepted_args = 2; // For the $instance and $args arguments.
Expand Down Expand Up @@ -1076,6 +1077,7 @@ public static function decorate_filter_source( $value ) {
public static function get_source( $callback ) {
$reflection = null;
$class_name = null; // Because ReflectionMethod::getDeclaringClass() can return a parent class.
$file = null;
try {
if ( is_string( $callback ) && is_callable( $callback ) ) {
// The $callback is a function or static method.
Expand All @@ -1093,10 +1095,22 @@ public static function get_source( $callback ) {
} elseif ( is_object( $callback[0] ) ) {
$class_name = get_class( $callback[0] );
}

/*
* Obtain file from ReflectionClass because if the method is not on base class then
* file returned by ReflectionMethod will be for the base class not the subclass.
*/
$reflection = new ReflectionClass( $callback[0] );
$file = $reflection->getFileName();

// This is needed later for AMP_Validation_Manager::has_parameters_passed_by_reference().
$reflection = new ReflectionMethod( $callback[0], $callback[1] );
} elseif ( is_object( $callback ) && ( 'Closure' === get_class( $callback ) ) ) {
$reflection = new ReflectionFunction( $callback );
}
if ( $reflection && ! $file ) {
$file = $reflection->getFileName();
}
} catch ( Exception $e ) {
return null;
}
Expand All @@ -1107,7 +1121,6 @@ public static function get_source( $callback ) {

$source = compact( 'reflection' );

$file = $reflection->getFileName();
if ( $file ) {
$file = wp_normalize_path( $file );
$slug_pattern = '([^/]+)';
Expand Down

0 comments on commit 014e0a0

Please sign in to comment.