Edit File by line
/home/barbar84/www/wp-conte.../plugins/ninja-fo.../includes/Updates
File: CacheFieldReconcilliation.php
<?php if ( ! defined( 'ABSPATH' ) ) exit;
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Class NF_Updates_CacheFieldReconcilliation
[3] Fix | Delete
*
[4] Fix | Delete
* This allows us to make sure that the data being saved to the database is in the fields table.
[5] Fix | Delete
*/
[6] Fix | Delete
class NF_Updates_CacheFieldReconcilliation extends NF_Abstracts_RequiredUpdate
[7] Fix | Delete
{
[8] Fix | Delete
[9] Fix | Delete
private $data = array();
[10] Fix | Delete
[11] Fix | Delete
private $running = array();
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Non-associatve array of field ids from the cache.
[15] Fix | Delete
* @var array
[16] Fix | Delete
*/
[17] Fix | Delete
private $field_ids = array();
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* columns to retrieve from meta table
[21] Fix | Delete
*/
[22] Fix | Delete
private $meta_keys = array(
[23] Fix | Delete
'label',
[24] Fix | Delete
'key',
[25] Fix | Delete
'order',
[26] Fix | Delete
'required',
[27] Fix | Delete
'default',
[28] Fix | Delete
'label_pos',
[29] Fix | Delete
'personally_identifiable'
[30] Fix | Delete
);
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* The denominator object for calculating our steps.
[34] Fix | Delete
* @var Integer
[35] Fix | Delete
*/
[36] Fix | Delete
private $divisor = 200;
[37] Fix | Delete
[38] Fix | Delete
/**
[39] Fix | Delete
* The table names for our database queries.
[40] Fix | Delete
*/
[41] Fix | Delete
private $table;
[42] Fix | Delete
private $meta_table;
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Constructor
[46] Fix | Delete
*
[47] Fix | Delete
* @param $data (Array) The data object passed in by the AJAX call.
[48] Fix | Delete
* @param $running (Array) The array of required updates being run.
[49] Fix | Delete
*
[50] Fix | Delete
* @since 3.4.0
[51] Fix | Delete
*/
[52] Fix | Delete
public function __construct( $data = array(), $running )
[53] Fix | Delete
{
[54] Fix | Delete
// Build our arguments array.
[55] Fix | Delete
$args = array(
[56] Fix | Delete
'slug' => 'CacheFieldReconcilliation',
[57] Fix | Delete
'class_name' => 'NF_Updates_CacheFieldReconcilliation',
[58] Fix | Delete
'debug' => false,
[59] Fix | Delete
);
[60] Fix | Delete
$this->data = $data;
[61] Fix | Delete
$this->running = $running;
[62] Fix | Delete
[63] Fix | Delete
// Call the parent constructor.
[64] Fix | Delete
parent::__construct( $args );
[65] Fix | Delete
[66] Fix | Delete
// Set our table names.
[67] Fix | Delete
$this->table = $this->db->prefix . 'nf3_fields';
[68] Fix | Delete
$this->meta_table = $this->db->prefix . 'nf3_field_meta';
[69] Fix | Delete
[70] Fix | Delete
// Begin processing.
[71] Fix | Delete
$this->process();
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
/**
[75] Fix | Delete
* Function to loop over the batch.
[76] Fix | Delete
*
[77] Fix | Delete
* @since 3.4.0
[78] Fix | Delete
*/
[79] Fix | Delete
public function process()
[80] Fix | Delete
{
[81] Fix | Delete
// If we've not already started...
[82] Fix | Delete
if ( ! isset( $this->running[ 0 ][ 'running' ] ) ) {
[83] Fix | Delete
// Run our startup method.
[84] Fix | Delete
$this->startup();
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
/**
[88] Fix | Delete
* Get the next round of fields to update
[89] Fix | Delete
*/
[90] Fix | Delete
$this->get_fields_this_step();
[91] Fix | Delete
[92] Fix | Delete
/**
[93] Fix | Delete
* Update fields
[94] Fix | Delete
*/
[95] Fix | Delete
$this->update_fields();
[96] Fix | Delete
[97] Fix | Delete
/**
[98] Fix | Delete
* Saves our current location, along with any processing data we may need for the next step.
[99] Fix | Delete
* If we're done with our step, runs cleanup instead.
[100] Fix | Delete
*/
[101] Fix | Delete
$this->end_of_step();
[102] Fix | Delete
[103] Fix | Delete
/**
[104] Fix | Delete
* Respond to the AJAX call.
[105] Fix | Delete
*/
[106] Fix | Delete
$this->respond();
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
/**
[110] Fix | Delete
* Function to run any setup steps necessary to begin processing.
[111] Fix | Delete
*
[112] Fix | Delete
* @since 3.4.0
[113] Fix | Delete
*/
[114] Fix | Delete
public function startup()
[115] Fix | Delete
{
[116] Fix | Delete
// Record that we're processing the update.
[117] Fix | Delete
$this->running[ 0 ][ 'running' ] = true;
[118] Fix | Delete
[119] Fix | Delete
$sql = "SELECT `id` FROM `{$this->table}`";
[120] Fix | Delete
$fields = $this->db->get_results( $sql, 'ARRAY_A' );
[121] Fix | Delete
// Record the total number of steps in this batch.
[122] Fix | Delete
$this->running[ 0 ][ 'steps' ] = ceil(count( $fields ) / $this->divisor);
[123] Fix | Delete
// Record our current step (defaulted to 0 here).
[124] Fix | Delete
$this->running[ 0 ][ 'current' ] = 0;
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
public function get_fields_this_step() {
[128] Fix | Delete
[129] Fix | Delete
$offset = 0;
[130] Fix | Delete
[131] Fix | Delete
if( 0 < $this->running[ 0 ][ 'current' ] ) {
[132] Fix | Delete
$offset = $this->running[ 0 ][ 'current' ] * $this->divisor;
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
// Get a list of our fields...
[136] Fix | Delete
$sql = "SELECT `id` FROM `{$this->table}` LIMIT {$offset}, {$this->divisor}";
[137] Fix | Delete
$this->field_ids = $this->db->get_results( $sql, 'ARRAY_A' );
[138] Fix | Delete
$this->field_ids = $this->array_squash( $this->field_ids );
[139] Fix | Delete
// $this->running[ 0 ][ 'fields' ] = $this->field_ids;
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
/**
[143] Fix | Delete
* Update field table records with data from field meta
[144] Fix | Delete
*/
[145] Fix | Delete
public function update_fields() {
[146] Fix | Delete
$field_meta = $this->get_field_meta();
[147] Fix | Delete
[148] Fix | Delete
if($field_meta) {
[149] Fix | Delete
$update_query = $this->get_update_query( $field_meta );
[150] Fix | Delete
[151] Fix | Delete
if( $update_query ) {
[152] Fix | Delete
$this->query($update_query);
[153] Fix | Delete
}
[154] Fix | Delete
}
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
/**
[158] Fix | Delete
* Get meta data to use for updating
[159] Fix | Delete
*/
[160] Fix | Delete
public function get_field_meta() {
[161] Fix | Delete
[162] Fix | Delete
if(0 === count($this->field_ids)) return false;
[163] Fix | Delete
[164] Fix | Delete
$in_fields = implode( ', ', $this->field_ids );
[165] Fix | Delete
$meta_keys = "'" . implode( "' , '", $this->meta_keys ) . "'";
[166] Fix | Delete
[167] Fix | Delete
$meta_query = $this->get_field_meta_query();
[168] Fix | Delete
[169] Fix | Delete
$results = $this->db->get_results( $meta_query, 'ARRAY_A');
[170] Fix | Delete
[171] Fix | Delete
$meta_data = $this->format_field_meta($results);
[172] Fix | Delete
[173] Fix | Delete
return $meta_data;
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
/**
[177] Fix | Delete
* Construct the query to get meta data
[178] Fix | Delete
*
[179] Fix | Delete
* return String $meta_query
[180] Fix | Delete
*/
[181] Fix | Delete
public function get_field_meta_query() {
[182] Fix | Delete
$in_fields = implode( ', ', $this->field_ids );
[183] Fix | Delete
$meta_keys = "'" . implode( "' , '", $this->meta_keys ) . "'";
[184] Fix | Delete
[185] Fix | Delete
$meta_query = "SELECT `parent_id`, `key`, `meta_key`, `meta_value`, `value` FROM `{$this->meta_table}` WHERE `parent_id` IN ({$in_fields}) AND `key` IN ({$meta_keys}) ORDER BY `parent_id` ASC";
[186] Fix | Delete
[187] Fix | Delete
return $meta_query;
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
/**
[191] Fix | Delete
* Format the data into format that helps us construct the insert/update query
[192] Fix | Delete
*
[193] Fix | Delete
* @param Array $metadata
[194] Fix | Delete
*
[195] Fix | Delete
* @return Array $meta_data
[196] Fix | Delete
*/
[197] Fix | Delete
public function format_field_meta( $metadata ) {
[198] Fix | Delete
$meta_data = array();
[199] Fix | Delete
[200] Fix | Delete
foreach( $metadata as $meta ) {
[201] Fix | Delete
$parent_id = $meta['parent_id'];
[202] Fix | Delete
foreach( $meta as $key => $val ) {
[203] Fix | Delete
[204] Fix | Delete
if( 'parent_id' !== $key ) {
[205] Fix | Delete
$meta_data[ $parent_id ][ $meta['key'] ] = $meta['value'];
[206] Fix | Delete
$meta_data[ $parent_id ][ 'meta_' . $meta['meta_key'] ] = $meta['meta_value'];
[207] Fix | Delete
}
[208] Fix | Delete
}
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
return $meta_data;
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
/**
[215] Fix | Delete
* Construct field update query
[216] Fix | Delete
*/
[217] Fix | Delete
public function get_update_query( $field_data ) {
[218] Fix | Delete
if( 0 === count( $field_data) ) return false;
[219] Fix | Delete
[220] Fix | Delete
$sql = "INSERT INTO {$this->table}
[221] Fix | Delete
(`id`, `label`, `key`, `field_label`, `field_key`, `order`, `required`, `default_value`, `label_pos`, `personally_identifiable`)
[222] Fix | Delete
VALUES";
[223] Fix | Delete
[224] Fix | Delete
foreach( $field_data as $field_id => $meta ) {
[225] Fix | Delete
$sql .= "({$field_id}, '{$this->db->_real_escape($meta['label'])}', '{$this->db->_real_escape($meta['key'])}', '{$this->db->_real_escape($meta['meta_label'])}', '{$this->db->_real_escape($meta['meta_key'])}', {$meta['order']},";
[226] Fix | Delete
[227] Fix | Delete
if( isset( $meta[ 'required' ] ) && '' !== $meta[ 'required' ]) {
[228] Fix | Delete
$sql .= "{$meta['required']},";
[229] Fix | Delete
} else {
[230] Fix | Delete
$sql .= "0,";
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
if(isset( $meta[ 'meta_default' ] ) ) {
[234] Fix | Delete
$sql .= "'{$this->db->_real_escape($meta['meta_default'])}',";
[235] Fix | Delete
} else {
[236] Fix | Delete
$sql .= "'',";
[237] Fix | Delete
}
[238] Fix | Delete
[239] Fix | Delete
if( isset( $meta[ 'meta_label_pos' ] ) ) {
[240] Fix | Delete
$sql .= "'{$meta['meta_label_pos']}',";
[241] Fix | Delete
} else {
[242] Fix | Delete
$sql .= "'',";
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
if(isset($meta['personally_identifiable'])) {
[246] Fix | Delete
$sql .= "{$meta['personally_identifiable']}";
[247] Fix | Delete
} else {
[248] Fix | Delete
$sql .= "0";
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
$sql .= "),";
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
$sql = rtrim( $sql, ',' );
[255] Fix | Delete
[256] Fix | Delete
$sql .= "ON DUPLICATE KEY
[257] Fix | Delete
UPDATE
[258] Fix | Delete
`label` = VALUES(`label`),
[259] Fix | Delete
`key` = VALUES(`key`),
[260] Fix | Delete
`field_label` = VALUES(`field_label`),
[261] Fix | Delete
`field_key` = VALUES(`field_key`),
[262] Fix | Delete
`order` = VALUES(`order`),
[263] Fix | Delete
`required` = VALUES(`required`),
[264] Fix | Delete
`required` = VALUES(`required`),
[265] Fix | Delete
`default_value` = VALUES(`default_value`),
[266] Fix | Delete
`label_pos` = VALUES(`label_pos`),
[267] Fix | Delete
`personally_identifiable` = VALUES(`personally_identifiable`)";
[268] Fix | Delete
[269] Fix | Delete
return $sql;
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
/**
[273] Fix | Delete
* Function to cleanup any lingering temporary elements of a required update after completion.
[274] Fix | Delete
*
[275] Fix | Delete
* @since 3.4.0
[276] Fix | Delete
*/
[277] Fix | Delete
public function cleanup()
[278] Fix | Delete
{
[279] Fix | Delete
// Remove the current process from the array.
[280] Fix | Delete
array_shift( $this->running );
[281] Fix | Delete
// Record to our updates setting that this update is complete.
[282] Fix | Delete
$this->confirm_complete();
[283] Fix | Delete
// If we have no updates left to process...
[284] Fix | Delete
if ( empty( $this->running ) ) {
[285] Fix | Delete
// Call the parent cleanup method.
[286] Fix | Delete
parent::cleanup();
[287] Fix | Delete
}
[288] Fix | Delete
}
[289] Fix | Delete
[290] Fix | Delete
/**
[291] Fix | Delete
* After we've done our processing, but before we get to step cleanup, we need to store process information.
[292] Fix | Delete
*
[293] Fix | Delete
* This method updates our form class var so that it can be passed to the next step.
[294] Fix | Delete
* If we've completed this step, it calls the cleanup method.
[295] Fix | Delete
*
[296] Fix | Delete
* @since 3.4.0
[297] Fix | Delete
* @return void
[298] Fix | Delete
*/
[299] Fix | Delete
private function end_of_step()
[300] Fix | Delete
{
[301] Fix | Delete
$this->running[ 0 ][ 'current' ] = intval( $this->running[ 0 ][ 'current' ] ) + 1;
[302] Fix | Delete
[303] Fix | Delete
// Prepare to output our number of steps and current step.
[304] Fix | Delete
$this->response[ 'stepsTotal' ] = $this->running[ 0 ][ 'steps' ];
[305] Fix | Delete
$this->response[ 'currentStep' ] = $this->running[ 0 ][ 'current' ];
[306] Fix | Delete
[307] Fix | Delete
if ( $this->divisor > count($this->field_ids)) {
[308] Fix | Delete
// Run our cleanup method.
[309] Fix | Delete
$this->cleanup();
[310] Fix | Delete
}
[311] Fix | Delete
[312] Fix | Delete
// Record our current location in the process.
[313] Fix | Delete
update_option( 'ninja_forms_doing_required_updates', $this->running );
[314] Fix | Delete
// Prepare to output the number of updates remaining.
[315] Fix | Delete
$this->response[ 'updatesRemaining' ] = count( $this->running );
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
/**
[319] Fix | Delete
* Function to compress our db results into a more useful format.
[320] Fix | Delete
*
[321] Fix | Delete
* @param $data (Array) The result to be compressed.
[322] Fix | Delete
*
[323] Fix | Delete
* @return (Array) Associative if our data was complex.
[324] Fix | Delete
* Non-associative if our data was a single item.
[325] Fix | Delete
*
[326] Fix | Delete
* @since UPDATE_VERSION_ON_MERGE
[327] Fix | Delete
*/
[328] Fix | Delete
private function array_squash( $data )
[329] Fix | Delete
{
[330] Fix | Delete
$response = array();
[331] Fix | Delete
// For each item in the array...
[332] Fix | Delete
foreach ( $data as $row ) {
[333] Fix | Delete
// If the item has more than 1 attribute...
[334] Fix | Delete
if ( 1 < count( $row ) ) {
[335] Fix | Delete
// Assign the data to an associated result.
[336] Fix | Delete
$response[] = intval($row['id']);
[337] Fix | Delete
// Unset the id setting, as that will be the key.
[338] Fix | Delete
unset( $response[ $row[ 'id' ] ][ '' ] );
[339] Fix | Delete
} // Otherwise... (We only have 1 attribute.)
[340] Fix | Delete
else {
[341] Fix | Delete
// Add the id to the stack in a non-associated result.
[342] Fix | Delete
$response[] = intval( $row[ 'id' ] );
[343] Fix | Delete
}
[344] Fix | Delete
}
[345] Fix | Delete
return $response;
[346] Fix | Delete
}
[347] Fix | Delete
}
[348] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function