| Conditions | 21 |
| Paths | 2690 |
| Total Lines | 206 |
| Code Lines | 66 |
| Lines | 56 |
| Ratio | 27.18 % |
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 |
||
| 50 | static function get_files_array( $value, $gv_class ) { |
||
| 51 | |||
| 52 | $gravityview_view = GravityView_View::getInstance(); |
||
| 53 | |||
| 54 | extract( $gravityview_view->getCurrentField() ); |
||
| 55 | |||
| 56 | $output_arr = array(); |
||
| 57 | |||
| 58 | // Get an array of file paths for the field. |
||
| 59 | $file_paths = rgar( $field , 'multipleFiles' ) ? json_decode( $value ) : array( $value ); |
||
| 60 | |||
| 61 | // Process each file path |
||
| 62 | foreach( $file_paths as $file_path ) { |
||
| 63 | |||
| 64 | // If the site is HTTPS, use HTTPS |
||
| 65 | if(function_exists('set_url_scheme')) { $file_path = set_url_scheme($file_path); } |
||
| 66 | |||
| 67 | // This is from Gravity Forms's code |
||
| 68 | $file_path = esc_attr(str_replace(" ", "%20", $file_path)); |
||
| 69 | |||
| 70 | // If the field is set to link to the single entry, link to it. |
||
| 71 | $link = !empty( $field_settings['show_as_link'] ) ? GravityView_API::entry_link( $entry, $field ) : $file_path; |
||
| 72 | |||
| 73 | // Get file path information |
||
| 74 | $file_path_info = pathinfo($file_path); |
||
| 75 | |||
| 76 | $html_format = NULL; |
||
| 77 | |||
| 78 | $disable_lightbox = false; |
||
| 79 | |||
| 80 | $disable_wrapped_link = false; |
||
| 81 | |||
| 82 | // Is this an image? |
||
| 83 | $image = new GravityView_Image(array( |
||
| 84 | 'src' => $file_path, |
||
| 85 | 'class' => 'gv-image gv-field-id-'.$field_settings['id'], |
||
| 86 | 'alt' => $field_settings['label'], |
||
| 87 | 'width' => (gravityview_get_context() === 'single' ? NULL : 250) |
||
| 88 | )); |
||
| 89 | |||
| 90 | $content = $image->html(); |
||
| 91 | |||
| 92 | // The new default content is the image, if it exists. If not, use the file name as the content. |
||
| 93 | $content = !empty( $content ) ? $content : $file_path_info['basename']; |
||
| 94 | |||
| 95 | // If pathinfo() gave us the extension of the file, run the switch statement using that. |
||
| 96 | $extension = empty( $file_path_info['extension'] ) ? NULL : strtolower( $file_path_info['extension'] ); |
||
| 97 | |||
| 98 | |||
| 99 | switch( true ) { |
||
| 100 | |||
| 101 | // Audio file |
||
| 102 | View Code Duplication | case in_array( $extension, wp_get_audio_extensions() ): |
|
| 103 | |||
| 104 | $disable_lightbox = true; |
||
| 105 | |||
| 106 | if( shortcode_exists( 'audio' ) ) { |
||
| 107 | |||
| 108 | $disable_wrapped_link = true; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @filter `gravityview_audio_settings` Modify the settings passed to the `wp_video_shortcode()` function |
||
| 112 | * @since 1.2 |
||
| 113 | * @param array $audio_settings Array with `src` and `class` keys |
||
| 114 | */ |
||
| 115 | $audio_settings = apply_filters( 'gravityview_audio_settings', array( |
||
| 116 | 'src' => $file_path, |
||
| 117 | 'class' => 'wp-audio-shortcode gv-audio gv-field-id-'.$field_settings['id'] |
||
| 118 | )); |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Generate the audio shortcode |
||
| 122 | * @see http://codex.wordpress.org/Audio_Shortcode |
||
| 123 | * @see https://developer.wordpress.org/reference/functions/wp_audio_shortcode/ |
||
| 124 | */ |
||
| 125 | $content = wp_audio_shortcode( $audio_settings ); |
||
| 126 | |||
| 127 | } |
||
| 128 | |||
| 129 | break; |
||
| 130 | |||
| 131 | // Video file |
||
| 132 | View Code Duplication | case in_array( $extension, wp_get_video_extensions() ): |
|
| 133 | |||
| 134 | $disable_lightbox = true; |
||
| 135 | |||
| 136 | if( shortcode_exists( 'video' ) ) { |
||
| 137 | |||
| 138 | $disable_wrapped_link = true; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @filter `gravityview_video_settings` Modify the settings passed to the `wp_video_shortcode()` function |
||
| 142 | * @since 1.2 |
||
| 143 | * @param array $video_settings Array with `src` and `class` keys |
||
| 144 | */ |
||
| 145 | $video_settings = apply_filters( 'gravityview_video_settings', array( |
||
| 146 | 'src' => $file_path, |
||
| 147 | 'class' => 'wp-video-shortcode gv-video gv-field-id-'.$field_settings['id'] |
||
| 148 | )); |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Generate the video shortcode |
||
| 152 | * @see http://codex.wordpress.org/Video_Shortcode |
||
| 153 | * @see https://developer.wordpress.org/reference/functions/wp_video_shortcode/ |
||
| 154 | */ |
||
| 155 | $content = wp_video_shortcode( $video_settings ); |
||
| 156 | |||
| 157 | } |
||
| 158 | |||
| 159 | break; |
||
| 160 | |||
| 161 | |||
| 162 | case $extension === 'pdf': |
||
| 163 | |||
| 164 | // PDF needs to be displayed in an IFRAME |
||
| 165 | $link = add_query_arg( array( 'TB_iframe' => 'true' ), $link ); |
||
| 166 | |||
| 167 | break; |
||
| 168 | |||
| 169 | // if not image, do not set the lightbox (@since 1.5.3) |
||
| 170 | case !in_array( $extension, array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ) ): |
||
| 171 | |||
| 172 | $disable_lightbox = true; |
||
| 173 | |||
| 174 | break; |
||
| 175 | |||
| 176 | } |
||
| 177 | |||
| 178 | // If using Link to File, override the content. |
||
| 179 | // (We do this here so that the $disable_lightbox can be set. Yes, there's a little more processing time, but oh well.) |
||
| 180 | if( !empty( $field_settings['link_to_file'] ) ) { |
||
| 181 | |||
| 182 | // Force the content to be the file name |
||
| 183 | $content = $file_path_info["basename"]; |
||
| 184 | |||
| 185 | // Restore the wrapped link |
||
| 186 | $disable_wrapped_link = false; |
||
| 187 | |||
| 188 | } |
||
| 189 | |||
| 190 | // Whether to use lightbox or not |
||
| 191 | if( $disable_lightbox || empty( $gravityview_view->atts['lightbox'] ) || !empty( $field_settings['show_as_link'] ) ) { |
||
| 192 | |||
| 193 | $link_atts = empty( $field_settings['show_as_link'] ) ? array( 'target' => '_blank' ) : array(); |
||
| 194 | |||
| 195 | } else { |
||
| 196 | |||
| 197 | $link_atts = array( |
||
| 198 | 'rel' => sprintf( "%s-%s", $gv_class, $entry['id'] ), |
||
| 199 | 'target' => '_blank', |
||
| 200 | 'class' => 'thickbox', |
||
| 201 | ); |
||
| 202 | |||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @filter `gravityview/fields/fileupload/link_atts` Modify the link attributes for a file upload field |
||
| 207 | * @param array|string $link_atts Array or attributes string |
||
| 208 | * @param array $field Current GravityView field array |
||
| 209 | */ |
||
| 210 | $link_atts = apply_filters( 'gravityview/fields/fileupload/link_atts', $link_atts, $gravityview_view->getCurrentField() ); |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @filter `gravityview/fields/fileupload/disable_link` Filter to alter the default behaviour of wrapping images (or image names) with a link to the content object |
||
| 214 | * @since 1.5.1 |
||
| 215 | * @param bool $disable_wrapped_link whether to wrap the content with a link to the content object. |
||
| 216 | * @param array $gravityview_view->field_data |
||
| 217 | * @see GravityView_API:field_value() for info about $gravityview_view->field_data |
||
| 218 | */ |
||
| 219 | $disable_wrapped_link = apply_filters( 'gravityview/fields/fileupload/disable_link', $disable_wrapped_link, $gravityview_view->getCurrentField() ); |
||
| 220 | |||
| 221 | // If the HTML output hasn't been overridden by the switch statement above, use the default format |
||
| 222 | if( !empty( $content ) && empty( $disable_wrapped_link ) ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Modify the link text (defaults to the file name) |
||
| 226 | * |
||
| 227 | * @since 1.7 |
||
| 228 | * |
||
| 229 | * @param string $content The existing anchor content. Could be `<img>` tag, audio/video embed or the file name |
||
| 230 | * @param array $field GravityView array of the current field being processed |
||
| 231 | */ |
||
| 232 | $content = apply_filters( 'gravityview/fields/fileupload/link_content', $content, $gravityview_view->getCurrentField() ); |
||
| 233 | |||
| 234 | $content = gravityview_get_link( $link, $content, $link_atts ); |
||
| 235 | } |
||
| 236 | |||
| 237 | $output_arr[] = array( |
||
| 238 | 'file_path' => $file_path, |
||
| 239 | 'content' => $content |
||
| 240 | ); |
||
| 241 | |||
| 242 | } // End foreach loop |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @filter `gravityview/fields/fileupload/files_array` Modify the files array |
||
| 246 | * @since 1.7 |
||
| 247 | * @param array $output_arr Associative array of files \n |
||
| 248 | * @type string $file_path The path to the file as stored in Gravity Forms \n |
||
| 249 | * @type string $content The generated output for the file \n |
||
| 250 | * @param array $field GravityView array of the current field being processed |
||
| 251 | */ |
||
| 252 | $output_arr = apply_filters( 'gravityview/fields/fileupload/files_array', $output_arr, $gravityview_view->getCurrentField() ); |
||
| 253 | |||
| 254 | return $output_arr; |
||
| 255 | } |
||
| 256 | |||
| 260 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.