<?php if ( ! defined( 'ABSPATH' ) ) exit;
* Class NF_Updates_CacheCollateFields
* This class manages the step process of running through the CacheCollateFields required update.
* It will define an object to pull data from (if necessary) to pick back up if exited early.
* It will run an upgrade function to alter the nf3_fields and nf3_field_meta tables.
* Then, it will step over each form on the site, following this process:
* - Fields that exist in the data tables but not in the cache will be deleted.
* - Fields that exist in the cache but not in the data tables will be inserted.
* - 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.
* - Fields that exist in both will be updated from the cache to ensure the data is correct.
* After completing the above for every form on the site, it will remove the data object that manages its location.
class NF_Updates_CacheCollateFields extends NF_Abstracts_RequiredUpdate
private $running = array();
* Non-associatve array of field ids from the cache.
private $field_ids = array();
* Associative array of field ids from the cache, using the field id as the key.
* $fields_by_id[ field_id ] = $settings;
private $fields_by_id = array();
* Non-associative array that tracks what we should we insert because it exists in our cache but not in the Fields table.
private $insert = array();
* Non-associatve array that tracks field ids that should be deleted from fields DB table.
private $delete = array();
* Associative array that tracks field ids that have changed.
* $submission_updates[ old_field_id ] = new_field_id;
private $submission_updates = array();
* Associatve array that tracks newly inserted fields.
* $insert_ids[ field_id ] = field_id;
private $insert_ids = array();
* Hard limit for the number of querys we run during a single step.
* Stores information about the current form being processed.
* The table names for our database queries.
* @param $data (Array) The data object passed in by the AJAX call.
* @param $running (Array) The array of required updates being run.
public function __construct( $data = array(), $running )
// Build our arguments array.
'slug' => 'CacheCollateFields',
'class_name' => 'NF_Updates_CacheCollateFields',
$this->running = $running;
// Call the parent constructor.
parent::__construct( $args );
$this->table = $this->db->prefix . 'nf3_fields';
$this->meta_table = $this->db->prefix . 'nf3_field_meta';
* Function to loop over the batch.
public function process()
// If we've not already started...
if ( ! isset( $this->running[ 0 ][ 'running' ] ) ) {
// Run our startup method.
* Get all of our database variables up and running.
* Sets up class vars that are used in subsequent methods.
* Run SQL queries to delete fields if necessary.
$this->maybe_delete_fields();
* Insert fields if necessary.
* Also sets up the class var $submission_updates with duplicate ids that need replaced.
$this->maybe_insert_fields();
* Update submission post meta if necessary.
* Uses the class var $submission_updates setup in the method above.
$this->maybe_update_submissions();
* If we have fields that exist in the DB for a form, update those with cache settings.
$this->maybe_update_fields();
* Update our form cache with any field id changes.
$this->update_form_cache();
* Saves our current location, along with any processing data we may need for the next step.
* If we're done with our step, runs cleanup instead.
* Respond to the AJAX call.
* Function to run any setup steps necessary to begin processing.
public function startup()
// Record that we're processing the update.
$this->running[ 0 ][ 'running' ] = true;
// If we're not debugging...
// Ensure that our data tables are updated.
$this->migrate( 'cache_collate_fields' );
// Set out new db version.
update_option( 'ninja_forms_db_version', '1.3' );
// Get a list of our forms...
$sql = "SELECT ID FROM `{$this->db->prefix}nf3_forms`";
$forms = $this->db->get_results( $sql, 'ARRAY_A' );
$this->running[ 0 ][ 'forms' ] = $forms;
// Record the total number of steps in this batch.
$this->running[ 0 ][ 'steps' ] = count( $forms );
// Record our current step (defaulted to 0 here).
$this->running[ 0 ][ 'current' ] = 0;
* Function to cleanup any lingering temporary elements of a required update after completion.
public function cleanup()
// Remove the current process from the array.
array_shift( $this->running );
// Record to our updates setting that this update is complete.
$this->confirm_complete();
// If we have no updates left to process...
if ( empty( $this->running ) ) {
// Call the parent cleanup method.
* Function to delete unncessary items from our existing tables.
* @param $items (Array) The list of ids to be deleted.
public function maybe_delete_fields()
if ( empty( $this->delete ) ) {
// Delete all meta for those fields.
$sql = "DELETE FROM `{$this->meta_table}` WHERE parent_id IN(" . implode( ', ', $this->delete ) . ")";
$sql = "DELETE FROM `{$this->table}` WHERE id IN(" . implode( ', ', $this->delete ) . ")";
* Most of the methods in this class use class vars to access and store data.
* This method sets the initial state of these class vars.
* $form <- reference to the form currently being processed.
* $field_ids <- non-associatve array of field ids from the cache.
* $insert <- array that tracks what we should we insert because it exists in our cache but not in the Fields table.
* $submission_updates <- array that tracks fields that have had their id changed.
* $fields_by_id <- associative array of field ids from the cache, using the field id as the key.
* If we are not running a form for the first time,
* we set class vars based on what we have been passed.
* After setting those class vars, we bail early.
* If we are running for the first time, set have to hit the database to
* get the information for class vars.
* We need to compare the fields in our form cache to those in the fields table.
* Ultimately, we're trying to make sure that our fields table matches our form cache.
* Since we're treating the cache as the truth, we want to remove fields that don't exist in the cache.
* We also want to insert any fields that exist in the cache, but not in the fields table.
* This method doesn't perform those operations, but it sets the class vars that the appropriate
* methods use to figure out what to add and remove.
private function setup_vars()
// Set the form we're currently working with.
$this->form = array_pop( $this->running[ 0 ][ 'forms' ] );
// Enable maintenance mode on the front end when the fields start processing.
$this->enable_maintenance_mode( $this->db->prefix, $this->form[ 'ID' ] );
// Get the fields for our form from the cache.
$form_cache = WPN_Helper::get_nf_cache( $this->form[ 'ID' ] );
// Create an empty $fields array.
* Loop over our cached form fields and instantiate a model for each.
* Update its settings to match those in the cache.
* Add it to our $fields array.
foreach( $form_cache[ 'fields' ] as $cached_field ){
// Create a new model for this field.
$field = new NF_Database_Models_Field( $this->db, $cached_field[ 'id' ], $this->form[ 'ID' ] );
// Update settings to match cache.
$field->update_settings( $cached_field[ 'settings' ] );
// Add this to our $fields array, using the field id as the key.
$fields[ $field->get_id() ] = $field;
* For each field in our cache, add it to our class vars:
* field_ids <- non-associatve array of field ids from the cache.
* fields_by_id <- associative array of field ids from the cache, using the field id as the key.
foreach ( $fields as $field ) {
array_push( $this->field_ids, $field->get_id() );
$this->fields_by_id[ $field->get_id() ] = $field->get_settings();
* If we're continuing a process, set our class vars appropriately.
* Bail early so that nothing else fires.
if ( isset( $this->form[ 'field_ids' ] ) ) {
$this->field_ids = $this->form[ 'field_ids' ];
$this->insert = $this->form[ 'insert' ];
$this->submission_updates = $this->form[ 'submission_updates' ];
* We need to cross reference the Fields table to see if these ids exist for this form.
* If they exist in the table, we don't need to insert them.
$sql = "SELECT id FROM `{$this->table}` WHERE parent_id = {$this->form[ 'ID' ]}";
$db_fields = $this->db->get_results( $sql, 'ARRAY_A' );
* Loop over every field that exists in the table:
* If it doesn't exist in the cache, add it to our delete class var so that it is deleted later.
* If it does exist in both, add it to our $db_field_ids array for later comparison.
foreach ( $db_fields as $field ) {
// If we have no reference to it in the cache...
if ( ! in_array( $field[ 'id' ], $this->field_ids ) ) {
// Schedule it for deletion.
array_push( $this->delete, $field[ 'id' ] );
} else { // Push the id onto our comparison array.
array_push( $db_field_ids, $field[ 'id' ] );
* Loop over every field that exists in our form cache to see if we need to insert it.
foreach ( $this->field_ids as $field ) {
// If we have no reference to it in the fields table...
if ( ! in_array( $field, $db_field_ids ) ) {
// Schedule it for insertion.
array_push( $this->insert, $field );
* Cross reference the Fields table to see if these ids exist on other Forms.
* 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.
if ( ! empty( $this->field_ids ) ) {
$sql = "SELECT id FROM `{$this->table}` WHERE id IN(" . implode( ', ', $this->field_ids ) . ") AND parent_id <> {$this->form[ 'ID' ]}";
$duplicates = $this->db->get_results( $sql, 'ARRAY_A' );
* If we got something back, there were duplicates.
if ( ! empty( $duplicates ) ) {
* Loop over our duplicates and add it to our insert class var if it isn't already there.
* Also, add this field to our submission_updates class var so that we can handle the id change later.
foreach ( $duplicates as $duplicate ) {
if ( ! in_array( $duplicate[ 'id' ], $this->insert ) ) {
array_push( $this->insert, $duplicate[ 'id' ] );
$this->submission_updates[ $duplicate[ 'id' ] ] = true;
* Our setup_vars method adds fields to the insert class var.
* Any fields that are in this array need to be inserted into our database.
* This is the first insert/update method to run, so it doesn't check lock_process.
* If the insert class var is empty, then we bail early.
private function maybe_insert_fields()
// If we don't have any items to insert, bail early.
if ( empty( $this->insert ) ) {
// Store the meta items outside the loop for faster insertion.
// While we still have items to insert...
while ( 0 < count( $this->insert ) ) {
// If we have hit our limit...
if ( 1 > $this->limit ) {
$this->lock_process = true;
// Get our item to be inserted.
$inserting = array_pop( $this->insert );
$settings = $this->fields_by_id[ $inserting ];
* We want to preserve the field ids from the cache if we can.
* To do this, we check our $this->submission_updates array for this current field.
* If it doesn't exist in the array, we can trust the cached field id.
* If it exists in that array, then this is a duplicate.
if ( ! isset( $this->submission_updates[ $inserting ] ) ) {
$maybe_field_id = intval( $inserting ); // Use the cached field id.
$maybe_field_id = 'NULL'; // Setting 'NULL' uses SQL auto-increment.
// Insert into the fields table.
$sql = "INSERT INTO `{$this->table}` ( `id`, label, `key`, `type`, parent_id, field_label, field_key, `order`, required, default_value, label_pos, personally_identifiable ) VALUES ( " .
$maybe_field_id . ", '" .
$this->prepare( $settings[ 'label' ] ) . "', '".
$this->prepare( $settings[ 'key' ] ) . "', '" .
$this->prepare( $settings[ 'type' ] ) . "', " .
intval( $this->form[ 'ID' ] ) . ", '" .
$this->prepare( $settings[ 'label' ] ) . "', '" .
$this->prepare( $settings[ 'key' ] ) . "', " .
intval( $settings[ 'order' ] ) . ", " .
intval( $settings[ 'required' ] ) . ", '" .
$this->prepare( $settings[ 'default_value' ] ) . "', '" .
$this->prepare( $settings[ 'label_pos' ] ) . "', " .
intval( $settings[ 'personally_identifiable' ] ) . " )";
// Set a default new_id for debugging.
// If we're not in debug mode...
// Get the ID of the new field.
$new_id = $this->db->insert_id;
$settings[ 'old_field_id' ] = $inserting;
// Save a reference to this insertion.
$this->insert_ids[ $inserting ] = $new_id;
// Update our submission_updates array with the new ID of this field so that we can use it later.
if ( isset ( $this->submission_updates[ $inserting ] ) ) {
$this->submission_updates[ $inserting ] = $new_id;
// Push the new ID onto our list of IDs to flush.
array_push( $flush_ids, $new_id );
// For each meta of the field...
foreach ( $settings as $meta => $value ) {
if ( ( ! empty( $value ) || '0' == $value ) ) {
// Add the data to the list.
array_push( $meta_items, "( " . intval( $new_id ) . ", '" . $meta . "', '" . $this->prepare( $value ) . "', '" . $meta . "', '" . $this->prepare( $value ) . "' )" );
// Remove the item from the list of fields.
unset( $this->fields_by_id[ $inserting ] );
$field_index = array_search( $inserting, $this->field_ids );
unset( $this->field_ids[ $field_index ] );
if ( ! empty ( $flush_ids ) ) {
// Flush our existing meta.
$sql = "DELETE FROM `{$this->meta_table}` WHERE parent_id IN(" . implode( ', ', $flush_ids ) . ")";
$sql = "INSERT INTO `{$this->meta_table}` ( parent_id, `key`, value, meta_key, meta_value ) VALUES " . implode( ', ', $meta_items );
* If we have any duplicate field ids, we need to update any existing submissions with the new field ID.
* The $this->submission_updates array will look like:
* $this->submission_updates[ original_id ] = new_id;
* Checks to see if we have any fields in our $this->submission_updates array (have a changed ID)
* Makes sure that processing isn't locked
* Loops over fields in our $this->submission_updates array
* Fetches submission post meta for the specific form ID and _field_OLDID
* Uses a SQL UPDATE statement to replace _field_OLDID with _field_NEWID
private function maybe_update_submissions()
// If we don't have any submissions to update OR the lock_process is true, bail early.
if ( empty ( $this->submission_updates ) || $this->lock_process ) {