__( 'You do not have permission to perform this action.', 'abp-writer-ai-blog-generator' ), ) ); } // Sanitize input. $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; $password = isset( $_POST['password'] ) ? sanitize_text_field( wp_unslash( $_POST['password'] ) ) : ''; $full_name = isset( $_POST['full_name'] ) ? sanitize_text_field( wp_unslash( $_POST['full_name'] ) ) : ''; $confirm_password = isset( $_POST['confirm_password'] ) ? sanitize_text_field( wp_unslash( $_POST['confirm_password'] ) ) : ''; // Validate. if ( empty( $full_name ) ) { wp_send_json_error( array( 'message' => __( 'Please enter your full name.', 'abp-writer-ai-blog-generator' ), ) ); } if ( empty( $email ) || ! is_email( $email ) ) { wp_send_json_error( array( 'message' => __( 'Please enter a valid email address.', 'abp-writer-ai-blog-generator' ), ) ); } if ( empty( $password ) || strlen( $password ) < 8 ) { wp_send_json_error( array( 'message' => __( 'Password must be at least 8 characters.', 'abp-writer-ai-blog-generator' ), ) ); } if ( $password !== $confirm_password ) { wp_send_json_error( array( 'message' => __( 'Passwords do not match.', 'abp-writer-ai-blog-generator' ), ) ); } // Call the API. $response = ABPW_Api_Client::post_unauthenticated( '/auth/register', array( 'email' => $email, 'password' => $password, 'confirm_password' => $confirm_password, 'full_name' => $full_name, 'scope' => 'full_free', 'site_url' => get_site_url(), ) ); if ( is_wp_error( $response ) ) { $error_data = $response->get_error_data(); $status = isset( $error_data['status'] ) ? $error_data['status'] : 500; $send_data = array( 'message' => $response->get_error_message(), ); // Mark 409 as email_exists for special handling in JS. if ( 409 === $status ) { $send_data['code'] = 'email_exists'; } wp_send_json_error( $send_data ); } // Handle pending confirmation response. if ( isset( $response['status'] ) && 'pending_confirmation' === $response['status'] ) { wp_send_json_success( array( 'status' => 'pending_confirmation', 'email' => $email, ) ); } // Backward compat: if API returns api_key directly, store and redirect. $this->store_credentials( $response ); wp_send_json_success( array( 'redirect' => admin_url( 'admin.php?page=abp-writer-onboarding' ), ) ); } /** * Handle login AJAX request. */ public function handle_login() { // Verify nonce. check_ajax_referer( 'abpw_auth_nonce', 'abpw_nonce' ); // Check capability. if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'abp-writer-ai-blog-generator' ), ) ); } // Sanitize input. $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; $password = isset( $_POST['password'] ) ? sanitize_text_field( wp_unslash( $_POST['password'] ) ) : ''; // Validate. if ( empty( $email ) || ! is_email( $email ) ) { wp_send_json_error( array( 'message' => __( 'Please enter a valid email address.', 'abp-writer-ai-blog-generator' ), ) ); } if ( empty( $password ) || strlen( $password ) < 8 ) { wp_send_json_error( array( 'message' => __( 'Password must be at least 8 characters.', 'abp-writer-ai-blog-generator' ), ) ); } // Call the API. $response = ABPW_Api_Client::post_unauthenticated( '/auth/login', array( 'email' => $email, 'password' => $password, 'scope' => 'full_free', ) ); if ( is_wp_error( $response ) ) { wp_send_json_error( array( 'message' => $response->get_error_message(), ) ); } // Store credentials. $this->store_credentials( $response ); // Check if user already has a config on the server (reconnect scenario). $config_response = ABPW_Api_Client::get( '/config' ); if ( ! is_wp_error( $config_response ) && isset( $config_response['config'] ) && ! empty( $config_response['config'] ) ) { update_option( 'aiblogpress_onboarding_complete', true ); } // Determine redirect: onboarding or dashboard. $onboarding_complete = get_option( 'aiblogpress_onboarding_complete', false ); $redirect = $onboarding_complete ? admin_url( 'admin.php?page=abp-writer' ) : admin_url( 'admin.php?page=abp-writer-onboarding' ); wp_send_json_success( array( 'redirect' => $redirect, ) ); } /** * Handle confirm-status AJAX request (polling for email confirmation). */ public function handle_confirm_status() { // Verify nonce. check_ajax_referer( 'abpw_auth_nonce', 'abpw_nonce' ); // Check capability. if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'abp-writer-ai-blog-generator' ), ) ); } // Sanitize input. $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; if ( empty( $email ) || ! is_email( $email ) ) { wp_send_json_error( array( 'message' => __( 'Please enter a valid email address.', 'abp-writer-ai-blog-generator' ), ) ); } // Call the API. $response = ABPW_Api_Client::post_unauthenticated( '/auth/confirm-status', array( 'email' => $email, 'scope' => 'full_free', ) ); if ( is_wp_error( $response ) ) { wp_send_json_error( array( 'message' => $response->get_error_message(), ) ); } wp_send_json_success( $response ); } /** * Handle store-credentials AJAX request (after email confirmation). */ public function handle_store_credentials() { // Verify nonce. check_ajax_referer( 'abpw_auth_nonce', 'abpw_nonce' ); // Check capability. if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'abp-writer-ai-blog-generator' ), ) ); } // Sanitize input. $api_key = isset( $_POST['api_key'] ) ? sanitize_text_field( wp_unslash( $_POST['api_key'] ) ) : ''; $user_id = isset( $_POST['user_id'] ) ? sanitize_text_field( wp_unslash( $_POST['user_id'] ) ) : ''; $scope = isset( $_POST['scope'] ) ? sanitize_text_field( wp_unslash( $_POST['scope'] ) ) : ''; if ( empty( $api_key ) ) { wp_send_json_error( array( 'message' => __( 'API key is required.', 'abp-writer-ai-blog-generator' ), ) ); } // Store credentials. $this->store_credentials( array( 'api_key' => $api_key, 'user_id' => $user_id, 'scope' => $scope, ) ); wp_send_json_success( array( 'redirect' => admin_url( 'admin.php?page=abp-writer-onboarding' ), ) ); } /** * Handle resend confirmation email AJAX request. */ public function handle_resend_confirmation() { // Verify nonce. check_ajax_referer( 'abpw_auth_nonce', 'abpw_nonce' ); // Check capability. if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'abp-writer-ai-blog-generator' ), ) ); } // Sanitize input. $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; if ( empty( $email ) || ! is_email( $email ) ) { wp_send_json_error( array( 'message' => __( 'Please enter a valid email address.', 'abp-writer-ai-blog-generator' ), ) ); } // Call the API. $response = ABPW_Api_Client::post_unauthenticated( '/auth/resend-confirmation', array( 'email' => $email, ) ); if ( is_wp_error( $response ) ) { wp_send_json_error( array( 'message' => $response->get_error_message(), ) ); } wp_send_json_success( array( 'message' => __( 'Confirmation email sent! Check your inbox.', 'abp-writer-ai-blog-generator' ), ) ); } /** * Store API credentials in wp_options. * * @param array $response API response with api_key, user_id, scope. */ private function store_credentials( $response ) { if ( isset( $response['api_key'] ) ) { update_option( 'aiblogpress_api_key', sanitize_text_field( $response['api_key'] ) ); } if ( isset( $response['user_id'] ) ) { update_option( 'aiblogpress_user_id', sanitize_text_field( $response['user_id'] ) ); } if ( isset( $response['scope'] ) ) { update_option( 'aiblogpress_scope', sanitize_text_field( $response['scope'] ) ); } } }