Skip to content

Commit e0eee69

Browse files
authored
Tables: Use Settings Errors to pass admin notices (#1953)
1 parent 32caa00 commit e0eee69

File tree

2 files changed

+50
-78
lines changed

2 files changed

+50
-78
lines changed

includes/table/class-followers.php

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,21 @@ public function process_action() {
5757
return;
5858
}
5959

60+
if ( ! $this->current_action() ) {
61+
return;
62+
}
63+
64+
$redirect_to = \add_query_arg(
65+
array(
66+
'settings-updated' => true, // Tell WordPress to load settings errors transient.
67+
'action' => false, // Remove action parameter to prevent redirect loop.
68+
)
69+
);
70+
6071
switch ( $this->current_action() ) {
6172
case 'delete':
73+
$redirect_to = \remove_query_arg( array( 'follower', 'followers' ), $redirect_to );
74+
6275
// Handle single follower deletion.
6376
if ( isset( $_GET['follower'], $_GET['_wpnonce'] ) ) {
6477
$follower = \absint( $_GET['follower'] );
@@ -67,13 +80,7 @@ public function process_action() {
6780
if ( \wp_verify_nonce( $nonce, 'delete-follower_' . $follower ) ) {
6881
Follower_Collection::remove( $follower, $this->user_id );
6982

70-
$redirect_args = array(
71-
'updated' => 'true',
72-
'action' => 'deleted',
73-
);
74-
75-
\wp_safe_redirect( \add_query_arg( $redirect_args, \remove_query_arg( array( 'follower' ) ) ) );
76-
exit;
83+
\add_settings_error( 'activitypub', 'follower_deleted', \__( 'Follower deleted.', 'activitypub' ), 'success' );
7784
}
7885
}
7986

@@ -87,52 +94,31 @@ public function process_action() {
8794
Follower_Collection::remove( $follower, $this->user_id );
8895
}
8996

90-
$redirect_args = array(
91-
'updated' => 'true',
92-
'action' => 'all_deleted',
93-
'count' => \count( $followers ),
94-
);
97+
$count = \count( $followers );
98+
/* translators: %d: Number of followers deleted. */
99+
$message = \_n( '%d follower deleted.', '%d followers deleted.', $count, 'activitypub' );
100+
$message = \sprintf( $message, \number_format_i18n( $count ) );
95101

96-
\wp_safe_redirect( \add_query_arg( $redirect_args, \remove_query_arg( array( 'followers' ) ) ) );
97-
exit;
102+
\add_settings_error( 'activitypub', 'followers_deleted', $message, 'success' );
98103
}
99104
}
100105
break;
101106

102107
default:
103108
break;
104109
}
110+
111+
\set_transient( 'settings_errors', get_settings_errors(), 30 ); // 30 seconds.
112+
113+
\wp_safe_redirect( $redirect_to );
114+
exit;
105115
}
106116

