Forked from spivurno/gw-gravity-forms-manual-entries.php
Created
February 8, 2018 02:33
-
-
Save joedooley/c61cce09349a51ce7afe020b2c9b137c to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms // Manual Entries
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
<?php | |
/** | |
* Gravity Wiz // Gravity Forms // Manual Entries | |
* | |
* Create entries manually for Gravity Forms. Adds an "Add New" button next to the page title on all entry-related pages. | |
* | |
* @version 1.2 | |
* @author David Smith <david@gravitywiz.com> | |
* @license GPL-2.0+ | |
* @link http://gravitywiz.com/... | |
* @copyright 2015 Gravity Wiz | |
* | |
* Plugin Name: Gravity Forms Manual Entries | |
* Plugin URI: http://gravitywiz.com | |
* Description: Create entries manually for Gravity Forms. Adds an "Add New" button next to the page title on all entry-related pages. | |
* Author: Gravity Wiz | |
* Version: 1.0 | |
* Author URI: http://gravitywiz.com | |
*/ | |
class GW_Manual_Entries { | |
public function __construct( $args = array() ) { | |
// do version check in the init to make sure if GF is going to be loaded, it is already loaded | |
add_action( 'init', array( $this, 'init' ) ); | |
} | |
public function init() { | |
// make sure we're running the required minimum version of Gravity Forms | |
if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) { | |
return; | |
} | |
add_filter( 'admin_print_scripts-forms_page_gf_entries', array( $this, 'output_entry_button_script' ) ); | |
$this->process_query_string(); | |
} | |
public function output_entry_button_script() { | |
$button = sprintf( '<a href="%s" class="page-title-action">%s</a>', add_query_arg( 'add_new', 1 ), __( 'Add New' ) ); | |
?> | |
<script type="text/javascript"> | |
var gwmeInterval = setInterval( function() { | |
var header = document.querySelectorAll( 'h1, h2' )[0]; | |
if( header ) { | |
clearInterval( gwmeInterval ); | |
header.innerHTML = header.innerHTML + '<?php echo $button; ?>'; | |
} | |
}, 100 ); | |
</script> | |
<?php | |
} | |
public function process_query_string() { | |
$is_entry_view = rgget( 'page' ) == 'gf_entries'; | |
$is_entry_list = $is_entry_view && ! rgget( 'lid' ); | |
$is_entry_detail = $is_entry_view && rgget( 'lid' ); | |
if( $is_entry_view && rgget( 'add_new' ) && rgget( 'id' ) ) { | |
$form_id = rgget( 'id' ); // @todo add support for id-less entry list page | |
$entry_id = GFAPI::add_entry( array( 'form_id' => $form_id ) ); | |
/* | |
* GF will not fetch an entry that does not have any data in the lead detail table. | |
* Let's add a placeholder value to avoid this error. | |
*/ | |
global $wpdb; | |
$wpdb->insert( $wpdb->prefix . 'rg_lead_detail', array( 'lead_id' => $entry_id, 'form_id' => $form_id, 'field_number' => 1, 'value' => '' ) ); | |
$entry_url = sprintf( '%s/wp-admin/admin.php?page=gf_entries&view=entry&id=%d&lid=%d&pos=0', get_bloginfo( 'wpurl' ), $form_id, $entry_id ); | |
wp_redirect( add_query_arg( array( 'edit' => 1 ), $entry_url ) ); | |
exit; | |
} | |
if( $is_entry_detail ) { | |
$is_edit_mode = rgget( 'edit' ) && rgget( 'lid' ); | |
if( $is_edit_mode && ! rgpost( 'action' ) ) { | |
$_POST['screen_mode'] = 'edit'; | |
} else if( $is_edit_mode && rgpost( 'action' ) == 'update' ) { | |
ob_start(); | |
add_action( 'gform_after_update_entry', array( $this, 'redirect_from_edit_mode' ) ); | |
} | |
} | |
} | |
public function redirect_from_edit_mode() { | |
wp_redirect( remove_query_arg( 'edit' ) ); | |
ob_end_flush(); | |
exit; | |
} | |
} | |
# Configuration | |
new GW_Manual_Entries(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment