Skip to content

Commit

Permalink
Show active theme as source when no sources found
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Sep 22, 2018
1 parent 620a8aa commit 3be5767
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
52 changes: 37 additions & 15 deletions includes/validation/class-amp-invalid-url-post-type.php
Expand Up @@ -877,7 +877,7 @@ public static function output_custom_column( $column_name, $post_id ) {
}
break;
case AMP_Validation_Error_Taxonomy::SOURCES_INVALID_OUTPUT:
self::render_sources_column( $error_summary );
self::render_sources_column( $error_summary, $post_id );
break;
}
}
Expand All @@ -886,12 +886,19 @@ public static function output_custom_column( $column_name, $post_id ) {
* Renders the sources column on the the single error URL page and the 'Invalid URLs' page.
*
* @param array $error_summary The summary of errors.
* @param int $post_id The ID of the amp_invalid_url post.
*/
public static function render_sources_column( $error_summary ) {
public static function render_sources_column( $error_summary, $post_id ) {
if ( ! isset( $error_summary[ AMP_Validation_Error_Taxonomy::SOURCES_INVALID_OUTPUT ] ) ) {
return;
}

$active_theme = null;
$validated_environment = get_post_meta( $post_id, '_amp_validated_environment', true );
if ( isset( $validated_environment['theme'] ) ) {
$active_theme = $validated_environment['theme'];
}

$sources = $error_summary[ AMP_Validation_Error_Taxonomy::SOURCES_INVALID_OUTPUT ];
$output = array();

Expand All @@ -911,7 +918,7 @@ public static function render_sources_column( $error_summary ) {
}
$count = count( $plugin_names );
if ( 1 === $count ) {
$output[] = sprintf( '<span class="details-attributes__summary"><strong><span class="dashicons dashicons-admin-plugins"></span>%s</strong></span>', esc_html( $plugin_names[0] ) );
$output[] = sprintf( '<strong class="source"><span class="dashicons dashicons-admin-plugins"></span>%s</strong>', esc_html( $plugin_names[0] ) );
} else {
$output[] = '<details class="source">';
$output[] = sprintf( '<summary class="details-attributes__summary"><strong><span class="dashicons dashicons-admin-plugins"></span>%s (%d)</strong></summary>', esc_html__( 'Plugins', 'amp' ), $count );
Expand All @@ -921,11 +928,26 @@ public static function render_sources_column( $error_summary ) {
$output[] = '</details>';
}
}
if ( isset( $sources['theme'] ) ) {
$output[] = '<div class="source">';
$output[] = '<span class="dashicons dashicons-admin-appearance"></span>';
$themes = array_unique( $sources['theme'] );
foreach ( $themes as $theme_slug ) {
$theme_obj = wp_get_theme( $theme_slug );
if ( ! $theme_obj->errors() ) {
$theme_name = $theme_obj->get( 'Name' );
} else {
$theme_name = $theme_slug;
}
$output[] = sprintf( '<strong>%s</strong>', esc_html( $theme_name ) );
}
$output[] = '</div>';
}
if ( isset( $sources['core'] ) ) {
$core_sources = array_unique( $sources['core'] );
$count = count( $core_sources );
if ( 1 === $count ) {
$output[] = sprintf( '<span class="details-attributes__summary"><strong><span class="dashicons dashicons-wordpress-alt"></span>%s</strong></span>', esc_html( $core_sources[0] ) );
$output[] = sprintf( '<strong class="source"><span class="dashicons dashicons-wordpress-alt"></span>%s</strong>', esc_html( $core_sources[0] ) );
} else {
$output[] = '<details class="source">';
$output[] = sprintf( '<summary class="details-attributes__summary"><strong><span class="dashicons dashicons-wordpress-alt"></span>%s (%d)</strong></summary>', esc_html__( 'Other', 'amp' ), $count );
Expand All @@ -935,21 +957,21 @@ public static function render_sources_column( $error_summary ) {
$output[] = '</details>';
}
}
if ( isset( $sources['theme'] ) ) {

if ( empty( $sources ) && $active_theme ) {
$theme_obj = wp_get_theme( $active_theme );
if ( ! $theme_obj->errors() ) {
$theme_name = $theme_obj->get( 'Name' );
} else {
$theme_name = $active_theme;
}
$output[] = '<div class="source">';
$output[] = '<span class="dashicons dashicons-admin-appearance"></span>';
$themes = array_unique( $sources['theme'] );
foreach ( $themes as $theme_slug ) {
$theme_obj = wp_get_theme( $theme_slug );
if ( ! $theme_obj->errors() ) {
$theme_name = $theme_obj->get( 'Name' );
} else {
$theme_name = $theme_slug;
}
$output[] = sprintf( '<strong>%s</strong><br/>', esc_html( $theme_name ) );
}
/* translators: %s is the guessed theme as the source for the error */
$output[] = esc_html( sprintf( __( '%s (?)', 'amp' ), $theme_name ) );
$output[] = '</div>';
}

echo implode( '', $output ); // WPCS: XSS ok.
}

Expand Down
5 changes: 3 additions & 2 deletions includes/validation/class-amp-validation-error-taxonomy.php
Expand Up @@ -1810,12 +1810,13 @@ public static function filter_manage_custom_columns( $content, $column_name, $te
if ( ! isset( $_GET['post'], $_GET['action'] ) || 'edit' !== $_GET['action'] ) { // WPCS: CSRF OK.
break;
}
$validation_errors = AMP_Invalid_URL_Post_Type::get_invalid_url_validation_errors( intval( $_GET['post'] ) ); // WPCS: CSRF OK.
$url_post_id = intval( $_GET['post'] ); // WPCS: CSRF OK.
$validation_errors = AMP_Invalid_URL_Post_Type::get_invalid_url_validation_errors( $url_post_id );
$validation_errors = array_filter( $validation_errors, function( $error ) use ( $term ) {
return $error['term']->term_id === $term->term_id;
} );
$error_summary = self::summarize_validation_errors( wp_list_pluck( $validation_errors, 'data' ) );
AMP_Invalid_URL_Post_Type::render_sources_column( $error_summary );
AMP_Invalid_URL_Post_Type::render_sources_column( $error_summary, $url_post_id );

break;
case 'error_type':
Expand Down

0 comments on commit 3be5767

Please sign in to comment.