Edit File by line
/home/barbar84/www/wp-conte.../plugins/ninja-fo.../includes/Updates
File: CacheCollateObjects.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 CacheCollateObjects 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_objects and nf3_object_meta tables.
[7] Fix | Delete
* Then, it will step over each object in the db, following this process:
[8] Fix | Delete
* - Append the object_title
[9] Fix | Delete
* Then, it will step over each object_meta in the db, following this process:
[10] Fix | Delete
* - Copy over the meta_key
[11] Fix | Delete
* - Append the meta_value
[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_CacheCollateObjects 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
* The table names for our database queries.
[23] Fix | Delete
*/
[24] Fix | Delete
private $table;
[25] Fix | Delete
private $meta_table;
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* The row counts of our database tables.
[29] Fix | Delete
*/
[30] Fix | Delete
private $table_rows = 0;
[31] Fix | Delete
private $meta_rows = 0;
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* The denominator object for calculating our steps.
[35] Fix | Delete
* @var Integer
[36] Fix | Delete
*/
[37] Fix | Delete
private $divisor = 500;
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Constructor
[41] Fix | Delete
*
[42] Fix | Delete
* @param $data (Array) The data object passed in by the AJAX call.
[43] Fix | Delete
* @param $running (Array) The array of required updates being run.
[44] Fix | Delete
*
[45] Fix | Delete
* @since 3.4.0
[46] Fix | Delete
*/
[47] Fix | Delete
public function __construct( $data = array(), $running )
[48] Fix | Delete
{
[49] Fix | Delete
// Build our arguments array.
[50] Fix | Delete
$args = array(
[51] Fix | Delete
'slug' => 'CacheCollateObjects',
[52] Fix | Delete
'class_name' => 'NF_Updates_CacheCollateObjects',
[53] Fix | Delete
'debug' => false,
[54] Fix | Delete
);
[55] Fix | Delete
$this->data = $data;
[56] Fix | Delete
$this->running = $running;
[57] Fix | Delete
[58] Fix | Delete
// Call the parent constructor.
[59] Fix | Delete
parent::__construct( $args );
[60] Fix | Delete
[61] Fix | Delete
// Set our table names.
[62] Fix | Delete
$this->table = $this->db->prefix . 'nf3_objects';
[63] Fix | Delete
$this->meta_table = $this->db->prefix . 'nf3_object_meta';
[64] Fix | Delete
[65] Fix | Delete
// Begin processing.
[66] Fix | Delete
$this->process();
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* Function to loop over the batch.
[71] Fix | Delete
*
[72] Fix | Delete
* @since 3.4.0
[73] Fix | Delete
*/
[74] Fix | Delete
public function process()
[75] Fix | Delete
{
[76] Fix | Delete
// If we've not already started...
[77] Fix | Delete
if ( ! isset( $this->running[ 0 ][ 'running' ] ) ) {
[78] Fix | Delete
// Run our startup method.
[79] Fix | Delete
$this->startup();
[80] Fix | Delete
} // Otherwise... (We're picking up an old process.)
[81] Fix | Delete
else {
[82] Fix | Delete
// Get our number of remaining table rows.
[83] Fix | Delete
if ( isset( $this->running[ 0 ][ 'table_rows' ] ) ) {
[84] Fix | Delete
$this->table_rows = intval( $this->running[ 0 ][ 'table_rows' ] );
[85] Fix | Delete
}
[86] Fix | Delete
// Get our number of remaining meta rows.
[87] Fix | Delete
if ( isset( $this->running[ 0 ][ 'meta_rows' ] ) ) {
[88] Fix | Delete
$this->meta_rows = intval( $this->running[ 0 ][ 'meta_rows' ] );
[89] Fix | Delete
}
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
// Update values in the objects table.
[93] Fix | Delete
$this->maybe_update_objects();
[94] Fix | Delete
[95] Fix | Delete
// Update values in the object_meta table.
[96] Fix | Delete
$this->maybe_update_object_meta();
[97] Fix | Delete
[98] Fix | Delete
// Increment our step count.
[99] Fix | Delete
$this->running[ 0 ][ 'current' ] += 1;
[100] Fix | Delete
// Prepare to output our number of steps and current step.
[101] Fix | Delete
$this->response[ 'stepsTotal' ] = $this->running[ 0 ][ 'steps' ];
[102] Fix | Delete
$this->response[ 'currentStep' ] = $this->running[ 0 ][ 'current' ];
[103] Fix | Delete
$this->running[ 0 ][ 'table_rows' ] = $this->table_rows;
[104] Fix | Delete
$this->running[ 0 ][ 'meta_rows' ] = $this->meta_rows;
[105] Fix | Delete
// If we have no meta left to update at this point...
[106] Fix | Delete
if ( 0 >= $this->table_rows && 0 >= $this->meta_rows ) {
[107] Fix | Delete
// Run our cleanup process.
[108] Fix | Delete
$this->cleanup();
[109] Fix | Delete
}
[110] Fix | Delete
// Prepare to output the number of updates remaining.
[111] Fix | Delete
$this->response[ 'updatesRemaining' ] = count( $this->running );
[112] Fix | Delete
[113] Fix | Delete
// Record our current location in the process.
[114] Fix | Delete
update_option( 'ninja_forms_doing_required_updates', $this->running );
[115] Fix | Delete
[116] Fix | Delete
// Respond to the AJAX call.
[117] Fix | Delete
$this->respond();
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
/**
[121] Fix | Delete
* Function to run any setup steps necessary to begin processing.
[122] Fix | Delete
*
[123] Fix | Delete
* @since 3.4.0
[124] Fix | Delete
*/
[125] Fix | Delete
public function startup()
[126] Fix | Delete
{
[127] Fix | Delete
// Record that we're processing the update.
[128] Fix | Delete
$this->running[ 0 ][ 'running' ] = true;
[129] Fix | Delete
// If we're not debugging...
[130] Fix | Delete
if ( ! $this->debug ) {
[131] Fix | Delete
// Ensure that our data tables are updated.
[132] Fix | Delete
$this->migrate( 'cache_collate_objects' );
[133] Fix | Delete
// Set out new db version.
[134] Fix | Delete
update_option( 'ninja_forms_db_version', '1.4' );
[135] Fix | Delete
}
[136] Fix | Delete
// Get the number of rows in the objects table.
[137] Fix | Delete
$sql = "SELECT COUNT( `id` ) as Total FROM `{$this->table}`";
[138] Fix | Delete
$result = $this->db->get_results( $sql, 'ARRAY_A' );
[139] Fix | Delete
// If we got something back...
[140] Fix | Delete
if ( ! empty( $result ) ) {
[141] Fix | Delete
// Record the total.
[142] Fix | Delete
$this->table_rows = intval( $result[ 0 ][ 'Total' ] );
[143] Fix | Delete
}
[144] Fix | Delete
// If the table was empty...
[145] Fix | Delete
if ( 0 == $this->table_rows ) {
[146] Fix | Delete
/**
[147] Fix | Delete
* Clean out the object_meta table.
[148] Fix | Delete
* It should contain nothing if there is nothing in the objects table.
[149] Fix | Delete
*/
[150] Fix | Delete
$sql = "DELETE FROM `{$this->meta_table}`";
[151] Fix | Delete
$this->query( $sql );
[152] Fix | Delete
// Lock processing.
[153] Fix | Delete
$this->lock_process = true;
[154] Fix | Delete
}
[155] Fix | Delete
// If processing is locked...
[156] Fix | Delete
if ( $this->lock_process ) {
[157] Fix | Delete
// Prepare to output our number of steps and current step.
[158] Fix | Delete
$this->response[ 'stepsTotal' ] = 1;
[159] Fix | Delete
$this->response[ 'currentStep' ] = 1;
[160] Fix | Delete
// Skip straight to our cleanup method.
[161] Fix | Delete
$this->cleanup();
[162] Fix | Delete
// Prepare to output the number of updates remaining.
[163] Fix | Delete
$this->response[ 'updatesRemaining' ] = count( $this->running );
[164] Fix | Delete
// Record our current location in the process.
[165] Fix | Delete
update_option( 'ninja_forms_doing_required_updates', $this->running );
[166] Fix | Delete
$this->respond();
[167] Fix | Delete
}
[168] Fix | Delete
// Get the number of rows in the object_meta table.
[169] Fix | Delete
$sql = "SELECT COUNT( `id` ) as Total FROM `{$this->meta_table}`";
[170] Fix | Delete
$result = $this->db->get_results( $sql, 'ARRAY_A' );
[171] Fix | Delete
// If we got something back...
[172] Fix | Delete
if ( ! empty( $result ) ) {
[173] Fix | Delete
// Record the total.
[174] Fix | Delete
$this->meta_rows = intval( $result[ 0 ][ 'Total' ] );
[175] Fix | Delete
}
[176] Fix | Delete
$steps = ceil( $this->table_rows / $this->divisor );
[177] Fix | Delete
$steps += ceil( $this->meta_rows / $this->divisor );
[178] Fix | Delete
[179] Fix | Delete
// Record the total number of steps in this batch.
[180] Fix | Delete
$this->running[ 0 ][ 'steps' ] = $steps;
[181] Fix | Delete
// Record our current step (defaulted to 0 here).
[182] Fix | Delete
$this->running[ 0 ][ 'current' ] = 0;
[183] Fix | Delete
}
[184] Fix | Delete
[185] Fix | Delete
/**
[186] Fix | Delete
* Function to cleanup any lingering temporary elements of a required update after completion.
[187] Fix | Delete
*
[188] Fix | Delete
* @since 3.4.0
[189] Fix | Delete
*/
[190] Fix | Delete
public function cleanup()
[191] Fix | Delete
{
[192] Fix | Delete
// Remove the current process from the array.
[193] Fix | Delete
array_shift( $this->running );
[194] Fix | Delete
// Record to our updates setting that this update is complete.
[195] Fix | Delete
$this->confirm_complete();
[196] Fix | Delete
// If we have no updates left to process...
[197] Fix | Delete
if ( empty( $this->running ) ) {
[198] Fix | Delete
// Call the parent cleanup method.
[199] Fix | Delete
parent::cleanup();
[200] Fix | Delete
}
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* Function to manage the updating of our objects table.
[205] Fix | Delete
*
[206] Fix | Delete
* @return Void
[207] Fix | Delete
*
[208] Fix | Delete
* @since 3.4.0
[209] Fix | Delete
*/
[210] Fix | Delete
private function maybe_update_objects()
[211] Fix | Delete
{
[212] Fix | Delete
// If we have no table rows left to process, exit early.
[213] Fix | Delete
if ( 0 >= $this->table_rows ) return false;
[214] Fix | Delete
// Compile our query.
[215] Fix | Delete
$sql = "SELECT `id`, `title` FROM `{$this->table}` ";
[216] Fix | Delete
// If we are picking up an old process...
[217] Fix | Delete
if ( isset( $this->running[ 0 ][ 'last_updated' ] ) ) {
[218] Fix | Delete
// Make sure we don't fetch old values.
[219] Fix | Delete
$sql .= "WHERE `id` > " . intval( $this->running[ 0 ][ 'last_updated' ] ) . " ";
[220] Fix | Delete
}
[221] Fix | Delete
// Ensure that we gate the number of records that we fetch.
[222] Fix | Delete
$sql .= "ORDER BY `id` ASC LIMIT {$this->divisor}";
[223] Fix | Delete
$result = $this->db->get_results( $sql, 'ARRAY_A' );
[224] Fix | Delete
// Build our Update query.
[225] Fix | Delete
$sub_sql = array();
[226] Fix | Delete
foreach ( $result as $object ) {
[227] Fix | Delete
array_push( $sub_sql, "WHEN `id` = " . intval( $object[ 'id' ] ) . " THEN '" . $this->prepare( $object[ 'title' ] ) . "'" );
[228] Fix | Delete
}
[229] Fix | Delete
// If we have values to update...
[230] Fix | Delete
if ( ! empty( $sub_sql ) ) {
[231] Fix | Delete
// Run the update.
[232] Fix | Delete
$sql = "UPDATE `{$this->table}` SET `object_title` = CASE " . implode( ' ', $sub_sql ) . " ELSE `object_title` END;";
[233] Fix | Delete
$this->query( $sql );
[234] Fix | Delete
// Get the last item updated.
[235] Fix | Delete
$last = array_pop( $result );
[236] Fix | Delete
// Record it to our process object.
[237] Fix | Delete
$this->running[ 0 ][ 'last_updated' ] = $last[ 'id' ];
[238] Fix | Delete
// Also record that we ran the update on this table.
[239] Fix | Delete
$this->running[ 0 ][ 'updating_table' ] = $this->table;
[240] Fix | Delete
// Reduce our table rows by the divisor.
[241] Fix | Delete
$this->table_rows = $this->table_rows - $this->divisor;
[242] Fix | Delete
// Lock processing.
[243] Fix | Delete
$this->lock_process = true;
[244] Fix | Delete
}
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
/**
[248] Fix | Delete
* Function to manage the updating of our object_meta table.
[249] Fix | Delete
*
[250] Fix | Delete
* @return Void
[251] Fix | Delete
*
[252] Fix | Delete
* @since 3.4.0
[253] Fix | Delete
*/
[254] Fix | Delete
private function maybe_update_object_meta()
[255] Fix | Delete
{
[256] Fix | Delete
// If we've locked processing, exit early.
[257] Fix | Delete
if ( $this->lock_process ) return false;
[258] Fix | Delete
// If we have no meta rows left to process, exit early.
[259] Fix | Delete
if ( 0 >= $this->meta_rows ) return false;
[260] Fix | Delete
// Compile our query.
[261] Fix | Delete
$sql = "SELECT `id`, `key`, `value` FROM `{$this->meta_table}` ";
[262] Fix | Delete
// If we are picking up an old process...
[263] Fix | Delete
if ( $this->meta_table == $this->running[ 0 ][ 'updating_table' ] ) {
[264] Fix | Delete
// Make sure we don't fetch old values.
[265] Fix | Delete
$sql .= "WHERE `id` > " . intval( $this->running[ 0 ][ 'last_updated' ] ) . " ";
[266] Fix | Delete
}
[267] Fix | Delete
// Ensure that we gate the number of records that we fetch.
[268] Fix | Delete
$sql .= "ORDER BY `id` ASC LIMIT {$this->divisor}";
[269] Fix | Delete
$result = $this->db->get_results( $sql, 'ARRAY_A' );
[270] Fix | Delete
// Build our Update query.
[271] Fix | Delete
$sub_sql = array();
[272] Fix | Delete
$meta_ids = array();
[273] Fix | Delete
foreach ( $result as $object ) {
[274] Fix | Delete
array_push( $sub_sql, "WHEN `id` = " . intval( $object[ 'id' ] ) . " THEN '" . $this->prepare( $object[ 'value' ] ) . "'" );
[275] Fix | Delete
array_push( $meta_ids, $object[ 'id' ] );
[276] Fix | Delete
}
[277] Fix | Delete
// If we have values to update...
[278] Fix | Delete
if ( ! empty( $sub_sql ) ) {
[279] Fix | Delete
// Run the update on the meta_key column.
[280] Fix | Delete
$sql = "UPDATE `{$this->meta_table}` SET `meta_key` = `key` WHERE `id` IN(" . implode( ', ', $meta_ids ) . ");";
[281] Fix | Delete
$this->query( $sql );
[282] Fix | Delete
// Run the update on the meta_value column.
[283] Fix | Delete
$sql = "UPDATE `{$this->meta_table}` SET `meta_value` = CASE " . implode( ' ', $sub_sql ) . " ELSE `meta_value` END;";
[284] Fix | Delete
$this->query( $sql );
[285] Fix | Delete
// Get the last item updated.
[286] Fix | Delete
$last = array_pop( $result );
[287] Fix | Delete
// Record it to our process object.
[288] Fix | Delete
$this->running[ 0 ][ 'last_updated' ] = $last[ 'id' ];
[289] Fix | Delete
// Also record that we ran the update on this table.
[290] Fix | Delete
$this->running[ 0 ][ 'updating_table' ] = $this->meta_table;
[291] Fix | Delete
// Reduce our meta rows by the divisor.
[292] Fix | Delete
$this->meta_rows -= $this->divisor;
[293] Fix | Delete
// Lock processing.
[294] Fix | Delete
$this->lock_process = true;
[295] Fix | Delete
}
[296] Fix | Delete
}
[297] Fix | Delete
[298] Fix | Delete
}
[299] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function