'GET', 'callback' => array( $this, 'auth_self_test' ), 'permission_callback' => '__return_true', ) ); } public function auth_self_test() { $authorization = isset( $_SERVER['HTTP_AUTHORIZATION'] ) && '' !== $_SERVER['HTTP_AUTHORIZATION']; $php_auth = isset( $_SERVER['PHP_AUTH_USER'] ) && '' !== $_SERVER['PHP_AUTH_USER']; $response = rest_ensure_response( array( 'authorizationHeaderReceived' => $authorization || $php_auth, 'authenticated' => is_user_logged_in(), 'hasWoocommerce' => class_exists( 'WooCommerce' ), 'applicationPasswordsAvailable' => function_exists( 'wp_is_application_passwords_available' ) && wp_is_application_passwords_available(), ) ); // The answer depends on the caller's credentials, so host caches must not keep it. $response->header( 'Cache-Control', 'no-store, private, max-age=0' ); return $response; } private function is_connector_password( $name ) { return 0 === strpos( $name, $this->app_name ); } private function clear_password_rotation_lock( $rotation_id ) { $lock = get_option( $this->rotation_lock_option ); if ( is_array( $lock ) && isset( $lock['id'] ) && $lock['id'] === $rotation_id ) { delete_option( $this->rotation_lock_option ); } } /** * Check if current user has all required capabilities * * @return array|true Returns true if all capabilities present, or array of missing capabilities */ private function has_required_capabilities() { $required_caps = [ 'manage_options' => 'Administrative plugin access', 'edit_posts' => 'Create and edit blog posts', 'upload_files' => 'Upload images and media', 'publish_posts' => 'Publish posts to website' ]; $missing_caps = []; foreach ($required_caps as $cap => $description) { if (!current_user_can($cap)) { $missing_caps[$cap] = $description; } } return empty($missing_caps) ? true : $missing_caps; } /** * Generate detailed error message for missing capabilities * * @param array $missing_caps Array of missing capabilities * @return string Formatted error message */ private function format_capability_error($missing_caps) { $all_caps = [ 'edit_posts' => 'Create and edit blog posts', 'upload_files' => 'Upload images and media', 'publish_posts' => 'Publish posts to website' ]; $message = esc_html__('Insufficient permissions. You need the following capabilities to use Agents24x7:', 'agents24x7') . "\n\n"; foreach ($all_caps as $cap => $description) { $status = isset($missing_caps[$cap]) ? '✗ MISSING' : '✓'; $message .= "• {$description} ({$cap}) - {$status}\n"; } $message .= "\n" . esc_html__('Please contact your site administrator to grant the missing permissions.', 'agents24x7'); return $message; } /** * Check if application password exists */ public function has_app_password() { $user_id = get_current_user_id(); if (!$user_id) { return false; } $existing_passwords = WP_Application_Passwords::get_user_application_passwords($user_id); foreach ($existing_passwords as $password) { if ($this->is_connector_password( $password['name'] )) { return true; } } return false; } /** * Create or get Application Password for current user */ public function get_or_create_app_password() { $user_id = get_current_user_id(); if (!$user_id) { return new WP_Error('no_user', 'No authenticated user'); } // Check if user has all required capabilities $capability_check = $this->has_required_capabilities(); if ($capability_check !== true) { $error_message = $this->format_capability_error($capability_check); return new WP_Error('insufficient_capabilities', $error_message); } // Check if WordPress supports and allows Application Passwords. if (!class_exists('WP_Application_Passwords') || ( function_exists( 'wp_is_application_passwords_available' ) && ! wp_is_application_passwords_available() )) { return new WP_Error('no_app_passwords', 'WordPress Application Passwords not available'); } // Keep existing credentials live until the platform has saved and // tested this one. A unique name lets the follow-up AJAX call identify // exactly the credential to keep or discard. $rotation_id = wp_generate_uuid4(); $lock = get_option( $this->rotation_lock_option ); if ( is_array( $lock ) && isset( $lock['created'] ) && time() - (int) $lock['created'] < 300 ) { return new WP_Error( 'rotation_in_progress', 'A WordPress credential update is already in progress. Please wait a few minutes and try again.' ); } if ( false !== $lock ) { delete_option( $this->rotation_lock_option ); } if ( ! add_option( $this->rotation_lock_option, array( 'id' => $rotation_id, 'created' => time() ), '', false ) ) { return new WP_Error( 'rotation_in_progress', 'A WordPress credential update is already in progress. Please wait a few minutes and try again.' ); } $app_password = WP_Application_Passwords::create_new_application_password($user_id, [ 'name' => $this->app_name . ' ' . $rotation_id, ]); if (is_wp_error($app_password)) { delete_option( $this->rotation_lock_option ); return $app_password; } // Return the plain text password (only available during creation) return [ 'password' => $app_password[0], 'username' => wp_get_current_user()->user_login, 'site_url' => home_url(), 'rotationId' => $rotation_id, ]; } public function finish_password_rotation( $rotation_id, $keep_new ) { if ( ! class_exists( 'WP_Application_Passwords' ) || ! current_user_can( 'manage_options' ) ) { return new WP_Error( 'rotation_forbidden', 'You cannot update Agents24x7 application passwords.' ); } $user_id = get_current_user_id(); $passwords = WP_Application_Passwords::get_user_application_passwords( $user_id ); $prefix = $this->app_name . ' ' . $rotation_id; $found = false; foreach ( $passwords as $password ) { if ( $password['name'] === $prefix ) { $found = true; break; } } if ( ! $found && $keep_new ) { $this->clear_password_rotation_lock( $rotation_id ); return new WP_Error( 'rotation_not_found', 'The new Agents24x7 application password could not be confirmed.' ); } foreach ( $passwords as $password ) { if ( $password['name'] === $prefix ) { if ( ! $keep_new || ! $this->is_connector_password( $password['name'] ) ) { WP_Application_Passwords::delete_application_password( $user_id, $password['uuid'] ); } } elseif ( $keep_new && $this->is_connector_password( $password['name'] ) ) { WP_Application_Passwords::delete_application_password( $user_id, $password['uuid'] ); } } $this->clear_password_rotation_lock( $rotation_id ); return true; } public function release_password_rotation_lock( $rotation_id ) { if ( ! current_user_can( 'manage_options' ) ) { return new WP_Error( 'rotation_forbidden', 'You cannot update Agents24x7 application passwords.' ); } $this->clear_password_rotation_lock( $rotation_id ); return true; } /** * Check if WooCommerce is installed and active */ public function check_for_woocommerce() { if (!defined('WC_VERSION')) { return false; } else { if (version_compare(WC_VERSION, '3.0', '>=')) { return true; } return false; } } /** * Get WooCommerce information */ public function get_woocommerce_info() { if (!$this->check_for_woocommerce()) { return [ 'available' => false, 'version' => null, ]; } return [ 'available' => true, 'version' => WC_VERSION, 'store_currency' => get_woocommerce_currency(), 'store_country' => WC()->countries->get_base_country(), ]; } /** * Get WordPress credentials for Convex integration */ public function get_wordpress_credentials() { $app_password_data = $this->get_or_create_app_password(); if (is_wp_error($app_password_data)) { return $app_password_data; } return [ 'rotationId' => $app_password_data['rotationId'], 'baseUrl' => home_url(), 'username' => $app_password_data['username'], 'password' => $app_password_data['password'], ]; } /** * Disconnect entire site from Agents24x7 (site-level operation) * Removes ALL Application Passwords for this plugin across all users */ public function disconnect_site() { if (!class_exists('WP_Application_Passwords')) { return true; } // Get all users and remove plugin's Application Passwords $users = get_users(['fields' => 'ID']); foreach ($users as $user_id) { $existing_passwords = WP_Application_Passwords::get_user_application_passwords($user_id); foreach ($existing_passwords as $password) { if ($this->is_connector_password( $password['name'] )) { WP_Application_Passwords::delete_application_password($user_id, $password['uuid']); } } } return true; } }