Admin
8 years ago
Column
8 years ago
Form
8 years ago
Helper
8 years ago
ListScreen
8 years ago
Meta
8 years ago
Notice
8 years ago
Plugin
8 years ago
Preferences
8 years ago
Relation
8 years ago
Settings
8 years ago
ThirdParty
8 years ago
API.php
8 years ago
Addon.php
8 years ago
Admin.php
8 years ago
Autoloader.php
8 years ago
Collection.php
8 years ago
Column.php
8 years ago
Groups.php
8 years ago
Helper.php
8 years ago
ListScreen.php
8 years ago
ListScreenPost.php
8 years ago
ListScreenWP.php
8 years ago
Plugin.php
8 years ago
PluginInformation.php
8 years ago
Preferences.php
8 years ago
Relation.php
8 years ago
TableScreen.php
8 years ago
View.php
8 years ago
ViewInterface.php
8 years ago
TableScreen.php
509 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | final class AC_TableScreen { |
| 8 | |
| 9 | /** |
| 10 | * @var array $column_headings |
| 11 | */ |
| 12 | private $column_headings = array(); |
| 13 | |
| 14 | /** |
| 15 | * @var AC_ListScreen $list_screen |
| 16 | */ |
| 17 | private $current_list_screen; |
| 18 | |
| 19 | public function __construct() { |
| 20 | add_action( 'current_screen', array( $this, 'load_list_screen' ) ); |
| 21 | add_action( 'admin_init', array( $this, 'load_list_screen_doing_quick_edit' ) ); |
| 22 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) ); |
| 23 | add_action( 'admin_footer', array( $this, 'admin_footer_scripts' ) ); |
| 24 | add_action( 'admin_head', array( $this, 'admin_head_scripts' ) ); |
| 25 | add_filter( 'admin_body_class', array( $this, 'admin_class' ) ); |
| 26 | add_filter( 'list_table_primary_column', array( $this, 'set_primary_column' ), 20 ); |
| 27 | add_action( 'wp_ajax_ac_get_column_value', array( $this, 'ajax_get_column_value' ) ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Get column value by ajax. |
| 32 | */ |
| 33 | public function ajax_get_column_value() { |
| 34 | check_ajax_referer( 'ac-ajax' ); |
| 35 | |
| 36 | // Get ID of entry to edit |
| 37 | $id = intval( filter_input( INPUT_POST, 'pk' ) ); |
| 38 | |
| 39 | if ( ! $id ) { |
| 40 | $this->ajax_error( __( 'Invalid item ID.', 'codepress-admin-columns' ) ); |
| 41 | } |
| 42 | |
| 43 | $list_screen = AC()->get_list_screen( filter_input( INPUT_POST, 'list_screen' ) ); |
| 44 | |
| 45 | if ( ! $list_screen ) { |
| 46 | $this->ajax_error( __( 'Invalid list screen.', 'codepress-admin-columns' ) ); |
| 47 | } |
| 48 | |
| 49 | $list_screen->set_layout_id( filter_input( INPUT_POST, 'layout' ) ); |
| 50 | |
| 51 | $column = $list_screen->get_column_by_name( filter_input( INPUT_POST, 'column' ) ); |
| 52 | |
| 53 | if ( ! $column ) { |
| 54 | $this->ajax_error( __( 'Invalid column.', 'codepress-admin-columns' ) ); |
| 55 | } |
| 56 | |
| 57 | if ( ! $column instanceof AC_Column_AjaxValue ) { |
| 58 | $this->ajax_error( __( 'Invalid method.', 'codepress-admin-columns' ) ); |
| 59 | } |
| 60 | |
| 61 | // Trigger ajax callback |
| 62 | echo $column->get_ajax_value( $id ); |
| 63 | exit; |
| 64 | } |
| 65 | |
| 66 | private function ajax_error( $message ) { |
| 67 | wp_die( $message, null, 400 ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Set the primary columns for the Admin Columns columns. Used to place the actions bar. |
| 72 | * |
| 73 | * @since 2.5.5 |
| 74 | */ |
| 75 | public function set_primary_column( $default ) { |
| 76 | if ( $this->current_list_screen ) { |
| 77 | |
| 78 | if ( ! $this->current_list_screen->get_column_by_name( $default ) ) { |
| 79 | $default = key( $this->current_list_screen->get_columns() ); |
| 80 | } |
| 81 | |
| 82 | // If actions column is present, set it as primary |
| 83 | foreach ( $this->current_list_screen->get_columns() as $column ) { |
| 84 | if ( 'column-actions' == $column->get_type() ) { |
| 85 | $default = $column->get_name(); |
| 86 | |
| 87 | if ( $this->current_list_screen instanceof AC_ListScreen_Media ) { |
| 88 | |
| 89 | // Add download button to the actions column |
| 90 | add_filter( 'media_row_actions', array( $this, 'set_media_row_actions' ), 10, 2 ); |
| 91 | } |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | // Set inline edit data if the default column (title) is not present |
| 96 | if ( $this->current_list_screen instanceof AC_ListScreen_Post && 'title' !== $default ) { |
| 97 | add_filter( 'page_row_actions', array( $this, 'set_inline_edit_data' ), 20, 2 ); |
| 98 | add_filter( 'post_row_actions', array( $this, 'set_inline_edit_data' ), 20, 2 ); |
| 99 | } |
| 100 | |
| 101 | // Remove inline edit action if the default column (author) is not present |
| 102 | if ( $this->current_list_screen instanceof AC_ListScreen_Comment && 'comment' !== $default ) { |
| 103 | add_filter( 'comment_row_actions', array( $this, 'remove_quick_edit_from_actions' ), 20, 2 ); |
| 104 | } |
| 105 | |
| 106 | // Adds the default hidden bulk edit markup for the new primary column |
| 107 | if ( $this->current_list_screen instanceof ACP_ListScreen_Taxonomy && 'name' !== $default ) { |
| 108 | add_filter( 'tag_row_actions', array( $this, 'add_taxonomy_hidden_quick_edit_markup' ), 20, 2 ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $default; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Add a download link to the table screen |
| 117 | * |
| 118 | * @param array $actions |
| 119 | * @param WP_Post $post |
| 120 | */ |
| 121 | public function set_media_row_actions( $actions, $post ) { |
| 122 | $link_attributes = array( |
| 123 | 'download' => '', |
| 124 | 'title' => __( 'Download', 'codepress-admin-columns' ), |
| 125 | ); |
| 126 | $actions['download'] = ac_helper()->html->link( wp_get_attachment_url( $post->ID ), __( 'Download', 'codepress-admin-columns' ), $link_attributes ); |
| 127 | |
| 128 | return $actions; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Sets the inline data when the title columns is not present on a AC_ListScreen_Post screen |
| 133 | * |
| 134 | * @param array $actions |
| 135 | * @param WP_Post $post |
| 136 | */ |
| 137 | public function set_inline_edit_data( $actions, $post ) { |
| 138 | get_inline_data( $post ); |
| 139 | |
| 140 | return $actions; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Remove quick edit from actions |
| 145 | * |
| 146 | * @param array $actions |
| 147 | */ |
| 148 | public function remove_quick_edit_from_actions( $actions ) { |
| 149 | unset( $actions['quickedit'] ); |
| 150 | |
| 151 | return $actions; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Add the default markup for the default primary column for the Taxonomy list screen which is necessary for bulk edit |
| 156 | * |
| 157 | * @param $actions |
| 158 | * @param $term |
| 159 | */ |
| 160 | public function add_taxonomy_hidden_quick_edit_markup( $actions, $term ) { |
| 161 | $list_screen = $this->get_current_list_screen(); |
| 162 | |
| 163 | if ( $list_screen instanceof ACP_ListScreen_Taxonomy ) { |
| 164 | echo sprintf( '<div class="hidden">%s</div>', $list_screen->get_list_table()->column_name( $term ) ); |
| 165 | } |
| 166 | |
| 167 | return $actions; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Adds a body class which is used to set individual column widths |
| 172 | * |
| 173 | * @since 1.4.0 |
| 174 | * |
| 175 | * @param string $classes body classes |
| 176 | * |
| 177 | * @return string |
| 178 | */ |
| 179 | public function admin_class( $classes ) { |
| 180 | if ( ! $this->current_list_screen ) { |
| 181 | return $classes; |
| 182 | } |
| 183 | |
| 184 | $classes .= " ac-" . $this->current_list_screen->get_key(); |
| 185 | |
| 186 | return apply_filters( 'ac/table/body_class', $classes, $this ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @since 2.2.4 |
| 191 | * |
| 192 | * @param AC_ListScreen $list_screen |
| 193 | */ |
| 194 | public function admin_scripts() { |
| 195 | if ( ! $this->current_list_screen ) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | $list_screen = $this->current_list_screen; |
| 200 | |
| 201 | // Tooltip |
| 202 | wp_register_script( 'jquery-qtip2', AC()->get_plugin_url() . "external/qtip2/jquery.qtip.min.js", array( 'jquery' ), AC()->get_version() ); |
| 203 | wp_enqueue_style( 'jquery-qtip2', AC()->get_plugin_url() . "external/qtip2/jquery.qtip.min.css", array(), AC()->get_version() ); |
| 204 | |
| 205 | // Main |
| 206 | wp_enqueue_script( 'ac-table', AC()->get_plugin_url() . "assets/js/table.js", array( 'jquery', 'jquery-qtip2' ), AC()->get_version() ); |
| 207 | wp_enqueue_style( 'ac-table', AC()->get_plugin_url() . "assets/css/table.css", array(), AC()->get_version() ); |
| 208 | |
| 209 | wp_localize_script( 'ac-table', 'AC', array( |
| 210 | 'list_screen' => $list_screen->get_key(), |
| 211 | 'layout' => $list_screen->get_layout_id(), |
| 212 | 'column_types' => $this->get_column_types_mapping( $list_screen ), |
| 213 | 'ajax_nonce' => wp_create_nonce( 'ac-ajax' ), |
| 214 | 'table_id' => $list_screen->get_table_attr_id(), |
| 215 | 'edit_link' => $this->get_edit_link( $list_screen ), |
| 216 | 'screen' => $this->get_current_screen_id(), |
| 217 | 'i18n' => array( |
| 218 | 'edit_columns' => esc_html( __( 'Edit columns', 'codepress-admin-columns' ) ), |
| 219 | ), |
| 220 | ) |
| 221 | ); |
| 222 | |
| 223 | /** |
| 224 | * @param AC_ListScreen $list_screen |
| 225 | */ |
| 226 | do_action( 'ac/table_scripts', $list_screen ); |
| 227 | |
| 228 | // Column specific scripts |
| 229 | foreach ( $list_screen->get_columns() as $column ) { |
| 230 | $column->scripts(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @return false|string |
| 236 | */ |
| 237 | private function get_current_screen_id() { |
| 238 | $screen = get_current_screen(); |
| 239 | |
| 240 | if ( ! $screen ) { |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | return $screen->id; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * @param AC_ListScreen $list_screen |
| 249 | * |
| 250 | * @return array |
| 251 | */ |
| 252 | private function get_column_types_mapping( AC_ListScreen $list_screen ) { |
| 253 | $types = array(); |
| 254 | foreach ( $list_screen->get_columns() as $column ) { |
| 255 | $types[ $column->get_name() ] = $column->get_type(); |
| 256 | } |
| 257 | |
| 258 | return $types; |
| 259 | } |
| 260 | |
| 261 | public function get_current_list_screen() { |
| 262 | return $this->current_list_screen; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Applies the width setting to the table headers |
| 267 | */ |
| 268 | private function display_width_styles() { |
| 269 | if ( ! $this->current_list_screen || ! $this->current_list_screen->get_settings() ) { |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | // CSS: columns width |
| 274 | $css_column_width = false; |
| 275 | |
| 276 | foreach ( $this->current_list_screen->get_columns() as $column ) { |
| 277 | /* @var AC_Settings_Column_Width $setting */ |
| 278 | $setting = $column->get_setting( 'width' ); |
| 279 | |
| 280 | if ( $width = $setting->get_display_width() ) { |
| 281 | $css_column_width .= ".ac-" . esc_attr( $this->current_list_screen->get_key() ) . " .wrap table th.column-" . esc_attr( $column->get_name() ) . " { width: " . $width . " !important; }"; |
| 282 | $css_column_width .= "body.acp-overflow-table.ac-" . esc_attr( $this->current_list_screen->get_key() ) . " .wrap th.column-" . esc_attr( $column->get_name() ) . " { min-width: " . $width . " !important; }"; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | if ( ! $css_column_width ) { |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | ?> |
| 291 | |
| 292 | <style> |
| 293 | @media screen and (min-width: 783px) { |
| 294 | <?php echo $css_column_width; ?> |
| 295 | } |
| 296 | </style> |
| 297 | |
| 298 | <?php |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * @param AC_ListScreen $list_screen |
| 303 | * |
| 304 | * @return string|false |
| 305 | */ |
| 306 | private function get_edit_link( AC_ListScreen $list_screen ) { |
| 307 | if ( ! AC()->user_can_manage_admin_columns() ) { |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | /* @var AC_Admin_Page_Settings $settings */ |
| 312 | $settings = AC()->admin()->get_page( 'settings' ); |
| 313 | |
| 314 | if ( ! $settings->show_edit_button() ) { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | return $list_screen->get_edit_link(); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Admin header scripts |
| 323 | * |
| 324 | * @since 3.1.4 |
| 325 | */ |
| 326 | public function admin_head_scripts() { |
| 327 | if ( ! $this->current_list_screen ) { |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | $this->display_width_styles(); |
| 332 | |
| 333 | /** |
| 334 | * Add header scripts that only apply to column screens. |
| 335 | * |
| 336 | * @since 3.1.4 |
| 337 | * |
| 338 | * @param AC_ListScreen |
| 339 | * @param AC_TableScreen |
| 340 | */ |
| 341 | do_action( 'ac/admin_head', $this->current_list_screen, $this ); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Admin footer scripts |
| 346 | * |
| 347 | * @since 1.4.0 |
| 348 | */ |
| 349 | public function admin_footer_scripts() { |
| 350 | if ( ! $this->current_list_screen ) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Add footer scripts that only apply to column screens. |
| 356 | * |
| 357 | * @since 2.3.5 |
| 358 | * |
| 359 | * @param AC_ListScreen |
| 360 | * @param AC_TableScreen |
| 361 | */ |
| 362 | do_action( 'ac/admin_footer', $this->current_list_screen, $this ); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Load current list screen |
| 367 | * |
| 368 | * @param WP_Screen $wp_screen |
| 369 | */ |
| 370 | public function load_list_screen( $wp_screen ) { |
| 371 | if ( ! $wp_screen instanceof WP_Screen ) { |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | foreach ( AC()->get_list_screens() as $list_screen ) { |
| 376 | if ( $list_screen->is_current_screen( $wp_screen ) ) { |
| 377 | |
| 378 | $this->set_current_list_screen( $list_screen ); |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Runs when doing Quick Edit, a native WordPress ajax call |
| 386 | */ |
| 387 | public function load_list_screen_doing_quick_edit() { |
| 388 | $this->set_current_list_screen( AC()->get_list_screen( $this->get_list_screen_when_doing_quick_edit() ) ); |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * @param AC_ListScreen $list_screen |
| 393 | */ |
| 394 | public function set_current_list_screen( $list_screen ) { |
| 395 | if ( ! $list_screen ) { |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | $this->current_list_screen = $list_screen; |
| 400 | |
| 401 | // Init Values |
| 402 | $list_screen->set_manage_value_callback(); |
| 403 | |
| 404 | /** |
| 405 | * Init Headings |
| 406 | * @see get_column_headers() for filter location |
| 407 | */ |
| 408 | add_filter( "manage_" . $list_screen->get_screen_id() . "_columns", array( $this, 'add_headings' ), 200 ); |
| 409 | |
| 410 | /** |
| 411 | * @since 3.0 |
| 412 | * |
| 413 | * @param AC_ListScreen |
| 414 | */ |
| 415 | do_action( 'ac/table/list_screen', $list_screen ); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Is WordPress doing ajax |
| 420 | * |
| 421 | * @since 2.5 |
| 422 | * @return string List screen key |
| 423 | */ |
| 424 | public function get_list_screen_when_doing_quick_edit() { |
| 425 | $list_screen = false; |
| 426 | |
| 427 | if ( AC()->is_doing_ajax() ) { |
| 428 | |
| 429 | switch ( filter_input( INPUT_POST, 'action' ) ) { |
| 430 | |
| 431 | // Quick edit post |
| 432 | case 'inline-save' : |
| 433 | $list_screen = filter_input( INPUT_POST, 'post_type' ); |
| 434 | break; |
| 435 | |
| 436 | // Adding term & Quick edit term |
| 437 | case 'add-tag' : |
| 438 | case 'inline-save-tax' : |
| 439 | $list_screen = 'wp-taxonomy_' . filter_input( INPUT_POST, 'taxonomy' ); |
| 440 | break; |
| 441 | |
| 442 | // Quick edit comment & Inline reply on comment |
| 443 | case 'edit-comment' : |
| 444 | case 'replyto-comment' : |
| 445 | $list_screen = 'wp-comments'; |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | return $list_screen; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * @since 2.0 |
| 455 | */ |
| 456 | public function add_headings( $columns ) { |
| 457 | if ( empty( $columns ) ) { |
| 458 | return $columns; |
| 459 | } |
| 460 | |
| 461 | if ( ! $this->current_list_screen ) { |
| 462 | return $columns; |
| 463 | } |
| 464 | |
| 465 | // Store default headings |
| 466 | if ( ! AC()->is_doing_ajax() ) { |
| 467 | $this->current_list_screen->save_default_headings( $columns ); |
| 468 | } |
| 469 | |
| 470 | // Run once |
| 471 | if ( $this->column_headings ) { |
| 472 | return $this->column_headings; |
| 473 | } |
| 474 | |
| 475 | // Nothing stored. Show default columns on screen. |
| 476 | if ( ! $this->current_list_screen->get_settings() ) { |
| 477 | return $columns; |
| 478 | } |
| 479 | |
| 480 | // Add mandatory checkbox |
| 481 | if ( isset( $columns['cb'] ) ) { |
| 482 | $this->column_headings['cb'] = $columns['cb']; |
| 483 | } |
| 484 | |
| 485 | // On first visit 'columns' can be empty, because they were put in memory before 'default headings' |
| 486 | // were stored. We force get_columns() to be re-populated. |
| 487 | if ( ! $this->current_list_screen->get_columns() ) { |
| 488 | $this->current_list_screen->reset(); |
| 489 | $this->current_list_screen->reset_original_columns(); |
| 490 | } |
| 491 | |
| 492 | foreach ( $this->current_list_screen->get_columns() as $column ) { |
| 493 | |
| 494 | /** |
| 495 | * @since 3.0 |
| 496 | * |
| 497 | * @param string $label |
| 498 | * @param AC_Column $column |
| 499 | */ |
| 500 | $label = apply_filters( 'ac/headings/label', $column->get_setting( 'label' )->get_value(), $column ); |
| 501 | |
| 502 | $this->column_headings[ $column->get_name() ] = $label; |
| 503 | } |
| 504 | |
| 505 | return apply_filters( 'ac/headings', $this->column_headings, $this->current_list_screen ); |
| 506 | } |
| 507 | |
| 508 | } |
| 509 |