Completed
Push — develop ( 0bccf3...c7374d )
by Gennady
23:52 queued 17:40
created

View_List_Template::body_after()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 16
ccs 4
cts 4
cp 1
crap 1
rs 9.7333
c 0
b 0
f 0
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 14 and the first side effect is on line 6.

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
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * The View List Template class .
11
 *
12
 * Renders a \GV\View and a \GV\Entry_Collection via a \GV\View_Renderer.
13
 */
14
class View_List_Template extends View_Template {
15
	/**
16
	 * @var string The template slug to be loaded (like "table", "list")
17
	 */
18
	public static $slug = 'list';
19
20
	/**
21
	 * Output the field in the list view.
22
	 *
23
	 * @param \GV\Field $field The field to output.
24
	 * @param \GV\Entry $entry The entry.
25
	 * @param array $extras Extra stuff, like wpautop, etc.
26
	 *
27
	 * @return string
28
	 */
29 2
	public function the_field( \GV\Field $field, \GV\Entry $entry, $extras = null ) {
30 2
		$form = $this->view->form;
31
32 2
		if ( $entry->is_multi() ) {
33
			if ( ! $single_entry = $entry->from_field( $field ) ) {
34
				return;
35
			}
36
			$form = GF_Form::by_id( $field->form_id );
0 ignored issues
show
Documentation introduced by
The property form_id does not exist on object<GV\Field>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
37
		}
38
39
		/**
40
		 * Push legacy entry context.
41
		 */
42 2
		\GV\Mocks\Legacy_Context::load( array(
43 2
			'entry' => $entry,
44 2
			'form' => $form,
45
		) );
46
47 2
		$context = Template_Context::from_template( $this, compact( 'field', 'entry' ) );
48
49 2
		$renderer = new Field_Renderer();
50 2
		$source = is_numeric( $field->ID ) ? $form : new Internal_Source();
51
52 2
		$value = $renderer->render( $field, $this->view, $source, $entry, $this->request );
53
54
		/**
55
		 * @deprecated Here for back-compatibility.
56
		 */
57 2
		$label = apply_filters( 'gravityview_render_after_label', $field->get_label( $this->view, $form ), $field->as_configuration() );
58 2
		$label = apply_filters( 'gravityview/template/field_label', $label, $field->as_configuration(), $form->form ? $form->form : null, null );
0 ignored issues
show
Documentation introduced by
The property $form is declared private in GV\Form. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
59
60
		/**
61
		 * @filter `gravityview/template/field/label` Override the field label.
62
		 * @since 2.0
63
		 * @param[in,out] string $label The label to override.
64
		 * @param \GV\Template_Context $context The context.
65
		 */
66 2
		$label = apply_filters( 'gravityview/template/field/label', $label, $context );
67
68
		/**
69
		 * @filter `gravityview/template/table/entry/hide_empty`
70
		 * @param boolean Should the row be hidden if the value is empty? Default: don't hide.
71
		 * @param \GV\Template_Context $context The context ;) Love it, cherish it. And don't you dare modify it!
72
		 */
73 2
		$hide_empty = apply_filters( 'gravityview/render/hide-empty-zone', Utils::get( $extras, 'hide_empty', $this->view->settings->get( 'hide_empty', false ) ), $context );
74
75 2
		if ( is_numeric( $field->ID ) ) {
76 2
			$extras['field'] = $field->as_configuration();
77
		}
78
79 2
		$extras['entry'] = $entry->as_entry();
80 2
		$extras['hide_empty'] = $hide_empty;
81 2
		$extras['label'] = $label;
82 2
		$extras['value'] = $value;
83
84 2
		return \gravityview_field_output( $extras, $context );
85
	}
86
87
	/**
88
	 * Return an array of variables ready to be extracted.
89
	 *
90
	 * @param string|array $zones The field zones to grab.
91
	 *
92
	 * @return array An array ready to be extract()ed in the form of
93
	 *  $zone => \GV\Field_Collection
94
	 *  has_$zone => int
95
	 */
96 2
	public function extract_zone_vars( $zones ) {
97 2
		if ( ! is_array( $zones ) ) {
98
			$zones = array( $zones );
99
		}
100
101 2
		$vars = array();
102 2
		foreach ( $zones as $zone ) {
103 2
			$zone_var = str_replace( '-', '_', $zone );
104 2
			$vars[ $zone_var ] = $this->view->fields->by_position( 'directory_list-' . $zone )->by_visible();
105 2
			$vars[ "has_$zone_var" ] = $vars[ $zone_var ]->count();
106
		}
107
108 2
		return $vars;
109
	}
110
111
	/**
112
	 * `gravityview_entry_class` and `gravityview/template/list/entry/class` filters.
113
	 *
114
	 * Modify of the class of a row.
115
	 *
116
	 * @param string $class The class.
117
	 * @param \GV\Entry $entry The entry.
118
	 * @param \GV\Template_Context The context.
119
	 *
120
	 * @return string The classes.
121
	 */
122 2
	public static function entry_class( $class, $entry, $context ) {
123
		/**
124
		 * @filter `gravityview_entry_class` Modify the class applied to the entry row.
125
		 * @param string $class Existing class.
126
		 * @param array $entry Current entry being displayed
127
		 * @param \GravityView_View $this Current GravityView_View object
128
		 * @deprecated Use `gravityview/template/list/entry/class`
129
		 * @return string The modified class.
130
		 */
131 2
		$class = apply_filters( 'gravityview_entry_class', $class, $entry->as_entry(), \GravityView_View::getInstance() );
132
133
		/**
134
		 * @filter `gravityview/template/list/entry/class` Modify the class aplied to the entry row.
135
		 * @param string $class The existing class.
136
		 * @param \GV\Template_Context The context.
137
		 * @return string The modified class.
138
		 */
139 2
		return apply_filters( 'gravityview/template/list/entry/class', $class, Template_Context::from_template( $context->template, compact( 'entry' ) ) );
140
	}
141
142
	/**
143
	 * `gravityview_list_body_before` and `gravityview/template/list/body/before` actions.
144
	 *
145
	 * Output inside the `tbody` of the list.
146
	 *
147
	 * @param $context \GV\Template_Context The 2.0 context.
148
	 *
149
	 * @return void
150
	 */
151 2
	public static function body_before( $context ) {
152
		/**
153
		 * @action `gravityview/template/list/body/before` Output inside the `tbody` of the list.
154
		 * @since 2.0
155
		 * @param \GV\Template_Context $context The template context.
156
		 */
157 2
		do_action( 'gravityview/template/list/body/before', $context );
158
159
		/**
160
		* @action `gravityview_list_body_before` Inside the `tbody`, before any rows are rendered. Can be used to insert additional rows.
161
		* @deprecated Use `gravityview/template/list/body/before`
162
		* @since 1.0.7
163
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
164
		*/
165 2
		do_action( 'gravityview_list_body_before', \GravityView_View::getInstance() /** ugh! */ );
166 2
	}
167
168
	/**
169
	 * `gravityview_list_body_after` and `gravityview/template/list/body/after` actions.
170
	 *
171
	 * Output inside the `tbody` of the list.
172
	 *
173
	 * @param $context \GV\Template_Context The 2.0 context.
174
	 *
175
	 * @return void
176
	 */
177 2
	public static function body_after( $context ) {
178
		/**
179
		 * @action `gravityview/template/list/body/after` Output inside the `tbody` of the list at the end.
180
		 * @since 2.0
181
		 * @param \GV\Template_Context $context The template context.
182
		 */
183 2
		do_action( 'gravityview/template/list/body/after', $context );
184
185
		/**
186
		* @action `gravityview_list_body_after` Inside the `tbody`, after any rows are rendered. Can be used to insert additional rows.
187
		* @deprecated Use `gravityview/template/list/body/after`
188
		* @since 1.0.7
189
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
190
		*/
191 2
		do_action( 'gravityview_list_body_after', \GravityView_View::getInstance() /** ugh! */ );
192 2
	}
193
194
	/**
195
	 * `gravityview_list_entry_before` and `gravityview/template/list/entry/before` actions.
196
	 * `gravityview_list_entry_title_before` and `gravityview/template/list/entry/title/before` actions.
197
	 * `gravityview_list_entry_content_before` and `gravityview/template/list/entry/content/before` actions.
198
	 * `gravityview_list_entry_footer_before` and `gravityview/template/list/entry/footer/before` actions.
199
	 *
200
	 * Output inside the `entry` of the list.
201
	 *
202
	 * @param \GV\Entry $entry The entry.
203
	 * @param \GV\Template_Context $context The 2.0 context.
204
	 * @param string $zone The list zone (footer, image, title, etc.).
205
	 *
206
	 * @return void
207
	 */
208 2
	public static function entry_before( $entry, $context, $zone = '' ) {
209 2
		$zone = str_replace( '//', '/', "/$zone/" );
210
211
		/**
212
		 * @action `gravityview/template/list/entry/$zone/before` Output inside the `entry` of the list at the end.
213
		 * @since 2.0
214
		 * @param \GV\Template_Context $context The template context.
215
		 */
216 2
		do_action( sprintf( 'gravityview/template/list/entry%sbefore', $zone ), Template_Context::from_template( $context->template, compact( 'entry' ) ) );
217
218 2
		$zone = str_replace( '/', '_', $zone );
219
220
		/**
221
		* @action `gravityview_list_entry_$zone_before` Inside the `entry`, before any rows are rendered. Can be used to insert additional rows.
222
		* @deprecated Use `gravityview/template/list/entry/$zone/before`
223
		* @since 1.0.7
224
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
225
		*/
226 2
		do_action( sprintf( 'gravityview_list_entry%sbefore', $zone ), $entry->as_entry(), \GravityView_View::getInstance() /** ugh! */ );
227 2
	}
228
229
	/**
230
	 * `gravityview_list_entry_after` and `gravityview/template/list/entry/after` actions.
231
	 * `gravityview_list_entry_title_after` and `gravityview/template/list/entry/title/after` actions.
232
	 * `gravityview_list_entry_content_after` and `gravityview/template/list/entry/content/after` actions.
233
	 * `gravityview_list_entry_footer_after` and `gravityview/template/list/entry/footer/after` actions.
234
	 *
235
	 * Output inside the `entry` of the list.
236
	 *
237
	 * @param \GV\Entry $entry The entry.
238
	 * @param \GV\Template_Context $context The 2.0 context.
239
	 * @param string $zone The list zone (footer, image, title, etc.).
240
	 *
241
	 * @return void
242
	 */
243 2
	public static function entry_after( $entry, $context, $zone = '' ) {
244 2
		$zone = str_replace( '//', '/', "/$zone/" );
245
246
		/**
247
		 * @action `gravityview/template/list/entry/$zone/after` Output inside the `entry` of the list at the end.
248
		 * @since 2.0
249
		 * @param \GV\Template_Context $context The template context.
250
		 */
251 2
		do_action( sprintf( 'gravityview/template/list/entry%safter', $zone ), Template_Context::from_template( $context->template, compact( 'entry' ) ) );
252
253 2
		$zone = str_replace( '/', '_', $zone );
254
255
		/**
256
		* @action `gravityview_list_entry_$zone_after` Inside the `entry`, after any rows are rendered. Can be used to insert additional rows.
257
		* @deprecated Use `gravityview/template/list/entry/after`
258
		* @since 1.0.7
259
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
260
		*/
261 2
		do_action( sprintf( 'gravityview_list_entry%safter', $zone ), $entry->as_entry(), \GravityView_View::getInstance() /** ugh! */ );
262 2
	}
263
}
264