Edit File by line
/home/barbar84/www/wp-conte.../plugins/ninja-fo.../includes/Updates
File: CacheCollateActions.php
<?php if ( ! defined( 'ABSPATH' ) ) exit;
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Class NF_Updates_CacheCollateActions
[3] Fix | Delete
*
[4] Fix | Delete
* This class manages the step process of running through the CacheCollateActions 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_actions and nf3_action_meta tables.
[7] Fix | Delete
* Then, it will step over each form on the site, following this process:
[8] Fix | Delete
* - Actions that exist in the data tables but not in the cache will be deleted.
[9] Fix | Delete
* - Actions that exist in the cache but not in the data tables will be inserted.
[10] Fix | Delete
* - Actions 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
* - Actions 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_CacheCollateActions 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
* Stores information about the current form being processed.
[23] Fix | Delete
* @var array
[24] Fix | Delete
*/
[25] Fix | Delete
private $form;
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Stores the actions for the current form being processed.
[29] Fix | Delete
* @var array
[30] Fix | Delete
*/
[31] Fix | Delete
private $actions;
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* Associative array of actions keyed by action id.
[35] Fix | Delete
* @var array
[36] Fix | Delete
*/
[37] Fix | Delete
private $actions_by_id = array();
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Non-associative array of action ids.
[41] Fix | Delete
* @var array
[42] Fix | Delete
*/
[43] Fix | Delete
private $action_ids = array();
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* Hard limit for the number of querys we run during a single step.
[47] Fix | Delete
* @var integer
[48] Fix | Delete
*/
[49] Fix | Delete
private $limit = 10;
[50] Fix | Delete
[51] Fix | Delete
/**
[52] Fix | Delete
* Array of action ids that need an update.
[53] Fix | Delete
* @var array
[54] Fix | Delete
*/
[55] Fix | Delete
private $update = array();
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* List of setting keys we don't want to save in the database.
[59] Fix | Delete
* @var array
[60] Fix | Delete
*/
[61] Fix | Delete
private $blacklist = array(
[62] Fix | Delete
'objectType',
[63] Fix | Delete
'objectDomain',
[64] Fix | Delete
'editActive',
[65] Fix | Delete
'title',
[66] Fix | Delete
'key',
[67] Fix | Delete
);
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* The table names for our database queries.
[71] Fix | Delete
*/
[72] Fix | Delete
private $table;
[73] Fix | Delete
private $meta_table;
[74] Fix | Delete
[75] Fix | Delete
/**
[76] Fix | Delete
* Constructor
[77] Fix | Delete
*
[78] Fix | Delete
* @param $data (Array) The data object passed in by the AJAX call.
[79] Fix | Delete
* @param $running (Array) The array of required updates being run.
[80] Fix | Delete
*
[81] Fix | Delete
* @since 3.4.0
[82] Fix | Delete
*/
[83] Fix | Delete
public function __construct( $data = array(), $running )
[84] Fix | Delete
{
[85] Fix | Delete
// Build our arguments array.
[86] Fix | Delete
$args = array(
[87] Fix | Delete
'slug' => 'CacheCollateActions',
[88] Fix | Delete
'class_name' => 'NF_Updates_CacheCollateActions',
[89] Fix | Delete
'debug' => false,
[90] Fix | Delete
);
[91] Fix | Delete
$this->data = $data;
[92] Fix | Delete
$this->running = $running;
[93] Fix | Delete
[94] Fix | Delete
// Call the parent constructor.
[95] Fix | Delete
parent::__construct( $args );
[96] Fix | Delete
[97] Fix | Delete
// Set our table names.
[98] Fix | Delete
$this->table = $this->db->prefix . 'nf3_actions';
[99] Fix | Delete
$this->meta_table = $this->db->prefix . 'nf3_action_meta';
[100] Fix | Delete
[101] Fix | Delete
// Begin processing.
[102] Fix | Delete
$this->process();
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* Function to loop over the batch.
[107] Fix | Delete
*
[108] Fix | Delete
* @since 3.4.0
[109] Fix | Delete
*/
[110] Fix | Delete
public function process()
[111] Fix | Delete
{
[112] Fix | Delete
// If we've not already started...
[113] Fix | Delete
if ( ! isset( $this->running[ 0 ][ 'running' ] ) ) {
[114] Fix | Delete
// Run our startup method.
[115] Fix | Delete
$this->startup();
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
/**
[119] Fix | Delete
* Get all of our database variables up and running.
[120] Fix | Delete
* Sets up class vars that are used in subsequent methods.
[121] Fix | Delete
*/
[122] Fix | Delete
$this->setup_vars();
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* Update action values and meta if necessary.
[126] Fix | Delete
*/
[127] Fix | Delete
$this->maybe_update_actions();
[128] Fix | Delete
[129] Fix | Delete
/**
[130] Fix | Delete
* Saves our current location, along with any processing data we may need for the next step.
[131] Fix | Delete
* If we're done with our step, runs cleanup instead.
[132] Fix | Delete
*/
[133] Fix | Delete
$this->end_of_step();
[134] Fix | Delete
[135] Fix | Delete
// Respond to the AJAX call.
[136] Fix | Delete
$this->respond();
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
/**
[140] Fix | Delete
* Function to run any setup steps necessary to begin processing.
[141] Fix | Delete
*
[142] Fix | Delete
* @since 3.4.0
[143] Fix | Delete
*/
[144] Fix | Delete
public function startup()
[145] Fix | Delete
{
[146] Fix | Delete
// Record that we're processing the update.
[147] Fix | Delete
$this->running[ 0 ][ 'running' ] = true;
[148] Fix | Delete
// If we're not debugging...
[149] Fix | Delete
if ( ! $this->debug ) {
[150] Fix | Delete
// Ensure that our data tables are updated.
[151] Fix | Delete
$this->migrate( 'cache_collate_actions' );
[152] Fix | Delete
// Set out new db version.
[153] Fix | Delete
update_option( 'ninja_forms_db_version', '1.2' );
[154] Fix | Delete
}
[155] Fix | Delete
// Get a list of our forms...
[156] Fix | Delete
$sql = "SELECT ID FROM `{$this->db->prefix}nf3_forms`";
[157] Fix | Delete
$forms = $this->db->get_results( $sql, 'ARRAY_A' );
[158] Fix | Delete
$this->running[ 0 ][ 'forms' ] = $forms;
[159] Fix | Delete
// Record the total number of steps in this batch.
[160] Fix | Delete
$this->running[ 0 ][ 'steps' ] = count( $forms );
[161] Fix | Delete
// Record our current step (defaulted to 0 here).
[162] Fix | Delete
$this->running[ 0 ][ 'current' ] = 0;
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
/**
[166] Fix | Delete
* Setup our global variables used in other methods.
[167] Fix | Delete
*
[168] Fix | Delete
* @since 3.4.0
[169] Fix | Delete
* @return void
[170] Fix | Delete
*/
[171] Fix | Delete
private function setup_vars()
[172] Fix | Delete
{
[173] Fix | Delete
// See which form we're currently working with.
[174] Fix | Delete
$this->form = array_pop( $this->running[ 0 ][ 'forms' ] );
[175] Fix | Delete
[176] Fix | Delete
// Get the actions for that form.
[177] Fix | Delete
$this->actions = Ninja_Forms()->form( $this->form[ 'ID' ] )->get_actions();
[178] Fix | Delete
[179] Fix | Delete
// For each action...
[180] Fix | Delete
foreach ( $this->actions as $action ) {
[181] Fix | Delete
// Add the ID to the list.
[182] Fix | Delete
array_push( $this->action_ids, $action->get_id() );
[183] Fix | Delete
$this->actions_by_id[ $action->get_id() ] = $action->get_settings();
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
// If we're continuing an old process...
[187] Fix | Delete
if ( isset( $this->form[ 'update' ] ) ) {
[188] Fix | Delete
// Fetch our remaining udpates.
[189] Fix | Delete
$this->update = $this->form[ 'update' ];
[190] Fix | Delete
} // Otherwise... (We're beginning a new process.)
[191] Fix | Delete
else {
[192] Fix | Delete
// Copy all IDs to our update list.
[193] Fix | Delete
$this->update = $this->action_ids;
[194] Fix | Delete
}
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
/**
[198] Fix | Delete
* Function to cleanup any lingering temporary elements of a required update after completion.
[199] Fix | Delete
*
[200] Fix | Delete
* @since 3.4.0
[201] Fix | Delete
*/
[202] Fix | Delete
public function cleanup()
[203] Fix | Delete
{
[204] Fix | Delete
// Remove the current process from the array.
[205] Fix | Delete
array_shift( $this->running );
[206] Fix | Delete
// Record to our updates setting that this update is complete.
[207] Fix | Delete
$this->confirm_complete();
[208] Fix | Delete
// If we have no updates left to process...
[209] Fix | Delete
if ( empty( $this->running ) ) {
[210] Fix | Delete
// Call the parent cleanup method.
[211] Fix | Delete
parent::cleanup();
[212] Fix | Delete
}
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
/**
[216] Fix | Delete
* Check to see if we've locked processing.
[217] Fix | Delete
* If we have, then we need to run this process again.
[218] Fix | Delete
*
[219] Fix | Delete
* If we haven't locked processing, prepare to end this process.
[220] Fix | Delete
*
[221] Fix | Delete
* @since 3.4.0
[222] Fix | Delete
* @return void
[223] Fix | Delete
*/
[224] Fix | Delete
private function end_of_step()
[225] Fix | Delete
{
[226] Fix | Delete
// If we have locked processing...
[227] Fix | Delete
if ( $this->lock_process ) {
[228] Fix | Delete
// Record that we have more to do.
[229] Fix | Delete
$this->form[ 'update' ] = $this->update;
[230] Fix | Delete
array_push( $this->running[ 0 ][ 'forms' ], $this->form );
[231] Fix | Delete
} // Otherwise... (Processing isn't locked.)
[232] Fix | Delete
else {
[233] Fix | Delete
// If we have actions...
[234] Fix | Delete
if ( ! empty( $this->action_ids ) ) {
[235] Fix | Delete
// Update our meta keys.
[236] Fix | Delete
$sql = "UPDATE `{$this->meta_table}` SET `meta_key` = `key` WHERE `parent_id` IN(" . implode( ',', $this->action_ids ) . ")";
[237] Fix | Delete
$this->query( $sql );
[238] Fix | Delete
}
[239] Fix | Delete
/**
[240] Fix | Delete
* Update our form cache with any action changes.
[241] Fix | Delete
*/
[242] Fix | Delete
$this->update_form_cache();
[243] Fix | Delete
// Increment our step count.
[244] Fix | Delete
$this->running[ 0 ][ 'current' ] = intval( $this->running[ 0 ][ 'current' ] ) +1;
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
[248] Fix | Delete
// Prepare to output our number of steps and current step.
[249] Fix | Delete
$this->response[ 'stepsTotal' ] = $this->running[ 0 ][ 'steps' ];
[250] Fix | Delete
$this->response[ 'currentStep' ] = $this->running[ 0 ][ 'current' ];
[251] Fix | Delete
[252] Fix | Delete
// If we do not have locked processing...
[253] Fix | Delete
if ( ! $this->lock_process ) {
[254] Fix | Delete
// If all steps have been completed...
[255] Fix | Delete
if ( empty( $this->running[ 0 ] [ 'forms' ] ) ) {
[256] Fix | Delete
// Run our cleanup method.
[257] Fix | Delete
$this->cleanup();
[258] Fix | Delete
}
[259] Fix | Delete
}
[260] Fix | Delete
[261] Fix | Delete
// Record our current location in the process.
[262] Fix | Delete
update_option( 'ninja_forms_doing_required_updates', $this->running );
[263] Fix | Delete
// Prepare to output the number of updates remaining.
[264] Fix | Delete
$this->response[ 'updatesRemaining' ] = count( $this->running );
[265] Fix | Delete
}
[266] Fix | Delete
[267] Fix | Delete
/**
[268] Fix | Delete
* If we've made any changes to our form actions, update our form cache to match.
[269] Fix | Delete
*
[270] Fix | Delete
* @since 3.4.0
[271] Fix | Delete
* @return void
[272] Fix | Delete
*/
[273] Fix | Delete
private function update_form_cache()
[274] Fix | Delete
{
[275] Fix | Delete
// Get the cache for that form.
[276] Fix | Delete
$cache = WPN_Helper::get_nf_cache( $this->form[ 'ID' ] );
[277] Fix | Delete
// Bust the cache.
[278] Fix | Delete
$cache[ 'actions' ] = array();
[279] Fix | Delete
// For each action...
[280] Fix | Delete
foreach ( $this->actions_by_id as $id => $settings ) {
[281] Fix | Delete
// Append the settings for that action to the cache.
[282] Fix | Delete
$action = array();
[283] Fix | Delete
$action[ 'settings' ] = $settings;
[284] Fix | Delete
$action[ 'id' ] = $id;
[285] Fix | Delete
array_push( $cache[ 'actions' ], $action );
[286] Fix | Delete
}
[287] Fix | Delete
// Save the cache, passing 2 as the current stage.
[288] Fix | Delete
WPN_Helper::update_nf_cache( $this->form[ 'ID' ], $cache, 2 );
[289] Fix | Delete
}
[290] Fix | Delete
[291] Fix | Delete
/**
[292] Fix | Delete
* Loop over all of our actions and update our database if necessary.
[293] Fix | Delete
* Check each setting against $this->blacklist to make sure we want to insert that value.
[294] Fix | Delete
*
[295] Fix | Delete
* @since 3.4.0
[296] Fix | Delete
* @return void
[297] Fix | Delete
*/
[298] Fix | Delete
private function maybe_update_actions()
[299] Fix | Delete
{
[300] Fix | Delete
// Declare placeholder values.
[301] Fix | Delete
$sub_sql = array();
[302] Fix | Delete
$meta_values = array();
[303] Fix | Delete
[304] Fix | Delete
// While we have actions to update...
[305] Fix | Delete
while ( 0 < count( $this->update ) ) {
[306] Fix | Delete
// If we have hit our limit...
[307] Fix | Delete
if ( 1 > $this->limit ) {
[308] Fix | Delete
// Lock processing.
[309] Fix | Delete
$this->lock_process = true;
[310] Fix | Delete
// Exit the loop.
[311] Fix | Delete
break;
[312] Fix | Delete
}
[313] Fix | Delete
// Get our action to be updated.
[314] Fix | Delete
$action = array_pop( $this->update );
[315] Fix | Delete
// Get our settings.
[316] Fix | Delete
$settings = $this->actions_by_id[ $action ];
[317] Fix | Delete
// Update the new label column.
[318] Fix | Delete
array_push( $sub_sql, "WHEN `id` = " . intval( $action ) . " THEN '" . $this->prepare( $settings[ 'label' ] ) . "'" );
[319] Fix | Delete
// For each setting...
[320] Fix | Delete
foreach ( $settings as $key => $setting ) {
[321] Fix | Delete
// If the key is not blacklisted...
[322] Fix | Delete
if ( ! in_array( $key, $this->blacklist ) ) {
[323] Fix | Delete
// Add the value to be updated.
[324] Fix | Delete
$action = intval( $action );
[325] Fix | Delete
array_push( $meta_values, "WHEN `key` = '{$key}' AND `parent_id` = {$action} THEN '" . $this->prepare( $setting ) . "'" );
[326] Fix | Delete
}
[327] Fix | Delete
}
[328] Fix | Delete
$this->limit--;
[329] Fix | Delete
}
[330] Fix | Delete
[331] Fix | Delete
// If we've got updates to run...
[332] Fix | Delete
if ( ! empty( $sub_sql ) ) {
[333] Fix | Delete
// Update our actions table.
[334] Fix | Delete
$sql = "UPDATE `{$this->table}` SET `label` = CASE " . implode ( ' ', $sub_sql ) . " ELSE `label` END;";
[335] Fix | Delete
$this->query( $sql );
[336] Fix | Delete
// Update our meta values.
[337] Fix | Delete
$sql = "UPDATE `{$this->meta_table}` SET `meta_value` = CASE " . implode( ' ', $meta_values ) . " ELSE `meta_value` END;";
[338] Fix | Delete
$this->query( $sql );
[339] Fix | Delete
}
[340] Fix | Delete
}
[341] Fix | Delete
[342] Fix | Delete
}
[343] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function