Edit File by line
/home/barbar84/www/wp-conte.../plugins/ninja-fo.../includes/Admin/Processe...
File: ChunkPublish.php
<?php if ( ! defined( 'ABSPATH' ) ) exit;
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Class NF_Abstracts_Batch_Process
[3] Fix | Delete
*/
[4] Fix | Delete
class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess
[5] Fix | Delete
{
[6] Fix | Delete
// header( 'Content-Type: application/json' );
[7] Fix | Delete
private $data;
[8] Fix | Delete
private $form_id;
[9] Fix | Delete
protected $response = array(
[10] Fix | Delete
'last_request' => 'failure',
[11] Fix | Delete
'batch_complete' => false,
[12] Fix | Delete
);
[13] Fix | Delete
protected $_data = array();
[14] Fix | Delete
protected $_errors = array();
[15] Fix | Delete
protected $_debug = array();
[16] Fix | Delete
[17] Fix | Delete
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* Constructor
[21] Fix | Delete
*/
[22] Fix | Delete
public function __construct( $data = array() )
[23] Fix | Delete
{
[24] Fix | Delete
//Bail if we aren't in the admin.
[25] Fix | Delete
if ( ! is_admin() )
[26] Fix | Delete
return false;
[27] Fix | Delete
// Record our data if we have any.
[28] Fix | Delete
$this->data = $data[ 'data' ];
[29] Fix | Delete
$this->form_id = $this->data[ 'form_id' ];
[30] Fix | Delete
// Run process.
[31] Fix | Delete
$this->process();
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Function to loop over the batch.
[37] Fix | Delete
*
[38] Fix | Delete
* @return JSON
[39] Fix | Delete
* Str last_response = success/failure
[40] Fix | Delete
* Bool batch_complete = true/false
[41] Fix | Delete
* Int requesting = x
[42] Fix | Delete
*/
[43] Fix | Delete
public function process()
[44] Fix | Delete
{
[45] Fix | Delete
// If we were told this is a new publish...
[46] Fix | Delete
if ( $this->data[ 'new_publish' ] === 'true' ) {
[47] Fix | Delete
// Delete our option to reset the process.
[48] Fix | Delete
$this->remove_option();
[49] Fix | Delete
}
[50] Fix | Delete
// Fetch our option to see what step we're on.
[51] Fix | Delete
$batch = $this->get_chunk( 'nf_chunk_publish_' . $this->form_id );
[52] Fix | Delete
// If we don't have an option to see what step we're on...
[53] Fix | Delete
if ( ! $batch ) {
[54] Fix | Delete
// Run startup.
[55] Fix | Delete
$this->startup();
[56] Fix | Delete
// Fetch our option now that it's created.
[57] Fix | Delete
$batch = $this->get_chunk( 'nf_chunk_publish_' . $this->form_id );
[58] Fix | Delete
}
[59] Fix | Delete
$batch = explode( ',', $batch );
[60] Fix | Delete
// Update the chunk.
[61] Fix | Delete
$this->add_chunk( 'nf_form_' . $this->form_id . '_publishing_' . $batch[ 0 ], stripslashes( $this->data[ 'chunk' ] ) );
[62] Fix | Delete
// Increment our step.
[63] Fix | Delete
$batch[ 0 ]++;
[64] Fix | Delete
// If this was our last step...
[65] Fix | Delete
if ( $batch[ 0 ] == $batch[ 1 ] ) {
[66] Fix | Delete
// Run cleanup.
[67] Fix | Delete
$this->cleanup();
[68] Fix | Delete
} // Otherwise... (We have more steps.)
[69] Fix | Delete
else {
[70] Fix | Delete
// Update our step option.
[71] Fix | Delete
$this->add_chunk( 'nf_chunk_publish_' . $this->form_id, implode( ',', $batch ) );
[72] Fix | Delete
// Request our next chunk.
[73] Fix | Delete
$this->response[ 'requesting' ] = $batch[ 0 ];
[74] Fix | Delete
}
[75] Fix | Delete
$this->response[ 'last_request' ] = 'success';
[76] Fix | Delete
echo wp_json_encode( $this->response );
[77] Fix | Delete
wp_die();
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
[81] Fix | Delete
/**
[82] Fix | Delete
* Function to run any setup steps necessary to begin processing.
[83] Fix | Delete
*/
[84] Fix | Delete
public function startup()
[85] Fix | Delete
{
[86] Fix | Delete
$value = '0,' . $this->data[ 'chunk_total' ];
[87] Fix | Delete
// Write our option to manage the process.
[88] Fix | Delete
$this->add_chunk( 'nf_chunk_publish_' . $this->form_id, $value );
[89] Fix | Delete
// Process the first item.
[90] Fix | Delete
$this->process();
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
[94] Fix | Delete
/**
[95] Fix | Delete
* Function to cleanup any lingering temporary elements of a batch process after completion.
[96] Fix | Delete
*/
[97] Fix | Delete
public function cleanup()
[98] Fix | Delete
{
[99] Fix | Delete
[100] Fix | Delete
// Get all of the chunks.
[101] Fix | Delete
[102] Fix | Delete
[103] Fix | Delete
$build = '';
[104] Fix | Delete
$batch = $this->get_chunk( 'nf_chunk_publish_' . $this->form_id );
[105] Fix | Delete
$batch = explode( ',', $batch );
[106] Fix | Delete
// Add all of our chunks into a string.
[107] Fix | Delete
for ( $i = 0; $i < $batch[ 1 ]; $i++ ) {
[108] Fix | Delete
$build .= $this->get_chunk( 'nf_form_' . $this->form_id . '_publishing_' . $i );
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
$form_data = json_decode( $build, ARRAY_A );
[112] Fix | Delete
// Start copied code.
[113] Fix | Delete
if( is_string( $form_data[ 'id' ] ) ) {
[114] Fix | Delete
$tmp_id = $form_data[ 'id' ];
[115] Fix | Delete
$form = Ninja_Forms()->form()->get();
[116] Fix | Delete
$form->save();
[117] Fix | Delete
$form_data[ 'id' ] = $form->get_id();
[118] Fix | Delete
$this->_data[ 'new_ids' ][ 'forms' ][ $tmp_id ] = $form_data[ 'id' ];
[119] Fix | Delete
} else {
[120] Fix | Delete
$form = Ninja_Forms()->form($form_data['id'])->get();
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
unset( $form_data[ 'settings' ][ '_seq_num' ] );
[124] Fix | Delete
[125] Fix | Delete
$form->update_settings( $form_data[ 'settings' ] )->save();
[126] Fix | Delete
[127] Fix | Delete
if( isset( $form_data[ 'fields' ] ) ) {
[128] Fix | Delete
$db_fields_controller = new NF_Database_FieldsController( $form_data[ 'id' ], $form_data[ 'fields' ] );
[129] Fix | Delete
$db_fields_controller->run();
[130] Fix | Delete
$form_data[ 'fields' ] = $db_fields_controller->get_updated_fields_data();
[131] Fix | Delete
$this->_data['new_ids']['fields'] = $db_fields_controller->get_new_field_ids();
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
if( isset( $form_data[ 'deleted_fields' ] ) ){
[135] Fix | Delete
[136] Fix | Delete
foreach( $form_data[ 'deleted_fields' ] as $deleted_field_id ){
[137] Fix | Delete
[138] Fix | Delete
$field = Ninja_Forms()->form( $form_data[ 'id' ])->get_field( $deleted_field_id );
[139] Fix | Delete
$field->delete();
[140] Fix | Delete
}
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
if( isset( $form_data[ 'actions' ] ) ) {
[144] Fix | Delete
[145] Fix | Delete
/*
[146] Fix | Delete
* Loop Actions and fire Save() hooks.
[147] Fix | Delete
*/
[148] Fix | Delete
foreach ($form_data['actions'] as &$action_data) {
[149] Fix | Delete
[150] Fix | Delete
$id = $action_data['id'];
[151] Fix | Delete
[152] Fix | Delete
$action = Ninja_Forms()->form( $form_data[ 'id' ] )->get_action( $id );
[153] Fix | Delete
[154] Fix | Delete
$action->update_settings($action_data['settings'])->save();
[155] Fix | Delete
[156] Fix | Delete
$action_type = $action->get_setting( 'type' );
[157] Fix | Delete
[158] Fix | Delete
if( isset( Ninja_Forms()->actions[ $action_type ] ) ) {
[159] Fix | Delete
$action_class = Ninja_Forms()->actions[ $action_type ];
[160] Fix | Delete
[161] Fix | Delete
$action_settings = $action_class->save( $action_data['settings'] );
[162] Fix | Delete
if( $action_settings ){
[163] Fix | Delete
$action_data['settings'] = $action_settings;
[164] Fix | Delete
$action->update_settings( $action_settings )->save();
[165] Fix | Delete
}
[166] Fix | Delete
}
[167] Fix | Delete
[168] Fix | Delete
if ($action->get_tmp_id()) {
[169] Fix | Delete
[170] Fix | Delete
$tmp_id = $action->get_tmp_id();
[171] Fix | Delete
$this->_data['new_ids']['actions'][$tmp_id] = $action->get_id();
[172] Fix | Delete
$action_data[ 'id' ] = $action->get_id();
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
$this->_data[ 'actions' ][ $action->get_id() ] = $action->get_settings();
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
/*
[179] Fix | Delete
* Loop Actions and fire Publish() hooks.
[180] Fix | Delete
*/
[181] Fix | Delete
foreach ($form_data['actions'] as &$action_data) {
[182] Fix | Delete
[183] Fix | Delete
$action = Ninja_Forms()->form( $form_data[ 'id' ] )->get_action( $action_data['id'] );
[184] Fix | Delete
[185] Fix | Delete
$action_type = $action->get_setting( 'type' );
[186] Fix | Delete
[187] Fix | Delete
if( isset( Ninja_Forms()->actions[ $action_type ] ) ) {
[188] Fix | Delete
$action_class = Ninja_Forms()->actions[ $action_type ];
[189] Fix | Delete
[190] Fix | Delete
if( $action->get_setting( 'active' ) && method_exists( $action_class, 'publish' ) ) {
[191] Fix | Delete
$data = $action_class->publish( $this->_data );
[192] Fix | Delete
if ($data) {
[193] Fix | Delete
$this->_data = $data;
[194] Fix | Delete
}
[195] Fix | Delete
}
[196] Fix | Delete
}
[197] Fix | Delete
}
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
if( isset( $form_data[ 'deleted_actions' ] ) ){
[201] Fix | Delete
[202] Fix | Delete
foreach( $form_data[ 'deleted_actions' ] as $deleted_action_id ){
[203] Fix | Delete
[204] Fix | Delete
$action = Ninja_Forms()->form()->get_action( $deleted_action_id );
[205] Fix | Delete
$action->delete();
[206] Fix | Delete
}
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
delete_user_option( get_current_user_id(), 'nf_form_preview_' . $form_data['id'] );
[210] Fix | Delete
WPN_Helper::update_nf_cache( $form_data[ 'id' ], $form_data );
[211] Fix | Delete
[212] Fix | Delete
do_action( 'ninja_forms_save_form', $form->get_id() );
[213] Fix | Delete
[214] Fix | Delete
if( isset( $this->_data['debug'] ) ) {
[215] Fix | Delete
$this->_debug = array_merge( $this->_debug, $this->_data[ 'debug' ] );
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
if( isset( $this->_data['errors'] ) && $this->_data[ 'errors' ] ) {
[219] Fix | Delete
$this->_errors = array_merge( $this->_errors, $this->_data[ 'errors' ] );
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
// Remove our option.
[223] Fix | Delete
$this->remove_option();
[224] Fix | Delete
[225] Fix | Delete
$response = array( 'data' => $this->_data, 'errors' => $this->_errors, 'debug' => $this->_debug, 'batch_complete' => true );
[226] Fix | Delete
[227] Fix | Delete
echo wp_json_encode( $response );
[228] Fix | Delete
[229] Fix | Delete
wp_die(); // this is required to terminate immediately and return a proper response
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
/**
[233] Fix | Delete
* Function to get our chunk data from the chunks table.
[234] Fix | Delete
*
[235] Fix | Delete
* @param $slug (string) The name of the option in the db.
[236] Fix | Delete
* @return string or FALSE
[237] Fix | Delete
*/
[238] Fix | Delete
public function get_chunk( $slug ) {
[239] Fix | Delete
global $wpdb;
[240] Fix | Delete
// Get our option from our chunks table.
[241] Fix | Delete
$sql = $wpdb->prepare( "SELECT `value` FROM `{$wpdb->prefix}nf3_chunks` WHERE `name` = %s", $slug );
[242] Fix | Delete
$data = $wpdb->get_results( $sql, 'ARRAY_A' );
[243] Fix | Delete
// If it exists there...
[244] Fix | Delete
if ( ! empty( $data ) ) {
[245] Fix | Delete
// Hand it off.
[246] Fix | Delete
return $data[ 0 ][ 'value' ];
[247] Fix | Delete
} // Otherwise... (It does not exist there.)
[248] Fix | Delete
else {
[249] Fix | Delete
// Try to fetch it from the options table.
[250] Fix | Delete
return get_option( $slug );
[251] Fix | Delete
}
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
/**
[255] Fix | Delete
* Function to replace update_option.
[256] Fix | Delete
*
[257] Fix | Delete
* @param $slug (string) The name of the option in the db.
[258] Fix | Delete
* @param $content (string) The data to be stored in the option.
[259] Fix | Delete
*/
[260] Fix | Delete
public function add_chunk( $slug, $content ) {
[261] Fix | Delete
// Check for an existing option.
[262] Fix | Delete
global $wpdb;
[263] Fix | Delete
$sql = "SELECT id FROM `{$wpdb->prefix}nf3_chunks` WHERE name = '{$slug}'";
[264] Fix | Delete
$result = $wpdb->query( $sql );
[265] Fix | Delete
// If we don't have one...
[266] Fix | Delete
if ( empty ( $result ) ) {
[267] Fix | Delete
// Insert it.
[268] Fix | Delete
$sql = $wpdb->prepare( "INSERT INTO `{$wpdb->prefix}nf3_chunks` (name, value) VALUES ( %s, %s )", $slug, $content );
[269] Fix | Delete
} // Otherwise... (We do have one.)
[270] Fix | Delete
else {
[271] Fix | Delete
// Update the existing one.
[272] Fix | Delete
$sql = $wpdb->prepare( "UPDATE `{$wpdb->prefix}nf3_chunks` SET value = %s WHERE name = %s", $content, $slug );
[273] Fix | Delete
}
[274] Fix | Delete
$wpdb->query( $sql );
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
/*
[278] Fix | Delete
* Function to remove our management option and remove any temporary chunk data.
[279] Fix | Delete
*/
[280] Fix | Delete
public function remove_option() {
[281] Fix | Delete
// Remove our option to manage the process.
[282] Fix | Delete
global $wpdb;
[283] Fix | Delete
$sql = $wpdb->prepare( "DELETE FROM `{$wpdb->prefix}nf3_chunks` WHERE name = %s", 'nf_chunk_publish_' . $this->form_id );
[284] Fix | Delete
$wpdb->query( $sql );
[285] Fix | Delete
// If our form_id was a temp id...
[286] Fix | Delete
if ( ! is_numeric( $this->form_id ) ) {
[287] Fix | Delete
// Remove all of our chunk options.
[288] Fix | Delete
$sql = $wpdb->prepare( "DELETE FROM `" . $wpdb->prefix . "nf3_chunks` WHERE name LIKE %s", 'nf_form_' . $this->form_id . '_publishing_%' );
[289] Fix | Delete
$wpdb->query( $sql );
[290] Fix | Delete
}
[291] Fix | Delete
$this->data[ 'new_publish' ] = 'false';
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
}
[295] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function