label). * * @param string|null $doc A system email's type, or null for the one the * editor has open (its `doc` query arg). * @return array */ public static function groups( $doc = null ) { $groups = array( array( 'key' => 'subscriber', 'title' => __( 'Subscriber', 'mailpuff' ), 'tokens' => array( '{{first_name}}' => __( 'First name', 'mailpuff' ), '{{last_name}}' => __( 'Last name', 'mailpuff' ), '{{full_name}}' => __( 'Full name', 'mailpuff' ), '{{email}}' => __( 'Email address', 'mailpuff' ), '{{list_name}}' => __( 'List name', 'mailpuff' ), ), ), array( 'key' => 'business', 'title' => __( 'Business', 'mailpuff' ), 'tokens' => array( '{{business_name}}' => __( 'Business name', 'mailpuff' ), '{{business_address}}' => __( 'Business address', 'mailpuff' ), '{{site_name}}' => __( 'Site name', 'mailpuff' ), '{{site_url}}' => __( 'Website URL', 'mailpuff' ), ), ), array( 'key' => 'links', 'title' => __( 'Links', 'mailpuff' ), 'tokens' => array( '{{unsubscribe_url}}' => __( 'Unsubscribe link', 'mailpuff' ), '{{manage_subscription_url}}' => __( 'Manage subscription link', 'mailpuff' ), '{{web_url}}' => __( 'Web version link (when it is on)', 'mailpuff' ), ), ), array( 'key' => 'other', 'title' => __( 'Other', 'mailpuff' ), 'tokens' => array( '{{date}}' => __( "Today's date", 'mailpuff' ), '{{last_post_title}}' => __( 'Latest post title', 'mailpuff' ), '{{last_post_url}}' => __( 'Latest post link', 'mailpuff' ), ), ), ); // The owner's own fields, right after the subscriber's name and // address. `{{field:key|fallback}}` also works; the picker inserts // the plain tag. $fields = array(); foreach ( Mailpuff_Fields::all() as $field ) { $fields[ '{{field:' . $field['key'] . '}}' ] = $field['label']; } if ( $fields ) { array_splice( $groups, 1, 0, array( array( 'key' => 'fields', 'title' => __( 'Fields', 'mailpuff' ), 'tokens' => $fields, ), ) ); } // The confirm link exists only in the confirmation email, and each // automatic email has tags of its own, so only that email's editor // offers them. $doc = null === $doc ? self::doc() : (string) $doc; if ( in_array( $doc, array( 'confirmation', Mailpuff_Confirmation_Email::TYPE ), true ) || 1 === preg_match( '/^tag-confirm-\d+$/', $doc ) ) { $groups[] = array( 'key' => 'confirmation', 'title' => __( 'Confirmation', 'mailpuff' ), 'tokens' => array( '{{confirm_url}}' => __( 'Confirm subscription link', 'mailpuff' ), ), ); } if ( 'post' === $doc || 'digest' === $doc ) { $post = array( '{{post_title}}' => __( 'Post title', 'mailpuff' ), '{{post_excerpt}}' => __( 'Post excerpt', 'mailpuff' ), '{{post_url}}' => __( 'Post link', 'mailpuff' ), '{{post_image}}' => __( 'Featured image (alone in a paragraph)', 'mailpuff' ), '{{post_author}}' => __( 'Post author', 'mailpuff' ), '{{post_date}}' => __( 'Post date', 'mailpuff' ), ); // New products go out as new posts too (Mailpuff_WooCommerce). if ( class_exists( 'WooCommerce' ) ) { $post['{{product_price}}'] = __( 'Product price (products only)', 'mailpuff' ); } if ( 'digest' === $doc ) { $post = array( '{{posts}}' => __( 'The new posts (alone in a paragraph)', 'mailpuff' ), '{{post_count}}' => __( 'Number of new posts', 'mailpuff' ), '{{post_title}}' => __( 'Newest post title', 'mailpuff' ), '{{post_url}}' => __( 'Newest post link', 'mailpuff' ), ); } array_unshift( $groups, array( 'key' => 'post', 'title' => 'digest' === $doc ? __( 'Digest', 'mailpuff' ) : __( 'New post', 'mailpuff' ), 'tokens' => $post, ) ); } if ( 'owner_notice' === $doc ) { // It goes to the owner: the person who joined has tags of their // own, and there is no unsubscribe link to offer. $groups = array_values( array_filter( $groups, static fn( $group ) => ! in_array( $group['key'], array( 'subscriber', 'links' ), true ) ) ); array_unshift( $groups, array( 'key' => 'new_subscriber', 'title' => __( 'New subscriber', 'mailpuff' ), 'tokens' => array( '{{subscriber_name}}' => __( 'Their name', 'mailpuff' ), '{{subscriber_email}}' => __( 'Their email address', 'mailpuff' ), '{{subscriber_tags}}' => __( 'Their tags', 'mailpuff' ), '{{subscriber_url}}' => __( 'Their page in Mailpuff', 'mailpuff' ), ), ) ); } /** * Filter the available merge-tag groups. * * @param array $groups Grouped token list. */ $groups = apply_filters( 'mailpuff_smartcode_groups', $groups ); return $groups; } /** * The system email the editor now has open (`doc=`), or ''. * * @return string */ private static function doc() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended return isset( $_GET['doc'] ) ? sanitize_key( wp_unslash( $_GET['doc'] ) ) : ''; } /** * Values shared by every recipient — compute once per batch. * * @return array token => value. */ public static function global_values() { $brand = (array) get_option( 'mailpuff_brand_settings', array() ); $company = ! empty( $brand['company_name'] ) ? $brand['company_name'] : Mailpuff_Mailer::site_name(); $address = ! empty( $brand['address'] ) ? $brand['address'] : ''; $last_title = ''; $last_url = ''; $posts = get_posts( array( 'numberposts' => 1, 'post_status' => 'publish', 'suppress_filters' => false, ) ); if ( ! empty( $posts ) ) { $last_title = get_the_title( $posts[0] ); $last_url = get_permalink( $posts[0] ); } return array( '{{business_name}}' => esc_html( $company ), '{{business_address}}' => esc_html( $address ), '{{site_name}}' => esc_html( Mailpuff_Mailer::site_name() ), '{{site_url}}' => esc_url( home_url( '/' ) ), '{{date}}' => esc_html( date_i18n( get_option( 'date_format' ) ) ), '{{last_post_title}}' => esc_html( $last_title ), '{{last_post_url}}' => esc_url( $last_url ), // The campaign's web version: the outbox fills it in when it is up. '{{web_url}}' => '', ); } /** * Per-recipient values. * * @param object|array $row Subscriber row (email, first_name, last_name, unsubscribe_token). * @return array token => value. */ public static function subscriber_values( $row ) { $row = (array) $row; $first = isset( $row['first_name'] ) ? (string) $row['first_name'] : ''; $last = isset( $row['last_name'] ) ? (string) $row['last_name'] : ''; $email = isset( $row['email'] ) ? (string) $row['email'] : ''; $token = isset( $row['unsubscribe_token'] ) ? (string) $row['unsubscribe_token'] : ''; $full = trim( $first . ' ' . $last ); $unsub = rest_url( 'mailpuff/v1/unsubscribe/' . $token ); $prefs = rest_url( 'mailpuff/v1/preferences/' . $token ); // The confirm token exists only while a confirmation is going out (the // outbox passes it in). A test email carries the preview token, whose // page explains it is a test. Anywhere else the tag is empty. $confirm = ''; if ( ! empty( $row['confirm_token'] ) ) { $confirm = rest_url( 'mailpuff/v1/confirm/' . rawurlencode( (string) $row['confirm_token'] ) ); } elseif ( 'preview' === $token ) { $confirm = rest_url( 'mailpuff/v1/confirm/preview' ); } return array( '{{first_name}}' => esc_html( $first ), '{{last_name}}' => esc_html( $last ), '{{full_name}}' => esc_html( $full ), '{{email}}' => esc_html( $email ), '{{unsubscribe_url}}' => esc_url( $unsub ), '{{manage_subscription_url}}' => esc_url( $prefs ), '{{confirm_url}}' => esc_url( $confirm ), // The tag the email is about, which the outbox works out; '' here // becomes the business name in map(). '{{list_name}}' => esc_html( isset( $row['list_name'] ) ? (string) $row['list_name'] : '' ), ); } /** * Replace every token in $html for one recipient. * * @param string $html Email HTML. * @param object|array $row Subscriber row. * @param array $globals Precomputed global_values() (optional). * @return string */ public static function replace( $html, $row, $globals = null ) { return Mailpuff_Fields::fill( strtr( (string) $html, self::map( $row, $globals ) ), $row ); } /** * Replace every token in a plain-text line, a subject: the same values as * replace(), not escaped for HTML. * * @param string $text Plain text. * @param object|array $row Subscriber row. * @param array $globals Precomputed global_values() (optional). * @return string */ public static function replace_text( $text, $row, $globals = null ) { $text = (string) $text; if ( false === strpos( $text, '{{' ) ) { return $text; } $map = array_map( static function ( $value ) { return html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); }, self::map( $row, $globals ) ); return Mailpuff_Fields::fill( strtr( $text, $map ), $row, false ); } /** * Every token's value for one recipient. * * @param object|array $row Subscriber row. * @param array|null $globals Precomputed global_values(). * @return array token => value, escaped for HTML. */ private static function map( $row, $globals ) { if ( null === $globals ) { $globals = self::global_values(); } $map = $globals + self::subscriber_values( $row ); if ( '' === $map['{{list_name}}'] ) { $map['{{list_name}}'] = '' !== (string) ( $globals['{{business_name}}'] ?? '' ) ? $globals['{{business_name}}'] : (string) ( $globals['{{site_name}}'] ?? '' ); } return $map; } }