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
|
|
|
* |
47
|
|
|
* @param string $value The stored value of an image, impoded with `|:|` values |
48
|
|
|
* |
49
|
|
|
* @return array with `url`, `title`, `caption` and `description` values |
50
|
|
|
*/ |
51
|
|
|
private function explode_value( $value ) { |
52
|
|
|
|
53
|
|
|
// Already is an array, perhaps? |
54
|
|
|
if ( ! is_string( $value ) ) { |
55
|
|
|
return $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$url = $title = $caption = $description = ''; |
59
|
|
|
|
60
|
|
|
// If there's a |:| match, process. Otherwise, empty array! |
61
|
|
|
if( preg_match( '/\|\:\|/', $value ) ) { |
|
|
|
|
62
|
|
|
list( $url, $title, $caption, $description ) = array_pad( explode( '|:|', $value ), 4, false ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return array( |
66
|
|
|
'url' => $url, |
67
|
|
|
'title' => $title, |
68
|
|
|
'caption' => $caption, |
69
|
|
|
'description' => $description, |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the field inner markup |
75
|
|
|
* |
76
|
|
|
* Overriding GF_Field_Post_Image is necessary because they don't check for existing post image values, because |
77
|
|
|
* GF only creates, not updates. |
78
|
|
|
* |
79
|
|
|
* @since 1.16.2 |
80
|
|
|
* |
81
|
|
|
* @param array $form The Form Object currently being processed. |
82
|
|
|
* @param string|array $value The field value. From default/dynamic population, $_POST, or a resumed incomplete submission. |
83
|
|
|
* @param null|array $entry Null or the Entry Object currently being edited. |
84
|
|
|
* @param GF_Field_Post_Image $field |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function get_field_input( $form, $value = '', $entry = null, GF_Field_Post_Image $field ) { |
|
|
|
|
89
|
|
|
|
90
|
|
|
$id = (int) $field->id; |
91
|
|
|
$form_id = $form['id']; |
92
|
|
|
$input_name = "input_{$id}"; |
93
|
|
|
$field_id = sprintf( 'input_%d_%d', $form_id, $id ); |
94
|
|
|
$img_name = null; |
95
|
|
|
|
96
|
|
|
// Convert |:| to associative array |
97
|
|
|
$img_array = $this->explode_value( $value ); |
98
|
|
|
|
99
|
|
|
if( ! empty( $img_array['url'] ) ) { |
|
|
|
|
100
|
|
|
|
101
|
|
|
$img_name = basename( $img_array['url'] ); |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set the $uploaded_files value so that the .ginput_preview renders, and the file upload is hidden |
105
|
|
|
* @see GF_Field_Post_Image::get_field_input See the `<span class='ginput_preview'>` code |
106
|
|
|
* @see GFFormsModel::get_temp_filename See the `rgget( $input_name, self::$uploaded_files[ $form_id ] );` code |
107
|
|
|
*/ |
108
|
|
|
if( empty( GFFormsModel::$uploaded_files[ $form_id ][ $input_name ] ) ) { |
|
|
|
|
109
|
|
|
GFFormsModel::$uploaded_files[ $form_id ][ $input_name ] = $img_name; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Tell Gravity Forms we're not in the Admin |
114
|
|
|
add_filter( 'gform_is_entry_detail', '__return_false' ); |
115
|
|
|
add_filter( 'gform_is_form_editor', '__return_false' ); |
116
|
|
|
|
117
|
|
|
$input_value = array( |
118
|
|
|
"{$id}.1" => rgar( $img_array, 'title' ), |
119
|
|
|
"{$id}.4" => rgar( $img_array, 'caption' ), |
120
|
|
|
"{$id}.7" => rgar( $img_array, 'description' ), |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
// Get the field HTML output |
124
|
|
|
$gf_post_image_field_output = $field->get_field_input( $form, $input_value ); |
125
|
|
|
|
126
|
|
|
// Clean up our own filters |
127
|
|
|
remove_filter( 'gform_is_entry_detail', '__return_false' ); |
128
|
|
|
remove_filter( 'gform_is_form_editor', '__return_false' ); |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Insert a hidden field into the output that is used to store the image URL |
132
|
|
|
* @var string $current_file We need to have a reference of whether same file is being updated, or user wants to remove the image. |
133
|
|
|
* @see \GravityView_Edit_Entry_Render::maybe_update_post_fields |
134
|
|
|
* @hack |
135
|
|
|
*/ |
136
|
|
|
if ( null !== $img_name ) { |
137
|
|
|
$current_file = sprintf( "<input name='%s' id='%s' type='hidden' value='%s' />", $input_name, $field_id, esc_url_raw( $img_array['url'] ) ); |
138
|
|
|
$gf_post_image_field_output = str_replace('<span class=\'ginput_preview\'>', '<span class=\'ginput_preview\'>'.$current_file, $gf_post_image_field_output ); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $gf_post_image_field_output; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
new GravityView_Field_Post_Image; |
147
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.