Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UR-1825 Tweak - My account selection validation and install pages #763

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions assets/js/admin/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div class="ur-spinner is-active"></div>');

$.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(
"<div id='message' class='error inline' style='padding:10px;'>" +
response.responseJSON.data.message +
"</div>"
);
$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();
Expand Down
67 changes: 0 additions & 67 deletions includes/RestApi/controllers/version1/class-ur-getting-started.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
)
);
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
28 changes: 15 additions & 13 deletions includes/admin/class-ur-admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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' ),
Expand Down Expand Up @@ -690,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'] ) ) {
Expand Down
22 changes: 15 additions & 7 deletions includes/admin/class-ur-admin-welcome.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) ),
)
);

Expand Down
4 changes: 2 additions & 2 deletions includes/admin/settings/class-ur-settings-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '' ),
),
),
),
Expand Down
82 changes: 57 additions & 25 deletions includes/class-ur-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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();
1 change: 1 addition & 0 deletions includes/class-ur-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
7 changes: 7 additions & 0 deletions includes/functions-ur-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading
Loading