Skip to content

Commit

Permalink
Instead of adding the taxonomy as a query var, add it to $_REQUEST
Browse files Browse the repository at this point in the history
As Weston suggested.
It would be ideal to do this in render_single_url_list_table(),
but set_current_screen() looks to run before that, and that needs access to the $_REQUEST['taxonomy'].
  • Loading branch information
kienstra committed Sep 11, 2018
1 parent 6324b34 commit aef6e05
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 30 deletions.
47 changes: 17 additions & 30 deletions includes/validation/class-amp-invalid-url-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static function add_admin_hooks() {
add_action( 'add_meta_boxes', array( __CLASS__, 'add_meta_boxes' ) );
add_action( 'edit_form_after_title', array( __CLASS__, 'render_single_url_list_table' ) );
add_filter( 'edit_' . AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG . '_per_page', array( __CLASS__, 'get_terms_per_page' ) );
add_filter( 'get_edit_post_link', array( __CLASS__, 'add_taxonomy_to_edit_link' ), 10, 3 );
add_action( 'admin_init', array( __CLASS__, 'add_taxonomy' ) );
add_action( 'edit_form_top', array( __CLASS__, 'print_url_as_title' ) );

// Post list screen hooks.
Expand Down Expand Up @@ -1250,13 +1250,11 @@ public static function print_status_meta_box( $post ) {
* @return void
*/
public static function render_single_url_list_table( $post ) {
global $post_type;

if ( self::POST_TYPE_SLUG !== $post->post_type || ! isset( $_GET['taxonomy'] ) ) { // WPCS: CSRF OK.
if ( self::POST_TYPE_SLUG !== $post->post_type ) {
return;
}

$taxonomy = sanitize_key( $_GET['taxonomy'] ); // WPCS: CSRF OK.
$taxonomy = AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG;
$taxonomy_object = get_taxonomy( $taxonomy );
if ( ! $taxonomy_object ) {
wp_die( esc_html__( 'Invalid taxonomy.', 'default' ) );
Expand Down Expand Up @@ -1292,7 +1290,7 @@ public static function render_single_url_list_table( $post ) {
?>
<form class="search-form wp-clearfix" method="get">
<input type="hidden" name="taxonomy" value="<?php echo esc_attr( $taxonomy ); ?>" />
<input type="hidden" name="post_type" value="<?php echo esc_attr( $post_type ); ?>" />
<input type="hidden" name="post_type" value="<?php echo esc_attr( $post->post_type ); ?>" />
<?php $wp_list_table->search_box( esc_html__( 'Search Errors', 'amp' ), 'invalid-url-search' ); ?>
</form>
<div id="url-post-filter" class="alignleft actions">
Expand All @@ -1302,7 +1300,7 @@ public static function render_single_url_list_table( $post ) {

<form id="posts-filter" method="post">
<input type="hidden" name="taxonomy" value="<?php echo esc_attr( $taxonomy ); ?>" />
<input type="hidden" name="post_type" value="<?php echo esc_attr( $post_type ); ?>" />
<input type="hidden" name="post_type" value="<?php echo esc_attr( $post->post_type ); ?>" />
<?php $wp_list_table->display(); ?>
</form>
<style>
Expand Down Expand Up @@ -1330,33 +1328,22 @@ public static function get_terms_per_page( $terms_per_page ) {
}

/**
* Adds a taxonomy query var to the amp_invalid_url post edit link.
* Adds the taxonomy to the $_REQUEST, so that it is available in WP_Screen and WP_Terms_List_Table.
*
* This changes the link from something like:
* https://example.test/wp-admin/post.php?post=4416&action=edit
* to:
* https://example.test/wp-admin/post.php?post=4416&action=edit&taxonomy=amp_validation_error
* The post.php page that this link leads to is really more like an edit-tags.php page.
* It has a WP_Terms_List_Table of the validation errors (amp_validation_error terms).
* For the WP_Terms_List_Table to render, its $screen->taxonomy property needs to be set.
* So this adds a taxonomy query var, which is later parsed into $screen->taxonomy.
*
* @param string $link The edit link to filter.
* @param int $post_id The post ID.
* @param string $context The link context. If set to 'display' then ampersands
* are encoded.
* @return string $link The filtered link.
* It would be ideal to do this in render_single_url_list_table(),
* but set_current_screen() looks to run before that, and that needs access to the 'taxonomy'.
*/
public static function add_taxonomy_to_edit_link( $link, $post_id, $context ) {
if ( self::POST_TYPE_SLUG !== get_post_type( $post_id ) ) {
return $link;
public static function add_taxonomy() {
global $pagenow;

if ( 'post.php' !== $pagenow || ! isset( $_REQUEST['post'] ) ) { // WPCS: CSRF OK.
return;
}

return add_query_arg(
'taxonomy',
AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG,
$link
);
$post_id = sanitize_key( $_REQUEST['post'] );
if ( self::POST_TYPE_SLUG === get_post_type( $post_id ) ) { // WPCS: CSRF OK.
$_REQUEST['taxonomy'] = AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG;
}
}

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/validation/test-class-amp-invalid-url-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,35 @@ public function test_get_terms_per_page() {
}
}

/**
* Test for add_taxonomy()
*
* @covers \AMP_Invalid_URL_Post_Type::add_taxonomy()
*/
public function test_add_taxonomy() {
// The 'pagenow' value is incorrect, so this should not add the taxonomy.
$GLOBALS['pagenow'] = 'edit.php';
AMP_Invalid_URL_Post_Type::add_taxonomy();
$this->assertFalse( isset( $_REQUEST['taxonomy'] ) ); // WPCS: CSRF OK.

// Though the 'pagenow' value is correct, the $_REQUEST['post'] is not set, and this should not add the taxonomy.
$GLOBALS['pagenow'] = 'post.php';
AMP_Invalid_URL_Post_Type::add_taxonomy();
$this->assertFalse( isset( $_REQUEST['taxonomy'] ) ); // WPCS: CSRF OK.

// Though the $_REQUEST['post'] is set, it is for a post of the wrong type.
$wrong_post_type = $this->factory()->post->create();
$_REQUEST['post'] = $wrong_post_type;
AMP_Invalid_URL_Post_Type::add_taxonomy();
$this->assertFalse( isset( $_REQUEST['taxonomy'] ) ); // WPCS: CSRF OK.

// Now that the post type is correct, this should add the taxonomy to $_REQUEST.
$correct_post_type = $this->factory()->post->create( array( 'post_type' => AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG ) );
$_REQUEST['post'] = $correct_post_type;
AMP_Invalid_URL_Post_Type::add_taxonomy();
$this->assertEquals( AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG, $_REQUEST['taxonomy'] ); // WPCS: CSRF OK.
}

/**
* Test for print_status_meta_box()
*
Expand Down

0 comments on commit aef6e05

Please sign in to comment.