Skip to content

Commit

Permalink
Merge pull request #1408 from Automattic/update/1360-trash-text
Browse files Browse the repository at this point in the history
Admin amp_invalid_url table - replace 'trash' text
  • Loading branch information
westonruter committed Sep 7, 2018
2 parents 785afe6 + 4bf6713 commit 4015913
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 12 deletions.
113 changes: 107 additions & 6 deletions includes/validation/class-amp-invalid-url-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static function register() {
'menu_name' => __( 'Invalid Pages', 'amp' ),
'singular_name' => __( 'Invalid AMP Page (URL)', 'amp' ),
'not_found' => __( 'No invalid AMP pages found', 'amp' ),
'not_found_in_trash' => __( 'No invalid AMP pages in trash', 'amp' ),
'not_found_in_trash' => __( 'No forgotten invalid AMP pages', 'amp' ),
'search_items' => __( 'Search invalid AMP pages', 'amp' ),
'edit_item' => __( 'Invalid AMP Page (URL)', 'amp' ),
),
Expand Down Expand Up @@ -136,12 +136,15 @@ public static function add_admin_hooks() {
add_filter( 'manage_' . self::POST_TYPE_SLUG . '_posts_columns', array( __CLASS__, 'add_post_columns' ) );
add_action( 'manage_posts_custom_column', array( __CLASS__, 'output_custom_column' ), 10, 2 );
add_filter( 'post_row_actions', array( __CLASS__, 'filter_row_actions' ), 10, 2 );
add_filter( 'bulk_actions-edit-' . self::POST_TYPE_SLUG, array( __CLASS__, 'add_bulk_action' ), 10, 2 );
add_filter( 'bulk_actions-edit-' . self::POST_TYPE_SLUG, array( __CLASS__, 'filter_bulk_actions' ), 10, 2 );
add_filter( 'handle_bulk_actions-edit-' . self::POST_TYPE_SLUG, array( __CLASS__, 'handle_bulk_action' ), 10, 3 );
add_action( 'admin_notices', array( __CLASS__, 'print_admin_notice' ) );
add_action( 'admin_action_' . self::VALIDATE_ACTION, array( __CLASS__, 'handle_validate_request' ) );
add_action( 'post_action_' . self::UPDATE_POST_TERM_STATUS_ACTION, array( __CLASS__, 'handle_validation_error_status_update' ) );
add_action( 'admin_menu', array( __CLASS__, 'add_admin_menu_new_invalid_url_count' ) );
add_filter( 'post_row_actions', array( __CLASS__, 'filter_post_row_actions' ), 10, 2 );
add_filter( sprintf( 'views_edit-%s', self::POST_TYPE_SLUG ), array( __CLASS__, 'filter_table_views' ) );
add_filter( 'bulk_post_updated_messages', array( __CLASS__, 'filter_bulk_post_updated_messages' ), 10, 2 );

// Hide irrelevant "published" label in the invalid URL post list.
add_filter( 'post_date_column_status', function( $status, $post ) {
Expand Down Expand Up @@ -625,7 +628,7 @@ public static function filter_row_actions( $actions, $post ) {
$actions[ self::VALIDATE_ACTION ] = sprintf(
'<a href="%s">%s</a>',
esc_url( self::get_recheck_url( $post ) ),
esc_html__( 'Re-check', 'amp' )
esc_html__( 'Recheck', 'amp' )
);
if ( self::get_post_staleness( $post ) ) {
$actions[ self::VALIDATE_ACTION ] = sprintf( '<em>%s</em>', $actions[ self::VALIDATE_ACTION ] );
Expand All @@ -635,12 +638,20 @@ public static function filter_row_actions( $actions, $post ) {
}

/**
* Adds a 'Recheck' bulk action to the edit.php page.
* Adds a 'Recheck' bulk action to the edit.php page and modifies the 'Move to Trash' text.
*
* @param array $actions The bulk actions in the edit.php page.
* @return array $actions The filtered bulk actions.
*/
public static function add_bulk_action( $actions ) {
public static function filter_bulk_actions( $actions ) {
if ( isset( $actions['trash'] ) ) {
$actions['trash'] = esc_html__( 'Forget', 'amp' );
}

if ( isset( $actions['delete'] ) ) {
$actions['delete'] = esc_html__( 'Forget permanently', 'amp' );
}

unset( $actions['edit'] );
$actions[ self::BULK_VALIDATE_ACTION ] = esc_html__( 'Recheck', 'amp' );
return $actions;
Expand Down Expand Up @@ -1041,7 +1052,7 @@ public static function print_status_meta_box( $post ) {
<div id="minor-publishing-actions">
<div id="re-check-action">
<a class="button button-secondary" href="<?php echo esc_url( self::get_recheck_url( $post ) ); ?>">
<?php esc_html_e( 'Re-check', 'amp' ); ?>
<?php esc_html_e( 'Recheck', 'amp' ); ?>
</a>
</div>
<?php if ( ! ( AMP_Validation_Manager::is_sanitization_forcibly_accepted() || $is_sanitization_forcibly_accepted_by_filter ) ) : ?>
Expand Down Expand Up @@ -1493,4 +1504,94 @@ public static function print_dashboard_glance_styles() {
<?php
}

/**
* Filters post row actions.
*
* @param array $actions Row action links.
* @param \WP_Post $post Current WP post.
* @return array Filtered action links.
*/
public static function filter_post_row_actions( $actions, $post ) {
// Replace 'Trash' text with 'Forget'.
if ( isset( $actions['trash'] ) ) {
$actions['trash'] = sprintf(
'<a href="%s" class="submitdelete" aria-label="%s">%s</a>',
get_delete_post_link( $post->ID ),
/* translators: %s: post title */
esc_attr( sprintf( __( 'Forget &#8220;%s&#8221;', 'amp' ), $post->post_title ) ),
esc_html__( 'Forget', 'amp' )
);
}

if ( isset( $actions['delete'] ) ) {
$actions['delete'] = sprintf(
'<a href="%s" class="submitdelete" aria-label="%s">%s</a>',
get_delete_post_link( $post->ID, '', true ),
/* translators: %s: post title */
esc_attr( sprintf( __( 'Forget &#8220;%s&#8221; permanently', 'amp' ), $post->post_title ) ),
esc_html__( 'Forget Permanently', 'amp' )
);
}

return $actions;
}

/**
* Filters table views for the post type.
*
* @param array $views Array of table view links keyed by status slug.
* @return array Filtered views.
*/
public static function filter_table_views( $views ) {
// Replace 'Trash' text with 'Forgotten'.
if ( isset( $views['trash'] ) ) {
$status = get_post_status_object( 'trash' );

$views['trash'] = str_replace( $status->label, esc_html__( 'Forgotten', 'amp' ), $views['trash'] );
}

return $views;
}


/**
* Filters messages displayed after bulk updates.
*
* @param array $messages Bulk message text.
* @param array $bulk_counts Post numbers for the current message.
* @return array Filtered messages.
*/
public static function filter_bulk_post_updated_messages( $messages, $bulk_counts ) {
if ( get_current_screen()->id === sprintf( 'edit-%s', self::POST_TYPE_SLUG ) ) {
$messages['post'] = array_merge(
$messages['post'],
array(
/* translators: %s is the number of posts permanently forgotten */
'deleted' => _n(
'%s invalid AMP page permanently forgotten.',
'%s invalid AMP post permanently forgotten.',
$bulk_counts['deleted'],
'amp'
),
/* translators: %s is the number of posts forgotten */
'trashed' => _n(
'%s invalid AMP page forgotten.',
'%s invalid AMP pages forgotten.',
$bulk_counts['trashed'],
'amp'
),
/* translators: %s is the number of posts restored from trash. */
'untrashed' => _n(
'%s invalid AMP page unforgotten.',
'%s invalid AMP pages unforgotten.',
$bulk_counts['untrashed'],
'amp'
),
)
);
}

return $messages;
}

}
87 changes: 81 additions & 6 deletions tests/validation/test-class-amp-invalid-url-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function test_add_admin_hooks() {
$this->assertEquals( 10, has_filter( 'manage_' . AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG . '_posts_columns', array( self::TESTED_CLASS, 'add_post_columns' ) ) );
$this->assertEquals( 10, has_action( 'manage_posts_custom_column', array( self::TESTED_CLASS, 'output_custom_column' ) ) );
$this->assertEquals( 10, has_filter( 'post_row_actions', array( self::TESTED_CLASS, 'filter_row_actions' ) ) );
$this->assertEquals( 10, has_filter( 'bulk_actions-edit-' . AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG, array( self::TESTED_CLASS, 'add_bulk_action' ) ) );
$this->assertEquals( 10, has_filter( 'bulk_actions-edit-' . AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG, array( self::TESTED_CLASS, 'filter_bulk_actions' ) ) );
$this->assertEquals( 10, has_filter( 'handle_bulk_actions-edit-' . AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG, array( self::TESTED_CLASS, 'handle_bulk_action' ) ) );
$this->assertEquals( 10, has_action( 'admin_notices', array( self::TESTED_CLASS, 'print_admin_notice' ) ) );
$this->assertEquals( 10, has_action( 'admin_action_' . AMP_Invalid_URL_Post_Type::VALIDATE_ACTION, array( self::TESTED_CLASS, 'handle_validate_request' ) ) );
Expand Down Expand Up @@ -549,17 +549,21 @@ public function test_filter_row_actions() {
}

/**
* Test for add_bulk_action()
* Test for filter_bulk_actions()
*
* @covers \AMP_Invalid_URL_Post_Type::add_bulk_action()
* @covers \AMP_Invalid_URL_Post_Type::filter_bulk_actions()
*/
public function test_add_bulk_action() {
public function test_filter_bulk_actions() {
$initial_action = array(
'edit' => 'Edit',
'edit' => 'Edit',
'trash' => 'Trash',
'delete' => 'Trash permanently',
);
$actions = AMP_Invalid_URL_Post_Type::add_bulk_action( $initial_action );
$actions = AMP_Invalid_URL_Post_Type::filter_bulk_actions( $initial_action );
$this->assertFalse( isset( $action['edit'] ) );
$this->assertEquals( 'Recheck', $actions[ AMP_Invalid_URL_Post_Type::BULK_VALIDATE_ACTION ] );
$this->assertEquals( 'Forget', $actions['trash'] );
$this->assertEquals( 'Forget permanently', $actions['delete'] );
}

/**
Expand Down Expand Up @@ -1167,6 +1171,77 @@ public function test_filter_dashboard_glance_items() {
$this->assertContains( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_STATUS_QUERY_VAR, $items[0] );
}

/**
* Test for filter_post_row_actions()
*
* @covers \AMP_Invalid_URL_Post_Type::filter_post_row_actions()
*/
public function test_filter_post_row_actions() {
$this->assertEquals( array(), AMP_Invalid_URL_Post_Type::filter_post_row_actions( array(), null ) );

$actions = array(
'trash' => '',
'delete' => '',
);

$post = $this->factory()->post->create_and_get( array(
'post_type' => AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG,
'title' => 'My Post',
) );

$filtered_actions = AMP_Invalid_URL_Post_Type::filter_post_row_actions( $actions, $post );

$this->assertContains( 'Forget</a>', $filtered_actions['trash'] );
$this->assertContains( 'Forget Permanently</a>', $filtered_actions['delete'] );

}

/**
* Test for filter_table_views()
*
* @covers \AMP_Invalid_URL_Post_Type::filter_table_views()
*/
public function test_filter_table_views() {
$this->assertEquals( array(), AMP_Invalid_URL_Post_Type::filter_table_views( array() ) );

$views = array(
'trash' => 'Trash',
);

$filtered_views = AMP_Invalid_URL_Post_Type::filter_table_views( $views );

$this->assertEquals( 'Forgotten', $filtered_views['trash'] );
}

/**
* Test for filter_bulk_post_updated_messages()
*
* @covers \AMP_Invalid_URL_Post_Type::filter_bulk_post_updated_messages()
*/
public function test_filter_bulk_post_updated_messages() {
set_current_screen( 'index.php' );

$this->assertEquals( array(), AMP_Invalid_URL_Post_Type::filter_bulk_post_updated_messages( array(), array() ) );

set_current_screen( 'edit.php' );
get_current_screen()->id = sprintf( 'edit-%s', AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG );

$messages = array(
'post' => array(),
);

$filtered_messages = AMP_Invalid_URL_Post_Type::filter_bulk_post_updated_messages( $messages, array(
'deleted' => 1,
'trashed' => 99,
'untrashed' => 99,
) );

$this->assertEquals( '%s invalid AMP page permanently forgotten.', $filtered_messages['post']['deleted'] );
$this->assertEquals( '%s invalid AMP pages forgotten.', $filtered_messages['post']['trashed'] );
$this->assertEquals( '%s invalid AMP pages unforgotten.', $filtered_messages['post']['untrashed'] );
}


/**
* Gets mock errors for tests.
*
Expand Down

0 comments on commit 4015913

Please sign in to comment.