PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 4.1.6
Admin Columns v4.1.6
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 6 years ago Preference.php 6 years ago Screen.php 6 years ago TableFormView.php 6 years ago
Screen.php
431 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
215 // Tooltip
216 $script = new Asset\Script( 'jquery-qtip2', $this->location->with_suffix( 'external/qtip2/jquery.qtip.min.js' ), [ 'jquery' ] );
217 $script->register();
218
219 $style = new Asset\Style( 'jquery-qtip2', $this->location->with_suffix( 'external/qtip2/jquery.qtip.min.css' ) );
220 $style->enqueue();
221
222 $script = new Asset\Script( 'ac-table', $this->location->with_suffix( 'assets/js/table.js' ), [ 'jquery', 'jquery-qtip2' ] );
223 $script->enqueue();
224
225 $style = new Asset\Style( 'ac-table', $this->location->with_suffix( 'assets/css/table.css' ) );
226 $style->enqueue();
227
228 wp_localize_script( 'ac-table', 'AC', [
229 'list_screen' => $this->list_screen->get_key(),
230 'layout' => $this->list_screen->get_layout_id(),
231 'column_types' => $this->get_column_types_mapping(),
232 'ajax_nonce' => wp_create_nonce( 'ac-ajax' ),
233 'table_id' => $this->list_screen->get_table_attr_id(),
234 'screen' => $this->get_current_screen_id(),
235 'meta_type' => $this->list_screen->get_meta_type(),
236 ]
237 );
238
239 /**
240 * @param ListScreen $list_screen
241 */
242 do_action( 'ac/table_scripts', $this->list_screen, $this );
243
244 // Column specific scripts
245 foreach ( $this->list_screen->get_columns() as $column ) {
246 $column->scripts();
247 }
248 }
249
250 /**
251 * @return false|string
252 */
253 private function get_current_screen_id() {
254 $screen = get_current_screen();
255
256 if ( ! $screen ) {
257 return false;
258 }
259
260 return $screen->id;
261 }
262
263 /**
264 * @return array
265 */
266 private function get_column_types_mapping() {
267 $types = [];
268 foreach ( $this->list_screen->get_columns() as $column ) {
269 $types[ $column->get_name() ] = $column->get_type();
270 }
271
272 return $types;
273 }
274
275 /**
276 * @return ListScreen
277 * @deprecated 3.2.5
278 */
279 public function get_current_list_screen() {
280 _deprecated_function( __METHOD__, '3.2.5', 'AC\Table\Screen::get_list_screen()' );
281
282 return $this->get_list_screen();
283 }
284
285 /**
286 * @return ListScreen
287 */
288 public function get_list_screen() {
289 return $this->list_screen;
290 }
291
292 /**
293 * Applies the width setting to the table headers
294 */
295 private function display_width_styles() {
296 if ( ! $this->list_screen->get_settings() ) {
297 return;
298 }
299
300 // CSS: columns width
301 $css_column_width = false;
302
303 foreach ( $this->list_screen->get_columns() as $column ) {
304 /* @var Settings\Column\Width $setting */
305 $setting = $column->get_setting( 'width' );
306
307 $width = $setting->get_display_width();
308
309 if ( $width ) {
310 $css_column_width .= '.ac-' . esc_attr( $this->list_screen->get_key() ) . ' .wrap table th.column-' . esc_attr( $column->get_name() ) . ' { width: ' . $width . ' !important; }';
311 $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; }';
312 }
313 }
314
315 if ( ! $css_column_width ) {
316 return;
317 }
318
319 ?>
320
321 <style>
322 @media screen and (min-width: 783px) {
323 <?php echo $css_column_width; ?>
324 }
325 </style>
326
327 <?php
328 }
329
330 /**
331 * Admin header scripts
332 * @since 3.1.4
333 */
334 public function admin_head_scripts() {
335 $this->display_width_styles();
336
337 /**
338 * Add header scripts that only apply to column screens.
339 *
340 * @param ListScreen
341 * @param self
342 *
343 * @since 3.1.4
344 */
345 do_action( 'ac/admin_head', $this->list_screen, $this );
346 }
347
348 /**
349 * Admin footer scripts
350 * @since 1.4.0
351 */
352 public function admin_footer_scripts() {
353 /**
354 * Add footer scripts that only apply to column screens.
355 *
356 * @param ListScreen
357 * @param self
358 *
359 * @since 2.3.5
360 */
361 do_action( 'ac/admin_footer', $this->list_screen, $this );
362 }
363
364 /**
365 * @since 3.2.5
366 */
367 public function render_actions() {
368 ?>
369 <div id="ac-table-actions" class="ac-table-actions">
370
371 <?php $this->render_buttons(); ?>
372
373 <?php do_action( 'ac/table/actions', $this ); ?>
374 </div>
375 <?php
376 }
377
378 private function render_buttons() {
379 if ( ! $this->get_buttons() ) {
380 return;
381 }
382 ?>
383 <div class="ac-table-actions-buttons">
384 <?php
385 foreach ( $this->get_buttons() as $button ) {
386 $button->render();
387 }
388 ?>
389 </div>
390 <?php
391 }
392
393 /**
394 * @param Form\Element $option
395 */
396 public function register_screen_option( AC\Form\Element $option ) {
397 $this->screen_options[] = $option;
398 }
399
400 /**
401 * @param string $html
402 *
403 * @return string
404 */
405 public function screen_options( $html ) {
406 if ( empty( $this->screen_options ) ) {
407 return $html;
408 }
409
410 ob_start();
411 ?>
412
413 <fieldset class='acp-screen-option-prefs'>
414 <legend><?= __( 'Admin Columns', 'codepress-admin-columns' ); ?></legend>
415 <?php
416
417 foreach ( $this->screen_options as $option ) {
418 echo $option->render();
419 }
420
421 ?>
422 </fieldset>
423
424 <?php
425
426 $html .= ob_get_clean();
427
428 return $html;
429 }
430
431 }