array( 'is', 'is_not', 'contains', 'not_contains', 'is_set', 'is_not_set' ), 'number' => array( 'is', 'is_not', 'gt', 'lt', 'is_set', 'is_not_set' ), 'date' => array( 'is', 'before', 'after', 'within_days', 'is_set', 'is_not_set' ), 'boolean' => array( 'is_true', 'is_false' ), ); /** Keys a field may not take: the contact's own columns and Mailpuff's meta. */ const RESERVED = array( 'id', 'email', 'first_name', 'last_name', 'full_name', 'name', 'status', 'source', 'customer_type', 'gross_revenue', 'created_at', 'updated_at', 'confirmed_at', 'consent_source', 'groups', 'tags', 'post_delivery' ); /** * Every field, in the order they were made. * * @return array */ public static function all() { $stored = get_option( self::OPTION, array() ); $fields = array(); foreach ( is_array( $stored ) ? $stored : array() as $field ) { $key = is_array( $field ) ? sanitize_key( (string) ( $field['key'] ?? '' ) ) : ''; if ( '' === $key || isset( $fields[ $key ] ) ) { continue; } $type = (string) ( $field['type'] ?? 'text' ); $fields[ $key ] = array( 'key' => $key, 'label' => '' !== (string) ( $field['label'] ?? '' ) ? (string) $field['label'] : $key, 'type' => in_array( $type, self::TYPES, true ) ? $type : 'text', ); } return array_values( $fields ); } /** * One field. * * @param string $key Field key. * @return array|null */ public static function find( $key ) { foreach ( self::all() as $field ) { if ( $field['key'] === (string) $key ) { return $field; } } return null; } /** * A key for a new field: the one asked for, or one made from the label. * * @param string $label Label. * @param string $key Wanted key, or ''. * @return string */ public static function key_for( $label, $key = '' ) { $key = '' !== (string) $key ? (string) $key : str_replace( '-', '_', sanitize_title( (string) $label ) ); return substr( ltrim( sanitize_key( $key ), '_' ), 0, 64 ); } /** * Make a field. * * @param string $label Label people read. * @param string $type One of TYPES. * @param string $key The key merge tags use; made from the label when ''. * @return array|WP_Error The field. */ public static function create( $label, $type = 'text', $key = '' ) { $label = trim( sanitize_text_field( (string) $label ) ); $key = self::key_for( $label, $key ); $type = (string) $type; if ( '' === $label || '' === $key ) { return new WP_Error( 'invalid_field', __( 'Give the field a name.', 'mailpuff' ), array( 'status' => 400 ) ); } if ( ! in_array( $type, self::TYPES, true ) ) { return new WP_Error( 'invalid_type', __( 'A field is text, a number, a date or yes or no.', 'mailpuff' ), array( 'status' => 400 ) ); } if ( in_array( $key, self::RESERVED, true ) ) { /* translators: %s: a field key, such as "email". */ return new WP_Error( 'reserved_key', sprintf( __( 'Mailpuff keeps "%s" already. Pick another name.', 'mailpuff' ), $key ), array( 'status' => 400 ) ); } $fields = self::all(); if ( null !== self::find( $key ) ) { return new WP_Error( 'duplicate_field', __( 'A field with that key already exists.', 'mailpuff' ), array( 'status' => 409 ) ); } if ( count( $fields ) >= self::MAX ) { /* translators: %d: the most fields a site may have. */ return new WP_Error( 'too_many_fields', sprintf( __( 'A site may have %d fields.', 'mailpuff' ), self::MAX ), array( 'status' => 400 ) ); } $field = array( 'key' => $key, 'label' => substr( $label, 0, 100 ), 'type' => $type, ); $fields[] = $field; update_option( self::OPTION, $fields, false ); return $field; } /** * Rename a field. The key and the type stay. * * @param string $key Field key. * @param string $label New label. * @return array|WP_Error The field. */ public static function rename( $key, $label ) { $label = trim( sanitize_text_field( (string) $label ) ); if ( '' === $label ) { return new WP_Error( 'invalid_field', __( 'Give the field a name.', 'mailpuff' ), array( 'status' => 400 ) ); } $fields = self::all(); foreach ( $fields as $i => $field ) { if ( $field['key'] === (string) $key ) { $fields[ $i ]['label'] = substr( $label, 0, 100 ); update_option( self::OPTION, $fields, false ); return $fields[ $i ]; } } return self::not_found(); } /** * Delete a field and every value it holds. Refused while a segment or an * email that is not sent yet uses it, so nothing quietly changes who a * segment reaches or what an email says. * * @param string $key Field key. * @return array|WP_Error { key, deleted, values }. */ public static function delete( $key ) { global $wpdb; $field = self::find( $key ); if ( ! $field ) { return self::not_found(); } $used = self::used_by( $field['key'] ); if ( $used['segments'] || $used['emails'] ) { $names = array_merge( array_map( /* translators: %s: a segment's name. */ static fn( $s ) => sprintf( __( 'the segment "%s"', 'mailpuff' ), $s['name'] ), $used['segments'] ), array_map( /* translators: %s: an email's name. */ static fn( $e ) => sprintf( __( 'the email "%s"', 'mailpuff' ), $e['name'] ), $used['emails'] ) ); return new WP_Error( 'field_in_use', /* translators: %s: a list of segment and email names. */ sprintf( __( 'This field is used by %s. Change those first.', 'mailpuff' ), wp_sprintf( '%l', $names ) ), array( 'status' => 409, 'used' => $used, ) ); } $values = self::count_values( $field['key'] ); $wpdb->delete( Mailpuff_Schema::table( Mailpuff_Schema::SUBSCRIBER_META ), array( 'meta_key' => $field['key'] ), array( '%s' ) ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_query_meta_key -- Mailpuff's own meta table; a field's values go with it. $fields = array_values( array_filter( self::all(), static fn( $f ) => $f['key'] !== $field['key'] ) ); update_option( self::OPTION, $fields, false ); return array( 'key' => $field['key'], 'deleted' => true, 'values' => $values, ); } /** * What uses a field: segments that filter on it, and emails not sent yet * (drafts, scheduled campaigns, the automatic emails' templates) whose * subject or body has its merge tag. * * @param string $key Field key. * @return array{segments: array, emails: array} */ public static function used_by( $key ) { global $wpdb; $key = (string) $key; $segments = array(); $table = Mailpuff_Schema::table( Mailpuff_Schema::SEGMENTS ); foreach ( (array) $wpdb->get_results( $wpdb->prepare( 'SELECT id, name, filter FROM %i ORDER BY name ASC', $table ) ) as $row ) { $filter = json_decode( (string) $row->filter, true ); foreach ( (array) ( is_array( $filter ) ? ( $filter['fields'] ?? array() ) : array() ) as $condition ) { if ( is_array( $condition ) && (string) ( $condition['key'] ?? '' ) === $key ) { $segments[] = array( 'id' => (int) $row->id, 'name' => (string) $row->name, ); break; } } } $emails = array(); $campaigns = Mailpuff_Schema::table( Mailpuff_Schema::CAMPAIGNS ); $like = '%' . $wpdb->esc_like( '{{field:' . $key ) . '%'; $system = Mailpuff_System_Emails::kinds(); $in = implode( ',', array_fill( 0, count( $system ), '%s' ) ); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- $in is a list of %s placeholders, one per bound type; every value is bound. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, title, subject, type, content_html FROM %i WHERE ( status IN ('draft','scheduled','sending','paused') OR type IN ($in) ) AND ( subject LIKE %s OR content_html LIKE %s OR content_blocks LIKE %s ) ORDER BY id ASC", array_merge( array( $campaigns ), $system, array( $like, $like, $like ) ) ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber foreach ( (array) $rows as $row ) { // `{{field:city}}` must not count for a field called `cit`. $text = (string) $row->subject . ' ' . (string) $row->content_html; if ( ! preg_match( '/\{\{\s*field:' . preg_quote( $key, '/' ) . '\s*(?:\||\}\})/', $text ) ) { continue; } $name = '' !== (string) $row->title ? (string) $row->title : (string) $row->subject; $emails[] = array( 'id' => (int) $row->id, 'name' => '' !== $name ? $name : (string) $row->type, 'type' => (string) $row->type, ); } return array( 'segments' => $segments, 'emails' => $emails, ); } /** * How many contacts have a value in a field. * * @param string $key Field key. * @return int */ public static function count_values( $key ) { global $wpdb; $table = Mailpuff_Schema::table( Mailpuff_Schema::SUBSCRIBER_META ); return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(DISTINCT contact_id) FROM %i WHERE meta_key = %s AND meta_value <> %s', $table, (string) $key, '' ) ); } /** * Every field with what it holds and what uses it, as the Fields page * and the tools list them. * * @return array */ public static function described() { $out = array(); foreach ( self::all() as $field ) { $out[] = $field + array( 'merge_tag' => '{{field:' . $field['key'] . '}}', 'ops' => self::OPS[ $field['type'] ], 'values' => self::count_values( $field['key'] ), 'used_by' => self::used_by( $field['key'] ), ); } return $out; } /* Values -------------------------------------------------------------- */ /** * A value as a field of this type keeps it, or a WP_Error. '' and null * mean no value. * * @param array $field The field. * @param mixed $value Raw value. * @param bool $strict False for imports: a value that does not fit a number or date field is dropped (''). * @return string|WP_Error */ public static function clean( array $field, $value, $strict = true ) { if ( null === $value || ( is_string( $value ) && '' === trim( $value ) ) ) { return ''; } if ( ! is_scalar( $value ) ) { return self::bad_value( $field ); } switch ( $field['type'] ) { case 'number': $raw = str_replace( array( ' ', ',' ), array( '', '.' ), trim( (string) $value ) ); if ( ! is_numeric( $raw ) ) { return $strict ? self::bad_value( $field ) : ''; } // 12, 12.5 and -3 as written, without 1e3 or a leading +. return (string) ( 0 + $raw ); case 'date': $raw = trim( (string) $value ); if ( preg_match( '/^(\d{4})-(\d{2})-(\d{2})$/', $raw, $m ) && checkdate( (int) $m[2], (int) $m[3], (int) $m[1] ) ) { return $raw; } if ( $strict ) { return self::bad_value( $field ); } $time = strtotime( $raw ); return false === $time ? '' : gmdate( 'Y-m-d', $time ); case 'boolean': if ( is_bool( $value ) ) { return $value ? '1' : '0'; } $raw = strtolower( trim( (string) $value ) ); if ( in_array( $raw, array( '1', 'true', 'yes', 'y', 'on' ), true ) ) { return '1'; } if ( in_array( $raw, array( '0', 'false', 'no', 'n', 'off' ), true ) ) { return '0'; } return $strict ? self::bad_value( $field ) : ''; default: return substr( sanitize_text_field( (string) $value ), 0, self::MAX_TEXT ); } } /** * One contact's values: field key => value, only the fields that exist * and have one. * * @param int $contact_id Contact ID. * @return array */ public static function values_of( $contact_id ) { $all = self::values_for( array( (int) $contact_id ) ); return $all[ (int) $contact_id ] ?? array(); } /** * Some contacts' values in one query: contact id => (key => value). * * @param int[] $ids Contact IDs. * @return array> */ public static function values_for( array $ids ) { global $wpdb; $ids = array_values( array_filter( array_map( 'intval', $ids ) ) ); $keys = wp_list_pluck( self::all(), 'key' ); if ( ! $ids || ! $keys ) { return array(); } $table = Mailpuff_Schema::table( Mailpuff_Schema::SUBSCRIBER_META ); $id_in = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); $key_in = implode( ',', array_fill( 0, count( $keys ), '%s' ) ); $by_id = array(); $ordered = array_flip( $keys ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- the lists are %d and %s placeholders, one per bound value. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT contact_id, meta_key, meta_value FROM %i WHERE contact_id IN ($id_in) AND meta_key IN ($key_in) AND meta_value <> ''", array_merge( array( $table ), $ids, $keys ) ) ); foreach ( (array) $rows as $row ) { $by_id[ (int) $row->contact_id ][ (string) $row->meta_key ] = (string) $row->meta_value; } // In the fields' own order, as the page lists them. foreach ( $by_id as $id => $values ) { uksort( $values, static fn( $a, $b ) => $ordered[ $a ] <=> $ordered[ $b ] ); $by_id[ $id ] = $values; } return $by_id; } /** * Change some of a contact's values. Every value is checked before any is * stored, so a bad one changes nothing; '' or null clears a value. * * @param int $contact_id Contact ID. * @param array $values Field key => value. * @return array|WP_Error The contact's values after. */ public static function set_values( $contact_id, array $values ) { $contact_id = (int) $contact_id; $clean = array(); foreach ( $values as $key => $value ) { $field = self::find( (string) $key ); if ( ! $field ) { /* translators: %s: a field key. */ return new WP_Error( 'unknown_field', sprintf( __( 'There is no field "%s".', 'mailpuff' ), (string) $key ), array( 'status' => 400 ) ); } $value = self::clean( $field, $value ); if ( is_wp_error( $value ) ) { return $value; } $clean[ $field['key'] ] = $value; } $before = self::values_of( $contact_id ); foreach ( $clean as $key => $value ) { if ( '' === $value ) { Mailpuff_Schema::delete_meta_key( $contact_id, $key ); } else { Mailpuff_Schema::set_meta( $contact_id, $key, $value ); } } $after = self::values_of( $contact_id ); if ( $before !== $after ) { /** This action is documented in includes/api/class-contacts-controller.php */ do_action( 'mailpuff_contact_updated', $contact_id, array( 'fields' => $clean ) ); } return $after; } /** * A value as people read it: a date in the site's format, a boolean as * Yes or No. * * @param array $field The field. * @param string $value Stored value. * @return string Plain text. */ public static function display( array $field, $value ) { $value = (string) $value; if ( '' === $value ) { return ''; } if ( 'boolean' === $field['type'] ) { return '1' === $value ? __( 'Yes', 'mailpuff' ) : __( 'No', 'mailpuff' ); } if ( 'date' === $field['type'] && preg_match( '/^\d{4}-\d{2}-\d{2}$/', $value ) ) { $date = date_create_immutable( $value, wp_timezone() ); return $date ? wp_date( (string) get_option( 'date_format' ), $date->getTimestamp() ) : $value; } return $value; } /* Merge tags ---------------------------------------------------------- */ /** `{{field:key}}` or `{{field:key|fallback}}`. */ const TAG = '/\{\{\s*field:([a-z0-9_-]+)\s*(?:\|([^{}]*))?\}\}/i'; /** * Fill every field tag in some text for one recipient. A field that is * gone or empty gives its fallback, else nothing; a tag never shows as * written. * * @param string $text Text with tags. * @param object|array $row Recipient: `contact_id`, or `fields` (key => value) for a sample. * @param bool $html Escape for HTML; else plain text on one line (a subject). * @return string */ public static function fill( $text, $row, $html = true ) { $text = (string) $text; if ( false === stripos( $text, 'field:' ) ) { return $text; } $row = (array) $row; if ( isset( $row['fields'] ) && is_array( $row['fields'] ) ) { $values = $row['fields']; } elseif ( ! empty( $row['contact_id'] ) ) { $values = self::values_of( (int) $row['contact_id'] ); } else { $values = array(); } $fields = array(); foreach ( self::all() as $field ) { $fields[ $field['key'] ] = $field; } return (string) preg_replace_callback( self::TAG, static function ( $m ) use ( $values, $fields, $html ) { $key = strtolower( $m[1] ); $value = isset( $fields[ $key ] ) ? self::display( $fields[ $key ], (string) ( $values[ $key ] ?? '' ) ) : ''; if ( '' === $value ) { // The fallback was typed in the editor, so it may hold entities. $value = html_entity_decode( trim( (string) ( $m[2] ?? '' ) ), ENT_QUOTES, 'UTF-8' ); } return $html ? esc_html( $value ) : trim( preg_replace( '/[\r\n\t]+/', ' ', $value ) ); }, $text ); } /* Segment conditions -------------------------------------------------- */ /** * Clean a segment's field conditions: known shapes only. A condition on a * field that is gone is kept, so where() selects nobody rather than the * segment growing. * * @param mixed $conditions Raw conditions. * @return array */ public static function normalize_conditions( $conditions ) { if ( is_string( $conditions ) ) { $conditions = json_decode( $conditions, true ); } $out = array(); foreach ( is_array( $conditions ) ? $conditions : array() as $condition ) { if ( ! is_array( $condition ) ) { continue; } $key = sanitize_key( (string) ( $condition['key'] ?? '' ) ); $op = sanitize_key( (string) ( $condition['op'] ?? '' ) ); if ( '' === $key || '' === $op ) { continue; } $clean = array( 'key' => $key, 'op' => $op, ); if ( ! in_array( $op, array( 'is_set', 'is_not_set', 'is_true', 'is_false' ), true ) ) { $clean['value'] = substr( sanitize_text_field( (string) ( $condition['value'] ?? '' ) ), 0, self::MAX_TEXT ); } $out[] = $clean; } return $out; } /** * Why a condition cannot be saved, or '' when it can. * * @param array $condition { key, op, value? }. * @return string */ public static function condition_error( array $condition ) { $field = self::find( (string) ( $condition['key'] ?? '' ) ); if ( ! $field ) { /* translators: %s: a field key. */ return sprintf( __( 'There is no field "%s".', 'mailpuff' ), (string) ( $condition['key'] ?? '' ) ); } $op = (string) ( $condition['op'] ?? '' ); if ( ! in_array( $op, self::OPS[ $field['type'] ], true ) ) { /* translators: 1: a field's name, 2: the conditions it takes. */ return sprintf( __( '%1$s takes these conditions: %2$s.', 'mailpuff' ), $field['label'], implode( ', ', self::OPS[ $field['type'] ] ) ); } if ( ! array_key_exists( 'value', $condition ) ) { return ''; } $value = (string) $condition['value']; if ( 'within_days' === $op ) { return ctype_digit( $value ) && (int) $value > 0 ? '' : __( 'Enter a number of days.', 'mailpuff' ); } if ( '' === trim( $value ) ) { /* translators: %s: a field's name. */ return sprintf( __( 'Enter a value for %s.', 'mailpuff' ), $field['label'] ); } if ( in_array( $field['type'], array( 'number', 'date' ), true ) ) { $cleaned = self::clean( $field, $value ); return is_wp_error( $cleaned ) ? $cleaned->get_error_message() : ''; } return ''; } /** * SQL for a segment's field conditions, each an ` AND (...)` on the * contact alias, with its params. A condition on a field that is gone, or * that does not fit its field, selects nobody. * * @param array $conditions Normalized conditions. * @param string $alias The contacts table's alias. * @return array{0:string,1:array} */ public static function where( array $conditions, $alias = 'c' ) { global $wpdb; $meta = Mailpuff_Schema::table( Mailpuff_Schema::SUBSCRIBER_META ); $sql = ''; $params = array(); $exists = static fn( $cond ) => "EXISTS ( SELECT 1 FROM %i m WHERE m.contact_id = $alias.id AND m.meta_key = %s AND $cond )"; $missing = static fn( $cond ) => "NOT EXISTS ( SELECT 1 FROM %i m WHERE m.contact_id = $alias.id AND m.meta_key = %s AND $cond )"; foreach ( $conditions as $condition ) { if ( '' !== self::condition_error( $condition ) ) { $sql .= ' AND 1 = 0'; continue; } $field = self::find( $condition['key'] ); $key = $field['key']; $value = (string) ( $condition['value'] ?? '' ); $typed = in_array( $field['type'], array( 'number', 'date' ), true ) && '' !== $value && 'within_days' !== $condition['op'] ? (string) self::clean( $field, $value ) : $value; switch ( $condition['op'] ) { case 'is': if ( 'number' === $field['type'] ) { $sql .= ' AND ' . $exists( "m.meta_value <> '' AND ( m.meta_value + 0 ) = %f" ); $params[] = $meta; $params[] = $key; $params[] = (float) $typed; } else { $sql .= ' AND ' . $exists( 'LOWER( m.meta_value ) = LOWER( %s )' ); $params[] = $meta; $params[] = $key; $params[] = $typed; } break; case 'is_not': if ( 'number' === $field['type'] ) { $sql .= ' AND ' . $missing( "m.meta_value <> '' AND ( m.meta_value + 0 ) = %f" ); $params[] = $meta; $params[] = $key; $params[] = (float) $typed; } else { $sql .= ' AND ' . $missing( 'LOWER( m.meta_value ) = LOWER( %s )' ); $params[] = $meta; $params[] = $key; $params[] = $typed; } break; case 'contains': case 'not_contains': $build = 'contains' === $condition['op'] ? $exists : $missing; $sql .= ' AND ' . $build( 'LOWER( m.meta_value ) LIKE LOWER( %s )' ); $params[] = $meta; $params[] = $key; $params[] = '%' . $wpdb->esc_like( $value ) . '%'; break; case 'gt': case 'lt': $sql .= ' AND ' . $exists( "m.meta_value <> '' AND ( m.meta_value + 0 ) " . ( 'gt' === $condition['op'] ? '>' : '<' ) . ' %f' ); $params[] = $meta; $params[] = $key; $params[] = (float) $typed; break; case 'before': case 'after': // Dates are kept as Y-m-d, so text order is date order. $sql .= ' AND ' . $exists( "m.meta_value <> '' AND m.meta_value " . ( 'before' === $condition['op'] ? '<' : '>' ) . ' %s' ); $params[] = $meta; $params[] = $key; $params[] = $typed; break; case 'within_days': // From N days ago to today, in the site's time zone. $sql .= ' AND ' . $exists( 'm.meta_value >= %s AND m.meta_value <= %s' ); $params[] = $meta; $params[] = $key; $params[] = wp_date( 'Y-m-d', time() - (int) $value * DAY_IN_SECONDS ); $params[] = wp_date( 'Y-m-d' ); break; case 'is_set': $sql .= ' AND ' . $exists( "m.meta_value <> ''" ); $params[] = $meta; $params[] = $key; break; case 'is_not_set': $sql .= ' AND ' . $missing( "m.meta_value <> ''" ); $params[] = $meta; $params[] = $key; break; case 'is_true': $sql .= ' AND ' . $exists( "m.meta_value = '1'" ); $params[] = $meta; $params[] = $key; break; case 'is_false': $sql .= ' AND ' . $missing( "m.meta_value = '1'" ); $params[] = $meta; $params[] = $key; break; } } return array( $sql, $params ); } /** * A condition as people read it: "City is Berlin". * * @param array $condition { key, op, value? }. * @return string */ public static function describe( array $condition ) { $field = self::find( (string) ( $condition['key'] ?? '' ) ); $label = $field ? $field['label'] : (string) ( $condition['key'] ?? '' ); $value = (string) ( $condition['value'] ?? '' ); $words = array( /* translators: 1: a field's name, 2: a value. */ 'is' => __( '%1$s is %2$s', 'mailpuff' ), /* translators: 1: a field's name, 2: a value. */ 'is_not' => __( '%1$s is not %2$s', 'mailpuff' ), /* translators: 1: a field's name, 2: a value. */ 'contains' => __( '%1$s contains %2$s', 'mailpuff' ), /* translators: 1: a field's name, 2: a value. */ 'not_contains' => __( '%1$s does not contain %2$s', 'mailpuff' ), /* translators: 1: a field's name, 2: a number. */ 'gt' => __( '%1$s is more than %2$s', 'mailpuff' ), /* translators: 1: a field's name, 2: a number. */ 'lt' => __( '%1$s is less than %2$s', 'mailpuff' ), /* translators: 1: a field's name, 2: a date. */ 'before' => __( '%1$s is before %2$s', 'mailpuff' ), /* translators: 1: a field's name, 2: a date. */ 'after' => __( '%1$s is after %2$s', 'mailpuff' ), /* translators: 1: a field's name, 2: a number of days. */ 'within_days' => __( '%1$s is in the last %2$s days', 'mailpuff' ), /* translators: %1$s: a field's name. */ 'is_set' => __( '%1$s has a value', 'mailpuff' ), /* translators: %1$s: a field's name. */ 'is_not_set' => __( '%1$s is empty', 'mailpuff' ), /* translators: %1$s: a field's name. */ 'is_true' => __( '%1$s is yes', 'mailpuff' ), /* translators: %1$s: a field's name. */ 'is_false' => __( '%1$s is not yes', 'mailpuff' ), ); $op = (string) ( $condition['op'] ?? '' ); return isset( $words[ $op ] ) ? sprintf( $words[ $op ], $label, $value ) : $label; } /** * @return WP_Error */ private static function not_found() { return new WP_Error( 'no_field', __( 'There is no field with that key.', 'mailpuff' ), array( 'status' => 404 ) ); } /** * @param array $field The field. * @return WP_Error */ private static function bad_value( array $field ) { $what = array( /* translators: %s: a field's name. */ 'number' => __( '%s takes a number.', 'mailpuff' ), /* translators: %s: a field's name. */ 'date' => __( '%s takes a date, such as 2026-10-27.', 'mailpuff' ), /* translators: %s: a field's name. */ 'boolean' => __( '%s takes yes or no.', 'mailpuff' ), /* translators: %s: a field's name. */ 'text' => __( '%s takes text.', 'mailpuff' ), ); return new WP_Error( 'invalid_value', sprintf( $what[ $field['type'] ] ?? $what['text'], $field['label'] ), array( 'status' => 400 ) ); } }