GravityKit\GravityMigrate\Import
Gravity Forms import class.
Source: src/Import.php:22
Details
- Kind:
class - Namespace:
GravityKit\GravityMigrate
Properties
| Property | Type | Description |
|---|---|---|
$chunk_size | int | Stores the chunk size used for imports |
$data_chunk_sizes | array | Chunks sizes for migrating data types. |
$temp_prefix | string | Temp prefix for imported tables. |
$known_temp_table_names_build_count | int | Count of \self::get_known_temp_table_names() cache misses (rebuilds). |
$import_info | array | Import information from JSON file. |
Methods
| Method | Description |
|---|---|
__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
| Name | Type | Default | Description |
|---|---|---|---|
$file | string | Dump file. | |
$line | int | 0 | Line number. Ignored when $byte_offset is given. |
$byte_offset | int | null | null | When 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
SplFileObject| WP_Error
Since:
1.0.01.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
| Name | Type | Default | Description |
|---|---|---|---|
$file | SplFileObject | SPL File. |
Returns
int|object| WP_Error
Since:
1.0.01.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
| Name | Type | Default | Description |
|---|---|---|---|
$file | string | SQL File. | |
$info_file_path | string | Path of the info file. | |
$chunk | int | 0 | Chunk of the SQL file. |
$current_query | string | '' | Remaining query. |
$byte_offset | int | null | null | Byte position to resume reading from; when null, seeks by line instead using $chunk. |
Returns
array|object| WP_Error
Since:
1.0.01.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
| Name | Type | Default | Description |
|---|---|---|---|
$url | string |
Returns
bool
Since:
1.3.01.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
| Name | Type | Default | Description |
|---|---|---|---|
$forms | string | array | Comma-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
| Name | Type | Default | Description |
|---|---|---|---|
$table | string | Uploads meta table name. | |
$where | array | Array of where statements for all tables. | |
&$uploads_data | array | [] | Field-map cache. Populated in place on a cache miss (empty array in); reused as-is otherwise. |
Returns
string
Since:
1.0.01.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
| Name | Type | Default | Description |
|---|---|---|---|
$chunk | string |
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
| Name | Type | Default | Description |
|---|---|---|---|
$routes | array | Setting 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
| Name | Type | Default | Description |
|---|---|---|---|
$string | string | String 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
| Name | Type | Default | Description |
|---|---|---|---|
$query | string | SQL query. | |
$export_db_prefix | string | Table prefix the bundle was exported with. |
Returns
string
Since:
1.0.01.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
| Name | Type | Default | Description |
|---|---|---|---|
$query | string | Query already run through convert_to_temp_query(). |
Returns
bool
Since:
1.3.01.3.0(No longer recognizesLOCK 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
| Name | Type | Default | Description |
|---|---|---|---|
$sql | string |
Returns
bool
Since:
1.3.01.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
| Name | Type | Default | Description |
|---|---|---|---|
$search | string | ||
$replace | string | ||
$string | string |
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
| Name | Type | Default | Description |
|---|---|---|---|
$file_path | string | ZIP File path. | |
$password | string | Optional password. |
Returns
bool|mixed|void| WP_Error
Since:
1.0.01.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
| Name | Type | Default | Description |
|---|---|---|---|
$content | string | Post content to search for form IDs. | |
$old_id | string | The old form ID to replace. | |
$new_id | string | The 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
| Name | Type | Default | Description |
|---|---|---|---|
&$blocks | array | An array of Gutenberg blocks. | |
$old_id | string | The old form ID to replace. | |
$new_id | string | The 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
| Name | Type | Default | Description |
|---|---|---|---|
$content | string | Post content to search for form IDs. | |
$old_id | string | The old form ID to replace. | |
$new_id | string | The 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
| Name | Type | Default | Description |
|---|---|---|---|
$id_type | string | ID type. | |
$to_sync | array | Attributes 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
| Name | Type | Default | Description |
|---|---|---|---|
$content | string | Post content to search for form IDs. | |
$old_id | string | The old form ID to replace. | |
$new_id | string | The 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
| Name | Type | Default | Description |
|---|---|---|---|
$content | string | Post content to search for ids. | |
$id_map | array | Map of old id (int|string) => new id. | |
$id_type | string | ID 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
| Name | Type | Default | Description |
|---|---|---|---|
$content | string | Post content to search for ids. | |
$mappings_by_id_type | array | Map 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
| Name | Type | Default | Description |
|---|---|---|---|
$array | array | string | Array to search for IDs. | |
$old_id | string | The old ID to replace. | |
$new_id | string | The 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.