|null */ protected ?array $grant = null; /** * Set up. */ public function __construct() { $this->id = 'agentadapt_agent_connected'; $this->customer_email = true; $this->title = __( 'Personal assistant connected', 'agent-adapt' ); $this->description = __( 'Sent to a customer when they connect a personal assistant (an AI agent) to their account.', 'agent-adapt' ); $this->placeholders = array( '{agent}' => '' ); parent::__construct(); } /** * Default subject. */ public function get_default_subject() { return __( 'An assistant was connected to your {site_title} account', 'agent-adapt' ); } /** * Default heading. */ public function get_default_heading() { return __( 'Personal assistant connected', 'agent-adapt' ); } /** * Send for a grant. * * @param string $grant_id Grant. */ public function trigger( $grant_id ) { $grant = AgentAdapt_Grants::get( (string) $grant_id ); $user = $grant ? get_userdata( (int) $grant['user_id'] ) : false; if ( ! $grant || ! $user || ! $this->is_enabled() ) { return; } $this->setup_locale(); $agent = AgentAdapt_Agents::row( (string) $this->client_id( $grant ) ); $this->grant = $grant + array( 'agent_name' => (string) ( $agent['name'] ?? '' ), 'client_id' => (string) ( $agent['client_id'] ?? '' ) ); $this->recipient = $user->user_email; $this->placeholders['{agent}'] = '' !== $this->grant['agent_name'] ? $this->grant['agent_name'] : (string) wp_parse_url( $this->grant['client_id'], PHP_URL_HOST ); if ( $this->get_recipient() ) { $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); } $this->restore_locale(); } /** * HTML body inside WooCommerce's own header and footer. */ public function get_content_html() { return wc_get_template_html( 'emails/email-header.php', array( 'email_heading' => $this->get_heading(), 'email' => $this ) ) . wpautop( esc_html( $this->body() ) ) . wc_get_template_html( 'emails/email-footer.php', array( 'email' => $this ) ); } /** * Plain-text body. */ public function get_content_plain() { return $this->body(); } /** * The message. */ private function body(): string { $g = (array) $this->grant; $domain = (string) wp_parse_url( (string) ( $g['client_id'] ?? '' ), PHP_URL_HOST ); $desc = AgentAdapt_Signin::descriptions(); $can = array_map( static fn( $s ) => '- ' . ( $desc[ $s ] ?? $s ), array_filter( explode( ' ', (string) ( $g['scope'] ?? '' ) ) ) ); return sprintf( /* translators: 1: agent name, 2: agent domain, 3: store name, 4: list of permissions, 5: account page URL */ __( "%1\$s (%2\$s) can now act for you on your %3\$s account. It can:\n\n%4\$s\n\nIf this wasn't you, disconnect it and change your password: %5\$s", 'agent-adapt' ), $this->placeholders['{agent}'], $domain, $this->get_blogname(), implode( "\n", $can ), wc_get_account_endpoint_url( AgentAdapt_My_Account::ENDPOINT ) ); } /** * client_id for a grant's agent hash. * * @param array $grant Grant. */ private function client_id( array $grant ): string { global $wpdb; return (string) $wpdb->get_var( $wpdb->prepare( 'SELECT client_id FROM %i WHERE client_hash = %s', AgentAdapt_Schema::table( 'agents' ), (string) $grant['client_hash'] ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } }