Skip to content

Instantly share code, notes, and snippets.

@New0
Created November 14, 2023 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save New0/db80ec642b918c8415e3c4f14d14a3c1 to your computer and use it in GitHub Desktop.
Save New0/db80ec642b918c8415e3c4f14d14a3c1 to your computer and use it in GitHub Desktop.
Reset the wp cron job to cleanup expired Ninja Forms nf_processing_data session entries from the options table
<?php
/**
* Plugin Name: Ninja Forms - Clean nf_processing_data
* Description: Clean the expired nf_processing_data entries from options table twice a day
* Version: 0.0.1
* Author: New0
*/
/**
* The function `delete_nf_processing_data` deletes rows from the WordPress options table where the
* option value contains the string 'nf_processing_data'.
*/
function select_nf_processing_data_entries() {
global $wpdb;
$table_prefix = $wpdb->prefix;
$options_table = $table_prefix . 'options';
$entries = $wpdb->get_results("SELECT * FROM $options_table WHERE option_value LIKE '%nf_processing_data%'", "ARRAY_A");
return $entries;
}
/**
* The function "select_expired_entries" retrieves expired entries from the WordPress options table
* based on a given identifier.
*
* @param string expired_identifier The expired_identifier parameter is a unique identifier that is used to
* identify a specific set of expired entries. It is used to construct the option_name in the SQL query
* to select the expired entries from the options table.
*
* @return an array of expired entries from the options table in the WordPress database.
*/
function select_expired_entries($expired_identifier) {
global $wpdb;
$table_prefix = $wpdb->prefix;
$options_table = $table_prefix . 'options';
$select_expired = '_wp_session_expires_' . $expired_identifier;
$entries = $wpdb->get_results("SELECT * FROM $options_table WHERE option_name LIKE '$select_expired'", "ARRAY_A");
return $entries;
}
/**
* The function "delete_nf_processing_data" deletes expired entries from the "options" table in the database.
*/
function delete_nf_processing_data() {
$expired = false;
$entries = select_nf_processing_data_entries();
if(!empty($entries)) {
foreach($entries as $entry) {
$explode_identifier = explode('_', $entry['option_name']);
$expired_identifier = array_pop($explode_identifier);
if($expired_identifier) {
$expires = select_expired_entries($expired_identifier);
if($expires){
$expired = $expires[0]['option_value'] < time();
}
}
if($expired){
delete_option($entry['option_name']);
delete_option('_wp_session_expires_' . $expired_identifier);
}
}
}
}
add_action('nf_processing_data_garbage_collection', 'delete_nf_processing_data');
/**
* Register the garbage collector as a twice daily event.
*/
function nf_processing_data_register_garbage_collection() {
if ( ! wp_next_scheduled( 'nf_processing_data_garbage_collection' ) ) {
wp_schedule_event( current_time( 'timestamp' ), 'twicedaily', 'nf_processing_data_garbage_collection' );
}
}
function nf_unhook_processing_data_register_garbage_collection() {
wp_clear_scheduled_hook( 'nf_processing_data_garbage_collection' );
}
register_deactivation_hook( __FILE__, 'nf_unhook_processing_data_register_garbage_collection' );
register_activation_hook( __FILE__, 'nf_processing_data_register_garbage_collection' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment