class.swpm-init-time-tasks.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. class SwpmInitTimeTasks {
  3. public function __construct() {
  4. }
  5. public function do_init_tasks() {
  6. //Set up localisation. First loaded ones will override strings present in later loaded file.
  7. //Allows users to have a customized language in a different folder.
  8. $locale = apply_filters( 'plugin_locale', get_locale(), 'simple-membership' );
  9. load_textdomain( 'simple-membership', WP_LANG_DIR . "/simple-membership-$locale.mo" );
  10. load_plugin_textdomain( 'simple-membership', false, SIMPLE_WP_MEMBERSHIP_DIRNAME . '/languages/' );
  11. if ( ! isset( $_COOKIE['swpm_session'] ) ) { // give a unique ID to current session.
  12. $uid = md5( microtime() );
  13. $_COOKIE['swpm_session'] = $uid; // fake it for current session/
  14. if ( ! headers_sent() ) {
  15. setcookie( 'swpm_session', $uid, 0, '/' );
  16. }
  17. }
  18. //Crete the custom post types
  19. $this->create_post_type();
  20. //Do frontend-only init time tasks
  21. if ( ! is_admin() ) {
  22. SwpmAuth::get_instance();
  23. $this->check_and_handle_auto_login();
  24. $this->verify_and_delete_account();
  25. $swpm_logout = filter_input( INPUT_GET, 'swpm-logout' );
  26. if ( ! empty( $swpm_logout ) ) {
  27. SwpmAuth::get_instance()->logout();
  28. $redirect_url = apply_filters( 'swpm_after_logout_redirect_url', SIMPLE_WP_MEMBERSHIP_SITE_HOME_URL );
  29. wp_redirect( trailingslashit( $redirect_url ) );
  30. exit( 0 );
  31. }
  32. $this->process_password_reset();
  33. $this->register_member();
  34. $this->check_and_do_email_activation();
  35. $this->edit_profile();
  36. SwpmCommentFormRelated::check_and_restrict_comment_posting_to_members();
  37. } else {
  38. //Do admin side init time tasks
  39. if ( current_user_can( SWPM_MANAGEMENT_PERMISSION ) ) {
  40. //Admin dashboard side stuff
  41. $this->admin_init();
  42. }
  43. }
  44. }
  45. public function admin_init() {
  46. $createswpmuser = filter_input( INPUT_POST, 'createswpmuser' );
  47. if ( ! empty( $createswpmuser ) ) {
  48. SwpmAdminRegistration::get_instance()->register_admin_end();
  49. }
  50. $editswpmuser = filter_input( INPUT_POST, 'editswpmuser' );
  51. if ( ! empty( $editswpmuser ) ) {
  52. $id = filter_input( INPUT_GET, 'member_id', FILTER_VALIDATE_INT );
  53. SwpmAdminRegistration::get_instance()->edit_admin_end( $id );
  54. }
  55. $createswpmlevel = filter_input( INPUT_POST, 'createswpmlevel' );
  56. if ( ! empty( $createswpmlevel ) ) {
  57. SwpmMembershipLevel::get_instance()->create_level();
  58. }
  59. $editswpmlevel = filter_input( INPUT_POST, 'editswpmlevel' );
  60. if ( ! empty( $editswpmlevel ) ) {
  61. $id = filter_input( INPUT_GET, 'id' );
  62. SwpmMembershipLevel::get_instance()->edit_level( $id );
  63. }
  64. $update_category_list = filter_input( INPUT_POST, 'update_category_list' );
  65. if ( ! empty( $update_category_list ) ) {
  66. include_once 'class.swpm-category-list.php';
  67. SwpmCategoryList::update_category_list();
  68. }
  69. $update_post_list = filter_input( INPUT_POST, 'update_post_list' );
  70. if ( ! empty( $update_post_list ) ) {
  71. include_once 'class.swpm-post-list.php';
  72. SwpmPostList::update_post_list();
  73. }
  74. }
  75. public function create_post_type() {
  76. //The payment button data for membership levels will be stored using this CPT
  77. register_post_type(
  78. 'swpm_payment_button',
  79. array(
  80. 'public' => false,
  81. 'publicly_queryable' => false,
  82. 'show_ui' => false,
  83. 'query_var' => false,
  84. 'rewrite' => false,
  85. 'capability_type' => 'page',
  86. 'has_archive' => false,
  87. 'hierarchical' => false,
  88. 'supports' => array( 'title', 'editor' ),
  89. )
  90. );
  91. //Transactions will be stored using this CPT in parallel with swpm_payments_tbl DB table
  92. $args = array(
  93. 'supports' => array( '' ),
  94. 'hierarchical' => false,
  95. 'public' => false,
  96. 'show_ui' => false,
  97. 'can_export' => false,
  98. 'has_archive' => false,
  99. 'exclude_from_search' => true,
  100. 'publicly_queryable' => false,
  101. 'capability_type' => 'post',
  102. );
  103. register_post_type( 'swpm_transactions', $args );
  104. }
  105. private function verify_and_delete_account() {
  106. include_once SIMPLE_WP_MEMBERSHIP_PATH . 'classes/class.swpm-members.php';
  107. $delete_account = filter_input( INPUT_GET, 'swpm_delete_account' );
  108. if ( empty( $delete_account ) ) {
  109. return;
  110. }
  111. $password = filter_input( INPUT_POST, 'account_delete_confirm_pass', FILTER_UNSAFE_RAW );
  112. $auth = SwpmAuth::get_instance();
  113. if ( ! $auth->is_logged_in() ) {
  114. return;
  115. }
  116. if ( empty( $password ) ) {
  117. SwpmUtils::account_delete_confirmation_ui();
  118. }
  119. $nonce_field = filter_input( INPUT_POST, 'account_delete_confirm_nonce' );
  120. if ( empty( $nonce_field ) || ! wp_verify_nonce( $nonce_field, 'swpm_account_delete_confirm' ) ) {
  121. SwpmUtils::account_delete_confirmation_ui( SwpmUtils::_( 'Sorry, Nonce verification failed.' ) );
  122. }
  123. if ( $auth->match_password( $password ) ) {
  124. $auth->delete();
  125. wp_safe_redirect( get_home_url() );
  126. exit( 0 );
  127. } else {
  128. SwpmUtils::account_delete_confirmation_ui( SwpmUtils::_( "Sorry, Password didn't match." ) );
  129. }
  130. }
  131. public function process_password_reset() {
  132. $message = '';
  133. $swpm_reset = filter_input( INPUT_POST, 'swpm-reset' );
  134. $swpm_reset_email = filter_input( INPUT_POST, 'swpm_reset_email', FILTER_UNSAFE_RAW );
  135. if ( ! empty( $swpm_reset ) ) {
  136. SwpmFrontRegistration::get_instance()->reset_password( $swpm_reset_email );
  137. }
  138. }
  139. private function register_member() {
  140. $registration = filter_input( INPUT_POST, 'swpm_registration_submit' );
  141. if ( ! empty( $registration ) ) {
  142. SwpmFrontRegistration::get_instance()->register_front_end();
  143. }
  144. }
  145. private function check_and_do_email_activation() {
  146. $email_activation = filter_input( INPUT_GET, 'swpm_email_activation', FILTER_SANITIZE_NUMBER_INT );
  147. if ( ! empty( $email_activation ) ) {
  148. SwpmFrontRegistration::get_instance()->email_activation();
  149. }
  150. //also check activation email resend request
  151. $email_activation_resend = filter_input( INPUT_GET, 'swpm_resend_activation_email', FILTER_SANITIZE_NUMBER_INT );
  152. if ( ! empty( $email_activation_resend ) ) {
  153. SwpmFrontRegistration::get_instance()->resend_activation_email();
  154. }
  155. }
  156. private function edit_profile() {
  157. $swpm_editprofile_submit = filter_input( INPUT_POST, 'swpm_editprofile_submit' );
  158. if ( ! empty( $swpm_editprofile_submit ) ) {
  159. SwpmFrontRegistration::get_instance()->edit_profile_front_end();
  160. //TODO - allow an option to do a redirect if successful edit profile form submission?
  161. }
  162. }
  163. public function check_and_handle_auto_login() {
  164. if ( isset( $_REQUEST['swpm_auto_login'] ) && $_REQUEST['swpm_auto_login'] == '1' ) {
  165. //Handle the auto login
  166. SwpmLog::log_simple_debug( 'Handling auto login request...', true );
  167. $enable_auto_login = SwpmSettings::get_instance()->get_value( 'auto-login-after-rego' );
  168. if ( empty( $enable_auto_login ) ) {
  169. SwpmLog::log_simple_debug( 'Auto login after registration feature is disabled in settings.', true );
  170. return;
  171. }
  172. //Check auto login nonce value
  173. $auto_login_nonce = isset( $_REQUEST['swpm_auto_login_nonce'] ) ? $_REQUEST['swpm_auto_login_nonce'] : '';
  174. if ( ! wp_verify_nonce( $auto_login_nonce, 'swpm-auto-login-nonce' ) ) {
  175. SwpmLog::log_simple_debug( 'Error! Auto login nonce verification check failed!', false );
  176. wp_die( 'Auto login nonce verification check failed!' );
  177. }
  178. //Perform the login
  179. $auth = SwpmAuth::get_instance();
  180. $user = apply_filters( 'swpm_user_name', filter_input( INPUT_GET, 'swpm_user_name' ) );
  181. $user = sanitize_user( $user );
  182. $encoded_pass = filter_input( INPUT_GET, 'swpm_encoded_pw' );
  183. $pass = base64_decode( $encoded_pass );
  184. $auth->login( $user, $pass );
  185. SwpmLog::log_simple_debug( 'Auto login request completed for: ' . $user, true );
  186. }
  187. }
  188. }