| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Topic Replies List Table class. |
| 5 |
* |
| 6 |
* @package bbPress |
| 7 |
* @subpackage Administration |
| 8 |
* @since 2.6.0 bbPress (r5886) |
| 9 |
* @access private |
| 10 |
* |
| 11 |
* @see WP_Posts_List_Table |
| 12 |
*/ |
| 13 |
|
| 14 |
// Include the main list table class if it's not included yet |
| 15 |
if ( ! class_exists( 'WP_List_Table' ) ) { |
| 16 |
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 17 |
} |
| 18 |
|
| 19 |
if ( class_exists( 'WP_List_Table' ) ) : |
| 20 |
/** |
| 21 |
* Topic replies list table |
| 22 |
* |
| 23 |
* This list table is responsible for showing the replies to a topic in a |
| 24 |
* meta-box, similar to comments in posts and pages. |
| 25 |
* |
| 26 |
* @since 2.6.0 bbPress (r5886) |
| 27 |
*/ |
| 28 |
class BBP_Topic_Replies_List_Table extends WP_List_Table { |
| 29 |
|
| 30 |
/** |
| 31 |
* The main constructor method |
| 32 |
* |
| 33 |
* @since 2.6.0 bbPress (r5886) |
| 34 |
*/ |
| 35 |
public function __construct( $args = array() ) { |
| 36 |
|
| 37 |
// Parse arguments |
| 38 |
$args = bbp_parse_args( |
| 39 |
$args, |
| 40 |
array( |
| 41 |
'singular' => 'reply', |
| 42 |
'plural' => 'replies', |
| 43 |
'ajax' => false |
| 44 |
), |
| 45 |
'topic_replies_list_table' |
| 46 |
); |
| 47 |
|
| 48 |
// Construct the list table |
| 49 |
parent::__construct( $args ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Setup the list-table columns |
| 54 |
* |
| 55 |
* @since 2.6.0 bbPress (r5886) |
| 56 |
* |
| 57 |
* @see WP_List_Table::::single_row_columns() |
| 58 |
* |
| 59 |
* @return array An associative array containing column information |
| 60 |
*/ |
| 61 |
public function get_columns() { |
| 62 |
return array( |
| 63 |
//'cb' => '<input type="checkbox" />', |
| 64 |
'bbp_topic_reply_author' => esc_html__( 'Author', 'bbpress' ), |
| 65 |
'bbp_reply_content' => esc_html__( 'Content', 'bbpress' ), |
| 66 |
'bbp_reply_created' => esc_html__( 'Replied', 'bbpress' ), |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Allow `bbp_reply_created` to be sortable |
| 72 |
* |
| 73 |
* @since 2.6.0 bbPress (r5886) |
| 74 |
* |
| 75 |
* @return array An associative array containing the `bbp_reply_created` column |
| 76 |
*/ |
| 77 |
public function get_sortable_columns() { |
| 78 |
return array( |
| 79 |
'bbp_reply_created' => array( 'bbp_reply_created', false ) |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Setup the bulk actions |
| 85 |
* |
| 86 |
* @since 2.6.0 bbPress (r5886) |
| 87 |
* |
| 88 |
* @return array An associative array containing all the bulk actions |
| 89 |
*/ |
| 90 |
public function get_bulk_actions() { |
| 91 |
return array(); |
| 92 |
|
| 93 |
// @todo cap checks |
| 94 |
return array( |
| 95 |
'unapprove' => esc_html__( 'Unapprove', 'bbpress' ), |
| 96 |
'spam' => esc_html__( 'Spam', 'bbpress' ), |
| 97 |
'trash' => esc_html__( 'Trash', 'bbpress' ) |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Output the check-box column for bulk actions (if we implement them) |
| 103 |
* |
| 104 |
* @since 2.6.0 bbPress (r5886) |
| 105 |
*/ |
| 106 |
public function column_cb( $item = '' ) { |
| 107 |
return sprintf( |
| 108 |
'<input type="checkbox" name="%1$s[]" value="%2$s" />', |
| 109 |
$this->_args['singular'], |
| 110 |
$item->ID |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Output the contents of the `bbp_topic_reply_author` column |
| 116 |
* |
| 117 |
* @since 2.6.0 bbPress (r5886) |
| 118 |
*/ |
| 119 |
public function column_bbp_topic_reply_author( $item = '' ) { |
| 120 |
bbp_reply_author_avatar( $item->ID, 50 ); |
| 121 |
echo esc_html( bbp_get_reply_author_display_name( $item->ID ) ); |
| 122 |
echo '<br>'; |
| 123 |
echo esc_html( bbp_get_reply_author_email( $item->ID ) ); |
| 124 |
echo '<br>'; |
| 125 |
bbp_author_ip( array( 'post_id' => $item->ID ) ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Output the contents of the `bbp_reply_created` column |
| 130 |
* |
| 131 |
* @since 2.6.0 bbPress (r5886) |
| 132 |
*/ |
| 133 |
public function column_bbp_reply_created( $item = '' ) { |
| 134 |
return sprintf( '%1$s <br /> %2$s', |
| 135 |
esc_attr( get_the_date( '', $item ) ), |
| 136 |
esc_attr( get_the_time( '', $item ) ) |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Output the contents of the `bbp_reply_content` column |
| 142 |
* |
| 143 |
* @since 2.6.0 bbPress (r5886) |
| 144 |
*/ |
| 145 |
public function column_bbp_reply_content( $item = '' ) { |
| 146 |
|
| 147 |
// Define actions array |
| 148 |
$actions = array( |
| 149 |
'view' => '<a href="' . esc_url( bbp_get_reply_url( $item->ID ) ) . '">' . esc_html__( 'View', 'bbpress' ) . '</a>' |
| 150 |
); |
| 151 |
|
| 152 |
// Prepend `edit` link |
| 153 |
if ( current_user_can( 'edit_reply', $item->ID ) ) { |
| 154 |
$actions['edit'] = '<a href="' . esc_url( get_edit_post_link( $item->ID ) ) . '">' . esc_html__( 'Edit', 'bbpress' ) . '</a>'; |
| 155 |
$actions = array_reverse( $actions ); |
| 156 |
} |
| 157 |
|
| 158 |
// Filter the reply content |
| 159 |
$reply_content = apply_filters( 'bbp_get_reply_content', $item->post_content, $item->ID ); |
| 160 |
$reply_actions = $this->row_actions( $actions ); |
| 161 |
|
| 162 |
// Return content & actions |
| 163 |
return $reply_content . $reply_actions; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Handle bulk action requests |
| 168 |
* |
| 169 |
* @since 2.6.0 bbPress (r5886) |
| 170 |
*/ |
| 171 |
public function process_bulk_action() { |
| 172 |
switch ( $this->current_action() ) { |
| 173 |
case 'trash' : |
| 174 |
break; |
| 175 |
case 'unapprove' : |
| 176 |
break; |
| 177 |
case 'spam' : |
| 178 |
break; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Prepare the list-table items for display |
| 184 |
* |
| 185 |
* @since 2.6.0 bbPress (r5886) |
| 186 |
*/ |
| 187 |
public function prepare_items( $topic_id = 0 ) { |
| 188 |
|
| 189 |
// Sanitize the topic ID |
| 190 |
$topic_id = bbp_get_topic_id( $topic_id ); |
| 191 |
|
| 192 |
// Set column headers |
| 193 |
$this->_column_headers = array( |
| 194 |
$this->get_columns(), |
| 195 |
array(), |
| 196 |
$this->get_sortable_columns() |
| 197 |
); |
| 198 |
|
| 199 |
// Handle bulk actions |
| 200 |
$this->process_bulk_action(); |
| 201 |
|
| 202 |
// Query parameters |
| 203 |
$per_page = 5; |
| 204 |
$current_page = $this->get_pagenum(); |
| 205 |
$orderby = ! empty( $_REQUEST['orderby'] ) ? sanitize_key( $_REQUEST['orderby'] ) : 'date'; |
| 206 |
$order = ! empty( $_REQUEST['order'] ) ? sanitize_key( $_REQUEST['order'] ) : 'asc'; |
| 207 |
$statuses = bbp_get_public_reply_statuses(); |
| 208 |
|
| 209 |
// Maybe add private statuses to query |
| 210 |
if ( current_user_can( 'edit_others_replies' ) ) { |
| 211 |
|
| 212 |
// Default view=all statuses |
| 213 |
$statuses = array_keys( bbp_get_topic_statuses() ); |
| 214 |
|
| 215 |
// Add support for private status |
| 216 |
if ( current_user_can( 'read_private_replies' ) ) { |
| 217 |
$statuses[] = bbp_get_private_status_id(); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
// Query for replies |
| 222 |
$reply_query = new WP_Query( |
| 223 |
array( |
| 224 |
'post_type' => bbp_get_reply_post_type(), |
| 225 |
'post_status' => $statuses, |
| 226 |
'post_parent' => $topic_id, |
| 227 |
'posts_per_page' => $per_page, |
| 228 |
'paged' => $current_page, |
| 229 |
'orderby' => $orderby, |
| 230 |
'order' => ucwords( $order ), |
| 231 |
'hierarchical' => false, |
| 232 |
'ignore_sticky_posts' => true |
| 233 |
) |
| 234 |
); |
| 235 |
|
| 236 |
// Get the total number of replies, for pagination |
| 237 |
$total_items = bbp_get_topic_reply_count( $topic_id ); |
| 238 |
|
| 239 |
// Set list table items to queried posts |
| 240 |
$this->items = $reply_query->posts; |
| 241 |
|
| 242 |
// Set the pagination arguments |
| 243 |
$this->set_pagination_args( |
| 244 |
array( |
| 245 |
'total_items' => $total_items, |
| 246 |
'per_page' => $per_page, |
| 247 |
'total_pages' => ceil( $total_items / $per_page ) |
| 248 |
) |
| 249 |
); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Message to be displayed when there are no items |
| 254 |
* |
| 255 |
* @since 2.6.0 bbPress (r5930) |
| 256 |
*/ |
| 257 |
public function no_items() { |
| 258 |
esc_html_e( 'No replies to this topic.', 'bbpress' ); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Display the list table |
| 263 |
* |
| 264 |
* This custom method is necessary because the one in `WP_List_Table` comes |
| 265 |
* with a nonce and check that we do not need. |
| 266 |
* |
| 267 |
* @since 2.6.0 bbPress (r5930) |
| 268 |
*/ |
| 269 |
public function display() { |
| 270 |
|
| 271 |
// Top |
| 272 |
$this->display_tablenav( 'top' ); ?> |
| 273 |
|
| 274 |
<table id="bbp-reply-list" class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>"> |
| 275 |
<thead> |
| 276 |
<tr> |
| 277 |
<?php $this->print_column_headers(); ?> |
| 278 |
</tr> |
| 279 |
</thead> |
| 280 |
|
| 281 |
<tbody data-wp-lists='list:<?php echo $this->_args['singular']; ?>'> |
| 282 |
<?php $this->display_rows_or_placeholder(); ?> |
| 283 |
</tbody> |
| 284 |
|
| 285 |
<tfoot> |
| 286 |
<tr> |
| 287 |
<?php $this->print_column_headers( false ); ?> |
| 288 |
</tr> |
| 289 |
</tfoot> |
| 290 |
</table> |
| 291 |
|
| 292 |
<?php |
| 293 |
|
| 294 |
// Bottom |
| 295 |
$this->display_tablenav( 'bottom' ); |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Generate the table navigation above or below the table |
| 300 |
* |
| 301 |
* This custom method is necessary because the one in `WP_List_Table` comes |
| 302 |
* with a nonce and check that we do not need. |
| 303 |
* |
| 304 |
* @since 2.6.0 bbPress (r5930) |
| 305 |
* |
| 306 |
* @param string $which |
| 307 |
*/ |
| 308 |
protected function display_tablenav( $which = '' ) { |
| 309 |
?> |
| 310 |
|
| 311 |
<div class="tablenav <?php echo esc_attr( $which ); ?>"> |
| 312 |
<?php |
| 313 |
$this->extra_tablenav( $which ); |
| 314 |
$this->pagination( $which ); |
| 315 |
?> |
| 316 |
<br class="clear" /> |
| 317 |
</div> |
| 318 |
|
| 319 |
<?php |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Generates content for a single row of the table |
| 324 |
* |
| 325 |
* @since 2.6.0 bbPress (r6546) |
| 326 |
* @access public |
| 327 |
* |
| 328 |
* @param object $item The current item |
| 329 |
*/ |
| 330 |
public function single_row( $item ) { |
| 331 |
|
| 332 |
// Author |
| 333 |
$classes = 'author-' . ( get_current_user_id() == $item->post_author ? 'self' : 'other' ); |
| 334 |
|
| 335 |
// Locked |
| 336 |
if ( wp_check_post_lock( $item->ID ) ) { |
| 337 |
$classes .= ' wp-locked'; |
| 338 |
} |
| 339 |
|
| 340 |
// Hierarchy |
| 341 |
if ( ! empty( $item->post_parent ) ) { |
| 342 |
$count = count( get_post_ancestors( $item->ID ) ); |
| 343 |
$classes .= ' level-'. $count; |
| 344 |
} else { |
| 345 |
$classes .= ' level-0'; |
| 346 |
} ?> |
| 347 |
|
| 348 |
<tr id="post-<?php echo esc_attr( $item->ID ); ?>" class="<?php echo implode( ' ', get_post_class( $classes, $item->ID ) ); ?>"> |
| 349 |
<?php $this->single_row_columns( $item ); ?> |
| 350 |
</tr> |
| 351 |
|
| 352 |
<?php |
| 353 |
} |
| 354 |
} |
| 355 |
endif; |
| 356 |
|