PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/flamingo/admin/includes/class-inbound-messages-list-table.php

https://gitlab.com/hunt9310/ras
PHP | 345 lines | 261 code | 81 blank | 3 comment | 49 complexity | 93096be056d42f15f618adc26875d7b3 MD5 | raw file
  1. <?php
  2. if ( ! class_exists( 'WP_List_Table' ) )
  3. require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
  4. class Flamingo_Inbound_Messages_List_Table extends WP_List_Table {
  5. private $is_trash = false;
  6. private $is_spam = false;
  7. public static function define_columns() {
  8. $columns = array(
  9. 'cb' => '<input type="checkbox" />',
  10. 'subject' => __( 'Subject', 'flamingo' ),
  11. 'from' => __( 'From', 'flamingo' ),
  12. 'channel' => __( 'Channel', 'flamingo' ),
  13. 'date' => __( 'Date', 'flamingo' ) );
  14. $columns = apply_filters(
  15. 'manage_flamingo_inbound_posts_columns', $columns );
  16. return $columns;
  17. }
  18. function __construct() {
  19. parent::__construct( array(
  20. 'singular' => 'post',
  21. 'plural' => 'posts',
  22. 'ajax' => false ) );
  23. }
  24. function prepare_items() {
  25. $current_screen = get_current_screen();
  26. $per_page = $this->get_items_per_page( $current_screen->id . '_per_page' );
  27. $this->_column_headers = $this->get_column_info();
  28. $args = array(
  29. 'posts_per_page' => $per_page,
  30. 'offset' => ( $this->get_pagenum() - 1 ) * $per_page,
  31. 'orderby' => 'date',
  32. 'order' => 'DESC' );
  33. if ( ! empty( $_REQUEST['s'] ) ) {
  34. $args['s'] = $_REQUEST['s'];
  35. }
  36. if ( ! empty( $_REQUEST['orderby'] ) ) {
  37. if ( 'subject' == $_REQUEST['orderby'] ) {
  38. $args['meta_key'] = '_subject';
  39. $args['orderby'] = 'meta_value';
  40. } elseif ( 'from' == $_REQUEST['orderby'] ) {
  41. $args['meta_key'] = '_from';
  42. $args['orderby'] = 'meta_value';
  43. }
  44. }
  45. if ( ! empty( $_REQUEST['order'] ) && 'asc' == strtolower( $_REQUEST['order'] ) )
  46. $args['order'] = 'ASC';
  47. if ( ! empty( $_REQUEST['m'] ) ) {
  48. $args['m'] = $_REQUEST['m'];
  49. }
  50. if ( ! empty( $_REQUEST['channel_id'] ) ) {
  51. $args['channel_id'] = $_REQUEST['channel_id'];
  52. }
  53. if ( ! empty( $_REQUEST['channel'] ) ) {
  54. $args['channel'] = $_REQUEST['channel'];
  55. }
  56. if ( ! empty( $_REQUEST['post_status'] ) ) {
  57. if ( 'trash' == $_REQUEST['post_status'] ) {
  58. $args['post_status'] = 'trash';
  59. $this->is_trash = true;
  60. } elseif ( 'spam' == $_REQUEST['post_status'] ) {
  61. $args['post_status'] = Flamingo_Inbound_Message::spam_status;
  62. $this->is_spam = true;
  63. }
  64. }
  65. $this->items = Flamingo_Inbound_Message::find( $args );
  66. $total_items = Flamingo_Inbound_Message::$found_items;
  67. $total_pages = ceil( $total_items / $per_page );
  68. $this->set_pagination_args( array(
  69. 'total_items' => $total_items,
  70. 'total_pages' => $total_pages,
  71. 'per_page' => $per_page ) );
  72. }
  73. function get_views() {
  74. $status_links = array();
  75. $post_status = empty( $_REQUEST['post_status'] )
  76. ? '' : $_REQUEST['post_status'];
  77. // Inbox
  78. Flamingo_Inbound_Message::find( array( 'post_status' => 'any' ) );
  79. $posts_in_inbox = Flamingo_Inbound_Message::$found_items;
  80. $inbox = sprintf(
  81. _nx( 'Inbox <span class="count">(%s)</span>',
  82. 'Inbox <span class="count">(%s)</span>',
  83. $posts_in_inbox, 'posts', 'flamingo' ),
  84. number_format_i18n( $posts_in_inbox ) );
  85. $status_links['inbox'] = sprintf( '<a href="%1$s"%2$s>%3$s</a>',
  86. admin_url( 'admin.php?page=flamingo_inbound' ),
  87. ( $this->is_trash || $this->is_spam ) ? '' : ' class="current"',
  88. $inbox );
  89. // Spam
  90. Flamingo_Inbound_Message::find( array(
  91. 'post_status' => Flamingo_Inbound_Message::spam_status ) );
  92. $posts_in_spam = Flamingo_Inbound_Message::$found_items;
  93. $spam = sprintf(
  94. _nx( 'Spam <span class="count">(%s)</span>',
  95. 'Spam <span class="count">(%s)</span>',
  96. $posts_in_spam, 'posts', 'flamingo' ),
  97. number_format_i18n( $posts_in_spam ) );
  98. $status_links['spam'] = sprintf( '<a href="%1$s"%2$s>%3$s</a>',
  99. admin_url( 'admin.php?page=flamingo_inbound&post_status=spam' ),
  100. 'spam' == $post_status ? ' class="current"' : '',
  101. $spam );
  102. // Trash
  103. Flamingo_Inbound_Message::find( array( 'post_status' => 'trash' ) );
  104. $posts_in_trash = Flamingo_Inbound_Message::$found_items;
  105. if ( empty( $posts_in_trash ) )
  106. return $status_links;
  107. $trash = sprintf(
  108. _nx( 'Trash <span class="count">(%s)</span>',
  109. 'Trash <span class="count">(%s)</span>',
  110. $posts_in_trash, 'posts', 'flamingo' ),
  111. number_format_i18n( $posts_in_trash ) );
  112. $status_links['trash'] = sprintf( '<a href="%1$s"%2$s>%3$s</a>',
  113. admin_url( 'admin.php?page=flamingo_inbound&post_status=trash' ),
  114. 'trash' == $post_status ? ' class="current"' : '',
  115. $trash );
  116. return $status_links;
  117. }
  118. function get_columns() {
  119. return get_column_headers( get_current_screen() );
  120. }
  121. function get_sortable_columns() {
  122. $columns = array(
  123. 'subject' => array( 'subject', false ),
  124. 'from' => array( 'from', false ),
  125. 'date' => array( 'date', true ) );
  126. return $columns;
  127. }
  128. function get_bulk_actions() {
  129. $actions = array();
  130. if ( $this->is_trash ) {
  131. $actions['untrash'] = __( 'Restore', 'flamingo' );
  132. }
  133. if ( $this->is_trash || ! EMPTY_TRASH_DAYS ) {
  134. $actions['delete'] = __( 'Delete Permanently', 'flamingo' );
  135. } else {
  136. $actions['trash'] = __( 'Move to Trash', 'flamingo' );
  137. }
  138. if ( $this->is_spam ) {
  139. $actions['unspam'] = __( 'Not Spam', 'flamingo' );
  140. } else {
  141. $actions['spam'] = __( 'Mark as Spam', 'flamingo' );
  142. }
  143. return $actions;
  144. }
  145. function extra_tablenav( $which ) {
  146. $channel = 0;
  147. if ( ! empty( $_REQUEST['channel_id'] ) ) {
  148. $term = get_term( $_REQUEST['channel_id'], Flamingo_Inbound_Message::channel_taxonomy );
  149. if ( ! empty( $term ) && ! is_wp_error( $term ) )
  150. $channel = $term->term_id;
  151. } elseif ( ! empty( $_REQUEST['channel'] ) ) {
  152. $term = get_term_by( 'slug', $_REQUEST['channel'],
  153. Flamingo_Inbound_Message::channel_taxonomy );
  154. if ( ! empty( $term ) && ! is_wp_error( $term ) )
  155. $channel = $term->term_id;
  156. }
  157. ?>
  158. <div class="alignleft actions">
  159. <?php
  160. if ( 'top' == $which ) {
  161. $this->months_dropdown( Flamingo_Inbound_Message::post_type );
  162. wp_dropdown_categories( array(
  163. 'taxonomy' => Flamingo_Inbound_Message::channel_taxonomy,
  164. 'name' => 'channel_id',
  165. 'show_option_all' => __( 'View all channels', 'flamingo' ),
  166. 'show_count' => 1,
  167. 'hide_empty' => 0,
  168. 'hide_if_empty' => 1,
  169. 'orderby' => 'name',
  170. 'hierarchical' => 1,
  171. 'selected' => $channel ) );
  172. submit_button( __( 'Filter', 'flamingo' ),
  173. 'secondary', false, false, array( 'id' => 'post-query-submit' ) );
  174. if ( ! $this->is_spam && ! $this->is_trash ) {
  175. submit_button( __( 'Export', 'flamingo' ),
  176. 'secondary', 'export', false );
  177. }
  178. }
  179. if ( $this->is_trash && current_user_can( 'flamingo_delete_inbound_messages' ) ) {
  180. submit_button( __( 'Empty Trash', 'flamingo' ),
  181. 'button-secondary apply', 'delete_all', false );
  182. }
  183. ?>
  184. </div>
  185. <?php
  186. }
  187. function column_default( $item, $column_name ) {
  188. do_action( 'manage_flamingo_inbound_posts_custom_column',
  189. $column_name, $item->id );
  190. }
  191. function column_cb( $item ) {
  192. return sprintf(
  193. '<input type="checkbox" name="%1$s[]" value="%2$s" />',
  194. $this->_args['singular'],
  195. $item->id );
  196. }
  197. function column_subject( $item ) {
  198. if ( $this->is_trash )
  199. return '<strong>' . esc_html( $item->subject ) . '</strong>';
  200. $actions = array();
  201. $url = admin_url( 'admin.php?page=flamingo_inbound&post=' . absint( $item->id ) );
  202. $edit_link = add_query_arg( array( 'action' => 'edit' ), $url );
  203. $actions['edit'] = '<a href="' . $edit_link . '">'
  204. . esc_html( __( 'Edit', 'flamingo' ) ) . '</a>';
  205. if ( $item->spam ) {
  206. $link = add_query_arg( array( 'action' => 'unspam' ), $url );
  207. $link = wp_nonce_url( $link,
  208. 'flamingo-unspam-inbound-message_' . $item->id );
  209. $actions['unspam'] = '<a href="' . $link . '">'
  210. . esc_html( __( 'Not Spam', 'flamingo' ) ) . '</a>';
  211. } else {
  212. $link = add_query_arg( array( 'action' => 'spam' ), $url );
  213. $link = wp_nonce_url( $link, 'flamingo-spam-inbound-message_' . $item->id );
  214. $actions['spam'] = '<a href="' . $link . '">'
  215. . esc_html( __( 'Spam', 'flamingo' ) ) . '</a>';
  216. }
  217. $a = sprintf( '<a class="row-title" href="%1$s" title="%2$s">%3$s</a>',
  218. $edit_link,
  219. esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;', 'flamingo' ), $item->subject ) ),
  220. esc_html( $item->subject ) );
  221. return '<strong>' . $a . '</strong> ' . $this->row_actions( $actions );
  222. }
  223. function column_from( $item ) {
  224. return esc_html( $item->from );
  225. }
  226. function column_channel( $item ) {
  227. if ( empty( $item->channel ) )
  228. return '';
  229. $term = get_term_by( 'slug', $item->channel,
  230. Flamingo_Inbound_Message::channel_taxonomy );
  231. if ( empty( $term ) || is_wp_error( $term ) )
  232. return $item->channel;
  233. $output = '';
  234. $ancestors = (array) get_ancestors( $term->term_id,
  235. Flamingo_Inbound_Message::channel_taxonomy );
  236. while ( $parent = array_pop( $ancestors ) ) {
  237. $parent = get_term( $parent, Flamingo_Inbound_Message::channel_taxonomy );
  238. if ( empty( $parent ) || is_wp_error( $parent ) )
  239. continue;
  240. $link = admin_url(
  241. 'admin.php?page=flamingo_inbound&channel=' . $parent->slug );
  242. $output .= sprintf( '<a href="%1$s" title="%2$s">%3$s</a> / ',
  243. $link, esc_attr( $parent->name ), esc_html( $parent->name ) );
  244. }
  245. $link = admin_url( 'admin.php?page=flamingo_inbound&channel=' . $term->slug );
  246. $output .= sprintf( '<a href="%1$s" title="%2$s">%3$s</a>',
  247. $link, esc_attr( $term->name ), esc_html( $term->name ) );
  248. return $output;
  249. }
  250. function column_date( $item ) {
  251. $post = get_post( $item->id );
  252. if ( ! $post )
  253. return '';
  254. $t_time = get_the_time( __( 'Y/m/d g:i:s A', 'flamingo' ), $item->id );
  255. $m_time = $post->post_date;
  256. $time = get_post_time( 'G', true, $item->id );
  257. $time_diff = time() - $time;
  258. if ( $time_diff > 0 && $time_diff < 24*60*60 )
  259. $h_time = sprintf( __( '%s ago', 'flamingo' ), human_time_diff( $time ) );
  260. else
  261. $h_time = mysql2date( __( 'Y/m/d', 'flamingo' ), $m_time );
  262. return '<abbr title="' . $t_time . '">' . $h_time . '</abbr>';
  263. }
  264. }