PageRenderTime 233ms CodeModel.GetById 48ms RepoModel.GetById 1ms app.codeStats 1ms

/includes/admin/classes/class-cp-followers-list-table.php

https://gitlab.com/clusterpress/clusterpress
PHP | 395 lines | 182 code | 57 blank | 156 comment | 26 complexity | 89ff1681e42e8fb2311ecb18c0cad3e1 MD5 | raw file
  1. <?php
  2. /**
  3. * ClusterPress Followers List Table
  4. *
  5. * @since 1.0.0
  6. *
  7. * @package ClusterPress\Admin\classes
  8. * @subpackage followers-list-table
  9. */
  10. // Exit if accessed directly
  11. defined( 'ABSPATH' ) || exit;
  12. /**
  13. * Class extending the WP_List_Table to display a managing
  14. * followers list inside sites network admin.
  15. *
  16. * @since 1.0.0
  17. */
  18. class CP_Followers_List_Table extends WP_List_Table {
  19. /**
  20. * The Site ID
  21. *
  22. * @var int The Site ID
  23. */
  24. public $site_id;
  25. /**
  26. * Constructor.
  27. *
  28. * @since 1.0.0
  29. *
  30. * @see WP_List_Table::__construct() for more information on default arguments.
  31. *
  32. * @param array $args An associative array of arguments.
  33. */
  34. public function __construct( $args = array() ) {
  35. parent::__construct( array(
  36. 'singular' => 'follower',
  37. 'plural' => 'followers',
  38. 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  39. ) );
  40. $this->site_id = 0;
  41. if ( isset( $_REQUEST['id'] ) ) {
  42. $this->site_id = (int) $_REQUEST['id'];
  43. }
  44. }
  45. /**
  46. * Check the current user's permissions.
  47. *
  48. * @since 1.0.0
  49. *
  50. * @return bool
  51. */
  52. public function ajax_user_can() {
  53. return current_user_can( 'manage_sites' );
  54. }
  55. /**
  56. * Prepare the followers list for display.
  57. *
  58. * @since 1.0.0
  59. */
  60. public function prepare_items() {
  61. $followersearch = '';
  62. if ( isset( $_REQUEST['s'] ) ) {
  63. $followersearch = wp_unslash( trim( $_REQUEST['s'] ) );
  64. };
  65. $followers_per_page = $this->get_items_per_page( 'sites_page_clusterpress_followers_network_per_page' );
  66. $paged = $this->get_pagenum();
  67. $site_followers = 0;
  68. if ( cp_cluster_is_enabled( 'site' ) && cp_site_is_following_enabled() ) {
  69. $followers = cp_sites_get_site_users( $this->site_id, 'followers' );
  70. if ( ! empty( $followers ) ) {
  71. $site_followers = $followers;
  72. }
  73. }
  74. /**
  75. * Filters the query arguments used to retrieve the followers of the list table.
  76. *
  77. * @since 1.0.0
  78. *
  79. * @param array $args Arguments passed to CP_User_Query to retrieve items for the
  80. * followers list table.
  81. */
  82. $args = apply_filters( 'cp_followers_list_table_query_args', array(
  83. 'orderby' => array( 'display_name' => 'ASC' ),
  84. 'include' => $site_followers,
  85. 'number' => $followers_per_page,
  86. 'offset' => ( $paged-1 ) * $followers_per_page,
  87. 'cp_search' => $followersearch,
  88. ) );
  89. if ( empty( $args['include'] ) ) {
  90. $this->items = array();
  91. $t = 0;
  92. // Query the followers
  93. } else {
  94. $qf = cp_get_users( $args );
  95. $this->items = $qf['users'];
  96. $t = $qf['total'];
  97. }
  98. $this->set_pagination_args( array(
  99. 'total_items' => $t,
  100. 'per_page' => $followers_per_page,
  101. ) );
  102. }
  103. /**
  104. * Output 'no followers' message.
  105. *
  106. * @since 1.0.0
  107. */
  108. public function no_items() {
  109. esc_html_e( 'Aucun adepte n\'a été trouvé.', 'clusterpress' );
  110. }
  111. /**
  112. * Return an empty array as we don't need any views for this table.
  113. *
  114. * @since 1.0.0
  115. *
  116. * @return array An empty array.
  117. */
  118. protected function get_views() {
  119. return array();
  120. }
  121. /**
  122. * Retrieve an associative array of bulk actions available on this table.
  123. *
  124. * @since 1.0.0
  125. *
  126. * @return array Array of bulk actions.
  127. */
  128. protected function get_bulk_actions() {
  129. $actions = array();
  130. if ( ! cp_cluster_is_enabled( 'site' ) || ! cp_site_is_following_enabled() ) {
  131. return $actions;
  132. }
  133. if ( current_user_can( 'remove_users' ) ) {
  134. $actions = array( 'remove' => __( 'Supprimer', 'clusterpress' ) );
  135. }
  136. /**
  137. * Filter here to add ClusterPress specific bulk actions
  138. *
  139. * @since 1.0.0
  140. *
  141. * @param array $actions The ClusterPress Bulk actions.
  142. */
  143. return apply_filters( 'cp_followers_bulk_actions', $actions );
  144. }
  145. /**
  146. * Output the controls to allow the superadmin to promote Followers.
  147. *
  148. * @since 1.0.0
  149. *
  150. * @param string $which Whether this is being invoked above ("top")
  151. * or below the table ("bottom").
  152. */
  153. protected function extra_tablenav( $which ) {
  154. $id = 'promote';
  155. if ( 'bottom' === $which ) {
  156. $id = 'promote2';
  157. }
  158. ?>
  159. <div class="alignleft actions">
  160. <?php if ( current_user_can( 'promote_users' ) && $this->has_items() ) : ?>
  161. <label class="screen-reader-text" for="<?php echo $id ?>">
  162. <?php esc_html_e( 'Promouvoir en tant que&hellip;', 'clusterpress' ) ?>
  163. </label>
  164. <select name="<?php echo $id ?>" id="<?php echo $id ?>">
  165. <option value=""><?php esc_html_e( 'Promouvoir en tant que&hellip;', 'clusterpress' ) ?></option>
  166. <?php wp_dropdown_roles(); ?>
  167. </select>
  168. <?php submit_button( __( 'Promouvoir', 'clusterpress' ), 'button', 'promoteit', false );
  169. endif;
  170. /**
  171. * Fires just before the closing div containing the bulk role-promote controls
  172. * in the Followers list table.
  173. *
  174. * @since 1.0.0.
  175. *
  176. * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
  177. */
  178. do_action( 'cp_restrict_manage_followers', $which ); ?>
  179. </div>
  180. <?php
  181. }
  182. /**
  183. * Capture the bulk action required, and return it.
  184. *
  185. * @since 1.0.0
  186. *
  187. * @return string The bulk action required.
  188. */
  189. public function current_action() {
  190. if ( isset( $_REQUEST['promoteit'] ) &&
  191. ( ! empty( $_REQUEST['promote'] ) || ! empty( $_REQUEST['promote2'] ) ) ) {
  192. return 'promote';
  193. }
  194. return parent::current_action();
  195. }
  196. /**
  197. * Get a list of columns for the list table.
  198. *
  199. * @since 1.0.0
  200. *
  201. * @return array Array in which the key is the ID of the column,
  202. * and the value is the description.
  203. */
  204. public function get_columns() {
  205. return array(
  206. 'cb' => '<input type="checkbox" />',
  207. 'username' => __( 'Nom d\'utilisateur', 'clusterpress' ),
  208. 'name' => __( 'Nom', 'clusterpress' ),
  209. 'email' => __( 'Courriel', 'clusterpress' ),
  210. );
  211. }
  212. /**
  213. * No sortable columns available for the list table.
  214. *
  215. * @since 1.0.0
  216. *
  217. * @return array An empty array to disable sortable columns.
  218. */
  219. protected function get_sortable_columns() {
  220. return array();
  221. }
  222. /**
  223. * Generate the list table rows.
  224. *
  225. * @since 1.0.0
  226. */
  227. public function display_rows() {
  228. foreach ( $this->items as $followerid => $follower_object ) {
  229. echo "\n\t" . $this->single_row( $follower_object );
  230. }
  231. }
  232. /**
  233. * Generate HTML for a single row for the follower.
  234. *
  235. * @since 1.0.0
  236. *
  237. * @param object $follower_object The current follower object.
  238. * @return string Output for a single row.
  239. */
  240. public function single_row( $follower_object ) {
  241. $email = $follower_object->user_email;
  242. // Set up the hover actions for this user
  243. $actions = array();
  244. $checkbox = '';
  245. // Check if the user for this row is editable
  246. if ( current_user_can( 'list_users' ) ) {
  247. // Set up the user editing link
  248. $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $follower_object->ID ) ) );
  249. if ( current_user_can( 'edit_user', $follower_object->ID ) ) {
  250. $edit = sprintf( '<strong><a href="%1$s">%2$s</a></strong><br />', $edit_link, esc_html( $follower_object->user_login ) );
  251. $remove_link = esc_url( wp_nonce_url( add_query_arg( array(
  252. 'page' => 'clusterpress-followers',
  253. 'id' => $this->site_id,
  254. 'action' => 'remove_follower',
  255. 'followers' => $follower_object->ID,
  256. ), network_admin_url( 'sites.php' ) ), 'cp-remove-follower' ) );
  257. $actions = array(
  258. 'edit' => sprintf( '<a href="%1$s">%2$s</a>', $edit_link, esc_html__( 'Modifier', 'clusterpress' ) ),
  259. 'remove' => sprintf( '<a href="%1$s" class="delete">%2$s</a>', $remove_link, esc_html__( 'Supprimer', 'clusterpress' ) ),
  260. );
  261. } else {
  262. $edit = sprintf( '<strong>%s</strong><br />', esc_html( $follower_object->user_login ) );
  263. }
  264. /**
  265. * Filters the action links displayed under each user in the Users list table.
  266. *
  267. * @since 1.0.0
  268. *
  269. * @param array $actions An array of action links to be displayed.
  270. * Default 'Edit'.
  271. * @param object $follower_object Follower object for the currently-listed follower.
  272. */
  273. $actions = apply_filters( 'cp_follower_row_actions', $actions, $follower_object );
  274. // Set up the checkbox ( because the user is editable, otherwise it's empty )
  275. $checkbox = '<label class="screen-reader-text" for="user_' . $follower_object->ID . '">' . sprintf( __( 'Sélectionner %s', 'clusterpress' ), $follower_object->user_login ) . '</label>'
  276. . "<input type='checkbox' name='followers[]' id='follower_{$follower_object->ID}' value='{$follower_object->ID}' />";
  277. } else {
  278. $edit = '<strong>' . $follower_object->user_login . '</strong>';
  279. }
  280. $avatar = get_avatar( $follower_object->ID, 32 );
  281. $r = "<tr id='follower-$follower_object->ID'>";
  282. list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
  283. foreach ( $columns as $column_name => $column_display_name ) {
  284. $classes = "$column_name column-$column_name";
  285. if ( $primary === $column_name ) {
  286. $classes .= ' has-row-actions column-primary';
  287. }
  288. if ( in_array( $column_name, $hidden ) ) {
  289. $classes .= ' hidden';
  290. }
  291. $data = 'data-colname="' . wp_strip_all_tags( $column_display_name ) . '"';
  292. $attributes = "class='$classes' $data";
  293. if ( 'cb' === $column_name ) {
  294. $r .= "<th scope='row' class='check-column'>$checkbox</th>";
  295. } else {
  296. $r .= "<td $attributes>";
  297. switch ( $column_name ) {
  298. case 'username':
  299. $r .= "$avatar $edit";
  300. break;
  301. case 'name':
  302. $r .= "$follower_object->display_name";
  303. break;
  304. case 'email':
  305. $r .= sprintf( '<a href="%1$s">%2$s</a>', esc_url( "mailto:$email" ), $email );
  306. break;
  307. default:
  308. /**
  309. * Filters the display output of custom columns in the Followers list table.
  310. *
  311. * @since 1.0.0
  312. *
  313. * @param string $output Custom column output. Default empty.
  314. * @param string $column_name Column name.
  315. * @param int $user_id ID of the currently-listed follower.
  316. */
  317. $r .= apply_filters( 'cp_manage_followers_custom_column', '', $column_name, $follower_object->ID );
  318. }
  319. if ( $primary === $column_name ) {
  320. $r .= $this->row_actions( $actions );
  321. }
  322. $r .= "</td>";
  323. }
  324. }
  325. $r .= '</tr>';
  326. return $r;
  327. }
  328. /**
  329. * Gets the name of the default primary column.
  330. *
  331. * @since 1.0.0
  332. *
  333. * @return string Name of the default primary column, in this case, 'username'.
  334. */
  335. protected function get_default_primary_column_name() {
  336. return 'username';
  337. }
  338. }