class.swpm-utils-member.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * SwpmMemberUtils
  4. * All the utility functions related to member records should be added to this class
  5. */
  6. class SwpmMemberUtils {
  7. public static function create_swpm_member_entry_from_array_data( $fields ) {
  8. global $wpdb;
  9. $res = $wpdb->insert( $wpdb->prefix . 'swpm_members_tbl', $fields );
  10. if ( ! $res ) {
  11. //DB error occurred
  12. $error_msg = 'create_swpm_member_entry_from_array_data() - DB error occurred: ' . json_encode( $wpdb->last_result );
  13. SwpmLog::log_simple_debug( $error_msg, false );
  14. }
  15. $member_id = $wpdb->insert_id;
  16. SwpmLog::log_simple_debug( 'create_swpm_member_entry_from_array_data() - SWPM member entry created successfully. Member ID: ' . $member_id, true );
  17. return $member_id;
  18. }
  19. public static function is_member_logged_in() {
  20. $auth = SwpmAuth::get_instance();
  21. if ( $auth->is_logged_in() ) {
  22. return true;
  23. } else {
  24. return false;
  25. }
  26. }
  27. public static function get_logged_in_members_id() {
  28. $auth = SwpmAuth::get_instance();
  29. if ( ! $auth->is_logged_in() ) {
  30. return SwpmUtils::_( 'User is not logged in.' );
  31. }
  32. return $auth->get( 'member_id' );
  33. }
  34. public static function get_logged_in_members_username() {
  35. $auth = SwpmAuth::get_instance();
  36. if ( ! $auth->is_logged_in() ) {
  37. return SwpmUtils::_( 'User is not logged in.' );
  38. }
  39. return $auth->get( 'user_name' );
  40. }
  41. public static function get_logged_in_members_level() {
  42. $auth = SwpmAuth::get_instance();
  43. if ( ! $auth->is_logged_in() ) {
  44. return SwpmUtils::_( 'User is not logged in.' );
  45. }
  46. return $auth->get( 'membership_level' );
  47. }
  48. public static function get_logged_in_members_level_name() {
  49. $auth = SwpmAuth::get_instance();
  50. if ( $auth->is_logged_in() ) {
  51. return $auth->get( 'alias' );
  52. }
  53. return SwpmUtils::_( 'User is not logged in.' );
  54. }
  55. public static function get_logged_in_members_email() {
  56. $auth = SwpmAuth::get_instance();
  57. if ( ! $auth->is_logged_in() ) {
  58. return SwpmUtils::_( 'User is not logged in.' );
  59. }
  60. return $auth->get( 'email' );
  61. }
  62. public static function get_member_field_by_id( $id, $field, $default = '' ) {
  63. global $wpdb;
  64. $query = 'SELECT * FROM ' . $wpdb->prefix . 'swpm_members_tbl WHERE member_id = %d';
  65. $userData = $wpdb->get_row( $wpdb->prepare( $query, $id ) );
  66. if ( isset( $userData->$field ) ) {
  67. return $userData->$field;
  68. }
  69. return apply_filters( 'swpm_get_member_field_by_id', $default, $id, $field );
  70. }
  71. public static function get_formatted_expiry_date_by_user_id( $swpm_id ) {
  72. $expiry_timestamp = self::get_expiry_date_timestamp_by_user_id( $swpm_id );
  73. if ( $expiry_timestamp == PHP_INT_MAX ) {
  74. //No Expiry Setting
  75. $formatted_expiry_date = SwpmUtils::_( 'No Expiry' );
  76. } else {
  77. $expiry_date = date( 'Y-m-d', $expiry_timestamp );
  78. $formatted_expiry_date = SwpmUtils::get_formatted_date_according_to_wp_settings( $expiry_date );
  79. }
  80. return $formatted_expiry_date;
  81. }
  82. public static function get_expiry_date_timestamp_by_user_id( $swpm_id ) {
  83. $swpm_user = self::get_user_by_id( $swpm_id );
  84. $expiry_timestamp = SwpmUtils::get_expiration_timestamp( $swpm_user );
  85. return $expiry_timestamp;
  86. }
  87. public static function get_user_by_id( $swpm_id ) {
  88. //Retrieves the SWPM user record for the given member ID
  89. global $wpdb;
  90. $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}swpm_members_tbl WHERE member_id = %d", $swpm_id );
  91. $result = $wpdb->get_row( $query );
  92. return $result;
  93. }
  94. public static function get_user_by_user_name( $swpm_user_name ) {
  95. //Retrieves the SWPM user record for the given member username
  96. global $wpdb;
  97. $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}swpm_members_tbl WHERE user_name = %s", $swpm_user_name );
  98. $result = $wpdb->get_row( $query );
  99. return $result;
  100. }
  101. public static function get_user_by_email( $swpm_email ) {
  102. //Retrieves the SWPM user record for the given member email address
  103. global $wpdb;
  104. $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}swpm_members_tbl WHERE email = %s", $swpm_email );
  105. $result = $wpdb->get_row( $query );
  106. return $result;
  107. }
  108. public static function get_user_by_subsriber_id( $subsc_id ) {
  109. //Retrieves the SWPM user record for the given member ID
  110. global $wpdb;
  111. $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}swpm_members_tbl WHERE subscr_id = %s", $subsc_id );
  112. $result = $wpdb->get_row( $query );
  113. return $result;
  114. }
  115. public static function get_wp_user_from_swpm_user_id( $swpm_id ) {
  116. //Retrieves the WP user record for the given SWPM member ID.
  117. $swpm_user_row = self::get_user_by_id( $swpm_id );
  118. $username = $swpm_user_row->user_name;
  119. $wp_user = get_user_by( 'login', $username );
  120. return $wp_user;
  121. }
  122. public static function get_all_members_of_a_level( $level_id ) {
  123. //Retrieves all the SWPM user records for the given membership level
  124. global $wpdb;
  125. $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}swpm_members_tbl WHERE membership_level = %s", $level_id );
  126. $result = $wpdb->get_results( $query );
  127. return $result;
  128. }
  129. /*
  130. * Use this function to update or set membership level of a member easily.
  131. */
  132. public static function update_membership_level( $member_id, $target_membership_level ) {
  133. global $wpdb;
  134. $members_table_name = $wpdb->prefix . 'swpm_members_tbl';
  135. $query = $wpdb->prepare( "UPDATE $members_table_name SET membership_level=%s WHERE member_id=%s", $target_membership_level, $member_id );
  136. $resultset = $wpdb->query( $query );
  137. }
  138. /*
  139. * Use this function to update or set account status of a member easily.
  140. */
  141. public static function update_account_state( $member_id, $new_status = 'active' ) {
  142. global $wpdb;
  143. $members_table_name = $wpdb->prefix . 'swpm_members_tbl';
  144. SwpmLog::log_simple_debug( 'Updating the account status value of member (' . $member_id . ') to: ' . $new_status, true );
  145. $query = $wpdb->prepare( "UPDATE $members_table_name SET account_state=%s WHERE member_id=%s", $new_status, $member_id );
  146. $resultset = $wpdb->query( $query );
  147. }
  148. /*
  149. * Use this function to update or set access starts date of a member easily.
  150. */
  151. public static function update_access_starts_date( $member_id, $new_date ) {
  152. global $wpdb;
  153. $members_table_name = $wpdb->prefix . 'swpm_members_tbl';
  154. $query = $wpdb->prepare( "UPDATE $members_table_name SET subscription_starts=%s WHERE member_id=%s", $new_date, $member_id );
  155. $resultset = $wpdb->query( $query );
  156. }
  157. /*
  158. * Calculates the Access Starts date value considering the level and current expiry. Useful for after payment member profile update.
  159. */
  160. public static function calculate_access_start_date_for_account_update( $args ) {
  161. $swpm_id = $args['swpm_id'];
  162. $membership_level = $args['membership_level'];
  163. $old_membership_level = $args['old_membership_level'];
  164. $subscription_starts = SwpmUtils::get_current_date_in_wp_zone();//( date( 'Y-m-d' ) );
  165. if ( $membership_level == $old_membership_level ) {
  166. //Payment for the same membership level (renewal).
  167. //Algorithm - ONLY set the $subscription_starts date to current expiry date if the current expiry date is in the future.
  168. //Otherwise set $subscription_starts to TODAY.
  169. $expiry_timestamp = self::get_expiry_date_timestamp_by_user_id( $swpm_id );
  170. if ( $expiry_timestamp > time() ) {
  171. //Account is not expired. Expiry date is in the future.
  172. $level_row = SwpmUtils::get_membership_level_row_by_id( $membership_level );
  173. $subs_duration_type = $level_row->subscription_duration_type;
  174. if ( $subs_duration_type == SwpmMembershipLevel::NO_EXPIRY ) {
  175. //No expiry type level.
  176. //Use todays date for $subscription_starts date parameter.
  177. } elseif ( $subs_duration_type == SwpmMembershipLevel::FIXED_DATE ) {
  178. //Fixed date expiry level.
  179. //Use todays date for $subscription_starts date parameter.
  180. } else {
  181. //Duration expiry level.
  182. //Set the $subscription_starts date to the current expiry date so the renewal time starts from then.
  183. $subscription_starts = date( 'Y-m-d', $expiry_timestamp );
  184. }
  185. } else {
  186. //Account is already expired.
  187. //Use todays date for $subscription_starts date parameter.
  188. }
  189. } else {
  190. //Payment for a NEW membership level (upgrade).
  191. //Use todays date for $subscription_starts date parameter.
  192. }
  193. return $subscription_starts;
  194. }
  195. public static function is_valid_user_name( $user_name ) {
  196. return preg_match( '/^[a-zA-Z0-9.\-_*@]+$/', $user_name ) == 1;
  197. }
  198. public static function check_and_die_if_email_belongs_to_admin_user( $email_to_check ){
  199. //Check if the email belongs to an existing wp user account.
  200. $wp_user_id = email_exists( $email_to_check );
  201. if ( $wp_user_id ) {
  202. //A wp user account exist with this email.
  203. //Check if the user has admin role.
  204. $admin_user = SwpmMemberUtils::wp_user_has_admin_role( $wp_user_id );
  205. if ( $admin_user ) {
  206. //This email belongs to an admin user. Cannot use/register using an admin user's email from front-end. Show error message then exit.
  207. $error_msg = '<p>This email address (' . $email_to_check . ') belongs to an admin user. This email cannot be used to register a new account on this site for security reasons. Contact site admin.</p>';
  208. $error_msg .= '<p>For testing purpose, you can create another user account that is completely separate from the admin user account of this site.</p>';
  209. wp_die( $error_msg );
  210. }
  211. }
  212. }
  213. public static function check_and_die_if_username_belongs_to_admin_user( $username_to_check ){
  214. //Check if the username belongs to an existing wp user account.
  215. $wp_user_id = username_exists( $username_to_check );
  216. if ( $wp_user_id ) {
  217. //A wp user account exists with this username.
  218. //Check if the user has admin role.
  219. $admin_user = SwpmMemberUtils::wp_user_has_admin_role( $wp_user_id );
  220. if ( $admin_user ) {
  221. //This Username belongs to an admin user. Cannot use/register using an existing admin user's username from front-end. Show error message then exit.
  222. $error_msg = '<p>This username (' . $username_to_check . ') belongs to an admin user. It cannot be used to register a new account on this site for security reasons. Contact site admin.</p>';
  223. $error_msg .= '<p>For testing purpose, you can create another user account that is completely separate from the admin user account of this site.</p>';
  224. wp_die( $error_msg );
  225. }
  226. }
  227. }
  228. public static function wp_user_has_admin_role( $wp_user_id ) {
  229. $caps = get_user_meta( $wp_user_id, 'wp_capabilities', true );
  230. if ( is_array( $caps ) && in_array( 'administrator', array_keys( (array) $caps ) ) ) {
  231. //This wp user has "administrator" role.
  232. return true;
  233. }
  234. return false;
  235. }
  236. public static function update_wp_user_role_with_level_id( $wp_user_id, $level_id ) {
  237. $level_row = SwpmUtils::get_membership_level_row_by_id( $level_id );
  238. $user_role = $level_row->role;
  239. self::update_wp_user_role( $wp_user_id, $user_role );
  240. }
  241. public static function update_wp_user_role( $wp_user_id, $role ) {
  242. if ( SwpmUtils::is_multisite_install() ) {//MS install
  243. return; //TODO - don't do this for MS install
  244. }
  245. $admin_user = self::wp_user_has_admin_role( $wp_user_id );
  246. if ( $admin_user ) {
  247. SwpmLog::log_simple_debug( 'This user has admin role. No role modification will be done.', true );
  248. return;
  249. }
  250. //wp_update_user() function will trigger the 'set_user_role' hook.
  251. wp_update_user(
  252. array(
  253. 'ID' => $wp_user_id,
  254. 'role' => $role,
  255. )
  256. );
  257. SwpmLog::log_simple_debug( 'User role updated.', true );
  258. }
  259. }