Completed
Push — master ( 9cf7be...029c15 )
by Zack
04:07
created

GravityView_Field_Post_Image   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 134
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A field_options() 0 15 2
A explode_value() 0 21 3
A get_field_input() 0 55 4
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 11 and the first side effect is on line 146.

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.

Loading history...
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';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
14
15
	var $_gf_field_class_name = 'GF_Field_Post_Image';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_gf_field_class_name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
16
17
	var $group = 'post';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $group.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
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 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
26
		unset ( $field_options['search_filter'] );
27
28
		if( 'edit' === $context ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
29
			return $field_options;
30
		}
31
32
		$this->add_field_support('link_to_post', $field_options );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
33
34
		// @since 1.5.4
35
		$this->add_field_support('dynamic_data', $field_options );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
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 ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $entry is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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'] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
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 ] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
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 );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
139
		}
140
141
		return $gf_post_image_field_output;
142
	}
143
144
}
145
146
new GravityView_Field_Post_Image;
147