Last active
November 6, 2018 17:38
-
-
Save panoslyrakis/f9b11a950b59e9ced94f867c52d88d9c to your computer and use it in GitHub Desktop.
Membership2 - Use default registration page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Membership2 - Use default registration page | |
| Plugin URI: https://premium.wpmudev.org/ | |
| Description: Use the WordPress default registration page | |
| Author: Panos Lyrakis @ WPMUDEV | |
| Author URI: https://premium.wpmudev.org/ | |
| License: GPLv2 or later | |
| */ | |
| if ( ! defined( 'ABSPATH' ) ) { | |
| exit; | |
| } | |
| if ( ! class_exists( 'WPMUDEV_MS_WP_Default_Reg' ) ) { | |
| class WPMUDEV_MS_WP_Default_Reg { | |
| private static $_instance = null; | |
| private static $_reg_url = null; | |
| public static function get_instance() { | |
| if( is_null( self::$_instance ) ){ | |
| self::$_instance = new WPMUDEV_MS_WP_Default_Reg(); | |
| self::$_reg_url = 'http://site.com/path-to-memberships-list'; | |
| } | |
| return self::$_instance; | |
| } | |
| private function __construct() { | |
| add_filter( 'ms_frontend_handle_registration', array( $this, 'cancel_registration_redir' ), 10 ); | |
| add_filter( 'ms_model_pages_get_ms_page_url', array( $this, 'get_ms_page_url' ), 10, 4 ); | |
| add_action( 'register_form', array( $this, 'register_form_fields' ), 10 ); | |
| add_action( 'login_form', array( $this, 'register_form_fields' ), 10 ); | |
| add_action( 'register_post', array( $this, 'redirect_on_registration_error' ), 10, 3 ); | |
| add_filter( 'login_message', array( $this, 'registration_errors' ), 10 ); | |
| add_action( 'user_register', array( $this, 'user_register' ), 10 ); | |
| add_action( 'ms_show_prices', array( $this, 'ms_show_prices' ), 10 ); | |
| add_action( 'login_url', array( $this, 'login_url' ), 10, 3 ); | |
| add_filter( 'login_redirect', array( $this, 'login_redirect' ), 10, 3 ); | |
| } | |
| public function cancel_registration_redir(){ | |
| return false; | |
| } | |
| public function get_ms_page_url( $url, $page_type, $ssl, $site_id ){ | |
| if( $url == self::$_reg_url ){ | |
| $url = wp_registration_url(); | |
| } | |
| return $url; | |
| } | |
| public function register_form_fields(){ | |
| if( isset( $_REQUEST['_ms_action'] ) && $_REQUEST['_ms_action'] == 'membership_signup' && | |
| isset( $_REQUEST['membership_id'] ) && is_numeric( $_REQUEST['membership_id'] ) ){ | |
| foreach( $_REQUEST as $field_key => $field_val ){ | |
| if( $field_key == 'reg_errors' ){ | |
| continue; | |
| } | |
| ?> | |
| <input type="hidden" name="<?php echo $field_key; ?>" value="<?php echo sanitize_text_field( $field_val ); ?>" /> | |
| <?php | |
| } | |
| } | |
| } | |
| public function redirect_on_registration_error( $sanitized_user_login, $user_email, $errors ){ | |
| if( ! isset( $_REQUEST['_ms_action'] ) || $_REQUEST['_ms_action'] != 'membership_signup' ){ | |
| return; | |
| } | |
| if ( $errors->get_error_code() ){ | |
| $_registration_errors = urlencode( json_encode( $errors->errors ) ); | |
| $redirect_url = wp_registration_url(); | |
| $url_query_string = ''; | |
| $query_args_list = array( '_wpnonce', '_wp_http_referer', 'membership_id', '_ms_action', 'step', 'submit' ); | |
| foreach( $_POST as $field_key => $field_val ){ | |
| if( ! in_array( $field_key, $query_args_list ) ){ | |
| continue; | |
| } | |
| $url_query_string .= '&' . $field_key . '=' . $field_val; | |
| } | |
| $redirect_url .= $url_query_string . '®_errors=' . $_registration_errors; | |
| wp_redirect( $redirect_url ); | |
| exit; | |
| } | |
| } | |
| public function registration_errors( $message ){ | |
| if( ! isset( $_REQUEST[ 'reg_errors' ] ) || $_REQUEST[ 'reg_errors' ] == '' ){ | |
| return; | |
| } | |
| $error_log = json_decode( stripslashes( urldecode($_REQUEST[ 'reg_errors' ] ) ) ); | |
| $error_message = ''; | |
| foreach( $error_log as $error_key => $error_msg ){ | |
| $error_message .= $error_msg[0] . '</br>'; | |
| } | |
| $error_message = '<div id="login_error">' . $error_message . '</div>'; | |
| return $error_message . $message; | |
| } | |
| public function user_register( $user_id ){ | |
| if( isset( $_REQUEST['_ms_action'] ) && $_REQUEST['_ms_action'] == 'membership_signup' && | |
| isset( $_REQUEST['membership_id'] ) && is_numeric( $_REQUEST['membership_id'] ) ){ | |
| wp_set_current_user($user_id); | |
| wp_set_auth_cookie($user_id); | |
| $membership_id = (int)$_REQUEST['membership_id']; | |
| $url = self::$_reg_url . '?step=payment_table&membership_id=' . $membership_id; | |
| wp_redirect( $url ); | |
| exit; | |
| } | |
| } | |
| public function login_url( $login_url, $redirect, $force_reauth ){ | |
| $url_query_string = ''; | |
| $query_args_list = array( '_wpnonce', '_wp_http_referer', 'membership_id', '_ms_action', 'step', 'submit' ); | |
| foreach( $_REQUEST as $field_key => $field_val ){ | |
| if( ! in_array( $field_key, $query_args_list ) ){ | |
| continue; | |
| } | |
| $url_query_string .= '&' . $field_key . '=' . $field_val; | |
| } | |
| return $login_url . '?' . $url_query_string; | |
| } | |
| public function login_redirect( $redirect_to, $request, $user ){ | |
| if( isset( $_REQUEST['_ms_action'] ) && $_REQUEST['_ms_action'] == 'membership_signup' && | |
| isset( $_REQUEST['membership_id'] ) && is_numeric( $_REQUEST['membership_id'] ) ){ | |
| $membership_id = (int)$_REQUEST['membership_id']; | |
| $redirect_to = self::$_reg_url . '?step=payment_table&membership_id=' . $membership_id; | |
| } | |
| return $redirect_to; | |
| } | |
| public function ms_show_prices(){ | |
| ?> | |
| <script type="text/javascript"> | |
| (function($){ | |
| $(document).ready(function(){ | |
| $('.ms-membership-form').on('submit', function () { | |
| if( $( this ).data( 'has-been-reset' ) == 'true' ){ | |
| return; | |
| } | |
| $(this).data( 'has-been-reset', 'true' ); | |
| /*Changing form method to GET so the registration page doesn't check for post var and throw error message*/ | |
| $(this).prop('method','get'); | |
| /*Changing action name so it doesn't replace the ?action=register*/ | |
| $(this).find('#action').attr( 'name', '_ms_action' ); | |
| $("<input>").attr({ | |
| 'type':'text', | |
| 'name':'action' | |
| }).val('register').appendTo($(this)); | |
| }); | |
| }); | |
| })(jQuery); | |
| </script> | |
| <?php | |
| } | |
| } | |
| add_action( 'plugins_loaded', function(){ | |
| $GLOBALS['WPMUDEV_MS_WP_Default_Reg'] = WPMUDEV_MS_WP_Default_Reg::get_instance(); | |
| }, 10 ); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please remember to replace line
self::$_reg_url = 'http://site.com/path-to-memberships-list';with your memberships list path