Skip to content

Commit

Permalink
Fix obtaining source for widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 14, 2018
1 parent 764236d commit a534804
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 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,21 @@ 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();

$reflection = new ReflectionMethod( $callback[0], $callback[1] );
} elseif ( is_object( $callback ) && ( 'Closure' === get_class( $callback ) ) ) {
$reflection = new ReflectionFunction( $callback );
}
if ( ! $file ) {
$file = $reflection->getFileName();
}
} catch ( Exception $e ) {
return null;
}
Expand All @@ -1107,7 +1120,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 a534804

Please sign in to comment.