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' ); ?>