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