db = $db; self::install_defaults(); add_action( 'admin_menu', array( $this, 'register_admin_page' ) ); add_action( 'admin_post_divi_powers_ai_connector_save', array( $this, 'save' ) ); add_action( 'admin_post_divi_powers_ai_connector_revoke', array( $this, 'revoke' ) ); } public static function defaults() { return array( 'enabled' => false, 'customer_connection_enabled' => false, 'transport_mode' => 'standalone', 'mcp_bearer_token' => '', 'mcp_role' => 'readonly', 'customer_mcp_role' => 'readonly', 'operator_tools_enabled' => false, ); } public static function install_defaults() { $saved = get_option( self::OPTION, false ); if ( false === $saved ) { add_option( self::OPTION, self::defaults(), '', false ); return; } $saved = is_array( $saved ) ? $saved : array(); $next = wp_parse_args( $saved, self::defaults() ); $next['transport_mode'] = 'standalone'; if ( $next !== $saved ) { update_option( self::OPTION, $next, false ); } } public function all() { $saved = get_option( self::OPTION, array() ); $saved = is_array( $saved ) ? $saved : array(); $settings = wp_parse_args( $saved, self::defaults() ); $settings['transport_mode'] = 'standalone'; $settings['customer_mcp_role'] = 'readwrite' === $settings['customer_mcp_role'] ? 'readwrite' : 'readonly'; $settings['operator_tools_enabled'] = ! empty( $settings['operator_tools_enabled'] ); return $settings; } public function get( $key, $fallback = null ) { $settings = $this->all(); return array_key_exists( $key, $settings ) ? $settings[ $key ] : $fallback; } public function enabled() { return (bool) $this->get( 'customer_connection_enabled', false ); } public function register_admin_page() { add_submenu_page( 'divi-powers', __( 'AI Connection', '5d-powers-for-divi' ), __( 'AI Connection', '5d-powers-for-divi' ), 'manage_options', 'divi-powers-ai-connector', array( $this, 'render' ) ); } public function save() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to configure this connector.', '5d-powers-for-divi' ) ); } check_admin_referer( 'divi_powers_ai_connector_save' ); $settings = get_option( self::OPTION, array() ); $settings = is_array( $settings ) ? $settings : array(); $role = isset( $_POST['connection_role'] ) ? sanitize_key( wp_unslash( $_POST['connection_role'] ) ) : 'readonly'; $settings['customer_connection_enabled'] = ! empty( $_POST['connection_enabled'] ); $settings['enabled'] = $settings['customer_connection_enabled']; $settings['customer_mcp_role'] = 'readwrite' === $role ? 'readwrite' : 'readonly'; $settings['transport_mode'] = 'standalone'; $settings['mcp_bearer_token'] = ''; $settings['operator_tools_enabled'] = class_exists( 'Divi_Powers_Operator_MCP' ) && ! empty( $_POST['operator_tools_enabled'] ); update_option( self::OPTION, $settings, false ); $this->db->audit( 'divi_powers_ai_connector', 'settings', home_url( '/' ), 'success' ); $this->redirect( 'saved' ); } public function revoke() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to revoke connections.', '5d-powers-for-divi' ) ); } check_admin_referer( 'divi_powers_ai_connector_revoke' ); global $wpdb; foreach ( array( 'fived_mcp_oauth_tokens', 'mwai_mcp_oauth_tokens' ) as $suffix ) { $table = $wpdb->prefix . $suffix; if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) === $table ) { $wpdb->query( $wpdb->prepare( 'UPDATE %i SET revoked = %d WHERE revoked <> %d', $table, 1, 1 ) ); } } $this->db->audit( 'divi_powers_ai_connector', 'revoke', home_url( '/' ), 'success' ); $this->redirect( 'revoked' ); } private function redirect( $notice ) { wp_safe_redirect( add_query_arg( array( 'page' => 'divi-powers-ai-connector', 'notice' => sanitize_key( $notice ), ), admin_url( 'admin.php' ) ) ); exit; } public function render() { if ( ! current_user_can( 'manage_options' ) ) { return; } $endpoint = rest_url( 'mcp/v1/http' ); $role = (string) $this->get( 'customer_mcp_role', 'readonly' ); $status = $this->connection_status(); $notice = isset( $_GET['notice'] ) ? sanitize_key( wp_unslash( $_GET['notice'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only display state. ?>

enabled() ) : ?>

↗

↗

prefix . $suffix; if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) === $table ) { $count = max( $count, (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE revoked <> 1', $table ) ) ); } } return array( 'oauth_grants' => $count ); } }