|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
namespace GV; |
|
3
|
|
|
|
|
4
|
|
|
/** If this file is called directly, abort. */ |
|
5
|
|
|
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
|
6
|
|
|
die(); |
|
7
|
|
|
} |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* The \GV\Field_Renderer class. |
|
11
|
|
|
* |
|
12
|
|
|
* Houses some preliminary \GV\Field rendering functionality. |
|
13
|
|
|
*/ |
|
14
|
|
|
class Field_Renderer extends Renderer { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Renders a \GV\Field instance. |
|
18
|
|
|
* |
|
19
|
|
|
* @param \GV\Field $request The field. |
|
20
|
|
|
* @param \GV\View $view The view for this context if applicable. |
|
21
|
|
|
* @param \GV\Source $source The source (form) for this context if applicable. |
|
22
|
|
|
* @param \GV\Entry $entry The entry for this context if applicable. |
|
23
|
|
|
* @param \GV\Request $request The request for this context if applicable. |
|
24
|
|
|
* @param string $class The field template class. Default: \GV\Field_HTML_Template'. |
|
25
|
|
|
* |
|
26
|
|
|
* @api |
|
27
|
|
|
* @since 2.0 |
|
28
|
|
|
* @since 2.1 Added Field Template class $class parameter |
|
29
|
|
|
* |
|
30
|
|
|
* @return string The rendered Field |
|
31
|
|
|
*/ |
|
32
|
58 |
|
public function render( Field $field, View $view = null, Source $source = null, Entry $entry = null, Request $request = null, $class = '\GV\Field_HTML_Template' ) { |
|
33
|
58 |
|
if ( is_null( $request ) ) { |
|
34
|
|
|
$request = &gravityview()->request; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
58 |
|
if ( ! in_array( get_class( $request ), array( 'GV\Frontend_Request', 'GV\Mock_Request', 'GV\REST\Request' ) ) ) { |
|
38
|
|
|
gravityview()->log->error( 'Renderer unable to render View in {request_class} context', array( 'request_class' => get_class( $request ) ) ); |
|
39
|
|
|
return null; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @filter `gravityview/template/field/class` Filter the template class that is about to be used to render the view. |
|
44
|
|
|
* @since 2.0 |
|
45
|
|
|
* @param string $class The chosen class - Default: \GV\Field_HTML_Template. |
|
46
|
|
|
* @param \GV\Field $field The field about to be rendered. |
|
47
|
|
|
* @param \GV\View $view The view in this context, if applicable. |
|
48
|
|
|
* @param \GV\Source $source The source (form) in this context, if applicable. |
|
49
|
|
|
* @param \GV\Entry $entry The entry in this context, if applicable. |
|
50
|
|
|
* @param \GV\Request $request The request in this context, if applicable. |
|
51
|
|
|
*/ |
|
52
|
58 |
|
$class = apply_filters( 'gravityview/template/field/class', $class, $field, $view, $source, $entry, $request ); |
|
53
|
58 |
|
if ( ! $class || ! class_exists( $class ) ) { |
|
54
|
|
|
gravityview()->log->error( '{template_class} not found', array( 'template_class' => $class ) ); |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
58 |
|
$renderer = new $class( $field, $view, $source, $entry, $request ); |
|
59
|
58 |
|
ob_start(); |
|
60
|
58 |
|
$renderer->render(); |
|
61
|
58 |
|
return ob_get_clean(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
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.