Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix obtaining source for widgets #1212

Merged
merged 1 commit into from
Jun 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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