__( 'Connected agents', 'agent-adapt' ) ); add_action( 'woocommerce_account_' . self::ENDPOINT . '_endpoint', array( __CLASS__, 'render' ) ); add_action( 'template_redirect', array( __CLASS__, 'handle_disconnect' ), 5 ); add_action( 'init', array( __CLASS__, 'maybe_flush' ), 99 ); add_filter( 'woocommerce_email_classes', array( __CLASS__, 'email_class' ) ); add_action( 'agentadapt_agent_connected', array( __CLASS__, 'send_connected_email' ) ); } /** * Register the endpoint as a WooCommerce account endpoint (WooCommerce * adds the rewrite rule for it). * * @param array $vars Query vars. * @return array */ public static function query_vars( array $vars ): array { $vars[ self::ENDPOINT ] = self::ENDPOINT; return $vars; } /** * Flush rewrite rules once after the endpoint first appears. */ public static function maybe_flush(): void { if ( get_option( 'agentadapt_flush_rewrites' ) ) { delete_option( 'agentadapt_flush_rewrites' ); flush_rewrite_rules( false ); } } /** * Add the tab before Log out. * * @param array $items Menu items. * @return array */ public static function menu( array $items ): array { $out = array(); foreach ( $items as $k => $label ) { if ( 'customer-logout' === $k ) { $out[ self::ENDPOINT ] = __( 'Connected agents', 'agent-adapt' ); } $out[ $k ] = $label; } if ( ! isset( $out[ self::ENDPOINT ] ) ) { $out[ self::ENDPOINT ] = __( 'Connected agents', 'agent-adapt' ); } return $out; } /** * The tab's content. */ public static function render(): void { $user_id = get_current_user_id(); if ( $user_id < 1 ) { return; } $grants = AgentAdapt_Grants::for_user( $user_id ); $desc = AgentAdapt_Signin::descriptions(); echo '

' . esc_html__( 'Personal assistants you have allowed to act for you on this account. Disconnecting one ends its access straight away.', 'agent-adapt' ) . '

'; if ( ! $grants ) { echo '

' . esc_html__( 'No assistants are connected.', 'agent-adapt' ) . '

'; return; } echo ''; foreach ( array( __( 'Assistant', 'agent-adapt' ), __( 'It can', 'agent-adapt' ), __( 'Last used', 'agent-adapt' ), __( 'Access until', 'agent-adapt' ), '' ) as $h ) { echo ''; } echo ''; foreach ( $grants as $g ) { $domain = (string) wp_parse_url( (string) $g['client_id'], PHP_URL_HOST ); $can = array_map( static fn( $s ) => $desc[ $s ] ?? $s, array_filter( explode( ' ', (string) $g['scope'] ) ) ); echo ''; echo ''; echo ''; /* translators: %s: human time difference */ echo ''; echo ''; echo ''; echo ''; } echo '
' . esc_html( $h ) . '
' . esc_html( '' !== (string) $g['name'] ? (string) $g['name'] : $domain ) . '
' . esc_html( $domain ) . '
' . esc_html( implode( '; ', $can ) ) . '' . esc_html( sprintf( __( '%s ago', 'agent-adapt' ), human_time_diff( (int) $g['last_used'] ) ) ) . '' . esc_html( wp_date( get_option( 'date_format' ), (int) $g['expires'] ) ) . '
'; wp_nonce_field( 'agentadapt-disconnect', 'agentadapt_nonce' ); echo ''; echo '
'; } /** * Disconnect, POSTed from the tab. */ public static function handle_disconnect(): void { if ( 'POST' !== AgentAdapt_Http::method() || ! isset( $_POST['agentadapt_disconnect'] ) || ! is_user_logged_in() ) { return; } // The nonce first (it is per user and session); then the grant must be this customer's own. $nonce = isset( $_POST['agentadapt_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['agentadapt_nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'agentadapt-disconnect' ) ) { wc_add_notice( __( 'That assistant could not be disconnected. Reload the page and try again.', 'agent-adapt' ), 'error' ); wp_safe_redirect( wc_get_account_endpoint_url( self::ENDPOINT ) ); exit; } $grant_id = sanitize_text_field( wp_unslash( $_POST['agentadapt_disconnect'] ) ); $grant = AgentAdapt_Grants::get( $grant_id ); if ( ! $grant || (int) $grant['user_id'] !== get_current_user_id() ) { wc_add_notice( __( 'That assistant could not be disconnected. Reload the page and try again.', 'agent-adapt' ), 'error' ); } else { AgentAdapt_Grants::revoke( $grant_id, 'customer_disconnected' ); wc_add_notice( __( 'The assistant was disconnected.', 'agent-adapt' ) ); } wp_safe_redirect( wc_get_account_endpoint_url( self::ENDPOINT ) ); exit; } /** * Register the email with WooCommerce (WooCommerce → Settings → Emails). * * @param array $emails Email classes. * @return array */ public static function email_class( array $emails ): array { $emails['AgentAdapt_Email_Agent_Connected'] = new AgentAdapt_Email_Agent_Connected(); return $emails; } /** * Send "an agent was connected" to the customer. * * @param string $grant_id Grant. */ public static function send_connected_email( string $grant_id ): void { $emails = WC()->mailer()->get_emails(); if ( isset( $emails['AgentAdapt_Email_Agent_Connected'] ) ) { $emails['AgentAdapt_Email_Agent_Connected']->trigger( $grant_id ); } } }