Skip to content

Commit

Permalink
Check validity when switching between template mode and show results
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Sep 22, 2018
1 parent 199436e commit 75e6905
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 37 deletions.
143 changes: 135 additions & 8 deletions includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static function validate_options( $new_options ) {

// If this option was changed, display a notice with the new template mode.
if ( self::get_option( 'theme_support' ) !== $new_options['theme_support'] ) {
self::add_updated_theme_support_message( $new_options['theme_support'] );
add_action( 'update_option_' . self::OPTION_NAME, array( __CLASS__, 'handle_updated_theme_support_option' ) );
}
}

Expand Down Expand Up @@ -432,24 +432,151 @@ public static function show_response_cache_disabled_notice() {

/**
* Adds a message for an update of the theme support setting.
*
* @param string $template_mode The template mode.
*/
public static function add_updated_theme_support_message( $template_mode ) {
public static function handle_updated_theme_support_option() {
$template_mode = self::get_option( 'theme_support' );

// Make sure post type support has been added for sake of amp_admin_get_preview_permalink().
foreach ( AMP_Post_Type_Support::get_eligible_post_types() as $post_type ) {
remove_post_type_support( $post_type, amp_get_slug() );
}
AMP_Post_Type_Support::add_post_type_support();

// Ensure theme support flags are set properly according to the new mode so that proper AMP URL can be generated.
$has_theme_support = ( 'native' === $template_mode || 'paired' === $template_mode );
if ( $has_theme_support ) {
$theme_support = current_theme_supports( 'amp' );
if ( ! is_array( $theme_support ) ) {
$theme_support = array();
}
$theme_support['paired'] = 'paired' === $template_mode;
add_theme_support( 'amp', $theme_support );
}

$url = amp_admin_get_preview_permalink();

$notice_type = 'updated';
$review_messages = array();
if ( $url && $has_theme_support ) {
$validation = AMP_Validation_Manager::validate_url( $url );

if ( is_wp_error( $validation ) ) {
$review_messages[] = esc_html( sprintf(
/* translators: %1$s is the error message, %2$s is the error code */
__( 'However, there was an error when checking the AMP validity for your site: %1$s (code: %2$s) ', 'amp' ),
$validation->get_error_message(),
$validation->get_error_code()
) );

$notice_type = 'error';
} elseif ( is_array( $validation ) ) {
$new_errors = 0;
$rejected_errors = 0;

$errors = wp_list_pluck( $validation['results'], 'error' );
foreach ( $errors as $error ) {
$sanitization = AMP_Validation_Error_Taxonomy::get_validation_error_sanitization( $error );
$is_new_rejected = AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS === $sanitization['status'];
if ( $is_new_rejected || AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_ACCEPTED_STATUS === $sanitization['status'] ) {
$new_errors++;
}
if ( $is_new_rejected || AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_REJECTED_STATUS === $sanitization['status'] ) {
$rejected_errors++;
}
}

$invalid_url_post_id = AMP_Invalid_URL_Post_Type::store_validation_errors( $errors, $url );
$invalid_url_screen_url = ! is_wp_error( $invalid_url_post_id ) ? get_edit_post_link( $invalid_url_post_id, 'raw' ) : null;

if ( $rejected_errors > 0 ) {
$notice_type = 'error';

$message = wp_kses_post(
sprintf(
/* translators: %s is count of rejected errors */
_n(
'However, AMP is not yet available due to %s validation error (for one URL at least).',
'However, AMP is not yet available due to %s validation errors (for one URL at least).',
number_format_i18n( $rejected_errors ),
'amp'
),
$rejected_errors,
esc_url( $invalid_url_screen_url )
)
);

if ( $invalid_url_screen_url ) {
$message .= ' ' . wp_kses_post(
sprintf(
/* translators: %s is URL to review issues */
_n(
'<a href="%s">Review Issue</a>.',
'<a href="%s">Review Issues</a>.',
$rejected_errors,
'amp'
),
esc_url( $invalid_url_screen_url )
)
);
}

$review_messages[] = $message;
} else {
$message = wp_kses_post(
sprintf(
/* translators: %s is an AMP URL */
__( 'View an <a href="%s">AMP version of your site</a>.', 'amp' ),
esc_url( $url )
)
);

if ( $new_errors > 0 && $invalid_url_screen_url ) {
$message .= ' ' . wp_kses_post(
sprintf(
/* translators: %1$s is URL to review issues, %2$s is count of new errors */
_n(
'Please also <a href="%1$s">review %2$s issue</a> which may need to be fixed (for one URL at least).',
'Please also <a href="%1$s">review %2$s issues</a> which may need to be fixed (for one URL at least).',
$new_errors,
'amp'
),
esc_url( $invalid_url_screen_url ),
number_format_i18n( $new_errors )
)
);
}

$review_messages[] = $message;
}
}
}

switch ( $template_mode ) {
case 'native':
$message = esc_html__( 'Native Mode activated! View your site as AMP now or Review Errors', 'amp' );
$message = esc_html__( 'Native mode activated!', 'amp' );
if ( $review_messages ) {
$message .= ' ' . join( ' ', $review_messages );
}
break;
case 'paired':
$message = esc_html__( 'Paired Mode activated! View your site as AMP now or Review Errors', 'amp' );
$message = esc_html__( 'Paired mode activated!', 'amp' );
if ( $review_messages ) {
$message .= ' ' . join( ' ', $review_messages );
}
break;
case 'disabled':
$message = esc_html__( 'Classic Mode activated! View your site as AMP now. We recommend upgrading to Native or Paired mode.', 'amp' );
$message = wp_kses_post(
sprintf(
/* translators: %s is an AMP URL */
__( 'Classic mode activated! View the <a href="%s">AMP version of a recent post</a>. It is recommended that you upgrade to Native or Paired mode.', 'amp' ),
esc_url( $url )
)
);
break;
}

if ( isset( $message ) ) {
add_settings_error( self::OPTION_NAME, 'template_mode_updated', $message, 'updated' );
add_settings_error( self::OPTION_NAME, 'template_mode_updated', $message, $notice_type );
}
}
}
168 changes: 139 additions & 29 deletions tests/test-class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,43 +420,153 @@ public function test_render_cache_miss_notice() {
}

/**
* Data for test_add_updated_theme_support_message.
* Test handle_updated_theme_support_option for classic.
*
* @return array
* @covers AMP_Options_Manager::handle_updated_theme_support_option()
* @covers \amp_admin_get_preview_permalink()
*/
public function get_template_mode_message() {
return array(
'Invalid template mode' => array(
'foo_invalid_mode',
'',
),
'Native Mode' => array(
'native',
'Native Mode activated! View your site as AMP now or Review Errors',
),
'Paired Mode' => array(
'paired',
'Paired Mode activated! View your site as AMP now or Review Errors',
),
'Classic Mode (Disabled)' => array(
'disabled',
'Classic Mode activated! View your site as AMP now. We recommend upgrading to Native or Paired mode.',
),
);
public function test_handle_updated_theme_support_option_disabled() {
wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) );
AMP_Validation_Manager::init();

$page_id = $this->factory()->post->create( array( 'post_type' => 'page' ) );
AMP_Options_Manager::update_option( 'supported_post_types', array( 'page' ) );
AMP_Options_Manager::update_option( 'theme_support', 'disabled' );
AMP_Options_Manager::handle_updated_theme_support_option();
$amp_settings_errors = get_settings_errors( AMP_Options_Manager::OPTION_NAME );
$new_error = end( $amp_settings_errors );
$this->assertStringStartsWith( 'Classic mode activated!', $new_error['message'] );
$this->assertContains( esc_url( amp_get_permalink( $page_id ) ), $new_error['message'], 'Expect amp_admin_get_preview_permalink() to return a page since it is the only post type supported.' );
$this->assertCount( 0, get_posts( array( 'post_type' => AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG ) ) );
}

