abstract-class-wc-admin-list-table.php
5 years ago
class-wc-admin-list-table-coupons.php
2 years ago
class-wc-admin-list-table-orders.php
5 months ago
class-wc-admin-list-table-products.php
1 month ago
abstract-class-wc-admin-list-table.php
277 lines
| 1 | <?php |
| 2 | /** |
| 3 | * List tables. |
| 4 | * |
| 5 | * @package WooCommerce\Admin |
| 6 | * @version 3.3.0 |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | if ( class_exists( 'WC_Admin_List_Table', false ) ) { |
| 14 | return; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * WC_Admin_List_Table Class. |
| 19 | */ |
| 20 | abstract class WC_Admin_List_Table { |
| 21 | |
| 22 | /** |
| 23 | * Post type. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $list_table_type = ''; |
| 28 | |
| 29 | /** |
| 30 | * Object being shown on the row. |
| 31 | * |
| 32 | * @var object|null |
| 33 | */ |
| 34 | protected $object = null; |
| 35 | |
| 36 | /** |
| 37 | * Constructor. |
| 38 | */ |
| 39 | public function __construct() { |
| 40 | if ( $this->list_table_type ) { |
| 41 | add_action( 'manage_posts_extra_tablenav', array( $this, 'maybe_render_blank_state' ) ); |
| 42 | add_filter( 'view_mode_post_types', array( $this, 'disable_view_mode' ) ); |
| 43 | add_action( 'restrict_manage_posts', array( $this, 'restrict_manage_posts' ) ); |
| 44 | add_filter( 'request', array( $this, 'request_query' ) ); |
| 45 | add_filter( 'post_row_actions', array( $this, 'row_actions' ), 100, 2 ); |
| 46 | add_filter( 'default_hidden_columns', array( $this, 'default_hidden_columns' ), 10, 2 ); |
| 47 | add_filter( 'list_table_primary_column', array( $this, 'list_table_primary_column' ), 10, 2 ); |
| 48 | add_filter( 'manage_edit-' . $this->list_table_type . '_sortable_columns', array( $this, 'define_sortable_columns' ) ); |
| 49 | add_filter( 'manage_' . $this->list_table_type . '_posts_columns', array( $this, 'define_columns' ) ); |
| 50 | add_filter( 'bulk_actions-edit-' . $this->list_table_type, array( $this, 'define_bulk_actions' ) ); |
| 51 | add_action( 'manage_' . $this->list_table_type . '_posts_custom_column', array( $this, 'render_columns' ), 10, 2 ); |
| 52 | add_filter( 'handle_bulk_actions-edit-' . $this->list_table_type, array( $this, 'handle_bulk_actions' ), 10, 3 ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Show blank slate. |
| 58 | * |
| 59 | * @param string $which String which tablenav is being shown. |
| 60 | */ |
| 61 | public function maybe_render_blank_state( $which ) { |
| 62 | global $post_type; |
| 63 | |
| 64 | if ( $post_type === $this->list_table_type && 'bottom' === $which ) { |
| 65 | $counts = (array) wp_count_posts( $post_type ); |
| 66 | unset( $counts['auto-draft'] ); |
| 67 | $count = array_sum( $counts ); |
| 68 | |
| 69 | if ( 0 < $count ) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | $this->render_blank_state(); |
| 74 | |
| 75 | echo '<style type="text/css">#posts-filter .wp-list-table, #posts-filter .tablenav.top, .tablenav.bottom .actions, .wrap .subsubsub { display: none; } #posts-filter .tablenav.bottom { height: auto; } </style>'; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Render blank state. Extend to add content. |
| 81 | */ |
| 82 | protected function render_blank_state() {} |
| 83 | |
| 84 | /** |
| 85 | * Removes this type from list of post types that support "View Mode" switching. |
| 86 | * View mode is seen on posts where you can switch between list or excerpt. Our post types don't support |
| 87 | * it, so we want to hide the useless UI from the screen options tab. |
| 88 | * |
| 89 | * @param array $post_types Array of post types supporting view mode. |
| 90 | * @return array Array of post types supporting view mode, without this type. |
| 91 | */ |
| 92 | public function disable_view_mode( $post_types ) { |
| 93 | unset( $post_types[ $this->list_table_type ] ); |
| 94 | return $post_types; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * See if we should render search filters or not. |
| 99 | */ |
| 100 | public function restrict_manage_posts() { |
| 101 | global $typenow; |
| 102 | |
| 103 | if ( $this->list_table_type === $typenow ) { |
| 104 | $this->render_filters(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Handle any filters. |
| 110 | * |
| 111 | * @param array $query_vars Query vars. |
| 112 | * @return array |
| 113 | */ |
| 114 | public function request_query( $query_vars ) { |
| 115 | global $typenow; |
| 116 | |
| 117 | if ( $this->list_table_type === $typenow ) { |
| 118 | return $this->query_filters( $query_vars ); |
| 119 | } |
| 120 | |
| 121 | return $query_vars; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Render any custom filters and search inputs for the list table. |
| 126 | */ |
| 127 | protected function render_filters() {} |
| 128 | |
| 129 | /** |
| 130 | * Handle any custom filters. |
| 131 | * |
| 132 | * @param array $query_vars Query vars. |
| 133 | * @return array |
| 134 | */ |
| 135 | protected function query_filters( $query_vars ) { |
| 136 | return $query_vars; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Set row actions. |
| 141 | * |
| 142 | * @param array $actions Array of actions. |
| 143 | * @param WP_Post $post Current post object. |
| 144 | * @return array |
| 145 | */ |
| 146 | public function row_actions( $actions, $post ) { |
| 147 | if ( $this->list_table_type === $post->post_type ) { |
| 148 | return $this->get_row_actions( $actions, $post ); |
| 149 | } |
| 150 | return $actions; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Get row actions to show in the list table. |
| 155 | * |
| 156 | * @param array $actions Array of actions. |
| 157 | * @param WP_Post $post Current post object. |
| 158 | * @return array |
| 159 | */ |
| 160 | protected function get_row_actions( $actions, $post ) { |
| 161 | return $actions; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Adjust which columns are displayed by default. |
| 166 | * |
| 167 | * @param array $hidden Current hidden columns. |
| 168 | * @param object $screen Current screen. |
| 169 | * @return array |
| 170 | */ |
| 171 | public function default_hidden_columns( $hidden, $screen ) { |
| 172 | if ( isset( $screen->id ) && 'edit-' . $this->list_table_type === $screen->id ) { |
| 173 | $hidden = array_merge( $hidden, $this->define_hidden_columns() ); |
| 174 | } |
| 175 | return $hidden; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Set list table primary column. |
| 180 | * |
| 181 | * @param string $default Default value. |
| 182 | * @param string $screen_id Current screen ID. |
| 183 | * @return string |
| 184 | */ |
| 185 | public function list_table_primary_column( $default, $screen_id ) { |
| 186 | if ( 'edit-' . $this->list_table_type === $screen_id && $this->get_primary_column() ) { |
| 187 | return $this->get_primary_column(); |
| 188 | } |
| 189 | return $default; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Define primary column. |
| 194 | * |
| 195 | * @return array |
| 196 | */ |
| 197 | protected function get_primary_column() { |
| 198 | return ''; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Define hidden columns. |
| 203 | * |
| 204 | * @return array |
| 205 | */ |
| 206 | protected function define_hidden_columns() { |
| 207 | return array(); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Define which columns are sortable. |
| 212 | * |
| 213 | * @param array $columns Existing columns. |
| 214 | * @return array |
| 215 | */ |
| 216 | public function define_sortable_columns( $columns ) { |
| 217 | return $columns; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Define which columns to show on this screen. |
| 222 | * |
| 223 | * @param array $columns Existing columns. |
| 224 | * @return array |
| 225 | */ |
| 226 | public function define_columns( $columns ) { |
| 227 | return $columns; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Define bulk actions. |
| 232 | * |
| 233 | * @param array $actions Existing actions. |
| 234 | * @return array |
| 235 | */ |
| 236 | public function define_bulk_actions( $actions ) { |
| 237 | return $actions; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Pre-fetch any data for the row each column has access to it. |
| 242 | * |
| 243 | * @param int $post_id Post ID being shown. |
| 244 | */ |
| 245 | protected function prepare_row_data( $post_id ) {} |
| 246 | |
| 247 | /** |
| 248 | * Render individual columns. |
| 249 | * |
| 250 | * @param string $column Column ID to render. |
| 251 | * @param int $post_id Post ID being shown. |
| 252 | */ |
| 253 | public function render_columns( $column, $post_id ) { |
| 254 | $this->prepare_row_data( $post_id ); |
| 255 | |
| 256 | if ( ! $this->object ) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | if ( is_callable( array( $this, 'render_' . $column . '_column' ) ) ) { |
| 261 | $this->{"render_{$column}_column"}(); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Handle bulk actions. |
| 267 | * |
| 268 | * @param string $redirect_to URL to redirect to. |
| 269 | * @param string $action Action name. |
| 270 | * @param array $ids List of ids. |
| 271 | * @return string |
| 272 | */ |
| 273 | public function handle_bulk_actions( $redirect_to, $action, $ids ) { |
| 274 | return esc_url_raw( $redirect_to ); |
| 275 | } |
| 276 | } |
| 277 |