GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-plugin-hooks-gravity-forms-coupon.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Add Gravity Forms Coupon compatibility to Edit Entry
4  *
5  * @file class-gravityview-plugin-hooks-gravity-forms-coupon.php
6  * @package GravityView
7  * @license GPL2+
8  * @author GravityView <[email protected]>
9  * @link https://gravityview.co
10  * @copyright Copyright 2017, Katz Web Services, Inc.
11  *
12  * @since 1.20
13  */
14 
15 /**
16  * @since 1.20
17  */
19 
20  /**
21  * @var string gf_coupons() wrapper function only exists in Version 2.x; don't want to support 1.x
22  * @since 1.20
23  */
24  protected $function_name = 'gf_coupons';
25 
26  /**
27  * @since 1.20
28  */
29  protected function add_hooks() {
30  parent::add_hooks();
31 
32  add_filter( 'gravityview/edit_entry/field_blocklist', array( $this, 'edit_entry_field_blocklist' ), 10, 2 );
33  add_filter( 'gravityview/edit_entry/field_value_coupon', array( $this, 'edit_entry_field_value' ), 10, 3 );
34  }
35 
36  /**
37  * Should Coupon fields be hidden in Edit Entry?
38  *
39  * @since 1.20
40  *
41  * @param array $entry Entry being edited in Edit Entry, if set
42  *
43  * @return bool True: Yes, show coupon fields in Edit Entry; False: no, don't show Coupon fields
44  */
45  public function should_hide_coupon_fields( $entry = array() ) {
46 
47  $has_transaction_data = GVCommon::entry_has_transaction_data( $entry );
48 
49  /**
50  * @filter `gravityview/edit_entry/hide-coupon-fields` Should Coupon fields be hidden in Edit Entry?
51  * @since 1.20
52  * @param bool $has_transaction_data If true (the Entry has transaction data), hide the fields. Otherwise (false), show the Coupon field
53  */
54  $hide_coupon_fields = apply_filters( 'gravityview/edit_entry/hide-coupon-fields', $has_transaction_data );
55 
56  return (bool) $hide_coupon_fields;
57  }
58 
59  /**
60  * @depecated 2.14
61  * @since 1.20
62  */
63  public function edit_entry_field_blacklist( $blocklist = array(), $entry = array() ) {
64  _deprecated_function( __METHOD__, '2.14', 'GravityView_Plugin_Hooks_Gravity_Forms_Coupon::edit_entry_field_blocklist' );
65  return $this->edit_entry_field_blocklist( $blocklist, $entry );
66  }
67 
68  /**
69  * Adds Coupon fields to Edit Entry field blocklist
70  *
71  * @since 1.20
72  *
73  * @param array $blocklist Array of field types
74  * @param array $entry Entry array of entry being edited in Edit Entry
75  *
76  * @return array Blocklist array, with coupon possibly added
77  */
78  public function edit_entry_field_blocklist( $blocklist = array(), $entry = array() ) {
79 
80  if ( $this->should_hide_coupon_fields( $entry ) ) {
81  $blocklist[] = 'coupon';
82  }
83 
84  return $blocklist;
85  }
86 
87  /**
88  * Set the coupon values for entries that have coupons applied
89  *
90  * Uses $_POST hacks
91  *
92  * @since 1.20
93  *
94  * @param string $value
95  * @param GF_Field_Coupon $field
96  * @param GravityView_Edit_Entry_Render $Edit_Entry_Render
97  *
98  * @return string $value is returned unmodified. Only $_POST is modified.
99  */
100  public function edit_entry_field_value( $value, $field, $Edit_Entry_Render ) {
101 
102  if ( $this->should_hide_coupon_fields() ) {
103  return $value;
104  }
105 
106  $entry = $Edit_Entry_Render->entry;
107  $form = $Edit_Entry_Render->form;
108 
109  $coupon_codes = gf_coupons()->get_submitted_coupon_codes( $form, $entry );
110 
111  // Entry has no coupon codes
112  if ( ! $coupon_codes ) {
113  return $value;
114  }
115 
116  // No coupons match the codes provided
117  $discounts = gf_coupons()->get_coupons_by_codes( $coupon_codes, $form );
118 
119  if( ! $discounts ) {
120  return $value;
121  }
122 
123  /**
124  * @hack Fake POST data so that the data gets pre-filled. Both are needed.
125  * @see GF_Field_Coupon::get_field_input
126  */
127  $_POST = ! isset( $_POST ) ? array() : $_POST;
128  $_POST[ 'gf_coupons_' . $form['id'] ] = json_encode( (array) $discounts );
129  $_POST[ 'input_' . $field->id ] = implode( ',', $coupon_codes );
130 
131  return $value;
132  }
133 
134 }
135 
static entry_has_transaction_data( $entry=array())
Check if an entry has transaction data.
if(gravityview() ->plugin->is_GF_25()) $form
edit_entry_field_blocklist( $blocklist=array(), $entry=array())
Adds Coupon fields to Edit Entry field blocklist.
should_hide_coupon_fields( $entry=array())
Should Coupon fields be hidden in Edit Entry?
Abstract class that makes it easy for plugins and themes to register no-conflict scripts and styles...
$entry
Definition: notes.php:27
edit_entry_field_value( $value, $field, $Edit_Entry_Render)
Set the coupon values for entries that have coupons applied.