Button.php
5 years ago
Preference.php
5 years ago
Screen.php
5 years ago
TableFormView.php
5 years ago
Screen.php
466 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( 'assets/external/qtip2/jquery.qtip.min.js' ), [ 'jquery' ] ); |
| 217 | $script->register(); |
| 218 | |
| 219 | $style = new Asset\Style( 'jquery-qtip2', $this->location->with_suffix( 'assets/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 | 'list_screen_link' => $this->get_list_screen_clear_link(), |
| 237 | ] |
| 238 | ); |
| 239 | |
| 240 | /** |
| 241 | * @param ListScreen $list_screen |
| 242 | */ |
| 243 | do_action( 'ac/table_scripts', $this->list_screen, $this ); |
| 244 | |
| 245 | // Column specific scripts |
| 246 | foreach ( $this->list_screen->get_columns() as $column ) { |
| 247 | $column->scripts(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * @return string |
| 253 | */ |
| 254 | private function get_list_screen_clear_link() { |
| 255 | |
| 256 | $query_args_whitelist = [ |
| 257 | 'layout', |
| 258 | 'orderby', |
| 259 | 'order', |
| 260 | ]; |
| 261 | |
| 262 | switch ( true ) { |
| 263 | case $this->list_screen instanceof ListScreen\Post : |
| 264 | $query_args_whitelist[] = 'post_status'; |
| 265 | break; |
| 266 | case $this->list_screen instanceof ListScreen\User : |
| 267 | $query_args_whitelist[] = 'role'; |
| 268 | break; |
| 269 | case $this->list_screen instanceof ListScreen\Comment : |
| 270 | $query_args_whitelist[] = 'comment_status'; |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | $args = []; |
| 275 | |
| 276 | foreach ( $query_args_whitelist as $query_arg ) { |
| 277 | if ( isset( $_GET[ $query_arg ] ) ) { |
| 278 | $args[ $query_arg ] = $_GET[ $query_arg ]; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return add_query_arg( $args, $this->list_screen->get_screen_link() ); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * @return false|string |
| 287 | */ |
| 288 | private function get_current_screen_id() { |
| 289 | $screen = get_current_screen(); |
| 290 | |
| 291 | if ( ! $screen ) { |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | return $screen->id; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * @return array |
| 300 | */ |
| 301 | private function get_column_types_mapping() { |
| 302 | $types = []; |
| 303 | foreach ( $this->list_screen->get_columns() as $column ) { |
| 304 | $types[ $column->get_name() ] = $column->get_type(); |
| 305 | } |
| 306 | |
| 307 | return $types; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * @return ListScreen |
| 312 | * @deprecated 3.2.5 |
| 313 | */ |
| 314 | public function get_current_list_screen() { |
| 315 | _deprecated_function( __METHOD__, '3.2.5', 'AC\Table\Screen::get_list_screen()' ); |
| 316 | |
| 317 | return $this->get_list_screen(); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * @return ListScreen |
| 322 | */ |
| 323 | public function get_list_screen() { |
| 324 | return $this->list_screen; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Applies the width setting to the table headers |
| 329 | */ |
| 330 | private function display_width_styles() { |
| 331 | if ( ! $this->list_screen->get_settings() ) { |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | // CSS: columns width |
| 336 | $css_column_width = false; |
| 337 | |
| 338 | foreach ( $this->list_screen->get_columns() as $column ) { |
| 339 | /* @var Settings\Column\Width $setting */ |
| 340 | $setting = $column->get_setting( 'width' ); |
| 341 | |
| 342 | $width = $setting->get_display_width(); |
| 343 | |
| 344 | if ( $width ) { |
| 345 | $css_column_width .= '.ac-' . esc_attr( $this->list_screen->get_key() ) . ' .wrap table th.column-' . esc_attr( $column->get_name() ) . ' { width: ' . $width . ' !important; }'; |
| 346 | $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; }'; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if ( ! $css_column_width ) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | ?> |
| 355 | |
| 356 | <style> |
| 357 | @media screen and (min-width: 783px) { |
| 358 | <?php echo $css_column_width; ?> |
| 359 | } |
| 360 | </style> |
| 361 | |
| 362 | <?php |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Admin header scripts |
| 367 | * @since 3.1.4 |
| 368 | */ |
| 369 | public function admin_head_scripts() { |
| 370 | $this->display_width_styles(); |
| 371 | |
| 372 | /** |
| 373 | * Add header scripts that only apply to column screens. |
| 374 | * |
| 375 | * @param ListScreen |
| 376 | * @param self |
| 377 | * |
| 378 | * @since 3.1.4 |
| 379 | */ |
| 380 | do_action( 'ac/admin_head', $this->list_screen, $this ); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Admin footer scripts |
| 385 | * @since 1.4.0 |
| 386 | */ |
| 387 | public function admin_footer_scripts() { |
| 388 | /** |
| 389 | * Add footer scripts that only apply to column screens. |
| 390 | * |
| 391 | * @param ListScreen |
| 392 | * @param self |
| 393 | * |
| 394 | * @since 2.3.5 |
| 395 | */ |
| 396 | do_action( 'ac/admin_footer', $this->list_screen, $this ); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * @since 3.2.5 |
| 401 | */ |
| 402 | public function render_actions() { |
| 403 | ?> |
| 404 | <div id="ac-table-actions" class="ac-table-actions"> |
| 405 | |
| 406 | <?php $this->render_buttons(); ?> |
| 407 | |
| 408 | <?php do_action( 'ac/table/actions', $this ); ?> |
| 409 | </div> |
| 410 | <?php |
| 411 | } |
| 412 | |
| 413 | private function render_buttons() { |
| 414 | if ( ! $this->get_buttons() ) { |
| 415 | return; |
| 416 | } |
| 417 | ?> |
| 418 | <div class="ac-table-actions-buttons"> |
| 419 | <?php |
| 420 | foreach ( $this->get_buttons() as $button ) { |
| 421 | $button->render(); |
| 422 | } |
| 423 | ?> |
| 424 | </div> |
| 425 | <?php |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * @param Form\Element $option |
| 430 | */ |
| 431 | public function register_screen_option( AC\Form\Element $option ) { |
| 432 | $this->screen_options[] = $option; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * @param string $html |
| 437 | * |
| 438 | * @return string |
| 439 | */ |
| 440 | public function screen_options( $html ) { |
| 441 | if ( empty( $this->screen_options ) ) { |
| 442 | return $html; |
| 443 | } |
| 444 | |
| 445 | ob_start(); |
| 446 | ?> |
| 447 | |
| 448 | <fieldset class='acp-screen-option-prefs'> |
| 449 | <legend><?= __( 'Admin Columns', 'codepress-admin-columns' ); ?></legend> |
| 450 | <?php |
| 451 | |
| 452 | foreach ( $this->screen_options as $option ) { |
| 453 | echo $option->render(); |
| 454 | } |
| 455 | |
| 456 | ?> |
| 457 | </fieldset> |
| 458 | |
| 459 | <?php |
| 460 | |
| 461 | $html .= ob_get_clean(); |
| 462 | |
| 463 | return $html; |
| 464 | } |
| 465 | |
| 466 | } |