Edit File by line
/home/barbar84/www/wp-conte.../plugins/ninja-fo.../includes/Updates
File: CacheCollateFields.php
<?php if ( ! defined( 'ABSPATH' ) ) exit;
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Class NF_Updates_CacheCollateFields
[3] Fix | Delete
*
[4] Fix | Delete
* This class manages the step process of running through the CacheCollateFields required update.
[5] Fix | Delete
* It will define an object to pull data from (if necessary) to pick back up if exited early.
[6] Fix | Delete
* It will run an upgrade function to alter the nf3_fields and nf3_field_meta tables.
[7] Fix | Delete
* Then, it will step over each form on the site, following this process:
[8] Fix | Delete
* - Fields that exist in the data tables but not in the cache will be deleted.
[9] Fix | Delete
* - Fields that exist in the cache but not in the data tables will be inserted.
[10] Fix | Delete
* - Fields that exist in the data tables but have an incorrect form ID will be inserted as a new ID and referenced from the cache.
[11] Fix | Delete
* - Fields that exist in both will be updated from the cache to ensure the data is correct.
[12] Fix | Delete
* After completing the above for every form on the site, it will remove the data object that manages its location.
[13] Fix | Delete
*/
[14] Fix | Delete
class NF_Updates_CacheCollateFields extends NF_Abstracts_RequiredUpdate
[15] Fix | Delete
{
[16] Fix | Delete
[17] Fix | Delete
private $data = array();
[18] Fix | Delete
[19] Fix | Delete
private $running = array();
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Non-associatve array of field ids from the cache.
[23] Fix | Delete
* @var array
[24] Fix | Delete
*/
[25] Fix | Delete
private $field_ids = array();
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Associative array of field ids from the cache, using the field id as the key.
[29] Fix | Delete
* $fields_by_id[ field_id ] = $settings;
[30] Fix | Delete
* @var array
[31] Fix | Delete
*/
[32] Fix | Delete
private $fields_by_id = array();
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* Non-associative array that tracks what we should we insert because it exists in our cache but not in the Fields table.
[36] Fix | Delete
* @var array
[37] Fix | Delete
*/
[38] Fix | Delete
private $insert = array();
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* Non-associatve array that tracks field ids that should be deleted from fields DB table.
[42] Fix | Delete
* @var array
[43] Fix | Delete
*/
[44] Fix | Delete
private $delete = array();
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* Associative array that tracks field ids that have changed.
[48] Fix | Delete
* $submission_updates[ old_field_id ] = new_field_id;
[49] Fix | Delete
* @var array
[50] Fix | Delete
*/
[51] Fix | Delete
private $submission_updates = array();
[52] Fix | Delete
[53] Fix | Delete
/**
[54] Fix | Delete
* Associatve array that tracks newly inserted fields.
[55] Fix | Delete
* $insert_ids[ field_id ] = field_id;
[56] Fix | Delete
* @var array
[57] Fix | Delete
*/
[58] Fix | Delete
private $insert_ids = array();
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* Hard limit for the number of querys we run during a single step.
[62] Fix | Delete
* @var integer
[63] Fix | Delete
*/
[64] Fix | Delete
private $limit = 10;
[65] Fix | Delete
[66] Fix | Delete
/**
[67] Fix | Delete
* Stores information about the current form being processed.
[68] Fix | Delete
* @var array
[69] Fix | Delete
*/
[70] Fix | Delete
private $form;
[71] Fix | Delete
[72] Fix | Delete
/**
[73] Fix | Delete
* The table names for our database queries.
[74] Fix | Delete
*/
[75] Fix | Delete
private $table;
[76] Fix | Delete
private $meta_table;
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* Constructor
[80] Fix | Delete
*
[81] Fix | Delete
* @param $data (Array) The data object passed in by the AJAX call.
[82] Fix | Delete
* @param $running (Array) The array of required updates being run.
[83] Fix | Delete
*
[84] Fix | Delete
* @since 3.4.0
[85] Fix | Delete
*/
[86] Fix | Delete
public function __construct( $data = array(), $running )
[87] Fix | Delete
{
[88] Fix | Delete
// Build our arguments array.
[89] Fix | Delete
$args = array(
[90] Fix | Delete
'slug' => 'CacheCollateFields',
[91] Fix | Delete
'class_name' => 'NF_Updates_CacheCollateFields',
[92] Fix | Delete
'debug' => false,
[93] Fix | Delete
);
[94] Fix | Delete
$this->data = $data;
[95] Fix | Delete
$this->running = $running;
[96] Fix | Delete
[97] Fix | Delete
// Call the parent constructor.
[98] Fix | Delete
parent::__construct( $args );
[99] Fix | Delete
[100] Fix | Delete
// Set our table names.
[101] Fix | Delete
$this->table = $this->db->prefix . 'nf3_fields';
[102] Fix | Delete
$this->meta_table = $this->db->prefix . 'nf3_field_meta';
[103] Fix | Delete
[104] Fix | Delete
// Begin processing.
[105] Fix | Delete
$this->process();
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
/**
[109] Fix | Delete
* Function to loop over the batch.
[110] Fix | Delete
*
[111] Fix | Delete
* @since 3.4.0
[112] Fix | Delete
*/
[113] Fix | Delete
public function process()
[114] Fix | Delete
{
[115] Fix | Delete
// If we've not already started...
[116] Fix | Delete
if ( ! isset( $this->running[ 0 ][ 'running' ] ) ) {
[117] Fix | Delete
// Run our startup method.
[118] Fix | Delete
$this->startup();
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
/**
[122] Fix | Delete
* Get all of our database variables up and running.
[123] Fix | Delete
* Sets up class vars that are used in subsequent methods.
[124] Fix | Delete
*/
[125] Fix | Delete
$this->setup_vars();
[126] Fix | Delete
[127] Fix | Delete
[128] Fix | Delete
/**
[129] Fix | Delete
* Run SQL queries to delete fields if necessary.
[130] Fix | Delete
*/
[131] Fix | Delete
$this->maybe_delete_fields();
[132] Fix | Delete
/**
[133] Fix | Delete
* Insert fields if necessary.
[134] Fix | Delete
* Also sets up the class var $submission_updates with duplicate ids that need replaced.
[135] Fix | Delete
*/
[136] Fix | Delete
$this->maybe_insert_fields();
[137] Fix | Delete
/**
[138] Fix | Delete
* Update submission post meta if necessary.
[139] Fix | Delete
* Uses the class var $submission_updates setup in the method above.
[140] Fix | Delete
*/
[141] Fix | Delete
$this->maybe_update_submissions();
[142] Fix | Delete
/**
[143] Fix | Delete
* If we have fields that exist in the DB for a form, update those with cache settings.
[144] Fix | Delete
*/
[145] Fix | Delete
$this->maybe_update_fields();
[146] Fix | Delete
/**
[147] Fix | Delete
* Update our form cache with any field id changes.
[148] Fix | Delete
*/
[149] Fix | Delete
$this->update_form_cache();
[150] Fix | Delete
/**
[151] Fix | Delete
* Saves our current location, along with any processing data we may need for the next step.
[152] Fix | Delete
* If we're done with our step, runs cleanup instead.
[153] Fix | Delete
*/
[154] Fix | Delete
$this->end_of_step();
[155] Fix | Delete
[156] Fix | Delete
[157] Fix | Delete
[158] Fix | Delete
/**
[159] Fix | Delete
* Respond to the AJAX call.
[160] Fix | Delete
*/
[161] Fix | Delete
$this->respond();
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
[165] Fix | Delete
[166] Fix | Delete
/**
[167] Fix | Delete
* Function to run any setup steps necessary to begin processing.
[168] Fix | Delete
*
[169] Fix | Delete
* @since 3.4.0
[170] Fix | Delete
*/
[171] Fix | Delete
public function startup()
[172] Fix | Delete
{
[173] Fix | Delete
// Record that we're processing the update.
[174] Fix | Delete
$this->running[ 0 ][ 'running' ] = true;
[175] Fix | Delete
// If we're not debugging...
[176] Fix | Delete
if ( ! $this->debug ) {
[177] Fix | Delete
// Ensure that our data tables are updated.
[178] Fix | Delete
$this->migrate( 'cache_collate_fields' );
[179] Fix | Delete
// Set out new db version.
[180] Fix | Delete
update_option( 'ninja_forms_db_version', '1.3' );
[181] Fix | Delete
}
[182] Fix | Delete
// Get a list of our forms...
[183] Fix | Delete
$sql = "SELECT ID FROM `{$this->db->prefix}nf3_forms`";
[184] Fix | Delete
$forms = $this->db->get_results( $sql, 'ARRAY_A' );
[185] Fix | Delete
$this->running[ 0 ][ 'forms' ] = $forms;
[186] Fix | Delete
// Record the total number of steps in this batch.
[187] Fix | Delete
$this->running[ 0 ][ 'steps' ] = count( $forms );
[188] Fix | Delete
// Record our current step (defaulted to 0 here).
[189] Fix | Delete
$this->running[ 0 ][ 'current' ] = 0;
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
/**
[193] Fix | Delete
* Function to cleanup any lingering temporary elements of a required update after completion.
[194] Fix | Delete
*
[195] Fix | Delete
* @since 3.4.0
[196] Fix | Delete
*/
[197] Fix | Delete
public function cleanup()
[198] Fix | Delete
{
[199] Fix | Delete
// Remove the current process from the array.
[200] Fix | Delete
array_shift( $this->running );
[201] Fix | Delete
// Record to our updates setting that this update is complete.
[202] Fix | Delete
$this->confirm_complete();
[203] Fix | Delete
// If we have no updates left to process...
[204] Fix | Delete
if ( empty( $this->running ) ) {
[205] Fix | Delete
// Call the parent cleanup method.
[206] Fix | Delete
parent::cleanup();
[207] Fix | Delete
}
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
/**
[211] Fix | Delete
* Function to delete unncessary items from our existing tables.
[212] Fix | Delete
*
[213] Fix | Delete
* @param $items (Array) The list of ids to be deleted.
[214] Fix | Delete
*
[215] Fix | Delete
* @since 3.4.0
[216] Fix | Delete
*/
[217] Fix | Delete
public function maybe_delete_fields()
[218] Fix | Delete
{
[219] Fix | Delete
if ( empty( $this->delete ) ) {
[220] Fix | Delete
return false;
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
// Delete all meta for those fields.
[224] Fix | Delete
$sql = "DELETE FROM `{$this->meta_table}` WHERE parent_id IN(" . implode( ', ', $this->delete ) . ")";
[225] Fix | Delete
$this->query( $sql );
[226] Fix | Delete
// Delete the fields.
[227] Fix | Delete
$sql = "DELETE FROM `{$this->table}` WHERE id IN(" . implode( ', ', $this->delete ) . ")";
[228] Fix | Delete
$this->query( $sql );
[229] Fix | Delete
[230] Fix | Delete
$this->delete = array();
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
/**
[234] Fix | Delete
* Most of the methods in this class use class vars to access and store data.
[235] Fix | Delete
*
[236] Fix | Delete
* This method sets the initial state of these class vars.
[237] Fix | Delete
* Class vars include:
[238] Fix | Delete
* $form <- reference to the form currently being processed.
[239] Fix | Delete
* $field_ids <- non-associatve array of field ids from the cache.
[240] Fix | Delete
* $insert <- array that tracks what we should we insert because it exists in our cache but not in the Fields table.
[241] Fix | Delete
* $submission_updates <- array that tracks fields that have had their id changed.
[242] Fix | Delete
* $fields_by_id <- associative array of field ids from the cache, using the field id as the key.
[243] Fix | Delete
*
[244] Fix | Delete
* If we are not running a form for the first time,
[245] Fix | Delete
* we set class vars based on what we have been passed.
[246] Fix | Delete
* After setting those class vars, we bail early.
[247] Fix | Delete
*
[248] Fix | Delete
* If we are running for the first time, set have to hit the database to
[249] Fix | Delete
* get the information for class vars.
[250] Fix | Delete
*
[251] Fix | Delete
* We need to compare the fields in our form cache to those in the fields table.
[252] Fix | Delete
* Ultimately, we're trying to make sure that our fields table matches our form cache.
[253] Fix | Delete
*
[254] Fix | Delete
* Since we're treating the cache as the truth, we want to remove fields that don't exist in the cache.
[255] Fix | Delete
* We also want to insert any fields that exist in the cache, but not in the fields table.
[256] Fix | Delete
*
[257] Fix | Delete
* This method doesn't perform those operations, but it sets the class vars that the appropriate
[258] Fix | Delete
* methods use to figure out what to add and remove.
[259] Fix | Delete
*
[260] Fix | Delete
* @since 3.4.0
[261] Fix | Delete
* @return void
[262] Fix | Delete
*/
[263] Fix | Delete
private function setup_vars()
[264] Fix | Delete
{
[265] Fix | Delete
// Set the form we're currently working with.
[266] Fix | Delete
$this->form = array_pop( $this->running[ 0 ][ 'forms' ] );
[267] Fix | Delete
// Enable maintenance mode on the front end when the fields start processing.
[268] Fix | Delete
$this->enable_maintenance_mode( $this->db->prefix, $this->form[ 'ID' ] );
[269] Fix | Delete
[270] Fix | Delete
// Get the fields for our form from the cache.
[271] Fix | Delete
$form_cache = WPN_Helper::get_nf_cache( $this->form[ 'ID' ] );
[272] Fix | Delete
// Create an empty $fields array.
[273] Fix | Delete
$fields = array();
[274] Fix | Delete
/**
[275] Fix | Delete
* Loop over our cached form fields and instantiate a model for each.
[276] Fix | Delete
* Update its settings to match those in the cache.
[277] Fix | Delete
* Add it to our $fields array.
[278] Fix | Delete
*/
[279] Fix | Delete
foreach( $form_cache[ 'fields' ] as $cached_field ){
[280] Fix | Delete
// Create a new model for this field.
[281] Fix | Delete
$field = new NF_Database_Models_Field( $this->db, $cached_field[ 'id' ], $this->form[ 'ID' ] );
[282] Fix | Delete
// Update settings to match cache.
[283] Fix | Delete
$field->update_settings( $cached_field[ 'settings' ] );
[284] Fix | Delete
// Add this to our $fields array, using the field id as the key.
[285] Fix | Delete
$fields[ $field->get_id() ] = $field;
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
/**
[289] Fix | Delete
* For each field in our cache, add it to our class vars:
[290] Fix | Delete
* field_ids <- non-associatve array of field ids from the cache.
[291] Fix | Delete
* fields_by_id <- associative array of field ids from the cache, using the field id as the key.
[292] Fix | Delete
*/
[293] Fix | Delete
foreach ( $fields as $field ) {
[294] Fix | Delete
array_push( $this->field_ids, $field->get_id() );
[295] Fix | Delete
$this->fields_by_id[ $field->get_id() ] = $field->get_settings();
[296] Fix | Delete
}
[297] Fix | Delete
[298] Fix | Delete
/**
[299] Fix | Delete
* If we're continuing a process, set our class vars appropriately.
[300] Fix | Delete
* Bail early so that nothing else fires.
[301] Fix | Delete
*/
[302] Fix | Delete
if ( isset( $this->form[ 'field_ids' ] ) ) {
[303] Fix | Delete
$this->field_ids = $this->form[ 'field_ids' ];
[304] Fix | Delete
$this->insert = $this->form[ 'insert' ];
[305] Fix | Delete
$this->submission_updates = $this->form[ 'submission_updates' ];
[306] Fix | Delete
return false;
[307] Fix | Delete
}
[308] Fix | Delete
/**
[309] Fix | Delete
* We need to cross reference the Fields table to see if these ids exist for this form.
[310] Fix | Delete
* If they exist in the table, we don't need to insert them.
[311] Fix | Delete
*/
[312] Fix | Delete
$sql = "SELECT id FROM `{$this->table}` WHERE parent_id = {$this->form[ 'ID' ]}";
[313] Fix | Delete
$db_fields = $this->db->get_results( $sql, 'ARRAY_A' );
[314] Fix | Delete
$db_field_ids = array();
[315] Fix | Delete
/**
[316] Fix | Delete
* Loop over every field that exists in the table:
[317] Fix | Delete
* If it doesn't exist in the cache, add it to our delete class var so that it is deleted later.
[318] Fix | Delete
* If it does exist in both, add it to our $db_field_ids array for later comparison.
[319] Fix | Delete
*/
[320] Fix | Delete
foreach ( $db_fields as $field ) {
[321] Fix | Delete
// If we have no reference to it in the cache...
[322] Fix | Delete
if ( ! in_array( $field[ 'id' ], $this->field_ids ) ) {
[323] Fix | Delete
// Schedule it for deletion.
[324] Fix | Delete
array_push( $this->delete, $field[ 'id' ] );
[325] Fix | Delete
} else { // Push the id onto our comparison array.
[326] Fix | Delete
array_push( $db_field_ids, $field[ 'id' ] );
[327] Fix | Delete
}
[328] Fix | Delete
}
[329] Fix | Delete
[330] Fix | Delete
/**
[331] Fix | Delete
* Loop over every field that exists in our form cache to see if we need to insert it.
[332] Fix | Delete
*/
[333] Fix | Delete
foreach ( $this->field_ids as $field ) {
[334] Fix | Delete
// If we have no reference to it in the fields table...
[335] Fix | Delete
if ( ! in_array( $field, $db_field_ids ) ) {
[336] Fix | Delete
// Schedule it for insertion.
[337] Fix | Delete
array_push( $this->insert, $field );
[338] Fix | Delete
}
[339] Fix | Delete
}
[340] Fix | Delete
/**
[341] Fix | Delete
* Cross reference the Fields table to see if these ids exist on other Forms.
[342] Fix | Delete
* If an id exists on another form, then we need to change the current field's id and add that field to our submission_updates class var.
[343] Fix | Delete
*/
[344] Fix | Delete
if ( ! empty( $this->field_ids ) ) {
[345] Fix | Delete
$sql = "SELECT id FROM `{$this->table}` WHERE id IN(" . implode( ', ', $this->field_ids ) . ") AND parent_id <> {$this->form[ 'ID' ]}";
[346] Fix | Delete
$duplicates = $this->db->get_results( $sql, 'ARRAY_A' );
[347] Fix | Delete
} else {
[348] Fix | Delete
$duplicates = array();
[349] Fix | Delete
}
[350] Fix | Delete
/**
[351] Fix | Delete
* If we got something back, there were duplicates.
[352] Fix | Delete
*/
[353] Fix | Delete
if ( ! empty( $duplicates ) ) {
[354] Fix | Delete
/**
[355] Fix | Delete
* Loop over our duplicates and add it to our insert class var if it isn't already there.
[356] Fix | Delete
* Also, add this field to our submission_updates class var so that we can handle the id change later.
[357] Fix | Delete
*/
[358] Fix | Delete
foreach ( $duplicates as $duplicate ) {
[359] Fix | Delete
if ( ! in_array( $duplicate[ 'id' ], $this->insert ) ) {
[360] Fix | Delete
array_push( $this->insert, $duplicate[ 'id' ] );
[361] Fix | Delete
}
[362] Fix | Delete
[363] Fix | Delete
$this->submission_updates[ $duplicate[ 'id' ] ] = true;
[364] Fix | Delete
}
[365] Fix | Delete
}
[366] Fix | Delete
}
[367] Fix | Delete
[368] Fix | Delete
/**
[369] Fix | Delete
* Our setup_vars method adds fields to the insert class var.
[370] Fix | Delete
* Any fields that are in this array need to be inserted into our database.
[371] Fix | Delete
*
[372] Fix | Delete
* This is the first insert/update method to run, so it doesn't check lock_process.
[373] Fix | Delete
* If the insert class var is empty, then we bail early.
[374] Fix | Delete
*
[375] Fix | Delete
* @since 3.4.0
[376] Fix | Delete
* @return void
[377] Fix | Delete
*/
[378] Fix | Delete
private function maybe_insert_fields()
[379] Fix | Delete
{
[380] Fix | Delete
// If we don't have any items to insert, bail early.
[381] Fix | Delete
if ( empty( $this->insert ) ) {
[382] Fix | Delete
return false;
[383] Fix | Delete
}
[384] Fix | Delete
[385] Fix | Delete
// Store the meta items outside the loop for faster insertion.
[386] Fix | Delete
$meta_items = array();
[387] Fix | Delete
$flush_ids = array();
[388] Fix | Delete
// While we still have items to insert...
[389] Fix | Delete
while ( 0 < count( $this->insert ) ) {
[390] Fix | Delete
// If we have hit our limit...
[391] Fix | Delete
if ( 1 > $this->limit ) {
[392] Fix | Delete
// Lock processing.
[393] Fix | Delete
$this->lock_process = true;
[394] Fix | Delete
// Exit the loop.
[395] Fix | Delete
break;
[396] Fix | Delete
}
[397] Fix | Delete
// Get our item to be inserted.
[398] Fix | Delete
$inserting = array_pop( $this->insert );
[399] Fix | Delete
$settings = $this->fields_by_id[ $inserting ];
[400] Fix | Delete
[401] Fix | Delete
/*
[402] Fix | Delete
* We want to preserve the field ids from the cache if we can.
[403] Fix | Delete
* To do this, we check our $this->submission_updates array for this current field.
[404] Fix | Delete
* If it doesn't exist in the array, we can trust the cached field id.
[405] Fix | Delete
* If it exists in that array, then this is a duplicate.
[406] Fix | Delete
*/
[407] Fix | Delete
if ( ! isset( $this->submission_updates[ $inserting ] ) ) {
[408] Fix | Delete
$maybe_field_id = intval( $inserting ); // Use the cached field id.
[409] Fix | Delete
} else {
[410] Fix | Delete
$maybe_field_id = 'NULL'; // Setting 'NULL' uses SQL auto-increment.
[411] Fix | Delete
}
[412] Fix | Delete
[413] Fix | Delete
// Insert into the fields table.
[414] Fix | Delete
$sql = "INSERT INTO `{$this->table}` ( `id`, label, `key`, `type`, parent_id, field_label, field_key, `order`, required, default_value, label_pos, personally_identifiable ) VALUES ( " .
[415] Fix | Delete
$maybe_field_id . ", '" .
[416] Fix | Delete
$this->prepare( $settings[ 'label' ] ) . "', '".
[417] Fix | Delete
$this->prepare( $settings[ 'key' ] ) . "', '" .
[418] Fix | Delete
$this->prepare( $settings[ 'type' ] ) . "', " .
[419] Fix | Delete
intval( $this->form[ 'ID' ] ) . ", '" .
[420] Fix | Delete
$this->prepare( $settings[ 'label' ] ) . "', '" .
[421] Fix | Delete
$this->prepare( $settings[ 'key' ] ) . "', " .
[422] Fix | Delete
intval( $settings[ 'order' ] ) . ", " .
[423] Fix | Delete
intval( $settings[ 'required' ] ) . ", '" .
[424] Fix | Delete
$this->prepare( $settings[ 'default_value' ] ) . "', '" .
[425] Fix | Delete
$this->prepare( $settings[ 'label_pos' ] ) . "', " .
[426] Fix | Delete
intval( $settings[ 'personally_identifiable' ] ) . " )";
[427] Fix | Delete
[428] Fix | Delete
$this->query( $sql );
[429] Fix | Delete
[430] Fix | Delete
// Set a default new_id for debugging.
[431] Fix | Delete
$new_id = 0;
[432] Fix | Delete
// If we're not in debug mode...
[433] Fix | Delete
if ( ! $this->debug ) {
[434] Fix | Delete
// Get the ID of the new field.
[435] Fix | Delete
$new_id = $this->db->insert_id;
[436] Fix | Delete
$settings[ 'old_field_id' ] = $inserting;
[437] Fix | Delete
}
[438] Fix | Delete
// Save a reference to this insertion.
[439] Fix | Delete
$this->insert_ids[ $inserting ] = $new_id;
[440] Fix | Delete
[441] Fix | Delete
// Update our submission_updates array with the new ID of this field so that we can use it later.
[442] Fix | Delete
if ( isset ( $this->submission_updates[ $inserting ] ) ) {
[443] Fix | Delete
$this->submission_updates[ $inserting ] = $new_id;
[444] Fix | Delete
}
[445] Fix | Delete
[446] Fix | Delete
// Push the new ID onto our list of IDs to flush.
[447] Fix | Delete
array_push( $flush_ids, $new_id );
[448] Fix | Delete
[449] Fix | Delete
// For each meta of the field...
[450] Fix | Delete
foreach ( $settings as $meta => $value ) {
[451] Fix | Delete
// If it's not empty...
[452] Fix | Delete
if ( ( ! empty( $value ) || '0' == $value ) ) {
[453] Fix | Delete
// Add the data to the list.
[454] Fix | Delete
array_push( $meta_items, "( " . intval( $new_id ) . ", '" . $meta . "', '" . $this->prepare( $value ) . "', '" . $meta . "', '" . $this->prepare( $value ) . "' )" );
[455] Fix | Delete
}
[456] Fix | Delete
}
[457] Fix | Delete
// Remove the item from the list of fields.
[458] Fix | Delete
unset( $this->fields_by_id[ $inserting ] );
[459] Fix | Delete
$field_index = array_search( $inserting, $this->field_ids );
[460] Fix | Delete
unset( $this->field_ids[ $field_index ] );
[461] Fix | Delete
// Reduce the limit.
[462] Fix | Delete
$this->limit--;
[463] Fix | Delete
}
[464] Fix | Delete
[465] Fix | Delete
if ( ! empty ( $flush_ids ) ) {
[466] Fix | Delete
// Flush our existing meta.
[467] Fix | Delete
$sql = "DELETE FROM `{$this->meta_table}` WHERE parent_id IN(" . implode( ', ', $flush_ids ) . ")";
[468] Fix | Delete
$this->query( $sql );
[469] Fix | Delete
}
[470] Fix | Delete
[471] Fix | Delete
// Insert our meta.
[472] Fix | Delete
$sql = "INSERT INTO `{$this->meta_table}` ( parent_id, `key`, value, meta_key, meta_value ) VALUES " . implode( ', ', $meta_items );
[473] Fix | Delete
$this->query( $sql );
[474] Fix | Delete
}
[475] Fix | Delete
[476] Fix | Delete
/**
[477] Fix | Delete
* If we have any duplicate field ids, we need to update any existing submissions with the new field ID.
[478] Fix | Delete
*
[479] Fix | Delete
* The $this->submission_updates array will look like:
[480] Fix | Delete
*
[481] Fix | Delete
* $this->submission_updates[ original_id ] = new_id;
[482] Fix | Delete
*
[483] Fix | Delete
* This method:
[484] Fix | Delete
* Checks to see if we have any fields in our $this->submission_updates array (have a changed ID)
[485] Fix | Delete
* Makes sure that processing isn't locked
[486] Fix | Delete
* Loops over fields in our $this->submission_updates array
[487] Fix | Delete
* Fetches submission post meta for the specific form ID and _field_OLDID
[488] Fix | Delete
* Uses a SQL UPDATE statement to replace _field_OLDID with _field_NEWID
[489] Fix | Delete
*
[490] Fix | Delete
* @since 3.4.0
[491] Fix | Delete
* @return void
[492] Fix | Delete
*/
[493] Fix | Delete
private function maybe_update_submissions()
[494] Fix | Delete
{
[495] Fix | Delete
// If we don't have any submissions to update OR the lock_process is true, bail early.
[496] Fix | Delete
if ( empty ( $this->submission_updates ) || $this->lock_process ) {
[497] Fix | Delete
return false;
[498] Fix | Delete
}
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function