Gravity Wiz // Gravity Forms // Edit Products on Entry Detail
<?php
/**
* Gravity Wiz // Gravity Forms // Edit Products on Entry Detail
*
* Gravity Forms does not let you edit products on the entry detail page. This plugin let's you edit products.
*
* @version 1.0
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/...
* @copyright 2014 Gravity Wiz
*/
class GW_Edit_Products {
private static $instance = null;
public static function get_instance( $args = array() ) {
if( null == self::$instance )
self::$instance = new self( $args );
return self::$instance;
}
private function __construct( $args ) {
// make sure we're running the required minimum version of Gravity Forms
if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) )
return;
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'field_id' => false
) );
// time for hooks
add_filter( 'gform_field_input', array( $this, 'display_product_edit_mode' ), 10, 5 );
add_filter( 'gform_after_update_entry', array( $this, 'save_product_edits' ), 10, 2 );
}
public function display_product_edit_mode( $input, $field, $value, $entry_id, $form_id ) {
if( ! $this->is_entry_detail() || ! GFCommon::is_product_field( $field['type'] ) )
return $input;
$field['type'] = 'Hijacked by GW_Edit_Products!';
$input = $this->get_field_input( $field, $value, $entry_id, $form_id );
return $input;
}
public function get_field_input( $field, $value, $entry_id, $form_id ) {
remove_filter( 'gform_field_input', array( $this, 'display_product_edit_mode' ) );
$input = GFCommon::get_field_input( $field, $value, $entry_id, $form_id );
add_filter( 'gform_field_input', array( $this, 'display_product_edit_mode' ), 10, 5 );
return $input;
}
public function save_product_edits( $form, $entry_id ) {
if( ! $this->is_entry_detail() )
return;
$has_product_field = false;
foreach( $form['fields'] as &$field ) {
if( GFCommon::is_product_field( $field['type'] ) ) {
$has_product_field = true;
$field['type'] = 'Hijacked by GW_Edit_Products!';
}
}
if( $has_product_field )
GFFormsModel::save_lead( $form, GFAPI::get_entry( $entry_id ) );
$this->clear_product_cache( $entry_id );
}
public function clear_product_cache( $entry_id ) {
gform_delete_meta( $entry_id, 'gform_product_info_1_1' );
gform_delete_meta( $entry_id, 'gform_product_info__1' );
gform_delete_meta( $entry_id, 'gform_product_info_1_' );
gform_delete_meta( $entry_id, 'gform_product_info__' );
}
public function is_entry_detail() {
return in_array( GFForms::get_page(), array( 'entry_detail', 'entry_detail_edit' ) );
}
}
function gw_edit_products( $args = array() ) {
return GW_Edit_Products::get_instance( $args );
}
# Configuration
gw_edit_products();