← All changes
|
php/Admin/Menus/Manage/Manage_Menu_Screen_Options.php
+79
-5
3.10.0-beta.1
→
trunk
View file →
| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Code_Snippets\Admin\Menus\Manage; |
| 4 | 4 | |
| 5 | +use WP_Screen; | |
| 5 | 6 | use function Code_Snippets\code_snippets; |
| 6 | 7 | |
| 7 | 8 | /** |
| 8 | 9 | * Provides Screen Options for the manage snippets table. |
| @@ -12,9 +13,9 @@ | ||
| 12 | 13 | /** |
| 13 | 14 | * Register hooks this class. |
| 14 | 15 | */ |
| 15 | 16 | public function __construct() { |
| 16 | - add_action( 'admin_init', [ $this, 'save_truncation_preference' ] ); | |
| 17 | + add_action( 'wp_loaded', [ $this, 'save_truncation_preference' ] ); | |
| 17 | 18 | add_filter( 'set-screen-option', [ $this, 'save_per_page_option' ], 10, 3 ); |
| 18 | 19 | } |
| 19 | 20 | |
| 20 | 21 | /** |
| @@ -24,10 +25,18 @@ | ||
| 24 | 25 | */ |
| 25 | 26 | public function load() { |
| 26 | 27 | $screen = get_current_screen(); |
| 27 | 28 | |
| 29 | + if ( $this->is_upsell_view() ) { | |
| 30 | + // The upsell page has no table or settings of its own, so the whole | |
| 31 | + // screen-meta-links block (Screen Options + Help tabs) is unhooked. | |
| 32 | + add_filter( 'screen_options_show_screen', '__return_false' ); | |
| 33 | + return; | |
| 34 | + } | |
| 35 | + | |
| 28 | 36 | if ( $screen && ! $this->is_cloud_community_view() ) { |
| 29 | 37 | add_filter( "manage_{$screen->id}_columns", [ $this, 'get_columns' ] ); |
| 38 | + add_filter( 'default_hidden_columns', [ $this, 'get_default_hidden_columns' ], 10, 2 ); | |
| 30 | 39 | add_filter( 'screen_settings', [ $this, 'render' ] ); |
| 31 | 40 | } |
| 32 | 41 | |
| 33 | 42 | add_screen_option( |
| @@ -52,8 +61,9 @@ | ||
| 52 | 61 | $columns, |
| 53 | 62 | [ |
| 54 | 63 | '_title' => __( 'Columns', 'code-snippets' ), |
| 55 | 64 | 'activate' => __( 'Active', 'code-snippets' ), |
| 65 | + 'id' => __( 'ID', 'code-snippets' ), | |
| 56 | 66 | 'name' => __( 'Name', 'code-snippets' ), |
| 57 | 67 | 'type' => __( 'Type', 'code-snippets' ), |
| 58 | 68 | 'desc' => __( 'Description', 'code-snippets' ), |
| 59 | 69 | 'tags' => __( 'Tags', 'code-snippets' ), |
| @@ -58,13 +68,29 @@ | ||
| 58 | 68 | 'desc' => __( 'Description', 'code-snippets' ), |
| 59 | 69 | 'tags' => __( 'Tags', 'code-snippets' ), |
| 60 | 70 | 'date' => __( 'Modified', 'code-snippets' ), |
| 61 | 71 | 'priority' => __( 'Priority', 'code-snippets' ), |
| 72 | + 'author' => __( 'Author', 'code-snippets' ), | |
| 73 | + 'insights' => __( 'Insights', 'code-snippets' ), | |
| 62 | 74 | ] |
| 63 | 75 | ); |
| 64 | 76 | } |
| 65 | 77 | |
| 66 | 78 | /** |
| 79 | + * Hide the optional ID column until a user enables it. | |
| 80 | + * | |
| 81 | + * @param string[] $hidden_columns Column identifiers hidden by default. | |
| 82 | + * @param WP_Screen $screen Current admin screen. | |
| 83 | + * | |
| 84 | + * @return string[] | |
| 85 | + */ | |
| 86 | + public function get_default_hidden_columns( array $hidden_columns, WP_Screen $screen ): array { | |
| 87 | + return get_current_screen() === $screen | |
| 88 | + ? array_merge( $hidden_columns, [ 'id' ] ) | |
| 89 | + : $hidden_columns; | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 67 | 93 | * Get the columns hidden for the current user. |
| 68 | 94 | * |
| 69 | 95 | * @return string[] |
| 70 | 96 | */ |
| @@ -99,28 +125,76 @@ | ||
| 99 | 125 | * |
| 100 | 126 | * @return bool |
| 101 | 127 | */ |
| 102 | 128 | public function is_manage_table_view(): bool { |
| 103 | - return ! $this->get_current_subpage(); | |
| 129 | + return in_array( $this->get_current_subpage(), [ '', 'snippets' ], true ); | |
| 104 | 130 | } |
| 105 | 131 | |
| 106 | 132 | /** |
| 133 | + * Whether the current request renders the AI Agent. | |
| 134 | + * | |
| 135 | + * The subpage has no screen options or help tabs of its own, so it remains | |
| 136 | + * an upsell view for {@see load()}; this detection exists only so its | |
| 137 | + * runtime data can be localized on the subpage that uses it. | |
| 138 | + * | |
| 139 | + * @return bool | |
| 140 | + */ | |
| 141 | + public function is_ai_agent_view(): bool { | |
| 142 | + return 'ai-agent' === $this->get_current_subpage(); | |
| 143 | + } | |
| 144 | + | |
| 145 | + /** | |
| 107 | 146 | * Whether the current request renders Community Cloud. |
| 108 | 147 | * |
| 148 | + * The "Bundles" tab within Community Cloud is a Pro-only upsell rather than | |
| 149 | + * genuine Community Cloud content, so it is excluded here and picked up by | |
| 150 | + * {@see is_upsell_view()} instead. | |
| 151 | + * | |
| 109 | 152 | * @return bool |
| 110 | 153 | */ |
| 111 | 154 | public function is_cloud_community_view(): bool { |
| 112 | - return 'cloud-community' === $this->get_current_subpage(); | |
| 155 | + if ( 'cloud-community' !== $this->get_current_subpage() ) { | |
| 156 | + return false; | |
| 157 | + } | |
| 158 | + | |
| 159 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only routing parameter. | |
| 160 | + $tab = isset( $_REQUEST['tab'] ) ? sanitize_key( wp_unslash( $_REQUEST['tab'] ) ) : ''; | |
| 161 | + | |
| 162 | + return 'bundles' !== $tab; | |
| 113 | 163 | } |
| 114 | 164 | |
| 115 | 165 | /** |
| 166 | + * Whether the current request renders an upsell page. | |
| 167 | + * | |
| 168 | + * Every genuine Core subpage has its own positive `is_*_view()` detection | |
| 169 | + * above; a subpage that matches none of them (Blueprints, Cloud Library, | |
| 170 | + * and any future Pro-only addition) has no real content of its own and | |
| 171 | + * falls through to the upsell placeholder. | |
| 172 | + * | |
| 173 | + * @return bool | |
| 174 | + */ | |
| 175 | + public function is_upsell_view(): bool { | |
| 176 | + return ! $this->is_manage_table_view() && ! $this->is_cloud_community_view(); | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 116 | 180 | * Render the manage table controls. |
| 117 | 181 | * |
| 118 | - * @param string $screen_settings Existing screen settings HTML. | |
| 182 | + * Anything may filter `screen_settings` before this runs, and a callback | |
| 183 | + * that forgets to return its value hands the next one null. Declaring the | |
| 184 | + * parameter as a string turned that into a fatal error, and because it is | |
| 185 | + * raised while the screen meta is being rendered, the page dies after the | |
| 186 | + * admin chrome but before any content: the snippets screen appears blank | |
| 187 | + * while the rest of the admin looks fine. | |
| 119 | 188 | * |
| 189 | + * @param mixed $screen_settings Existing screen settings HTML, from an | |
| 190 | + * unknown number of earlier callbacks. | |
| 191 | + * | |
| 120 | 192 | * @return string |
| 121 | 193 | */ |
| 122 | - public function render( string $screen_settings ): string { | |
| 194 | + public function render( $screen_settings ): string { | |
| 195 | + $screen_settings = is_string( $screen_settings ) ? $screen_settings : ''; | |
| 196 | + | |
| 123 | 197 | if ( $this->is_cloud_community_view() ) { |
| 124 | 198 | return $screen_settings; |
| 125 | 199 | } |
| 126 | 200 | |