107117
/**
108118
* Process admin notices based on query parameters.
109119
*/
110120
public function process_admin_notices() {
111-
if ( isset( $_REQUEST['updated'] ) && 'true' === $_REQUEST['updated'] && ! empty( $_REQUEST['action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
112-
$message = '';
113-
switch ( $_REQUEST['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification
114-
case 'deleted':
115-
$message = \__( 'Follower deleted.', 'activitypub' );
116-
break;
117-
case 'all_deleted':
118-
$count = \absint( $_REQUEST['count'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification
119-
/* translators: %d: Number of followers deleted. */
120-
$message = \_n( '%d follower deleted.', '%d followers deleted.', $count, 'activitypub' );
121-
$message = \sprintf( $message, \number_format_i18n( $count ) );
122-
break;
123-
}
124-
125-
if ( ! empty( $message ) ) {
126-
\wp_admin_notice(
127-
$message,
128-
array(
129-
'type' => 'success',
130-
'dismissible' => true,
131-
'id' => 'message',
132-
)
133-
);
134-
}
135-
}
121+
\settings_errors( 'activitypub' );
136122
}
137123

138124
/**

includes/table/class-following.php

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,21 @@ public function process_action() {
5757
return;
5858
}
5959

60+
if ( ! $this->current_action() ) {
61+
return;
62+
}
63+
64+
$redirect_to = \add_query_arg(
65+
array(
66+
'settings-updated' => true, // Tell WordPress to load settings errors transient.
67+
'action' => false, // Remove action parameter to prevent redirect loop.
68+
)
69+
);
70+
6071
switch ( $this->current_action() ) {
6172
case 'delete':
73+
$redirect_to = \remove_query_arg( array( 'follower', 'following' ), $redirect_to );
74+
6275
// Handle single follower deletion.
6376
if ( isset( $_GET['follower'], $_GET['_wpnonce'] ) ) {
6477
$follower = \absint( $_GET['follower'] );
@@ -67,13 +80,7 @@ public function process_action() {
6780
if ( \wp_verify_nonce( $nonce, 'delete-follower_' . $follower ) ) {
6881
Following_Collection::unfollow( $follower, $this->user_id );
6982

70-
$redirect_args = array(
71-
'updated' => 'true',
72-
'action' => 'deleted',
73-
);
74-
75-
\wp_safe_redirect( \add_query_arg( $redirect_args, \remove_query_arg( array( 'follower' ) ) ) );
76-
exit;
83+
\add_settings_error( 'activitypub', 'follower_deleted', \__( 'Account unfollowed.', 'activitypub' ), 'success' );
7784
}
7885
}
7986

@@ -88,52 +95,31 @@ public function process_action() {
8895
Following_Collection::unfollow( $post_id, $this->user_id );
8996
}
9097

91-
$redirect_args = array(
92-
'updated' => 'true',
93-
'action' => 'all_deleted',
94-
'count' => \count( $following ),
95-
);
98+
$count = \count( $following );
99+
/* translators: %d: Number of accounts unfollowed. */
100+
$message = \_n( '%d account unfollowed.', '%d accounts unfollowed.', $count, 'activitypub' );
101+
$message = \sprintf( $message, \number_format_i18n( $count ) );
96102

97-
\wp_safe_redirect( \add_query_arg( $redirect_args, \remove_query_arg( array( 'following' ) ) ) );
98-
exit;
103+
\add_settings_error( 'activitypub', 'followers_deleted', $message, 'success' );
99104
}
100105
}
101106
break;
102107

103108
default:
104109
break;
105110
}
111+
112+
\set_transient( 'settings_errors', get_settings_errors(), 30 ); // 30 seconds.
113+
114+
\wp_safe_redirect( $redirect_to );
115+
exit;
106116
}
107117

108118
/**
109119
* Process admin notices based on query parameters.
110120
*/
111121
public function process_admin_notices() {
112-
if ( isset( $_REQUEST['updated'] ) && 'true' === $_REQUEST['updated'] && ! empty( $_REQUEST['action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
113-
$message = '';
114-
switch ( $_REQUEST['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification
115-
case 'deleted':
116-
$message = \__( 'Account unfollowed.', 'activitypub' );
117-
break;
118-
case 'all_deleted':
119-
$count = \absint( $_REQUEST['count'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification
120-
/* translators: %d: Number of accounts unfollowed. */
121-
$message = \_n( '%d account unfollowed.', '%d accounts unfollowed.', $count, 'activitypub' );
122-
$message = \sprintf( $message, \number_format_i18n( $count ) );
123-
break;
124-
}
125-
126-
if ( ! empty( $message ) ) {
127-
\wp_admin_notice(
128-
$message,
129-
array(
130-
'type' => 'success',
131-
'dismissible' => true,
132-
'id' => 'message',
133-
)
134-
);
135-
}
136-
}
122+
\settings_errors( 'activitypub' );
137123
}
138124

139125
/**

0 commit comments

Comments
 (0)