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