Skip to content

Commit 369059a

Browse files
committed
feat(admin): screen options on the picker page
Wires WordPress's standard Screen Options dropdown for the picker: - Per-page setting backed by user meta (msls_tp_per_page) with a 20-item default, persisted via the set-screen-option / set_screen_option_* filters. The list table reads it through get_items_per_page so the pagination args reflect the user's choice. - Column toggles surface automatically: on load-<hook> we register a manage_<screen>_columns filter that returns the table's columns. WP then renders a checkbox per column and stores hidden ones in manage<screen>columnshidden user meta. prepare_items now passes get_hidden_columns into _column_headers so hidden columns are honored at render time. The columns filter instantiates a temporary table without requiring a source blog, so the toggle list is available before the user picks a source. Taxonomy lookup falls back to the current (target) blog when no source is set.
1 parent aac250c commit 369059a

2 files changed

Lines changed: 87 additions & 7 deletions

File tree

includes/MslsTranslationPickerPage.php

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class MslsTranslationPickerPage {
2222

2323
const SCRIPT_HANDLE = 'msls-translation-picker';
2424

25+
const PER_PAGE_OPTION = 'msls_tp_per_page';
26+
27+
const PER_PAGE_DEFAULT = 20;
28+
2529
/**
2630
* Kept for template callers that reference a single slug; equal to
2731
* the base slug for the 'post' post type.
@@ -36,6 +40,26 @@ public static function init(): void {
3640
// Late-priority reorder: put our entries right under "All Posts"
3741
// regardless of what other plugins do to the submenu array.
3842
add_action( 'admin_menu', array( self::class, 'reorder_submenu' ), 999 );
43+
44+
add_filter( 'set-screen-option', array( self::class, 'save_per_page_option' ), 10, 3 );
45+
add_filter( 'set_screen_option_' . self::PER_PAGE_OPTION, array( self::class, 'save_per_page_option' ), 10, 3 );
46+
}
47+
48+
/**
49+
* Persists the chosen per-page value submitted via screen options.
50+
*
51+
* @param mixed $status
52+
* @param string $option
53+
* @param mixed $value
54+
*
55+
* @return mixed
56+
*/
57+
public static function save_per_page_option( $status, $option, $value ) {
58+
if ( self::PER_PAGE_OPTION === $option ) {
59+
$value = (int) $value;
60+
return $value > 0 ? $value : self::PER_PAGE_DEFAULT;
61+
}
62+
return $status;
3963
}
4064

4165
/**
@@ -53,17 +77,62 @@ public static function register(): void {
5377
continue;
5478
}
5579

56-
add_submenu_page(
80+
$hook = add_submenu_page(
5781
$parent,
5882
__( 'Add Post from Translation', 'multisite-language-switcher' ),
5983
__( 'Add from Translation', 'multisite-language-switcher' ),
6084
'edit_posts',
6185
self::page_slug( $post_type ),
6286
array( self::class, 'render' )
6387
);
88+
89+
if ( $hook ) {
90+
add_action(
91+
'load-' . $hook,
92+
static function () use ( $post_type ) {
93+
MslsTranslationPickerPage::on_page_load( $post_type );
94+
}
95+
);
96+
}
6497
}
6598
}
6699

100+
/**
101+
* Called once per request when the picker page is being loaded for a
102+
* specific post type. Registers the screen options (per-page and the
103+
* automatic column-toggle dropdown).
104+
*
105+
* @codeCoverageIgnore
106+
*/
107+
public static function on_page_load( string $post_type ): void {
108+
add_screen_option(
109+
'per_page',
110+
array(
111+
'label' => __( 'Posts per page', 'multisite-language-switcher' ),
112+
'default' => self::PER_PAGE_DEFAULT,
113+
'option' => self::PER_PAGE_OPTION,
114+
)
115+
);
116+
117+
$screen = get_current_screen();
118+
if ( ! $screen ) {
119+
return;
120+
}
121+
122+
// Surface the table's columns to WordPress so the screen-options
123+
// dropdown shows toggles for them and hidden-column user prefs
124+
// persist through manage{$screen->id}columnshidden.
125+
add_filter(
126+
'manage_' . $screen->id . '_columns',
127+
static function () use ( $post_type ) {
128+
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
129+
$source = isset( $_GET['msls_source'] ) ? absint( wp_unslash( (string) $_GET['msls_source'] ) ) : 0;
130+
$table = new MslsTranslationPickerTable( $source, $post_type );
131+
return $table->get_columns();
132+
}
133+
);
134+
}
135+
67136
/**
68137
* Moves each of our submenu entries to the slot directly after the
69138
* parent's first item ("All Posts" / "All Pages" / "All CPTs"). Runs

includes/MslsTranslationPickerTable.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,22 @@ protected function get_admin_column_taxonomies(): array {
8080
return $this->taxonomies_cache;
8181
}
8282

83-
switch_to_blog( $this->source_blog_id );
83+
$switched = false;
84+
if ( $this->source_blog_id > 0 ) {
85+
switch_to_blog( $this->source_blog_id );
86+
$switched = true;
87+
}
88+
8489
$this->taxonomies_cache = array();
8590
foreach ( get_object_taxonomies( $this->post_type, 'objects' ) as $tax ) {
8691
if ( ! empty( $tax->show_admin_column ) ) {
8792
$this->taxonomies_cache[ $tax->name ] = $tax;
8893
}
8994
}
90-
restore_current_blog();
95+
96+
if ( $switched ) {
97+
restore_current_blog();
98+
}
9199

92100
return $this->taxonomies_cache;
93101
}
@@ -99,10 +107,13 @@ protected function get_bulk_actions(): array {
99107
}
100108

101109
public function prepare_items(): void {
102-
$this->_column_headers = array( $this->get_columns(), array(), array() );
110+
$columns = $this->get_columns();
111+
$hidden = is_object( $this->screen ) ? get_hidden_columns( $this->screen ) : array();
112+
$this->_column_headers = array( $columns, $hidden, array() );
103113

104114
$target_lang = MslsBlogCollection::get_blog_language( get_current_blog_id() );
105115
$current_page = $this->get_pagenum();
116+
$per_page = (int) $this->get_items_per_page( MslsTranslationPickerPage::PER_PAGE_OPTION, self::PER_PAGE );
106117

107118
switch_to_blog( $this->source_blog_id );
108119

@@ -111,7 +122,7 @@ public function prepare_items(): void {
111122
$args = array(
112123
'post_type' => $this->post_type,
113124
'post_status' => array( 'publish', 'draft', 'pending', 'future' ),
114-
'posts_per_page' => self::PER_PAGE,
125+
'posts_per_page' => $per_page,
115126
'paged' => $current_page,
116127
'post__not_in' => $translated_ids,
117128
'orderby' => 'date',
@@ -153,8 +164,8 @@ public function prepare_items(): void {
153164
$this->set_pagination_args(
154165
array(
155166
'total_items' => $total,
156-
'per_page' => self::PER_PAGE,
157-
'total_pages' => (int) ceil( $total / self::PER_PAGE ),
167+
'per_page' => $per_page,
168+
'total_pages' => $per_page > 0 ? (int) ceil( $total / $per_page ) : 0,
158169
)
159170
);
160171
}

0 commit comments

Comments
 (0)