From 658dd10e2e5e774115c27a283713ecbd55f55b74 Mon Sep 17 00:00:00 2001 From: Prajjwal Poudel Date: Tue, 24 Sep 2024 08:11:53 +0545 Subject: [PATCH 1/3] Enhance - Display proper error when wrong page is selected as my account --- assets/js/admin/settings.js | 57 +++++++++++++++ includes/admin/class-ur-admin-settings.php | 26 +++---- includes/class-ur-ajax.php | 82 +++++++++++++++------- includes/functions-ur-core.php | 7 ++ 4 files changed, 135 insertions(+), 37 deletions(-) diff --git a/assets/js/admin/settings.js b/assets/js/admin/settings.js index 71f595962..31d34fb77 100644 --- a/assets/js/admin/settings.js +++ b/assets/js/admin/settings.js @@ -804,6 +804,63 @@ } }); + // Display error when page with our my account or login shortcode is not selected + $( + "#user_registration_login_options_login_redirect_url, #user_registration_myaccount_page_id" + ).on("change", function () { + var $this = $(this), + data = { + action: "user_registration_my_account_selection_validator", + security: + user_registration_settings_params.user_registration_my_account_selection_validator_nonce + }; + + data.user_registration_selected_my_account_page = $this.val(); + + $this.prop("disabled", true); + $this.css("border", "1px solid #e1e1e1"); + $this + .closest(".user-registration-global-settings--field") + .find(".error.inline") + .remove(); + $this + .closest(".user-registration-global-settings") + .append('
'); + + $.ajax({ + url: user_registration_settings_params.ajax_url, + data: data, + type: "POST", + complete: function (response) { + if (response.responseJSON.success === false) { + $this + .closest(".user-registration-global-settings--field") + .append( + "
" + + response.responseJSON.data.message + + "
" + ); + $this.css("border", "1px solid red"); + $this + .closest("form") + .find("input[name='save']") + .prop("disabled", true); + } else { + $this + .closest("form") + .find("input[name='save']") + .prop("disabled", false); + } + $this.prop("disabled", false); + + $this + .closest(".user-registration-global-settings") + .find(".ur-spinner") + .remove(); + } + }); + }); + // Set localStorage with expiry function setStorageValue(key, value) { var current = new Date(); diff --git a/includes/admin/class-ur-admin-settings.php b/includes/admin/class-ur-admin-settings.php index 4bdb341a0..f5a6438be 100644 --- a/includes/admin/class-ur-admin-settings.php +++ b/includes/admin/class-ur-admin-settings.php @@ -87,18 +87,6 @@ public static function save() { if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'user-registration-settings' ) ) { die( esc_html__( 'Action failed. Please refresh the page and retry.', 'user-registration' ) ); } - /** - * Action to save current tab settings - */ - do_action( 'user_registration_settings_save_' . $current_tab ); - /** - * Action to save current tab options - */ - do_action( 'user_registration_update_options_' . $current_tab ); - /** - * Action to save options - */ - do_action( 'user_registration_update_options' ); /** * Filter to modify display of setting message @@ -111,6 +99,19 @@ public static function save() { if ( $flag && is_bool( $flag ) ) { self::add_message( esc_html__( 'Your settings have been saved.', 'user-registration' ) ); + + /** + * Action to save current tab settings + */ + do_action( 'user_registration_settings_save_' . $current_tab ); + /** + * Action to save current tab options + */ + do_action( 'user_registration_update_options_' . $current_tab ); + /** + * Action to save options + */ + do_action( 'user_registration_update_options' ); } elseif ( $flag && 'redirect_login_error' === $flag ) { self::add_error( @@ -197,6 +198,7 @@ public static function output() { array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'user_registration_search_global_settings_nonce' => wp_create_nonce( 'user_registration_search_global_settings' ), + 'user_registration_my_account_selection_validator_nonce' => wp_create_nonce( 'user_registration_my_account_selection_validator' ), 'i18n_nav_warning' => esc_html__( 'The changes you made will be lost if you navigate away from this page.', 'user-registration' ), 'i18n' => array( 'captcha_success' => esc_html__( 'Captcha Test Successful !', 'user-registration' ), diff --git a/includes/class-ur-ajax.php b/includes/class-ur-ajax.php index 724fe45b8..738f22914 100644 --- a/includes/class-ur-ajax.php +++ b/includes/class-ur-ajax.php @@ -49,31 +49,32 @@ public static function init() { */ public static function add_ajax_events() { $ajax_events = array( - 'user_input_dropped' => true, - 'user_form_submit' => true, - 'update_profile_details' => true, - 'profile_pic_upload' => true, - 'ajax_login_submit' => true, - 'send_test_email' => false, - 'create_form' => false, - 'rated' => false, - 'dashboard_widget' => false, - 'dismiss_notice' => false, - 'import_form_action' => false, - 'template_licence_check' => false, - 'captcha_setup_check' => false, - 'install_extension' => false, - 'profile_pic_remove' => false, - 'form_save_action' => false, - 'embed_form_action' => false, - 'embed_page_list' => false, - 'allow_usage_dismiss' => false, - 'cancel_email_change' => false, - 'email_setting_status' => false, - 'locked_form_fields_notice' => false, - 'search_global_settings' => false, - 'php_notice_dismiss' => false, - 'locate_form_action' => false, + 'user_input_dropped' => true, + 'user_form_submit' => true, + 'update_profile_details' => true, + 'profile_pic_upload' => true, + 'ajax_login_submit' => true, + 'send_test_email' => false, + 'create_form' => false, + 'rated' => false, + 'dashboard_widget' => false, + 'dismiss_notice' => false, + 'import_form_action' => false, + 'template_licence_check' => false, + 'captcha_setup_check' => false, + 'install_extension' => false, + 'profile_pic_remove' => false, + 'form_save_action' => false, + 'embed_form_action' => false, + 'embed_page_list' => false, + 'allow_usage_dismiss' => false, + 'cancel_email_change' => false, + 'email_setting_status' => false, + 'locked_form_fields_notice' => false, + 'search_global_settings' => false, + 'php_notice_dismiss' => false, + 'locate_form_action' => false, + 'my_account_selection_validator' => false, ); foreach ( $ajax_events as $ajax_event => $nopriv ) { @@ -1667,6 +1668,37 @@ public static function php_notice_dismiss() { return false; } + + /** + * AJAX validate selected my account page. + */ + public static function my_account_selection_validator() { + check_ajax_referer( 'user_registration_my_account_selection_validator', 'security' ); + + if ( ! current_user_can( 'manage_options' ) ) { + wp_send_json_error( array( 'message' => __( 'You do not have permission to edit settings form.', 'user-registration' ) ) ); + wp_die( -1 ); + } + + // Return if default wp_login is disabled and no redirect url is set. + if ( isset( $_POST['user_registration_selected_my_account_page'] ) ) { + if ( is_numeric( $_POST['user_registration_selected_my_account_page'] ) ) { + $is_page_my_account_page = ur_find_my_account_in_page( sanitize_text_field( wp_unslash( $_POST['user_registration_selected_my_account_page'] ) ) ); + if ( ! $is_page_my_account_page ) { + wp_send_json_error( + array( + 'message' => esc_html__( + 'The selected page is not a User Registration Login or My Account page.', + 'user-registration' + ), + ) + ); + } + } + } + + wp_send_json_success(); + } } UR_AJAX::init(); diff --git a/includes/functions-ur-core.php b/includes/functions-ur-core.php index 09017bcf1..2d08bafbc 100644 --- a/includes/functions-ur-core.php +++ b/includes/functions-ur-core.php @@ -6429,6 +6429,13 @@ function ur_prevent_default_login( $data ) { } } } + } elseif ( isset( $data['user_registration_myaccount_page_id'] ) ) { + if ( is_numeric( $data['user_registration_myaccount_page_id'] ) ) { + $is_page_my_account_page = ur_find_my_account_in_page( $data['user_registration_myaccount_page_id'] ); + if ( ! $is_page_my_account_page ) { + return 'redirect_login_not_myaccount'; + } + } } return true; } From 4bb63483c0f9f36510428a595beeff54afe5ec36 Mon Sep 17 00:00:00 2001 From: Prajjwal Poudel Date: Tue, 24 Sep 2024 09:10:25 +0545 Subject: [PATCH 2/3] Tweak - Selected my account page by default in Redirect wp login screen settings --- includes/admin/class-ur-admin-settings.php | 2 +- includes/admin/settings/class-ur-settings-general.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/admin/class-ur-admin-settings.php b/includes/admin/class-ur-admin-settings.php index f5a6438be..2803077d9 100644 --- a/includes/admin/class-ur-admin-settings.php +++ b/includes/admin/class-ur-admin-settings.php @@ -692,7 +692,7 @@ class="' . esc_attr( isset( $value['class'] ) ? $value['class'] : '' ) . '" 'show_option_none' => ' ', 'class' => $value['class'], 'echo' => false, - 'selected' => absint( self::get_option( $value['id'] ) ), + 'selected' => absint( self::get_option( $value['id'], $value['default'] ) ), ); if ( isset( $value['args'] ) ) { diff --git a/includes/admin/settings/class-ur-settings-general.php b/includes/admin/settings/class-ur-settings-general.php index 9ef274e31..5a5b240ef 100644 --- a/includes/admin/settings/class-ur-settings-general.php +++ b/includes/admin/settings/class-ur-settings-general.php @@ -531,8 +531,8 @@ public function get_login_options_settings() { 'type' => 'single_select_page', 'desc_tip' => true, 'css' => 'min-width: 350px;', - 'class' => 'ur-redirect-to-login-page', - 'default' => '', + 'class' => 'ur-redirect-to-login-page ur-enhanced-select-nostd', + 'default' => get_option( 'user_registration_myaccount_page_id', '' ), ), ), ), From 578ec5a0ff40562b5fae8345abf799aaf8ecf10c Mon Sep 17 00:00:00 2001 From: Prajjwal Poudel Date: Tue, 24 Sep 2024 12:15:05 +0545 Subject: [PATCH 3/3] Tweak - Install pages on installation and remove install & proceed process in setup wizard --- .../version1/class-ur-getting-started.php | 67 --------- includes/admin/class-ur-admin-welcome.php | 22 ++- includes/class-ur-install.php | 1 + src/context/gettingStartedContext.js | 23 ---- src/welcome/components/App.js | 128 +++--------------- src/welcome/components/screens/InstallPage.js | 98 +++----------- 6 files changed, 48 insertions(+), 291 deletions(-) diff --git a/includes/RestApi/controllers/version1/class-ur-getting-started.php b/includes/RestApi/controllers/version1/class-ur-getting-started.php index 19e784366..583e0283b 100644 --- a/includes/RestApi/controllers/version1/class-ur-getting-started.php +++ b/includes/RestApi/controllers/version1/class-ur-getting-started.php @@ -53,15 +53,6 @@ public function register_routes() { 'permission_callback' => array( __CLASS__, 'check_admin_permissions' ), ) ); - register_rest_route( - $this->namespace, - '/' . $this->rest_base . '/install-pages', - array( - 'methods' => 'POST', - 'callback' => array( __CLASS__, 'ur_getting_started_install_pages' ), - 'permission_callback' => array( __CLASS__, 'check_admin_permissions' ), - ) - ); } /** @@ -125,64 +116,6 @@ public static function ur_save_getting_started_settings( $request ) { ); } - /** - * Install default pages when user hits Install & Proceed button in setup wizard. - * - * @since 2.1.4 - * - * @param WP_REST_Request $request Full data about the request. - * @return array settings. - */ - public static function ur_getting_started_install_pages( $request ) { - - if ( ! isset( $request['install_pages'] ) || ! $request['install_pages'] ) { - return new \WP_REST_Response( - array( - 'success' => false, - 'message' => __( 'Pages cannot be installed', 'user-registration' ), - ), - 200 - ); - } - - include_once untrailingslashit( plugin_dir_path( UR_PLUGIN_FILE ) ) . '/includes/admin/functions-ur-admin.php'; - - $pages = apply_filters( 'user_registration_create_pages', array() ); - $default_form_page_id = get_option( 'user_registration_default_form_page_id' ); - $is_pro = false !== ur_get_license_plan() ? true : false; - - if ( $default_form_page_id ) { - $pages['registration'] = array( - 'name' => _x( 'registration', 'Page slug', 'user-registration' ), - 'title' => _x( 'Registration', 'Page title', 'user-registration' ), - 'content' => '[' . apply_filters( 'user_registration_form_shortcode_tag', 'user_registration_form' ) . ' id="' . esc_attr( $default_form_page_id ) . '"]', - ); - } - - $pages['myaccount'] = array( - 'name' => _x( 'my-account', 'Page slug', 'user-registration' ), - 'title' => _x( 'My Account', 'Page title', 'user-registration' ), - 'content' => '[' . apply_filters( 'user_registration_my_account_shortcode_tag', 'user_registration_my_account' ) . ']', - ); - - $page_slug = array(); - foreach ( $pages as $key => $page ) { - $post_id = ur_create_page( esc_sql( $page['name'] ), 'user_registration_' . $key . '_page_id', wp_kses_post( ( $page['title'] ) ), wp_kses_post( $page['content'] ) ); - array_push( $page_slug, get_post_field( 'post_name', $post_id ) ); - } - - return new \WP_REST_Response( - array( - 'success' => true, - 'page_slug' => $page_slug, - 'default_form_id' => $default_form_page_id, - 'is_pro' => $is_pro, - 'registration_page_link' => get_permalink( get_option( 'user_registration_registration_page_id' ) ), - ), - 200 - ); - } - /** * Get settings for getting started page. * diff --git a/includes/admin/class-ur-admin-welcome.php b/includes/admin/class-ur-admin-welcome.php index 089bd8a16..342f4c029 100644 --- a/includes/admin/class-ur-admin-welcome.php +++ b/includes/admin/class-ur-admin-welcome.php @@ -56,18 +56,26 @@ public static function welcome_page() { wp_register_script( 'ur-setup-wizard-script', UR()->plugin_url() . '/chunks/welcome.js', array( 'wp-element', 'wp-blocks', 'wp-editor' ), UR()->version, true ); wp_enqueue_style( 'ur-setup-wizard-style', UR()->plugin_url() . '/assets/css/user-registration-setup-wizard.css', array(), UR()->version ); wp_enqueue_script( 'ur-setup-wizard-script' ); + $default_form_page_id = get_option( 'user_registration_default_form_page_id' ); + $registration_page_id = get_option( 'user_registration_registration_page_id' ); + $my_account_page_id = get_option( 'user_registration_myaccount_page_id' ); + $registration_page_slug = $registration_page_id ? get_post_field( 'post_name', $registration_page_id ) : ''; + $my_account_page_slug = $my_account_page_id ? get_post_field( 'post_name', $my_account_page_id ) : ''; wp_localize_script( 'ur-setup-wizard-script', '_UR_WIZARD_', array( - 'adminURL' => esc_url( admin_url() ), - 'siteURL' => esc_url( home_url( '/' ) ), - 'defaultFormURL' => esc_url( admin_url( '/admin.php?page=add-new-registration&edit-registration=' . get_option( 'user_registration_default_form_page_id' ) ) ), - 'urRestApiNonce' => wp_create_nonce( 'wp_rest' ), - 'onBoardIconsURL' => esc_url( UR()->plugin_url() . '/assets/images/onboard-icons' ), - 'restURL' => rest_url(), - 'registrationPageURL' => get_permalink( get_option( 'user_registration_registration_page_id' ) ), + 'adminURL' => esc_url( admin_url() ), + 'siteURL' => esc_url( home_url( '/' ) ), + 'defaultFormId' => $default_form_page_id, + 'registrationPageSlug' => $registration_page_slug, + 'myAccountPageSlug' => $my_account_page_slug, + 'defaultFormURL' => esc_url( admin_url( '/admin.php?page=add-new-registration&edit-registration=' . $default_form_page_id ) ), + 'urRestApiNonce' => wp_create_nonce( 'wp_rest' ), + 'onBoardIconsURL' => esc_url( UR()->plugin_url() . '/assets/images/onboard-icons' ), + 'restURL' => rest_url(), + 'registrationPageURL' => get_permalink( get_option( 'user_registration_registration_page_id' ) ), ) ); diff --git a/includes/class-ur-install.php b/includes/class-ur-install.php index c4b70175d..99be07938 100644 --- a/includes/class-ur-install.php +++ b/includes/class-ur-install.php @@ -146,6 +146,7 @@ public static function install() { self::maybe_update_db_version(); self::maybe_add_installation_date(); self::maybe_run_migrations(); + self::create_pages(); $path = UR_UPLOAD_PATH . 'profile-pictures'; diff --git a/src/context/gettingStartedContext.js b/src/context/gettingStartedContext.js index a267356ea..d76ae2332 100644 --- a/src/context/gettingStartedContext.js +++ b/src/context/gettingStartedContext.js @@ -1,23 +1,10 @@ export const initialState = { settings: {}, - installPage: { - registration_page: { - status: "not_installed", - slug: "" - }, - my_account_page: { - status: "not_installed", - slug: "" - } - }, - defaultFormId: "", isPro: false }; export const actionTypes = { GET_SETTINGS: "GET_SETTINGS", - GET_INSTALL_PAGE: "GET_INSTALL_PAGE", - GET_DEFAULT_FORM: "GET_DEFAULT_FORM", GET_DEFAULT_REGISTRATION_PAGE: "GET_DEFAULT_REGISTRATION_PAGE", GET_IS_PRO: "GET_IS_PRO" }; @@ -29,16 +16,6 @@ const reducer = (state, action) => { ...state, settings: action.settings }; - case actionTypes.GET_INSTALL_PAGE: - return { - ...state, - installPage: action.installPage - }; - case actionTypes.GET_DEFAULT_FORM: - return { - ...state, - defaultFormId: action.defaultFormId - }; case actionTypes.GET_DEFAULT_REGISTRATION_PAGE: return { ...state, diff --git a/src/welcome/components/App.js b/src/welcome/components/App.js index f761fbb3c..1bdf394ee 100644 --- a/src/welcome/components/App.js +++ b/src/welcome/components/App.js @@ -30,9 +30,7 @@ import { useStateValue } from "../../context/StateProvider"; import { actionTypes } from "../../context/gettingStartedContext"; function App() { - const [{ settings, installPage, registrationPageLink }, dispatch] = - useStateValue(); - const [initiateInstall, setInitiateInstall] = useState(false); + const [{ settings, registrationPageLink }, dispatch] = useStateValue(); const [disabledLink, setDisabledLink] = useState(false); const [nextStepProgess, setNextStepProgess] = useState(false); @@ -53,7 +51,7 @@ function App() { label: __("Install Pages", "user-registration"), title: __("Install Pages", "user-registration"), description: __( - "The following pages and forms will be created automatically.", + "The following pages have been created automatically.", "user-registration" ), isDone: true, @@ -228,83 +226,6 @@ function App() { handleNext(); }; - /** - * Install Pages in backend when Install Pages button is clicked. - */ - const handleInstallPages = () => { - setInitiateInstall(true); - // POST - apiFetch({ - path: - restURL + "user-registration/v1/getting-started/install-pages", - method: "POST", - headers: { - "X-WP-Nonce": urRestApiNonce - }, - data: { install_pages: true } - }).then((res) => { - if (res.success) { - if (res.default_form_id) { - dispatch({ - type: actionTypes.GET_DEFAULT_FORM, - defaultFormId: res.default_form_id - }); - } - - if (res.registration_page_link) { - dispatch({ - type: actionTypes.GET_DEFAULT_REGISTRATION_PAGE, - registrationPageLink: res.registration_page_link - }); - } - - if (res.is_pro) { - dispatch({ - type: actionTypes.GET_IS_PRO, - defaultFormId: res.is_pro - }); - } - - let newInstallPageRef = { ...installPage }; - newInstallPageRef.registration_page.status = "installing"; - newInstallPageRef.registration_page.slug = - "/" + res.page_slug[0]; - - dispatch({ - type: actionTypes.GET_INSTALL_PAGE, - installPage: newInstallPageRef - }); - - new Promise(function (resolve, reject) { - setTimeout(resolve, 5000); - }).then(function () { - newInstallPageRef.registration_page.status = "installed"; - newInstallPageRef.my_account_page.status = "installing"; - newInstallPageRef.my_account_page.slug = - "/" + res.page_slug[1]; - - dispatch({ - type: actionTypes.GET_INSTALL_PAGE, - installPage: newInstallPageRef - }); - - new Promise(function (resolve, reject) { - setTimeout(resolve, 5000); - }).then(function () { - newInstallPageRef.my_account_page.status = "installed"; - - dispatch({ - type: actionTypes.GET_INSTALL_PAGE, - installPage: newInstallPageRef - }); - }); - }); - } else { - console.log(res.message); - } - }); - }; - /** * Save settings on button press. */ @@ -488,37 +409,20 @@ function App() { )} )} - {steps[0].key === activeStep.key && - installPage.my_account_page.status !== - "installed" ? ( - - ) : ( - - )} + )} diff --git a/src/welcome/components/screens/InstallPage.js b/src/welcome/components/screens/InstallPage.js index c30354867..044f760b1 100644 --- a/src/welcome/components/screens/InstallPage.js +++ b/src/welcome/components/screens/InstallPage.js @@ -8,7 +8,7 @@ import { Box, Checkbox, CircularProgress, - CircularProgressLabel, + CircularProgressLabel } from "@chakra-ui/react"; import { __ } from "@wordpress/i18n"; @@ -16,39 +16,13 @@ import { __ } from "@wordpress/i18n"; * Internal Dependencies */ import ConsentModal from "../common/ConsentModal"; -import { useStateValue } from "../../../context/StateProvider"; const InstallPage = () => { - const [{ installPage, defaultFormId }] = useStateValue(); - const [counter, setCounter] = useState(0); const [allowTracking, setAllowTracking] = useState(false); - /** - * Change counter every time installPage state is changed to show spinner while installing. - */ - useEffect(() => { - if ( - installPage.registration_page.status === "installing" || - installPage.my_account_page.status === "installing" - ) { - const timer = setInterval(() => { - setCounter((prevCounter) => { - if (prevCounter < 100) { - return prevCounter + 20; - } else { - prevCounter = 0; - return prevCounter; - } - }); - }, 840); - - return () => { - clearInterval(timer); - }; - } else { - setCounter(0); - } - }, [installPage]); + /* global _UR_WIZARD_ */ + const { defaultFormId, registrationPageSlug, myAccountPageSlug } = + typeof _UR_WIZARD_ !== "undefined" && _UR_WIZARD_; /** * Create the HTML block for the pages to be installed. @@ -59,62 +33,28 @@ const InstallPage = () => { const createInstallPageBox = (page, slug) => { return ( - - + + {slug === "registration_page" ? __("Registration Page", "user-registration") : __("My Account Page", "user-registration")} - {page.status !== "not_installed" && ( - - {page.slug} - - )} + + {"/" + page} + - {page.status === "installing" ? ( - - - {__("Installing...", "user-registration")} - - - - {counter} % - - - - ) : ( - page.status === "installed" && ( - - {__("Installed", "user-registration")} - - ) - )} + + {__("Installed", "user-registration")} + ); @@ -152,14 +92,8 @@ const InstallPage = () => { - {createInstallPageBox( - installPage.registration_page, - "registration_page" - )} - {createInstallPageBox( - installPage.my_account_page, - "my_account_page" - )} + {createInstallPageBox(registrationPageSlug, "registration_page")} + {createInstallPageBox(myAccountPageSlug, "my_account_page")} ); };