description. * * @return array */ abstract protected function variables(): array; /** * Hook the event that sends (or rewrites) this email. * * @return void */ abstract public function register(): void; /** * When it's sent, shown under the title. * * @return string */ public function description(): string { return ''; } /** * Whether the header switch can turn this email off. False for emails * a site can't safely do without (password reset links). * * @return bool */ public function can_disable(): bool { return true; } /** * Whether a fresh install sends this email. * * @return bool */ public function enabled_by_default(): bool { return true; } /** * Whether the panel shows an editable "Send to" field. * * @return bool */ public function has_recipient_field(): bool { return false; } /** * Default for the "Send to" field. * * @return string */ public function default_recipient(): string { return (string) get_option( 'admin_email' ); } /** * Variables whose values are already HTML (inserted unescaped). * * @return string[] */ protected function html_variables(): array { return array(); } /** * Called by `EmailManager::add()`. * * @param string $tab Settings tab. * @param string $section Settings section. * @param string $owner Registering plugin. * @return void */ final public function attach( string $tab, string $section, string $owner ): void { $this->tab = $tab; $this->section = $section; $this->owner = $owner; } /** * The plugin that registered this email. * * @return string */ final public function owner(): string { return $this->owner; } /** * Every variable, including ones other plugins add, as * tag => [ 'description' => string, 'owner' => string ] (filtered, so a * consumer shouldn't assume either key is present). * * @return array> */ final public function all_variables(): array { $variables = array(); foreach ( $this->variables() as $tag => $description ) { $variables[ $tag ] = array( 'description' => $description, 'owner' => $this->owner, ); } /** * Filters the variables listed in an email's "Available variables" * box. Add entries as tag => [ 'description' => ..., 'owner' => ... ]; * supply the values themselves through `aiou_email_placeholders`. * * @param array $variables Variables so far. * @param Email $email The email. */ return (array) apply_filters( 'aiou_email_variables', $variables, $this ); } /** * Default stored value. * * @return array */ final public function defaults(): array { $defaults = array( 'enabled' => $this->enabled_by_default() || ! $this->can_disable() ? 1 : 0, 'subject' => $this->default_subject(), 'body' => $this->default_body(), ); if ( $this->has_recipient_field() ) { $defaults['recipient'] = $this->default_recipient(); } return $defaults; } /** * The stored value merged over the defaults. * * @return array */ final public function settings(): array { $stored = aiou_get_setting( $this->tab, $this->section, $this->id(), array() ); return array_merge( $this->defaults(), is_array( $stored ) ? $stored : array() ); } /** * Whether this email should be sent. * * @return bool */ final public function is_enabled(): bool { return ! $this->can_disable() || ! empty( $this->settings()['enabled'] ); } /** * Subject template (falls back to the default when blank). * * @return string */ final public function subject(): string { $subject = trim( (string) $this->settings()['subject'] ); return '' !== $subject ? $subject : $this->default_subject(); } /** * Body template (falls back to the default when blank). * * @return string */ final public function body(): string { $body = (string) $this->settings()['body']; return '' !== trim( wp_strip_all_tags( $body ) ) ? $body : $this->default_body(); } /** * Valid "Send to" addresses (comma-separated in settings). * * @return string[] */ final public function recipients(): array { $raw = $this->has_recipient_field() ? (string) $this->settings()['recipient'] : $this->default_recipient(); return array_values( array_filter( array_map( 'trim', explode( ',', $raw ) ), 'is_email' ) ); } /** * Build the final subject, HTML message, and headers. * * @param array $values Variable values, tag => value. * @param array $context Extra context for `aiou_email_placeholders` (e.g. 'user'). * @return array{subject:string,message:string,headers:string[]} */ final public function compose( array $values, array $context = array() ): array { /** * Filters the variable values for one email. Values a plugin's own * email already supplies should never be overridden -- add new * tags only. * * @param array $values Tag => value. * @param string $id Email id. * @param array $context Email-specific context. */ $values = (array) apply_filters( 'aiou_email_placeholders', $values, $this->id(), $context ); $html_tags = $this->html_variables(); $escaped = array(); foreach ( $values as $tag => $value ) { $escaped[ $tag ] = in_array( $tag, $html_tags, true ) ? (string) $value : esc_html( (string) $value ); } $subject = wp_specialchars_decode( Placeholders::apply( $this->subject(), $values ), ENT_QUOTES ); $body = make_clickable( wpautop( Placeholders::apply( $this->body(), $escaped ) ) ); $content = array( 'subject' => $subject, 'message' => self::layout( $body, $subject ), 'headers' => array( 'Content-Type: text/html; charset=UTF-8' ), ); /** * Filters a composed email right before it's handed to wp_mail(). * * @param array{subject:string,message:string,headers:string[]} $content The email. * @param Email $email The email definition. * @param array $values Variable values. * @param array $context Email-specific context. */ return (array) apply_filters( 'aiou_email_content', $content, $this, $values, $context ); } /** * Compose and send, if enabled. * * @param string|string[] $to Recipient(s). * @param array $values Variable values. * @param array $context Extra context. * @param bool $even_if_off Send while switched off, for an admin who asked for it on the spot. * @return bool Whether wp_mail() accepted it (false when disabled). */ final public function send( string|array $to, array $values, array $context = array(), bool $even_if_off = false ): bool { if ( ( ! $even_if_off && ! $this->is_enabled() ) || empty( $to ) ) { return false; } $email = $this->compose( $values, $context ); return wp_mail( $to, $email['subject'], $email['message'], $email['headers'] ); } /** * The settings field for this email. * * @param int $priority Ordering within its section. * @return Field */ final public function field( int $priority ): Field { return new Field( id: $this->id(), label: $this->title(), type: 'email', default: $this->defaults(), args: array( 'email' => $this, 'full_width' => true, ), sanitize: array( $this, 'sanitize' ), priority: $priority ); } /** * Sanitize a submitted/imported value. * * @param mixed $value Raw value. * @return array */ final public function sanitize( mixed $value ): array { $value = is_array( $value ) ? $value : array(); $clean = array( 'enabled' => $this->can_disable() ? ( empty( $value['enabled'] ) ? 0 : 1 ) : 1, 'subject' => sanitize_text_field( (string) ( $value['subject'] ?? '' ) ), 'body' => wp_kses_post( (string) ( $value['body'] ?? '' ) ), ); if ( $this->has_recipient_field() ) { $addresses = array_filter( array_map( 'trim', explode( ',', (string) ( $value['recipient'] ?? '' ) ) ), 'is_email' ); $clean['recipient'] = implode( ', ', array_map( 'sanitize_email', $addresses ) ); } return $clean; } /** * Wrap a body in the shared, minimal HTML email layout. * * @param string $body Body HTML. * @param string $subject Subject (used as the document title). * @return string */ public static function layout( string $body, string $subject ): string { /** * Filters the branded header shown above every managed email's body * (empty by default; Pro's Email Branding fills it). Full-width: * supply your own padding. * * @param string $header Header HTML. * @param string $subject Subject line. */ $header = (string) apply_filters( 'aiou_email_header_html', '', $subject ); /** * Filters the branded footer shown below every managed email's body * (empty by default; Pro's Email Branding fills it). Full-width: * supply your own padding. * * @param string $footer Footer HTML. * @param string $subject Subject line. */ $footer = (string) apply_filters( 'aiou_email_footer_html', '', $subject ); $html = '' . esc_html( $subject ) . '' . '' . '
' . $header . '
' . $body . '
' . $footer . '
'; /** * Filters the full HTML of an email built from a managed template. * * @param string $html Full HTML document. * @param string $body The body HTML inside the layout. * @param string $subject Subject line. */ return (string) apply_filters( 'aiou_email_html', $html, $body, $subject ); } }