Skip to main content

GravityKit\GravityMigrate\Import

Gravity Forms import class.

Source: src/Import.php:22

Details

  • Kind: class
  • Namespace: GravityKit\GravityMigrate

Properties

PropertyTypeDescription
$chunk_sizeintStores the chunk size used for imports
$data_chunk_sizesarrayChunks sizes for migrating data types.
$temp_prefixstringTemp prefix for imported tables.
$known_temp_table_names_build_countintCount of \self::get_known_temp_table_names() cache misses (rebuilds).
$import_infoarrayImport information from JSON file.

Methods

MethodDescription
__construct()Constructor.
get_instance()Returns class instance.
get_file_object()Returns the SplFileObject for the provided file.
get_num_chunks_in_file()Returns the number of chunks in an SQL file.
import_chunk()Imports a chunk of a provided SQL file into the database
is_safe_remote_url()Whether $url is safe to fetch: http(s) scheme, host resolves to one or
sanitize_form_id_list()Reduces a form-id list to positive integers, joined for direct use in an IN(...)
get_uploads_data()Creates a query to get upload meta fields attached to gravity form entries.
import_lock_check()Checks if import lock is required.
submissions_block_check()Check if Gravity Forms is currently upgrading the database.
ajax_routes()Imports the data.
cancel_import()Cancels the crashed import request.
reset_state()Clears migration state so a fresh import starts clean: drops the temporary tables,
get_skipped_statement_count()
delete_all_temp_tables()Deletes all migration temporary tables.
remove_ansi_quotes()Removes ANSI quotes from a given string and replaces them with backticks.
convert_to_temp_query()Converts a query to run on temporary tables.
is_safe_temp_query()Gate run after {@see self::convert_to_temp_query()} and before any query from
has_single_top_level_statement()Whether $sql is exactly one statement: a quote-aware scan (honoring the
str_replace_first()Replaces first instance of a string.
process_import_file()Processes the imported file.
update_gravity_forms_gutenberg_blocks()Updates the form ID in Gravity Forms Gutenberg blocks, including innerBlocks.
update_inner_blocks()Recursively updates form IDs within innerBlocks.
update_gravity_forms_classic_shortcodes()Updates the form ID in Gravity Forms classic shortcodes.
get_attr_name()Returns the attribute name.
update_gravity_forms_ids_in_content()Updates the form IDs in Post content (Gutenberg blocks and classic shortcodes).
remap_ids_in_content_using_map()Updates the ids in Post content (Gutenberg blocks and classic
remap_ids_in_content()Updates ids in Post content (Gutenberg blocks and classic shortcodes)
update_gravity_ids_in_array()Updates the form IDs in Array (Gutenberg blocks and classic shortcodes).
update_gravity_ids_in_array_using_map()Map-based counterpart to {@see update_gravity_ids_in_array()}: applies
update_gravity_post_ids_in_metas()Updates the form IDs in Post metas (Gutenberg blocks and classic shortcodes).

Method Reference

__construct()

public function __construct()

Constructor.

Since: 1.0.0

Source: src/Import.php:189

get_instance()

public static function get_instance(): Import

Returns class instance.

Returns

  • Import

Since: 1.0.0

Source: src/Import.php:216

get_file_object()

public function get_file_object( $file, $line=0, $byte_offset=null )

Returns the SplFileObject for the provided file.

Parameters

NameTypeDefaultDescription
$filestringDump file.
$lineint0Line number. Ignored when $byte_offset is given.
$byte_offsetint | nullnullWhen given, seeks directly to this byte position (O(1)) instead of $line's line-based \self::seek_spl(), which reads every line from the start of the file.

Returns

Since:

  • 1.0.0
  • 1.3.0 (Accepts an optional byte offset for O(1) resume seeking; see $byte_offset.)

Source: src/Import.php:293

get_num_chunks_in_file()

public function get_num_chunks_in_file( $file )

Returns the number of chunks in an SQL file.

Parameters

NameTypeDefaultDescription
$fileSplFileObjectSPL File.

Returns

Since:

  • 1.0.0
  • 1.3.0 (Refuses to scan a dump file larger than {@see self::MAX_DUMP_FILE_SIZE_BYTES}.)

Source: src/Import.php:371

import_chunk()

public function import_chunk( $file, $info_file_path, $chunk=0, $current_query='', $byte_offset=null )

Imports a chunk of a provided SQL file into the database

Parameters

NameTypeDefaultDescription
$filestringSQL File.
$info_file_pathstringPath of the info file.
$chunkint0Chunk of the SQL file.
$current_querystring''Remaining query.
$byte_offsetint | nullnullByte position to resume reading from; when null, seeks by line instead using $chunk.

Returns

Since:

  • 1.0.0
  • 1.3.0 (Refuses to read a dump file larger than {@see self::MAX_DUMP_FILE_SIZE_BYTES}.)
  • 1.3.0 (Accepts an optional byte offset to resume from ($byte_offset) and returns the byte offset reached, avoiding a full re-read from the start of the file.)

Source: src/Import.php:409

is_safe_remote_url()

public function is_safe_remote_url( $url )

Whether $url is safe to fetch: http(s) scheme, host resolves to one or

more addresses, passes WordPress' own wp_http_validate_url() checks, AND every resolved address is public (not loopback, private, link-local, or otherwise reserved). Resolution happens immediately before this returns, narrowing the DNS-rebind window between validation and use. A host that resolves to nothing at all fails closed (returns false) rather than falling back to a syntax-only check: an unresolvable host can't be fetched from anyway, so refusing it costs a legitimate migration nothing, while accepting it would reopen exactly the DNS-rebind/TOCTOU window this validation exists to close -- a host that answers NXDOMAIN right now and a private address by the time the real fetch happens.

Parameters

NameTypeDefaultDescription
$urlstring

Returns

  • bool

Since:

  • 1.3.0
  • 1.3.0 (Made public: {@see Settings::test_src_site()} reuses this same address-range validation for another untrusted info.json URL field (src_url).)
  • 1.3.0 (Fails closed (was fail-open) when the host doesn't currently resolve.)

Source: src/Import.php:802

sanitize_form_id_list()

public static function sanitize_form_id_list( $forms )

Reduces a form-id list to positive integers, joined for direct use in an IN(...)

clause. Form ids reach these clauses unquoted, where esc_sql() protects nothing (it escapes quotes, and an injected 1) OR (1=1 needs none), so the only safe treatment is to discard anything that is not an id. An empty result yields 0, which matches no form, rather than the IN() syntax error an empty list would produce.

Parameters

NameTypeDefaultDescription
$formsstring | arrayComma-separated ids, or an array of them.

Returns

  • string

Since: 1.3.0

Source: src/Import.php:1143

get_uploads_data()

public function get_uploads_data( $table, $where, &$uploads_data=[] )

Creates a query to get upload meta fields attached to gravity form entries.

Parameters

NameTypeDefaultDescription
$tablestringUploads meta table name.
$wherearrayArray of where statements for all tables.
&$uploads_dataarray[]Field-map cache. Populated in place on a cache miss (empty array in); reused as-is otherwise.

Returns

  • string

Since:

  • 1.0.0
  • 1.3.0 ($uploads_data is now taken by reference and populated in place, so repeated calls reuse the built field map instead of re-querying and re-decoding it.)

Source: src/Import.php:1245

import_lock_check()

public function import_lock_check( $chunk )

Checks if import lock is required.

Parameters

NameTypeDefaultDescription
$chunkstring

Returns

  • void

Throws

  • Exception

Since: 1.0.0

Source: src/Import.php:1383

submissions_block_check()

public function submissions_block_check()

Check if Gravity Forms is currently upgrading the database.

Returns

  • void

Throws

  • Exception

Since: 1.0.0

Source: src/Import.php:1450

ajax_routes()

public function ajax_routes( $routes )

Imports the data.

First, it will import the dump file in chunks. Then it will setup the data for insertion into real tables.

Parameters

NameTypeDefaultDescription
$routesarraySetting up routes for Foundation.

Returns

  • array

Since: 1.0.0

Source: src/Import.php:1470

cancel_import()

public function cancel_import()

Cancels the crashed import request.

Returns

  • void

Source: src/Import.php:4085

reset_state()

public function reset_state()

Clears migration state so a fresh import starts clean: drops the temporary tables,

deletes the extracted import files, clears the crashed-import record, and frees the import lock. Import-only by design (does not touch export state).

Returns

  • void

Since: 1.2.0

Source: src/Import.php:4100

get_skipped_statement_count()

public function get_skipped_statement_count(): int

Returns

  • int — Count of dump statements {@see self::is_safe_temp_query()} rejected (logged and skipped rather than executed) on this instance so far.

Since: 1.3.0

Source: src/Import.php:4127

delete_all_temp_tables()

public function delete_all_temp_tables()

Deletes all migration temporary tables.

Returns

  • void

Since: 1.0.0

Source: src/Import.php:4358

remove_ansi_quotes()

public function remove_ansi_quotes( $string )

Removes ANSI quotes from a given string and replaces them with backticks.

Parameters

NameTypeDefaultDescription
$stringstringString to remove ANSI quotes from.

Returns

  • string

Since: 1.0.0

Source: src/Import.php:4386

convert_to_temp_query()

public function convert_to_temp_query( $query, $export_db_prefix )

Converts a query to run on temporary tables.

The source site's table prefix is rewritten to this site's ONLY inside the statement's target table identifier, never across the whole statement: an exported row's own data can legitimately contain the source prefix as text (a path, a support note, a serialized option), and a statement-wide replacement silently rewrites that text on every migration between two sites whose prefixes differ.

Parameters

NameTypeDefaultDescription
$querystringSQL query.
$export_db_prefixstringTable prefix the bundle was exported with.

Returns

  • string

Since:

  • 1.0.0
  • 1.3.0 (Rewrites the source table prefix only within the target table identifier.)

Source: src/Import.php:4426

is_safe_temp_query()

public function is_safe_temp_query( $query ): bool

Gate run after {@see self::convert_to_temp_query()} and before any query from

an import bundle reaches the database. A query is safe only if it is one of the 3 statement shapes a legitimate export ever emits (see Export.php's write() call sites: a lone DROP TABLE IF EXISTS per table, the raw SHOW CREATE TABLE output, and INSERT INTO \table` (cols) VALUES (...)), targets exactly one of \self::get_known_temp_table_names() with NO second table reference of any kind (\self::has_single_validated_table_reference()), and is a single top-level statement (\self::has_single_top_level_statement()`).

LOCK TABLES and ALTER TABLE were previously also recognized here, but a legitimate export never emits either, so they're rejected outright now, same as any other unrecognized verb.

Parameters

NameTypeDefaultDescription
$querystringQuery already run through convert_to_temp_query().

Returns

  • bool

Since:

  • 1.3.0
  • 1.3.0 (No longer recognizes LOCK TABLES / ALTER TABLE (a legitimate export never emits them), and validates that the statement carries no second table reference past the one validated table name -- previously only the first backtick-quoted identifier was ever checked, so e.g. DROP TABLE IF EXISTS \known_temp`, `wp_users`;orINSERT INTO `known_temp` SELECT * FROM `wp_users`;` passed this gate and reached the real database.)

Source: src/Import.php:4540

has_single_top_level_statement()

public function has_single_top_level_statement( $sql ): bool

Whether $sql is exactly one statement: a quote-aware scan (honoring the

backslash-escaping \Export::mysql_escape_mimic() uses, and doubled-backtick escaping inside identifiers) confirming there is exactly one ; outside a quoted region, and it is the final non-whitespace character.

Parameters

NameTypeDefaultDescription
$sqlstring

Returns

  • bool

Since:

  • 1.3.0
  • 1.3.0 (Rejects a query carrying more than one top-level ; instead of only checking the position of the last one.)

Source: src/Import.php:4728

str_replace_first()

public static function str_replace_first( $search, $replace, $string )

Replaces first instance of a string.

Parameters

NameTypeDefaultDescription
$searchstring
$replacestring
$stringstring

Returns

  • string — replaced string.

Since: 1.0.0

Source: src/Import.php:4807

process_import_file()

public function process_import_file( $file_path, $password )

Processes the imported file.

Extract the zip file and return the info.json file.

Parameters

NameTypeDefaultDescription
$file_pathstringZIP File path.
$passwordstringOptional password.

Returns

Since:

  • 1.0.0
  • 1.3.0 (Refuses to extract an entry whose uncompressed size exceeds {@see self::MAX_DUMP_FILE_SIZE_BYTES}.)

Source: src/Import.php:4893

update_gravity_forms_gutenberg_blocks()

public function update_gravity_forms_gutenberg_blocks( $content, $old_id, $new_id, $id_type )

Updates the form ID in Gravity Forms Gutenberg blocks, including innerBlocks.

Parameters

NameTypeDefaultDescription
$contentstringPost content to search for form IDs.
$old_idstringThe old form ID to replace.
$new_idstringThe new form ID to set.
$id_type

Returns

  • string

Since: 1.0.0

Source: src/Import.php:4994

update_inner_blocks()

public function update_inner_blocks( &$blocks, $old_id, $new_id, $id_type )

Recursively updates form IDs within innerBlocks.

Parameters

NameTypeDefaultDescription
&$blocksarrayAn array of Gutenberg blocks.
$old_idstringThe old form ID to replace.
$new_idstringThe new form ID to set.
$id_type

Since: 1.0.0

Source: src/Import.php:5016

update_gravity_forms_classic_shortcodes()

public function update_gravity_forms_classic_shortcodes( $content, $old_id, $new_id, $id_type )

Updates the form ID in Gravity Forms classic shortcodes.

Parameters

NameTypeDefaultDescription
$contentstringPost content to search for form IDs.
$old_idstringThe old form ID to replace.
$new_idstringThe new form ID to set.
$id_type

Returns

  • string

Since: 1.0.0

Source: src/Import.php:5048

get_attr_name()

public function get_attr_name( $id_type, $to_sync )

Returns the attribute name.

Parameters

NameTypeDefaultDescription
$id_typestringID type.
$to_syncarrayAttributes to sync.

Returns

  • string

Since: 1.0.0

Source: src/Import.php:5094

update_gravity_forms_ids_in_content()

public function update_gravity_forms_ids_in_content( $content, $old_id, $new_id, $id_type )

Updates the form IDs in Post content (Gutenberg blocks and classic shortcodes).

Parameters

NameTypeDefaultDescription
$contentstringPost content to search for form IDs.
$old_idstringThe old form ID to replace.
$new_idstringThe new form ID to set.
$id_type

Returns

  • string

Since: 1.0.0

Source: src/Import.php:5123

remap_ids_in_content_using_map()

public function remap_ids_in_content_using_map( $content, array $id_map, $id_type )

Updates the ids in Post content (Gutenberg blocks and classic

shortcodes) for a single id type, given the full old-id-to-new-id map at once, instead of one old/new pair per call.

Applying pairs one at a time to the same content (\update_gravity_forms_ids_in_content) cascades when the id spaces overlap: rewriting old id 1 to new id 2 can leave a "2" in the content that a later pair (old id 2 -> new id 9) then matches and rewrites again. Looking every id up in the full map in a single pass guarantees each reference is rewritten at most once.

Parameters

NameTypeDefaultDescription
$contentstringPost content to search for ids.
$id_maparrayMap of old id (int|string) => new id.
$id_typestringID type (e.g. 'form_id', 'entry_id', 'view_id', 'view-id').

Returns

  • string

Since: 1.3.0

Source: src/Import.php:5153

remap_ids_in_content()

public function remap_ids_in_content( $content, array $mappings_by_id_type )

Updates ids in Post content (Gutenberg blocks and classic shortcodes)

for ALL applicable id types in a single parse + serialize pass.

\remap_ids_in_content_using_map handles one id type per call, so a post referencing several id types (e.g. a form, an entry, and a View) would otherwise be parsed and re-serialized once per id type -- an O(posts x id_types) cost. This walks the block tree once, applying every non-empty map to the attribute its id type resolves to, then serializes once; the shortcode pass mirrors this with one loop over the shortcode list checking every applicable id type per shortcode.

Each attribute is scoped to exactly one id type (\Util::get_blocks_list() / \Util::get_shortcodes_list(): no block or shortcode maps two id types to the same attribute), so applying every map in one pass cannot double-remap a single attribute -- the single-hop, non-cascading guarantee \remap_ids_in_content_using_map establishes for one id type holds here across all of them at once.

Parameters

NameTypeDefaultDescription
$contentstringPost content to search for ids.
$mappings_by_id_typearrayMap of id_type (e.g. 'form_id', 'entry_id', 'view_id', 'view-id') => (map of old id => new id). Id types with an empty or absent map are ignored entirely (no parse cost).

Returns

  • string

Since: 1.3.0

Source: src/Import.php:5287

update_gravity_ids_in_array()

public function update_gravity_ids_in_array( $array, $old_id, $new_id, $id_type )

Updates the form IDs in Array (Gutenberg blocks and classic shortcodes).

Parameters

NameTypeDefaultDescription
$arrayarray | stringArray to search for IDs.
$old_idstringThe old ID to replace.
$new_idstringThe new ID to set.
$id_type

Returns

  • array | string

Since: 1.0.0

Source: src/Import.php:5442

update_gravity_ids_in_array_using_map()

public function update_gravity_ids_in_array_using_map( $array, array $id_map, $id_type )

Map-based counterpart to {@see update_gravity_ids_in_array()}: applies

the full old-id-to-new-id map to every scalar leaf in a single pass (via \remap_ids_in_content_using_map), instead of one old/new pair at a time. An overlapping map (an old id that equals another pair's new id) applied pair-by-pair to the same value would otherwise cascade -- a freshly rewritten id matching and being rewritten again by a later pair.

Parameters

NameTypeDefaultDescription
$arrayarray | stringArray (or scalar) to search for ids.
$id_maparrayMap of old id (int|string) => new id.
$id_typestringID type.

Returns

  • array | string

Since: 1.3.0

Source: src/Import.php:5474

update_gravity_post_ids_in_metas()

public function update_gravity_post_ids_in_metas( $updated_post_ids )

Updates the form IDs in Post metas (Gutenberg blocks and classic shortcodes).

Parameters

NameTypeDefaultDescription
$updated_post_idsstringUpdated post ids against old ids.

Returns

  • bool | void

Since: 1.0.0

Source: src/Import.php:5497