1, 'register_enabled' => 1, ]; $data = get_option( self::OPTION_KEY, [] ); return wp_parse_args( is_array( $data ) ? $data : [], $defaults ); } /** * Persist auth settings. * * @param array $data Settings. */ public static function save_settings( array $data ) { $current = self::settings(); update_option( self::OPTION_KEY, [ 'login_enabled' => ! empty( $data['login_enabled'] ) ? 1 : 0, 'register_enabled' => ! empty( $data['register_enabled'] ) ? 1 : 0, ], false ); unset( $current ); } /** * Process login / register POSTs from shortcode forms. */ public function maybe_handle_post() { if ( empty( $_POST['phf_auth_action'] ) ) { return; } $action = sanitize_key( wp_unslash( $_POST['phf_auth_action'] ) ); if ( 'login' === $action ) { $this->handle_login(); } elseif ( 'register' === $action ) { $this->handle_register(); } } /** * Login shortcode output. * * @return string */ public function shortcode_login() { $settings = self::settings(); if ( empty( $settings['login_enabled'] ) ) { return '

' . esc_html__( 'The login shortcode is disabled in PhemightForms settings.', 'phemightforms' ) . '

'; } if ( is_user_logged_in() ) { return '

' . esc_html__( 'You are already logged in.', 'phemightforms' ) . '

'; } $error = isset( $_GET['phf_auth_error'] ) ? sanitize_text_field( wp_unslash( $_GET['phf_auth_error'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended ob_start(); ?>

' . esc_html__( 'The registration shortcode is disabled in PhemightForms settings.', 'phemightforms' ) . '

'; } if ( ! get_option( 'users_can_register' ) ) { return '

' . esc_html__( 'User registration is disabled in WordPress settings.', 'phemightforms' ) . '

'; } if ( is_user_logged_in() ) { return '

' . esc_html__( 'You are already logged in.', 'phemightforms' ) . '

'; } $error = isset( $_GET['phf_auth_error'] ) ? sanitize_text_field( wp_unslash( $_GET['phf_auth_error'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $success = isset( $_GET['phf_auth_registered'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ob_start(); ?>

isset( $_POST['log'] ) ? sanitize_text_field( wp_unslash( $_POST['log'] ) ) : '', 'user_password' => isset( $_POST['pwd'] ) ? (string) wp_unslash( $_POST['pwd'] ) : '', // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- passwords must not be altered by sanitization; validated as a string, passed only to wp_signon(), never stored or output. 'remember' => ! empty( $_POST['rememberme'] ), ]; $user = wp_signon( $creds, is_ssl() ); if ( is_wp_error( $user ) ) { wp_safe_redirect( add_query_arg( 'phf_auth_error', rawurlencode( $user->get_error_message() ), wp_get_referer() ? wp_get_referer() : home_url() ) ); exit; } wp_safe_redirect( apply_filters( 'phemightforms_login_redirect', admin_url(), $user ) ); exit; } /** * Handle front-end registration. */ private function handle_register() { if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'phemightforms_auth_register' ) ) { return; } $redirect = wp_get_referer() ? wp_get_referer() : home_url(); if ( ! get_option( 'users_can_register' ) ) { wp_safe_redirect( add_query_arg( 'phf_auth_error', rawurlencode( __( 'Registration is disabled.', 'phemightforms' ) ), $redirect ) ); exit; } $login = isset( $_POST['user_login'] ) ? sanitize_user( wp_unslash( $_POST['user_login'] ), true ) : ''; $email = isset( $_POST['user_email'] ) ? sanitize_email( wp_unslash( $_POST['user_email'] ) ) : ''; $pass = isset( $_POST['user_pass'] ) ? (string) wp_unslash( $_POST['user_pass'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- passwords must not be altered by sanitization; validated as a string, passed only to wp_create_user() which hashes it, never stored or output. $user_id = wp_create_user( $login, $pass, $email ); if ( is_wp_error( $user_id ) ) { wp_safe_redirect( add_query_arg( 'phf_auth_error', rawurlencode( $user_id->get_error_message() ), $redirect ) ); exit; } wp_safe_redirect( add_query_arg( 'phf_auth_registered', '1', $redirect ) ); exit; } /** * Ensure starter login/register Form posts exist; return their IDs. * * @return array{login:int,register:int} */ public static function ensure_starter_forms() { $ids = [ 'login' => (int) get_option( 'phemightforms_auth_login_form', 0 ), 'register' => (int) get_option( 'phemightforms_auth_register_form', 0 ), ]; if ( $ids['login'] && ( new Form( $ids['login'] ) )->exists() && $ids['register'] && ( new Form( $ids['register'] ) )->exists() ) { return $ids; } if ( ! $ids['login'] || ! ( new Form( $ids['login'] ) )->exists() ) { $config = Form::default_config(); $config['fields'] = [ [ 'id' => 1, 'type' => 'text', 'label' => __( 'Username or email', 'phemightforms' ), 'required' => 1, 'placeholder' => '', ], [ 'id' => 2, 'type' => 'text', 'label' => __( 'Password', 'phemightforms' ), 'required' => 1, 'placeholder' => '', ], ]; $config['_next_id'] = 3; $config['settings']['submit_text'] = __( 'Log in', 'phemightforms' ); $config['settings']['confirmation'] = __( 'Welcome back!', 'phemightforms' ); $ids['login'] = Form::save( __( 'Login Form (Starter)', 'phemightforms' ), $config, 0 ); update_option( 'phemightforms_auth_login_form', $ids['login'], false ); } if ( ! $ids['register'] || ! ( new Form( $ids['register'] ) )->exists() ) { $config = Form::default_config(); $config['fields'] = [ [ 'id' => 1, 'type' => 'text', 'label' => __( 'Username', 'phemightforms' ), 'required' => 1, ], [ 'id' => 2, 'type' => 'email', 'label' => __( 'Email', 'phemightforms' ), 'required' => 1, ], [ 'id' => 3, 'type' => 'text', 'label' => __( 'Password', 'phemightforms' ), 'required' => 1, ], ]; $config['_next_id'] = 4; $config['settings']['submit_text'] = __( 'Register', 'phemightforms' ); $config['settings']['confirmation'] = __( 'Account created. You can log in now.', 'phemightforms' ); $ids['register'] = Form::save( __( 'Registration Form (Starter)', 'phemightforms' ), $config, 0 ); update_option( 'phemightforms_auth_register_form', $ids['register'], false ); } return $ids; } }