Created
March 12, 2018 03:27
-
-
Save ericwindham/b5904138b1e9abcd08d832b8a4a187f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// use the smaller size for testing. | |
const CHUNK_SIZE = 100000; // Arbitrary size until we decide | |
// const CHUNK_SIZE = 65535; // MySQL TEXT Type | |
/** | |
* this code will set up pre_option and pre_update filter for the | |
* large cache fix | |
**/ | |
foreach( Ninja_Forms()->form()->get_forms() as $form ){ | |
// if( ! get_option( 'nf_form_' . $form->get_id() . '_chunks', false ) ) continue; | |
add_filter( 'pre_option_nf_form_' . $form->get_id(), | |
'WPN_Helper::pre_option', 10, 1 ); | |
add_filter( 'pre_update_option_nf_form_' . $form->get_id(), | |
'WPN_Helper::pre_update_option', 10, 2 ); | |
} | |
// Helper.php | |
public static function pre_option( $value ) | |
{ | |
$filter = str_replace( 'pre_option_', '', current_filter() ); | |
$flag = $filter . '_chunks'; | |
// if there is no nf_form_x_chunks option, we have not chunked | |
// this for, carry on | |
if( ! get_option( $flag ) ) return $value; | |
$new_value = ''; | |
// if we have chunk'd it, get the list of chunks | |
$options = explode( ',', get_option( $flag ) ); | |
//get the option value of each chunk and concat them into the form | |
foreach( $options as $option ){ | |
$new_value .= get_option( $option ); | |
} | |
return maybe_unserialize( $new_value ); | |
} | |
public static function pre_update_option( $new_value, $old_value ) | |
{ | |
if( is_array( $new_value ) ){ | |
$tmp_new_value = maybe_serialize( $new_value ); | |
} | |
// if serialized form length is less than chunk size, then carry on | |
if ( self::CHUNK_SIZE > strlen($tmp_new_value) ) return $new_value; | |
// otherwise, get the name of the option we want to set | |
$filter = str_replace( 'pre_update_option_', '', current_filter() ); | |
$new_options = array(); | |
// create the chunks | |
$chunks = explode("\r\n", chunk_split($tmp_new_value, self::CHUNK_SIZE)); | |
// create chunk option, eg. nf_form_1_0, nf_form_1_1... | |
foreach ($chunks as $key => $value) { | |
if( '' == $value ) continue; | |
$option = $filter . '_' . $key; | |
update_option($option, $value, false); | |
$new_options[] = $option; | |
} | |
// set the chunk option with a value of the list of the chunks | |
update_option($filter . '_chunks', implode(',', $new_options)); | |
return $new_value; | |
} | |
// AJAX/Controllers/Form.php | |
// if we've had chunked cache in the past, remove the reference | |
if( get_option( 'nf_form_' . $form->get_id() . '_chunks', false ) ) { | |
delete_option( 'nf_form_' . $form->get_id() . '_chunks' ); | |
} | |
// attach filters that might enable chunking if needed. | |
add_filter( 'pre_option_nf_form_' . $form->get_id(), | |
'WPN_Helper::pre_option', 10, 1 ); | |
add_filter( 'pre_update_option_nf_form_' . $form->get_id(), | |
'WPN_Helper::pre_update_option', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment