PluginProbe
Code Snippets / 3.10.0-beta.1
Code Snippets v3.10.0-beta.1
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / Admin / Menus / Manage / Manage_Menu_Screen_Options.php

Manage_Menu_Screen_Options.php in Code Snippets 3.10.0-beta.1, at php/Admin/Menus/Manage/Manage_Menu_Screen_Options.php

199 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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( 'admin_init', [ $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 ( $screen && ! $this->is_cloud_community_view() ) {
29 add_filter( "manage_{$screen->id}_columns", [ $this, 'get_columns' ] );
30 add_filter( 'screen_settings', [ $this, 'render' ] );
31 }
32
33 add_screen_option(
34 'per_page',
35 [
36 'label' => __( 'Snippets per page', 'code-snippets' ),
37 'default' => Manage_Menu::get_default_snippets_per_page(),
38 'option' => 'snippets_per_page',
39 ]
40 );
41 }
42
43 /**
44 * Add the snippets table columns.
45 *
46 * @param string[] $columns Existing columns.
47 *
48 * @return string[]
49 */
50 public function get_columns( array $columns = [] ): array {
51 return array_merge(
52 $columns,
53 [
54 '_title' => __( 'Columns', 'code-snippets' ),
55 'activate' => __( 'Active', 'code-snippets' ),
56 'name' => __( 'Name', 'code-snippets' ),
57 'type' => __( 'Type', 'code-snippets' ),
58 'desc' => __( 'Description', 'code-snippets' ),
59 'tags' => __( 'Tags', 'code-snippets' ),
60 'date' => __( 'Modified', 'code-snippets' ),
61 'priority' => __( 'Priority', 'code-snippets' ),
62 ]
63 );
64 }
65
66 /**
67 * Get the columns hidden for the current user.
68 *
69 * @return string[]
70 */
71 public function get_hidden_columns(): array {
72 $screen = get_current_screen();
73
74 return $screen ? get_hidden_columns( $screen ) : [];
75 }
76
77 /**
78 * Whether long row values should be truncated.
79 *
80 * @return bool
81 */
82 public function should_truncate_rows(): bool {
83 $setting = get_user_option( 'snippets_table_truncate_row_values' );
84 return false === $setting || $setting;
85 }
86
87 /**
88 * Read the current manage subpage.
89 *
90 * @return string
91 */
92 public function get_current_subpage(): string {
93 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only routing parameter.
94 return isset( $_REQUEST['subpage'] ) ? sanitize_key( wp_unslash( $_REQUEST['subpage'] ) ) : '';
95 }
96
97 /**
98 * Whether the current request renders the snippets table.
99 *
100 * @return bool
101 */
102 public function is_manage_table_view(): bool {
103 return ! $this->get_current_subpage();
104 }
105
106 /**
107 * Whether the current request renders Community Cloud.
108 *
109 * @return bool
110 */
111 public function is_cloud_community_view(): bool {
112 return 'cloud-community' === $this->get_current_subpage();
113 }
114
115 /**
116 * Render the manage table controls.
117 *
118 * @param string $screen_settings Existing screen settings HTML.
119 *
120 * @return string
121 */
122 public function render( string $screen_settings ): string {
123 if ( $this->is_cloud_community_view() ) {
124 return $screen_settings;
125 }
126
127 ob_start();
128 ?>
129 <fieldset class="metabox-prefs table-options-prefs">
130 <legend><?php esc_html_e( 'Table Options', 'code-snippets' ); ?></legend>
131 <div class="metabox-prefs-container">
132 <label for="snippets-table-truncate-row-values">
133 <input
134 id="snippets-table-truncate-row-values"
135 name="snippets_table_truncate_row_values"
136 type="checkbox"
137 value="1"
138 <?php checked( $this->should_truncate_rows() ); ?>
139 />
140 <?php esc_html_e( 'Truncate long snippet names and descriptions', 'code-snippets' ); ?>
141 </label>
142 </div>
143 </fieldset>
144 <?php
145
146 return $screen_settings . ob_get_clean();
147 }
148
149 /**
150 * Persist the row truncation preference.
151 *
152 * @return void
153 */
154 public function save_truncation_preference(): void {
155 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified below before persisting the option.
156 if ( empty( $_POST['wp_screen_options'] ) || ! is_array( $_POST['wp_screen_options'] ) ) {
157 return;
158 }
159
160 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Sanitized and matched to a known admin page.
161 $page = isset( $_REQUEST['page'] ) ? sanitize_key( wp_unslash( $_REQUEST['page'] ) ) : '';
162
163 if ( code_snippets()->get_menu_slug() !== $page || ! current_user_can( code_snippets()->get_cap() ) ) {
164 return;
165 }
166
167 if ( $this->is_cloud_community_view() ) {
168 return;
169 }
170
171 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified here before persisting the option.
172 $nonce = isset( $_POST['screenoptionnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['screenoptionnonce'] ) ) : '';
173
174 if ( ! wp_verify_nonce( $nonce, 'screen-options-nonce' ) ) {
175 return;
176 }
177
178 update_user_option(
179 get_current_user_id(),
180 'snippets_table_truncate_row_values',
181 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified above before reading the checkbox state.
182 isset( $_POST['snippets_table_truncate_row_values'] ) ? 1 : 0
183 );
184 }
185
186 /**
187 * Save only the snippets per-page Screen Option.
188 *
189 * @param mixed $status Current screen option status.
190 * @param string $option The screen option name.
191 * @param mixed $value Screen option value.
192 *
193 * @return mixed
194 */
195 public function save_per_page_option( $status, string $option, $value ) {
196 return 'snippets_per_page' === $option ? $value : $status;
197 }
198 }
199