GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-gfformsmodel.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Make some GFFormsModel public available.
4  * @since 1.16.2
5  */
6 
7 
9 
10  /**
11  * Make sure the method exists, regardless of GF version
12  *
13  * @since 1.22.2
14  *
15  * @return string|false False if not set, version string otherwise
16  */
17  public static function get_database_version() {
18 
19  if ( is_callable( 'parent::get_database_version' ) ) {
20  return parent::get_database_version();
21  }
22 
23  return get_option( 'gf_db_version' );
24  }
25 
26  /**
27  * Determines if the field value matches the conditional logic rule value.
28  *
29  * @since 1.22.3
30  *
31  * @param mixed $field_value The field value to be checked.
32  * @param mixed $target_value The conditional logic rule value.
33  * @param string $operation The conditional logic rule operator.
34  * @param null|GF_Field|string $source_field The field the rule is based on, or the entry meta
35  * @param null|array $rule The conditional logic rule properties.
36  * @param null|array $form The current form.
37  *
38  * @return bool
39  */
40  public static function is_value_match( $field_value, $target_value, $operation = 'is', $source_field = null, $rule = null, $form = null ) {
41 
42  if ( in_array( $source_field, array( 'date_created', 'date_updated', 'payment_date' ), true ) ) {
43  $field_value = is_int( $field_value )? $field_value : strtotime( $field_value );
44  $target_value = is_int( $target_value )? $target_value : strtotime( $target_value );
45  }
46 
47  if ( $source_field instanceof GF_Field && $source_field->type == 'date' ) {
48  $field_value = is_int( $field_value )? $field_value : strtotime( $field_value );
49  $target_value = is_int( $target_value )? $target_value : strtotime( $target_value );
50  }
51 
52  if ( in_array( $_operation = str_replace( ' ', '_', trim( $operation ) ), array( 'in', 'not_in' ) ) ) {
53  return GVCommon::matches_operation( (array) $field_value, (array) $target_value, $_operation );
54  }
55 
56  return parent::is_value_match( $field_value, $target_value, $operation, $source_field, $rule, $form );
57  }
58 
59  /**
60  * Given information provided in an entry, get array of media IDs
61  *
62  * This is necessary because GF doesn't expect to need to update post images, only to create them.
63  *
64  * @see GFFormsModel::create_post()
65  *
66  * @since 1.17
67  *
68  * @param array $form Gravity Forms form array
69  * @param array $entry Gravity Forms entry array
70  *
71  * @return array Array of "Field ID" => "Media IDs"
72  */
73  public static function get_post_field_images( $form, $entry ) {
74 
75  $post_data = self::get_post_fields( $form, $entry );
76 
77  $media = get_attached_media( 'image', $entry['post_id'] );
78 
79  $post_images = array();
80 
81  foreach ( $media as $media_item ) {
82  foreach( (array) $post_data['images'] as $post_data_item ) {
83  if(
84  \GV\Utils::get( $post_data_item, 'title' ) === $media_item->post_title &&
85  \GV\Utils::get( $post_data_item, 'description' ) === $media_item->post_content &&
86  \GV\Utils::get( $post_data_item, 'caption' ) === $media_item->post_excerpt
87  ) {
88  $post_images["{$post_data_item['field_id']}"] = $media_item->ID;
89  }
90  }
91  }
92 
93  return $post_images;
94  }
95 
96  /**
97  * Alias of GFFormsModel::get_post_fields(); just making it public
98  *
99  * @see GFFormsModel::get_post_fields()
100  *
101  * @since 1.17
102  *
103  * @param array $form Gravity Forms form array
104  * @param array $entry Gravity Forms entry array
105  *
106  * @return array
107  */
108  public static function get_post_fields( $form, $entry ) {
109 
110  $reflection = new ReflectionMethod( 'GFFormsModel', 'get_post_fields' );
111 
112  /**
113  * If the method changes to public, use Gravity Forms' method
114  * @todo: If/when the method is public, remove the unneeded copied code.
115  */
116  if( $reflection->isPublic() ) {
117  return parent::get_post_fields( $form, $entry );
118  }
119 
120  // It was private; let's make it public
121  $reflection->setAccessible( true );
122 
123  return $reflection->invoke( new GFFormsModel, $form, $entry );
124  }
125 
126  /**
127  * Copied function from Gravity Forms plugin \GFFormsModel::copy_post_image since the method is private.
128  *
129  * @since 1.16.2
130  *
131  * @param string $url URL of the post image to update
132  * @param int $post_id ID of the post image to update
133  * @return array|bool Array with `file`, `url` and `type` keys. False: failed to copy file to final directory path.
134  */
135  public static function copy_post_image( $url, $post_id ) {
136 
137  $reflection = new ReflectionMethod( 'GFFormsModel', 'copy_post_image' );
138 
139  /**
140  * If the method changes to public, use Gravity Forms' method
141  * @todo: If/when the method is public, remove the unneeded copied code.
142  */
143  if( $reflection->isPublic() ) {
144  return parent::copy_post_image( $url, $post_id );
145  }
146 
147  // It was private; let's make it public
148  $reflection->setAccessible( true );
149 
150  return $reflection->invoke( new GFFormsModel, $url, $post_id );
151  }
152 
153  /**
154  * Copied function from Gravity Forms plugin \GFFormsModel::media_handle_upload since the method is private.
155  *
156  * Note: The method became public in GF 1.9.17.7
157  *
158  * @see GFFormsModel::media_handle_upload
159  * @see GravityView_Edit_Entry_Render::maybe_update_post_fields
160  *
161  * @uses copy_post_image
162  * @uses wp_insert_attachment
163  * @uses wp_update_attachment_metadata
164  *
165  * @param string $url URL of the post image to update
166  * @param int $post_id ID of the post image to update
167  * @param array $post_data Array of data for the eventual attachment post type that is created using {@see wp_insert_attachment}. Supports `post_mime_type`, `guid`, `post_parent`, `post_title`, `post_content` keys.
168  * @return bool|int ID of attachment Post created. Returns false if file not created by copy_post_image
169  */
170  public static function media_handle_upload( $url, $post_id, $post_data = array() ) {
171 
172  $reflection = new ReflectionMethod( 'GFFormsModel', 'media_handle_upload' );
173 
174  /**
175  * If the method changes to public, use Gravity Forms' method
176  * @todo: If/when the method is public, remove the unneeded copied code.
177  */
178  if( $reflection->isPublic() ) {
179  return parent::media_handle_upload( $url, $post_id, $post_data );
180  }
181 
182  // It was private; let's make it public
183  $reflection->setAccessible( true );
184 
185  return $reflection->invoke( new GFFormsModel, $url, $post_id, $post_data );
186  }
187 
188 }
$url
Definition: post_image.php:25
static media_handle_upload( $url, $post_id, $post_data=array())
Copied function from Gravity Forms plugin ::media_handle_upload since the method is private...
static copy_post_image( $url, $post_id)
Copied function from Gravity Forms plugin ::copy_post_image since the method is private.
static get_post_field_images( $form, $entry)
Given information provided in an entry, get array of media IDs.
static is_value_match( $field_value, $target_value, $operation='is', $source_field=null, $rule=null, $form=null)
Determines if the field value matches the conditional logic rule value.
if(gravityview() ->plugin->is_GF_25()) $form
static matches_operation( $val1, $val2, $operation)
Wrapper for the GFFormsModel::matches_operation() method that adds additional comparisons, including: &#39;equals&#39;, &#39;greater_than_or_is&#39;, &#39;greater_than_or_equals&#39;, &#39;less_than_or_is&#39;, &#39;less_than_or_equals&#39;, &#39;not_contains&#39;, &#39;in&#39;, and &#39;not_in&#39;.
Make some GFFormsModel public available.
static get_post_fields( $form, $entry)
Alias of GFFormsModel::get_post_fields(); just making it public.
static get_database_version()
Make sure the method exists, regardless of GF version.
$entry
Definition: notes.php:27
$field_value
Definition: checkbox.php:24