ensure_receipt( $contact_form, $submission ); } /** * @param string|null $output * @param string $name * @param bool $html * @return string|null */ public function special_mail_tags( $output, $name, $html = false ) { if ( self::TAG_REF !== $name && self::TAG_LINK !== $name ) { return $output; } $submission = class_exists( '\WPCF7_Submission' ) ? \WPCF7_Submission::get_instance() : null; if ( ! $submission ) { return $output; } $receipt = $this->ensure_receipt( $submission->get_contact_form(), $submission ); if ( ! $receipt ) { return ''; } if ( self::TAG_REF === $name ) { return $receipt['reference']; } $url = ReceiptService::status_url( $receipt ); return $html ? '' . esc_html( $url ) . '' : $url; } /** * @param array $response * @param array $result */ public function feedback_response( $response, $result ) { if ( ! is_array( $response ) || ( $response['status'] ?? '' ) !== 'mail_sent' ) { return $response; } $form_id = isset( $response['contact_form_id'] ) ? (string) $response['contact_form_id'] : ''; $receipt = $form_id ? SubmissionRegistry::get( 'cf7:' . $form_id ) : SubmissionRegistry::latest_for_source( 'cf7' ); if ( ! $receipt ) { return $response; } $cfg = Settings::form( 'cf7:' . $receipt['form_id'] ); if ( empty( $cfg['show_in_message'] ) ) { return $response; } $response['message'] = trim( (string) ( $response['message'] ?? '' ) . ' ' . /* translators: %s: reference number */ sprintf( __( 'Your reference number is %s.', 'action-receipts' ), $receipt['reference'] ) ); return $response; } /** * @param \WPCF7_ContactForm|null $contact_form * @param \WPCF7_Submission|null $submission */ private function ensure_receipt( $contact_form, $submission ): ?array { if ( ! $contact_form || ! method_exists( $contact_form, 'id' ) ) { return null; } if ( ! $submission && class_exists( '\WPCF7_Submission' ) ) { $submission = \WPCF7_Submission::get_instance(); } if ( ! $submission ) { return null; } $form_id = (string) $contact_form->id(); $form_key = 'cf7:' . $form_id; return SubmissionRegistry::get_or_create( $form_key, function () use ( $contact_form, $submission, $form_id, $form_key ) { $posted = (array) $submission->get_posted_data(); $cfg = Settings::form( $form_key ); $fields = array(); $email = ''; foreach ( (array) $contact_form->scan_form_tags() as $tag ) { $name = isset( $tag->name ) ? (string) $tag->name : ''; if ( '' === $name || ! array_key_exists( $name, $posted ) ) { continue; } $type = (string) $tag->basetype; $fields[ $name ] = array( 'label' => self::label_for( $contact_form, $name ), 'value' => $posted[ $name ], 'type' => $type, ); if ( '' === $email && 'email' === $type ) { $email = $posted[ $name ]; } } if ( '' !== $cfg['email_field'] && isset( $posted[ $cfg['email_field'] ] ) ) { $email = $posted[ $cfg['email_field'] ]; } if ( is_array( $email ) ) { $email = (string) reset( $email ); } $files = 0; foreach ( (array) $submission->uploaded_files() as $paths ) { $files += is_array( $paths ) ? count( array_filter( $paths ) ) : ( $paths ? 1 : 0 ); } return array( 'source' => 'cf7', 'form_id' => $form_id, 'form_title' => (string) $contact_form->title(), 'fields' => $fields, 'email' => sanitize_email( (string) $email ), 'files_count' => $files, ); } ); } public function list_forms(): array { $posts = get_posts( array( 'post_type' => 'wpcf7_contact_form', 'posts_per_page' => 200, // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page -- admin settings screen lists every form; bounded. 'post_status' => 'any', '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 { $form = \WPCF7_ContactForm::get_instance( (int) $form_id ); if ( ! $form ) { return array(); } $out = array(); foreach ( (array) $form->scan_form_tags() as $tag ) { if ( empty( $tag->name ) || in_array( $tag->basetype, self::NON_INPUT_TYPES, true ) ) { continue; } $out[ (string) $tag->name ] = array( 'label' => self::label_for( $form, (string) $tag->name ), 'type' => (string) $tag->basetype, ); } return $out; } /** * True when one of the form's mails goes to the visitor (its recipient is a form field such as * [your-email]) 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 { $form = \WPCF7_ContactForm::get_instance( (int) $form_id ); if ( ! $form ) { return false; } foreach ( array( 'mail', 'mail_2' ) as $prop ) { $mail = (array) $form->prop( $prop ); if ( 'mail_2' === $prop && empty( $mail['active'] ) ) { continue; } // A field tag like [your-email]; special tags such as [_site_admin_email] start with "_". if ( ! preg_match( '/\[\s*[a-zA-Z][0-9a-zA-Z:._-]*\s*\]/', (string) ( $mail['recipient'] ?? '' ) ) ) { continue; } $text = (string) ( $mail['subject'] ?? '' ) . ' ' . (string) ( $mail['body'] ?? '' ); 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 (use in Mail or Mail (2))', 'action-receipts' ), '[' . self::TAG_LINK . ']' => __( 'Private status page link', 'action-receipts' ), ); } /** * Field labels for a form: the text of the form's own