ensure_receipt( (array) $form_data, (array) $fields ); } /** * @param array $fields * @param array $entry * @param array $form_data * @param int $entry_id */ public function on_complete( $fields, $entry, $form_data, $entry_id = 0 ): void { $this->ensure_receipt( (array) $form_data, (array) $fields ); } public function register_smart_tags( $tags ) { $tags = is_array( $tags ) ? $tags : array(); $tags[ self::TAG_REF ] = __( 'Action Receipt: reference number', 'action-receipts' ); $tags[ self::TAG_LINK ] = __( 'Action Receipt: status page link', 'action-receipts' ); return $tags; } /** * Current smart tag API. */ public function smart_tag_value( $value, $tag_name, $form_data = array(), $fields = array(), $entry_id = 0, $smart_tag_object = null ) { if ( self::TAG_REF !== $tag_name && self::TAG_LINK !== $tag_name ) { return $value; } $receipt = $this->ensure_receipt( (array) $form_data, (array) $fields ); return $receipt ? $this->tag_output( $tag_name, $receipt ) : ''; } /** * Legacy smart tag API: receives the whole content and the tag name. */ public function smart_tag_legacy( $content, $tag ) { if ( self::TAG_REF !== $tag && self::TAG_LINK !== $tag ) { return $content; } $receipt = SubmissionRegistry::latest_for_source( 'wpforms' ); return str_replace( '{' . $tag . '}', $receipt ? $this->tag_output( $tag, $receipt ) : '', (string) $content ); } public function confirmation_message( $message, $form_data = array(), $fields = array(), $entry_id = 0 ) { $form_id = isset( $form_data['id'] ) ? (string) $form_data['id'] : ''; $receipt = $form_id ? SubmissionRegistry::get( 'wpforms:' . $form_id ) : null; if ( ! $receipt ) { return $message; } $cfg = Settings::form( 'wpforms:' . $form_id ); if ( empty( $cfg['show_in_message'] ) || false !== strpos( (string) $message, $receipt['reference'] ) ) { return $message; } /* translators: %s: reference number */ $line = sprintf( __( 'Your reference number is %s.', 'action-receipts' ), '' . esc_html( $receipt['reference'] ) . '' ); return $message . '

' . $line . '

'; } private function tag_output( string $tag, array $receipt ): string { return self::TAG_REF === $tag ? (string) $receipt['reference'] : ReceiptService::status_url( $receipt ); } private function ensure_receipt( array $form_data, array $fields ): ?array { $form_id = isset( $form_data['id'] ) ? (string) absint( $form_data['id'] ) : ''; if ( '' === $form_id || '0' === $form_id || ! $fields ) { return null; } // Stored spam (WPForms "Store spam entries") must not get a reference or use up a sequence number. if ( ! empty( $form_data['spam_reason'] ) ) { return null; } $form_key = 'wpforms:' . $form_id; return SubmissionRegistry::get_or_create( $form_key, static function () use ( $form_data, $fields, $form_id, $form_key ) { $cfg = Settings::form( $form_key ); $normalised = array(); $email = ''; $files = 0; foreach ( $fields as $field_id => $field ) { if ( ! is_array( $field ) ) { continue; } $type = isset( $field['type'] ) ? (string) $field['type'] : 'text'; $name = 'field_' . ( isset( $field['id'] ) ? $field['id'] : $field_id ); if ( 'file-upload' === $type ) { $files += ! empty( $field['value'] ) ? max( 1, count( (array) ( $field['value_raw'] ?? array( 1 ) ) ) ) : 0; } $normalised[ $name ] = array( 'label' => isset( $field['name'] ) && '' !== $field['name'] ? (string) $field['name'] : $name, 'value' => $field['value'] ?? '', 'type' => $type, ); if ( '' === $email && 'email' === $type ) { $email = (string) ( $field['value'] ?? '' ); } } if ( '' !== $cfg['email_field'] && isset( $normalised[ $cfg['email_field'] ] ) ) { $email = (string) $normalised[ $cfg['email_field'] ]['value']; } $title = isset( $form_data['settings']['form_title'] ) ? (string) $form_data['settings']['form_title'] : get_the_title( (int) $form_id ); return array( 'source' => 'wpforms', 'form_id' => $form_id, 'form_title' => $title, 'fields' => $normalised, 'email' => sanitize_email( $email ), 'files_count' => $files, ); } ); } public function list_forms(): array { $posts = get_posts( array( 'post_type' => 'wpforms', 'posts_per_page' => 200, // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page -- admin settings screen lists every form; bounded. 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC', ) ); $out = array(); foreach ( $posts as $post ) { $out[ (string) $post->ID ] = $post->post_title; } return $out; } public function list_fields( string $form_id ): array { $post = get_post( (int) $form_id ); if ( ! $post || 'wpforms' !== $post->post_type ) { return array(); } $data = function_exists( 'wpforms_decode' ) ? wpforms_decode( $post->post_content ) : json_decode( $post->post_content, true ); $out = array(); foreach ( (array) ( $data['fields'] ?? array() ) as $field ) { $type = (string) ( $field['type'] ?? '' ); if ( ! isset( $field['id'] ) || in_array( $type, self::NON_INPUT_TYPES, true ) ) { continue; } $out[ 'field_' . $field['id'] ] = array( 'label' => ! empty( $field['label'] ) ? (string) $field['label'] : 'field_' . $field['id'], 'type' => $type, ); } return $out; } /** * True when an active notification goes to the visitor (its "Send To" uses a form field) * and already contains the reference or the status link. * * @param string $form_id Form ID. */ public function form_emails_visitor_reference( string $form_id ): bool { $post = get_post( (int) $form_id ); if ( ! $post || 'wpforms' !== $post->post_type ) { return false; } $data = function_exists( 'wpforms_decode' ) ? wpforms_decode( $post->post_content ) : json_decode( $post->post_content, true ); $settings = (array) ( $data['settings'] ?? array() ); if ( empty( $settings['notification_enable'] ) ) { return false; } $notifications = (array) ( $settings['notifications'] ?? array() ); if ( function_exists( 'wpforms' ) && ! wpforms()->is_pro() ) { $notifications = array_slice( $notifications, 0, 1, true ); // Lite sends only the first one. } foreach ( $notifications as $notification ) { if ( ! is_array( $notification ) || ( isset( $notification['enable'] ) && 0 === (int) $notification['enable'] ) ) { continue; } if ( false === strpos( (string) ( $notification['email'] ?? '' ), '{field_id=' ) ) { continue; } $text = (string) ( $notification['subject'] ?? '' ) . ' ' . (string) ( $notification['message'] ?? '' ); if ( false !== strpos( $text, '{' . self::TAG_REF . '}' ) || false !== strpos( $text, '{' . self::TAG_LINK . '}' ) ) { return true; } } return false; } public function tag_help(): array { return array( '{' . self::TAG_REF . '}' => __( 'Reference number (notifications and confirmation message)', 'action-receipts' ), '{' . self::TAG_LINK . '}' => __( 'Private status page link', 'action-receipts' ), ); } }