$form Form. * @param array $values Values. * @param int $entry_id Entry ID or zero. * @return array> */ public function send( array $form, array $values, int $entry_id ): array { $results = array(); if ( empty( $form['settings']['emailNotifications'] ) ) { return $results; } foreach ( $form['notifications'] as $notification ) { if ( empty( $notification['enabled'] ) || ! $this->condition_matches( (array) ( $notification['condition'] ?? array() ), $values ) ) { continue; } $to = $this->templates->render( $notification['to'], $form, $values, $entry_id, false ); $subject = $this->templates->render( $notification['subject'], $form, $values, $entry_id, false ); $body = $this->templates->render( $notification['body'], $form, $values, $entry_id, true ); $reply = $this->templates->render( $notification['replyTo'], $form, $values, $entry_id, false ); $recipients = preg_split( '/[\s,;]+/', $to ); $emails = array_values( array_filter( array_map( 'sanitize_email', $recipients ? $recipients : array() ), 'is_email' ) ); if ( ! $emails ) { $results[] = array( 'id' => $notification['id'], 'sent' => false, 'message' => __( 'گیرنده معتبر نیست.', 'mrn-forms' ), ); continue; } $headers = array( 'Content-Type: text/html; charset=UTF-8' ); if ( is_email( $reply ) ) { $headers[] = 'Reply-To: ' . sanitize_email( $reply ); } $from_email = Settings::get( 'from_email', get_option( 'admin_email' ) ); $from_name = Settings::get( 'from_name', get_bloginfo( 'name' ) ); if ( is_email( $from_email ) ) { $headers[] = 'From: ' . sanitize_text_field( $from_name ) . ' <' . sanitize_email( $from_email ) . '>'; } $sent = wp_mail( $emails, $subject, $this->templates->email_document( $subject, $body ), $headers ); foreach ( $emails as $email ) { $this->entries->log_notification( (int) $form['id'], $entry_id, $email, $sent ? 'sent' : 'failed', $sent ? '' : __( 'تابع wp_mail ناموفق بود.', 'mrn-forms' ) ); } $results[] = array( 'id' => $notification['id'], 'sent' => $sent, 'message' => $sent ? '' : __( 'ارسال ایمیل ناموفق بود.', 'mrn-forms' ), ); do_action( 'mrnf_after_notification', $notification, $sent, $form, $values, $entry_id ); } return (array) apply_filters( 'mrnf_notification_results', $results, $form, $values, $entry_id ); } /** * Evaluate optional notification routing. * * @param array $condition Routing condition. * @param array $values Submitted values. * @return bool */ private function condition_matches( array $condition, array $values ): bool { if ( empty( $condition['enabled'] ) || empty( $condition['field'] ) ) { return true; } $actual = $values[ $condition['field'] ] ?? ''; $actual = is_array( $actual ) ? implode( ',', $actual ) : (string) $actual; $target = (string) ( $condition['value'] ?? '' ); return match ( $condition['operator'] ?? 'equals' ) { 'not_equals' => $actual !== $target, 'contains' => str_contains( $actual, $target ), 'not_empty' => '' !== trim( $actual ), 'empty' => '' === trim( $actual ), default => $actual === $target, }; } }