$forms Forms. * @return array */ public static function forms( array $forms ): array { return array( 'subtleforms' => self::VERSION, 'kind' => 'forms', 'exported_at' => gmdate( 'c' ), 'forms' => array_values( $forms ), ); } /** * A submissions package for one form. * * @param array $form Form id and title. * @param array $submissions Rows. * @return array */ public static function submissions( array $form, array $submissions ): array { return array( 'subtleforms' => self::VERSION, 'kind' => 'submissions', 'exported_at' => gmdate( 'c' ), 'form' => array( 'id' => (int) ( $form['id'] ?? 0 ), 'title' => (string) ( $form['title'] ?? '' ), ), 'submissions' => array_values( $submissions ), ); } /** * Forms inside a package, or an error message. * * @param array $package Decoded JSON. * @return array{forms: array, error: string} */ public static function readForms( array $package ): array { if ( ( $package['kind'] ?? '' ) !== 'forms' || (int) ( $package['subtleforms'] ?? 0 ) !== self::VERSION ) { return array( 'forms' => array(), 'error' => __( 'This file is not a SubtleForms forms export.', 'subtleforms' ), ); } $forms = $package['forms'] ?? null; if ( ! is_array( $forms ) || ! $forms ) { return array( 'forms' => array(), 'error' => __( 'The export does not contain any forms.', 'subtleforms' ), ); } if ( count( $forms ) > self::MAX_FORMS ) { return array( 'forms' => array(), 'error' => sprintf( /* translators: %d: maximum number of forms */ __( 'Import up to %d forms at a time.', 'subtleforms' ), self::MAX_FORMS ), ); } $clean = array(); foreach ( $forms as $form ) { if ( ! is_array( $form ) || ! is_array( $form['schema'] ?? null ) ) { continue; } $status = (string) ( $form['status'] ?? 'draft' ); if ( ! in_array( $status, array( 'draft', 'published' ), true ) ) { $status = 'draft'; } $clean[] = array( 'title' => self::title( (string) ( $form['title'] ?? '' ) ), 'status' => $status, 'schema' => $form['schema'], ); } if ( ! $clean ) { return array( 'forms' => array(), 'error' => __( 'None of the forms in this file could be read.', 'subtleforms' ), ); } return array( 'forms' => $clean, 'error' => '', ); } /** * Submissions inside a package, or an error message. * * @param array $package Decoded JSON. * @return array{submissions: array, error: string} */ public static function readSubmissions( array $package ): array { if ( ( $package['kind'] ?? '' ) !== 'submissions' || (int) ( $package['subtleforms'] ?? 0 ) !== self::VERSION ) { return array( 'submissions' => array(), 'error' => __( 'This file is not a SubtleForms submissions export.', 'subtleforms' ), ); } $rows = $package['submissions'] ?? null; if ( ! is_array( $rows ) || ! $rows ) { return array( 'submissions' => array(), 'error' => __( 'The export does not contain any submissions.', 'subtleforms' ), ); } if ( count( $rows ) > self::MAX_SUBMISSIONS ) { return array( 'submissions' => array(), 'error' => sprintf( /* translators: %d: maximum number of submissions */ __( 'Import up to %d submissions at a time.', 'subtleforms' ), self::MAX_SUBMISSIONS ), ); } $clean = array(); foreach ( $rows as $row ) { if ( ! is_array( $row ) || ! is_array( $row['payload'] ?? null ) ) { continue; } $raw_status = (string) ( $row['status'] ?? 'unread' ); if ( 'trash' === $raw_status ) { continue; } $clean[] = array( 'entry_id' => absint( $row['entry_id'] ?? 0 ), 'status' => self::status( $raw_status ), 'is_read' => empty( $row['is_read'] ) ? 0 : 1, 'starred' => empty( $row['starred'] ) ? 0 : 1, 'created_at' => self::timestamp( (string) ( $row['created_at'] ?? '' ) ), 'payload' => $row['payload'], 'meta' => self::entryMeta( $row ), ); } if ( ! $clean ) { return array( 'submissions' => array(), 'error' => __( 'None of the submissions in this file could be read.', 'subtleforms' ), ); } return array( 'submissions' => $clean, 'error' => '', ); } /** * The entry meta records that travel with an entry: every record (an * array) another plugin saved on the entry, so an entry keeps what an * add-on stored on it even when that add-on is not active. The records * this plugin writes itself (see OWN_META) never travel. * * @param array $record Stored entry meta, or an exported row. * @return string[] Meta keys. */ public static function entryMetaKeys( array $record ): array { $keys = array(); foreach ( $record as $key => $value ) { if ( ! is_string( $key ) || ! is_array( $value ) || ! $value ) { continue; } if ( sanitize_key( $key ) !== $key || in_array( $key, self::OWN_META, true ) || in_array( $key, self::ROW_FIELDS, true ) ) { continue; } $json = wp_json_encode( $value ); if ( false === $json || strlen( $json ) > self::MAX_META_RECORD ) { continue; } $keys[] = $key; } /** * Entry meta records that go into an export and come back on import. * The default is every record another plugin saved on the entry. An * add-on narrows the list to the records it wants to travel. * * @since 2.7.2 * * @param string[] $keys Meta keys. * @param array $record Stored entry meta, or an exported row. */ $keys = apply_filters( 'subtleforms/transfer/entry_meta_keys', $keys, $record ); $out = array(); foreach ( (array) $keys as $key ) { if ( is_string( $key ) && '' !== $key && sanitize_key( $key ) === $key && ! in_array( $key, self::OWN_META, true ) && ! in_array( $key, self::ROW_FIELDS, true ) ) { $out[ $key ] = $key; } } return array_values( $out ); } /** * The records that travel with an entry, cleaned for storage: text is * sanitised, numbers and booleans kept, nesting limited, and anything that * looks like a secret (a key naming a token, secret, password or API key) * left out. * * @param array $record Stored entry meta, or an exported row. * @return array */ public static function entryMeta( array $record ): array { $out = array(); foreach ( self::entryMetaKeys( $record ) as $key ) { if ( isset( $record[ $key ] ) && is_array( $record[ $key ] ) && $record[ $key ] ) { $clean = self::cleanRecord( $record[ $key ], 0 ); if ( $clean ) { $out[ $key ] = $clean; } } } return $out; } /** * Clean a stored record: text is sanitised, numbers and booleans kept, * nesting limited. * * @param array $record Raw record. * @param int $depth Current depth. * @return array */ private static function cleanRecord( array $record, int $depth ): array { $out = array(); foreach ( $record as $key => $value ) { $key = is_int( $key ) ? $key : sanitize_key( (string) $key ); if ( '' === $key || ( is_string( $key ) && preg_match( '/secret|token|passw|api_?key|authorization|credential/i', $key ) ) ) { continue; } if ( is_array( $value ) ) { if ( $depth < 4 ) { $out[ $key ] = self::cleanRecord( $value, $depth + 1 ); } } elseif ( is_int( $value ) || is_float( $value ) || is_bool( $value ) || null === $value ) { $out[ $key ] = $value; } elseif ( is_scalar( $value ) ) { $out[ $key ] = sanitize_text_field( (string) $value ); } } return $out; } /** * Remove secret answers. The keys are the form's password fields. * * @param array $payload Stored answers. * @param string[] $secretKeys Field keys. * @return array */ public static function withoutSecrets( array $payload, array $secretKeys ): array { foreach ( $secretKeys as $key ) { unset( $payload[ $key ] ); } return $payload; } /** * A short title. * * @param string $title Raw title. * @return string */ public static function title( string $title ): string { $title = trim( (string) preg_replace( '/<[^>]*>/', '', $title ) ); if ( function_exists( 'mb_substr' ) ) { $title = mb_substr( $title, 0, 200 ); } else { $title = substr( $title, 0, 200 ); } return '' !== $title ? $title : __( 'Imported form', 'subtleforms' ); } /** * A status we will store. Trash is never imported. * * @param string $status Raw status. * @return string */ public static function status( string $status ): string { $allowed = array( 'unread', 'read', 'completed', 'saved', 'flagged', 'payment_pending', 'payment_failed', 'spam', 'failed', 'processing', 'archived' ); return in_array( $status, $allowed, true ) ? $status : 'unread'; } /** * A UTC timestamp, or an empty string when it is not one. * * @param string $value Raw time. * @return string */ public static function timestamp( string $value ): string { $value = trim( $value ); if ( preg_match( '/^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}/', $value ) ) { return substr( str_replace( 'T', ' ', $value ), 0, 19 ); } return ''; } }