GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-post-image.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-post-image.php
4  * @package GravityView
5  * @subpackage includes\fields
6  */
7 
8 /**
9  * Add custom options for Post Image fields
10  */
12 
13  var $name = 'post_image';
14 
15  var $_gf_field_class_name = 'GF_Field_Post_Image';
16 
17  var $group = 'post';
18 
19  var $icon = 'dashicons-format-image';
20 
21  public function __construct() {
22  $this->label = esc_html__( 'Post Image', 'gk-gravityview' );
23  parent::__construct();
24  }
25 
26  public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
27 
28  unset ( $field_options['search_filter'] );
29 
30  if( 'edit' === $context ) {
31  return $field_options;
32  }
33 
34  $this->add_field_support('link_to_post', $field_options );
35 
36  // @since 1.5.4
37  $this->add_field_support('dynamic_data', $field_options );
38 
39  return $field_options;
40  }
41 
42  /**
43  * Convert Gravity Forms `|:|`-separated image data into an array
44  *
45  * If passed something other than a string, returns the passed value.
46  *
47  * @since 1.16.2
48  * @since 1.19.2 Converted from private to static public method
49  *
50  * @param string $value The stored value of an image, impoded with `|:|` values
51  *
52  * @return array with `url`, `title`, `caption` and `description` values
53  */
54  static public function explode_value( $value ) {
55 
56  // Already is an array, perhaps?
57  if ( ! is_string( $value ) ) {
58  return $value;
59  }
60 
61  $url = $title = $caption = $description = '';
62 
63  // If there's a |:| match, process. Otherwise, empty array!
64  if( preg_match( '/\|\:\|/', $value ) ) {
65  list( $url, $title, $caption, $description ) = array_pad( explode( '|:|', $value ), 4, false );
66  }
67 
68  return array(
69  'url' => $url,
70  'title' => $title,
71  'caption' => $caption,
72  'description' => $description,
73  );
74  }
75 
76  /**
77  * Returns the field inner markup
78  *
79  * Overriding GF_Field_Post_Image is necessary because they don't check for existing post image values, because
80  * GF only creates, not updates.
81  *
82  * @since 1.16.2
83  *
84  * @param array $form The Form Object currently being processed.
85  * @param string|array $value The field value. From default/dynamic population, $_POST, or a resumed incomplete submission.
86  * @param null|array $entry Null or the Entry Object currently being edited.
87  * @param GF_Field_Post_Image $field The field being edited.
88  *
89  * @return string
90  */
91  public function get_field_input( $form, $value = '', $entry = null, GF_Field_Post_Image $field = null ) {
92 
93  $id = (int) $field->id;
94  $form_id = $form['id'];
95  $input_name = "input_{$id}";
96  $field_id = sprintf( 'input_%d_%d', $form_id, $id );
97  $img_name = null;
98 
99  // Convert |:| to associative array
100  $img_array = self::explode_value( $value );
101 
102  if( ! empty( $img_array['url'] ) ) {
103 
104  $img_name = basename( $img_array['url'] );
105 
106  /**
107  * Set the $uploaded_files value so that the .ginput_preview renders, and the file upload is hidden
108  * @see GF_Field_Post_Image::get_field_input See the `<span class='ginput_preview'>` code
109  * @see GFFormsModel::get_temp_filename See the `rgget( $input_name, self::$uploaded_files[ $form_id ] );` code
110  */
111  if( empty( GFFormsModel::$uploaded_files[ $form_id ][ $input_name ] ) ) {
112  GFFormsModel::$uploaded_files[ $form_id ][ $input_name ] = $img_name;
113  }
114  }
115 
116  // Tell Gravity Forms we're not in the Admin
117  add_filter( 'gform_is_entry_detail', '__return_false' );
118  add_filter( 'gform_is_form_editor', '__return_false' );
119 
120  $input_value = array(
121  "{$id}.1" => \GV\Utils::get( $img_array, 'title' ),
122  "{$id}.4" => \GV\Utils::get( $img_array, 'caption' ),
123  "{$id}.7" => \GV\Utils::get( $img_array, 'description' ),
124  );
125 
126  // Get the field HTML output
127  $gf_post_image_field_output = $field->get_field_input( $form, $input_value );
128 
129  // Clean up our own filters
130  remove_filter( 'gform_is_entry_detail', '__return_false' );
131  remove_filter( 'gform_is_form_editor', '__return_false' );
132 
133  /**
134  * Insert a hidden field into the output that is used to store the image URL
135  * @var string $current_file We need to have a reference of whether same file is being updated, or user wants to remove the image.
136  * @see \GravityView_Edit_Entry_Render::maybe_update_post_fields
137  * @hack
138  */
139  if ( null !== $img_name ) {
140  $current_file = sprintf( "<input name='%s' id='%s' type='hidden' value='%s' />", $input_name, $field_id, esc_url_raw( $img_array['url'] ) );
141  $gf_post_image_field_output = str_replace('<span class=\'ginput_preview\'>', '<span class=\'ginput_preview\'>'.$current_file, $gf_post_image_field_output );
142  }
143 
144  return $gf_post_image_field_output;
145  }
146 
147 }
148 
$url
Definition: post_image.php:25
Modify field settings by extending this class.
$caption
Definition: post_image.php:27
$icon
field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id)
static explode_value( $value)
Convert Gravity Forms |:|-separated image data into an array.
add_field_support( $key, &$field_options)
if(gravityview() ->plugin->is_GF_25()) $form
Add custom options for Post Image fields.
new GravityView_Field_Post_Image
__construct()
$group
$_gf_field_class_name
if(empty( $created_by)) $form_id
$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
$name
$title