/**
* Test handle_updated_theme_support_option for native when there is one auto-accepted issue.
*
* @covers AMP_Options_Manager::handle_updated_theme_support_option()
* @covers \amp_admin_get_preview_permalink()
*/
public function test_handle_updated_theme_support_option_native_success_but_error() {
wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) );

$post_id = $this->factory()->post->create( array( 'post_type' => 'post' ) );
AMP_Options_Manager::update_option( 'theme_support', 'native' );
AMP_Options_Manager::update_option( 'supported_post_types', array( 'post' ) );

$filter = function() {
$validation = array(
'results' => array(
array(
'error' => array( 'code' => 'example' ),
'sanitized' => false,
),
),
);
return array(
'body' => sprintf(
'<html amp><head></head><body></body><!--%s--></html>',
'AMP_VALIDATION:' . wp_json_encode( $validation )
),
);
};
add_filter( 'pre_http_request', $filter, 10, 3 );
AMP_Options_Manager::handle_updated_theme_support_option();
remove_filter( 'pre_http_request', $filter );
$amp_settings_errors = get_settings_errors( AMP_Options_Manager::OPTION_NAME );
$new_error = end( $amp_settings_errors );
$this->assertStringStartsWith( 'Native mode activated!', $new_error['message'] );
$this->assertContains( esc_url( amp_get_permalink( $post_id ) ), $new_error['message'], 'Expect amp_admin_get_preview_permalink() to return a post since it is the only post type supported.' );
$invalid_url_posts = get_posts( array(
'post_type' => AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG,
'fields' => 'ids',
) );
$this->assertEquals( 'updated', $new_error['type'] );
$this->assertCount( 1, $invalid_url_posts );
$this->assertContains( 'review 1 issue', $new_error['message'] );
$this->assertContains( esc_url( get_edit_post_link( $invalid_url_posts[0], 'raw' ) ), $new_error['message'], 'Expect edit post link for the invalid URL post to be present.' );
}

