Edit File by line
/home/barbar84/www/wp-conte.../plugins/ninja-fo.../includes/Updates
File: CacheCollateForms.php
<?php if ( ! defined( 'ABSPATH' ) ) exit;
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Class NF_Updates_CacheCollateForms
[3] Fix | Delete
*
[4] Fix | Delete
* This class manages the step process of running through the CacheCollateForms 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_forms and nf3_form_meta tables.
[7] Fix | Delete
* Then, it will step over each form on the site, following this process:
[8] Fix | Delete
* - New columns in the nf3_forms table will be populated with data from the cache.
[9] Fix | Delete
* - New and existing columns in the nf3_form_meta tables will be populated from the cache.
[10] Fix | Delete
* - A new record of the cache will be saved to the nf3_upgrades table (if it does not already exist).
[11] Fix | Delete
* After completing the above for every form on the site, it will remove the data object that manages its location.
[12] Fix | Delete
*/
[13] Fix | Delete
class NF_Updates_CacheCollateForms extends NF_Abstracts_RequiredUpdate
[14] Fix | Delete
{
[15] Fix | Delete
[16] Fix | Delete
private $data = array();
[17] Fix | Delete
[18] Fix | Delete
private $running = array();
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Stores information about the current form being processed.
[22] Fix | Delete
* @var array
[23] Fix | Delete
*/
[24] Fix | Delete
private $form;
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Declare a blacklist for settings to not be inserted.
[28] Fix | Delete
* @var array
[29] Fix | Delete
*/
[30] Fix | Delete
private $blacklist = array(
[31] Fix | Delete
'title',
[32] Fix | Delete
'objectType',
[33] Fix | Delete
'editActive',
[34] Fix | Delete
);
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* The table names for our database queries.
[38] Fix | Delete
*/
[39] Fix | Delete
private $table;
[40] Fix | Delete
private $meta_table;
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Constructor
[44] Fix | Delete
*
[45] Fix | Delete
* @param $data (Array) The data object passed in by the AJAX call.
[46] Fix | Delete
* @param $running (Array) The array of required updates being run.
[47] Fix | Delete
*
[48] Fix | Delete
* @since 3.4.0
[49] Fix | Delete
*/
[50] Fix | Delete
public function __construct( $data = array(), $running )
[51] Fix | Delete
{
[52] Fix | Delete
// Build our arguments array.
[53] Fix | Delete
$args = array(
[54] Fix | Delete
'slug' => 'CacheCollateForms',
[55] Fix | Delete
'class_name' => 'NF_Updates_CacheCollateForms',
[56] Fix | Delete
'debug' => false,
[57] Fix | Delete
);
[58] Fix | Delete
$this->data = $data;
[59] Fix | Delete
$this->running = $running;
[60] Fix | Delete
[61] Fix | Delete
// Call the parent constructor.
[62] Fix | Delete
parent::__construct( $args );
[63] Fix | Delete
[64] Fix | Delete
// Set our table names.
[65] Fix | Delete
$this->table = $this->db->prefix . 'nf3_forms';
[66] Fix | Delete
$this->meta_table = $this->db->prefix . 'nf3_form_meta';
[67] Fix | Delete
[68] Fix | Delete
// Begin processing.
[69] Fix | Delete
$this->process();
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
/**
[73] Fix | Delete
* Function to loop over the batch.
[74] Fix | Delete
*
[75] Fix | Delete
* @since 3.4.0
[76] Fix | Delete
*/
[77] Fix | Delete
public function process()
[78] Fix | Delete
{
[79] Fix | Delete
// If we've not already started...
[80] Fix | Delete
if ( ! isset( $this->running[ 0 ][ 'running' ] ) ) {
[81] Fix | Delete
// Run our startup method.
[82] Fix | Delete
$this->startup();
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
// See which form we're currently working with.
[86] Fix | Delete
$this->form = array_pop( $this->running[ 0 ][ 'forms' ] );
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* If we don't have a cache for this form, we need to add one for later processing.
[90] Fix | Delete
* We're going to check that the cache exists and create one if we don't have one.
[91] Fix | Delete
*/
[92] Fix | Delete
$cache = WPN_Helper::get_nf_cache( $this->form[ 'ID' ] );
[93] Fix | Delete
[94] Fix | Delete
if ( empty ( $cache ) ) { // No cache exists.
[95] Fix | Delete
// Call our build_nf_cache static method.
[96] Fix | Delete
$form_cache = WPN_Helper::build_nf_cache( $this->form[ 'ID' ] );
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
/**
[100] Fix | Delete
* Update our form table with the appropriate form settings.
[101] Fix | Delete
*/
[102] Fix | Delete
$this->update_form();
[103] Fix | Delete
[104] Fix | Delete
/**
[105] Fix | Delete
* Check to see if we're done with processing this form and prepare to respond.
[106] Fix | Delete
*/
[107] Fix | Delete
$this->end_of_step();
[108] Fix | Delete
[109] Fix | Delete
// Respond to the AJAX call.
[110] Fix | Delete
$this->respond();
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
/**
[114] Fix | Delete
* Function to run any setup steps necessary to begin processing.
[115] Fix | Delete
*
[116] Fix | Delete
* @since 3.4.0
[117] Fix | Delete
*/
[118] Fix | Delete
public function startup()
[119] Fix | Delete
{
[120] Fix | Delete
// Record that we're processing the update.
[121] Fix | Delete
$this->running[ 0 ][ 'running' ] = true;
[122] Fix | Delete
// If we're not debugging...
[123] Fix | Delete
if ( ! $this->debug ) {
[124] Fix | Delete
// Ensure that our data tables are updated.
[125] Fix | Delete
$this->migrate( 'cache_collate_forms' );
[126] Fix | Delete
// Set out new db version.
[127] Fix | Delete
update_option( 'ninja_forms_db_version', '1.1' );
[128] Fix | Delete
}
[129] Fix | Delete
// Get a list of our forms...
[130] Fix | Delete
$sql = "SELECT ID FROM `{$this->table}`";
[131] Fix | Delete
$forms = $this->db->get_results( $sql, 'ARRAY_A' );
[132] Fix | Delete
$this->running[ 0 ][ 'forms' ] = $forms;
[133] Fix | Delete
// Record the total number of steps in this batch.
[134] Fix | Delete
$this->running[ 0 ][ 'steps' ] = count( $forms );
[135] Fix | Delete
// Record our current step (defaulted to 0 here).
[136] Fix | Delete
$this->running[ 0 ][ 'current' ] = 0;
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
/**
[140] Fix | Delete
* Function to cleanup any lingering temporary elements of a required update after completion.
[141] Fix | Delete
*
[142] Fix | Delete
* @since 3.4.0
[143] Fix | Delete
*/
[144] Fix | Delete
public function cleanup()
[145] Fix | Delete
{
[146] Fix | Delete
// Remove the current process from the array.
[147] Fix | Delete
array_shift( $this->running );
[148] Fix | Delete
// Record to our updates setting that this update is complete.
[149] Fix | Delete
$this->confirm_complete();
[150] Fix | Delete
// If we have no updates left to process...
[151] Fix | Delete
if ( empty( $this->running ) ) {
[152] Fix | Delete
// Call the parent cleanup method.
[153] Fix | Delete
parent::cleanup();
[154] Fix | Delete
}
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
/**
[158] Fix | Delete
*
[159] Fix | Delete
* @since 3.4.0
[160] Fix | Delete
* @return void
[161] Fix | Delete
*/
[162] Fix | Delete
private function end_of_step()
[163] Fix | Delete
{
[164] Fix | Delete
// Update the upgrades table, passing in 1 for the current stage.
[165] Fix | Delete
$cache = WPN_Helper::get_nf_cache( $this->form[ 'ID' ] );
[166] Fix | Delete
WPN_Helper::update_nf_cache( $this->form[ 'ID' ], $cache, 1 );
[167] Fix | Delete
[168] Fix | Delete
// Increment our step count.
[169] Fix | Delete
$this->running[ 0 ][ 'current' ] = intval( $this->running[ 0 ][ 'current' ] ) + 1;
[170] Fix | Delete
[171] Fix | Delete
// Prepare to output our number of steps and current step.
[172] Fix | Delete
$this->response[ 'stepsTotal' ] = $this->running[ 0 ][ 'steps' ];
[173] Fix | Delete
$this->response[ 'currentStep' ] = $this->running[ 0 ][ 'current' ];
[174] Fix | Delete
[175] Fix | Delete
// If all steps have been completed...
[176] Fix | Delete
if ( empty( $this->running[ 0 ][ 'forms' ] ) ) {
[177] Fix | Delete
// Run our cleanup method.
[178] Fix | Delete
$this->cleanup();
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
// Record our current location in the process.
[182] Fix | Delete
update_option( 'ninja_forms_doing_required_updates', $this->running );
[183] Fix | Delete
// Prepare to output the number of updates remaining.
[184] Fix | Delete
$this->response[ 'updatesRemaining' ] = count( $this->running );
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
/**
[188] Fix | Delete
* Update our form table for the current form.
[189] Fix | Delete
* We have new table columns, so we want to make sure that those are populated properly.
[190] Fix | Delete
*
[191] Fix | Delete
* Also checks meta values against our $this->blacklist.
[192] Fix | Delete
*
[193] Fix | Delete
* @since 3.4.0
[194] Fix | Delete
* @updated 3.4.4
[195] Fix | Delete
* @return [type] [description]
[196] Fix | Delete
*/
[197] Fix | Delete
private function update_form()
[198] Fix | Delete
{
[199] Fix | Delete
// Get the settings for that form.
[200] Fix | Delete
$settings = Ninja_Forms()->form( $this->form[ 'ID' ] )->get()->get_settings();
[201] Fix | Delete
[202] Fix | Delete
// Get our seq_number from meta.
[203] Fix | Delete
$sql = "SELECT `value` FROM `{$this->meta_table}` WHERE `key` = '_seq_num' AND `parent_id` = " . intval( $this->form[ 'ID' ] );
[204] Fix | Delete
$result = $this->db->get_results( $sql, 'ARRAY_A' );
[205] Fix | Delete
// Default to 1.
[206] Fix | Delete
$seq_num = 1;
[207] Fix | Delete
if ( ! empty( $result[ 0 ][ 'value' ] ) ) {
[208] Fix | Delete
// If we got back something, set it to the proper value.
[209] Fix | Delete
$seq_num = intval( $result[ 0 ][ 'value' ] );
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
// If logged in is false...
[213] Fix | Delete
if ( ! $settings[ 'logged_in' ] || 'false' === $settings[ 'logged_in' ] ) {
[214] Fix | Delete
$logged_in = 0;
[215] Fix | Delete
} // Otherwise... (logged in is true.)
[216] Fix | Delete
else {
[217] Fix | Delete
$logged_in = 1;
[218] Fix | Delete
}
[219] Fix | Delete
[220] Fix | Delete
// Save the new columns to the forms table.
[221] Fix | Delete
$sql = "UPDATE `{$this->table}` SET form_title = '" . $this->prepare( $settings[ 'title' ] ) . "', default_label_pos = '" . $settings[ 'default_label_pos' ] . "', show_title = " . intval( $settings[ 'show_title' ] ) . ", clear_complete = " . intval( $settings[ 'clear_complete' ] ) . ", hide_complete = " . intval( $settings[ 'hide_complete' ] ) . ", logged_in = {$logged_in}, seq_num = {$seq_num} WHERE id = " . intval( $this->form[ 'ID' ] ) . ";";
[222] Fix | Delete
$this->query( $sql );
[223] Fix | Delete
[224] Fix | Delete
// Remove the existing meta from the form_meta table.
[225] Fix | Delete
$sql = "DELETE FROM `{$this->meta_table}` WHERE parent_id = " . intval( $this->form[ 'ID' ] );
[226] Fix | Delete
$this->query( $sql );
[227] Fix | Delete
[228] Fix | Delete
$insert_items = array();
[229] Fix | Delete
// Add _seq_num since it's protected and won't be a setting.
[230] Fix | Delete
array_push( $insert_items, "( " . intval( $this->form[ 'ID' ] ) . ", '_seq_num', '{$seq_num}', '_seq_num', '{$seq_num}' )" );
[231] Fix | Delete
// For each form setting...
[232] Fix | Delete
foreach ( $settings as $key => $setting ) {
[233] Fix | Delete
// If it's not a restricted setting...
[234] Fix | Delete
if ( ! in_array( $key, $this->blacklist ) ) {
[235] Fix | Delete
// Add it to the stack.
[236] Fix | Delete
array_push( $insert_items, "( " . intval( $this->form[ 'ID' ] ) . ", '{$key}', '" . $this->prepare( $setting ) . "', '{$key}', '" . $this->prepare( $setting ) . "' )" );
[237] Fix | Delete
}
[238] Fix | Delete
}
[239] Fix | Delete
// Insert the new meta values.
[240] Fix | Delete
$sql = "INSERT INTO `{$this->meta_table}` ( parent_id, `key`, `value`, meta_key, meta_value ) VALUES " . implode( ', ', $insert_items );
[241] Fix | Delete
$this->query( $sql );
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
}
[245] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function