Conditions | 4 |
Paths | 6 |
Total Lines | 55 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
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.