robotstxt-2fa/includes/user/class-frontend-profile.php
2026-06-05 16:30:07 +00:00

188 lines
4.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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 {
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 {
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 '';
}
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 {
global $post;
if ( ! is_a( $post, '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' ),
),
)
);
}
}