Skip to content

Commit 5b283fa

Browse files
committed
Handle list table referer cleanup centrally
1 parent 9943ec1 commit 5b283fa

5 files changed

Lines changed: 30 additions & 124 deletions

classes/class-pmpro-discount-code-list-table.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -90,36 +90,6 @@ public function set_screen_option( $dummy, $option, $value ) {
9090
}
9191
}
9292

93-
/**
94-
* Override display_tablenav to prevent _wp_http_referer from being added
95-
* to the form. The stale referer in the URL causes Screen Options to
96-
* redirect to the pre-filter page, clearing all active filters.
97-
*
98-
* @since TBD
99-
*
100-
* @param string $which The location of the tablenav: 'top' or 'bottom'.
101-
*/
102-
protected function display_tablenav( $which ) {
103-
if ( 'bottom' === $which && ! $this->has_items() ) {
104-
return;
105-
}
106-
if ( 'top' === $which ) {
107-
wp_nonce_field( 'bulk-' . $this->_args['plural'], '_wpnonce', false );
108-
}
109-
?>
110-
<div class="tablenav <?php echo esc_attr( $which ); ?>">
111-
<?php if ( $this->has_items() ) : ?>
112-
<div class="alignleft actions bulkactions">
113-
<?php $this->bulk_actions( $which ); ?>
114-
</div>
115-
<?php endif; ?>
116-
<?php $this->extra_tablenav( $which ); ?>
117-
<?php $this->pagination( $which ); ?>
118-
<br class="clear" />
119-
</div>
120-
<?php
121-
}
122-
12393
/**
12494
* Prepares the list of items for displaying.
12595
*

classes/class-pmpro-members-list-table.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -90,36 +90,6 @@ public function set_screen_option( $dummy, $option, $value ) {
9090
}
9191
}
9292

93-
/**
94-
* Override display_tablenav to prevent _wp_http_referer from being added
95-
* to the form. The stale referer in the URL causes Screen Options to
96-
* redirect to the pre-filter page, clearing all active filters.
97-
*
98-
* @since TBD
99-
*
100-
* @param string $which The location of the tablenav: 'top' or 'bottom'.
101-
*/
102-
protected function display_tablenav( $which ) {
103-
if ( 'bottom' === $which && ! $this->has_items() ) {
104-
return;
105-
}
106-
if ( 'top' === $which ) {
107-
wp_nonce_field( 'bulk-' . $this->_args['plural'], '_wpnonce', false );
108-
}
109-
?>
110-
<div class="tablenav <?php echo esc_attr( $which ); ?>">
111-
<?php if ( $this->has_items() ) : ?>
112-
<div class="alignleft actions bulkactions">
113-
<?php $this->bulk_actions( $which ); ?>
114-
</div>
115-
<?php endif; ?>
116-
<?php $this->extra_tablenav( $which ); ?>
117-
<?php $this->pagination( $which ); ?>
118-
<br class="clear" />
119-
</div>
120-
<?php
121-
}
122-
12393
/**
12494
* Prepares the list of items for displaying.
12595
*

classes/class-pmpro-orders-list-table.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -95,36 +95,6 @@ public function set_screen_option( $dummy, $option, $value ) {
9595
}
9696
}
9797

98-
/**
99-
* Override display_tablenav to prevent _wp_http_referer from being added
100-
* to the form. The stale referer in the URL causes Screen Options to
101-
* redirect to the pre-filter page, clearing all active filters.
102-
*
103-
* @since TBD
104-
*
105-
* @param string $which The location of the tablenav: 'top' or 'bottom'.
106-
*/
107-
protected function display_tablenav( $which ) {
108-
if ( 'bottom' === $which && ! $this->has_items() ) {
109-
return;
110-
}
111-
if ( 'top' === $which ) {
112-
wp_nonce_field( 'bulk-' . $this->_args['plural'], '_wpnonce', false );
113-
}
114-
?>
115-
<div class="tablenav <?php echo esc_attr( $which ); ?>">
116-
<?php if ( $this->has_items() ) : ?>
117-
<div class="alignleft actions bulkactions">
118-
<?php $this->bulk_actions( $which ); ?>
119-
</div>
120-
<?php endif; ?>
121-
<?php $this->extra_tablenav( $which ); ?>
122-
<?php $this->pagination( $which ); ?>
123-
<br class="clear" />
124-
</div>
125-
<?php
126-
}
127-
12898
/**
12999
* Prepares the list of items for displaying.
130100
*

classes/class-pmpro-subscriptions-list-table.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -90,36 +90,6 @@ public function set_screen_option( $dummy, $option, $value ) {
9090
}
9191
}
9292

93-
/**
94-
* Override display_tablenav to prevent _wp_http_referer from being added
95-
* to the form. The stale referer in the URL causes Screen Options to
96-
* redirect to the pre-filter page, clearing all active filters.
97-
*
98-
* @since TBD
99-
*
100-
* @param string $which The location of the tablenav: 'top' or 'bottom'.
101-
*/
102-
protected function display_tablenav( $which ) {
103-
if ( 'bottom' === $which && ! $this->has_items() ) {
104-
return;
105-
}
106-
if ( 'top' === $which ) {
107-
wp_nonce_field( 'bulk-' . $this->_args['plural'], '_wpnonce', false );
108-
}
109-
?>
110-
<div class="tablenav <?php echo esc_attr( $which ); ?>">
111-
<?php if ( $this->has_items() ) : ?>
112-
<div class="alignleft actions bulkactions">
113-
<?php $this->bulk_actions( $which ); ?>
114-
</div>
115-
<?php endif; ?>
116-
<?php $this->extra_tablenav( $which ); ?>
117-
<?php $this->pagination( $which ); ?>
118-
<br class="clear" />
119-
</div>
120-
<?php
121-
}
122-
12393
/**
12494
* Prepares the list of items for displaying.
12595
*

includes/adminpages.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,17 @@ function pmpro_add_pages() {
8787
add_submenu_page( 'admin.php', __( 'Advanced Settings', 'paid-memberships-pro' ), __( 'Advanced Settings', 'paid-memberships-pro' ), 'pmpro_advancedsettings', 'pmpro-advancedsettings', 'pmpro_advancedsettings' );
8888

8989
// Set up screen settings for list tables.
90-
add_action( 'load-' . $members_list_table_hook, 'PMPro_Members_List_Table::hook_screen_options' );
91-
add_action( 'load-' . $orders_list_table_hook, 'PMPro_Orders_List_Table::hook_screen_options' );
92-
add_action( 'load-' . $subscriptions_list_table_hook, 'PMPro_Subscriptions_List_Table::hook_screen_options' );
93-
add_action( 'load-' . $discount_codes_list_table_hook, 'PMPro_Discount_Code_List_Table::hook_screen_options' );
90+
$pmpro_list_table_hooks = array(
91+
$members_list_table_hook => 'PMPro_Members_List_Table::hook_screen_options',
92+
$orders_list_table_hook => 'PMPro_Orders_List_Table::hook_screen_options',
93+
$subscriptions_list_table_hook => 'PMPro_Subscriptions_List_Table::hook_screen_options',
94+
$discount_codes_list_table_hook => 'PMPro_Discount_Code_List_Table::hook_screen_options',
95+
);
96+
97+
foreach ( $pmpro_list_table_hooks as $list_table_hook => $hook_screen_options_callback ) {
98+
add_action( 'load-' . $list_table_hook, 'pmpro_maybe_redirect_list_table_referer', 5 );
99+
add_action( 'load-' . $list_table_hook, $hook_screen_options_callback );
100+
}
94101

95102
//updates page only if needed
96103
if ( pmpro_isUpdateRequired() ) {
@@ -111,6 +118,25 @@ function pmpro_add_pages() {
111118
}
112119
add_action( 'admin_menu', 'pmpro_add_pages' );
113120

121+
/**
122+
* Remove stale list table referer parameters from PMPro admin URLs.
123+
*
124+
* WP_List_Table adds _wp_http_referer to GET forms. Leaving that value in the
125+
* URL causes Screen Options submissions to redirect back to the previous URL
126+
* because set_screen_options() prefers the request referer. Mirror WordPress
127+
* core list screens by stripping the transient referer args once.
128+
*
129+
* @since TBD
130+
*/
131+
function pmpro_maybe_redirect_list_table_referer() {
132+
if ( empty( $_REQUEST['_wp_http_referer'] ) || empty( $_SERVER['REQUEST_URI'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
133+
return;
134+
}
135+
136+
wp_safe_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
137+
exit;
138+
}
139+
114140
/**
115141
* Keep the Memberships menu selected on subpages.
116142
*/

0 commit comments

Comments
 (0)