get( 'settings.enable_entries_mail', false ) ) { add_action( 'nx_after_entry_inserted', [ $this, 'notify_on_entry' ] ); } } /** * Triggered after every new entry insert. * * @param array $entry */ public function notify_on_entry( $entry ) { $emails = $this->receiver_emails(); $subject = $this->email_subject(); if ( empty( $emails ) ) { return; } $body = $this->build_email_body( $entry ); $headers = [ 'Content-Type: text/html; charset=UTF-8', 'From: NotificationX ', ]; wp_mail( $emails, $subject, $body, $headers ); } /** * REST handler for the "Send Test Email" button. * * @param \WP_REST_Request $request * @return array|\WP_Error */ public function send_test( $request ) { $raw_email = $request->get_param( 'entries_mail_email' ); $subject = $request->get_param( 'entries_mail_subject' ); $emails = $this->receiver_emails( ! empty( $raw_email ) ? $raw_email : null ); if ( empty( $emails ) ) { return new \WP_Error( 'nx_invalid_email', __( 'Please provide a valid email address.', 'notificationx' ) ); } if ( empty( $subject ) ) { $subject = $this->email_subject(); } $dummy_entry = [ 'source' => 'test', 'entry_key' => 'test_entry', 'data' => [ 'name' => 'John Doe', 'email' => 'johndoe@example.com', 'message' => 'This is a test entry notification from NotificationX.', ], ]; $body = $this->build_email_body( $dummy_entry ); $headers = [ 'Content-Type: text/html; charset=UTF-8', 'From: NotificationX ', ]; $sent = wp_mail( $emails, $subject, $body, $headers ); if ( $sent ) { return [ 'message' => __( 'Test email sent successfully.', 'notificationx' ) ]; } return new \WP_Error( 'nx_mail_failed', __( 'Failed to send the test email. Please check your mail configuration.', 'notificationx' ) ); } /** * Return receiver emails from settings. * Handles the simple-repeater array format: [['title' => 'email', ...], ...] * * @param mixed $raw Optional override value (e.g. from REST request). * @return array */ public function receiver_emails( $raw = null ) { if ( is_null( $raw ) ) { $raw = Settings::get_instance()->get( 'settings.entries_mail_email', [] ); } $emails = $this->extract_emails( $raw ); if ( empty( $emails ) ) { $emails = [ get_option( 'admin_email' ) ]; } return $emails; } /** * Extract a clean array of validated email strings. * Handles simple-repeater format [['title' => 'email@...'], ...] and * plain comma/newline-separated strings as a legacy fallback. * * @param mixed $value * @return array */ private function extract_emails( $value ) { if ( is_array( $value ) ) { $emails = []; foreach ( $value as $item ) { if ( is_array( $item ) && ! empty( $item['title'] ) ) { $email = trim( $item['title'] ); if ( is_email( $email ) ) { $emails[] = $email; } } } return $emails; } // Legacy plain-string fallback $parts = preg_split( '/[\s,]+/', trim( (string) $value ), -1, PREG_SPLIT_NO_EMPTY ); return array_values( array_filter( $parts, 'is_email' ) ); } /** * Return configured subject or a sensible default. * * @return string */ public function email_subject() { $subject = Settings::get_instance()->get( 'settings.entries_mail_subject', '' ); if ( empty( $subject ) ) { $site_name = get_bloginfo( 'name' ); $subject = sprintf( __( 'New Entry Received on "%s"', 'notificationx' ), $site_name ); } return stripcslashes( $subject ); } /** * Build the HTML email body for a given entry. * * @param array $entry * @return string */ private function build_email_body( $entry ) { $source = ! empty( $entry['source'] ) ? esc_html( $entry['source'] ) : __( 'Unknown', 'notificationx' ); $site_name = get_bloginfo( 'name' ); $site_url = get_bloginfo( 'url' ); $date = current_time( 'Y-m-d H:i:s' ); $data_rows = ''; $data = ! empty( $entry['data'] ) && is_array( $entry['data'] ) ? $entry['data'] : []; foreach ( $data as $key => $value ) { if ( is_array( $value ) ) { $value = implode( ', ', $value ); } $data_rows .= '' . '' . esc_html( ucwords( str_replace( '_', ' ', $key ) ) ) . '' . '' . esc_html( (string) $value ) . '' . ''; } $table = $data_rows ? '' . $data_rows . '
' : '

' . __( 'No entry data available.', 'notificationx' ) . '

'; return '

New Entry Notification

' . esc_html( $site_name ) . '

Source: ' . $source . '

Date: ' . esc_html( $date ) . '

' . $table . '
Sent by NotificationX
'; } }