class.swpm-members.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. <?php
  2. if ( ! class_exists( 'WP_List_Table' ) ) {
  3. require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
  4. }
  5. class SwpmMembers extends WP_List_Table {
  6. function __construct() {
  7. parent::__construct(
  8. array(
  9. 'singular' => SwpmUtils::_( 'Member' ),
  10. 'plural' => SwpmUtils::_( 'Members' ),
  11. 'ajax' => false,
  12. )
  13. );
  14. }
  15. function get_columns() {
  16. return array(
  17. 'cb' => '<input type="checkbox" />',
  18. 'member_id' => SwpmUtils::_( 'ID' ),
  19. 'user_name' => SwpmUtils::_( 'Username' ),
  20. 'first_name' => SwpmUtils::_( 'First Name' ),
  21. 'last_name' => SwpmUtils::_( 'Last Name' ),
  22. 'email' => SwpmUtils::_( 'Email' ),
  23. 'alias' => SwpmUtils::_( 'Membership Level' ),
  24. 'subscription_starts' => SwpmUtils::_( 'Access Starts' ),
  25. 'account_state' => SwpmUtils::_( 'Account State' ),
  26. 'last_accessed' => SwpmUtils::_( 'Last Login Date' ),
  27. );
  28. }
  29. function get_sortable_columns() {
  30. return array(
  31. 'member_id' => array( 'member_id', true ), //True means already sorted
  32. 'user_name' => array( 'user_name', false ),
  33. 'first_name' => array( 'first_name', false ),
  34. 'last_name' => array( 'last_name', false ),
  35. 'email' => array( 'email', false ),
  36. 'alias' => array( 'alias', false ),
  37. 'subscription_starts' => array( 'subscription_starts', false ),
  38. 'account_state' => array( 'account_state', false ),
  39. 'last_accessed' => array( 'last_accessed', false ),
  40. );
  41. }
  42. function get_bulk_actions() {
  43. $actions = array(
  44. 'bulk_delete' => SwpmUtils::_( 'Delete' ),
  45. 'bulk_active' => SwpmUtils::_( 'Set Status to Active' ),
  46. 'bulk_active_notify' => SwpmUtils::_( 'Set Status to Active and Notify' ),
  47. 'bulk_inactive' => SwpmUtils::_( 'Set Status to Inactive' ),
  48. 'bulk_pending' => SwpmUtils::_( 'Set Status to Pending' ),
  49. 'bulk_expired' => SwpmUtils::_( 'Set Status to Expired' ),
  50. );
  51. return $actions;
  52. }
  53. function column_default( $item, $column_name ) {
  54. return $item[ $column_name ];
  55. }
  56. function column_account_state( $item ) {
  57. $acc_state_str = ucfirst( $item['account_state'] );
  58. return SwpmUtils::_( $acc_state_str );
  59. }
  60. function column_member_id( $item ) {
  61. $delete_swpmuser_nonce = wp_create_nonce( 'delete_swpmuser_admin_end' );
  62. $actions = array(
  63. 'edit' => sprintf( '<a href="admin.php?page=simple_wp_membership&member_action=edit&member_id=%s">Edit/View</a>', $item['member_id'] ),
  64. 'delete' => sprintf( '<a href="admin.php?page=simple_wp_membership&member_action=delete&member_id=%s&delete_swpmuser_nonce=%s" onclick="return confirm(\'Are you sure you want to delete this entry?\')">Delete</a>', $item['member_id'], $delete_swpmuser_nonce ),
  65. );
  66. return $item['member_id'] . $this->row_actions( $actions );
  67. }
  68. function column_user_name( $item ) {
  69. $user_name = $item['user_name'];
  70. if ( empty( $user_name ) ) {
  71. $user_name = '[' . SwpmUtils::_( 'incomplete' ) . ']';
  72. }
  73. return $user_name;
  74. }
  75. function column_cb( $item ) {
  76. return sprintf(
  77. '<input type="checkbox" name="members[]" value="%s" />',
  78. $item['member_id']
  79. );
  80. }
  81. function prepare_items() {
  82. global $wpdb;
  83. $this->process_bulk_action();
  84. $records_query_head = 'SELECT member_id,user_name,first_name,last_name,email,alias,subscription_starts,account_state,last_accessed';
  85. $count_query_head = 'SELECT COUNT(member_id)';
  86. $query = ' ';
  87. $query .= ' FROM ' . $wpdb->prefix . 'swpm_members_tbl';
  88. $query .= ' LEFT JOIN ' . $wpdb->prefix . 'swpm_membership_tbl';
  89. $query .= ' ON ( membership_level = id ) ';
  90. //Get the search string (if any)
  91. $s = filter_input( INPUT_GET, 's' );
  92. if ( empty( $s ) ) {
  93. $s = filter_input( INPUT_POST, 's' );
  94. }
  95. $status = filter_input( INPUT_GET, 'status' );
  96. $status = esc_attr( $status );//Escape value
  97. $filters = array();
  98. //Add the search parameter to the query
  99. if ( ! empty( $s ) ) {
  100. $s = sanitize_text_field( $s );
  101. $s = trim( $s ); //Trim the input
  102. $s = esc_attr( $s );
  103. $filters[] = "( user_name LIKE '%" . strip_tags( $s ) . "%' "
  104. . " OR first_name LIKE '%" . strip_tags( $s ) . "%' "
  105. . " OR last_name LIKE '%" . strip_tags( $s ) . "%' "
  106. . " OR email LIKE '%" . strip_tags( $s ) . "%' "
  107. . " OR address_city LIKE '%" . strip_tags( $s ) . "%' "
  108. . " OR address_state LIKE '%" . strip_tags( $s ) . "%' "
  109. . " OR country LIKE '%" . strip_tags( $s ) . "%' "
  110. . " OR company_name LIKE '%" . strip_tags( $s ) . "%' )";
  111. }
  112. //Add account status filtering to the query
  113. if ( ! empty( $status ) ) {
  114. if ( $status == 'incomplete' ) {
  115. $filters[] = "user_name = ''";
  116. } else {
  117. $filters[] = "account_state = '" . $status . "'";
  118. }
  119. }
  120. //Add membership level filtering
  121. $membership_level = filter_input( INPUT_GET, 'membership_level', FILTER_SANITIZE_NUMBER_INT );
  122. if ( ! empty( $membership_level ) ) {
  123. $filters[] = sprintf( "membership_level = '%d'", $membership_level );
  124. }
  125. //Build the WHERE clause of the query string
  126. if ( ! empty( $filters ) ) {
  127. $filter_str = '';
  128. foreach ( $filters as $ind => $filter ) {
  129. $filter_str .= $ind === 0 ? $filter : ' AND ' . $filter;
  130. }
  131. $query .= 'WHERE ' . $filter_str;
  132. }
  133. //Build the orderby and order query parameters
  134. $orderby = filter_input( INPUT_GET, 'orderby' );
  135. $orderby = empty( $orderby ) ? 'member_id' : $orderby;
  136. $order = filter_input( INPUT_GET, 'order' );
  137. $order = empty( $order ) ? 'DESC' : $order;
  138. $sortable_columns = $this->get_sortable_columns();
  139. $orderby = SwpmUtils::sanitize_value_by_array( $orderby, $sortable_columns );
  140. $order = SwpmUtils::sanitize_value_by_array(
  141. $order,
  142. array(
  143. 'DESC' => '1',
  144. 'ASC' => '1',
  145. )
  146. );
  147. $query .= ' ORDER BY ' . $orderby . ' ' . $order;
  148. //Execute the query
  149. $totalitems = $wpdb->get_var( $count_query_head . $query );
  150. //Pagination setup
  151. $perpage = apply_filters( 'swpm_members_menu_items_per_page', 50 );
  152. $paged = filter_input( INPUT_GET, 'paged' );
  153. if ( empty( $paged ) || ! is_numeric( $paged ) || $paged <= 0 ) {
  154. $paged = 1;
  155. }
  156. $totalpages = ceil( $totalitems / $perpage );
  157. if ( ! empty( $paged ) && ! empty( $perpage ) ) {
  158. $offset = ( $paged - 1 ) * $perpage;
  159. $query .= ' LIMIT ' . (int) $offset . ',' . (int) $perpage;
  160. }
  161. $this->set_pagination_args(
  162. array(
  163. 'total_items' => $totalitems,
  164. 'total_pages' => $totalpages,
  165. 'per_page' => $perpage,
  166. )
  167. );
  168. $columns = $this->get_columns();
  169. $hidden = array();
  170. $sortable = $this->get_sortable_columns();
  171. $this->_column_headers = array( $columns, $hidden, $sortable );
  172. $this->items = $wpdb->get_results( $records_query_head . $query, ARRAY_A );
  173. }
  174. function get_user_count_by_account_state() {
  175. global $wpdb;
  176. $query = 'SELECT count(member_id) AS count, account_state FROM ' . $wpdb->prefix . 'swpm_members_tbl GROUP BY account_state';
  177. $result = $wpdb->get_results( $query, ARRAY_A );
  178. $count = array();
  179. $all = 0;
  180. foreach ( $result as $row ) {
  181. $count[ $row['account_state'] ] = $row['count'];
  182. $all += intval( $row['count'] );
  183. }
  184. $count ['all'] = $all;
  185. $count_incomplete_query = 'SELECT COUNT(*) FROM ' . $wpdb->prefix . "swpm_members_tbl WHERE user_name = ''";
  186. $count['incomplete'] = $wpdb->get_var( $count_incomplete_query );
  187. return $count;
  188. }
  189. function no_items() {
  190. _e( 'No member found.', 'simple-membership' );
  191. }
  192. function process_form_request() {
  193. if ( isset( $_REQUEST['member_id'] ) ) {
  194. //This is a member profile edit action
  195. $record_id = sanitize_text_field( $_REQUEST['member_id'] );
  196. if ( ! is_numeric( $record_id ) ) {
  197. wp_die( 'Error! ID must be numeric.' );
  198. }
  199. return $this->edit( absint( $record_id ) );
  200. }
  201. //This is an profile add action.
  202. return $this->add();
  203. }
  204. function add() {
  205. $form = apply_filters( 'swpm_admin_registration_form_override', '' );
  206. if ( ! empty( $form ) ) {
  207. echo $form;
  208. return;
  209. }
  210. global $wpdb;
  211. $member = SwpmTransfer::$default_fields;
  212. $member['member_since'] = SwpmUtils::get_current_date_in_wp_zone();//date( 'Y-m-d' );
  213. $member['subscription_starts'] = SwpmUtils::get_current_date_in_wp_zone();//date( 'Y-m-d' );
  214. if ( isset( $_POST['createswpmuser'] ) ) {
  215. $member = array_map( 'sanitize_text_field', $_POST );
  216. }
  217. extract( $member, EXTR_SKIP );
  218. $query = 'SELECT * FROM ' . $wpdb->prefix . 'swpm_membership_tbl WHERE id !=1 ';
  219. $levels = $wpdb->get_results( $query, ARRAY_A );
  220. $add_user_template_path = apply_filters('swpm_admin_registration_add_user_template_path', SIMPLE_WP_MEMBERSHIP_PATH . 'views/admin_add.php');
  221. include_once $add_user_template_path;
  222. return false;
  223. }
  224. function edit( $id ) {
  225. global $wpdb;
  226. $id = absint( $id );
  227. $query = "SELECT * FROM {$wpdb->prefix}swpm_members_tbl WHERE member_id = $id";
  228. $member = $wpdb->get_row( $query, ARRAY_A );
  229. if ( isset( $_POST['editswpmuser'] ) ) {
  230. $_POST['user_name'] = sanitize_text_field( $member['user_name'] );
  231. $_POST['email'] = sanitize_email( $member['email'] );
  232. foreach ( $_POST as $key => $value ) {
  233. $key = sanitize_text_field( $key );
  234. if ( $key == 'email' ) {
  235. $member[ $key ] = sanitize_email( $value );
  236. } else {
  237. $member[ $key ] = sanitize_text_field( $value );
  238. }
  239. }
  240. }
  241. extract( $member, EXTR_SKIP );
  242. $query = 'SELECT * FROM ' . $wpdb->prefix . 'swpm_membership_tbl WHERE id !=1 ';
  243. $levels = $wpdb->get_results( $query, ARRAY_A );
  244. $edit_user_template_path = apply_filters('swpm_admin_registration_edit_user_template_path', SIMPLE_WP_MEMBERSHIP_PATH . 'views/admin_edit.php');
  245. include_once $edit_user_template_path;
  246. return false;
  247. }
  248. function process_bulk_action() {
  249. //Detect when a bulk action is being triggered... then perform the action.
  250. $members = isset( $_REQUEST['members'] ) ? $_REQUEST['members'] : array();
  251. $members = array_map( 'sanitize_text_field', $members );
  252. $current_action = $this->current_action();
  253. if ( ! empty( $current_action ) ) {
  254. //Bulk operation action. Lets make sure multiple records were selected before going ahead.
  255. if ( empty( $members ) ) {
  256. echo '<div id="message" class="error"><p>Error! You need to select multiple records to perform a bulk action!</p></div>';
  257. return;
  258. }
  259. } else {
  260. //No bulk operation.
  261. return;
  262. }
  263. //perform the bulk operation according to the selection
  264. if ( 'bulk_delete' === $current_action ) {
  265. foreach ( $members as $record_id ) {
  266. if ( ! is_numeric( $record_id ) ) {
  267. wp_die( 'Error! ID must be numeric.' );
  268. }
  269. self::delete_user_by_id( $record_id );
  270. }
  271. echo '<div id="message" class="updated fade"><p>Selected records deleted successfully!</p></div>';
  272. return;
  273. } elseif ( 'bulk_active' === $current_action ) {
  274. $this->bulk_set_status( $members, 'active' );
  275. } elseif ( 'bulk_active_notify' == $current_action ) {
  276. $this->bulk_set_status( $members, 'active', true );
  277. } elseif ( 'bulk_inactive' == $current_action ) {
  278. $this->bulk_set_status( $members, 'inactive' );
  279. } elseif ( 'bulk_pending' == $current_action ) {
  280. $this->bulk_set_status( $members, 'pending' );
  281. } elseif ( 'bulk_expired' == $current_action ) {
  282. $this->bulk_set_status( $members, 'expired' );
  283. }
  284. echo '<div id="message" class="updated fade"><p>Bulk operation completed successfully!</p></div>';
  285. }
  286. function bulk_set_status( $members, $status, $notify = false ) {
  287. $ids = implode( ',', array_map( 'absint', $members ) );
  288. if ( empty( $ids ) ) {
  289. return;
  290. }
  291. global $wpdb;
  292. $query = 'UPDATE ' . $wpdb->prefix . 'swpm_members_tbl ' .
  293. " SET account_state = '" . $status . "' WHERE member_id in (" . $ids . ')';
  294. $wpdb->query( $query );
  295. if ( $notify ) {
  296. $settings = SwpmSettings::get_instance();
  297. $emails = $wpdb->get_col( 'SELECT email FROM ' . $wpdb->prefix . 'swpm_members_tbl ' . " WHERE member_id IN ( $ids ) " );
  298. $subject = $settings->get_value( 'bulk-activate-notify-mail-subject' );
  299. if ( empty( $subject ) ) {
  300. $subject = 'Account Activated!';
  301. }
  302. $body = $settings->get_value( 'bulk-activate-notify-mail-body' );
  303. if ( empty( $body ) ) {
  304. $body = 'Hi, Your account has been activated successfully!';
  305. }
  306. $from_address = $settings->get_value( 'email-from' );
  307. $headers = 'From: ' . $from_address . "\r\n";
  308. foreach ($emails as $to_email) {
  309. //Send the activation email one by one to all the selected members.
  310. $subject = apply_filters( 'swpm_email_bulk_set_status_subject', $subject );
  311. $body = apply_filters( 'swpm_email_bulk_set_status_body', $body );
  312. $to_email = trim($to_email);
  313. SwpmMiscUtils::mail( $to_email, $subject, $body, $headers );
  314. SwpmLog::log_simple_debug( 'Bulk activation email notification sent. Activation email sent to the following email: ' . $to_email, true );
  315. }
  316. }
  317. }
  318. function delete() {
  319. if ( isset( $_REQUEST['member_id'] ) ) {
  320. //Check we are on the admin end and user has management permission
  321. SwpmMiscUtils::check_user_permission_and_is_admin( 'member deletion by admin' );
  322. //Check nonce
  323. if ( ! isset( $_REQUEST['delete_swpmuser_nonce'] ) || ! wp_verify_nonce( $_REQUEST['delete_swpmuser_nonce'], 'delete_swpmuser_admin_end' ) ) {
  324. //Nonce check failed.
  325. wp_die( SwpmUtils::_( 'Error! Nonce verification failed for user delete from admin end.' ) );
  326. }
  327. $id = sanitize_text_field( $_REQUEST['member_id'] );
  328. $id = absint( $id );
  329. self::delete_user_by_id( $id );
  330. }
  331. }
  332. public static function delete_user_by_id( $id ) {
  333. if ( ! is_numeric( $id ) ) {
  334. wp_die( 'Error! Member ID must be numeric.' );
  335. }
  336. $swpm_user = SwpmMemberUtils::get_user_by_id( $id );
  337. $user_name = $swpm_user->user_name;
  338. self::delete_wp_user( $user_name ); //Deletes the WP User record
  339. self::delete_swpm_user_by_id( $id ); //Deletes the SWPM record
  340. }
  341. public static function delete_swpm_user_by_id( $id ) {
  342. self::delete_user_subs( $id );
  343. global $wpdb;
  344. $query = 'DELETE FROM ' . $wpdb->prefix . "swpm_members_tbl WHERE member_id = $id";
  345. $wpdb->query( $query );
  346. }
  347. public static function delete_wp_user( $user_name ) {
  348. $wp_user_id = username_exists( $user_name );
  349. if ( empty( $wp_user_id ) || ! is_numeric( $wp_user_id ) ) {
  350. return;
  351. }
  352. if ( ! self::is_wp_super_user( $wp_user_id ) ) {
  353. //Not an admin user so it is safe to delete this user.
  354. include_once ABSPATH . 'wp-admin/includes/user.php';
  355. wp_delete_user( $wp_user_id, 1 ); //assigns all related to this user to admin.
  356. } else {
  357. //This is an admin user. So not going to delete the WP User record.
  358. SwpmTransfer::get_instance()->set( 'status', 'For safety, we do not allow deletion of any associated WordPress account with administrator role.' );
  359. return;
  360. }
  361. }
  362. private static function delete_user_subs( $id ) {
  363. $member = SwpmMemberUtils::get_user_by_id( $id );
  364. if ( ! $member ) {
  365. return false;
  366. }
  367. // let's check if Stripe subscription needs to be cancelled
  368. global $wpdb;
  369. $q = $wpdb->prepare(
  370. 'SELECT *
  371. FROM `' . $wpdb->prefix . 'swpm_payments_tbl`
  372. WHERE email = %s
  373. AND (gateway = "stripe" OR gateway = "stripe-sca-subs")
  374. AND subscr_id != ""',
  375. array( $member->email )
  376. );
  377. $res = $wpdb->get_results( $q, ARRAY_A );
  378. if ( ! $res ) {
  379. return false;
  380. }
  381. foreach ( $res as $sub ) {
  382. if ( substr( $sub['subscr_id'], 0, 4 ) !== 'sub_' ) {
  383. //not Stripe subscription
  384. continue;
  385. }
  386. //let's find the payment button
  387. $q = $wpdb->prepare( "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key='subscr_id' AND meta_value=%s", $sub['subscr_id'] );
  388. $res_post = $wpdb->get_row( $q );
  389. if ( ! $res_post ) {
  390. //no button found
  391. continue;
  392. }
  393. $button_id = get_post_meta( $res_post->post_id, 'payment_button_id', true );
  394. $button = get_post( $button_id );
  395. if ( ! $button ) {
  396. //no button found
  397. continue;
  398. }
  399. SwpmLog::log_simple_debug( 'Attempting to cancel Stripe Subscription ' . $sub['subscr_id'], true );
  400. $is_live = get_post_meta( $button_id, 'is_live', true );
  401. //API Keys
  402. $api_keys = SwpmMiscUtils::get_stripe_api_keys_from_payment_button( $button_id, $is_live );
  403. //Include the Stripe library.
  404. SwpmMiscUtils::load_stripe_lib();
  405. \Stripe\Stripe::setApiKey( $api_keys['secret'] );
  406. $error = null;
  407. // Let's try to cancel subscription
  408. try {
  409. $sub = \Stripe\Subscription::retrieve( $sub['subscr_id'] );
  410. $sub->cancel();
  411. } catch ( Exception $e ) {
  412. SwpmLog::log_simple_debug( 'Error occurred during Stripe Subscription cancellation. ' . $e->getMessage(), false );
  413. $body = $e->getJsonBody();
  414. $error = $body['error'];
  415. $error_string = wp_json_encode( $error );
  416. SwpmLog::log_simple_debug( 'Error details: ' . $error_string, false );
  417. }
  418. if ( ! isset( $error ) ) {
  419. SwpmLog::log_simple_debug( 'Stripe Subscription has been cancelled.', true );
  420. }
  421. }
  422. }
  423. public static function is_wp_super_user( $wp_user_id ) {
  424. $user_data = get_userdata( $wp_user_id );
  425. if ( empty( $user_data ) ) {
  426. //Not an admin user if we can't find his data for the given ID.
  427. return false;
  428. }
  429. if ( isset( $user_data->wp_capabilities['administrator'] ) ) {//Check capability
  430. //admin user
  431. return true;
  432. }
  433. if ( $user_data->wp_user_level == 10 ) {//Check for old style wp user level
  434. //admin user
  435. return true;
  436. }
  437. //This is not an admin user
  438. return false;
  439. }
  440. function bulk_operation_menu() {
  441. echo '<div id="poststuff"><div id="post-body">';
  442. if ( isset( $_REQUEST['swpm_bulk_change_level_process'] ) ) {
  443. //Check nonce
  444. $swpm_bulk_change_level_nonce = filter_input( INPUT_POST, 'swpm_bulk_change_level_nonce' );
  445. if ( ! wp_verify_nonce( $swpm_bulk_change_level_nonce, 'swpm_bulk_change_level_nonce_action' ) ) {
  446. //Nonce check failed.
  447. wp_die( SwpmUtils::_( 'Error! Nonce security verification failed for Bulk Change Membership Level action. Clear cache and try again.' ) );
  448. }
  449. $errorMsg = '';
  450. $from_level_id = sanitize_text_field( $_REQUEST['swpm_bulk_change_level_from'] );
  451. $to_level_id = sanitize_text_field( $_REQUEST['swpm_bulk_change_level_to'] );
  452. if ( $from_level_id == 'please_select' || $to_level_id == 'please_select' ) {
  453. $errorMsg = SwpmUtils::_( 'Error! Please select a membership level first.' );
  454. }
  455. if ( empty( $errorMsg ) ) {//No validation errors so go ahead
  456. $member_records = SwpmMemberUtils::get_all_members_of_a_level( $from_level_id );
  457. if ( $member_records ) {
  458. foreach ( $member_records as $row ) {
  459. $member_id = $row->member_id;
  460. SwpmMemberUtils::update_membership_level( $member_id, $to_level_id );
  461. }
  462. }
  463. }
  464. $message = '';
  465. if ( ! empty( $errorMsg ) ) {
  466. $message = $errorMsg;
  467. } else {
  468. $message = SwpmUtils::_( 'Membership level change operation completed successfully.' );
  469. }
  470. echo '<div id="message" class="updated fade"><p><strong>';
  471. echo $message;
  472. echo '</strong></p></div>';
  473. }
  474. if ( isset( $_REQUEST['swpm_bulk_user_start_date_change_process'] ) ) {
  475. //Check nonce
  476. $swpm_bulk_start_date_nonce = filter_input( INPUT_POST, 'swpm_bulk_start_date_nonce' );
  477. if ( ! wp_verify_nonce( $swpm_bulk_start_date_nonce, 'swpm_bulk_start_date_nonce_action' ) ) {
  478. //Nonce check failed.
  479. wp_die( SwpmUtils::_( 'Error! Nonce security verification failed for Bulk Change Access Starts Date action. Clear cache and try again.' ) );
  480. }
  481. $errorMsg = '';
  482. $level_id = sanitize_text_field( $_REQUEST['swpm_bulk_user_start_date_change_level'] );
  483. $new_date = sanitize_text_field( $_REQUEST['swpm_bulk_user_start_date_change_date'] );
  484. if ( $level_id == 'please_select' ) {
  485. $errorMsg = SwpmUtils::_( 'Error! Please select a membership level first.' );
  486. }
  487. if ( empty( $errorMsg ) ) {//No validation errors so go ahead
  488. $member_records = SwpmMemberUtils::get_all_members_of_a_level( $level_id );
  489. if ( $member_records ) {
  490. foreach ( $member_records as $row ) {
  491. $member_id = $row->member_id;
  492. SwpmMemberUtils::update_access_starts_date( $member_id, $new_date );
  493. }
  494. }
  495. }
  496. $message = '';
  497. if ( ! empty( $errorMsg ) ) {
  498. $message = $errorMsg;
  499. } else {
  500. $message = SwpmUtils::_( 'Access starts date change operation successfully completed.' );
  501. }
  502. echo '<div id="message" class="updated fade"><p><strong>';
  503. echo $message;
  504. echo '</strong></p></div>';
  505. }
  506. ?>
  507. <div class="postbox">
  508. <h3 class="hndle"><label for="title"><?php SwpmUtils::e( 'Bulk Update Membership Level of Members' ); ?></label></h3>
  509. <div class="inside">
  510. <p>
  511. <?php SwpmUtils::e( 'You can manually change the membership level of any member by editing the record from the members menu. ' ); ?>
  512. <?php SwpmUtils::e( 'You can use the following option to bulk update the membership level of users who belong to the level you select below.' ); ?>
  513. </p>
  514. <form method="post" action="">
  515. <input type="hidden" name="swpm_bulk_change_level_nonce" value="<?php echo wp_create_nonce( 'swpm_bulk_change_level_nonce_action' ); ?>" />
  516. <table width="100%" border="0" cellspacing="0" cellpadding="6">
  517. <tr valign="top">
  518. <td width="25%" align="left">
  519. <strong><?php SwpmUtils::e( 'Membership Level: ' ); ?></strong>
  520. </td>
  521. <td align="left">
  522. <select name="swpm_bulk_change_level_from">
  523. <option value="please_select"><?php SwpmUtils::e( 'Select Current Level' ); ?></option>
  524. <?php echo SwpmUtils::membership_level_dropdown(); ?>
  525. </select>
  526. <p class="description"><?php SwpmUtils::e( 'Select the current membership level (the membership level of all members who are in this level will be updated).' ); ?></p>
  527. </td>
  528. </tr>
  529. <tr valign="top">
  530. <td width="25%" align="left">
  531. <strong><?php SwpmUtils::e( 'Level to Change to: ' ); ?></strong>
  532. </td>
  533. <td align="left">
  534. <select name="swpm_bulk_change_level_to">
  535. <option value="please_select"><?php SwpmUtils::e( 'Select Target Level' ); ?></option>
  536. <?php echo SwpmUtils::membership_level_dropdown(); ?>
  537. </select>
  538. <p class="description"><?php SwpmUtils::e( 'Select the new membership level.' ); ?></p>
  539. </td>
  540. </tr>
  541. <tr valign="top">
  542. <td width="25%" align="left">
  543. <input type="submit" class="button" name="swpm_bulk_change_level_process" value="<?php SwpmUtils::e( 'Bulk Change Membership Level' ); ?>" />
  544. </td>
  545. <td align="left"></td>
  546. </tr>
  547. </table>
  548. </form>
  549. </div></div>
  550. <div class="postbox">
  551. <h3 class="hndle"><label for="title"><?php SwpmUtils::e( 'Bulk Update Access Starts Date of Members' ); ?></label></h3>
  552. <div class="inside">
  553. <p>
  554. <?php SwpmUtils::e( 'The access starts date of a member is set to the day the user registers. This date value is used to calculate how long the member can access your content that are protected with a duration type protection in the membership level. ' ); ?>
  555. <?php SwpmUtils::e( 'You can manually set a specific access starts date value of all members who belong to a particular level using the following option.' ); ?>
  556. </p>
  557. <form method="post" action="">
  558. <input type="hidden" name="swpm_bulk_start_date_nonce" value="<?php echo wp_create_nonce( 'swpm_bulk_start_date_nonce_action' ); ?>" />
  559. <table width="100%" border="0" cellspacing="0" cellpadding="6">
  560. <tr valign="top">
  561. <td width="25%" align="left">
  562. <strong><?php SwpmUtils::e( 'Membership Level: ' ); ?></strong>
  563. </td><td align="left">
  564. <select name="swpm_bulk_user_start_date_change_level">
  565. <option value="please_select"><?php SwpmUtils::e( 'Select Level' ); ?></option>
  566. <?php echo SwpmUtils::membership_level_dropdown(); ?>
  567. </select>
  568. <p class="description"><?php SwpmUtils::e( 'Select the Membership level (the access start date of all members who are in this level will be updated).' ); ?></p>
  569. </td>
  570. </tr>
  571. <tr valign="top">
  572. <td width="25%" align="left">
  573. <strong>Access Starts Date: </strong>
  574. </td><td align="left">
  575. <input name="swpm_bulk_user_start_date_change_date" id="swpm_bulk_user_start_date_change_date" class="swpm-select-date" type="text" size="20" value="<?php echo ( date( 'Y-m-d' ) ); ?>" />
  576. <p class="description"><?php SwpmUtils::e( 'Specify the access starts date value.' ); ?></p>
  577. </td>
  578. </tr>
  579. <tr valign="top">
  580. <td width="25%" align="left">
  581. <input type="submit" class="button" name="swpm_bulk_user_start_date_change_process" value="<?php SwpmUtils::e( 'Bulk Change Access Starts Date' ); ?>" />
  582. </td>
  583. <td align="left"></td>
  584. </tr>
  585. </table>
  586. </form>
  587. </div></div>
  588. <script>
  589. jQuery(document).ready(function ($) {
  590. $('#swpm_bulk_user_start_date_change_date').datepicker({dateFormat: 'yy-mm-dd', changeMonth: true, changeYear: true, yearRange: "-100:+100"});
  591. });
  592. </script>
  593. <?php
  594. echo '</div></div>'; //<!-- end of #poststuff #post-body -->
  595. }
  596. function show_all_members() {
  597. ob_start();
  598. $status = filter_input( INPUT_GET, 'status' );
  599. include_once SIMPLE_WP_MEMBERSHIP_PATH . 'views/admin_members_list.php';
  600. $output = ob_get_clean();
  601. return $output;
  602. }
  603. function handle_main_members_admin_menu() {
  604. do_action( 'swpm_members_menu_start' );
  605. //Check current_user_can() or die.
  606. SwpmMiscUtils::check_user_permission_and_is_admin( 'Main Members Admin Menu' );
  607. $action = filter_input( INPUT_GET, 'member_action' );
  608. $action = empty( $action ) ? filter_input( INPUT_POST, 'action' ) : $action;
  609. $selected = $action;
  610. ?>
  611. <div class="wrap swpm-admin-menu-wrap"><!-- start wrap -->
  612. <h1><?php echo SwpmUtils::_( 'Simple WP Membership::Members' ); ?><!-- page title -->
  613. <a href="admin.php?page=simple_wp_membership&member_action=add" class="add-new-h2"><?php echo SwpmUtils::_( 'Add New' ); ?></a>
  614. </h1>
  615. <h2 class="nav-tab-wrapper swpm-members-nav-tab-wrapper"><!-- start nav menu tabs -->
  616. <a class="nav-tab <?php echo ( $selected == '' ) ? 'nav-tab-active' : ''; ?>" href="admin.php?page=simple_wp_membership"><?php echo SwpmUtils::_( 'Members' ); ?></a>
  617. <a class="nav-tab <?php echo ( $selected == 'add' ) ? 'nav-tab-active' : ''; ?>" href="admin.php?page=simple_wp_membership&member_action=add"><?php echo SwpmUtils::_( 'Add Member' ); ?></a>
  618. <a class="nav-tab <?php echo ( $selected == 'bulk' ) ? 'nav-tab-active' : ''; ?>" href="admin.php?page=simple_wp_membership&member_action=bulk"><?php echo SwpmUtils::_( 'Bulk Operation' ); ?></a>
  619. <?php
  620. if ( $selected == 'edit' ) {//Only show the "edit member" tab when a member profile is being edited from the admin side.
  621. echo '<a class="nav-tab nav-tab-active" href="#">Edit Member</a>';
  622. }
  623. //Trigger hooks that allows an extension to add extra nav tabs in the members menu.
  624. do_action( 'swpm_members_menu_nav_tabs', $selected );
  625. $menu_tabs = apply_filters( 'swpm_members_additional_menu_tabs_array', array() );
  626. foreach ( $menu_tabs as $member_action => $title ) {
  627. ?>
  628. <a class="nav-tab <?php echo ( $selected == $member_action ) ? 'nav-tab-active' : ''; ?>" href="admin.php?page=simple_wp_membership&member_action=<?php echo $member_action; ?>" ><?php SwpmUtils::e( $title ); ?></a>
  629. <?php
  630. }
  631. ?>
  632. </h2><!-- end nav menu tabs -->
  633. <?php
  634. do_action( 'swpm_members_menu_after_nav_tabs' );
  635. //Trigger hook so anyone listening for this particular action can handle the output.
  636. do_action( 'swpm_members_menu_body_' . $action );
  637. //Allows an addon to completely override the body section of the members admin menu for a given action.
  638. $output = apply_filters( 'swpm_members_menu_body_override', '', $action );
  639. if ( ! empty( $output ) ) {
  640. //An addon has overriden the body of this page for the given action. So no need to do anything in core.
  641. echo $output;
  642. echo '</div>'; //<!-- end of wrap -->
  643. return;
  644. }
  645. //Switch case for the various different actions handled by the core plugin.
  646. switch ( $action ) {
  647. case 'members_list':
  648. //Show the members listing
  649. echo $this->show_all_members();
  650. break;
  651. case 'add':
  652. //Process member profile add
  653. $this->process_form_request();
  654. break;
  655. case 'edit':
  656. //Process member profile edit
  657. $this->process_form_request();
  658. break;
  659. case 'bulk':
  660. //Handle the bulk operation menu
  661. $this->bulk_operation_menu();
  662. break;
  663. default:
  664. //Show the members listing page by default.
  665. echo $this->show_all_members();
  666. break;
  667. }
  668. echo '</div>'; //<!-- end of wrap -->
  669. }
  670. }