222 lines
5.5 KiB
PHP
222 lines
5.5 KiB
PHP
<?php
|
||
/**
|
||
* Frontend profile shortcode.
|
||
*
|
||
* @package Robotstxt_2FA
|
||
*/
|
||
|
||
namespace Robotstxt\TwoFA\User;
|
||
|
||
if ( ! defined( 'ABSPATH' ) ) {
|
||
exit;
|
||
}
|
||
|
||
/**
|
||
* Registers the [robotstxt_2fa_profile] shortcode and processes its form
|
||
* submissions on the frontend without requiring wp-admin access.
|
||
*
|
||
* Usage:
|
||
* [robotstxt_2fa_profile]
|
||
* [robotstxt_2fa_profile user_id="42" redirect="https://example.com/account"]
|
||
*
|
||
* Attributes:
|
||
* user_id – Optional. Defaults to the current user. Admins can specify another ID.
|
||
* redirect – Optional. URL to redirect to after a successful save.
|
||
*/
|
||
class Frontend_Profile {
|
||
/**
|
||
* Profile settings handler.
|
||
*
|
||
* @var Profile_Settings
|
||
*/
|
||
private Profile_Settings $profile_settings;
|
||
|
||
/**
|
||
* Constructor.
|
||
*/
|
||
public function __construct() {
|
||
$this->profile_settings = new Profile_Settings();
|
||
}
|
||
|
||
/**
|
||
* Register WordPress hooks.
|
||
*
|
||
* @since 1.0.0
|
||
*
|
||
* @return void
|
||
*/
|
||
public function register_hooks(): void {
|
||
add_shortcode( 'robotstxt_2fa_profile', array( $this, 'render_shortcode' ) );
|
||
add_action( 'init', array( $this, 'maybe_handle_frontend_save' ) );
|
||
add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_assets' ) );
|
||
}
|
||
|
||
/**
|
||
* Process a frontend profile form POST before page output.
|
||
*
|
||
* Fires on the `init` hook so the save completes and settings errors are
|
||
* populated before the shortcode renders on the same request.
|
||
*
|
||
* @since 1.0.0
|
||
*
|
||
* @return void
|
||
*/
|
||
public function maybe_handle_frontend_save(): void {
|
||
// On wp-admin pages the profile form is already handled by the
|
||
// personal_options_update / edit_user_profile_update hooks that fire
|
||
// later in the admin bootstrap (when all admin includes are loaded).
|
||
// Running here too would call save_profile_settings() twice and
|
||
// trigger a fatal because add_settings_error() is not yet available
|
||
// at init time in admin context.
|
||
if ( is_admin() ) {
|
||
return;
|
||
}
|
||
|
||
if ( 'POST' !== $_SERVER['REQUEST_METHOD'] ) {
|
||
return;
|
||
}
|
||
|
||
if ( ! isset( $_POST[ Profile_Settings::NONCE_FIELD ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Full nonce verification happens inside save_profile_settings().
|
||
return;
|
||
}
|
||
|
||
if ( ! is_user_logged_in() ) {
|
||
return;
|
||
}
|
||
|
||
$this->profile_settings->save_profile_settings( get_current_user_id() );
|
||
}
|
||
|
||
/**
|
||
* Render the [robotstxt_2fa_profile] shortcode.
|
||
*
|
||
* @since 1.0.0
|
||
*
|
||
* @param mixed $atts Shortcode attributes.
|
||
*
|
||
* @return string
|
||
*/
|
||
public function render_shortcode( $atts ): string {
|
||
// Content filters (including shortcodes) also run on REST requests,
|
||
// e.g. when the editor autosaves a post that mentions the shortcode
|
||
// in its text. Rendering there is useless and has side effects
|
||
// (such as generating an OTP secret), so bail out early.
|
||
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
|
||
return '';
|
||
}
|
||
|
||
if ( ! is_user_logged_in() ) {
|
||
return '';
|
||
}
|
||
|
||
$atts = shortcode_atts(
|
||
array(
|
||
'user_id' => (string) get_current_user_id(),
|
||
'redirect' => '',
|
||
),
|
||
is_array( $atts ) ? $atts : array(),
|
||
'robotstxt_2fa_profile'
|
||
);
|
||
|
||
$user_id = absint( $atts['user_id'] );
|
||
$redirect = esc_url_raw( $atts['redirect'] );
|
||
|
||
if ( ! current_user_can( 'edit_user', $user_id ) ) {
|
||
return '';
|
||
}
|
||
|
||
$user = get_user_by( 'ID', $user_id );
|
||
|
||
if ( ! $user instanceof \WP_User ) {
|
||
return '';
|
||
}
|
||
|
||
// settings_errors() and submit_button() are defined in wp-admin
|
||
// includes that are not loaded on frontend or REST requests (the
|
||
// shortcode also renders when content filters run on REST autosaves).
|
||
// Load them on demand.
|
||
if ( ! function_exists( 'settings_errors' ) ) {
|
||
require_once ABSPATH . 'wp-admin/includes/misc.php';
|
||
}
|
||
if ( ! function_exists( 'submit_button' ) ) {
|
||
require_once ABSPATH . 'wp-admin/includes/template.php';
|
||
}
|
||
|
||
ob_start();
|
||
|
||
/**
|
||
* Filter the CSS class applied to the frontend profile container.
|
||
*
|
||
* @since 1.0.0
|
||
*
|
||
* @param string $class Default container class.
|
||
*/
|
||
$wrapper_class = (string) apply_filters( 'robotstxt_2fa_profile_wrapper_class', 'robotstxt-2fa-frontend' );
|
||
|
||
settings_errors( 'robotstxt-2fa' );
|
||
?>
|
||
<div class="<?php echo esc_attr( $wrapper_class ); ?>">
|
||
<form method="post" action="">
|
||
<?php
|
||
if ( '' !== $redirect ) {
|
||
printf(
|
||
'<input type="hidden" name="robotstxt_2fa_frontend_redirect" value="%s" />',
|
||
esc_attr( $redirect )
|
||
);
|
||
}
|
||
$this->profile_settings->render_profile_section( $user );
|
||
submit_button( __( 'Save', 'robotstxt-2fa' ) );
|
||
?>
|
||
</form>
|
||
</div>
|
||
<?php
|
||
|
||
return (string) ob_get_clean();
|
||
}
|
||
|
||
/**
|
||
* Enqueue profile assets on pages that contain the shortcode.
|
||
*
|
||
* @since 1.0.0
|
||
*
|
||
* @return void
|
||
*/
|
||
public function maybe_enqueue_assets(): void {
|
||
/**
|
||
* Current post. Typed for PHPStan level max.
|
||
*
|
||
* @var \WP_Post|string|null $post
|
||
*/
|
||
global $post;
|
||
|
||
if ( ! $post instanceof \WP_Post || ! has_shortcode( $post->post_content, 'robotstxt_2fa_profile' ) ) {
|
||
return;
|
||
}
|
||
|
||
wp_enqueue_style(
|
||
'robotstxt-2fa-profile',
|
||
ROBOTSTXT_2FA_URL . 'assets/css/profile.css',
|
||
array(),
|
||
ROBOTSTXT_2FA_VERSION
|
||
);
|
||
|
||
wp_enqueue_script(
|
||
'robotstxt-2fa-recovery',
|
||
ROBOTSTXT_2FA_URL . 'assets/js/recovery-codes.js',
|
||
array(),
|
||
ROBOTSTXT_2FA_VERSION,
|
||
true
|
||
);
|
||
|
||
wp_localize_script(
|
||
'robotstxt-2fa-recovery',
|
||
'robotstxt2FARecovery',
|
||
array(
|
||
'strings' => array(
|
||
'copyConfirm' => __( 'The recovery codes have been copied to your clipboard.', 'robotstxt-2fa' ),
|
||
'copyFallback' => __( 'Copying failed. Please copy the codes manually.', 'robotstxt-2fa' ),
|
||
),
|
||
)
|
||
);
|
||
}
|
||
}
|