Edit File by line
/home/barbar84/www/wp-conte.../plugins/ninja-fo.../includes/Updates
File: CacheCollateCleanup.php
<?php if ( ! defined( 'ABSPATH' ) ) exit;
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Class NF_Updates_CacheCollateCleanup
[3] Fix | Delete
*
[4] Fix | Delete
* This class manages the step process of running through the CacheCollateCleanup 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 then step over each table in our structure and ensure that orphan records are removed from storage.
[7] Fix | Delete
* It will then step over all submissions, removing any orphans.
[8] Fix | Delete
* It will then step over all submissions (by form), updating or removing any orphan field records.
[9] Fix | Delete
* After completing the above for every form on the site, it will remove the data object that manages its location.
[10] Fix | Delete
*/
[11] Fix | Delete
class NF_Updates_CacheCollateCleanup extends NF_Abstracts_RequiredUpdate
[12] Fix | Delete
{
[13] Fix | Delete
[14] Fix | Delete
private $data = array();
[15] Fix | Delete
[16] Fix | Delete
private $running = array();
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* The denominator object for calculating our steps.
[20] Fix | Delete
* @var Integer
[21] Fix | Delete
*/
[22] Fix | Delete
private $divisor = 500;
[23] Fix | Delete
[24] Fix | Delete
private $stage;
[25] Fix | Delete
[26] Fix | Delete
private $stage_complete = false;
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Constructor
[30] Fix | Delete
*
[31] Fix | Delete
* @param $data (Array) The data object passed in by the AJAX call.
[32] Fix | Delete
* @param $running (Array) The array of required updates being run.
[33] Fix | Delete
*
[34] Fix | Delete
* @since 3.4.0
[35] Fix | Delete
*/
[36] Fix | Delete
public function __construct( $data = array(), $running )
[37] Fix | Delete
{
[38] Fix | Delete
// Build our arguments array.
[39] Fix | Delete
$args = array(
[40] Fix | Delete
'slug' => 'CacheCollateCleanup',
[41] Fix | Delete
'class_name' => 'NF_Updates_CacheCollateCleanup',
[42] Fix | Delete
'debug' => false,
[43] Fix | Delete
);
[44] Fix | Delete
$this->data = $data;
[45] Fix | Delete
$this->running = $running;
[46] Fix | Delete
[47] Fix | Delete
// Call the parent constructor.
[48] Fix | Delete
parent::__construct( $args );
[49] Fix | Delete
[50] Fix | Delete
// Begin processing.
[51] Fix | Delete
$this->process();
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* Function to loop over the batch.
[56] Fix | Delete
*
[57] Fix | Delete
* @since 3.4.0
[58] Fix | Delete
*/
[59] Fix | Delete
public function process()
[60] Fix | Delete
{
[61] Fix | Delete
// If we've not already started...
[62] Fix | Delete
if ( ! isset( $this->running[ 0 ][ 'running' ] ) ) {
[63] Fix | Delete
// Run our startup method.
[64] Fix | Delete
$this->startup();
[65] Fix | Delete
}
[66] Fix | Delete
$this->stage = $this->running[ 0 ][ 'stages' ][ 0 ];
[67] Fix | Delete
[68] Fix | Delete
// Determine what process to run.
[69] Fix | Delete
switch ( $this->stage[ 'table' ] ) {
[70] Fix | Delete
case 'submissions':
[71] Fix | Delete
// If we've cleaned out all orphan submissions...
[72] Fix | Delete
if ( $this->stage[ 'purged' ] ) {
[73] Fix | Delete
// Work on clearing out orphan field records.
[74] Fix | Delete
$this->adopt_fields();
[75] Fix | Delete
} // Otherwise... (We still have orphan submissions.)
[76] Fix | Delete
else {
[77] Fix | Delete
// Remove orphan submissions.
[78] Fix | Delete
$this->adopt_subs();
[79] Fix | Delete
}
[80] Fix | Delete
break;
[81] Fix | Delete
default:
[82] Fix | Delete
// If we need to step over this process...
[83] Fix | Delete
if ( $this->divisor < $this->stage[ 'parent_total' ] ) {
[84] Fix | Delete
// Call stepped deletion.
[85] Fix | Delete
$this->do_step_delete();
[86] Fix | Delete
} // Otherwise... (We don't need to step over this.)
[87] Fix | Delete
else {
[88] Fix | Delete
// Call simple deletion.
[89] Fix | Delete
$this->do_easy_delete();
[90] Fix | Delete
}
[91] Fix | Delete
// Increment our step count.
[92] Fix | Delete
$this->running[ 0 ][ 'current' ] += 1;
[93] Fix | Delete
break;
[94] Fix | Delete
}
[95] Fix | Delete
// If we have completed the current stage...
[96] Fix | Delete
if ( $this->stage_complete ) {
[97] Fix | Delete
// Remove it from our list.
[98] Fix | Delete
array_shift( $this->running[ 0 ][ 'stages' ] );
[99] Fix | Delete
} // Otherwise... (We have not completed the stage)
[100] Fix | Delete
else {
[101] Fix | Delete
// Record our changes.
[102] Fix | Delete
$this->running[ 0 ][ 'stages' ][ 0 ] = $this->stage;
[103] Fix | Delete
}
[104] Fix | Delete
// Prepare to output our number of steps and current step.
[105] Fix | Delete
$this->response[ 'stepsTotal' ] = $this->running[ 0 ][ 'steps' ];
[106] Fix | Delete
$this->response[ 'currentStep' ] = $this->running[ 0 ][ 'current' ];
[107] Fix | Delete
[108] Fix | Delete
// If we have no stages left...
[109] Fix | Delete
if ( empty( $this->running[ 0 ][ 'stages' ] ) ) {
[110] Fix | Delete
// Run our cleanup method.
[111] Fix | Delete
$this->cleanup();
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
// Prepare to output the number of updates remaining.
[115] Fix | Delete
$this->response[ 'updatesRemaining' ] = count( $this->running );
[116] Fix | Delete
[117] Fix | Delete
// Record our current location in the process.
[118] Fix | Delete
update_option( 'ninja_forms_doing_required_updates', $this->running );
[119] Fix | Delete
[120] Fix | Delete
// Respond to the AJAX call.
[121] Fix | Delete
$this->respond();
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* Function to run any setup steps necessary to begin processing.
[126] Fix | Delete
*
[127] Fix | Delete
* @since 3.4.0
[128] Fix | Delete
*/
[129] Fix | Delete
public function startup()
[130] Fix | Delete
{
[131] Fix | Delete
// Record that we're processing the update.
[132] Fix | Delete
$this->running[ 0 ][ 'running' ] = true;
[133] Fix | Delete
[134] Fix | Delete
// Get the number of records in the forms table.
[135] Fix | Delete
$form_count = $this->get_total( 'nf3_forms' );
[136] Fix | Delete
// Get the number of records in the fields table.
[137] Fix | Delete
$action_count = $this->get_total( 'nf3_actions' );
[138] Fix | Delete
// Get the number of records in the actions table.
[139] Fix | Delete
$field_count = $this->get_total( 'nf3_fields' );
[140] Fix | Delete
// Get the number of records in the objects table.
[141] Fix | Delete
$object_count = $this->get_total( 'nf3_objects' );
[142] Fix | Delete
[143] Fix | Delete
// Get all form_ids from the db for the submissions portion.
[144] Fix | Delete
$sql = "SELECT `id` FROM `{$this->db->prefix}nf3_forms`";
[145] Fix | Delete
$forms = $this->db->get_results( $sql, 'ARRAY_A' );
[146] Fix | Delete
[147] Fix | Delete
$stages = array(
[148] Fix | Delete
array(
[149] Fix | Delete
'table' => $this->db->prefix . 'nf3_form_meta',
[150] Fix | Delete
'parent' => $this->db->prefix . 'nf3_forms',
[151] Fix | Delete
'parent_total' => $form_count,
[152] Fix | Delete
),
[153] Fix | Delete
array(
[154] Fix | Delete
'table' => $this->db->prefix . 'nf3_actions',
[155] Fix | Delete
'parent' => $this->db->prefix . 'nf3_forms',
[156] Fix | Delete
'parent_total' => $form_count,
[157] Fix | Delete
),
[158] Fix | Delete
array(
[159] Fix | Delete
'table' => $this->db->prefix . 'nf3_action_meta',
[160] Fix | Delete
'parent' => $this->db->prefix . 'nf3_actions',
[161] Fix | Delete
'parent_total' => $action_count,
[162] Fix | Delete
),
[163] Fix | Delete
array(
[164] Fix | Delete
'table' => $this->db->prefix . 'nf3_fields',
[165] Fix | Delete
'parent' => $this->db->prefix . 'nf3_forms',
[166] Fix | Delete
'parent_total' => $form_count,
[167] Fix | Delete
),
[168] Fix | Delete
array(
[169] Fix | Delete
'table' => $this->db->prefix . 'nf3_field_meta',
[170] Fix | Delete
'parent' => $this->db->prefix . 'nf3_fields',
[171] Fix | Delete
'parent_total' => $field_count,
[172] Fix | Delete
),
[173] Fix | Delete
array(
[174] Fix | Delete
'table' => $this->db->prefix . 'nf3_object_meta',
[175] Fix | Delete
'parent' => $this->db->prefix . 'nf3_objects',
[176] Fix | Delete
'parent_total' => $object_count,
[177] Fix | Delete
),
[178] Fix | Delete
array(
[179] Fix | Delete
'table' => 'submissions',
[180] Fix | Delete
'parent_total' => $form_count,
[181] Fix | Delete
'purged' => false,
[182] Fix | Delete
'forms' => $forms,
[183] Fix | Delete
),
[184] Fix | Delete
);
[185] Fix | Delete
[186] Fix | Delete
$add = 0;
[187] Fix | Delete
// Set the steps for form meta (enforcing a minimum step count).
[188] Fix | Delete
$add = ceil( $form_count / $this->divisor );
[189] Fix | Delete
$steps = ( 0 == $add ) ? 1 : $add;
[190] Fix | Delete
// Add actions and fields.
[191] Fix | Delete
$steps *= 3;
[192] Fix | Delete
// Add action meta (enforcing a minimum step count).
[193] Fix | Delete
$add = ceil( $action_count / $this->divisor );
[194] Fix | Delete
$add = ( 0 == $add ) ? 1 : $add;
[195] Fix | Delete
$steps += $add;
[196] Fix | Delete
// Add field meta (enforcing a minimum step count).
[197] Fix | Delete
$add = ceil( $field_count / $this->divisor );
[198] Fix | Delete
$add = ( 0 == $add ) ? 1 : $add;
[199] Fix | Delete
$steps += $add;
[200] Fix | Delete
// Add object meta (enforcing a minimum step count).
[201] Fix | Delete
$add = ceil( $object_count / $this->divisor );
[202] Fix | Delete
$add = ( 0 == $add ) ? 1 : $add;
[203] Fix | Delete
$steps += $add;
[204] Fix | Delete
// Add one plus the form count for submissions (enforcing a minimum step count).
[205] Fix | Delete
$add = $form_count + 1;
[206] Fix | Delete
$add = ( 1 == $add ) ? 2 : $add;
[207] Fix | Delete
$steps += $add;
[208] Fix | Delete
[209] Fix | Delete
$this->running[ 0 ][ 'stages' ] = $stages;
[210] Fix | Delete
[211] Fix | Delete
// Record the total number of steps in this batch.
[212] Fix | Delete
$this->running[ 0 ][ 'steps' ] = $steps;
[213] Fix | Delete
// Record our current step (defaulted to 0 here).
[214] Fix | Delete
$this->running[ 0 ][ 'current' ] = 0;
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
/**
[218] Fix | Delete
* Function to cleanup any lingering temporary elements of a required update after completion.
[219] Fix | Delete
*
[220] Fix | Delete
* @since 3.4.0
[221] Fix | Delete
*/
[222] Fix | Delete
public function cleanup()
[223] Fix | Delete
{
[224] Fix | Delete
// Remove the current process from the array.
[225] Fix | Delete
array_shift( $this->running );
[226] Fix | Delete
// Record to our updates setting that this update is complete.
[227] Fix | Delete
$this->confirm_complete();
[228] Fix | Delete
// If we have no updates left to process...
[229] Fix | Delete
if ( empty( $this->running ) ) {
[230] Fix | Delete
// Call the parent cleanup method.
[231] Fix | Delete
parent::cleanup();
[232] Fix | Delete
}
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
/**
[236] Fix | Delete
* Function to get the number of objects in a table.
[237] Fix | Delete
*
[238] Fix | Delete
* @param $table (String) The name of the target table.
[239] Fix | Delete
*
[240] Fix | Delete
* @return (Int) The count of rows.
[241] Fix | Delete
*
[242] Fix | Delete
* @since 3.4.0
[243] Fix | Delete
*/
[244] Fix | Delete
public function get_total( $table )
[245] Fix | Delete
{
[246] Fix | Delete
$sql = "SELECT COUNT( `id` ) AS Total FROM `{$this->db->prefix}{$table}`;";
[247] Fix | Delete
$result = $this->db->get_results( $sql, 'ARRAY_A' );
[248] Fix | Delete
return intval( $result[ 0 ][ 'Total' ] );
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
/**
[252] Fix | Delete
* Function to perform a simple, single-step delete of orphan data.
[253] Fix | Delete
*
[254] Fix | Delete
* @since 3.4.0
[255] Fix | Delete
* @updated 3.4.11
[256] Fix | Delete
*/
[257] Fix | Delete
private function do_easy_delete()
[258] Fix | Delete
{
[259] Fix | Delete
// Remove the orphan records.
[260] Fix | Delete
$sql = "DELETE FROM `{$this->stage[ 'table' ]}`
[261] Fix | Delete
WHERE `parent_id` NOT IN(
[262] Fix | Delete
SELECT `id` FROM `{$this->stage[ 'parent' ]}`
[263] Fix | Delete
)";
[264] Fix | Delete
// Protect favorite fields from being removed.
[265] Fix | Delete
if ( false !== strpos( $this->stage[ 'table' ], 'nf3_fields' ) ) {
[266] Fix | Delete
$sql .= " AND `parent_id` <> 0";
[267] Fix | Delete
}
[268] Fix | Delete
$this->query( $sql );
[269] Fix | Delete
// Confirm that this stage is complete.
[270] Fix | Delete
$this->stage_complete = true;
[271] Fix | Delete
}
[272] Fix | Delete
[273] Fix | Delete
/**
[274] Fix | Delete
* Function to perform a multi-step delete of orphan data.
[275] Fix | Delete
*
[276] Fix | Delete
* @since 3.4.0
[277] Fix | Delete
* @updated 3.4.11
[278] Fix | Delete
*/
[279] Fix | Delete
private function do_step_delete()
[280] Fix | Delete
{
[281] Fix | Delete
// Get records from our table.
[282] Fix | Delete
$sub_sql = "SELECT DISTINCT( `parent_id` ) AS id FROM `{$this->stage[ 'table' ]}` ";
[283] Fix | Delete
// If we have a previous last record...
[284] Fix | Delete
if ( isset( $this->stage[ 'last' ] ) ) {
[285] Fix | Delete
// Make sure we exclude anything before it from the result.
[286] Fix | Delete
$sub_sql .= "WHERE `parent_id` > " . $this->stage[ 'last' ] . " ";
[287] Fix | Delete
} // Protect favorite fields from being removed.
[288] Fix | Delete
elseif ( false !== strpos( $this->stage[ 'table' ], 'nf3_fields' ) ) {
[289] Fix | Delete
$sub_sql .= "WHERE `parent_id` <> 0 ";
[290] Fix | Delete
}
[291] Fix | Delete
$sub_sql .= "ORDER BY `parent_id` ASC
[292] Fix | Delete
LIMIT {$this->divisor};";
[293] Fix | Delete
$result = $this->db->get_results( $sub_sql, 'ARRAY_A' );
[294] Fix | Delete
// Get the last affected row.
[295] Fix | Delete
$last = end( $result );
[296] Fix | Delete
// Squash our results to get a non-associative array.
[297] Fix | Delete
$result = $this->array_squash( $result );
[298] Fix | Delete
$this->stage[ 'last' ] = intval( $last[ 'id' ] );
[299] Fix | Delete
// Get records from the parent table.
[300] Fix | Delete
$sql = "SELECT `id` FROM `{$this->stage[ 'parent' ]}`
[301] Fix | Delete
WHERE `id` IN(" . implode( ', ', $result ) .
[302] Fix | Delete
");";
[303] Fix | Delete
$parent_result = $this->db->get_results( $sql, 'ARRAY_A' );
[304] Fix | Delete
// If we didn't get the same number of results...
[305] Fix | Delete
if ( count( $result ) !== count( $parent_result ) ) {
[306] Fix | Delete
// Merge our results.
[307] Fix | Delete
$result = array_merge( $result, $parent_result );
[308] Fix | Delete
// Convert the array to something we can sort by duplicates.
[309] Fix | Delete
$temp = array_count_values( array_column( $result, 'id' ) );
[310] Fix | Delete
// Get rid of all values that had more than 1 result.
[311] Fix | Delete
$temp = array_filter( $temp, array( $this, 'uniquify' ) );
[312] Fix | Delete
// Schedule all of the single result values for deletion.
[313] Fix | Delete
$delete = implode( ', ', array_keys( $temp ) );
[314] Fix | Delete
$sql = "DELETE FROM `{$this->stage[ 'table' ]}`
[315] Fix | Delete
WHERE `parent_id` IN( {$delete} );";
[316] Fix | Delete
$this->query( $sql );
[317] Fix | Delete
}
[318] Fix | Delete
// If we've not already determined the limit of this table...
[319] Fix | Delete
if ( ! isset( $this->stage[ 'max' ] ) ) {
[320] Fix | Delete
// Fetch the maximum value.
[321] Fix | Delete
$sql = "SELECT MAX( `parent_id` ) as target FROM `{$this->stage[ 'table' ]}`";
[322] Fix | Delete
$result = $this->db->get_results( $sql, 'ARRAY_A' );
[323] Fix | Delete
// Save a reference to it.
[324] Fix | Delete
$this->stage[ 'max' ] = intval( $result[ 0 ][ 'target' ] );
[325] Fix | Delete
}
[326] Fix | Delete
// If our last record is equal to our maximum record...
[327] Fix | Delete
if ( $this->stage[ 'last' ] == $this->stage[ 'max' ] ) {
[328] Fix | Delete
// Record that we're done with this stage.
[329] Fix | Delete
$this->stage_complete = true;
[330] Fix | Delete
}
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
/**
[334] Fix | Delete
* Function called by array_filter to remove ALL duplicate results.
[335] Fix | Delete
*
[336] Fix | Delete
* @param $v (Int) The number of results.
[337] Fix | Delete
*
[338] Fix | Delete
* @return (Boolean)
[339] Fix | Delete
*
[340] Fix | Delete
* @since 3.4.0
[341] Fix | Delete
*/
[342] Fix | Delete
private function uniquify( $v )
[343] Fix | Delete
{
[344] Fix | Delete
// If we have 1 result, keep it.
[345] Fix | Delete
return $v == 1;
[346] Fix | Delete
}
[347] Fix | Delete
[348] Fix | Delete
/**
[349] Fix | Delete
* Function to remove orphan submissions from the posts and postmeta tables.
[350] Fix | Delete
*
[351] Fix | Delete
* @since 3.4.0
[352] Fix | Delete
*/
[353] Fix | Delete
private function adopt_subs()
[354] Fix | Delete
{
[355] Fix | Delete
// Fetch a limited number of orphan subs to delete.
[356] Fix | Delete
$sub_sql = "SELECT m.post_id AS id from `{$this->db->prefix}postmeta` AS m
[357] Fix | Delete
LEFT OUTER JOIN `{$this->db->prefix}posts` AS p
[358] Fix | Delete
ON m.post_id = p.id
[359] Fix | Delete
WHERE m.meta_key = '_form_id'
[360] Fix | Delete
AND p.post_type = 'nf_sub'
[361] Fix | Delete
AND m.meta_value NOT IN(
[362] Fix | Delete
SELECT `id` from `{$this->db->prefix}nf3_forms`
[363] Fix | Delete
)
[364] Fix | Delete
ORDER BY m.post_id ASC
[365] Fix | Delete
LIMIT {$this->divisor};";
[366] Fix | Delete
$result = $this->db->get_results( $sub_sql, 'ARRAY_A' );
[367] Fix | Delete
// Count them.
[368] Fix | Delete
$count = count( $result );
[369] Fix | Delete
// Squash our results to get a non-associative array.
[370] Fix | Delete
$result = $this->array_squash( $result );
[371] Fix | Delete
// If we have records to be deleted...
[372] Fix | Delete
if ( 0 < $count ) {
[373] Fix | Delete
// Remove their postmeta.
[374] Fix | Delete
$sql = "DELETE FROM `{$this->db->prefix}postmeta`
[375] Fix | Delete
WHERE post_id IN(" . implode( ', ', $result ) . ");";
[376] Fix | Delete
$this->query( $sql );
[377] Fix | Delete
// Remove the posts.
[378] Fix | Delete
$sql = "DELETE FROM `{$this->db->prefix}posts`
[379] Fix | Delete
WHERE id IN(" . implode( ', ', $result ) . ");";
[380] Fix | Delete
$this->query( $sql );
[381] Fix | Delete
}
[382] Fix | Delete
// If the number of affected rows was less than our divisor...
[383] Fix | Delete
if ( $count < $this->divisor ) {
[384] Fix | Delete
// Mark that we've completed the process.
[385] Fix | Delete
$this->stage[ 'purged' ] = true;
[386] Fix | Delete
// Increment our step count.
[387] Fix | Delete
$this->running[ 0 ][ 'current' ] += 1;
[388] Fix | Delete
}
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
/**
[392] Fix | Delete
* Function to remove orphan field data that's still attached
[393] Fix | Delete
* to valid submissions from the postmeta table.
[394] Fix | Delete
*
[395] Fix | Delete
* @since 3.4.0
[396] Fix | Delete
*/
[397] Fix | Delete
private function adopt_fields()
[398] Fix | Delete
{
[399] Fix | Delete
// Get the form to work with.
[400] Fix | Delete
$form = array_pop( $this->stage[ 'forms' ] );
[401] Fix | Delete
// Fetch meta rows that match our criteria.
[402] Fix | Delete
$sql = "SELECT r.meta_id, r.meta_key AS field_id
[403] Fix | Delete
FROM `{$this->db->prefix}posts` AS p
[404] Fix | Delete
LEFT JOIN `{$this->db->prefix}postmeta` AS r
[405] Fix | Delete
ON r.post_id = p.id
[406] Fix | Delete
LEFT JOIN `{$this->db->prefix}postmeta` AS f
[407] Fix | Delete
ON f.post_id = p.id
[408] Fix | Delete
WHERE p.post_type = 'nf_sub'
[409] Fix | Delete
AND f.meta_key = '_form_id'
[410] Fix | Delete
AND f.meta_value = " . intval( $form[ 'id' ] ) . "
[411] Fix | Delete
AND r.meta_key LIKE '_field_%' ";
[412] Fix | Delete
// If last is set...
[413] Fix | Delete
if ( isset( $form[ 'last' ] ) ) {
[414] Fix | Delete
// Make sure we're getting new results instead of old ones.
[415] Fix | Delete
$sql .= "AND r.meta_id > {$form[ 'last' ]} ";
[416] Fix | Delete
}
[417] Fix | Delete
$sql .= "ORDER BY r.meta_id ASC
[418] Fix | Delete
LIMIT {$this->divisor};";
[419] Fix | Delete
$results = $this->db->get_results( $sql, 'ARRAY_A' );
[420] Fix | Delete
// Count them.
[421] Fix | Delete
$count = count( $results );
[422] Fix | Delete
// Get the last result.
[423] Fix | Delete
if ( 0 < $count ) {
[424] Fix | Delete
$last = end( $results );
[425] Fix | Delete
$last = $last[ 'meta_id' ];
[426] Fix | Delete
reset( $results );
[427] Fix | Delete
}
[428] Fix | Delete
// Get all fields associated with this form.
[429] Fix | Delete
$sql = "SELECT id FROM `{$this->db->prefix}nf3_fields` WHERE parent_id = " . intval( $form[ 'id' ] ) . ";";
[430] Fix | Delete
$fields = $this->db->get_results( $sql, 'ARRAY_A' );
[431] Fix | Delete
// Squash our results to get a non-associative array.
[432] Fix | Delete
$fields = $this->array_squash( $fields );
[433] Fix | Delete
$bad_records = array();
[434] Fix | Delete
// For each result...
[435] Fix | Delete
foreach( $results as $result ) {
[436] Fix | Delete
// Pull the text out of our meta.
[437] Fix | Delete
$id = str_replace( '_field_', '', $result[ 'field_id' ] );
[438] Fix | Delete
$id = intval( $id );
[439] Fix | Delete
// If this field isn't on the form...
[440] Fix | Delete
if ( ! in_array( $id, $fields ) ) {
[441] Fix | Delete
// Add it to our list to check later.
[442] Fix | Delete
$bad_records[ $id ][] = $result[ 'meta_id' ];
[443] Fix | Delete
}
[444] Fix | Delete
}
[445] Fix | Delete
[446] Fix | Delete
// Get a list of any old_field_ids from our field table update.
[447] Fix | Delete
$sql = "SELECT f.id AS new_id, m.meta_value AS id
[448] Fix | Delete
FROM `{$this->db->prefix}nf3_fields` AS f
[449] Fix | Delete
LEFT JOIN `{$this->db->prefix}nf3_field_meta` AS m
[450] Fix | Delete
ON f.id = m.parent_id
[451] Fix | Delete
WHERE f.parent_id = " . intval( $form[ 'id' ] ) . "
[452] Fix | Delete
AND m.meta_key = 'old_field_id';";
[453] Fix | Delete
$old_ids = $this->db->get_results( $sql, 'ARRAY_A' );
[454] Fix | Delete
// Squash our results to get an associative array.
[455] Fix | Delete
$old_ids = $this->array_squash( $old_ids );
[456] Fix | Delete
[457] Fix | Delete
// For each id in the bad records list...
[458] Fix | Delete
foreach ( $bad_records as $field => $meta ) {
[459] Fix | Delete
// If we have a new ID for that record...
[460] Fix | Delete
if ( isset( $old_ids[ $field ] ) ) {
[461] Fix | Delete
// Update our submissions.
[462] Fix | Delete
$sql = "UPDATE `{$this->db->prefix}postmeta`
[463] Fix | Delete
SET `meta_key` = '_field_" . $old_ids[ $field ] . "'
[464] Fix | Delete
WHERE `meta_id` IN(" . implode( ', ', $meta ) . ");";
[465] Fix | Delete
$this->query( $sql );
[466] Fix | Delete
} // Otherwise... (We don't have a new ID for it.)
[467] Fix | Delete
else {
[468] Fix | Delete
// Delete the orphan record.
[469] Fix | Delete
$sql = "DELETE FROM `{$this->db->prefix}postmeta`
[470] Fix | Delete
WHERE `meta_id` IN(" . implode( ', ', $meta ) . ");";
[471] Fix | Delete
$this->query( $sql );
[472] Fix | Delete
}
[473] Fix | Delete
}
[474] Fix | Delete
// If the number of affected rows was less than our divisor...
[475] Fix | Delete
if ( $count < $this->divisor ) {
[476] Fix | Delete
// Increment our step count.
[477] Fix | Delete
$this->running[ 0 ][ 'current' ] += 1;
[478] Fix | Delete
} // Otherwise... (We need to continue.)
[479] Fix | Delete
else {
[480] Fix | Delete
// Record where we stopped.
[481] Fix | Delete
$form[ 'last' ] = intval( $last );
[482] Fix | Delete
// Put our form back on the stack.
[483] Fix | Delete
array_push( $this->stage[ 'forms' ], $form );
[484] Fix | Delete
}
[485] Fix | Delete
// If there are no forms left to process...
[486] Fix | Delete
if ( empty( $this->stage[ 'forms' ] ) ) {
[487] Fix | Delete
// Mark that this stage is done.
[488] Fix | Delete
$this->stage_complete = true;
[489] Fix | Delete
}
[490] Fix | Delete
}
[491] Fix | Delete
[492] Fix | Delete
/**
[493] Fix | Delete
* Function to compress our db results into a more useful format.
[494] Fix | Delete
*
[495] Fix | Delete
* @param $data (Array) The result to be compressed.
[496] Fix | Delete
*
[497] Fix | Delete
* @return (Array) Associative if our data was complex.
[498] Fix | Delete
* Non-associative if our data was a single item.
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function