$field ) { $label = is_array( $field ) ? ( $field['label'] ?? '' ) : ''; if ( 0 === strcasecmp( (string) $field_key, $name ) || ( '' !== $label && 0 === strcasecmp( $label, $name ) ) ) { return self::stringify( is_array( $field ) ? ( $field['value'] ?? '' ) : $field ); } } return ''; } if ( 'fields' === $key ) { return self::fields_to_text( $fields ); } if ( array_key_exists( $key, $data ) && ! is_array( $data[ $key ] ) && ! is_object( $data[ $key ] ) ) { return (string) $data[ $key ]; } return $m[0]; }, (string) $template ); // Collapse 3+ newlines left by empty placeholders. $out = preg_replace( "/\n{3,}/", "\n\n", (string) $out ); return trim( (string) $out ); } /** * Fields as "Label: value" lines, skipping empties. * * @param array $fields Fields [key => ['label'=>..., 'value'=>...]]. * @return string */ public static function fields_to_text( array $fields ) { $lines = array(); foreach ( $fields as $key => $field ) { $label = is_array( $field ) && ! empty( $field['label'] ) ? $field['label'] : (string) $key; $value = self::stringify( is_array( $field ) ? ( $field['value'] ?? '' ) : $field ); if ( '' === trim( $value ) ) { continue; } $lines[] = $label . ': ' . $value; } return implode( "\n", $lines ); } /** * Turn a value into a single string. * * @param mixed $value Value. * @return string */ public static function stringify( $value ) { if ( is_array( $value ) ) { $flat = array(); array_walk_recursive( $value, static function ( $v ) use ( &$flat ) { if ( is_scalar( $v ) && '' !== trim( (string) $v ) ) { $flat[] = (string) $v; } } ); return implode( ', ', $flat ); } if ( is_bool( $value ) ) { return $value ? __( 'Yes', 'pingpush' ) : __( 'No', 'pingpush' ); } if ( is_scalar( $value ) ) { return trim( wp_strip_all_tags( html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ) ) ); } return ''; } /** * Plain text, safe length. * * @param string $text Text. * @param int $max Max characters. * @return string */ public static function truncate( $text, $max ) { $text = wp_strip_all_tags( html_entity_decode( (string) $text, ENT_QUOTES, 'UTF-8' ) ); $len = function_exists( 'mb_strlen' ) ? mb_strlen( $text, 'UTF-8' ) : strlen( $text ); if ( $len <= $max ) { return $text; } $cut = function_exists( 'mb_substr' ) ? mb_substr( $text, 0, $max - 1, 'UTF-8' ) : substr( $text, 0, $max - 1 ); return rtrim( $cut ) . '…'; } /** * Data available to every template. * * @return array */ private static function global_data() { return array( 'site_name' => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), 'site_url' => home_url( '/' ), 'admin_url' => admin_url(), 'date' => wp_date( get_option( 'date_format' ) ), 'time' => wp_date( get_option( 'time_format' ) ), ); } }