Skip to content

Commit

Permalink
Fix identifying sources for validation errors coming child themes (#3708
Browse files Browse the repository at this point in the history
)

* Remove dead code after #3505

* Fix matching file paths for child themes
  • Loading branch information
westonruter committed Nov 11, 2019
1 parent b0bd40c commit fe2cd4e
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions includes/validation/class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1426,31 +1426,21 @@ public static function decorate_filter_source( $value ) {
* @type string $type Source type (core, plugin, mu-plugin, or theme).
* @type string $name Source name.
* @type string $function Normalized function name.
* @type ReflectionMethod|ReflectionFunction $reflection
* @type ReflectionMethod|ReflectionFunction $reflection Reflection.
* }
*/
public static function get_source( $callback ) {
$reflection = null;
$class_name = null; // Because ReflectionMethod::getDeclaringClass() can return a parent class.
try {
if ( is_string( $callback ) && is_callable( $callback ) ) {
// The $callback is a function or static method.
$exploded_callback = explode( '::', $callback, 2 );
if ( 2 === count( $exploded_callback ) ) {
$class_name = $exploded_callback[0];
$reflection = new ReflectionMethod( $exploded_callback[0], $exploded_callback[1] );
} else {
$reflection = new ReflectionFunction( $callback );
}
} elseif ( is_array( $callback ) && isset( $callback[0], $callback[1] ) && method_exists( $callback[0], $callback[1] ) ) {
// The $callback is a method.
if ( is_string( $callback[0] ) ) {
$class_name = $callback[0];
} elseif ( is_object( $callback[0] ) ) {
$class_name = get_class( $callback[0] );
}

// This is needed later for AMP_Validation_Manager::has_parameters_passed_by_reference().
$reflection = new ReflectionMethod( $callback[0], $callback[1] );

// Handle the special case of the class being a widget, in which case the display_callback method should
Expand All @@ -1465,6 +1455,7 @@ public static function get_source( $callback ) {
return null;
}

// The reflection is needed later for AMP_Validation_Manager::has_parameters_passed_by_reference().
if ( ! $reflection ) {
return null;
}
Expand All @@ -1482,7 +1473,7 @@ public static function get_source( $callback ) {
$source['type'] = 'theme';
$source['name'] = self::$template_slug;
$source['file'] = $matches['file'];
} elseif ( ! empty( self::$stylesheet_directory ) && preg_match( ':' . preg_quote( trailingslashit( self::$stylesheet_directory ), ':' ) . '/(?P<file>.*$):s', $file, $matches ) ) {
} elseif ( ! empty( self::$stylesheet_directory ) && preg_match( ':' . preg_quote( trailingslashit( self::$stylesheet_directory ), ':' ) . '(?P<file>.*$):s', $file, $matches ) ) {
$source['type'] = 'theme';
$source['name'] = self::$stylesheet_slug;
$source['file'] = $matches['file'];
Expand All @@ -1501,8 +1492,8 @@ public static function get_source( $callback ) {
$source['line'] = $reflection->getStartLine();
}

if ( $class_name ) {
$source['function'] = $class_name . '::' . $reflection->getName();
if ( $reflection instanceof ReflectionMethod ) {
$source['function'] = $reflection->getDeclaringClass()->getName() . '::' . $reflection->getName();
} else {
$source['function'] = $reflection->getName();
}
Expand Down

0 comments on commit fe2cd4e

Please sign in to comment.