Guest User

register-functions

a guest
Apr 27th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 20.59 KB | None | 0 0
  1. <?php
  2. /***
  3.  * Class ThimEdumaRegisterFunction
  4.  *
  5.  * @since 4.2.4
  6.  * @version 1.0.2
  7.  */
  8.  
  9. class ThimEdumaRegisterFunction {
  10.     /**
  11.      * @var ThimEdumaRegisterFunction
  12.      */
  13.     protected static $_instance;
  14.  
  15.     /**
  16.      * Construct
  17.      */
  18.     protected function __construct() {
  19.         $this->addHook();
  20.     }
  21.  
  22.     protected function addHook() {
  23.         /*** Login user */
  24.         // add_filter( 'login_url', array( $this, 'thim_remove_default_login_url' ), 1000 );
  25.  
  26.         // redirect after login success
  27.         add_filter( 'login_redirect', array( $this, 'login_success_redirect' ), 99999, 3 );
  28.  
  29.         // redirect if login false
  30.         add_filter( 'authenticate', array( $this, 'login_authenticate' ), 99999, 2 );
  31.         /*** End login user */
  32.  
  33.         /*** Register user */
  34.         // Check extra register if set auto login when register
  35.         add_action( 'register_post', array( $this, 'check_extra_register_fields' ), 10, 3 );
  36.  
  37.         // Update password if set auto login when register
  38.         add_action( 'user_register', array( $this, 'register_update_pass_and_login' ), 99999 );
  39.  
  40.         // redirect if register false
  41.         add_action( 'registration_errors', array( $this, 'register_failed' ), 99999, 3 );
  42.  
  43.         // redirect if register success if not set auto login when register
  44.         add_action( 'register_new_user', array( $this, 'register_verify_mail_success_redirect' ), 999999 );
  45.  
  46.         add_filter(
  47.             'wp_new_user_notification_email',
  48.             array( $this, 'message_set_password_when_not_auto_login' ),
  49.             999999,
  50.             2
  51.         );
  52.         /*** End register user */
  53.  
  54.         /*** Reset password */
  55.         add_action( 'lostpassword_post', array( $this, 'check_field_to_reset_password' ), 99999, 1 );
  56.         add_filter( 'login_form_middle', array( $this, 'add_lost_password_link' ), 99999 );
  57.         add_filter( 'login_form_rp', array( $this, 'validate_password_reset' ), 99999 );
  58.         add_filter( 'login_form_resetpass', array( $this, 'validate_password_reset' ), 99999 );
  59.  
  60.         /*** Override url redirect after lp checkout(default) success */
  61.         add_filter(
  62.             'learn-press/add-to-cart-redirect',
  63.             array( $this, 'url_redirect_after_checkout_lp_course_success' ),
  64.             10,
  65.             4
  66.         );
  67.  
  68.         /*** Override message send mail with case auto-login */
  69.         add_filter( 'password_change_email', array( $this, 'message_when_user_register_auto_login' ), 999999, 1 );
  70.     }
  71.  
  72.     /**
  73.      * Check login has errors
  74.      *
  75.      * @param null|WP_User|WP_Error $user
  76.      * @param string $username
  77.      *
  78.      * @return mixed
  79.      */
  80.     public function login_authenticate( $user, $username ) {
  81.         if ( ! $username || wp_doing_ajax() || ! isset( $_POST['eduma_login_user'] ) ) {
  82.             return $user;
  83.         }
  84.  
  85.         $error_code = $user->get_error_code();
  86.         if ( $user instanceof WP_Error && $error_code ) {
  87.             $error_msg = '';
  88.  
  89.             if ( $error_code == 'incorrect_password' ) {
  90.                 $error_msg = __( 'The password is incorrect', 'eduma' );
  91.             } else {
  92.                 $error_msg = str_replace( array( '<strong>', '</strong>' ), '', $user->errors[ $error_code ][0] );
  93.             }
  94.  
  95.             $url = add_query_arg(
  96.                 array(
  97.                     'result'         => 'failed',
  98.                     'thim_login_msg' => rawurlencode( $error_msg ),
  99.                 ),
  100.                 thim_get_login_page_url()
  101.             );
  102.             wp_safe_redirect( $url );
  103.             die;
  104.         }
  105.  
  106.         return $user;
  107.     }
  108.  
  109.     /**
  110.      * If login success change redirect url
  111.      *
  112.      * @priority
  113.      * 1. Redirect to checkout if click buy course before
  114.      * 2. Redirect to option login after success (on Customize \ General \ Utilities)
  115.      * 3. Redirect to $_REQUEST['redirect_to']
  116.      * 4. Redirect to current url
  117.      *
  118.      * @param string $redirect_to
  119.      * @param string $requested_redirect_to
  120.      * @param WP_User|WP_Error $user
  121.      *
  122.      * @return mixed|string|void
  123.      */
  124.     public function login_success_redirect( $redirect_to, $requested_redirect_to, $user ) {
  125.         if ( ! isset( $_POST['eduma_login_user'] ) || $user instanceof WP_Error ) {
  126.             return $redirect_to;
  127.         }
  128.  
  129.         if ( is_wp_error( $user ) ) {
  130.             return $redirect_to;
  131.         }
  132.  
  133.         // Set current user.
  134.         wp_set_current_user( $user->ID );
  135.         wp_set_auth_cookie( $user->ID );
  136.  
  137.         /*** purchase course */
  138.         $this->buy_course();
  139.  
  140.         /*** get option redirect after login success */
  141.         $login_redirect_option = get_theme_mod( 'thim_login_redirect', false );
  142.  
  143.         if ( ! empty( $login_redirect_option ) ) {
  144.             $redirect_to = $login_redirect_option;
  145.         } elseif ( ! empty( $_REQUEST['redirect_to'] ) ) {
  146.             $redirect_to = $_REQUEST['redirect_to'];
  147.         } else {
  148.             $redirect_to = thim_eduma_get_current_url();
  149.         }
  150.  
  151.         return $redirect_to;
  152.     }
  153.  
  154.     /**
  155.      * @param string $user_login
  156.      * @param string $email
  157.      * @param WP_Error $errors
  158.      */
  159.     public function check_extra_register_fields($email, $errors ) {
  160.         if ( wp_doing_ajax() || ! isset( $_POST['eduma_register_user'] )
  161.             || ! isset( $_POST['password'] )
  162.             || ! get_theme_mod( 'thim_auto_login', true ) ) {
  163.             return;
  164.         }
  165.  
  166.         if ( ! isset( $_POST['repeat_password'] )
  167.             || $_POST['password'] !== $_POST['repeat_password'] ) {
  168.             $errors->add( 'passwords_not_matched', __( 'Passwords must match', 'eduma' ) );
  169.         }
  170.     }
  171.  
  172.     /**
  173.      * Update password
  174.      *
  175.      * @priority
  176.      * 1. Redirect to check out if click buy course before
  177.      * 2. Redirect to option Register after success (on Customize \ General \ Utilities)
  178.      * 3. Redirect to $_REQUEST['redirect_to']
  179.      * 4. Redirect to current url
  180.      *
  181.      * @param int $user_id
  182.      *
  183.      * @return bool|void
  184.      */
  185.     public function register_update_pass_and_login( $user_id ) {
  186.         if ( wp_doing_ajax() || ! isset( $_POST['eduma_register_user'] )
  187.             || ! isset( $_POST['password'] )
  188.             || ! get_theme_mod( 'thim_auto_login', true ) ) {
  189.             return;
  190.         }
  191.  
  192.         $pw = sanitize_text_field( $_POST['password'] );
  193.  
  194.         $user_data              = array();
  195.         $user_data['ID']        = $user_id;
  196.         $user_data['user_pass'] = $pw;
  197.  
  198.         $user_data = apply_filters( 'lp_user_data_register', $user_data, $user_id );
  199.  
  200.         $new_user_id = wp_update_user( $user_data );
  201.  
  202.         if ( $new_user_id instanceof WP_Error ) {
  203.             return;
  204.         }
  205.  
  206.         // Fix error become a teacher not working in register form
  207.         if ( function_exists( 'LP' ) ) {
  208.             $newuserdata = get_userdata( $new_user_id );
  209.             //Check become an teacher option in register form
  210.             if ( LP_Settings::instance()->get( 'instructor_registration' ) == 'yes' && isset( $_POST['become_teacher'] ) ) {
  211.                 update_user_meta( $new_user_id, '_requested_become_teacher', 'yes' );
  212.                 // Send email require become to teacher of user to Admin mail.
  213.                 do_action(
  214.                     'learn-press/become-a-teacher-sent',
  215.                     array(
  216.                         'bat_email'   => $newuserdata->user_email,
  217.                         'bat_message' => apply_filters( 'learnpress_become_instructor_message', esc_html__( 'I need become a instructor', 'learnpress' ) ),
  218.                     )
  219.                 );
  220.                 learn_press_add_message( __( 'Your request become a instructor has been sent. We will get back to you soon!', 'learnpress' ), 'success' );
  221.             }
  222.         }
  223.  
  224.         // Login after registered
  225.         if ( ! is_admin() ) {
  226.             wp_set_current_user( $user_id );
  227.             wp_set_auth_cookie( $user_id );
  228.             wp_new_user_notification(
  229.                 $user_id,
  230.                 null,
  231.                 'admin'
  232.             ); // new user registration notification only send to admin
  233.  
  234.             if ( isset( $_POST['level'] ) && $_POST['level'] &&
  235.                 isset( $_POST['token'] ) && $_POST['token'] &&
  236.                 isset( $_POST['gateway'] ) && $_POST['gateway'] ) {
  237.                 return;
  238.             } elseif ( isset( $_REQUEST['level'] ) && $_REQUEST['level'] &&
  239.                 isset( $_REQUEST['review'] ) && $_REQUEST['review'] &&
  240.                 isset( $_REQUEST['token'] ) && $_REQUEST['token'] &&
  241.                 isset( $_REQUEST['PayerID'] ) && $_REQUEST['PayerID'] ) {
  242.                 return;
  243.             } elseif ( ( isset( $_POST['billing_email'] ) && ! empty( $_POST['billing_email'] ) ) ||
  244.                 ( isset( $_POST['bconfirmemail'] ) && ! empty( $_POST['bconfirmemail'] ) ) ) {
  245.                 return;
  246.             } else {
  247.                 $redirect_to = '';
  248.  
  249.                 /*** purchase course */
  250.                 $this->buy_course();
  251.  
  252.                 $register_redirect_option = get_theme_mod( 'thim_register_redirect', false );
  253.  
  254.                 if ( ! empty( $redirect_to_checkout ) ) {
  255.                     $redirect_to = $redirect_to_checkout;
  256.                 } elseif ( ! empty( $register_redirect_option ) ) {
  257.                     $redirect_to = $register_redirect_option;
  258.                 } elseif ( ! empty( $_REQUEST['redirect_to'] ) ) {
  259.                     $redirect_to = $_REQUEST['redirect_to'];
  260.                 } else {
  261.                     $redirect_to = thim_eduma_get_current_url();
  262.                 }
  263.  
  264.                 wp_safe_redirect( $redirect_to );
  265.                 die;
  266.             }
  267.         }
  268.     }
  269.  
  270.  
  271.     /**
  272.      * @param WP_Error $errors
  273.      *
  274.      * @return mixed
  275.      */
  276.     public function register_failed( $errors ) {
  277.         if ( wp_doing_ajax() || ! isset( $_POST['eduma_register_user'] ) ) {
  278.             return $errors;
  279.         }
  280.  
  281.         $error_code = $errors->get_error_code();
  282.         if ( $error_code ) {
  283.             $error_msg = '';
  284.  
  285.             $error_msg = str_replace( array( '<strong>', '</strong>' ), '', $errors->errors[ $error_code ][0] );
  286.  
  287.             $url = add_query_arg(
  288.                 array(
  289.                     'action'            => 'register',
  290.                     'thim_register_msg' => rawurlencode( $error_msg ),
  291.                 ),
  292.                 thim_get_login_page_url()
  293.             );
  294.  
  295.             wp_redirect( $url );
  296.             die;
  297.         }
  298.  
  299.         return $errors;
  300.     }
  301.  
  302.     /**
  303.      * Register via email - send email success
  304.      *
  305.      * @param int $user_id
  306.      *
  307.      * @return void
  308.      */
  309.     public function register_verify_mail_success_redirect( $user_id ) {
  310.         if ( get_theme_mod( 'thim_auto_login', true )
  311.             || wp_doing_ajax() || ! isset( $_POST['eduma_register_user'] ) ) {
  312.             return;
  313.         }
  314.  
  315.         $redirect_url = thim_get_login_page_url();
  316.  
  317.         if ( ! empty( $redirect_url ) ) {
  318.             $redirect_url = add_query_arg( array( 'result' => 'registered' ), $redirect_url );
  319.             wp_safe_redirect( $redirect_url );
  320.             die;
  321.         }
  322.     }
  323.  
  324.     /**
  325.      * @param array $objectEmail
  326.      * @param WP_User $user
  327.      *
  328.      * @return array
  329.      */
  330.     public function message_set_password_when_not_auto_login( $objectEmail = array(), $user = '' ) {
  331.  
  332.         $key = get_password_reset_key( $user );
  333.  
  334.         $set_password_link = thim_get_login_page_url() . "?action=rp&key=$key&login=" . rawurlencode( $user->user_login );
  335.  
  336.         $message  = sprintf( __( 'Username: ', 'eduma' ) . '%s', $user->user_login ) . "\r\n\r\n";
  337.         $message .= __( 'To set your password, visit the following address:', 'eduma' ) . "\r\n\r\n";
  338.         $message .= $set_password_link . "\r\n\r\n";
  339.  
  340.         $message .= wp_login_url() . "\r\n";
  341.  
  342.         $objectEmail['message'] = $message;
  343.  
  344.         return $objectEmail;
  345.     }
  346.  
  347.     /*** Reset pass ***/
  348.  
  349.     /**
  350.      * Show error if have any error when enter username/email to reset password
  351.      *
  352.      * @param WP_Error $errors
  353.      */
  354.     public function check_field_to_reset_password( $errors ) {
  355.         if ( wp_doing_ajax() || ! isset( $_POST['eduma_lostpass'] ) ) {
  356.             return;
  357.         }
  358.  
  359.         $error_msg  = '';
  360.         $error_code = $errors->get_error_code();
  361.  
  362.         if ( $errors instanceof WP_Error && $errors->has_errors() && $error_code ) {
  363.             $error_msg = str_replace( array( '<strong>', '</strong>' ), '', $errors->errors[ $error_code ][0] );
  364.         } elseif ( $_POST['user_login'] ) {
  365.             $user_login = trim( wp_unslash( sanitize_text_field( $_POST['user_login'] ) ) );
  366.  
  367.             if ( is_email( $user_login ) ) {
  368.                 $user_data = get_user_by_email( $user_login );
  369.             } else {
  370.                 $user_data = get_user_by( 'login', $user_login );
  371.             }
  372.  
  373.             if ( ! $user_data ) {
  374.                 $error_msg = __( '<strong>Error</strong>: There is no account with that username or email address.' );
  375.             }
  376.         }
  377.  
  378.         if ( ! empty( $error_msg ) ) {
  379.             //$error_msg = __( '<strong>Error</strong>: There is no account with that username or email address.' );
  380.             add_filter( 'login_errors', 'check_field_to_reset_password', 1, 9 );
  381.  
  382.             $url = add_query_arg(
  383.                 array(
  384.                     'action'            => 'lostpassword',
  385.                     'thim_lostpass_msg' => rawurlencode( $error_msg ),
  386.                 ),
  387.                 thim_get_login_page_url()
  388.             );
  389.  
  390.             wp_safe_redirect( $url );
  391.             exit;
  392.         }
  393.     }
  394.  
  395.  
  396.     public function add_lost_password_link( $content ) {
  397.         $content .= '<a class="lost-pass-link" href="' . thim_get_lost_password_url() . '" title="' . esc_attr__(
  398.             'Lost Password',
  399.             'eduma'
  400.         ) . '">' . esc_html__( 'Lost your password?', 'eduma' ) . '</a>';
  401.  
  402.         return $content;
  403.     }
  404.  
  405.     public function validate_password_reset() {
  406.         if ( wp_doing_ajax() ) {
  407.             return;
  408.         }
  409.  
  410.         $login_page = thim_get_login_page_url();
  411.         if ( 'POST' == filter_input( INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING ) ) {
  412.  
  413.             if ( ! isset( $_REQUEST['key'] ) || ! isset( $_REQUEST['login'] ) ) {
  414.                 return;
  415.             }
  416.  
  417.             $error_msg = '';
  418.             $key       = $_REQUEST['key'];
  419.             $login     = $_REQUEST['login'];
  420.  
  421.             $user = check_password_reset_key( $key, $login );
  422.  
  423.             if ( ! $user || is_wp_error( $user ) ) {
  424.                 $error_msg  = 'invalid key';
  425.                 $error_code = $user->get_error_code();
  426.                 if ( $user && $error_code ) {
  427.                     $error_msg = $user->errors[ $error_code ][0];
  428.                 }
  429.  
  430.                 wp_redirect(
  431.                     add_query_arg(
  432.                         array(
  433.                             'action'             => 'rp',
  434.                             'thim_resetpass_msg' => rawurlencode( $error_msg ),
  435.                         ),
  436.                         $login_page
  437.                     )
  438.                 );
  439.  
  440.                 die;
  441.             }
  442.  
  443.             if ( isset( $_POST['password'] ) ) {
  444.  
  445.                 if ( empty( $_POST['password'] ) ) {
  446.                     // Password is empty
  447.                     wp_redirect(
  448.                         add_query_arg(
  449.                             array(
  450.                                 'action'           => 'rp',
  451.                                 'key'              => rawurlencode( $_REQUEST['key'] ),
  452.                                 'login'            => rawurlencode( $_REQUEST['login'] ),
  453.                                 'invalid_password' => '1',
  454.                             ),
  455.                             $login_page
  456.                         )
  457.                     );
  458.                     exit;
  459.                 }
  460.  
  461.                 // Parameter checks OK, reset password
  462.                 reset_password( $user, $_POST['password'] );
  463.                 wp_redirect(
  464.                     add_query_arg(
  465.                         array(
  466.                             'result' => 'changed',
  467.                         ),
  468.                         $login_page
  469.                     )
  470.                 );
  471.             } else {
  472.                 _e( 'Invalid request.', 'eduma' );
  473.             }
  474.  
  475.             exit;
  476.         }
  477.     }
  478.     /*** End reset pass ***/
  479.  
  480.     /**
  481.      * Click buy course => Login, Register success => redirect to check out page
  482.      *
  483.      * @apply-for
  484.      * 1. buy/enroll course default of learnpress
  485.      * 2. buy/enroll course via add on learnpress-woo-payment
  486.      *
  487.      * @return void
  488.      * @since   4.2.6
  489.      * @version 1.0.1
  490.      * @author  tungnx
  491.      */
  492.     private function buy_course() {
  493.         try {
  494.             $is_purchase = 1;
  495.             $course_id   = LP_Request::get_param( 'purchase-course', 0, 'int' );
  496.             if ( ! $course_id ) {
  497.                 $is_purchase = 0;
  498.                 $course_id   = LP_Request::get_param( 'enroll-course', 0, 'int' );
  499.             }
  500.  
  501.             if ( ! $course_id ) {
  502.                 return;
  503.             }
  504.  
  505.             if ( is_plugin_active( 'learnpress-woo-payment/learnpress-woo-payment.php' ) &&
  506.                 LP_Settings::instance()->get( 'woo-payment' )['enable'] == 'yes' &&
  507.                 isset( $_POST['add-to-cart'] ) ) {
  508.  
  509.                 /*$curl = curl_init();
  510.  
  511.                 // Set product id for param add-to-cart - woo will detect and add to cart
  512.                 $_POST['add-to-cart'] = $course_id;
  513.  
  514.                 curl_setopt_array(
  515.                     $curl,
  516.                     array(
  517.                         CURLOPT_URL            => home_url(),
  518.                         CURLOPT_HEADER         => false,
  519.                         CURLOPT_SSL_VERIFYPEER => false,
  520.                         CURLOPT_RETURNTRANSFER => true,
  521.                         CURLOPT_HTTPHEADER     => array(),
  522.                     )
  523.                 );
  524.  
  525.                 curl_setopt( $curl, CURLOPT_HTTPGET, true );
  526.                 curl_exec( $curl );*/
  527.  
  528.                 /*$wc_cart = WC()->cart;
  529.                 $wc_cart->add_to_cart( $course_id );*/
  530.  
  531.                 $pageCheckoutId = get_option( 'woocommerce_checkout_page_id', false );
  532.                 if ( $pageCheckoutId ) {
  533.                     $redirect_to = get_permalink( $pageCheckoutId );
  534.  
  535.                     wp_safe_redirect( $redirect_to );
  536.                     die;
  537.                 }
  538.             } elseif ( is_plugin_active( 'learnpress/learnpress.php' ) ) {
  539.                 $url_slug_handle = '';
  540.  
  541.                 $request = new WP_REST_Request( 'POST', $url_slug_handle );
  542.                 $request->set_param( 'id', $course_id );
  543.                 $course_controller = new LP_REST_Courses_Controller();
  544.                 if ( $is_purchase ) {
  545.                     $response = $course_controller->purchase_course( $request );
  546.                     $data_res = $response;
  547.                     if ( isset( $response->data ) ) {
  548.                         $data_res = $response->data;
  549.                     }
  550.                 } else {
  551.                     if ( version_compare( '4.2.0', LEARNPRESS_VERSION, '<' ) ) {
  552.                         return;
  553.                     }
  554.                     $data_res = $course_controller->enroll_courses( $request );
  555.                 }
  556.  
  557.                 $status   = $data_res->status ?? '';
  558.                 $redirect = $data_res->data->redirect ?? '';
  559.                 if ( 'success' == $status && ! empty( $redirect ) ) {
  560.                     wp_safe_redirect( $redirect );
  561.                     die;
  562.                 }
  563.             }
  564.         } catch ( Throwable $e ) {
  565.             error_log( $e->getMessage() );
  566.         }
  567.     }
  568.  
  569.     /**
  570.      * @param $url_redirect
  571.      * @param $course_id
  572.      * @param $cart_id
  573.      * @param $action
  574.      *
  575.      * @return mixed
  576.      * @since  4.2.6
  577.      * @author tungnx
  578.      */
  579.     public function url_redirect_after_checkout_lp_course_success( $url_redirect, $course_id, $cart_id, $action ) {
  580.  
  581.         return $url_redirect;
  582.     }
  583.  
  584.     /**
  585.      * Content mail when user register success with auto login
  586.      *
  587.      * @param array $objectEmail
  588.      *
  589.      * @return array
  590.      */
  591.     public function message_when_user_register_auto_login( $objectEmail = array() ) {
  592.         if ( get_theme_mod( 'thim_auto_login', true ) ) {
  593.             $objectEmail['subject'] = 'Welcome to [%s]';
  594.             $objectEmail['message'] = sprintf(
  595.                 'Hi ###USERNAME###,
  596.  
  597. You registered successfully on %s site
  598.  
  599. This email has been sent to ###EMAIL###
  600.  
  601. Regards,
  602. All at ###SITENAME###
  603. ###SITEURL###',
  604.                 get_bloginfo()
  605.             );
  606.  
  607.             return $objectEmail;
  608.         }
  609.  
  610.     }
  611.  
  612.     public static function getInstance() {
  613.         if ( is_null( self::$_instance ) ) {
  614.             self::$_instance = new self();
  615.         }
  616.  
  617.         return self::$_instance;
  618.     }
  619. }
  620.  
  621. ThimEdumaRegisterFunction::getInstance();
  622.  
  623. /**
  624.  * Get login page url
  625.  *
  626.  * @return false|string
  627.  */
  628. if ( ! function_exists( 'thim_get_login_page_url' ) ) {
  629.     function thim_get_login_page_url() {
  630.         $page = get_option( 'thim_login_page' );
  631.         if ( $page ) {
  632.             return get_permalink( $page );
  633.         } else {
  634.             global $wpdb;
  635.             $page = $wpdb->get_col(
  636.                 $wpdb->prepare(
  637.                     "SELECT p.ID FROM $wpdb->posts AS p INNER JOIN $wpdb->postmeta AS pm ON p.ID = pm.post_id
  638.                         WHERE   pm.meta_key = %s
  639.                         AND     pm.meta_value = %s
  640.                         AND     p.post_type = %s
  641.                         AND     p.post_status = %s",
  642.                     'thim_login_page',
  643.                     '1',
  644.                     'page',
  645.                     'publish'
  646.                 )
  647.             );
  648.             if ( ! empty( $page[0] ) ) {
  649.                 return get_permalink( $page[0] );
  650.             }
  651.         }
  652.  
  653.         return wp_login_url();
  654.     }
  655. }
  656.  
  657. /**
  658.  * Filter register link
  659.  *
  660.  * @param $register_url
  661.  *
  662.  * @return string|void
  663.  */
  664. if ( ! function_exists( 'thim_get_register_url' ) ) {
  665.     function thim_get_register_url() {
  666.         $url = add_query_arg( 'action', 'register', thim_get_login_page_url() );
  667.  
  668.         return $url;
  669.     }
  670. }
  671. if ( ! is_multisite() ) {
  672.     add_filter( 'register_url', 'thim_get_register_url' );
  673. }
  674.  
  675. /**
  676.  * Redirect to custom register page in case multi sites
  677.  *
  678.  * @param $url
  679.  *
  680.  * @return mixed
  681.  */
  682.  
  683. function thim_multisite_register_email_confirmation() {
  684.     add_filter('pre_option_new_user_registration', function($value) {
  685.         return 'pending';
  686.     });
  687.     add_filter('new_user_default_role', function($default_role) {
  688.         return 'subscriber';
  689.     });
  690. }
  691. add_action('init', 'thim_multisite_register_email_confirmation');
  692.  
  693. function thim_multisite_register_set_content_type() {
  694.     return "text/html";
  695. }
  696.  
  697. function thim_multisite_register_confirmation_email($user_id) {
  698.     $user = get_user_by('id', $user_id);
  699.     $user_email = stripslashes($user->user_email);
  700.     $key = md5($user_email . time());
  701.     $subject = __('Please confirm your registration', 'text-domain');
  702.     $message = __("Thank you for registering with us. Please click the link below to confirm your registration:", 'text-domain') . "\r\n\r\n";
  703.     $message .= home_url( '/wp-login.php?action=confirm_registration&email=' . $user_email . '&key=' . $key );
  704.     wp_mail($user_email, $subject, $message);
  705. }
  706. add_action('user_register', 'thim_multisite_register_confirmation_email');
  707.  
  708. if ( ! function_exists( 'thim_multisite_register_redirect' ) ) {
  709.     function thim_multisite_register_redirect( $url ) {
  710.  
  711.         if ( ! is_user_logged_in() ) {
  712.             if ( is_multisite() ) {
  713.                 $url = add_query_arg( 'action', 'register', thim_get_login_page_url() );
  714.             }
  715.  
  716.             $user_login = isset( $_POST['user_login'] ) ? $_POST['user_login'] : '';
  717.             $user_email = isset( $_POST['user_email'] ) ? $_POST['user_email'] : '';
  718.  
  719.             $errors = register_new_user( $user_login, $user_email );
  720.  
  721.             if ( ! is_wp_error( $errors ) ) {
  722.                 $redirect_to = ! empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : 'wp-login.php?checkemail=registered';
  723.                 wp_safe_redirect( $redirect_to );
  724.                 exit();
  725.             }
  726.         }
  727.  
  728.         return $url;
  729.     }
  730. }
  731. add_filter( 'wp_signup_location', 'thim_multisite_register_redirect' );
  732.  
  733. /**
  734.  * Filter lost password link
  735.  *
  736.  * @param $url
  737.  *
  738.  * @return string
  739.  */
  740. if ( ! function_exists( 'thim_get_lost_password_url' ) ) {
  741.     function thim_get_lost_password_url() {
  742.         $url = add_query_arg( 'action', 'lostpassword', thim_get_login_page_url() );
  743.  
  744.         return $url;
  745.     }
  746. }
  747.  
  748. /*
  749.  * Add google captcha register check to register form ( multisite case )
  750.  */
  751. if ( is_multisite() && function_exists( 'gglcptch_register_check' ) ) {
  752.     global $gglcptch_ip_in_whitelist;
  753.  
  754.     if ( ! $gglcptch_ip_in_whitelist ) {
  755.         add_action( 'registration_errors', 'gglcptch_register_check', 10, 1 );
  756.     }
  757. }
  758.  
Add Comment
Please, Sign In to add comment