PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 4.4.4
Admin Columns v4.4.4
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
Button.php 4 years ago Preference.php 4 years ago Screen.php 4 years ago TableFormView.php 4 years ago
Screen.php
473 lines
1 <?php
2
3 namespace AC\Table;
4
5 use AC;
6 use AC\Asset;
7 use AC\Capabilities;
8 use AC\Form;
9 use AC\ListScreen;
10 use AC\Registrable;
11 use AC\Settings;
12 use WP_Post;
13
14 final class Screen implements Registrable {
15
16 /**
17 * @var ListScreen $list_screen
18 */
19 private $list_screen;
20
21 /**
22 * @var Form\Element[]
23 */
24 private $screen_options;
25
26 /**
27 * @var Button[]
28 */
29 private $buttons = [];
30
31 /**
32 * @var Asset\Location\Absolute
33 */
34 private $location;
35
36 public function __construct( Asset\Location\Absolute $location, ListScreen $list_screen ) {
37 $this->location = $location;
38 $this->list_screen = $list_screen;
39 }
40
41 /**
42 * Register hooks
43 */
44 public function register() {
45 $controller = new AC\ScreenController( $this->list_screen );
46 $controller->register();
47
48 $render = new TableFormView( $this->list_screen->get_meta_type(), sprintf( '<input type="hidden" name="layout" value="%s">', $this->list_screen->get_layout_id() ) );
49 $render->register();
50
51 add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] );
52 add_action( 'admin_footer', [ $this, 'admin_footer_scripts' ] );
53 add_action( 'admin_head', [ $this, 'admin_head_scripts' ] );
54 add_action( 'admin_head', [ $this, 'register_settings_button' ] );
55 add_filter( 'admin_body_class', [ $this, 'admin_class' ] );
56 add_filter( 'list_table_primary_column', [ $this, 'set_primary_column' ], 20 );
57 add_action( 'admin_footer', [ $this, 'render_actions' ] );
58 add_filter( 'screen_settings', [ $this, 'screen_options' ] );
59 }
60
61 /**
62 * @return Button[]
63 */
64 public function get_buttons() {
65 return array_merge( [], ...$this->buttons );
66 }
67
68 /**
69 * @param Button $button
70 * @param int $priority
71 *
72 * @return bool
73 */
74 public function register_button( Button $button, $priority = 10 ) {
75 $this->buttons[ $priority ][] = $button;
76
77 ksort( $this->buttons, SORT_NUMERIC );
78
79 return true;
80 }
81
82 /**
83 * Set the primary columns for the Admin Columns columns. Used to place the actions bar.
84 *
85 * @param $default
86 *
87 * @return int|null|string
88 * @since 2.5.5
89 */
90 public function set_primary_column( $default ) {
91
92 if ( ! $this->list_screen->get_column_by_name( $default ) ) {
93 $default = key( $this->list_screen->get_columns() );
94 }
95
96 // If actions column is present, set it as primary
97 foreach ( $this->list_screen->get_columns() as $column ) {
98 if ( 'column-actions' === $column->get_type() ) {
99 $default = $column->get_name();
100
101 if ( $this->list_screen instanceof ListScreen\Media ) {
102
103 // Add download button to the actions column
104 add_filter( 'media_row_actions', [ $this, 'set_media_row_actions' ], 10, 2 );
105 }
106 }
107 }
108
109 // Set inline edit data if the default column (title) is not present
110 if ( $this->list_screen instanceof ListScreen\Post && 'title' !== $default ) {
111 add_filter( 'page_row_actions', [ $this, 'set_inline_edit_data' ], 20, 2 );
112 add_filter( 'post_row_actions', [ $this, 'set_inline_edit_data' ], 20, 2 );
113 }
114
115 // Remove inline edit action if the default column (author) is not present
116 if ( $this->list_screen instanceof ListScreen\Comment && 'comment' !== $default ) {
117 add_filter( 'comment_row_actions', [ $this, 'remove_quick_edit_from_actions' ], 20, 2 );
118 }
119
120 return $default;
121 }
122
123 /**
124 * Add a download link to the table screen
125 *
126 * @param array $actions
127 * @param WP_Post $post
128 *
129 * @return array
130 */
131 public function set_media_row_actions( $actions, $post ) {
132 $link_attributes = [
133 'download' => '',
134 'title' => __( 'Download', 'codepress-admin-columns' ),
135 ];
136 $actions['download'] = ac_helper()->html->link( wp_get_attachment_url( $post->ID ), __( 'Download', 'codepress-admin-columns' ), $link_attributes );
137
138 return $actions;
139 }
140
141 /**
142 * Sets the inline data when the title columns is not present on a AC\ListScreen_Post screen
143 *
144 * @param array $actions
145 * @param WP_Post $post
146 *
147 * @return array
148 */
149 public function set_inline_edit_data( $actions, $post ) {
150 get_inline_data( $post );
151
152 return $actions;
153 }
154
155 /**
156 * Remove quick edit from actions
157 *
158 * @param array $actions
159 *
160 * @return array
161 */
162 public function remove_quick_edit_from_actions( $actions ) {
163 unset( $actions['quickedit'] );
164
165 return $actions;
166 }
167
168 /**
169 * Adds a body class which is used to set individual column widths
170 *
171 * @param string $classes body classes
172 *
173 * @return string
174 * @since 1.4.0
175 */
176 public function admin_class( $classes ) {
177 $classes .= ' ac-' . $this->list_screen->get_key();
178
179 return apply_filters( 'ac/table/body_class', $classes, $this );
180 }
181
182 /**
183 * @since 3.2.5
184 */
185 public function register_settings_button() {
186 if ( ! current_user_can( Capabilities::MANAGE ) ) {
187 return;
188 }
189
190 $edit_button = new Settings\Option\EditButton();
191
192 if ( ! $edit_button->is_enabled() ) {
193 return;
194 }
195
196 $edit_link = $this->list_screen->get_edit_link();
197
198 if ( ! $edit_link ) {
199 return;
200 }
201
202 $button = new Button( 'edit-columns' );
203 $button->set_label( __( 'Edit columns', 'codepress-admin-columns' ) )
204 ->set_url( $edit_link )
205 ->set_dashicon( 'admin-generic' );
206
207 $this->register_button( $button, 1 );
208 }
209
210 /**
211 * @since 2.2.4
212 */
213 public function admin_scripts() {
214 $script = new Asset\Script( 'ac-table', $this->location->with_suffix( 'assets/js/table.js' ), [ 'jquery' ] );
215 $script->enqueue();
216
217 $style = new Asset\Style( 'ac-table', $this->location->with_suffix( 'assets/css/table.css' ) );
218 $style->enqueue();
219
220 wp_localize_script( 'ac-table', 'AC', [
221 'list_screen' => $this->list_screen->get_key(),
222 'layout' => $this->list_screen->get_layout_id(),
223 'column_types' => $this->get_column_types_mapping(),
224 'ajax_nonce' => wp_create_nonce( 'ac-ajax' ),
225 'table_id' => $this->list_screen->get_table_attr_id(),
226 'screen' => $this->get_current_screen_id(),
227 'meta_type' => $this->list_screen->get_meta_type(),
228 'list_screen_link' => $this->get_list_screen_clear_link(),
229 'column_widths' => $this->get_column_widths(),
230 ]
231 );
232
233 /**
234 * @param ListScreen $list_screen
235 */
236 do_action( 'ac/table_scripts', $this->list_screen, $this );
237
238 // Column specific scripts
239 foreach ( $this->list_screen->get_columns() as $column ) {
240 $column->scripts();
241 }
242 }
243
244 /**
245 * @return string
246 */
247 private function get_list_screen_clear_link() {
248
249 $query_args_whitelist = [
250 'layout',
251 'orderby',
252 'order',
253 ];
254
255 switch ( true ) {
256 case $this->list_screen instanceof ListScreen\Post :
257 $query_args_whitelist[] = 'post_status';
258 break;
259 case $this->list_screen instanceof ListScreen\User :
260 $query_args_whitelist[] = 'role';
261 break;
262 case $this->list_screen instanceof ListScreen\Comment :
263 $query_args_whitelist[] = 'comment_status';
264 break;
265 }
266
267 $args = [];
268
269 foreach ( $query_args_whitelist as $query_arg ) {
270 if ( isset( $_GET[ $query_arg ] ) ) {
271 $args[ $query_arg ] = $_GET[ $query_arg ];
272 }
273 }
274
275 return add_query_arg( $args, $this->list_screen->get_screen_link() );
276 }
277
278 /**
279 * @return false|string
280 */
281 private function get_current_screen_id() {
282 $screen = get_current_screen();
283
284 if ( ! $screen ) {
285 return false;
286 }
287
288 return $screen->id;
289 }
290
291 /**
292 * @return array
293 */
294 private function get_column_types_mapping() {
295 $types = [];
296 foreach ( $this->list_screen->get_columns() as $column ) {
297 $types[ $column->get_name() ] = $column->get_type();
298 }
299
300 return $types;
301 }
302
303 /**
304 * @return ListScreen
305 */
306 public function get_list_screen() {
307 return $this->list_screen;
308 }
309
310 /**
311 * @return array
312 */
313 private function get_column_widths() {
314 $result = [];
315 if ( ! $this->list_screen->get_settings() ) {
316 return $result;
317 }
318
319 foreach ( $this->list_screen->get_columns() as $column ) {
320 /* @var Settings\Column\Width $setting */
321 $setting = $column->get_setting( 'width' );
322
323 $result[ $column->get_name() ] = [
324 'width' => $setting->get_width(),
325 'width_unit' => $setting->get_width_unit(),
326 ];
327 }
328
329 return $result;
330 }
331
332 /**
333 * Applies the width setting to the table headers
334 */
335 private function display_width_styles() {
336 if ( ! $this->list_screen->get_settings() ) {
337 return;
338 }
339
340 // CSS: columns width
341 $css_column_width = false;
342
343 foreach ( $this->list_screen->get_columns() as $column ) {
344 /* @var Settings\Column\Width $setting */
345 $setting = $column->get_setting( 'width' );
346
347 $width = $setting->get_display_width();
348
349 if ( $width ) {
350 $css_column_width .= '.ac-' . esc_attr( $this->list_screen->get_key() ) . ' .wrap table th.column-' . esc_attr( $column->get_name() ) . ' { width: ' . $width . ' !important; }';
351 $css_column_width .= 'body.acp-overflow-table.ac-' . esc_attr( $this->list_screen->get_key() ) . ' .wrap th.column-' . esc_attr( $column->get_name() ) . ' { min-width: ' . $width . ' !important; }';
352 }
353 }
354
355 if ( ! $css_column_width ) {
356 return;
357 }
358
359 ?>
360
361 <style>
362 @media screen and (min-width: 783px) {
363 <?php echo $css_column_width; ?>
364 }
365 </style>
366
367 <?php
368 }
369
370 /**
371 * Admin header scripts
372 * @since 3.1.4
373 */
374 public function admin_head_scripts() {
375 $this->display_width_styles();
376
377 /**
378 * Add header scripts that only apply to column screens.
379 *
380 * @param ListScreen
381 * @param self
382 *
383 * @since 3.1.4
384 */
385 do_action( 'ac/admin_head', $this->list_screen, $this );
386 }
387
388 /**
389 * Admin footer scripts
390 * @since 1.4.0
391 */
392 public function admin_footer_scripts() {
393 /**
394 * Add footer scripts that only apply to column screens.
395 *
396 * @param ListScreen
397 * @param self
398 *
399 * @since 2.3.5
400 */
401 do_action( 'ac/admin_footer', $this->list_screen, $this );
402 }
403
404 /**
405 * @since 3.2.5
406 */
407 public function render_actions() {
408 ?>
409 <div id="ac-table-actions" class="ac-table-actions">
410
411 <?php $this->render_buttons(); ?>
412
413 <?php do_action( 'ac/table/actions', $this ); ?>
414 </div>
415 <?php
416 }
417
418 private function render_buttons() {
419 if ( ! $this->get_buttons() ) {
420 return;
421 }
422 ?>
423 <div class="ac-table-actions-buttons">
424 <?php
425 foreach ( $this->get_buttons() as $button ) {
426 $button->render();
427 }
428 ?>
429 </div>
430 <?php
431 }
432
433 /**
434 * @param Form\Element $option
435 */
436 public function register_screen_option( AC\Form\Element $option ) {
437 $this->screen_options[] = $option;
438 }
439
440 /**
441 * @param string $html
442 *
443 * @return string
444 */
445 public function screen_options( $html ) {
446 if ( empty( $this->screen_options ) ) {
447 return $html;
448 }
449
450 ob_start();
451 ?>
452
453 <fieldset class='acp-screen-option-prefs'>
454 <legend><?= __( 'Admin Columns', 'codepress-admin-columns' ); ?></legend>
455 <div class="acp-so-container">
456 <?php
457
458 foreach ( $this->screen_options as $option ) {
459 echo $option->render();
460 }
461
462 ?>
463 </div>
464 </fieldset>
465
466 <?php
467
468 $html .= ob_get_clean();
469
470 return $html;
471 }
472
473 }