Last active
September 15, 2021 21:09
-
-
Save patilvikasj/40a249809d545c5b2c8d3d3e959bb2f5 to your computer and use it in GitHub Desktop.
This will register a WP CLI command to delete Gravity form Entries
This file contains hidden or 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
| <?php | |
| /** | |
| * Plugin Name: WP CLI commands for deleting Gravity form entries. | |
| * Plugin URI: https://brainstormforce.com | |
| * Description: Register WP CLI command to delete Gravity form entries. | |
| * Version: 0.0.1 | |
| * Author: Brainstorm Force | |
| * Author URI: https://brainstormforce.com | |
| * Text Domain: bsf | |
| */ | |
| function bsf_delete_gf_entries( $args = array(), $assoc_args = array() ) { | |
| // Get arguments. | |
| $arguments = wp_parse_args( | |
| $assoc_args, | |
| array( | |
| 'days_old' => 0, | |
| 'interval' => 'days', | |
| 'delete_db_entries' => false | |
| ) | |
| ); | |
| if( ! class_exists( 'GFFormsModel' ) ) { | |
| WP_CLI::error( 'Gravity forms plugins is not installed on this site' ); | |
| } | |
| if( empty( $arguments['days_old'] ) || 0 === $arguments['days_old'] ) { | |
| WP_CLI::error( 'Invalid arguments. Specify --days_old parameter' ); | |
| } | |
| global $wpdb; | |
| $results = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}gf_entry WHERE date_created < now() - interval {$arguments['days_old']} DAY", ARRAY_A ); | |
| if( is_wp_error( $results ) ) { | |
| WP_CLI::error( 'Error running MySql query.' . $wpdb->last_error ); | |
| } | |
| $entry_table = GFFormsModel::get_entry_table_name(); | |
| $entry_notes_table = GFFormsModel::get_entry_notes_table_name(); | |
| $entry_meta_table_name = GFFormsModel::get_entry_meta_table_name(); | |
| $entries_list = array(); | |
| if( ! empty( $results ) ) { | |
| foreach( (array) $results as $result ) { | |
| $entry_id = $result['id']; | |
| // Deleting uploaded files | |
| GFFormsModel::delete_files( $entry_id ); | |
| if( $arguments['delete_db_entries'] ) { | |
| // Delete from entry meta | |
| $sql = $wpdb->prepare( "DELETE FROM $entry_meta_table_name WHERE entry_id=%d", $entry_id ); | |
| $wpdb->query( $sql ); | |
| // Delete from lead notes | |
| $sql = $wpdb->prepare( "DELETE FROM $entry_notes_table WHERE entry_id=%d", $entry_id ); | |
| $wpdb->query( $sql ); | |
| // Delete from entry table | |
| $sql = $wpdb->prepare( "DELETE FROM $entry_table WHERE id=%d", $entry_id ); | |
| $wpdb->query( $sql ); | |
| } | |
| WP_CLI::line( "Deleted entry with ID " . $entry_id ); | |
| $entries_list[] = $entry_id; | |
| } | |
| } | |
| WP_CLI::success( 'Sucessfully deleted '. count( $entries_list ) . ' entries with ID.' . implode( " ,", $entries_list ) ); | |
| } | |
| if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
| WP_CLI::add_command( 'delete-gf-entries', 'bsf_delete_gf_entries' ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment