Skip to content

Commit 0c65014

Browse files
authored
List Tables: Add support for Screen Options (#1925)
1 parent 12f385d commit 0c65014

File tree

9 files changed

+138
-32
lines changed

9 files changed

+138
-32
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: added
3+
4+
Followers and Following list tables now support Columns and Pagination screen options.

activitypub.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ function plugin_init() {
9696
* Initialize plugin admin.
9797
*/
9898
function plugin_admin_init() {
99-
// Menus are registered before `admin_init`, because of course they are.
99+
// Screen Options and Menus are set before `admin_init`.
100+
\add_filter( 'init', array( __NAMESPACE__ . '\WP_Admin\Screen_Options', 'init' ) );
100101
\add_action( 'admin_menu', array( __NAMESPACE__ . '\WP_Admin\Menu', 'admin_menu' ) );
102+
101103
\add_action( 'admin_init', array( __NAMESPACE__ . '\WP_Admin\Admin', 'init' ) );
102104
\add_action( 'admin_init', array( __NAMESPACE__ . '\WP_Admin\Health_Check', 'init' ) );
103105
\add_action( 'admin_init', array( __NAMESPACE__ . '\WP_Admin\Settings', 'init' ) );

includes/table/class-followers.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,10 @@ public function get_sortable_columns() {
7777
* Prepare items.
7878
*/
7979
public function prepare_items() {
80-
$columns = $this->get_columns();
81-
$hidden = array();
82-
8380
$this->process_action();
84-
$this->_column_headers = array( $columns, $hidden, $this->get_sortable_columns() );
8581

8682
$page_num = $this->get_pagenum();
87-
$per_page = 20;
83+
$per_page = $this->get_items_per_page( 'activitypub_followers_per_page' );
8884

8985
$args = array();
9086

includes/table/class-following.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,12 @@ public function get_sortable_columns() {
7777
* Prepare items.
7878
*/
7979
public function prepare_items() {
80-
$columns = $this->get_columns();
81-
$hidden = array();
82-
$status = Following_Collection::ALL;
80+
$status = Following_Collection::ALL;
8381

8482
$this->process_action();
85-
$this->_column_headers = array( $columns, $hidden, $this->get_sortable_columns() );
8683

8784
$page_num = $this->get_pagenum();
88-
$per_page = 20;
85+
$per_page = $this->get_items_per_page( 'activitypub_following_per_page' );
8986

9087
$args = array();
9188

includes/wp-admin/class-admin.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,17 @@ public static function following_list_page() {
124124
}
125125

126126
/**
127-
* Adds the follower list to the Help tab.
127+
* Creates the followers list table.
128128
*/
129-
public static function add_followers_list_help_tab() {
130-
// todo.
129+
public static function add_followers_list_table() {
130+
$GLOBALS['followers_list_table'] = new \Activitypub\Table\Followers();
131131
}
132132

133133
/**
134-
* Adds the following list to the Help tab.
134+
* Creates the following list table.
135135
*/
136-
public static function add_following_list_help_tab() {
137-
// todo.
136+
public static function add_following_list_table() {
137+
$GLOBALS['following_list_table'] = new \Activitypub\Table\Following();
138138
}
139139

140140
/**

includes/wp-admin/class-menu.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Menu {
1919
*/
2020
public static function admin_menu() {
2121
$settings_page = \add_options_page(
22-
'Welcome',
22+
\_x( 'Welcome', 'page title', 'activitypub' ),
2323
'ActivityPub',
2424
'manage_options',
2525
'activitypub',
@@ -28,6 +28,9 @@ public static function admin_menu() {
2828

2929
\add_action( 'load-' . $settings_page, array( Settings::class, 'add_settings_help_tab' ) );
3030
\add_action( 'load-users.php', array( Settings::class, 'add_users_help_tab' ) );
31+
\add_action( 'load-' . $settings_page, array( Admin::class, 'add_followers_list_table' ) );
32+
\add_action( 'load-' . $settings_page, array( Admin::class, 'add_following_list_table' ) );
33+
\add_action( 'load-' . $settings_page, array( Screen_Options::class, 'add_settings_list_options' ) );
3134

3235
// User has to be able to publish posts.
3336
if ( user_can_activitypub( \get_current_user_id() ) ) {
@@ -39,7 +42,8 @@ public static function admin_menu() {
3942
array( Admin::class, 'followers_list_page' )
4043
);
4144

42-
\add_action( 'load-' . $followers_list_page, array( Admin::class, 'add_followers_list_help_tab' ) );
45+
\add_action( 'load-' . $followers_list_page, array( Admin::class, 'add_followers_list_table' ) );
46+
\add_action( 'load-' . $followers_list_page, array( Screen_Options::class, 'add_followers_list_options' ) );
4347

4448
/**
4549
* Filter to show the following UI.
@@ -55,7 +59,8 @@ public static function admin_menu() {
5559
array( Admin::class, 'following_list_page' )
5660
);
5761

58-
\add_action( 'load-' . $following_list_page, array( Admin::class, 'add_following_list_help_tab' ) );
62+
\add_action( 'load-' . $following_list_page, array( Admin::class, 'add_following_list_table' ) );
63+
\add_action( 'load-' . $following_list_page, array( Screen_Options::class, 'add_following_list_options' ) );
5964
}
6065

6166
\add_users_page(
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Screen Options file.
4+
*
5+
* @package Activitypub
6+
*/
7+
8+
namespace Activitypub\WP_Admin;
9+
10+
/**
11+
* ActivityPub Screen Options Class.
12+
*/
13+
class Screen_Options {
14+
/**
15+
* Initialize the class.
16+
*/
17+
public static function init() {
18+
\add_filter( 'set-screen-option', array( self::class, 'set_per_page_option' ), 10, 3 );
19+
}
20+
21+
/**
22+
* Add settings list screen options.
23+
*
24+
* @see Menu::admin_menu()
25+
*/
26+
public static function add_settings_list_options() {
27+
$tab = \sanitize_text_field( \wp_unslash( $_GET['tab'] ?? 'welcome' ) ); // phpcs:ignore WordPress.Security.NonceVerification
28+
29+
switch ( $tab ) {
30+
case 'followers':
31+
self::add_followers_list_options();
32+
break;
33+
case 'following':
34+
self::add_following_list_options();
35+
break;
36+
}
37+
}
38+
39+
/**
40+
* Add follower list screen options.
41+
*
42+
* @see Menu::admin_menu()
43+
*/
44+
public static function add_followers_list_options() {
45+
\add_screen_option(
46+
'per_page',
47+
array(
48+
'label' => \__( 'Followers per page', 'activitypub' ),
49+
'default' => 20,
50+
'option' => 'activitypub_followers_per_page',
51+
)
52+
);
53+
}
54+
55+
/**
56+
* Add screen options for following list.
57+
*
58+
* @see Menu::admin_menu()
59+
*/
60+
public static function add_following_list_options() {
61+
\add_screen_option(
62+
'per_page',
63+
array(
64+
'label' => \__( 'Following per page', 'activitypub' ),
65+
'default' => 20,
66+
'option' => 'activitypub_following_per_page',
67+
)
68+
);
69+
}
70+
71+
/**
72+
* Set per_page screen options.
73+
*
74+
* @param mixed $status Screen option value. Default false to skip.
75+
* @param string $option The option name.
76+
* @param mixed $value The option value.
77+
* @return int
78+
*/
79+
public static function set_per_page_option( $status, $option, $value ) {
80+
if ( 'activitypub_followers_per_page' === $option || 'activitypub_following_per_page' === $option ) {
81+
$value = (int) $value;
82+
83+
if ( $value > 0 && $value <= 100 ) {
84+
return $value;
85+
}
86+
}
87+
88+
return $status;
89+
}
90+
}

templates/followers-list.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77

88
// phpcs:disable WordPress.Security.NonceVerification.Recommended
99

10-
$table = new \Activitypub\Table\Followers();
10+
/**
11+
* Followers list table.
12+
*
13+
* @global Activitypub\Table\Followers $followers_list_table
14+
*/
15+
global $followers_list_table;
16+
1117
$_search = \sanitize_text_field( \wp_unslash( $_REQUEST['s'] ?? '' ) );
1218
$_page = \sanitize_text_field( \wp_unslash( $_REQUEST['page'] ?? '' ) );
1319
$_tab = \sanitize_text_field( \wp_unslash( $_REQUEST['tab'] ?? '' ) );
1420

15-
$table->prepare_items();
21+
$followers_list_table->prepare_items();
1622
?>
1723
<div class="wrap">
1824
<h1 class="wp-heading-inline"><?php esc_html_e( 'Followers', 'activitypub' ); ?></h1>
@@ -28,18 +34,18 @@
2834

2935
<hr class="wp-header-end">
3036

31-
<?php $table->views(); ?>
37+
<?php $followers_list_table->views(); ?>
3238

3339
<form method="get">
3440
<input type="hidden" name="page" value="<?php echo esc_attr( $_page ); ?>" />
3541
<input type="hidden" name="tab" value="<?php echo esc_attr( $_tab ); ?>" />
36-
<?php $table->search_box( esc_html__( 'Search Followers', 'activitypub' ), 'search' ); ?>
42+
<?php $followers_list_table->search_box( esc_html__( 'Search Followers', 'activitypub' ), 'search' ); ?>
3743
</form>
3844

3945
<form method="post">
4046
<input type="hidden" name="page" value="<?php echo esc_attr( $_page ); ?>" />
4147
<input type="hidden" name="tab" value="<?php echo esc_attr( $_tab ); ?>" />
42-
<?php wp_nonce_field( 'bulk-' . $table->_args['plural'] ); ?>
43-
<?php $table->display(); ?>
48+
<?php wp_nonce_field( 'bulk-' . $followers_list_table->_args['plural'] ); ?>
49+
<?php $followers_list_table->display(); ?>
4450
</form>
4551
</div>

templates/following-list.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77

88
// phpcs:disable WordPress.Security.NonceVerification.Recommended
99

10-
$table = new \Activitypub\Table\Following();
10+
/**
11+
* Following list table.
12+
*
13+
* @global Activitypub\Table\Following $following_list_table
14+
*/
15+
global $following_list_table;
16+
1117
$_search = \sanitize_text_field( \wp_unslash( $_REQUEST['s'] ?? '' ) );
1218
$_page = \sanitize_text_field( \wp_unslash( $_REQUEST['page'] ?? '' ) );
1319
$_tab = \sanitize_text_field( \wp_unslash( $_REQUEST['tab'] ?? '' ) );
1420
$_status = \sanitize_text_field( \wp_unslash( $_REQUEST['status'] ?? 'accepted' ) );
1521

16-
$table->prepare_items();
22+
$following_list_table->prepare_items();
1723
?>
1824
<div class="wrap">
1925
<h1 class="wp-heading-inline"><?php esc_html_e( 'Followings', 'activitypub' ); ?></h1>
@@ -29,18 +35,18 @@
2935

3036
<hr class="wp-header-end">
3137

32-
<?php $table->views(); ?>
38+
<?php $following_list_table->views(); ?>
3339
<form method="get">
3440
<input type="hidden" name="page" value="<?php echo esc_attr( $_page ); ?>" />
3541
<input type="hidden" name="tab" value="<?php echo esc_attr( $_tab ); ?>" />
3642
<input type="hidden" name="status" value="<?php echo esc_attr( $_status ); ?>" />
37-
<?php $table->search_box( esc_html__( 'Search Followings', 'activitypub' ), 'search' ); ?>
43+
<?php $following_list_table->search_box( esc_html__( 'Search Followings', 'activitypub' ), 'search' ); ?>
3844
</form>
3945

4046
<form method="post">
4147
<input type="hidden" name="page" value="<?php echo esc_attr( $_page ); ?>" />
4248
<input type="hidden" name="tab" value="<?php echo esc_attr( $_tab ); ?>" />
43-
<?php wp_nonce_field( 'bulk-' . $table->_args['plural'] ); ?>
44-
<?php $table->display(); ?>
49+
<?php wp_nonce_field( 'bulk-' . $following_list_table->_args['plural'] ); ?>
50+
<?php $following_list_table->display(); ?>
4551
</form>
4652
</div>

0 commit comments

Comments
 (0)