$form Form. * @param array $values Values. * @param int $entry_id Entry ID. * @param bool $html Whether HTML output is expected. * @return string */ public function render( string $template, array $form, array $values, int $entry_id, bool $html = true ): string { $common = array( '{form_title}' => $form['title'], '{form_id}' => (string) $form['id'], '{entry_id}' => (string) $entry_id, '{site_name}' => get_bloginfo( 'name' ), '{site_url}' => home_url( '/' ), '{admin_email}' => Settings::get( 'admin_email', get_option( 'admin_email' ) ), ); foreach ( $common as $tag => $value ) { $template = str_replace( $tag, $html ? esc_html( (string) $value ) : (string) $value, $template ); } foreach ( $values as $key => $value ) { $text = is_array( $value ) ? implode( __( '، ', 'mrn-forms' ), $value ) : (string) $value; $template = str_replace( '{field:' . $key . '}', $html ? esc_html( $text ) : $text, $template ); } $template = str_replace( '{all_fields}', $html ? $this->fields_table( $form, $values ) : $this->fields_text( $form, $values ), $template ); return (string) preg_replace( '/\{field:[a-zA-Z0-9_-]+\}/', '', $template ); } /** * Render the HTML email template. * * @param string $subject Subject. * @param string $body Merged body HTML. * @return string */ public function email_document( string $subject, string $body ): string { $primary = Settings::get( 'primary_color', '#0b5e62' ); $accent = Settings::get( 'accent_color', '#d9a04d' ); $brand = Settings::get( 'brand_name', get_bloginfo( 'name' ) ); $logo = Settings::get( 'email_logo', '' ); $dir = is_rtl() ? 'rtl' : 'ltr'; $end = is_rtl() ? 'left' : 'right'; $mark = $logo ? '' . esc_attr( $brand ) . '' : '
MRN
'; return '' . '' . '
' . '' . '' . '' . '' . '
' . $mark . '' . esc_html( $brand ) . '
MRN FORMS

' . esc_html( $subject ) . '

' . '
' . wp_kses_post( $body ) . '
' . esc_html__( 'این پیام به‌صورت خودکار توسط MRN Forms ارسال شده است.', 'mrn-forms' ) . '
'; } /** * HTML table for all entered fields. * * @param array $form Form. * @param array $values Values. * @return string */ private function fields_table( array $form, array $values ): string { $labels = array(); foreach ( $form['fields'] as $field ) { $labels[ $field['key'] ] = $field['label']; } $start = is_rtl() ? 'right' : 'left'; $html = ''; foreach ( $values as $key => $value ) { $text = is_array( $value ) ? implode( __( '، ', 'mrn-forms' ), $value ) : (string) $value; $html .= '' . ''; } return $html . '
' . esc_html( $labels[ $key ] ?? $key ) . '' . ( str_starts_with( $text, 'http' ) ? '' . esc_html( $text ) . '' : nl2br( esc_html( $text ) ) ) . '
'; } /** * Plain-text field list. * * @param array $form Form. * @param array $values Values. * @return string */ private function fields_text( array $form, array $values ): string { $labels = array_column( $form['fields'], 'label', 'key' ); $lines = array(); foreach ( $values as $key => $value ) { $lines[] = ( $labels[ $key ] ?? $key ) . ': ' . ( is_array( $value ) ? implode( ', ', $value ) : $value ); } return implode( "\n", $lines ); } }