| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Admin\Menus\Manage; |
| 4 |
|
| 5 |
use function Code_Snippets\code_snippets; |
| 6 |
|
| 7 |
/** |
| 8 |
* Provides Screen Options for the manage snippets table. |
| 9 |
*/ |
| 10 |
class Manage_Menu_Screen_Options { |
| 11 |
|
| 12 |
/** |
| 13 |
* Register hooks this class. |
| 14 |
*/ |
| 15 |
public function __construct() { |
| 16 |
add_action( 'wp_loaded', [ $this, 'save_truncation_preference' ] ); |
| 17 |
add_filter( 'set-screen-option', [ $this, 'save_per_page_option' ], 10, 3 ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Load the screen options for the current page. |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function load() { |
| 26 |
$screen = get_current_screen(); |
| 27 |
|
| 28 |
if ( $this->is_upsell_view() ) { |
| 29 |
// The upsell page has no table or settings of its own, so the whole |
| 30 |
// screen-meta-links block (Screen Options + Help tabs) is unhooked. |
| 31 |
add_filter( 'screen_options_show_screen', '__return_false' ); |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
if ( $screen && ! $this->is_cloud_community_view() ) { |
| 36 |
add_filter( "manage_{$screen->id}_columns", [ $this, 'get_columns' ] ); |
| 37 |
add_filter( 'screen_settings', [ $this, 'render' ] ); |
| 38 |
} |
| 39 |
|
| 40 |
add_screen_option( |
| 41 |
'per_page', |
| 42 |
[ |
| 43 |
'label' => __( 'Snippets per page', 'code-snippets' ), |
| 44 |
'default' => Manage_Menu::get_default_snippets_per_page(), |
| 45 |
'option' => 'snippets_per_page', |
| 46 |
] |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Add the snippets table columns. |
| 52 |
* |
| 53 |
* @param string[] $columns Existing columns. |
| 54 |
* |
| 55 |
* @return string[] |
| 56 |
*/ |
| 57 |
public function get_columns( array $columns = [] ): array { |
| 58 |
return array_merge( |
| 59 |
$columns, |
| 60 |
[ |
| 61 |
'_title' => __( 'Columns', 'code-snippets' ), |
| 62 |
'activate' => __( 'Active', 'code-snippets' ), |
| 63 |
'name' => __( 'Name', 'code-snippets' ), |
| 64 |
'type' => __( 'Type', 'code-snippets' ), |
| 65 |
'desc' => __( 'Description', 'code-snippets' ), |
| 66 |
'tags' => __( 'Tags', 'code-snippets' ), |
| 67 |
'date' => __( 'Modified', 'code-snippets' ), |
| 68 |
'priority' => __( 'Priority', 'code-snippets' ), |
| 69 |
] |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get the columns hidden for the current user. |
| 75 |
* |
| 76 |
* @return string[] |
| 77 |
*/ |
| 78 |
public function get_hidden_columns(): array { |
| 79 |
$screen = get_current_screen(); |
| 80 |
|
| 81 |
return $screen ? get_hidden_columns( $screen ) : []; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Whether long row values should be truncated. |
| 86 |
* |
| 87 |
* @return bool |
| 88 |
*/ |
| 89 |
public function should_truncate_rows(): bool { |
| 90 |
$setting = get_user_option( 'snippets_table_truncate_row_values' ); |
| 91 |
return false === $setting || $setting; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Read the current manage subpage. |
| 96 |
* |
| 97 |
* @return string |
| 98 |
*/ |
| 99 |
public function get_current_subpage(): string { |
| 100 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only routing parameter. |
| 101 |
return isset( $_REQUEST['subpage'] ) ? sanitize_key( wp_unslash( $_REQUEST['subpage'] ) ) : ''; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Whether the current request renders the snippets table. |
| 106 |
* |
| 107 |
* @return bool |
| 108 |
*/ |
| 109 |
public function is_manage_table_view(): bool { |
| 110 |
return ! $this->get_current_subpage(); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Whether the current request renders Community Cloud. |
| 115 |
* |
| 116 |
* The "Bundles" tab within Community Cloud is a Pro-only upsell rather than |
| 117 |
* genuine Community Cloud content, so it is excluded here and picked up by |
| 118 |
* {@see is_upsell_view()} instead. |
| 119 |
* |
| 120 |
* @return bool |
| 121 |
*/ |
| 122 |
public function is_cloud_community_view(): bool { |
| 123 |
if ( 'cloud-community' !== $this->get_current_subpage() ) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only routing parameter. |
| 128 |
$tab = isset( $_REQUEST['tab'] ) ? sanitize_key( wp_unslash( $_REQUEST['tab'] ) ) : ''; |
| 129 |
|
| 130 |
return 'bundles' !== $tab; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Whether the current request renders an upsell page. |
| 135 |
* |
| 136 |
* Every genuine Core subpage has its own positive `is_*_view()` detection |
| 137 |
* above; a subpage that matches none of them (Blueprints, Cloud Library, |
| 138 |
* and any future Pro-only addition) has no real content of its own and |
| 139 |
* falls through to the upsell placeholder. |
| 140 |
* |
| 141 |
* @return bool |
| 142 |
*/ |
| 143 |
public function is_upsell_view(): bool { |
| 144 |
return ! $this->is_manage_table_view() && ! $this->is_cloud_community_view(); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Render the manage table controls. |
| 149 |
* |
| 150 |
* Anything may filter `screen_settings` before this runs, and a callback |
| 151 |
* that forgets to return its value hands the next one null. Declaring the |
| 152 |
* parameter as a string turned that into a fatal error, and because it is |
| 153 |
* raised while the screen meta is being rendered, the page dies after the |
| 154 |
* admin chrome but before any content: the snippets screen appears blank |
| 155 |
* while the rest of the admin looks fine. |
| 156 |
* |
| 157 |
* @param mixed $screen_settings Existing screen settings HTML, from an |
| 158 |
* unknown number of earlier callbacks. |
| 159 |
* |
| 160 |
* @return string |
| 161 |
*/ |
| 162 |
public function render( $screen_settings ): string { |
| 163 |
$screen_settings = is_string( $screen_settings ) ? $screen_settings : ''; |
| 164 |
|
| 165 |
if ( $this->is_cloud_community_view() ) { |
| 166 |
return $screen_settings; |
| 167 |
} |
| 168 |
|
| 169 |
ob_start(); |
| 170 |
?> |
| 171 |
<fieldset class="metabox-prefs table-options-prefs"> |
| 172 |
<legend><?php esc_html_e( 'Table Options', 'code-snippets' ); ?></legend> |
| 173 |
<div class="metabox-prefs-container"> |
| 174 |
<label for="snippets-table-truncate-row-values"> |
| 175 |
<input |
| 176 |
id="snippets-table-truncate-row-values" |
| 177 |
name="snippets_table_truncate_row_values" |
| 178 |
type="checkbox" |
| 179 |
value="1" |
| 180 |
<?php checked( $this->should_truncate_rows() ); ?> |
| 181 |
/> |
| 182 |
<?php esc_html_e( 'Truncate long snippet names and descriptions', 'code-snippets' ); ?> |
| 183 |
</label> |
| 184 |
</div> |
| 185 |
</fieldset> |
| 186 |
<?php |
| 187 |
|
| 188 |
return $screen_settings . ob_get_clean(); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Persist the row truncation preference. |
| 193 |
* |
| 194 |
* @return void |
| 195 |
*/ |
| 196 |
public function save_truncation_preference(): void { |
| 197 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified below before persisting the option. |
| 198 |
if ( empty( $_POST['wp_screen_options'] ) || ! is_array( $_POST['wp_screen_options'] ) ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Sanitized and matched to a known admin page. |
| 203 |
$page = isset( $_REQUEST['page'] ) ? sanitize_key( wp_unslash( $_REQUEST['page'] ) ) : ''; |
| 204 |
|
| 205 |
if ( code_snippets()->get_menu_slug() !== $page || ! current_user_can( code_snippets()->get_cap() ) ) { |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
if ( $this->is_cloud_community_view() ) { |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified here before persisting the option. |
| 214 |
$nonce = isset( $_POST['screenoptionnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['screenoptionnonce'] ) ) : ''; |
| 215 |
|
| 216 |
if ( ! wp_verify_nonce( $nonce, 'screen-options-nonce' ) ) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
update_user_option( |
| 221 |
get_current_user_id(), |
| 222 |
'snippets_table_truncate_row_values', |
| 223 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified above before reading the checkbox state. |
| 224 |
isset( $_POST['snippets_table_truncate_row_values'] ) ? 1 : 0 |
| 225 |
); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Save only the snippets per-page Screen Option. |
| 230 |
* |
| 231 |
* @param mixed $status Current screen option status. |
| 232 |
* @param string $option The screen option name. |
| 233 |
* @param mixed $value Screen option value. |
| 234 |
* |
| 235 |
* @return mixed |
| 236 |
*/ |
| 237 |
public function save_per_page_option( $status, string $option, $value ) { |
| 238 |
return 'snippets_per_page' === $option ? $value : $status; |
| 239 |
} |
| 240 |
} |
| 241 |
|