GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
field-post_image-html.php
Go to the documentation of this file.
1 <?php
2 /**
3  * The default post_image field output template.
4  *
5  * @global \GV\Template_Context $gravityview
6  * @since 2.0
7  */
8 
9 if ( ! isset( $gravityview ) || empty( $gravityview->template ) ) {
10  gravityview()->log->error( '{file} template loaded without context', array( 'file' => __FILE__ ) );
11  return;
12 }
13 
14 $field = $gravityview->field->field;
15 $value = $gravityview->value;
16 $entry = $gravityview->entry->as_entry();
17 $field_settings = $gravityview->field->as_configuration();
18 
19 /**
20  * Parse the stored value of the post image
21  *
22  * The image is stored with `|:|` dividing the fields. Break it into its parts and see what's set.
23  *
24  * @see GFCommon::get_lead_field_display()
25  * @var array
26  */
27 $ary = explode("|:|", $value);
28 $url = count($ary) > 0 ? $ary[0] : "";
29 $title = count($ary) > 1 ? $ary[1] : "";
30 $caption = count($ary) > 2 ? $ary[2] : "";
31 $description = count($ary) > 3 ? $ary[3] : "";
32 
33 $link_atts = array();
34 
35 /**
36  * @since 1.5.4
37  *
38  * $field['postFeaturedImage'] - holds if the Post Image field is set as post featured image
39  * $field_settings['dynamic_data'] - whether the field content should be fetched from the Post (dynamic data) or from the GF entry
40  *
41  * Dynamic data (get post featured image instead of GF entry field)
42  */
43 if ( ! empty( $field['postFeaturedImage'] ) && ! empty( $field_settings['dynamic_data'] ) && ! empty( $entry['post_id'] ) && has_post_thumbnail( $entry['post_id'] ) ) {
44 
45  /**
46  * Modify what size is fetched for the post's Featured Image
47  * @param string $size The size to be fetched using `wp_get_attachment_image_src()` (default: 'large')
48  * @param array $entry Gravity Forms entry array
49  * @since 2.0
50  * @param \GV\Template_Context $context The context
51  */
52  $image_size = apply_filters( 'gravityview/fields/post_image/size', 'large', $entry, $gravityview );
53  $image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $entry['post_id'] ), $image_size );
54 
55  if ( empty( $image_url[0] ) ) {
56  do_action('gravityview_log_debug', 'Dynamic featured image for post #'.$entry['post_id'].' doesnt exist (size: '.$image_size.').' );
57  } else {
58  $url = $image_url[0];
59  }
60 }
61 
62 ##
63 ## Get the link URL
64 ##
65 
66 // Link to the post created by the entry
67 if ( ! empty( $field_settings['link_to_post'] ) ) {
68  $href = get_permalink( $entry['post_id'] );
69 }
70 // Link to the single entry
71 else if ( ! empty( $field_settings['show_as_link'] ) ) {
73 }
74 // Link to the file itself
75 else {
76 
77  $href = $url;
78 
79  // Only show the lightbox if linking to the file itself
80  if ( $gravityview->view->settings->get( 'lightbox' ) ) {
81  $link_atts['class'] = apply_filters( 'gravityview_lightbox_script', 'thickbox' );
82  }
83 
84 }
85 
86 
87 // Set the attributes for the link
88 $link_atts['href'] = $href;
89 
90 // Add the title as the link title, if exists. This will appear as caption in the lightbox.
91 $link_atts['title'] = $title;
92 
93 
94 ##
95 ## Get the image
96 ##
97 $image_atts = array(
98  'src' => $url,
99  'alt' => ( !empty( $caption ) ? $caption : $title ),
100  'validate_src' => false, // Already validated by GF
101 );
102 
104 
105 
106 /**
107  * @filter `gravityview_post_image_meta` Modify the values used for the image meta.
108  * @see https://gravityview.co/support/documentation/201606759 Read more about the filter
109  * @var array $image_meta Associative array with `title`, `caption`, and `description` keys, each an array with `label`, `value`, `tag_label` and `tag_value` keys
110  */
111 $image_meta = apply_filters( 'gravityview_post_image_meta', array(
112  'title' => array(
113  'label' => esc_attr_x( 'Title:', 'Post Image field title heading', 'gk-gravityview'),
114  'value' => $title,
115  'tag_label' => 'span',
116  'tag_value' => 'div'
117  ),
118  'caption' => array(
119  'label' => esc_attr_x( 'Caption:', 'Post Image field caption heading', 'gk-gravityview'),
120  'value' => $caption,
121  'tag_label' => 'span',
122  'tag_value' => GFFormsModel::is_html5_enabled() ? 'figcaption' : 'div',
123  ),
124  'description' => array(
125  'label' => esc_attr_x( 'Description:', 'Post Image field description heading', 'gk-gravityview'),
126  'value' => $description,
127  'tag_label' => 'span',
128  'tag_value' => 'div'
129  ),
130 ));
131 
132 // If HTML5 output is enabled, support the `figure` and `figcaption` tags
133 $wrappertag = GFFormsModel::is_html5_enabled() ? 'figure' : 'div';
134 
135 /**
136  * @filter `gravityview_post_image_meta_show_labels` Whether to show labels for the image meta.
137  * @see https://gravityview.co/support/documentation/201606759 Read more about the filter
138  * @var boolean $showlabels True: Show labels; False: hide labels
139  */
140 $showlabels = apply_filters( 'gravityview_post_image_meta_show_labels', true );
141 
142 // Wrapper tag
143 $output = '<'.$wrappertag.' class="gv-image">';
144 
145 // Image with link tag
147 
148 foreach ( (array)$image_meta as $key => $meta ) {
149 
150  if ( ! empty( $meta['value'] ) ) {
151 
152  $output .= '<div class="gv-image-'.esc_attr( $key ).'">';
153 
154  // Display the label if the label's not empty
155  if ( ! empty( $showlabels ) && !empty( $meta['label'] ) ) {
156  $output .= '<'.esc_attr( $meta['tag_label'] ).' class="gv-image-label">';
157  $output .= esc_html( $meta['label'] );
158  $output .= '</'.esc_attr( $meta['tag_label'] ).'> ';
159  }
160 
161  // Display the value
162  $output .= '<'.esc_attr( $meta['tag_value'] ).' class="gv-image-value">';
163  $output .= esc_html( $meta['value'] );
164  $output .= '</'.esc_attr( $meta['tag_value'] ).'>';
165 
166  $output .= '</div>';
167  }
168 
169 }
170 
171 $output .= '</'.$wrappertag.'>';
172 
173 echo $output;
$image_atts
Generic class for generating image tag.
$url
$description
$value
$entry
$title
if(! isset( $gravityview)||empty( $gravityview->template)) $field
The default post_image field output template.
gravityview_get_link( $href='', $anchor_text='', $atts=array())
Generate an HTML anchor tag with a list of supported attributes.
$field_settings
$caption
$image_meta
$wrappertag
$ary
$output
gravityview()
The main GravityView wrapper function.
gv_entry_link( $entry, $post_id=NULL)
Definition: class-api.php:938
$showlabels
$link_atts
$image