GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-post-category.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-post-category.php
4  * @package GravityView
5  * @subpackage includes\fields
6  */
7 
9 
10  var $name = 'post_category';
11 
12  var $is_searchable = true;
13 
14  var $search_operators = array( 'is', 'in', 'not in', 'isnot', 'contains');
15 
16  var $_gf_field_class_name = 'GF_Field_Post_Category';
17 
18  var $group = 'post';
19 
20  var $icon = 'dashicons-category';
21 
22  public function __construct() {
23  $this->label = esc_html__( 'Post Category', 'gk-gravityview' );
24 
25  add_action( 'gravityview/edit-entry/render/before', array( $this, 'add_edit_entry_post_category_choices_filter' ) );
26 
27  add_action( 'gravityview/edit-entry/render/after', array( $this, 'remove_edit_entry_post_category_choices_filter' ) );
28 
29  add_action( 'gravityview/edit_entry/after_update', array( $this, 'set_post_categories' ), 10, 2 );
30 
31  parent::__construct();
32  }
33 
34  /**
35  * Update the post categories based on all post category fields
36  *
37  * @since 1.17
38  *
39  * @param array $form Gravity Forms form array
40  * @param int $entry_id Numeric ID of the entry that was updated
41  *
42  * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure. false if there are no post category fields or connected post.
43  */
44  public function set_post_categories( $form = array(), $entry_id = 0 ) {
45 
46  $entry = GFAPI::get_entry( $entry_id );
47  $post_id = \GV\Utils::get( $entry, 'post_id' );
48 
49  if( empty( $post_id ) ) {
50  return false;
51  }
52 
53  $return = false;
54 
55  $post_category_fields = GFAPI::get_fields_by_type( $form, 'post_category' );
56 
57  if( $post_category_fields ) {
58 
59  $updated_categories = array();
60 
61  foreach ( $post_category_fields as $field ) {
62  // Get the value of the field, including $_POSTed value
63  $field_cats = RGFormsModel::get_field_value( $field );
64  $field_cats = is_array( $field_cats ) ? array_values( $field_cats ) : (array)$field_cats;
65  $field_cats = gv_map_deep( $field_cats, 'intval' );
66  $updated_categories = array_merge( $updated_categories, array_values( $field_cats ) );
67  }
68 
69  // Remove `0` values from intval()
70  $updated_categories = array_filter( $updated_categories );
71 
72  /**
73  * @filter `gravityview/edit_entry/post_categories/append` Should post categories be added to or replaced?
74  * @since 1.17
75  * @param bool $append If `true`, don't delete existing categories, just add on. If `false`, replace the categories with the submitted categories. Default: `false`
76  */
77  $append = apply_filters( 'gravityview/edit_entry/post_categories/append', false );
78 
79  $return = wp_set_post_categories( $post_id, $updated_categories, $append );
80  }
81 
82  return $return;
83  }
84 
85  /**
86  * Add filter to show live category choices in Edit Entry
87  *
88  * @see edit_entry_post_category_choices
89  *
90  * @since 1.17
91  *
92  * @return void
93  */
95  add_filter( 'gform_post_category_choices', array( $this, 'edit_entry_post_category_choices' ), 10, 3 );
96  }
97 
98  /**
99  * Remove filter to show live category choices in Edit Entry
100  *
101  * @see edit_entry_post_category_choices
102  *
103  * @since 1.17
104  *
105  * @return void
106  */
108  remove_filter( 'gform_post_category_choices', array( $this, 'edit_entry_post_category_choices' ), 10 );
109  }
110 
111  /**
112  * Always show the live Category values
113  *
114  * By default, Gravity Forms would show unchecked/default choices. We want to show the live Post categories
115  *
116  * @since 1.17
117  *
118  * @param $choices
119  * @param $field
120  * @param $form_id
121  *
122  * @return mixed
123  */
125 
126  $entry = GravityView_Edit_Entry::getInstance()->instances['render']->get_entry();
127 
128  // $entry['post_id'] should always be set, but we check to make sure.
129  if( $entry && isset( $entry['post_id'] ) && $post_id = $entry['post_id'] ) {
130 
131  $post_categories = wp_get_post_categories( $post_id, array( 'fields' => 'ids' ) );
132 
133  // Always use the live value
134  foreach ( $choices as &$choice ) {
135  $choice['isSelected'] = in_array( $choice['value'], array_values( $post_categories ) );
136  }
137  }
138 
139  return $choices;
140  }
141 
142  public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
143 
144  if( 'edit' === $context ) {
145  return $field_options;
146  }
147 
148  $this->add_field_support('dynamic_data', $field_options );
149  $this->add_field_support('link_to_term', $field_options );
150  $this->add_field_support('new_window', $field_options );
151 
152  $field_options['new_window']['requires'] = 'link_to_term';
153 
154  return $field_options;
155  }
156 
157 }
158 
$group
Modify field settings by extending this class.
$is_searchable
$icon
edit_entry_post_category_choices( $choices, $field, $form_id)
Always show the live Category values.
add_edit_entry_post_category_choices_filter()
Add filter to show live category choices in Edit Entry.
add_field_support( $key, &$field_options)
$name
if(gravityview() ->plugin->is_GF_25()) $form
gv_map_deep( $value, $callback)
Maps a function to all non-iterable elements of an array or an object.
__construct()
$search_operators
set_post_categories( $form=array(), $entry_id=0)
Update the post categories based on all post category fields.
$_gf_field_class_name
field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id)
remove_edit_entry_post_category_choices_filter()
Remove filter to show live category choices in Edit Entry.
if(empty( $created_by)) $form_id
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
$entry
Definition: notes.php:27
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
Definition: time.php:22
new GravityView_Field_Post_Category
static getInstance()