GravityView  1.22.6
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 Katz Web Services, Inc.
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_blacklist', array( $this, 'edit_entry_field_blacklist' ), 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  * Adds Coupon fields to Edit Entry field blacklist
61  *
62  * @since 1.20
63  *
64  * @param array $blacklist Array of field types
65  * @param array $entry Entry array of entry being edited in Edit Entry
66  *
67  * @return array Blacklist array, with coupon possibly added
68  */
69  public function edit_entry_field_blacklist( $blacklist = array(), $entry = array() ) {
70 
71  if ( $this->should_hide_coupon_fields( $entry ) ) {
72  $blacklist[] = 'coupon';
73  }
74 
75  return $blacklist;
76  }
77 
78  /**
79  * Set the coupon values for entries that have coupons applied
80  *
81  * Uses $_POST hacks
82  *
83  * @since 1.20
84  *
85  * @param string $value
86  * @param GF_Field_Coupon $field
87  * @param GravityView_Edit_Entry_Render $Edit_Entry_Render
88  *
89  * @return string $value is returned unmodified. Only $_POST is modified.
90  */
91  public function edit_entry_field_value( $value, $field, $Edit_Entry_Render ) {
92 
93  if ( $this->should_hide_coupon_fields() ) {
94  return $value;
95  }
96 
97  $entry = $Edit_Entry_Render->entry;
98  $form = $Edit_Entry_Render->form;
99 
100  $coupon_codes = gf_coupons()->get_submitted_coupon_codes( $form, $entry );
101 
102  // Entry has no coupon codes
103  if ( ! $coupon_codes ) {
104  return $value;
105  }
106 
107  // No coupons match the codes provided
108  $discounts = gf_coupons()->get_coupons_by_codes( $coupon_codes, $form );
109 
110  if( ! $discounts ) {
111  return $value;
112  }
113 
114  /**
115  * @hack Fake POST data so that the data gets pre-filled. Both are needed.
116  * @see GF_Field_Coupon::get_field_input
117  */
118  $_POST = ! isset( $_POST ) ? array() : $_POST;
119  $_POST[ 'gf_coupons_' . $form['id'] ] = json_encode( (array) $discounts );
120  $_POST[ 'input_' . $field->id ] = implode( ',', $coupon_codes );
121 
122  return $value;
123  }
124 
125 }
126 
edit_entry_field_blacklist( $blacklist=array(), $entry=array())
Adds Coupon fields to Edit Entry field blacklist.
static entry_has_transaction_data( $entry=array())
Check if an entry has transaction data.
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.
$field
Definition: gquiz_grade.php:11