__( 'Lead Docket', 'agile-form-connector-for-lead-docket' ), 'callback' => array( __CLASS__, 'render' ), ); return $panels; } /** * Prints the tab. * * @param WPCF7_ContactForm $contact_form The form being edited. */ public static function render( $contact_form ) { $form_id = (int) $contact_form->id(); $rejected = self::get_rejected( $form_id ); $enabled = AFCFLD_Form_Settings::is_enabled( $form_id ); $endpoint = isset( $rejected['endpoint'] ) ? $rejected['endpoint'] : AFCFLD_Form_Settings::get_endpoint( $form_id ); $mapping = isset( $rejected['mapping'] ) ? $rejected['mapping'] : AFCFLD_Form_Settings::get_mapping_json( $form_id ); $example = AFCFLD_Form_Settings::encode_mapping( array( 'FirstName' => '[your-name]', 'Email1' => '[your-email]', 'MobilePhone' => '[your-phone]', 'Summary' => '[your-message]', 'CurrentUrl' => '[page-url]', ) ); $tags = array_map( static function ( $name ) { return '[' . $name . ']'; }, $contact_form->collect_mail_tags() ); $consent_field = AFCFLD_Form_Settings::get_consent_field( $form_id ); $texting_field = (string) get_post_meta( $form_id, AFCFLD_Form_Settings::META_TEXTING_FIELD, true ); ?>

'warning', 'additional_classes' => array( 'inline' ), ) ); } ?>

id(); if ( ! $form_id || ! current_user_can( 'wpcf7_edit_contact_form', $form_id ) ) { return; } $rejected = array( 'errors' => array() ); $endpoint_typed = isset( $_POST['afcfld_endpoint'] ) ? sanitize_text_field( wp_unslash( $_POST['afcfld_endpoint'] ) ) : ''; // esc_url_raw() turns a space pasted after the address into %20. $endpoint = isset( $_POST['afcfld_endpoint'] ) ? preg_replace( '/(?:%20)+$/', '', esc_url_raw( wp_unslash( $_POST['afcfld_endpoint'] ), array( 'https' ) ) ) : ''; if ( '' === $endpoint_typed ) { $endpoint = ''; } elseif ( '' === $endpoint || ! wp_parse_url( $endpoint, PHP_URL_HOST ) ) { $rejected['errors'][] = __( 'Lead Docket: the endpoint URL was not saved. Enter the full address, starting with https://.', 'agile-form-connector-for-lead-docket' ); $rejected['endpoint'] = $endpoint_typed; $endpoint = null; } // JSON is decoded as typed: sanitizing the text first would corrupt valid JSON, for example a value with "<". // parse_mapping() sanitizes every field name and value; the typed text is only shown back, escaped. $mapping_typed = isset( $_POST['afcfld_mapping'] ) ? wp_check_invalid_utf8( wp_unslash( $_POST['afcfld_mapping'] ), true ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $parsed = AFCFLD_Form_Settings::parse_mapping( $mapping_typed ); if ( null === $parsed['mapping'] ) { $rejected['errors'][] = sprintf( /* translators: %s: why the mapping was refused, such as "it is not valid JSON (Syntax error)." */ __( 'Lead Docket: the field mapping was not saved, because %s', 'agile-form-connector-for-lead-docket' ), $parsed['error'] ); $rejected['mapping'] = $mapping_typed; } AFCFLD_Form_Settings::save( $form_id, ! empty( $_POST['afcfld_enabled'] ), $endpoint, $parsed['mapping'] ); // Only when posted, so a save from an editor page opened before these fields existed keeps them. if ( isset( $_POST['afcfld_consent_field'], $_POST['afcfld_texting_field'] ) ) { AFCFLD_Form_Settings::save_texting( $form_id, trim( sanitize_text_field( wp_unslash( $_POST['afcfld_consent_field'] ) ), " \t[]" ), trim( sanitize_text_field( wp_unslash( $_POST['afcfld_texting_field'] ) ) ) ); } if ( $rejected['errors'] ) { set_transient( self::rejected_key( $form_id ), $rejected, 5 * MINUTE_IN_SECONDS ); } } /** * Shows, at the top of the editor, what the last save refused. * * @param string $page Contact Form 7 admin page. * @param string $action Current action. * @param WPCF7_ContactForm|null $contact_form The form being edited. */ public static function print_save_errors( $page, $action, $contact_form ) { if ( ! $contact_form instanceof WPCF7_ContactForm ) { return; } $rejected = self::get_rejected( (int) $contact_form->id() ); foreach ( isset( $rejected['errors'] ) ? $rejected['errors'] : array() as $error ) { wp_admin_notice( esc_html( $error ), array( 'type' => 'error' ) ); } } /** * What stops sending, or sends less than the mapping asks for, as the tab should say it. * * @param WPCF7_ContactForm $contact_form The form being edited. * @param bool $enabled Whether sending is switched on. * @return string[] */ private static function get_warnings( $contact_form, $enabled ) { $form_id = (int) $contact_form->id(); $warnings = array(); $endpoint = AFCFLD_Form_Settings::get_endpoint( $form_id ); $mapping = AFCFLD_Form_Settings::get_mapping( $form_id ); if ( $enabled && '' === $endpoint ) { $warnings[] = __( 'Sending is on, but there is no endpoint URL, so nothing is sent.', 'agile-form-connector-for-lead-docket' ); } if ( $enabled && ! $mapping ) { $warnings[] = __( 'Sending is on, but there is no field mapping, so nothing is sent.', 'agile-form-connector-for-lead-docket' ); } if ( 0 === stripos( $endpoint, 'http://' ) ) { $warnings[] = __( 'The endpoint URL starts with http://, so leads travel unencrypted. Change it to the https:// address.', 'agile-form-connector-for-lead-docket' ); } $unknown = self::get_unknown_tags( $mapping, $contact_form->collect_mail_tags() ); if ( $unknown ) { $warnings[] = sprintf( /* translators: %s: tags from the mapping, such as "[your-emial] [phone-number]". */ __( 'These tags match no field of this form, so they are always sent empty: %s', 'agile-form-connector-for-lead-docket' ), implode( ' ', $unknown ) ); } $consent_field = AFCFLD_Form_Settings::get_consent_field( $form_id ); if ( '' !== $consent_field && ! $contact_form->scan_form_tags( array( 'name' => $consent_field ) ) ) { $warnings[] = sprintf( /* translators: %s: the name given as the texting consent checkbox, such as sms-consent. */ __( 'This form has no field named %s, so no texting consent is sent.', 'agile-form-connector-for-lead-docket' ), $consent_field ); } return $warnings; } /** * The mapping's tags that can never get a value from the form, such as a mistyped field name. * * @param array $mapping Field mapping. * @param string[] $mail_tags The form's mail-tags, without brackets. * @return string[] The tags as written in the mapping. */ private static function get_unknown_tags( $mapping, $mail_tags ) { $unknown = array(); foreach ( $mapping as $template ) { if ( ! preg_match_all( AFCFLD_Sender::TAG_PATTERN, $template, $matches ) ) { continue; } foreach ( $matches[1] as $name ) { $name = trim( $name ); $fields = isset( AFCFLD_Sender::SHORTCUTS[ $name ] ) ? AFCFLD_Sender::SHORTCUTS[ $name ] : array(); // [page-url] and Contact Form 7's special tags, such as [_raw_your-field], do not name a field of their own. if ( 'page-url' === $name || 0 === strpos( $name, '_' ) || array_intersect( array_merge( $fields, array( $name ) ), $mail_tags ) ) { continue; } $unknown[] = '[' . $name . ']'; } } return array_values( array_unique( $unknown ) ); } /** * Prints the form's recent deliveries. * * @param array $log Deliveries, newest first. */ private static function print_log( $log ) { if ( ! $log ) { echo '

' . esc_html__( 'Nothing has been sent from this form yet.', 'agile-form-connector-for-lead-docket' ) . '

'; return; } ?>
= 200 && $code < 300 ) { /* translators: %d: HTTP status code, such as 200. */ return sprintf( __( 'Accepted by Lead Docket (HTTP %d)', 'agile-form-connector-for-lead-docket' ), $code ); } if ( $code ) { /* translators: %d: HTTP status code, such as 500. */ return sprintf( __( 'Refused by Lead Docket (HTTP %d)', 'agile-form-connector-for-lead-docket' ), $code ); } /* translators: %s: the connection error, such as a timeout. */ return sprintf( __( 'Not delivered: %s', 'agile-form-connector-for-lead-docket' ), $entry['error'] ); } /** * What the last save of this form by the current user refused, read once per request. * * @param int $form_id Contact form ID. * @return array */ private static function get_rejected( $form_id ) { if ( null === self::$rejected ) { $key = self::rejected_key( $form_id ); $rejected = get_transient( $key ); self::$rejected = is_array( $rejected ) ? $rejected : array(); if ( false !== $rejected ) { delete_transient( $key ); } } return self::$rejected; } /** * The transient that carries a refused save over Contact Form 7's redirect. * * @param int $form_id Contact form ID. * @return string */ private static function rejected_key( $form_id ) { return 'afcfld_rejected_' . get_current_user_id() . '_' . (int) $form_id; } }