MS_Helper_ListTable_Member

Members List Table.

Defined (1)

The class is defined in the following location(s).

/app/helper/listtable/class-ms-helper-listtable-member.php  
  1. class MS_Helper_ListTable_Member extends MS_Helper_ListTable { 
  2.  
  3. /** 
  4. * A list of all memberships is generated in __construct() for performance. 
  5. * @var array 
  6. */ 
  7. static $memberships = null; 
  8.  
  9. /** 
  10. * Constructor. 
  11. * @since 1.0.0 
  12. */ 
  13. public function __construct() { 
  14. parent::__construct( 
  15. array( 
  16. 'singular' => 'member',  
  17. 'plural' => 'members',  
  18. 'ajax' => false,  
  19. ); 
  20.  
  21. add_action( 
  22. 'ms_helper_listtable_searchbox_start',  
  23. array( $this, 'searchbox_filters' ) 
  24. ); 
  25.  
  26. $memberships = MS_Model_Membership::get_memberships( 
  27. array( 'include_guest' => 0 ) 
  28. ); 
  29. self::$memberships = array(); 
  30.  
  31. foreach ( $memberships as $item ) { 
  32. self::$memberships[$item->id] = (object) array( 
  33. 'label' => $item->name,  
  34. 'attr' => sprintf( 'data-color="%1$s"', $item->get_color() ),  
  35. ); 
  36.  
  37. /** 
  38. * Get list table columns. 
  39. * @since 1.0.0 
  40. * @return array { 
  41. * Returns array of $id => $title. 
  42. * @type string $id The list table column id. 
  43. * @type string $title The list table column title. 
  44. * } 
  45. */ 
  46. public function get_columns() { 
  47. $columns = array( 
  48. 'cb' => '<input type="checkbox" />',  
  49. 'username' => __( 'Username', 'membership2' ),  
  50. 'email' => __( 'E-mail', 'membership2' ),  
  51. 'membership' => __( 'Membership', 'membership2' ),  
  52. 'infos' => ' ',  
  53. ); 
  54.  
  55. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_TRIAL ) ) { 
  56. unset( $columns['trial'] ); 
  57.  
  58. return apply_filters( 
  59. 'ms_helper_listtable_member_get_columns',  
  60. $columns 
  61. ); 
  62.  
  63. /** 
  64. * Get list table sortable columns. 
  65. * @since 1.0.0 
  66. * @return array { 
  67. * Returns array of $id => $title. 
  68. * @type string $id The list table column id. 
  69. * @type array $orderby The field id to use order. 
  70. * } 
  71. */ 
  72. public function get_sortable_columns() { 
  73. return apply_filters( 
  74. 'ms_helper_listtable_member_get_sortable_columns',  
  75. array( 
  76. 'username' => 'login',  
  77. 'email' => 'email',  
  78. ); 
  79.  
  80. /** 
  81. * Prepare list items. 
  82. * @since 1.0.0 
  83. */ 
  84. public function prepare_items() { 
  85. $this->_column_headers = array( 
  86. $this->get_columns(),  
  87. array(),  
  88. $this->get_sortable_columns(),  
  89. ); 
  90.  
  91. $per_page = apply_filters( 
  92. 'ms_helper_listtable_member_items_per_page',  
  93. self::DEFAULT_PAGE_SIZE 
  94. ); 
  95. $current_page = $this->get_pagenum(); 
  96.  
  97. $args = array( 
  98. 'number' => $per_page,  
  99. 'offset' => ( $current_page - 1 ) * $per_page,  
  100. ); 
  101.  
  102. // Prepare the filter details. 
  103. $args = $this->prepare_query_args( $args ); 
  104.  
  105. $total_items = MS_Model_Member::get_members_count( $args ); 
  106. $this->items = MS_Model_Member::get_members( $args ); 
  107.  
  108. $this->set_pagination_args( 
  109. array( 
  110. 'total_items' => $total_items,  
  111. 'per_page' => $per_page,  
  112. ); 
  113.  
  114. do_action( 
  115. 'ms_helper_listtable_member_prepare_items',  
  116. $args,  
  117. $this 
  118. ); 
  119.  
  120. /** 
  121. * Returns a query arg structure tailored to give the defined results 
  122. * @since 1.0.0 
  123. * @return array Query args 
  124. */ 
  125. protected function prepare_query_args( $args ) { 
  126. lib3()->array->equip_request( 
  127. 's',  
  128. 'membership_id',  
  129. 'search_options',  
  130. 'status' 
  131. ); 
  132.  
  133. // Prepare order by statement. 
  134. if ( ! empty( $_REQUEST['orderby'] ) && ! empty( $_REQUEST['order'] ) ) { 
  135. $args['orderby'] = $_REQUEST['orderby']; 
  136. $args['order'] = $_REQUEST['order']; 
  137.  
  138. // Filter by search-term 
  139. $search_filter = $_REQUEST['s']; 
  140. if ( ! empty( $search_filter ) ) { 
  141. $this->search_string = $search_filter; 
  142. $search_option = $_REQUEST['search_options']; 
  143.  
  144. switch ( $search_option ) { 
  145. case 'email': 
  146. case 'username': 
  147. $args['search'] = sprintf( '*%s*', $search_filter ); 
  148. break; 
  149.  
  150. default: 
  151. $args['meta_query'][ $search_option ] = array( 
  152. 'key' => $search_option,  
  153. 'value' => $search_filter,  
  154. 'compare' => 'LIKE',  
  155. ); 
  156. break; 
  157.  
  158. $args['posts_per_page'] = -1; 
  159. $args['number'] = false; 
  160. $args['offset'] = 0; 
  161.  
  162. // Filter by membership_id and membership status 
  163. $membership_id = $_REQUEST['membership_id']; 
  164. $members = array(); 
  165. $filter = array(); 
  166.  
  167. if ( ! empty( $membership_id ) ) { 
  168. $args['membership_id'] = $membership_id; 
  169.  
  170. $status = $_REQUEST['status']; 
  171. if ( empty( $status ) ) { $status = MS_Model_Relationship::STATUS_ACTIVE; } 
  172. $args['subscription_status'] = $status; 
  173.  
  174. return $args; 
  175.  
  176. /** 
  177. * Display checkbox column. 
  178. * @since 1.0.0 
  179. * @param mixed $member The table item to display. 
  180. */ 
  181. public function column_cb( $member ) { 
  182. if ( MS_Model_Member::is_admin_user( $member->id ) ) { 
  183. $html = ''; 
  184. } else { 
  185. $html = sprintf( 
  186. '<input type="checkbox" name="member_id[]" value="%s" />',  
  187. esc_attr( $member->id ) 
  188. ); 
  189.  
  190. return $html; 
  191.  
  192. /** 
  193. * Infos-Column 
  194. * @since 1.0.0 
  195. * @param mixed $member The table item to display. 
  196. */ 
  197. public function column_infos( $member ) { 
  198. $dialog_data = array( 
  199. 'member_id' => $member->id,  
  200. ); 
  201.  
  202. $html = sprintf( 
  203. '<a href="#" data-ms-dialog="View_Member_Dialog" data-ms-data="%s"><i class="dashicons dashicons-id"></i></a>',  
  204. esc_attr( json_encode( $dialog_data ) ) 
  205. ); 
  206.  
  207. return $html; 
  208.  
  209. /** 
  210. * Display Username column. 
  211. * @since 1.0.0 
  212. * @param mixed $member The table item to display. 
  213. */ 
  214. public function column_username( $member ) { 
  215. $actions = array(); 
  216. $actions['edit'] = sprintf( 
  217. '<a href="%s">%s</a>',  
  218. MS_Controller_Plugin::get_admin_url( 
  219. 'add-member',  
  220. array( 'user_id' => $member->id ) 
  221. ),  
  222. __( 'Subscription Details', 'membership2' ) 
  223. ); 
  224. $actions['profile'] = sprintf( 
  225. '<a href="%s">%s</a>',  
  226. admin_url( 'user-edit.php?user_id=' . $member->id ),  
  227. __( 'Edit Profile', 'membership2' ) 
  228. ); 
  229.  
  230. $html = sprintf( 
  231. '%1$s %2$s',  
  232. $member->username,  
  233. $this->row_actions( $actions ) 
  234. ); 
  235.  
  236. return $html; 
  237.  
  238. /** 
  239. * Display Email column. 
  240. * @since 1.0.0 
  241. * @param mixed $member The table item to display. 
  242. */ 
  243. public function column_email( $member ) { 
  244. $html = $member->email; 
  245. return $html; 
  246.  
  247. /** 
  248. * Create membership column. 
  249. * @since 1.0.0 
  250. * @param MS_Model_Member $member The member object. 
  251. */ 
  252. public function column_membership( $member ) { 
  253. if ( MS_Model_Member::is_admin_user( $member->id ) ) { 
  254. $html = '<b>' . __( 'Admin User', 'membership2' ) . '</b>'; 
  255. } else { 
  256. $subscriptions = $member->get_membership_ids(); 
  257.  
  258. $visitor = array( 
  259. 'id' => 'ms-empty-' . $member->id,  
  260. 'type' => MS_Helper_Html::TYPE_HTML_TEXT,  
  261. 'value' => __( '(Visitor)' ),  
  262. 'after' => 'Edit',  
  263. 'class' => 'ms-empty-note',  
  264. ); 
  265.  
  266. $list = array( 
  267. 'id' => 'ms-memberships-' . $member->id,  
  268. 'type' => MS_Helper_Html::INPUT_TYPE_SELECT,  
  269. 'value' => $subscriptions,  
  270. 'field_options' => self::$memberships,  
  271. 'multiple' => true,  
  272. 'class' => 'ms-memberships',  
  273. 'ajax_data' => array( 
  274. 'action' => MS_Controller_Member::AJAX_ACTION_CHANGE_MEMBERSHIPS,  
  275. 'member' => $member->id,  
  276. ),  
  277. ); 
  278.  
  279. $html = sprintf( 
  280. '<div class="no-auto-init">%1$s%2$s</div>',  
  281. MS_Helper_Html::html_element( $visitor, true ),  
  282. MS_Helper_Html::html_element( $list, true ) 
  283. ); 
  284.  
  285. return apply_filters( 
  286. 'ms_helper_listtable_member_column_membership',  
  287. $html,  
  288. $member,  
  289. $this 
  290. ); 
  291.  
  292. /** 
  293. * Adds a class to the <tr> element 
  294. * @since 1.0.0 
  295. * @param object $member 
  296. */ 
  297. protected function single_row_class( $member ) { 
  298. $subscriptions = $member->get_membership_ids(); 
  299. $class = empty( $subscriptions ) ? 'ms-empty' : 'ms-assigned'; 
  300.  
  301. return $class; 
  302.  
  303. /** 
  304. * Bulk actions options. 
  305. * @since 1.0.0 
  306. * @param array { 
  307. * @type string $action The action name. 
  308. * @type mixed $desciption The action description. 
  309. * } 
  310. */ 
  311. public function get_bulk_actions() { 
  312. $protect_key = __( 'Add Membership', 'membership2' ); 
  313. $unprotect_key = __( 'Drop Membership', 'membership2' ); 
  314. $bulk_actions = array( 
  315. 'drop-all' => __( 'Drop all Memberships', 'membership2' ),  
  316. $protect_key => array(),  
  317. $unprotect_key => array(),  
  318. ); 
  319.  
  320. $args = array( 
  321. 'include_guest' => 0,  
  322. ); 
  323. $memberships = MS_Model_Membership::get_membership_names( $args ); 
  324. $txt_add = __( 'Add: %s', 'membership2' ); 
  325. $txt_rem = __( 'Drop: %s', 'membership2' ); 
  326. foreach ( $memberships as $id => $name ) { 
  327. $bulk_actions[$protect_key]['add-' . $id] = sprintf( $txt_add, $name ); 
  328. $bulk_actions[$unprotect_key]['drop-' . $id] = sprintf( $txt_rem, $name ); 
  329.  
  330. return apply_filters( 
  331. 'ms_helper_listtable_member_get_bulk_actions',  
  332. $bulk_actions,  
  333. $this 
  334. ); 
  335.  
  336. /** 
  337. * Add custom filters to the searchbox 
  338. * @since 1.0.0 
  339. */ 
  340. public function searchbox_filters() { 
  341. lib3()->array->equip_request( 'search_options' ); 
  342.  
  343. $search_options = array( 
  344. 'id' => 'search_options',  
  345. 'type' => MS_Helper_Html::INPUT_TYPE_SELECT,  
  346. 'value' => $_REQUEST['search_options'],  
  347. 'field_options' => array( 
  348. 'username' => __( 'Username / E-mail', 'membership2' ),  
  349. 'nickname' => __( 'Nickname', 'membership2' ),  
  350. 'first_name' => __( 'First Name', 'membership2' ),  
  351. 'last_name' => __( 'Last Name', 'membership2' ),  
  352. ),  
  353. ); 
  354.  
  355. // Display the extra search options 
  356. MS_Helper_Html::html_element( $search_options ); 
  357.  
  358. /** 
  359. * This list has no views. 
  360. * @since 1.0.2.0 
  361. * @return array 
  362. */ 
  363. public function get_views() { 
  364. $views = array(); 
  365. $args = array(); 
  366. $count = 0; 
  367.  
  368. $views['label'] = array( 
  369. 'label' => __( 'Subscription Status:', 'membership2' ),  
  370. ); 
  371.  
  372. if ( empty( $_REQUEST['membership_id'] ) ) { 
  373. // All users 
  374. $url = esc_url_raw( add_query_arg( 'status', 'all' ) ); 
  375. $views['all'] = array( 
  376. 'url' => $url,  
  377. 'label' => __( 'All users', 'membership2' ),  
  378. ); 
  379. } else { 
  380. $args['membership_id'] = $_REQUEST['membership_id']; 
  381.  
  382. // Active, Trial, Cancelled 
  383. $url = esc_url_raw( remove_query_arg( 'status' ) ); 
  384. $args['subscription_status'] = MS_Model_Relationship::STATUS_ACTIVE; 
  385. $count = MS_Model_Member::get_members_count( $args ); 
  386. $views['active'] = array( 
  387. 'url' => $url,  
  388. 'label' => __( 'Active subscription', 'membership2' ),  
  389. 'count' => $count,  
  390. ); 
  391.  
  392. // Cancelled 
  393. $url = esc_url_raw( add_query_arg( 'status', MS_Model_Relationship::STATUS_CANCELED ) ); 
  394. $args['subscription_status'] = MS_Model_Relationship::STATUS_CANCELED; 
  395. $count = MS_Model_Member::get_members_count( $args ); 
  396. $views['cancelled'] = array( 
  397. 'url' => $url,  
  398. 'label' => __( 'Cancelled', 'membership2' ),  
  399. 'count' => $count,  
  400. ); 
  401.  
  402. // Trial 
  403. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_TRIAL ) ) { 
  404. $url = esc_url_raw( add_query_arg( 'status', MS_Model_Relationship::STATUS_TRIAL ) ); 
  405. $args['subscription_status'] = MS_Model_Relationship::STATUS_TRIAL; 
  406. $count = MS_Model_Member::get_members_count( $args ); 
  407. $views['trial'] = array( 
  408. 'url' => $url,  
  409. 'label' => __( 'Trial', 'membership2' ),  
  410. 'count' => $count,  
  411. ); 
  412.  
  413. // Expired, Trial-Expired 
  414. $url = esc_url_raw( add_query_arg( 'status', 'expired' ) ); 
  415. $args['subscription_status'] = 'expired'; 
  416. $count = MS_Model_Member::get_members_count( $args ); 
  417. $views['expired'] = array( 
  418. 'url' => $url,  
  419. 'label' => __( 'Expired', 'membership2' ),  
  420. 'count' => $count,  
  421. ); 
  422.  
  423. return $views; 
  424.  
  425. /** 
  426. * Return true if the current list is a view except "all" 
  427. * @since 1.0.2.0 
  428. * @return bool 
  429. */ 
  430. public function is_view() { 
  431. return ! empty( $_GET['status'] ) || ! empty( $_GET['membership_id'] );