Completed
Push — master ( 6337a4...1e5684 )
by Zack
11s
created

fields/class-gravityview-field-post-image.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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