/**
* Test add_updated_theme_support_message.
* Test handle_updated_theme_support_option for native when there is one auto-accepted issue.
*
* @dataProvider get_template_mode_message
* @param string $template_mode The template mode.
* @param string $expected_message The expected message for the template mode.
* @covers AMP_Options_Manager::add_updated_theme_support_message()
* @covers AMP_Options_Manager::handle_updated_theme_support_option()
* @covers \amp_admin_get_preview_permalink()
*/
public function test_add_updated_theme_support_message( $template_mode, $expected_message ) {
AMP_Options_Manager::add_updated_theme_support_message( $template_mode );
public function test_handle_updated_theme_support_option_native_validate_error() {
wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) );
$this->factory()->post->create( array( 'post_type' => 'post' ) );

AMP_Options_Manager::update_option( 'theme_support', 'native' );
AMP_Options_Manager::update_option( 'supported_post_types', array( 'post' ) );

$filter = function() {
return array(
'body' => '<html amp><head></head><body></body>',
);
};
add_filter( 'pre_http_request', $filter, 10, 3 );
AMP_Options_Manager::handle_updated_theme_support_option();
remove_filter( 'pre_http_request', $filter );

$amp_settings_errors = get_settings_errors( AMP_Options_Manager::OPTION_NAME );
$new_error = end( $amp_settings_errors );
$this->assertEquals( $expected_message, $new_error['message'] );
$this->assertStringStartsWith( 'Native mode activated!', $new_error['message'] );
$invalid_url_posts = get_posts( array(
'post_type' => AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG,
'fields' => 'ids',
) );
$this->assertCount( 0, $invalid_url_posts );
$this->assertContains( 'response_comment_absent', $new_error['message'] );
$this->assertEquals( 'error', $new_error['type'] );
}

/**
* Test handle_updated_theme_support_option for classic.
*
* @covers AMP_Options_Manager::handle_updated_theme_support_option()
* @covers \amp_admin_get_preview_permalink()
*/
public function test_handle_updated_theme_support_option_paired() {
wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) );

$post_id = $this->factory()->post->create( array( 'post_type' => 'post' ) );
AMP_Options_Manager::update_option( 'theme_support', 'paired' );
AMP_Options_Manager::update_option( 'supported_post_types', array( 'post' ) );

$filter = function() {
$validation = array(
'results' => array(
array(
'error' => array( 'code' => 'foo' ),
'sanitized' => false,
),
array(
'error' => array( 'code' => 'bar' ),
'sanitized' => false,
),
),
);
return array(
'body' => sprintf(
'<html amp><head></head><body></body><!--%s--></html>',
'AMP_VALIDATION:' . wp_json_encode( $validation )
),
);
};
add_filter( 'pre_http_request', $filter, 10, 3 );
AMP_Options_Manager::handle_updated_theme_support_option();
remove_filter( 'pre_http_request', $filter );
$amp_settings_errors = get_settings_errors( AMP_Options_Manager::OPTION_NAME );
$new_error = end( $amp_settings_errors );
$this->assertStringStartsWith( 'Paired mode activated!', $new_error['message'] );
$this->assertContains( esc_url( amp_get_permalink( $post_id ) ), $new_error['message'], 'Expect amp_admin_get_preview_permalink() to return a post since it is the only post type supported.' );
$invalid_url_posts = get_posts( array(
'post_type' => AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG,
'fields' => 'ids',
) );
$this->assertEquals( 'updated', $new_error['type'] );
$this->assertCount( 1, $invalid_url_posts );
$this->assertContains( 'review 2 issues', $new_error['message'] );
$this->assertContains( esc_url( get_edit_post_link( $invalid_url_posts[0], 'raw' ) ), $new_error['message'], 'Expect edit post link for the invalid URL post to be present.' );
}
}
1 change: 1 addition & 0 deletions tests/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ public function test_get_template_availability() {
$this->assertEquals( 'is_special', $availability['template'] );
$this->assertFalse( $availability['immutable'] );

remove_post_type_support( 'page', 'amp' );
$availability = AMP_Theme_Support::get_template_availability( $this->factory()->post->create_and_get( array( 'post_type' => 'page' ) ) );
$this->assertFalse( $availability['supported'] );
$this->assertEquals( array( 'post-type-support' ), $availability['errors'] );
Expand Down

0 comments on commit 75e6905

Please sign in to comment.