| 1 |
<?php |
| 2 |
/** |
| 3 |
* Friends_Automatic_Status_List_Table class |
| 4 |
* |
| 5 |
* @package Friends |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Friends; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class used to implement displaying automatic status posts in a list table. |
| 12 |
* |
| 13 |
* @see \WP_Posts_List_Table |
| 14 |
*/ |
| 15 |
class Automatic_Status_List_Table extends \WP_Posts_List_Table { |
| 16 |
/** |
| 17 |
* Constructor |
| 18 |
* |
| 19 |
* @param array $args Array of arguments. |
| 20 |
*/ |
| 21 |
public function __construct( $args = array() ) { |
| 22 |
parent::__construct( |
| 23 |
array( |
| 24 |
'plural' => 'posts', |
| 25 |
'screen' => \WP_Screen::get( 'edit' ), |
| 26 |
) |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Prepares the list of items for displaying. |
| 32 |
*/ |
| 33 |
public function prepare_items() { |
| 34 |
global $mode, $avail_post_stati, $wp_query, $per_page; |
| 35 |
$mode = 'excerpt'; |
| 36 |
|
| 37 |
$post_data = array( |
| 38 |
'post_type' => 'post', |
| 39 |
'post_format' => 'status', |
| 40 |
'post_status' => 'private', |
| 41 |
'post_author' => get_current_user_id(), |
| 42 |
); |
| 43 |
|
| 44 |
$avail_post_stati = get_available_post_statuses( $post_data['post_type'] ); |
| 45 |
if ( ! empty( $_REQUEST['post_status'] ) && in_array( $_REQUEST['post_status'], $avail_post_stati ) ) { |
| 46 |
$post_data['post_status'] = $_REQUEST['post_status']; |
| 47 |
} elseif ( isset( $_REQUEST['all_posts'] ) ) { |
| 48 |
unset( $post_data['post_status'] ); |
| 49 |
} |
| 50 |
|
| 51 |
wp( $post_data ); |
| 52 |
|
| 53 |
$post_type = $this->screen->post_type; |
| 54 |
$per_page = $this->get_items_per_page( 'edit_' . $post_type . '_per_page' ); |
| 55 |
|
| 56 |
/** This filter is documented in wp-admin/includes/post.php */ |
| 57 |
$per_page = apply_filters( 'edit_posts_per_page', $per_page, $post_type ); |
| 58 |
|
| 59 |
$this->is_trash = 'trash' === $post_data['post_status']; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Gets the post status counts. |
| 64 |
* |
| 65 |
* @param string $post_type The post type. |
| 66 |
* |
| 67 |
* @return object The counts. |
| 68 |
*/ |
| 69 |
protected function get_post_status_counts( $post_type ) { |
| 70 |
global $wpdb; |
| 71 |
|
| 72 |
$counts = array(); |
| 73 |
$post_status_counts = $wpdb->get_results( |
| 74 |
$wpdb->prepare( |
| 75 |
"SELECT posts.post_status, COUNT(posts.ID) AS count |
| 76 |
FROM {$wpdb->posts} AS posts |
| 77 |
JOIN {$wpdb->term_relationships} AS relationships |
| 78 |
JOIN {$wpdb->term_taxonomy} AS taxonomy |
| 79 |
JOIN {$wpdb->terms} AS terms |
| 80 |
|
| 81 |
WHERE posts.post_author = %d |
| 82 |
AND posts.post_type = %s |
| 83 |
AND relationships.object_id = posts.ID |
| 84 |
AND relationships.term_taxonomy_id = taxonomy.term_taxonomy_id |
| 85 |
AND taxonomy.taxonomy = 'post_format' |
| 86 |
|
| 87 |
AND terms.slug = 'post-format-status' |
| 88 |
|
| 89 |
GROUP BY posts.post_status", |
| 90 |
get_current_user_id(), |
| 91 |
$post_type |
| 92 |
) |
| 93 |
); |
| 94 |
|
| 95 |
foreach ( $post_status_counts as $row ) { |
| 96 |
$counts[ $row->post_status ] = $row->count; |
| 97 |
} |
| 98 |
return (object) $counts; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* The no items text. |
| 103 |
*/ |
| 104 |
public function no_items() { |
| 105 |
if ( |
| 106 |
( ! isset( $_REQUEST['post_status'] ) && ! isset( $_REQUEST['all_posts'] ) ) |
| 107 |
|| ( isset( $_REQUEST['post_status'] ) && 'draft' === $_REQUEST['post_status'] ) |
| 108 |
) { |
| 109 |
esc_html_e( 'No unpublished automatically generated statuses found.', 'friends' ); |
| 110 |
} else { |
| 111 |
esc_html_e( 'No automatically generated statuses found.', 'friends' ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Helper to create links to edit.php with params. |
| 117 |
* |
| 118 |
* @param string[] $args Associative array of URL parameters for the link. |
| 119 |
* @param string $label Link text. |
| 120 |
* @param string $class Optional. Class attribute. Default empty string. |
| 121 |
* @return string The formatted link string. |
| 122 |
*/ |
| 123 |
protected function get_edit_link( $args, $label, $class = '' ) { |
| 124 |
$args = array_merge( |
| 125 |
array( |
| 126 |
'post_type' => 'post', |
| 127 |
'post_format' => 'status', |
| 128 |
'post_author' => get_current_user_id(), |
| 129 |
), |
| 130 |
$args |
| 131 |
); |
| 132 |
$url = add_query_arg( $args, self_admin_url( 'admin.php?page=friends-auto-status' ) ); |
| 133 |
$url = remove_query_arg( 'post_type', $url ); |
| 134 |
|
| 135 |
$class_html = ''; |
| 136 |
$aria_current = ''; |
| 137 |
|
| 138 |
if ( ! empty( $class ) ) { |
| 139 |
$class_html = sprintf( |
| 140 |
' class="%s"', |
| 141 |
esc_attr( $class ) |
| 142 |
); |
| 143 |
|
| 144 |
if ( 'current' === $class ) { |
| 145 |
$aria_current = ' aria-current="page"'; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return sprintf( |
| 150 |
'<a href="%s"%s%s>%s</a>', |
| 151 |
esc_url( $url ), |
| 152 |
$class_html, |
| 153 |
$aria_current, |
| 154 |
$label |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Handles the title column output. |
| 160 |
* |
| 161 |
* @global string $mode List table view mode. |
| 162 |
* |
| 163 |
* @param \WP_Post $post The current \WP_Post object. |
| 164 |
*/ |
| 165 |
public function column_title( $post ) { |
| 166 |
global $mode; |
| 167 |
|
| 168 |
if ( current_user_can( 'read_post', $post->ID ) ) { |
| 169 |
echo wp_kses( |
| 170 |
get_the_content(), |
| 171 |
array( |
| 172 |
'img' => array( 'src' => array() ), |
| 173 |
'a' => array( 'href' => array() ), |
| 174 |
) |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
get_inline_data( $post ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Gets the list of views available on this table. |
| 183 |
* |
| 184 |
* The format is an associative array: |
| 185 |
* - `'id' => 'link'` |
| 186 |
* |
| 187 |
* @return array |
| 188 |
*/ |
| 189 |
protected function get_views() { |
| 190 |
global $locked_post_status, $avail_post_stati; |
| 191 |
|
| 192 |
$post_type = $this->screen->post_type; |
| 193 |
|
| 194 |
if ( ! empty( $locked_post_status ) ) { |
| 195 |
return array(); |
| 196 |
} |
| 197 |
|
| 198 |
$status_links = array(); |
| 199 |
$num_posts = $this->get_post_status_counts( $post_type ); |
| 200 |
|
| 201 |
$total_posts = array_sum( (array) $num_posts ); |
| 202 |
$class = ''; |
| 203 |
|
| 204 |
$current_user_id = get_current_user_id(); |
| 205 |
$all_args = array( |
| 206 |
'post_type' => $post_type, |
| 207 |
'all_posts' => 1, |
| 208 |
); |
| 209 |
$mine = ''; |
| 210 |
|
| 211 |
// Subtract post types that are not included in the admin all list. |
| 212 |
foreach ( get_post_stati( array( 'show_in_admin_all_list' => false ) ) as $state ) { |
| 213 |
if ( isset( $num_posts->$state ) ) { |
| 214 |
$total_posts -= $num_posts->$state; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
if ( empty( $class ) && ( $this->is_base_request() || isset( $_REQUEST['all_posts'] ) ) ) { |
| 219 |
$class = 'current'; |
| 220 |
} |
| 221 |
|
| 222 |
$all_inner_html = sprintf( |
| 223 |
/* translators: %s: Number of posts. */ |
| 224 |
_nx( // phpcs:ignore WordPress.WP.I18n.MissingArgDomain |
| 225 |
'All <span class="count">(%s)</span>', |
| 226 |
'All <span class="count">(%s)</span>', |
| 227 |
$total_posts, |
| 228 |
'posts' |
| 229 |
), |
| 230 |
number_format_i18n( $total_posts ) |
| 231 |
); |
| 232 |
|
| 233 |
$status_links['all'] = $this->get_edit_link( $all_args, $all_inner_html, $class ); |
| 234 |
|
| 235 |
if ( $mine ) { |
| 236 |
$status_links['mine'] = $mine; |
| 237 |
} |
| 238 |
|
| 239 |
foreach ( get_post_stati( array( 'show_in_admin_status_list' => true ), 'objects' ) as $status ) { |
| 240 |
$class = ''; |
| 241 |
|
| 242 |
$status_name = $status->name; |
| 243 |
|
| 244 |
if ( ! in_array( $status_name, $avail_post_stati, true ) || empty( $num_posts->$status_name ) ) { |
| 245 |
continue; |
| 246 |
} |
| 247 |
|
| 248 |
if ( isset( $_REQUEST['post_status'] ) && $status_name === $_REQUEST['post_status'] ) { |
| 249 |
$class = 'current'; |
| 250 |
} elseif ( ! isset( $_REQUEST['post_status'] ) && ! isset( $_REQUEST['all_posts'] ) && 'draft' === $status_name ) { |
| 251 |
$class = 'current'; |
| 252 |
} |
| 253 |
|
| 254 |
$status_args = array( |
| 255 |
'post_status' => $status_name, |
| 256 |
'post_type' => $post_type, |
| 257 |
); |
| 258 |
|
| 259 |
$status_label = sprintf( |
| 260 |
translate_nooped_plural( $status->label_count, $num_posts->$status_name ), |
| 261 |
number_format_i18n( $num_posts->$status_name ) |
| 262 |
); |
| 263 |
|
| 264 |
$status_links[ $status_name ] = $this->get_edit_link( $status_args, $status_label, $class ); |
| 265 |
} |
| 266 |
|
| 267 |
if ( ! empty( $this->sticky_posts_count ) ) { |
| 268 |
$class = ! empty( $_REQUEST['show_sticky'] ) ? 'current' : ''; |
| 269 |
|
| 270 |
$sticky_args = array( |
| 271 |
'post_type' => $post_type, |
| 272 |
'show_sticky' => 1, |
| 273 |
); |
| 274 |
|
| 275 |
$sticky_inner_html = sprintf( |
| 276 |
/* translators: %s: Number of posts. */ |
| 277 |
_nx( // phpcs:ignore WordPress.WP.I18n.MissingArgDomain |
| 278 |
'Sticky <span class="count">(%s)</span>', |
| 279 |
'Sticky <span class="count">(%s)</span>', |
| 280 |
$this->sticky_posts_count, |
| 281 |
'posts' |
| 282 |
), |
| 283 |
number_format_i18n( $this->sticky_posts_count ) |
| 284 |
); |
| 285 |
|
| 286 |
$sticky_link = array( |
| 287 |
'sticky' => $this->get_edit_link( $sticky_args, $sticky_inner_html, $class ), |
| 288 |
); |
| 289 |
|
| 290 |
// Sticky comes after Publish, or if not listed, after All. |
| 291 |
$split = 1 + array_search( ( isset( $status_links['publish'] ) ? 'publish' : 'all' ), array_keys( $status_links ), true ); |
| 292 |
$status_links = array_merge( array_slice( $status_links, 0, $split ), $sticky_link, array_slice( $status_links, $split ) ); |
| 293 |
} |
| 294 |
return $status_links; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Retrieves the list of bulk actions available for this table. |
| 299 |
* |
| 300 |
* @return array |
| 301 |
*/ |
| 302 |
protected function get_bulk_actions() { |
| 303 |
$actions = array(); |
| 304 |
$post_type_obj = get_post_type_object( $this->screen->post_type ); |
| 305 |
|
| 306 |
if ( current_user_can( $post_type_obj->cap->publish_posts ) ) { |
| 307 |
$actions['publish'] = __( 'Publish' ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain |
| 308 |
} |
| 309 |
|
| 310 |
if ( current_user_can( $post_type_obj->cap->edit_posts ) ) { |
| 311 |
if ( $this->is_trash ) { |
| 312 |
$actions['untrash'] = __( 'Restore' ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain |
| 313 |
} else { |
| 314 |
if ( apply_filters( 'friends_debug', false ) ) { |
| 315 |
$actions['edit'] = __( 'Edit' ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain |
| 316 |
} |
| 317 |
$actions['publish-private'] = __( 'Publish Privately' ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
if ( current_user_can( $post_type_obj->cap->delete_posts ) ) { |
| 322 |
if ( $this->is_trash || ! EMPTY_TRASH_DAYS ) { |
| 323 |
$actions['delete'] = __( 'Delete permanently' ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain |
| 324 |
} else { |
| 325 |
$actions['trash'] = __( 'Move to Trash' ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
return $actions; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Generates the extra table navigation above or below the table |
| 334 |
* |
| 335 |
* @param string $which The location of the bulk actions: 'top' or 'bottom'. |
| 336 |
*/ |
| 337 |
protected function extra_tablenav( $which ) { |
| 338 |
} |
| 339 |
} |
| 340 |
|