| Conditions | 10 |
| Paths | 40 |
| Total Lines | 201 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 214 | public function render() { |
||
| 215 | |||
| 216 | /** Retrieve the value. */ |
||
| 217 | $display_value = $value = $this->field->get_value( $this->view, $this->source, $this->entry ); |
||
| 218 | |||
| 219 | $source = $this->source; |
||
| 220 | $source_backend = $source ? $source::$backend : null; |
||
| 221 | |||
| 222 | /** Alter the display value according to Gravity Forms. */ |
||
| 223 | if ( $source_backend == \GV\Source::BACKEND_GRAVITYFORMS ) { |
||
| 224 | /** Prevent any PHP warnings that may be generated. */ |
||
| 225 | ob_start(); |
||
| 226 | |||
| 227 | $display_value = \GFCommon::get_lead_field_display( $this->field->field, $value, $this->entry['currency'], false, 'html' ); |
||
| 228 | |||
| 229 | if ( $errors = ob_get_clean() ) { |
||
| 230 | gravityview()->log->error( 'Errors when calling GFCommon::get_lead_field_display()', array( 'data' => $errors ) ); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** Call the Gravity Forms field value filter. */ |
||
| 234 | $display_value = apply_filters( 'gform_entry_field_value', $display_value, $this->field->field, $this->entry->as_entry(), $this->source->form ); |
||
| 235 | |||
| 236 | /** Replace merge tags for admin-only fields. */ |
||
| 237 | if ( ! empty( $this->field->field->adminOnly ) ) { |
||
| 238 | $display_value = \GravityView_API::replace_variables( $display_value, $this->form->form, $this->entry->as_entry() ); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | $context = Template_Context::from_template( $this, compact( 'display_value', 'value' ) ); |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Make various pieces of data available to the template |
||
| 246 | * under the $gravityview scoped variable. |
||
| 247 | * |
||
| 248 | * @filter `gravityview/template/field/context` |
||
| 249 | * @param \GV\Template_Context $context The context for this template. |
||
| 250 | * @since 2.0 |
||
| 251 | */ |
||
| 252 | $this->push_template_data( apply_filters( 'gravityview/template/field/context', $context ), 'gravityview' ); |
||
| 253 | |||
| 254 | /** Bake the template. */ |
||
| 255 | ob_start(); |
||
| 256 | $this->located_template = $this->get_template_part( static::$slug ); |
||
| 257 | $output = ob_get_clean(); |
||
| 258 | |||
| 259 | if ( empty( $output ) ) { |
||
| 260 | /** |
||
| 261 | * @filter `gravityview_empty_value` What to display when a field is empty |
||
| 262 | * @deprecated Use the `gravityview/field/value/empty` filter instead |
||
| 263 | * @param string $value (empty string) |
||
| 264 | */ |
||
| 265 | $output = apply_filters( 'gravityview_empty_value', $output ); |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @filter `gravityview/field/value/empty` What to display when this field is empty. |
||
| 269 | * @param string $value The value to display (Default: empty string) |
||
| 270 | * @param \GV\Template_Context The template context this is being called from. |
||
| 271 | */ |
||
| 272 | $output = apply_filters( 'gravityview/field/value/empty', $output, Template_Context::from_template( $this ) ); |
||
| 273 | |||
| 274 | $context = Template_Context::from_template( $this, compact( 'display_value', 'value' ) ); |
||
| 275 | } |
||
| 276 | |||
| 277 | gravityview()->log->info( 'Field template for field #{field_id} loaded: {located_template}', array( 'field_id' => $this->field->ID, 'located_template' => $this->located_template ) ); |
||
| 278 | |||
| 279 | $this->pop_template_data( 'gravityview' ); |
||
| 280 | |||
| 281 | /** A compatibility array that's required by some of the deprecated filters. */ |
||
| 282 | $field_compat = array( |
||
| 283 | 'form' => $source_backend == \GV\Source::BACKEND_GRAVITYFORMS ? $this->source->form : null, |
||
| 284 | 'field_id' => $this->field->ID, |
||
| 285 | 'field' => $this->field->field, |
||
| 286 | 'field_settings' => $this->field->as_configuration(), |
||
| 287 | 'value' => $value, |
||
| 288 | 'display_value' => $display_value, |
||
| 289 | 'format' => 'html', |
||
| 290 | 'entry' => $this->entry->as_entry(), |
||
| 291 | 'field_type' => $this->field->type, |
||
| 292 | 'field_path' => $this->located_template, |
||
| 293 | ); |
||
| 294 | |||
| 295 | $pre_link_compat_callback = function( $output, $context ) use ( $field_compat ) { |
||
| 296 | $field = $context->field; |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @filter `gravityview_field_entry_value_{$field_type}_pre_link` Modify the field value output for a field type before Show As Link setting is applied. Example: `gravityview_field_entry_value_number_pre_link` |
||
| 300 | * @since 1.16 |
||
| 301 | * @param string $output HTML value output |
||
| 302 | * @param array $entry The GF entry array |
||
| 303 | * @param array $field_settings Settings for the particular GV field |
||
| 304 | * @param array $field Field array, as fetched from GravityView_View::getCurrentField() |
||
| 305 | * |
||
| 306 | * @deprecated Use the `gravityview/field/{$field_type}/output` or `gravityview/field/output` filters instead. |
||
| 307 | */ |
||
| 308 | $output = apply_filters( "gravityview_field_entry_value_{$field->type}_pre_link", $output, $context->entry->as_entry(), $field->as_configuration(), $field_compat ); |
||
| 309 | |||
| 310 | $output = apply_filters( 'gravityview_field_entry_value_pre_link', $output, $context->entry->as_entry(), $field->as_configuration(), $field_compat ); |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Link to the single entry by wrapping the output in an anchor tag |
||
| 314 | * |
||
| 315 | * Fields can override this by modifying the field data variable inside the field. See /templates/fields/post_image.php for an example. |
||
| 316 | */ |
||
| 317 | if ( ! empty( $field->show_as_link ) && ! \gv_empty( $output, false, false ) ) { |
||
| 318 | $link_atts = empty( $field->new_window ) ? array() : array( 'target' => '_blank' ); |
||
| 319 | |||
| 320 | $permalink = $context->entry->get_permalink( $context->view, $context->request ); |
||
| 321 | $output = \gravityview_get_link( $permalink, $output, $link_atts ); |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @filter `gravityview_field_entry_link` Modify the link HTML |
||
| 325 | * @param string $link HTML output of the link |
||
| 326 | * @param string $href URL of the link |
||
| 327 | * @param array $entry The GF entry array |
||
| 328 | * @param array $field_settings Settings for the particular GV field |
||
| 329 | * @deprecated Use `gravityview/template/field/entry_link` |
||
| 330 | */ |
||
| 331 | $output = apply_filters( 'gravityview_field_entry_link', $output, $permalink, $context->entry->as_entry(), $field->as_configuration() ); |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @filter `gravityview/template/field/entry_link` Modify the link HTML |
||
| 335 | * @since 2.0 |
||
| 336 | * @param string $link HTML output of the link |
||
| 337 | * @param string $href URL of the link |
||
| 338 | * @param \GV\Template_Context $context The context |
||
| 339 | */ |
||
| 340 | $output = apply_filters( 'gravityview/template/field/entry_link', $output, $permalink, $context ); |
||
| 341 | } |
||
| 342 | |||
| 343 | return $output; |
||
| 344 | }; |
||
| 345 | |||
| 346 | $post_link_compat_callback = function( $output, $context ) use ( $field_compat ) { |
||
| 347 | $field = $context->field; |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @filter `gravityview_field_entry_value_{$field_type}` Modify the field value output for a field type. Example: `gravityview_field_entry_value_number` |
||
| 351 | * @since 1.6 |
||
| 352 | * @param string $output HTML value output |
||
| 353 | * @param array $entry The GF entry array |
||
| 354 | * @param array $field_settings Settings for the particular GV field |
||
| 355 | * @param array $field Current field being displayed |
||
| 356 | * |
||
| 357 | * @deprecated Use the `gravityview/field/{$field_type}/output` or `gravityview/field/output` filters instead. |
||
| 358 | */ |
||
| 359 | $output = apply_filters( "gravityview_field_entry_value_{$field->type}", $output, $context->entry->as_entry(), $field->as_configuration(), $field_compat ); |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @filter `gravityview_field_entry_value` Modify the field value output for all field types |
||
| 363 | * @param string $output HTML value output |
||
| 364 | * @param array $entry The GF entry array |
||
| 365 | * @param array $field_settings Settings for the particular GV field |
||
| 366 | * @param array $field_data {@since 1.6} |
||
| 367 | * |
||
| 368 | * @deprecated Use the `gravityview/field/{$field_type}/output` or `gravityview/field/output` filters instead. |
||
| 369 | */ |
||
| 370 | $output = apply_filters( 'gravityview_field_entry_value', $output, $context->entry->as_entry(), $field->as_configuration(), $field_compat ); |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @filter `gravityview/template/field/{$field_type}/output` Modify the field output for a field type. |
||
| 374 | * |
||
| 375 | * @since 2.0 |
||
| 376 | * |
||
| 377 | * @param string $output The current output. |
||
| 378 | * @param \GV\Template_Context The template context this is being called from. |
||
| 379 | */ |
||
| 380 | return apply_filters( "gravityview/template/field/{$field->type}/output", $output, $context ); |
||
| 381 | }; |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Okay, what's this whole pre/post_link compat deal, huh? |
||
| 385 | * |
||
| 386 | * Well, the `gravityview_field_entry_value_{$field_type}_pre_link` filter |
||
| 387 | * is expected to be applied before the value is turned into an entry link. |
||
| 388 | * |
||
| 389 | * And then `gravityview_field_entry_value_{$field_type}` and `gravityview_field_entry_value` |
||
| 390 | * are called afterwards. |
||
| 391 | * |
||
| 392 | * So we're going to use filter priorities to make sure this happens inline with |
||
| 393 | * our new filters, in the correct sequence. Pre-link called with priority 5 and |
||
| 394 | * post-link called with priority 9. Then everything else. |
||
| 395 | * |
||
| 396 | * If a new code wants to alter the value before it is hyperlinked (hyperlinkified?), |
||
| 397 | * it should hook into a priority between -inf. and 8. Afterwards: 10 to +inf. |
||
| 398 | */ |
||
| 399 | add_filter( 'gravityview/template/field/output', $pre_link_compat_callback, 5, 2 ); |
||
| 400 | add_filter( 'gravityview/template/field/output', $post_link_compat_callback, 9, 2 ); |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @filter `gravityview/template/field/output` Modify the field output for a field. |
||
| 404 | * |
||
| 405 | * @since 2.0 |
||
| 406 | * |
||
| 407 | * @param string $output The current output. |
||
| 408 | * @param \GV\Template_Context The template this is being called from. |
||
| 409 | */ |
||
| 410 | echo apply_filters( "gravityview/template/field/output", $output, $context ); |
||
| 411 | |||
| 412 | remove_filter( 'gravityview/template/field/output', $pre_link_compat_callback, 5 ); |
||
| 413 | remove_filter( 'gravityview/template/field/output', $post_link_compat_callback, 9 ); |
||
| 414 | } |
||
| 415 | } |
||
| 419 |
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.