Commit 8abdc70f authored by Kenneth Hall's avatar Kenneth Hall

Merge branch 'stage-zero-tables' into 'develop'

Stage zero tables

See merge request ninja-forms/ninja-forms!3623
parents 8b779cb1 48ae311f
...@@ -271,6 +271,21 @@ _**nf3_upgrades**_ (Table of Forms as they exist in the current structure and th ...@@ -271,6 +271,21 @@ _**nf3_upgrades**_ (Table of Forms as they exist in the current structure and th
* NOT NULL * NOT NULL
* DEFAULT 0 * DEFAULT 0
### Chunks
_**nf3_chunks**_ (Table of Chunks created on publish to be reconstructed into a Ninja Forms cache)
* id (The unique ID of the Chunk)
* int(11)
* NOT NULL
* AUTO_INCREMENT
* Primary Key
* name (The name of the Chunk)
* varchar(200)
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
* value (The value of the Chunk)
* longtext
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
## Version 1.0 ## Version 1.0
Defined initial structure for Ninja Forms data layer. Defined initial structure for Ninja Forms data layer.
...@@ -542,4 +557,21 @@ _**nf3_upgrades**_ (Table of Forms as they exist in the current structure and th ...@@ -542,4 +557,21 @@ _**nf3_upgrades**_ (Table of Forms as they exist in the current structure and th
* stage (The current upgrade stage of the Form) * stage (The current upgrade stage of the Form)
* int(11) * int(11)
* NOT NULL * NOT NULL
* DEFAULT 0 * DEFAULT 0
\ No newline at end of file
Defined chunks table for ensuring collate of chunked publish.
### Chunks
_**nf3_chunks**_ (Table of Chunks created on publish to be reconstructed into a Ninja Forms cache)
* id (The unique ID of the Chunk)
* int(11)
* NOT NULL
* AUTO_INCREMENT
* Primary Key
* name (The name of the Chunk)
* varchar(200)
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
* value (The value of the Chunk)
* longtext
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
File mode changed from 100755 to 100644
...@@ -127,7 +127,7 @@ class NF_AJAX_Controllers_Form extends NF_Abstracts_Controller ...@@ -127,7 +127,7 @@ class NF_AJAX_Controllers_Form extends NF_Abstracts_Controller
} }
delete_user_option( get_current_user_id(), 'nf_form_preview_' . $form_data['id'] ); delete_user_option( get_current_user_id(), 'nf_form_preview_' . $form_data['id'] );
update_option( 'nf_form_' . $form_data[ 'id' ], $form_data ); WPN_Helper::update_nf_cache( $form_data[ 'id' ], $form_data );
do_action( 'ninja_forms_save_form', $form->get_id() ); do_action( 'ninja_forms_save_form', $form->get_id() );
......
...@@ -66,7 +66,7 @@ class NF_AJAX_Controllers_Submission extends NF_Abstracts_Controller ...@@ -66,7 +66,7 @@ class NF_AJAX_Controllers_Submission extends NF_Abstracts_Controller
$this->_respond(); $this->_respond();
} }
} else { } else {
$this->_form_cache = get_option( 'nf_form_' . $this->_form_id ); $this->_form_cache = WPN_Helper::get_nf_cache( $this->_form_id );
} }
// TODO: Update Conditional Logic to preserve field ID => [ Settings, ID ] structure. // TODO: Update Conditional Logic to preserve field ID => [ Settings, ID ] structure.
......
...@@ -255,7 +255,7 @@ class NF_Abstracts_Model ...@@ -255,7 +255,7 @@ class NF_Abstracts_Model
} }
if( ! $this->_settings ) { if( ! $this->_settings ) {
$form_cache = get_option('nf_form_' . $this->_parent_id); $form_cache = WPN_Helper::get_nf_cache( $this->_parent_id );
if ($form_cache) { if ($form_cache) {
if ('field' == $this->_type) { if ('field' == $this->_type) {
......
...@@ -67,7 +67,7 @@ class NF_Abstracts_ModelFactory ...@@ -67,7 +67,7 @@ class NF_Abstracts_ModelFactory
$this->_object = $this->_form = new NF_Database_Models_Form( $this->_db, $id ); $this->_object = $this->_form = new NF_Database_Models_Form( $this->_db, $id );
$form_cache = get_option( 'nf_form_' . $id, false ); $form_cache = WPN_Helper::get_nf_cache( $id );
if( $form_cache && isset ( $form_cache[ 'settings' ] ) ){ if( $form_cache && isset ( $form_cache[ 'settings' ] ) ){
$this->_object->update_settings( $form_cache[ 'settings' ] ); $this->_object->update_settings( $form_cache[ 'settings' ] );
...@@ -230,7 +230,7 @@ class NF_Abstracts_ModelFactory ...@@ -230,7 +230,7 @@ class NF_Abstracts_ModelFactory
if( $where || $fresh || ! $this->_fields ){ if( $where || $fresh || ! $this->_fields ){
$form_cache = get_option( 'nf_form_' . $form_id, false ); $form_cache = WPN_Helper::get_nf_cache( $form_id );
if( ! $form_cache ) { if( ! $form_cache ) {
$model_shell = new NF_Database_Models_Field($this->_db, 0); $model_shell = new NF_Database_Models_Field($this->_db, 0);
......
...@@ -160,7 +160,7 @@ class NF_Admin_CPT_Submission ...@@ -160,7 +160,7 @@ class NF_Admin_CPT_Submission
'id' => __( '#', 'ninja-forms' ), 'id' => __( '#', 'ninja-forms' ),
); );
$form_cache = get_option( 'nf_form_' . $form_id ); $form_cache = WPN_Helper::get_nf_cache( $form_id );
$form_fields = Ninja_Forms()->form( $form_id )->get_fields(); $form_fields = Ninja_Forms()->form( $form_id )->get_fields();
......
...@@ -267,7 +267,7 @@ final class NF_Admin_Menus_Forms extends NF_Abstracts_Menu ...@@ -267,7 +267,7 @@ final class NF_Admin_Menus_Forms extends NF_Abstracts_Menu
if( ! $form->get_tmp_id() ) { if( ! $form->get_tmp_id() ) {
if( $form_cache = get_option( 'nf_form_' . $form_id, false ) ) { if( $form_cache = WPN_Helper::get_nf_cache( $form_id ) ) {
$fields = $form_cache[ 'fields' ]; $fields = $form_cache[ 'fields' ];
} else { } else {
$fields = ($form_id) ? Ninja_Forms()->form($form_id)->get_fields() : array(); $fields = ($form_id) ? Ninja_Forms()->form($form_id)->get_fields() : array();
......
...@@ -50,24 +50,17 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess ...@@ -50,24 +50,17 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess
$this->remove_option(); $this->remove_option();
} }
// Fetch our option to see what step we're on. // Fetch our option to see what step we're on.
$batch = get_option( 'nf_chunk_publish_' . $this->form_id ); $batch = $this->get_chunk( 'nf_chunk_publish_' . $this->form_id );
// If we don't have an option to see what step we're on... // If we don't have an option to see what step we're on...
if ( ! $batch ) { if ( ! $batch ) {
// Run startup. // Run startup.
$this->startup(); $this->startup();
// Fetch our option now that it's created. // Fetch our option now that it's created.
$batch = get_option( 'nf_chunk_publish_' . $this->form_id ); $batch = $this->get_chunk( 'nf_chunk_publish_' . $this->form_id );
} }
$batch = explode( ',', $batch ); $batch = explode( ',', $batch );
// If we already have a chunk for this step... // Update the chunk.
if ( get_option( 'nf_form_' . $this->form_id . '_publishing_' . $batch[ 0 ] ) ) { $this->add_chunk( 'nf_form_' . $this->form_id . '_publishing_' . $batch[ 0 ], stripslashes( $this->data[ 'chunk' ] ) );
// Update it.
update_option( 'nf_form_' . $this->form_id . '_publishing_' . $batch[ 0 ], stripslashes( $this->data[ 'chunk' ] ) );
} // Otherwise... (No chunk was found.)
else {
// Add it.
add_option( 'nf_form_' . $this->form_id . '_publishing_' . $batch[ 0 ], stripslashes( $this->data[ 'chunk' ] ), '', 'no' );
}
// Increment our step. // Increment our step.
$batch[ 0 ]++; $batch[ 0 ]++;
// If this was our last step... // If this was our last step...
...@@ -77,7 +70,7 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess ...@@ -77,7 +70,7 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess
} // Otherwise... (We have more steps.) } // Otherwise... (We have more steps.)
else { else {
// Update our step option. // Update our step option.
update_option( 'nf_chunk_publish_' . $this->form_id, implode( ',', $batch ) ); $this->add_chunk( 'nf_chunk_publish_' . $this->form_id, implode( ',', $batch ) );
// Request our next chunk. // Request our next chunk.
$this->response[ 'requesting' ] = $batch[ 0 ]; $this->response[ 'requesting' ] = $batch[ 0 ];
} }
...@@ -94,7 +87,7 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess ...@@ -94,7 +87,7 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess
{ {
$value = '0,' . $this->data[ 'chunk_total' ]; $value = '0,' . $this->data[ 'chunk_total' ];
// Write our option to manage the process. // Write our option to manage the process.
add_option( 'nf_chunk_publish_' . $this->form_id, $value, '', 'no' ); $this->add_chunk( 'nf_chunk_publish_' . $this->form_id, $value );
// Process the first item. // Process the first item.
$this->process(); $this->process();
} }
...@@ -110,11 +103,11 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess ...@@ -110,11 +103,11 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess
$build = ''; $build = '';
$batch = get_option( 'nf_chunk_publish_' . $this->form_id ); $batch = $this->get_chunk( 'nf_chunk_publish_' . $this->form_id );
$batch = explode( ',', $batch ); $batch = explode( ',', $batch );
// Add all of our chunks into a string. // Add all of our chunks into a string.
for ( $i = 0; $i < $batch[ 1 ]; $i++ ) { for ( $i = 0; $i < $batch[ 1 ]; $i++ ) {
$build .= get_option( 'nf_form_' . $this->form_id . '_publishing_' . $i ); $build .= $this->get_chunk( 'nf_form_' . $this->form_id . '_publishing_' . $i );
} }
$form_data = json_decode( $build, ARRAY_A ); $form_data = json_decode( $build, ARRAY_A );
...@@ -216,7 +209,7 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess ...@@ -216,7 +209,7 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess
} }
delete_user_option( get_current_user_id(), 'nf_form_preview_' . $form_data['id'] ); delete_user_option( get_current_user_id(), 'nf_form_preview_' . $form_data['id'] );
update_option( 'nf_form_' . $form_data[ 'id' ], $form_data ); WPN_Helper::update_nf_cache( $form_data[ 'id' ], $form_data );
do_action( 'ninja_forms_save_form', $form->get_id() ); do_action( 'ninja_forms_save_form', $form->get_id() );
...@@ -236,37 +229,51 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess ...@@ -236,37 +229,51 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess
echo wp_json_encode( $response ); echo wp_json_encode( $response );
wp_die(); // this is required to terminate immediately and return a proper response wp_die(); // this is required to terminate immediately and return a proper response
}
/**************************************************************
* Start old data. /**
************************************************************** * Function to get our chunk data from the chunks table.
*/ *
// // Update the chunked cache option. * @param $slug (string) The name of the option in the db.
// $build = array(); * @return string or FALSE
// $batch = get_option( 'nf_chunk_publish_' . $this->form_id ); */
// $batch = explode( ',', $batch ); public function get_chunk( $slug ) {
// // Add all of our chunks onto an array. global $wpdb;
// for ( $i = 0; $i < $batch[ 1 ]; $i++ ) { // Get our option from our chunks table.
// $build[] = 'nf_form_' . $this->form_id . '_' . $i; $sql = $wpdb->prepare( "SELECT `value` FROM `{$wpdb->prefix}nf3_chunks` WHERE `name` = %s", $slug );
// } $data = $wpdb->get_results( $sql, 'ARRAY_A' );
// // Convert them to a string. // If it exists there...
// $build = implode( ',', $build ); if ( ! empty( $data ) ) {
// // If we already have a chunked option... // Hand it off.
// if ( get_option( 'nf_form_' . $this->form_id . '_chunks' ) ) { return $data[ 0 ][ 'value' ];
// // Update it. } // Otherwise... (It does not exist there.)
// update_option( 'nf_form_' . $this->form_id . '_chunks', $build ); else {
// } // Otherwise... (If we don't already have one.) // Try to fetch it from the options table.
// else { return get_option( $slug );
// // Create it. }
// add_option( 'nf_form_' . $this->form_id . '_chunks', $build, '', 'no' ); }
// }
// // Remove our option to manage the process. /**
// delete_option( 'nf_chunk_publish_' . $this->form_id ); * Function to replace update_option.
// $this->response[ 'batch_complete' ] = true; *
/************************************************************** * @param $slug (string) The name of the option in the db.
* End old data. * @param $content (string) The data to be stored in the option.
************************************************************** */
*/ public function add_chunk( $slug, $content ) {
// Check for an existing option.
global $wpdb;
$sql = "SELECT id FROM `{$wpdb->prefix}nf3_chunks` WHERE name = '{$slug}'";
$result = $wpdb->query( $sql );
// If we don't have one...
if ( empty ( $result ) ) {
// Insert it.
$sql = $wpdb->prepare( "INSERT INTO `{$wpdb->prefix}nf3_chunks` (name, value) VALUES ( %s, %s )", $slug, $content );
} // Otherwise... (We do have one.)
else {
// Update the existing one.
$sql = $wpdb->prepare( "UPDATE `{$wpdb->prefix}nf3_chunks` SET value = %s WHERE name = %s", $content, $slug );
}
$wpdb->query( $sql );
} }
/* /*
...@@ -274,12 +281,13 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess ...@@ -274,12 +281,13 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess
*/ */
public function remove_option() { public function remove_option() {
// Remove our option to manage the process. // Remove our option to manage the process.
delete_option( 'nf_chunk_publish_' . $this->form_id ); global $wpdb;
$sql = $wpdb->prepare( "DELETE FROM `{$wpdb->prefix}nf3_chunks` WHERE name = %s", 'nf_chunk_publish_' . $this->form_id );
$wpdb->query( $sql );
// If our form_id was a temp id... // If our form_id was a temp id...
if ( ! is_numeric( $this->form_id ) ) { if ( ! is_numeric( $this->form_id ) ) {
// Remove all of our chunk options. // Remove all of our chunk options.
global $wpdb; $sql = $wpdb->prepare( "DELETE FROM `" . $wpdb->prefix . "nf3_chunks` WHERE name LIKE %s", 'nf_form_' . $this->form_id . '_publishing_%' );
$sql = "DELETE FROM `" . $wpdb->prefix . "options` WHERE option_name LIKE 'nf_form_" . $this->form_id . "_publishing_%'";
$wpdb->query( $sql ); $wpdb->query( $sql );
} }
$this->data[ 'new_publish' ] = 'false'; $this->data[ 'new_publish' ] = 'false';
......
...@@ -17,6 +17,7 @@ class NF_Database_Migrations ...@@ -17,6 +17,7 @@ class NF_Database_Migrations
$this->migrations[ 'relationships' ] = new NF_Database_Migrations_Relationships(); $this->migrations[ 'relationships' ] = new NF_Database_Migrations_Relationships();
$this->migrations[ 'settings' ] = new NF_Database_Migrations_Settings(); $this->migrations[ 'settings' ] = new NF_Database_Migrations_Settings();
$this->migrations[ 'upgrades' ] = new NF_Database_Migrations_Upgrades(); $this->migrations[ 'upgrades' ] = new NF_Database_Migrations_Upgrades();
$this->migrations[ 'chunks' ] = new NF_Database_Migrations_Chunks();
} }
public function migrate() public function migrate()
...@@ -25,6 +26,16 @@ class NF_Database_Migrations ...@@ -25,6 +26,16 @@ class NF_Database_Migrations
$migration->_run(); $migration->_run();
} }
} }
/**
* Function to run all our stage one changes.
*/
public function do_stage_one()
{
foreach( $this->migrations as $migration ) {
$migration->_stage_one();
}
}
public function nuke( $areYouSure = FALSE, $areYouReallySure = FALSE ) public function nuke( $areYouSure = FALSE, $areYouReallySure = FALSE )
{ {
......
<?php if ( ! defined( 'ABSPATH' ) ) exit;
class NF_Database_Migrations_Chunks extends NF_Abstracts_Migration
{
/**
* Constructor function for the chunks table migration.
*/
public function __construct()
{
// Call the migration constructor with our table data.
parent::__construct(
'nf3_chunks',
'nf_migration_create_table_chunks'
);
}
/**
* Function to define the chunks table.
*/
public function run()
{
$query = "CREATE TABLE IF NOT EXISTS {$this->table_name()} (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(200),
`value` longtext,
UNIQUE KEY (`id`)
) {$this->charset_collate()};";
dbDelta( $query );
}
}
...@@ -60,7 +60,7 @@ final class NF_Database_Models_Form extends NF_Abstracts_Model ...@@ -60,7 +60,7 @@ final class NF_Database_Models_Form extends NF_Abstracts_Model
$this->delete_submissions(); $this->delete_submissions();
delete_option( 'nf_form_' . $this->_id ); WPN_Helper::delete_nf_cache( $this->_id );
} }
private function delete_submissions( ) { private function delete_submissions( ) {
...@@ -195,7 +195,7 @@ final class NF_Database_Models_Form extends NF_Abstracts_Model ...@@ -195,7 +195,7 @@ final class NF_Database_Models_Form extends NF_Abstracts_Model
)); ));
} }
update_option( 'nf_form_' . $form_id, $form_cache ); WPN_Helper::update_nf_cache( $form_id, $form_cache );
add_action( 'admin_notices', array( 'NF_Database_Models_Form', 'import_admin_notice' ) ); add_action( 'admin_notices', array( 'NF_Database_Models_Form', 'import_admin_notice' ) );
......
...@@ -332,7 +332,7 @@ final class NF_Display_Render ...@@ -332,7 +332,7 @@ final class NF_Display_Render
} }
if( $cache_updated ) { if( $cache_updated ) {
update_option('nf_form_' . $form_id, $form_cache); // Update form cache without duplicate fields. WPN_Helper::update_nf_cache( $form_id, $form_cache ); // Update form cache without duplicate fields.
} }
} }
......
...@@ -260,6 +260,74 @@ final class WPN_Helper ...@@ -260,6 +260,74 @@ final class WPN_Helper
} }
return $original; return $original;
} }
/**
* Function to fetch our cache from the upgrades table (if it exists there).
*
* @param $id (int) The form ID.
*
* @since 3.3.7
*/
public static function get_nf_cache( $id ) {
// See if we have the data in our table already.
global $wpdb;
$sql = "SELECT cache FROM `{$wpdb->prefix}nf3_upgrades` WHERE id = " . intval( $id );
$result = $wpdb->get_results( $sql, 'ARRAY_A' );
// If so...
if ( ! empty( $result ) ) {
// Unserialize the result.
$value = WPN_Helper::maybe_unserialize( $result[ 0 ][ 'cache' ] );
// Return it.
return $value;
} // Otherwise... (We don't have the data.)
else {
// Get it from the options table.
return get_option( 'nf_form_' . $id );
}
}
/**
* Function to insert or update our cache in the upgrades table (if it exists).
*
* @param $id (int) The form ID.
* @param $data (string) The form cache.
*
* @since 3.3.7
*/
public static function update_nf_cache( $id, $data ) {
// Define our current stage here for use as we run various upgrades.
$CURRENT_STAGE = 0;
// Serialize our data.
$cache = serialize( $data );
global $wpdb;
// See if we've already got a record.
$sql = "SELECT id FROM `{$wpdb->prefix}nf3_upgrades` WHERE id = " . intval( $id );
$result = $wpdb->get_results( $sql, 'ARRAY_A' );
// If we don't already have the data...
if ( empty( $result ) ) {
// Insert it.
$sql = $wpdb->prepare( "INSERT INTO `{$wpdb->prefix}nf3_upgrades` (id, cache, stage) VALUES (%d, %s, %s)", intval( $id ), $cache, $CURRENT_STAGE);
} // Otherwise... (We do have the data.)
else {
// Update the existing record.
$sql = $wpdb->prepare( "UPDATE `{$wpdb->prefix}nf3_upgrades` SET cache = %s WHERE id = %d", $cache, intval( $id ) ) ;
}
$wpdb->query( $sql );
}
/**
* Function to delete our cache.
*
* @param $id (int) The form ID.
*
* @since 3.3.7
*/
public static function delete_nf_cache( $id ) {
global $wpdb;
$sql = "DELETE FROM `{$wpdb->prefix}nf3_upgrades` WHERE id = " . intval( $id );
$wpdb->query( $sql );
delete_option( 'nf_form_' . intval( $id ) );
}
private static function parse_utf8_serialized( $matches ) private static function parse_utf8_serialized( $matches )
{ {
......
...@@ -18,7 +18,7 @@ function ninja_forms_ajax_import_form(){ ...@@ -18,7 +18,7 @@ function ninja_forms_ajax_import_form(){
$form_id = ( isset( $_POST[ 'formID' ] ) ) ? absint( $_POST[ 'formID' ] ) : ''; $form_id = ( isset( $_POST[ 'formID' ] ) ) ? absint( $_POST[ 'formID' ] ) : '';
delete_option( 'nf_form_' . $form_id ); // Bust the cache. WPN_Helper::delete_nf_cache( $form_id ); // Bust the cache.
Ninja_Forms()->form()->import_form( $import, TRUE, $form_id, TRUE ); Ninja_Forms()->form()->import_form( $import, TRUE, $form_id, TRUE );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment