PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 4.5.5
Admin Columns v4.5.5
7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Table / Screen.php
codepress-admin-columns / classes / Table Last commit date
InlineStyle 3 years ago Button.php 3 years ago LayoutPreference.php 3 years ago Screen.php 3 years ago ScreenPreferences.php 3 years ago ScreenTools.php 3 years ago TableFormView.php 3 years ago
Screen.php
440 lines
1 <?php
2
3 namespace AC\Table;
4
5 use AC;
6 use AC\Asset;
7 use AC\Capabilities;
8 use AC\ColumnSize;
9 use AC\Form;
10 use AC\ListScreen;
11 use AC\Registrable;
12 use AC\Renderable;
13 use AC\ScreenController;
14 use AC\Settings;
15 use WP_Post;
16
17 final class Screen implements Registrable {
18
19 /**
20 * @var Asset\Location\Absolute
21 */
22 private $location;
23
24 /**
25 * @var ListScreen
26 */
27 private $list_screen;
28
29 /**
30 * @var Form\Element[]
31 */
32 private $screen_options;
33
34 /**
35 * @var Button[]
36 */
37 private $buttons = [];
38
39 /**
40 * @var ColumnSize\ListStorage
41 */
42 private $column_size_list_storage;
43
44 /**
45 * @var ColumnSize\UserStorage
46 */
47 private $column_size_user_storage;
48
49 public function __construct( Asset\Location\Absolute $location, ListScreen $list_screen, ColumnSize\ListStorage $column_size_list_storage, ColumnSize\UserStorage $column_size_user_storage ) {
50 $this->location = $location;
51 $this->list_screen = $list_screen;
52 $this->column_size_list_storage = $column_size_list_storage;
53 $this->column_size_user_storage = $column_size_user_storage;
54 }
55
56 /**
57 * Register hooks
58 */
59 public function register() {
60 $controller = new ScreenController( $this->list_screen );
61 $controller->register();
62
63 $render = new TableFormView( $this->list_screen->get_meta_type(), sprintf( '<input type="hidden" name="layout" value="%s">', $this->list_screen->get_layout_id() ) );
64 $render->register();
65
66 add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] );
67 add_action( 'admin_footer', [ $this, 'admin_footer_scripts' ] );
68 add_action( 'admin_head', [ $this, 'admin_head_scripts' ] );
69 add_action( 'admin_head', [ $this, 'register_settings_button' ] );
70 add_filter( 'admin_body_class', [ $this, 'admin_class' ] );
71 add_filter( 'list_table_primary_column', [ $this, 'set_primary_column' ], 20 );
72 add_action( 'admin_footer', [ $this, 'render_actions' ] );
73 add_filter( 'screen_settings', [ $this, 'screen_options' ] );
74 }
75
76 /**
77 * @return Button[]
78 */
79 public function get_buttons() {
80 return array_merge( [], ...$this->buttons );
81 }
82
83 /**
84 * @param Button $button
85 * @param int $priority
86 *
87 * @return bool
88 */
89 public function register_button( Button $button, $priority = 10 ) {
90 $this->buttons[ $priority ][] = $button;
91
92 ksort( $this->buttons, SORT_NUMERIC );
93
94 return true;
95 }
96
97 /**
98 * Set the primary columns. Used to place the actions bar.
99 *
100 * @param $default
101 *
102 * @return int|null|string
103 * @since 2.5.5
104 */
105 public function set_primary_column( $default ) {
106
107 if ( ! $this->list_screen->get_column_by_name( $default ) ) {
108 $default = key( $this->list_screen->get_columns() );
109 }
110
111 // If actions column is present, set it as primary
112 foreach ( $this->list_screen->get_columns() as $column ) {
113 if ( 'column-actions' === $column->get_type() ) {
114 $default = $column->get_name();
115
116 if ( $this->list_screen instanceof ListScreen\Media ) {
117
118 // Add download button to the actions column
119 add_filter( 'media_row_actions', [ $this, 'set_media_row_actions' ], 10, 2 );
120 }
121 }
122 }
123
124 // Set inline edit data if the default column (title) is not present
125 if ( $this->list_screen instanceof ListScreen\Post && 'title' !== $default ) {
126 add_filter( 'page_row_actions', [ $this, 'set_inline_edit_data' ], 20, 2 );
127 add_filter( 'post_row_actions', [ $this, 'set_inline_edit_data' ], 20, 2 );
128 }
129
130 // Remove inline edit action if the default column (author) is not present
131 if ( $this->list_screen instanceof ListScreen\Comment && 'comment' !== $default ) {
132 add_filter( 'comment_row_actions', [ $this, 'remove_quick_edit_from_actions' ], 20, 2 );
133 }
134
135 return $default;
136 }
137
138 /**
139 * Add a download link to the table screen
140 *
141 * @param array $actions
142 * @param WP_Post $post
143 *
144 * @return array
145 */
146 public function set_media_row_actions( $actions, $post ) {
147 $link_attributes = [
148 'download' => '',
149 'title' => __( 'Download', 'codepress-admin-columns' ),
150 ];
151 $actions['download'] = ac_helper()->html->link( wp_get_attachment_url( $post->ID ), __( 'Download', 'codepress-admin-columns' ), $link_attributes );
152
153 return $actions;
154 }
155
156 /**
157 * Sets the inline data when the title columns is not present on a AC\ListScreen_Post screen
158 *
159 * @param array $actions
160 * @param WP_Post $post
161 *
162 * @return array
163 */
164 public function set_inline_edit_data( $actions, $post ) {
165 get_inline_data( $post );
166
167 return $actions;
168 }
169
170 /**
171 * Remove quick edit from actions
172 *
173 * @param array $actions
174 *
175 * @return array
176 */
177 public function remove_quick_edit_from_actions( $actions ) {
178 unset( $actions['quickedit'] );
179
180 return $actions;
181 }
182
183 /**
184 * Adds a body class which is used to set individual column widths
185 *
186 * @param string $classes body classes
187 *
188 * @return string
189 * @since 1.4.0
190 */
191 public function admin_class( $classes ) {
192 $classes .= ' ac-' . $this->list_screen->get_key();
193
194 return apply_filters( 'ac/table/body_class', $classes, $this );
195 }
196
197 /**
198 * @since 3.2.5
199 */
200 public function register_settings_button() {
201 if ( ! current_user_can( Capabilities::MANAGE ) ) {
202 return;
203 }
204
205 $edit_button = new Settings\Option\EditButton();
206
207 if ( ! $edit_button->is_enabled() ) {
208 return;
209 }
210
211 $edit_link = $this->list_screen->get_edit_link();
212
213 if ( ! $edit_link ) {
214 return;
215 }
216
217 $button = new Button( 'edit-columns' );
218 $button->set_label( __( 'Edit columns', 'codepress-admin-columns' ) )
219 ->set_url( $edit_link )
220 ->set_dashicon( 'admin-generic' );
221
222 $this->register_button( $button, 1 );
223 }
224
225 /**
226 * @since 2.2.4
227 */
228 public function admin_scripts() {
229 $script = new Asset\Script( 'ac-table', $this->location->with_suffix( 'assets/js/table.js' ), [ 'jquery' ] );
230 $script->enqueue();
231
232 $style = new Asset\Style( 'ac-table', $this->location->with_suffix( 'assets/css/table.css' ) );
233 $style->enqueue();
234
235 wp_localize_script( 'ac-table', 'AC',
236 [
237 'assets' => $this->location->with_suffix( 'assets/' )->get_url(),
238 'list_screen' => $this->list_screen->get_key(),
239 'layout' => $this->list_screen->get_layout_id(),
240 'column_types' => $this->get_column_types_mapping(),
241 'ajax_nonce' => wp_create_nonce( 'ac-ajax' ),
242 'table_id' => $this->list_screen->get_table_attr_id(),
243 'screen' => $this->get_current_screen_id(),
244 'meta_type' => $this->list_screen->get_meta_type(),
245 'list_screen_link' => $this->get_list_screen_clear_link(),
246 ]
247 );
248
249 wp_localize_script( 'ac-table', 'AC_I18N',
250 [
251 'value_loading' => __( 'Loading...', 'codepress-admin-columns' ),
252 'edit' => __( 'Edit', 'codepress-admin-columns' ),
253 'download' => __( 'Download', 'codepress-admin-columns' ),
254 ]
255 );
256
257 /**
258 * @param ListScreen $list_screen
259 */
260 do_action( 'ac/table_scripts', $this->list_screen, $this );
261
262 // Column specific scripts
263 foreach ( $this->list_screen->get_columns() as $column ) {
264 $column->scripts();
265 }
266 }
267
268 /**
269 * @return string
270 */
271 private function get_list_screen_clear_link() {
272
273 $query_args_whitelist = [
274 'layout',
275 'orderby',
276 'order',
277 ];
278
279 switch ( true ) {
280 case $this->list_screen instanceof ListScreen\Post :
281 $query_args_whitelist[] = 'post_status';
282 break;
283 case $this->list_screen instanceof ListScreen\User :
284 $query_args_whitelist[] = 'role';
285 break;
286 case $this->list_screen instanceof ListScreen\Comment :
287 $query_args_whitelist[] = 'comment_status';
288 break;
289 }
290
291 $args = [];
292
293 foreach ( $query_args_whitelist as $query_arg ) {
294 if ( isset( $_GET[ $query_arg ] ) ) {
295 $args[ $query_arg ] = $_GET[ $query_arg ];
296 }
297 }
298
299 return add_query_arg( $args, $this->list_screen->get_screen_link() );
300 }
301
302 /**
303 * @return false|string
304 */
305 private function get_current_screen_id() {
306 $screen = get_current_screen();
307
308 if ( ! $screen ) {
309 return false;
310 }
311
312 return $screen->id;
313 }
314
315 /**
316 * @return array
317 */
318 private function get_column_types_mapping() {
319 $types = [];
320 foreach ( $this->list_screen->get_columns() as $column ) {
321 $types[ $column->get_name() ] = $column->get_type();
322 }
323
324 return $types;
325 }
326
327 /**
328 * @return ListScreen
329 */
330 public function get_list_screen() {
331 return $this->list_screen;
332 }
333
334 /**
335 * Admin header scripts
336 * @since 3.1.4
337 */
338 public function admin_head_scripts() {
339 $inline_style = new AC\Table\InlineStyle\ColumnSize(
340 $this->list_screen,
341 $this->column_size_list_storage,
342 $this->column_size_user_storage
343 );
344
345 echo $inline_style->render();
346
347 /**
348 * Add header scripts that only apply to column screens.
349 *
350 * @param ListScreen
351 * @param self
352 *
353 * @since 3.1.4
354 */
355 do_action( 'ac/admin_head', $this->list_screen, $this );
356 }
357
358 /**
359 * Admin footer scripts
360 * @since 1.4.0
361 */
362 public function admin_footer_scripts() {
363 /**
364 * Add footer scripts that only apply to column screens.
365 *
366 * @param ListScreen
367 * @param self
368 *
369 * @since 2.3.5
370 */
371 do_action( 'ac/admin_footer', $this->list_screen, $this );
372 }
373
374 /**
375 * @since 3.2.5
376 */
377 public function render_actions() {
378 ?>
379 <div id="ac-table-actions" class="ac-table-actions">
380
381 <?php $this->render_buttons(); ?>
382
383 <?php do_action( 'ac/table/actions', $this ); ?>
384 </div>
385 <?php
386 }
387
388 private function render_buttons() {
389 ?>
390 <div class="ac-table-actions-buttons">
391 <?php
392 foreach ( $this->get_buttons() as $button ) {
393 $button->render();
394 }
395 ?>
396 </div>
397 <?php
398 }
399
400 /**
401 * @param Renderable $option
402 */
403 public function register_screen_option( Renderable $option ) {
404 $this->screen_options[] = $option;
405 }
406
407 /**
408 * @param string $html
409 *
410 * @return string
411 */
412 public function screen_options( $html ) {
413 if ( empty( $this->screen_options ) ) {
414 return $html;
415 }
416
417 ob_start();
418 ?>
419
420 <fieldset class='acp-screen-option-prefs'>
421 <legend><?= __( 'Admin Columns', 'codepress-admin-columns' ); ?></legend>
422 <div class="acp-so-container">
423 <?php
424
425 foreach ( $this->screen_options as $option ) {
426 echo $option->render();
427 }
428
429 ?>
430 </div>
431 </fieldset>
432
433 <?php
434
435 $html .= ob_get_clean();
436
437 return $html;
438 }
439
440 }