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
* NOT NULL
* 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
Defined initial structure for Ninja Forms data layer.
......@@ -543,3 +558,20 @@ _**nf3_upgrades**_ (Table of Forms as they exist in the current structure and th
* int(11)
* NOT NULL
* DEFAULT 0
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
}
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() );
......
......@@ -66,7 +66,7 @@ class NF_AJAX_Controllers_Submission extends NF_Abstracts_Controller
$this->_respond();
}
} 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.
......
......@@ -255,7 +255,7 @@ class NF_Abstracts_Model
}
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 ('field' == $this->_type) {
......
......@@ -67,7 +67,7 @@ class NF_Abstracts_ModelFactory
$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' ] ) ){
$this->_object->update_settings( $form_cache[ 'settings' ] );
......@@ -230,7 +230,7 @@ class NF_Abstracts_ModelFactory
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 ) {
$model_shell = new NF_Database_Models_Field($this->_db, 0);
......
......@@ -160,7 +160,7 @@ class NF_Admin_CPT_Submission
'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();
......
......@@ -267,7 +267,7 @@ final class NF_Admin_Menus_Forms extends NF_Abstracts_Menu
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' ];
} else {
$fields = ($form_id) ? Ninja_Forms()->form($form_id)->get_fields() : array();
......
......@@ -50,24 +50,17 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess
$this->remove_option();
}
// 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 ( ! $batch ) {
// Run startup.
$this->startup();
// 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 );
// If we already have a chunk for this step...
if ( get_option( 'nf_form_' . $this->form_id . '_publishing_' . $batch[ 0 ] ) ) {
// 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' );
}
// Update the chunk.
$this->add_chunk( 'nf_form_' . $this->form_id . '_publishing_' . $batch[ 0 ], stripslashes( $this->data[ 'chunk' ] ) );
// Increment our step.
$batch[ 0 ]++;
// If this was our last step...
......@@ -77,7 +70,7 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess
} // Otherwise... (We have more steps.)
else {
// 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.
$this->response[ 'requesting' ] = $batch[ 0 ];
}
......@@ -94,7 +87,7 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess
{
$value = '0,' . $this->data[ 'chunk_total' ];
// 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.
$this->process();
}
......@@ -110,11 +103,11 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess
$build = '';
$batch = get_option( 'nf_chunk_publish_' . $this->form_id );
$batch = $this->get_chunk( 'nf_chunk_publish_' . $this->form_id );
$batch = explode( ',', $batch );
// Add all of our chunks into a string.
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 );
......@@ -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'] );
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() );
......@@ -236,37 +229,51 @@ class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess
echo wp_json_encode( $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.
*
* @param $slug (string) The name of the option in the db.
* @return string or FALSE
*/
// // Update the chunked cache option.
// $build = array();
// $batch = get_option( 'nf_chunk_publish_' . $this->form_id );
// $batch = explode( ',', $batch );
// // Add all of our chunks onto an array.
// for ( $i = 0; $i < $batch[ 1 ]; $i++ ) {
// $build[] = 'nf_form_' . $this->form_id . '_' . $i;
// }
// // Convert them to a string.
// $build = implode( ',', $build );
// // If we already have a chunked option...
// if ( get_option( 'nf_form_' . $this->form_id . '_chunks' ) ) {
// // Update it.
// update_option( 'nf_form_' . $this->form_id . '_chunks', $build );
// } // Otherwise... (If we don't already have one.)
// else {
// // 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 );
// $this->response[ 'batch_complete' ] = true;
/**************************************************************
* End old data.
**************************************************************
public function get_chunk( $slug ) {
global $wpdb;
// Get our option from our chunks table.
$sql = $wpdb->prepare( "SELECT `value` FROM `{$wpdb->prefix}nf3_chunks` WHERE `name` = %s", $slug );
$data = $wpdb->get_results( $sql, 'ARRAY_A' );
// If it exists there...
if ( ! empty( $data ) ) {
// Hand it off.
return $data[ 0 ][ 'value' ];
} // Otherwise... (It does not exist there.)
else {
// Try to fetch it from the options table.
return get_option( $slug );
}
}
/**
* Function to replace update_option.
*
* @param $slug (string) The name of the option in the db.
* @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
*/
public function remove_option() {
// 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 ( ! is_numeric( $this->form_id ) ) {
// Remove all of our chunk options.
global $wpdb;
$sql = "DELETE FROM `" . $wpdb->prefix . "options` WHERE option_name LIKE 'nf_form_" . $this->form_id . "_publishing_%'";
$sql = $wpdb->prepare( "DELETE FROM `" . $wpdb->prefix . "nf3_chunks` WHERE name LIKE %s", 'nf_form_' . $this->form_id . '_publishing_%' );
$wpdb->query( $sql );
}
$this->data[ 'new_publish' ] = 'false';
......
......@@ -17,6 +17,7 @@ class NF_Database_Migrations
$this->migrations[ 'relationships' ] = new NF_Database_Migrations_Relationships();
$this->migrations[ 'settings' ] = new NF_Database_Migrations_Settings();
$this->migrations[ 'upgrades' ] = new NF_Database_Migrations_Upgrades();
$this->migrations[ 'chunks' ] = new NF_Database_Migrations_Chunks();
}
public function migrate()
......@@ -26,6 +27,16 @@ class NF_Database_Migrations
}
}
/**
* 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 )
{
if( ! $areYouSure || ! $areYouReallySure ) return;
......
<?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
$this->delete_submissions();
delete_option( 'nf_form_' . $this->_id );
WPN_Helper::delete_nf_cache( $this->_id );
}
private function delete_submissions( ) {
......@@ -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' ) );
......
......@@ -332,7 +332,7 @@ final class NF_Display_Render
}
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.
}
}
......
......@@ -261,6 +261,74 @@ final class WPN_Helper
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 )
{
if ( isset( $matches[2] ) ){
......
......@@ -18,7 +18,7 @@ function ninja_forms_ajax_import_form(){
$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 );
......
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