*/ public static function get_mapping( $form_id ) { $parsed = self::parse_mapping( self::get_mapping_json( $form_id ) ); return null === $parsed['mapping'] ? array() : $parsed['mapping']; } /** * Whether the form has any Lead Docket setting of its own. * * @param int $form_id Contact form ID. * @return bool */ public static function has_settings( $form_id ) { foreach ( array( self::META_ENABLED, self::META_ENDPOINT, self::META_MAPPING, self::META_CONSENT_FIELD, self::META_TEXTING_FIELD ) as $key ) { if ( metadata_exists( 'post', $form_id, $key ) ) { return true; } } return false; } /** * Stores the form's settings. A null endpoint or mapping leaves that setting as it is. * * @param int $form_id Contact form ID. * @param bool $enabled Whether sending is switched on. * @param string|null $endpoint Endpoint URL; an empty string removes it. * @param array|null $mapping Field mapping; an empty array removes it. */ public static function save( $form_id, $enabled, $endpoint, $mapping ) { if ( $enabled ) { update_post_meta( $form_id, self::META_ENABLED, '1' ); } else { delete_post_meta( $form_id, self::META_ENABLED ); } if ( null !== $endpoint ) { if ( '' === $endpoint ) { delete_post_meta( $form_id, self::META_ENDPOINT ); } else { update_post_meta( $form_id, self::META_ENDPOINT, $endpoint ); } } if ( null !== $mapping ) { if ( empty( $mapping ) ) { delete_post_meta( $form_id, self::META_MAPPING ); } else { // update_post_meta() unslashes the value, which would strip the JSON's own escapes. update_post_meta( $form_id, self::META_MAPPING, wp_slash( self::encode_mapping( $mapping ) ) ); } } } /** * The name of the form's texting consent checkbox, or an empty string when the form sends no consent. * * @param int $form_id Contact form ID. * @return string */ public static function get_consent_field( $form_id ) { return trim( (string) get_post_meta( $form_id, self::META_CONSENT_FIELD, true ) ); } /** * The Lead Docket field that receives the texting consent: the form's own setting, or Lead Docket's default name. * * @param int $form_id Contact form ID. * @return string */ public static function get_texting_field( $form_id ) { $name = trim( (string) get_post_meta( $form_id, self::META_TEXTING_FIELD, true ) ); return '' !== $name ? $name : self::TEXTING_FIELD_DEFAULT; } /** * Stores the form's texting consent settings. An empty value removes the setting. * * @param int $form_id Contact form ID. * @param string $consent_field Name of the consent checkbox in the form. * @param string $texting_field Lead Docket field name; empty or the default keeps no setting of its own. */ public static function save_texting( $form_id, $consent_field, $texting_field ) { if ( '' === $consent_field ) { delete_post_meta( $form_id, self::META_CONSENT_FIELD ); } else { update_post_meta( $form_id, self::META_CONSENT_FIELD, wp_slash( $consent_field ) ); } if ( '' === $texting_field || self::TEXTING_FIELD_DEFAULT === $texting_field ) { delete_post_meta( $form_id, self::META_TEXTING_FIELD ); } else { update_post_meta( $form_id, self::META_TEXTING_FIELD, wp_slash( $texting_field ) ); } } /** * Parses and checks a field mapping typed as JSON, and sanitizes every field name and value in it. * * @param string $json The mapping as typed. * @return array{mapping: array|null, error: string} The mapping, empty for empty input, * or null with the reason it was refused. */ public static function parse_mapping( $json ) { $json = trim( (string) $json ); if ( '' === $json ) { return array( 'mapping' => array(), 'error' => '', ); } $decoded = json_decode( $json ); if ( JSON_ERROR_NONE !== json_last_error() ) { return self::refuse( /* translators: %s: the JSON parser's error message, such as "Syntax error". */ sprintf( __( 'it is not valid JSON (%s).', 'agile-form-connector-for-lead-docket' ), json_last_error_msg() ) ); } if ( ! is_object( $decoded ) ) { return self::refuse( __( 'it must be a JSON object, such as {"FirstName": "[your-name]"}.', 'agile-form-connector-for-lead-docket' ) ); } $mapping = array(); foreach ( get_object_vars( $decoded ) as $field => $template ) { $field = sanitize_text_field( (string) $field ); if ( '' === $field ) { return self::refuse( __( 'every Lead Docket field name must be filled in.', 'agile-form-connector-for-lead-docket' ) ); } if ( is_array( $template ) || is_object( $template ) ) { return self::refuse( /* translators: %s: a Lead Docket field name from the mapping, such as "FirstName". */ sprintf( __( 'the value of "%s" must be text, not a list or an object.', 'agile-form-connector-for-lead-docket' ), $field ) ); } if ( is_bool( $template ) ) { $template = $template ? '1' : '0'; } $mapping[ $field ] = sanitize_textarea_field( (string) $template ); } return array( 'mapping' => $mapping, 'error' => '', ); } /** * The mapping as the editor shows it and the database keeps it. * * @param array $mapping Field mapping. * @return string */ public static function encode_mapping( $mapping ) { return (string) wp_json_encode( $mapping, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); } /** * The form's most recent deliveries, newest first. * * @param int $form_id Contact form ID. * @return array */ public static function get_log( $form_id ) { $log = get_post_meta( $form_id, self::META_LOG, true ); return is_array( $log ) ? $log : array(); } /** * Records a delivery and keeps only the most recent ones. * * @param int $form_id Contact form ID. * @param array{time: int, code: int, error: string} $entry The delivery: when, the HTTP status (0 when no answer came) * and the connection error, if any. */ public static function add_log_entry( $form_id, $entry ) { $log = self::get_log( $form_id ); array_unshift( $log, $entry ); update_post_meta( $form_id, self::META_LOG, wp_slash( array_slice( $log, 0, self::LOG_SIZE ) ) ); } /** * The result of a refused mapping. * * @param string $reason Why the mapping was refused. * @return array{mapping: null, error: string} */ private static function refuse( $reason ) { return array( 'mapping' => null, 'error' => $reason, ); } }