Skip to content

Commit

Permalink
Introduce "Flagged" status for validation errors which are Rejected b…
Browse files Browse the repository at this point in the history
…ut which are still forcibly sanitized

* Fix PHP 5.3 error
* Remove automatically setting amp_validation_error term_group based on forced status.
* Let preview URL include development=1 flag.
  • Loading branch information
westonruter committed Jun 8, 2018
1 parent f864d67 commit d890b6c
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 101 deletions.
7 changes: 5 additions & 2 deletions includes/options/class-amp-options-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,15 @@ public function render_validation_handling() {
?>
<fieldset>
<?php
$forced_sanitization = AMP_Validation_Error_Taxonomy::is_validation_error_sanitized( array(
$auto_sanitization = AMP_Validation_Error_Taxonomy::get_validation_error_sanitization( array(
'code' => 'non_existent',
) );
$forced_tree_shaking = $forced_sanitization || AMP_Validation_Error_Taxonomy::is_validation_error_sanitized( array(
$tree_shaking_sanitization = AMP_Validation_Error_Taxonomy::get_validation_error_sanitization( array(
'code' => AMP_Style_Sanitizer::TREE_SHAKING_ERROR_CODE,
) );

$forced_sanitization = 'with_filter' === $auto_sanitization['forced'];
$forced_tree_shaking = $forced_sanitization || 'with_filter' === $tree_shaking_sanitization['forced'];
?>

<?php if ( $forced_sanitization ) : ?>
Expand Down
107 changes: 57 additions & 50 deletions includes/validation/class-amp-invalid-url-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,55 +232,62 @@ public static function get_invalid_url_validation_errors( $url, $args = array()
}

/**
* Get counts for the validation errors associated with a given invalid URL.
* Display summary of the validation error counts for a given post.
*
* @param int|WP_Post $post Post of amp_invalid_url type.
* @return array Term counts.
*/
public static function get_invalid_url_validation_error_counts( $post ) {
public static function display_invalid_url_validation_error_counts_summary( $post ) {
$counts = array_fill_keys(
array(
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_STATUS,
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACCEPTED_STATUS,
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_REJECTED_STATUS,
),
array( 'new', 'accepted', 'rejected', 'flagged' ),
0
);

$validation_errors = self::get_invalid_url_validation_errors( $post );
foreach ( $validation_errors as $error ) {
$counts[ $error['status'] ]++;
switch ( $error['term']->term_group ) {
case AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_STATUS:
$counts['new']++;
break;
case AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACCEPTED_STATUS:
$counts['accepted']++;
break;
case AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_REJECTED_STATUS:
if ( $error['forced'] ) {
$counts['flagged']++;
} else {
$counts['rejected']++;
}
break;
}
}
return $counts;
}

/**
* Display summary of the validation error counts for a given post.
*
* @param int|WP_Post $post Post of amp_invalid_url type.
*/
public static function display_invalid_url_validation_error_counts_summary( $post ) {
$result = array();
$counts = self::get_invalid_url_validation_error_counts( $post );
if ( $counts[ AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_STATUS ] ) {
if ( $counts['new'] ) {
$result[] = esc_html( sprintf(
/* translators: %s is count */
__( '&#x2753; New: %s', 'amp' ),
number_format_i18n( $counts[ AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_STATUS ] )
number_format_i18n( $counts['new'] )
) );
}
if ( $counts[ AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACCEPTED_STATUS ] ) {
if ( $counts['accepted'] ) {
$result[] = esc_html( sprintf(
/* translators: %s is count */
__( '&#x2705; Accepted: %s', 'amp' ),
number_format_i18n( $counts[ AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACCEPTED_STATUS ] )
number_format_i18n( $counts['accepted'] )
) );
}
if ( $counts[ AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_REJECTED_STATUS ] ) {
if ( $counts['flagged'] ) {
$result[] = esc_html( sprintf(
/* translators: %s is count */
__( '&#x1F6A9; Flagged: %s', 'amp' ),
number_format_i18n( $counts['flagged'] )
) );
}
if ( $counts['rejected'] ) {
$result[] = esc_html( sprintf(
/* translators: %s is count */
__( '&#x274C; Rejected: %s', 'amp' ),
number_format_i18n( $counts[ AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_REJECTED_STATUS ] )
number_format_i18n( $counts['rejected'] )
) );
}
echo implode( '<br>', $result ); // WPCS: xss ok.
Expand Down Expand Up @@ -364,17 +371,8 @@ public static function store_validation_errors( $validation_errors, $url, $post

$terms = array();
foreach ( $validation_errors as $data ) {
$sanitization = AMP_Validation_Error_Taxonomy::get_validation_error_sanitization( $data );
$term_data = AMP_Validation_Error_Taxonomy::prepare_validation_error_taxonomy_term( $data );
$term_slug = $term_data['slug'];

/*
* If the sanitization for the error is forced, make sure the term is created/updated with that group since
* the user will not be able to change it from NEW to ACCEPTED or REJECTED.
*/
if ( $sanitization['forced'] ) {
$term_data['term_group'] = $sanitization['status'];
}
$term_data = AMP_Validation_Error_Taxonomy::prepare_validation_error_taxonomy_term( $data );
$term_slug = $term_data['slug'];

if ( ! isset( $terms[ $term_slug ] ) ) {

Expand All @@ -388,8 +386,6 @@ public static function store_validation_errors( $validation_errors, $url, $post
$term_id = $r['term_id'];
update_term_meta( $term_id, 'created_date_gmt', current_time( 'mysql', true ) );
$term = get_term( $term_id );
} elseif ( isset( $term_data['term_group'] ) && $term->term_group !== $term_data['term_group'] ) {
wp_update_term( $term->term_id, AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG, wp_slash( $term_data ) );
}
$terms[ $term_slug ] = $term;
}
Expand All @@ -412,7 +408,7 @@ public static function store_validation_errors( $validation_errors, $url, $post
&&
$placeholder === $post_data['post_content']
&&
self::POST_TYPE_SLUG === $post_data['post_type']
AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG === $post_data['post_type']
);
if ( $should_supply_post_content ) {
$post_data['post_content'] = wp_slash( $post_content );
Expand Down Expand Up @@ -522,8 +518,8 @@ public static function filter_views_edit( $views ) {
sprintf(
/* translators: %s is the post count */
_nx(
'With Rejected Errors <span class="count">(%s)</span>',
'With Rejected Errors <span class="count">(%s)</span>',
'With Unacceptable Errors <span class="count">(%s)</span>',
'With Unacceptable Errors <span class="count">(%s)</span>',
$with_rejected_query->found_posts,
'posts',
'amp'
Expand Down Expand Up @@ -1117,6 +1113,7 @@ public static function print_validation_errors_meta_box( $post ) {
}
} );
validatePreviewUrl += '&' + $.param( params );
validatePreviewUrl += '#development=1';
window.open( validatePreviewUrl, 'amp-validation-error-term-status-preview-' + String( postId ) );
} );
} );
Expand All @@ -1134,7 +1131,7 @@ public static function print_validation_errors_meta_box( $post ) {

<?php if ( $has_forced_sanitized ) : ?>
<div class="notice notice-info notice-alt inline">
<p><?php esc_html_e( 'This site is configured to automatically decide whether or not some validation errors may be accepted. For these errors, you cannot change their status below.', 'amp' ); ?></p>
<p>&#x1F6A9; <?php esc_html_e( 'Flagged errors will still be sanitized in the response (like accepted errors) but they are marked because you intend to fix them later.', 'amp' ); ?></p>
</div>
<?php endif; ?>

Expand All @@ -1147,23 +1144,33 @@ public static function print_validation_errors_meta_box( $post ) {
$select_name = sprintf( '%s[%s]', AMP_Validation_Manager::VALIDATION_ERROR_TERM_STATUS_QUERY_VAR, $term->slug );
?>
<li>
<details <?php echo ( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_STATUS === $error['status'] ) ? 'open' : ''; ?>>
<details <?php echo ( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_STATUS === $error['term']->term_group ) ? 'open' : ''; ?>>
<summary>
<label for="<?php echo esc_attr( $select_name ); ?>" class="screen-reader-text">
<?php esc_html_e( 'Status:', 'amp' ); ?>
</label>
<select class="amp-validation-error-status" id="<?php echo esc_attr( $select_name ); ?>" name="<?php echo esc_attr( $select_name ); ?>" <?php disabled( $error['forced'] ); ?>>
<?php if ( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_STATUS === $error['status'] ) : ?>
<select class="amp-validation-error-status" id="<?php echo esc_attr( $select_name ); ?>" name="<?php echo esc_attr( $select_name ); ?>">
<?php if ( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_STATUS === $error['term']->term_group ) : ?>
<option value=""><?php esc_html_e( 'New', 'amp' ); ?></option>
<?php endif; ?>
<option value="<?php echo esc_attr( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACCEPTED_STATUS ); ?>" <?php selected( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACCEPTED_STATUS, $error['status'] ); ?>><?php esc_html_e( 'Accepted', 'amp' ); ?></option>
<option value="<?php echo esc_attr( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_REJECTED_STATUS ); ?>" <?php selected( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_REJECTED_STATUS, $error['status'] ); ?>><?php esc_html_e( 'Rejected', 'amp' ); ?></option>
<option value="<?php echo esc_attr( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACCEPTED_STATUS ); ?>" <?php selected( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACCEPTED_STATUS, $error['term']->term_group ); ?>><?php esc_html_e( 'Accepted', 'amp' ); ?></option>
<option value="<?php echo esc_attr( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_REJECTED_STATUS ); ?>" <?php selected( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_REJECTED_STATUS, $error['term']->term_group ); ?>>
<?php if ( $error['forced'] ) : ?>
<?php esc_html_e( 'Flagged', 'amp' ); ?>
<?php else : ?>
<?php esc_html_e( 'Rejected', 'amp' ); ?>
<?php endif; ?>
</option>
</select>
<?php if ( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_STATUS === $error['status'] ) : ?>
<?php if ( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_STATUS === $error['term']->term_group ) : ?>
&#x2753;
<?php elseif ( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_REJECTED_STATUS === $error['status'] ) : ?>
&#x274C;
<?php elseif ( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACCEPTED_STATUS === $error['status'] ) : ?>
<?php elseif ( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_REJECTED_STATUS === $error['term']->term_group ) : ?>
<?php if ( $error['forced'] ) : ?>
&#x1F6A9;
<?php else : ?>
&#x274C;
<?php endif; ?>
<?php elseif ( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACCEPTED_STATUS === $error['term']->term_group ) : ?>
&#x2705;
<?php endif; ?>
<code><?php echo esc_html( $error['data']['code'] ); ?></code>
Expand Down
Loading

0 comments on commit d890b6c

Please sign in to comment.