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

View::as_data()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 3
rs 9.8666
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 17 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 default GravityView View class.
11
 *
12
 * Houses all base View functionality.
13
 *
14
 * Can be accessed as an array for old compatibility's sake
15
 *  in line with the elements inside the \GravityView_View_Data::$views array.
16
 */
17
class View implements \ArrayAccess {
18
19
	/**
20
	 * @var \WP_Post The backing post instance.
21
	 */
22
	private $post;
23
24
	/**
25
	 * @var \GV\View_Settings The settings.
26
	 *
27
	 * @api
28
	 * @since 2.0
29
	 */
30
	public $settings;
31
32
	/**
33
	 * @var \GV\Widget_Collection The widets attached here.
34
	 *
35
	 * @api
36
	 * @since 2.0
37
	 */
38
	public $widgets;
39
40
	/**
41
	 * @var \GV\GF_Form|\GV\Form The backing form for this view.
42
	 *
43
	 * Contains the form that is sourced for entries in this view.
44
	 *
45
	 * @api
46
	 * @since 2.0
47
	 */
48
	public $form;
49
50
	/**
51
	 * @var \GV\Field_Collection The fields for this view.
52
	 *
53
	 * Contains all the fields that are attached to this view.
54
	 *
55
	 * @api
56
	 * @since 2.0
57
	 */
58
	public $fields;
59
60
	/**
61
	 * @var array
62
	 *
63
	 * Internal static cache for gets, and whatnot.
64
	 * This is not persistent, resets across requests.
65
66
	 * @internal
67
	 */
68
	private static $cache = array();
69
70
	/**
71
	 * @var \GV\Join[] The joins for all sources in this view.
72
	 *
73
	 * @api
74
	 * @since future
75
	 */
76
	public $joins = array();
77
78
	/**
79
	 * The constructor.
80
	 */
81 109
	public function __construct() {
82 109
		$this->settings = new View_Settings();
83 109
		$this->fields = new Field_Collection();
84 109
		$this->widgets = new Widget_Collection();
85 109
	}
86
87
	/**
88
	 * Register the gravityview WordPress Custom Post Type.
89
	 *
90
	 * @internal
91
	 * @return void
92
	 */
93
	public static function register_post_type() {
94
95
		/** Register only once */
96
		if ( post_type_exists( 'gravityview' ) ) {
97
			return;
98
		}
99
100
		/**
101
		 * @filter `gravityview_is_hierarchical` Make GravityView Views hierarchical by returning TRUE
102
		 * This will allow for Views to be nested with Parents and also allows for menu order to be set in the Page Attributes metabox
103
		 * @since 1.13
104
		 * @param boolean $is_hierarchical Default: false
105
		 */
106
		$is_hierarchical = (bool)apply_filters( 'gravityview_is_hierarchical', false );
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
107
108
		$supports = array( 'title', 'revisions' );
109
110
		if ( $is_hierarchical ) {
111
			$supports[] = 'page-attributes';
112
		}
113
114
		/**
115
		 * @filter  `gravityview_post_type_supports` Modify post type support values for `gravityview` post type
116
		 * @see add_post_type_support()
117
		 * @since 1.15.2
118
		 * @param array $supports Array of features associated with a functional area of the edit screen. Default: 'title', 'revisions'. If $is_hierarchical, also 'page-attributes'
119
		 * @param[in] boolean $is_hierarchical Do Views support parent/child relationships? See `gravityview_is_hierarchical` filter.
120
		 */
121
		$supports = apply_filters( 'gravityview_post_type_support', $supports, $is_hierarchical );
122
123
		/** Register Custom Post Type - gravityview */
124
		$labels = array(
125
			'name'                => _x( 'Views', 'Post Type General Name', 'gravityview' ),
126
			'singular_name'       => _x( 'View', 'Post Type Singular Name', 'gravityview' ),
127
			'menu_name'           => _x( 'Views', 'Menu name', 'gravityview' ),
128
			'parent_item_colon'   => __( 'Parent View:', 'gravityview' ),
129
			'all_items'           => __( 'All Views', 'gravityview' ),
130
			'view_item'           => _x( 'View', 'View Item', 'gravityview' ),
131
			'add_new_item'        => __( 'Add New View', 'gravityview' ),
132
			'add_new'             => __( 'New View', 'gravityview' ),
133
			'edit_item'           => __( 'Edit View', 'gravityview' ),
134
			'update_item'         => __( 'Update View', 'gravityview' ),
135
			'search_items'        => __( 'Search Views', 'gravityview' ),
136
			'not_found'           => \GravityView_Admin::no_views_text(),
137
			'not_found_in_trash'  => __( 'No Views found in Trash', 'gravityview' ),
138
			'filter_items_list'     => __( 'Filter Views list', 'gravityview' ),
139
			'items_list_navigation' => __( 'Views list navigation', 'gravityview' ),
140
			'items_list'            => __( 'Views list', 'gravityview' ),
141
			'view_items'            => __( 'See Views', 'gravityview' ),
142
			'attributes'            => __( 'View Attributes', 'gravityview' ),
143
		);
144
		$args = array(
145
			'label'               => __( 'view', 'gravityview' ),
146
			'description'         => __( 'Create views based on a Gravity Forms form', 'gravityview' ),
147
			'labels'              => $labels,
148
			'supports'            => $supports,
149
			'hierarchical'        => $is_hierarchical,
150
			/**
151
			 * @filter `gravityview_direct_access` Should Views be directly accessible, or only visible using the shortcode?
152
			 * @see https://codex.wordpress.org/Function_Reference/register_post_type#public
153
			 * @since 1.15.2
154
			 * @param[in,out] boolean `true`: allow Views to be accessible directly. `false`: Only allow Views to be embedded via shortcode. Default: `true`
155
			 * @param int $view_id The ID of the View currently being requested. `0` for general setting
156
			 */
157
			'public'              => apply_filters( 'gravityview_direct_access', gravityview()->plugin->is_compatible(), 0 ),
158
			'show_ui'             => gravityview()->plugin->is_compatible(),
159
			'show_in_menu'        => gravityview()->plugin->is_compatible(),
160
			'show_in_nav_menus'   => true,
161
			'show_in_admin_bar'   => true,
162
			'menu_position'       => 17,
163
			'menu_icon'           => '',
164
			'can_export'          => true,
165
			/**
166
			 * @filter `gravityview_has_archive` Enable Custom Post Type archive?
167
			 * @since 1.7.3
168
			 * @param boolean False: don't have frontend archive; True: yes, have archive. Default: false
169
			 */
170
			'has_archive'         => apply_filters( 'gravityview_has_archive', false ),
171
			'exclude_from_search' => true,
172
			'rewrite'             => array(
173
				/**
174
				 * @filter `gravityview_slug` Modify the url part for a View.
175
				 * @see https://docs.gravityview.co/article/62-changing-the-view-slug
176
				 * @param string $slug The slug shown in the URL
177
				 */
178
				'slug' => apply_filters( 'gravityview_slug', 'view' ),
179
180
				/**
181
				 * @filter `gravityview/post_type/with_front` Should the permalink structure
182
				 *  be prepended with the front base.
183
				 *  (example: if your permalink structure is /blog/, then your links will be: false->/view/, true->/blog/view/).
184
				 *  Defaults to true.
185
				 * @see https://codex.wordpress.org/Function_Reference/register_post_type
186
				 * @since 2.0
187
				 * @param bool $with_front
188
				 */
189
				'with_front' => apply_filters( 'gravityview/post_type/with_front', true ),
190
			),
191
			'capability_type'     => 'gravityview',
192
			'map_meta_cap'        => true,
193
		);
194
195
		register_post_type( 'gravityview', $args );
196
	}
197
198
	/**
199
	 * Add extra rewrite endpoints.
200
	 *
201
	 * @return void
202
	 */
203 1
	public static function add_rewrite_endpoint() {
204
		/**
205
		 * CSV.
206
		 */
207
		global $wp_rewrite;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
208
209
		$slug = apply_filters( 'gravityview_slug', 'view' );
210
		$rule = array( sprintf( '%s/([^/]+)/csv/?', $slug ), 'index.php?gravityview=$matches[1]&csv=1', 'top' );
211
212 1
		add_filter( 'query_vars', function( $query_vars ) { 
213 1
			$query_vars[] = 'csv';
214 1
			return $query_vars;
215
		} );
216
217
		if ( ! isset( $wp_rewrite->extra_rules_top[ $rule[0] ] ) ) {
218
			call_user_func_array( 'add_rewrite_rule', $rule );
219
		}
220
	}
221
222
	/**
223
	 * A renderer filter for the View post type content.
224
	 *
225
	 * @param string $content Should be empty, as we don't store anything there.
226
	 *
227
	 * @return string $content The view content as output by the renderers.
228
	 */
229 10
	public static function content( $content ) {
230 10
		$request = gravityview()->request;
231
232
		// Plugins may run through the content in the header. WP SEO does this for its OpenGraph functionality.
233 10
		if ( ! defined( 'DOING_GRAVITYVIEW_TESTS' ) ) {
234
			if ( ! did_action( 'loop_start' ) ) {
235
				gravityview()->log->debug( 'Not processing yet: loop_start hasn\'t run yet. Current action: {action}', array( 'action' => current_filter() ) );
236
				return $content;
237
			}
238
239
			//	We don't want this filter to run infinite loop on any post content fields
240
			remove_filter( 'the_content', array( __CLASS__, __METHOD__ ) );
241
		}
242
243
		/**
244
		 * This is not a View. Bail.
245
		 *
246
		 * Shortcodes and oEmbeds and whatnot will be handled
247
		 *  elsewhere.
248
		 */
249 10
		if ( ! $view = $request->is_view() ) {
250 5
			return $content;
251
		}
252
253
		/**
254
		 * Check permissions.
255
		 */
256 5
		while ( $error = $view->can_render( null, $request ) ) {
257 5
			if ( ! is_wp_error( $error ) )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
258 5
				break;
259
260 1
			switch ( str_replace( 'gravityview/', '', $error->get_error_code() ) ) {
261 1
				case 'post_password_required':
262 1
					return get_the_password_form( $view->ID );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
263 1
				case 'no_form_attached':
264
265
					gravityview()->log->error( 'View #{view_id} cannot render: {error_code} {error_message}', array( 'error_code' => $error->get_error_code(), 'error_message' => $error->get_error_message() ) );
266
267
					/**
268
					 * This View has no data source. There's nothing to show really.
269
					 * ...apart from a nice message if the user can do anything about it.
270
					 */
271
					if ( \GVCommon::has_cap( array( 'edit_gravityviews', 'edit_gravityview' ), $view->ID ) ) {
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
272
						return __( sprintf( 'This View is not configured properly. Start by <a href="%s">selecting a form</a>.', esc_url( get_edit_post_link( $view->ID, false ) ) ), 'gravityview' );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
273
					}
274
					break;
275 1
				case 'no_direct_access':
276 1
				case 'embed_only':
277 1
				case 'not_public':
278
				default:
279 1
					gravityview()->log->notice( 'View #{view_id} cannot render: {error_code} {error_message}', array( 'error_code' => $error->get_error_code(), 'error_message' => $error->get_error_message() ) );
280 1
					return __( 'You are not allowed to view this content.', 'gravityview' );
281
			}
282
283
			return $content;
284
		}
285
286 5
		$is_admin_and_can_view = $view->settings->get( 'admin_show_all_statuses' ) && \GVCommon::has_cap('gravityview_moderate_entries', $view->ID );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
287
288
		/**
289
		 * Editing a single entry.
290
		 */
291 5
		if ( $entry = $request->is_edit_entry( $view->form ? $view->form->ID : 0 ) ) {
292
			if ( $entry['status'] != 'active' ) {
0 ignored issues
show
introduced by
Found "!= '". Use Yoda Condition checks, you must
Loading history...
293
				gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) );
294
				return __( 'You are not allowed to view this content.', 'gravityview' );
295
			}
296
297
			if ( apply_filters( 'gravityview_custom_entry_slug', false ) && $entry->slug != get_query_var( \GV\Entry::get_endpoint_name() ) ) {
298
				gravityview()->log->error( 'Entry ID #{entry_id} was accessed by a bad slug', array( 'entry_id' => $entry->ID ) );
299
				return __( 'You are not allowed to view this content.', 'gravityview' );
300
			}
301
302
			if ( $view->settings->get( 'show_only_approved' ) && ! $is_admin_and_can_view ) {
303
				if ( ! \GravityView_Entry_Approval_Status::is_approved( gform_get_meta( $entry->ID, \GravityView_Entry_Approval::meta_key ) )  ) {
304
					gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $entry->ID ) );
305
					return __( 'You are not allowed to view this content.', 'gravityview' );
306
				}
307
			}
308
309
			$renderer = new Edit_Entry_Renderer();
310
			return $renderer->render( $entry, $view, $request );
311
312
		/**
313
		 * Viewing a single entry.
314
		 */
315 5
		} else if ( $entry = $request->is_entry( $view->form ? $view->form->ID : 0 ) ) {
316
317 1
			$entryset = $entry->is_multi() ? $entry->entries : array( $entry );
0 ignored issues
show
Bug introduced by
The property entries does not seem to exist in GV\GF_Entry.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
318
319 1
			$custom_slug = apply_filters( 'gravityview_custom_entry_slug', false );
320 1
			$ids = explode( ',', get_query_var( \GV\Entry::get_endpoint_name() ) );
321
322 1
			$show_only_approved = $view->settings->get( 'show_only_approved' );
323
324 1
			foreach ( $entryset as $e ) {
325
326 1
				if ( 'active' !== $e['status'] ) {
327 1
					gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $e->ID ) );
328 1
					return __( 'You are not allowed to view this content.', 'gravityview' );
329
				}
330
331 1
				if ( $custom_slug && ! in_array( $e->slug, $ids ) ) {
332 1
					gravityview()->log->error( 'Entry ID #{entry_id} was accessed by a bad slug', array( 'entry_id' => $e->ID ) );
333 1
					return __( 'You are not allowed to view this content.', 'gravityview' );
334
				}
335
336 1
				if ( $show_only_approved && ! $is_admin_and_can_view ) {
337 1
					if ( ! \GravityView_Entry_Approval_Status::is_approved( gform_get_meta( $e->ID, \GravityView_Entry_Approval::meta_key ) )  ) {
338 1
						gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $e->ID ) );
339 1
						return __( 'You are not allowed to view this content.', 'gravityview' );
340
					}
341
				}
342
			}
343
344 1
			$error = \GVCommon::check_entry_display( $entry->as_entry(), $view );
345
346 1
			if ( is_wp_error( $error ) ) {
347
				gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing: {message}', array( 'entry_id' => $entry->ID, 'message' => $error->get_error_message() ) );
348
				return __( 'You are not allowed to view this content.', 'gravityview' );
349
			}
350
351 1
			$renderer = new Entry_Renderer();
352 1
			return $renderer->render( $entry, $view, $request );
353
		}
354
355
		/**
356
		 * Plain old View.
357
		 */
358 4
		$renderer = new View_Renderer();
359 4
		return $renderer->render( $view, $request );
360
	}
361
362
	/**
363
	 * Checks whether this view can be accessed or not.
364
	 *
365
	 * @param string[]    $context The context we're asking for access from.
366
	 *                             Can any and as many of one of:
367
	 *                                 edit      An edit context.
368
	 *                                 single    A single context.
369
	 *                                 cpt       The custom post type single page acessed.
370
	 *                                 shortcode Embedded as a shortcode.
371
	 *                                 oembed    Embedded as an oEmbed.
372
	 *                                 rest      A REST call.
373
	 * @param \GV\Request $request The request
374
	 *
375
	 * @return bool|\WP_Error An error if this View shouldn't be rendered here.
376
	 */
377 19
	public function can_render( $context = null, $request = null ) {
378 19
		if ( ! $request ) {
379 1
			$request = gravityview()->request;
380
		}
381
382 19
		if ( ! is_array( $context ) ) {
383 5
			$context = array();
384
		}
385
386
		/**
387
		 * @filter `gravityview/view/can_render` Whether the view can be rendered or not.
388
		 * @param bool|\WP_Error $result  The result. Default: null.
389
		 * @param \GV\View       $view	The view.
390
		 * @param string[]       $context See \GV\View::can_render
391
		 * @param \GV\Request    $request The request.
392
		 */
393 19
		if ( ! is_null( $result = apply_filters( 'gravityview/view/can_render', null, $this, $context, $request ) ) ) {
394
			return $result;
395
		}
396
397 19
		if ( in_array( 'rest', $context ) ) {
398
			// REST
399 6
			if ( gravityview()->plugin->settings->get( 'rest_api' ) === '1' && $this->settings->get( 'rest_disable' ) === '1' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
400 1
				return new \WP_Error( 'gravityview/rest_disabled' );
401 6
			} elseif ( gravityview()->plugin->settings->get( 'rest_api' ) !== '1' && $this->settings->get( 'rest_enable' ) !== '1' ) {
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
402 1
				return new \WP_Error( 'gravityview/rest_disabled' );
403
			}
404
		}
405
406 19
		if ( in_array( 'csv', $context ) ) {
407 1
			if ( $this->settings->get( 'csv_enable' ) !== '1' ) {
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
408 1
				return new \WP_Error( 'gravityview/csv_disabled', 'The CSV endpoint is not enabled for this View' );
409
			}
410
		}
411
412
		/**
413
		 * This View is password protected. Nothing to do here.
414
		 */
415 19
		if ( post_password_required( $this->ID ) ) {
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
416 3
			gravityview()->log->notice( 'Post password is required for View #{view_id}', array( 'view_id' => $this->ID ) );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
417 3
			return new \WP_Error( 'gravityview/post_password_required' );
418
		}
419
420 19
		if ( ! $this->form ) {
421
			gravityview()->log->notice( 'View #{id} has no form attached to it.', array( 'id' => $this->ID ) );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
422
			return new \WP_Error( 'gravityview/no_form_attached' );
423
		}
424
425 19
		if ( ! in_array( 'shortcode', $context ) ) {
426
			/**
427
			 * Is this View directly accessible via a post URL?
428
			 *
429
			 * @see https://codex.wordpress.org/Function_Reference/register_post_type#public
430
			 */
431
432
			/**
433
			 * @filter `gravityview_direct_access` Should Views be directly accessible, or only visible using the shortcode?
434
			 * @deprecated
435
			 * @param[in,out] boolean `true`: allow Views to be accessible directly. `false`: Only allow Views to be embedded. Default: `true`
436
			 * @param int $view_id The ID of the View currently being requested. `0` for general setting
437
			 */
438 12
			$direct_access = apply_filters( 'gravityview_direct_access', true, $this->ID );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
439
440
			/**
441
			 * @filter `gravityview/request/output/direct` Should this View be directly accessbile?
442
			 * @since 2.0
443
			 * @param[in,out] boolean Accessible or not. Default: accessbile.
444
			 * @param \GV\View $view The View we're trying to directly render here.
445
			 * @param \GV\Request $request The current request.
446
			 */
447 12
			if ( ! apply_filters( 'gravityview/view/output/direct', $direct_access, $this, $request ) ) {
448
				return new \WP_Error( 'gravityview/no_direct_access' );
449
			}
450
451
			/**
452
			 * Is this View an embed-only View? If so, don't allow rendering here,
453
			 *  as this is a direct request.
454
			 */
455 12
			if ( $this->settings->get( 'embed_only' ) && ! \GVCommon::has_cap( 'read_private_gravityviews' ) ) {
456 1
				return new \WP_Error( 'gravityview/embed_only' );
457
			}
458
		}
459
460
		/** Private, pending, draft, etc. */
461 19
		$public_states = get_post_stati( array( 'public' => true ) );
462 19
		if ( ! in_array( $this->post_status, $public_states ) && ! \GVCommon::has_cap( 'read_gravityview', $this->ID ) ) {
0 ignored issues
show
Documentation introduced by
The property post_status does not exist on object<GV\View>. 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...
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
463 3
			gravityview()->log->notice( 'The current user cannot access this View #{view_id}', array( 'view_id' => $this->ID ) );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
464 3
			return new \WP_Error( 'gravityview/not_public' );
465
		}
466
467 19
		return true;
468
	}
469
470
	/**
471
	 * Get joins associated with a view
472
	 *
473
	 * @param \WP_Post $post GravityView CPT to get joins for
474
	 *
475
	 * @api
476
	 * @since 2.0.11
477
	 *
478
	 * @return \GV\Join[] Array of \GV\Join instances
479
	 */
480 109
	public static function get_joins( $post ) {
481 109
		$joins = array();
482
483 109
		if ( ! gravityview()->plugin->supports( Plugin::FEATURE_JOINS ) ) {
484
			gravityview()->log->error( 'Cannot get joined forms; joins feature not supported.' );
485
			return $joins;
486
		}
487
488 109
		if ( ! $post || 'gravityview' !== get_post_type( $post ) ) {
489
			gravityview()->log->error( 'Only "gravityview" post types can be \GV\View instances.' );
490
			return $joins;
491
		}
492
493 109
		$joins_meta = get_post_meta( $post->ID, '_gravityview_form_joins', true );
494
495 109
		if ( empty( $joins_meta ) ) {
496 109
			return $joins;
497
		}
498
499
		foreach ( $joins_meta as $meta ) {
500
			if ( ! is_array( $meta ) || count( $meta ) != 4 ) {
0 ignored issues
show
introduced by
Found "!= 4". Use Yoda Condition checks, you must
Loading history...
501
				continue;
502
			}
503
504
			list( $join, $join_column, $join_on, $join_on_column ) = $meta;
505
506
			$join    = GF_Form::by_id( $join );
507
			$join_on = GF_Form::by_id( $join_on );
508
509
			$join_column    = is_numeric( $join_column ) ? GF_Field::by_id( $join, $join_column ) : Internal_Field::by_id( $join_column );
510
			$join_on_column = is_numeric( $join_on_column ) ? GF_Field::by_id( $join_on, $join_on_column ) : Internal_Field::by_id( $join_on_column );
511
512
			$joins [] = new Join( $join, $join_column, $join_on, $join_on_column );
513
		}
514
515
		return $joins;
516
	}
517
518
	/**
519
	 * Get joined forms associated with a view
520
	 * In no particular order.
521
	 *
522
	 * @since 2.0.11
523
	 *
524
	 * @api
525
	 * @since 2.0
526
	 * @param int $post_id ID of the View
527
	 *
528
	 * @return \GV\GF_Form[] Array of \GV\GF_Form instances
529
	 */
530
	public static function get_joined_forms( $post_id ) {
531
		$forms = array();
532
533
		if ( ! gravityview()->plugin->supports( Plugin::FEATURE_JOINS ) ) {
534
			gravityview()->log->error( 'Cannot get joined forms; joins feature not supported.' );
535
			return $forms;
536
		}
537
538
		if ( ! $post_id || ! gravityview()->plugin->supports( Plugin::FEATURE_JOINS ) ) {
539
			return $forms;
540
		}
541
542
		if ( empty( $post_id ) ) {
543
			gravityview()->log->error( 'Cannot get joined forms; $post_id was empty' );
544
			return $forms;
545
		}
546
547
		$joins_meta = get_post_meta( $post_id, '_gravityview_form_joins', true );
548
549
		if ( empty( $joins_meta ) ) {
550
			return $forms;
551
		}
552
553
		foreach ( $joins_meta  as $meta ) {
554
			if ( ! is_array( $meta ) || count( $meta ) != 4 ) {
0 ignored issues
show
introduced by
Found "!= 4". Use Yoda Condition checks, you must
Loading history...
555
				continue;
556
			}
557
558
			list( $join, $join_column, $join_on, $join_on_column ) = $meta;
559
560
			if ( $form = GF_Form::by_id( $join_on ) ) {
561
				$forms[ $join_on ] = $form;
562
			}
563
564
			if ( $form = GF_Form::by_id( $join ) ) {
565
				$forms[ $join ] = $form;
566
			}
567
		}
568
569
		return $forms;
570
	}
571
572
	/**
573
	 * Construct a \GV\View instance from a \WP_Post.
574
	 *
575
	 * @param \WP_Post $post The \WP_Post instance to wrap.
576
	 *
577
	 * @api
578
	 * @since 2.0
579
	 * @return \GV\View|null An instance around this \WP_Post if valid, null otherwise.
580
	 */
581 110
	public static function from_post( $post ) {
582
583 110
		if ( ! $post || 'gravityview' !== get_post_type( $post ) ) {
584 2
			gravityview()->log->error( 'Only gravityview post types can be \GV\View instances.' );
585 2
			return null;
586
		}
587
588 110
		if ( $view = Utils::get( self::$cache, "View::from_post:{$post->ID}" ) ) {
589
			/**
590
			 * @filter `gravityview/view/get` Override View.
591
			 * @param \GV\View $view The View instance pointer.
592
			 * @since 2.1
593
			 */
594 54
			do_action_ref_array( 'gravityview/view/get', array( &$view ) );
595
596 54
			return $view;
597
		}
598
599 110
		$view = new self();
600 110
		$view->post = $post;
601
602
		/** Get connected form. */
603 110
		$view->form = GF_Form::by_id( $view->_gravityview_form_id );
0 ignored issues
show
Documentation introduced by
The property _gravityview_form_id does not exist on object<GV\View>. 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...
604 110
		if ( ! $view->form ) {
605
			gravityview()->log->error( 'View #{view_id} tried attaching non-existent Form #{form_id} to it.', array(
606
				'view_id' => $view->ID,
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
607
				'form_id' => $view->_gravityview_form_id ? : 0,
0 ignored issues
show
Documentation introduced by
The property _gravityview_form_id does not exist on object<GV\View>. 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...
608
			) );
609
		}
610
611 110
		$view->joins = $view->get_joins( $post );
612
613
		/**
614
		 * @filter `gravityview/configuration/fields` Filter the View fields' configuration array.
615
		 * @since 1.6.5
616
		 *
617
		 * @deprecated Use `gravityview/view/configuration/fields` or `gravityview/view/fields` filters.
618
		 *
619
		 * @param $fields array Multi-array of fields with first level being the field zones.
620
		 * @param $view_id int The View the fields are being pulled for.
621
		 */
622 110
		$configuration = apply_filters( 'gravityview/configuration/fields', (array)$view->_gravityview_directory_fields, $view->ID );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
623
624
		/**
625
		 * @filter `gravityview/view/configuration/fields` Filter the View fields' configuration array.
626
		 * @since 2.0
627
		 *
628
		 * @param array $fields Multi-array of fields with first level being the field zones.
629
		 * @param \GV\View $view The View the fields are being pulled for.
630
		 */
631 110
		$configuration = apply_filters( 'gravityview/view/configuration/fields', $configuration, $view );
632
633
		/**
634
		 * @filter `gravityview/view/fields` Filter the Field Collection for this View.
635
		 * @since 2.0
636
		 *
637
		 * @param \GV\Field_Collection $fields A collection of fields.
638
		 * @param \GV\View $view The View the fields are being pulled for.
639
		 */
640 110
		$view->fields = apply_filters( 'gravityview/view/fields', Field_Collection::from_configuration( $configuration ), $view );
641
642
		/**
643
		 * @filter `gravityview/view/configuration/widgets` Filter the View widgets' configuration array.
644
		 * @since 2.0
645
		 *
646
		 * @param array $fields Multi-array of widgets with first level being the field zones.
647
		 * @param \GV\View $view The View the widgets are being pulled for.
648
		 */
649 110
		$configuration = apply_filters( 'gravityview/view/configuration/widgets', (array)$view->_gravityview_directory_widgets, $view );
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
650
651
		/**
652
		 * @filter `gravityview/view/widgets` Filter the Widget Collection for this View.
653
		 * @since 2.0
654
		 *
655
		 * @param \GV\Widget_Collection $widgets A collection of widgets.
656
		 * @param \GV\View $view The View the widgets are being pulled for.
657
		 */
658 110
		$view->widgets = apply_filters( 'gravityview/view/widgets', Widget_Collection::from_configuration( $configuration ), $view );
659
660
		/** View configuration. */
661 110
		$view->settings->update( gravityview_get_template_settings( $view->ID ) );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
662
663
		/** Add the template name into the settings. */
664 110
		$view->settings->update( array( 'template' => gravityview_get_template_id( $view->ID ) ) );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
665
666
		/** View basics. */
667 110
		$view->settings->update( array(
668 110
			'id' => $view->ID,
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
669
		) );
670
671 110
		self::$cache[ "View::from_post:{$post->ID}" ] = &$view;
672
673
		/**
674
		 * @filter `gravityview/view/get` Override View.
675
		 * @param \GV\View $view The View instance pointer.
676
		 * @since 2.1
677
		 */
678 110
		do_action_ref_array( 'gravityview/view/get', array( &$view ) );
679
680 110
		return $view;
681
	}
682
683
	/**
684
	 * Flush the view cache.
685
	 *
686
	 * @param int $view_id The View to reset cache for. Optional. Default: resets everything.
687
	 *
688
	 * @internal
689
	 */
690 124
	public static function _flush_cache( $view_id = null ) {
691 124
		if ( $view_id ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $view_id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
692 117
			unset( self::$cache[ "View::from_post:$view_id" ] );
693 117
			return;
694
		}
695 71
		self::$cache = array();
696 71
	}
697
698
	/**
699
	 * Construct a \GV\View instance from a post ID.
700
	 *
701
	 * @param int|string $post_id The post ID.
702
	 *
703
	 * @api
704
	 * @since 2.0
705
	 * @return \GV\View|null An instance around this \WP_Post or null if not found.
706
	 */
707 67
	public static function by_id( $post_id ) {
708 67
		if ( ! $post_id || ! $post = get_post( $post_id ) ) {
709 3
			return null;
710
		}
711 67
		return self::from_post( $post );
712
	}
713
714
	/**
715
	 * Determines if a view exists to begin with.
716
	 *
717
	 * @param int|\WP_Post|null $view The WordPress post ID, a \WP_Post object or null for global $post;
718
	 *
719
	 * @api
720
	 * @since 2.0
721
	 * @return bool Whether the post exists or not.
722
	 */
723 13
	public static function exists( $view ) {
724 13
		return get_post_type( $view ) == 'gravityview';
725
	}
726
727
	/**
728
	 * ArrayAccess compatibility layer with GravityView_View_Data::$views
729
	 *
730
	 * @internal
731
	 * @deprecated
732
	 * @since 2.0
733
	 * @return bool Whether the offset exists or not, limited to GravityView_View_Data::$views element keys.
734
	 */
735 13
	public function offsetExists( $offset ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetExists is in camel caps, but expected offset_exists instead as per the coding standard.
Loading history...
736 13
		$data_keys = array( 'id', 'view_id', 'form_id', 'template_id', 'atts', 'fields', 'widgets', 'form' );
737 13
		return in_array( $offset, $data_keys );
738
	}
739
740
	/**
741
	 * ArrayAccess compatibility layer with GravityView_View_Data::$views
742
	 *
743
	 * Maps the old keys to the new data;
744
	 *
745
	 * @internal
746
	 * @deprecated
747
	 * @since 2.0
748
	 *
749
	 * @return mixed The value of the requested view data key limited to GravityView_View_Data::$views element keys.
750
	 */
751 13
	public function offsetGet( $offset ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetGet is in camel caps, but expected offset_get instead as per the coding standard.
Loading history...
752
753 13
		gravityview()->log->notice( 'This is a \GV\View object should not be accessed as an array.' );
754
755 13
		if ( ! isset( $this[ $offset ] ) ) {
756
			return null;
757
		}
758
759 13
		switch ( $offset ) {
760 13
			case 'id':
761 13
			case 'view_id':
762 1
				return $this->ID;
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
763 13
			case 'form':
764 13
				return $this->form;
765 1
			case 'form_id':
766 1
				return $this->form ? $this->form->ID : null;
767 1
			case 'atts':
768
				return $this->settings->as_atts();
0 ignored issues
show
Deprecated Code introduced by
The method GV\View_Settings::as_atts() has been deprecated.

This method has been deprecated.

Loading history...
769 1
			case 'template_id':
770 1
				return $this->settings->get( 'template' );
771
			case 'widgets':
772
				return $this->widgets->as_configuration();
773
		}
774
	}
775
776
	/**
777
	 * ArrayAccess compatibility layer with GravityView_View_Data::$views
778
	 *
779
	 * @internal
780
	 * @deprecated
781
	 * @since 2.0
782
	 *
783
	 * @return void
784
	 */
785 1
	public function offsetSet( $offset, $value ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetSet is in camel caps, but expected offset_set instead as per the coding standard.
Loading history...
786 1
		gravityview()->log->error( 'The old view data is no longer mutable. This is a \GV\View object should not be accessed as an array.' );
787 1
	}
788
789
	/**
790
	 * ArrayAccess compatibility layer with GravityView_View_Data::$views
791
	 *
792
	 * @internal
793
	 * @deprecated
794
	 * @since 2.0
795
	 * @return void
796
	 */
797 1
	public function offsetUnset( $offset ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetUnset is in camel caps, but expected offset_unset instead as per the coding standard.
Loading history...
798 1
		gravityview()->log->error( 'The old view data is no longer mutable. This is a \GV\View object should not be accessed as an array.' );
799 1
	}
800
801
	/**
802
	 * Be compatible with the old data object.
803
	 *
804
	 * Some external code expects an array (doing things like foreach on this, or array_keys)
805
	 *  so let's return an array in the old format for such cases. Do not use unless using
806
	 *  for back-compatibility.
807
	 *
808
	 * @internal
809
	 * @deprecated
810
	 * @since 2.0
811
	 * @return array
812
	 */
813 16
	public function as_data() {
814
		return array(
815 16
			'id' => $this->ID,
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
816 16
			'view_id' => $this->ID,
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. 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...
817 16
			'form_id' => $this->form ? $this->form->ID : null,
818 16
			'form' => $this->form ? gravityview_get_form( $this->form->ID ) : null,
819 16
			'atts' => $this->settings->as_atts(),
0 ignored issues
show
Deprecated Code introduced by
The method GV\View_Settings::as_atts() has been deprecated.

This method has been deprecated.

Loading history...
820 16
			'fields' => $this->fields->by_visible()->as_configuration(),
821 16
			'template_id' => $this->settings->get( 'template' ),
822 16
			'widgets' => $this->widgets->as_configuration(),
823
		);
824
	}
825
826
	/**
827
	 * Retrieve the entries for the current view and request.
828
	 *
829
	 * @param \GV\Request The request. Unused for now.
830
	 *
831
	 * @return \GV\Entry_Collection The entries.
832
	 */
833 32
	public function get_entries( $request = null ) {
834 32
		$entries = new \GV\Entry_Collection();
835 32
		if ( $this->form ) {
836
			/**
837
			 * @todo: Stop using _frontend and use something like $request->get_search_criteria() instead
838
			 */
839 32
			$parameters = \GravityView_frontend::get_view_entries_parameters( $this->settings->as_atts(), $this->form->ID );
0 ignored issues
show
Deprecated Code introduced by
The method GV\View_Settings::as_atts() has been deprecated.

This method has been deprecated.

Loading history...
840 32
			$parameters['context_view_id'] = $this->ID;
0 ignored issues
show
Bug introduced by
The property ID does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
841 32
			$parameters = \GVCommon::calculate_get_entries_criteria( $parameters, $this->form->ID );
842
843 32
			if ( $request instanceof REST\Request ) {
844 4
				$atts = $this->settings->as_atts();
0 ignored issues
show
Deprecated Code introduced by
The method GV\View_Settings::as_atts() has been deprecated.

This method has been deprecated.

Loading history...
845 4
				$paging_parameters = wp_parse_args( $request->get_paging(), array(
846 4
						'paging' => array( 'page_size' => $atts['page_size'] ),
847
					) );
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 20.
Loading history...
848 4
				$parameters['paging'] = $paging_parameters['paging'];
849
			}
850
851 32
			$page = Utils::get( $parameters['paging'], 'current_page' ) ?
852 32
				: ( ( ( $parameters['paging']['offset'] - $this->settings->get( 'offset' ) ) / $parameters['paging']['page_size'] ) + 1 );
853
854
			/**
855
			 * Cleanup duplicate field_filter parameters to simplify the query.
856
			 */
857 32
			$unique_field_filters = array();
858 32
			foreach ( $parameters['search_criteria']['field_filters'] as $key => $filter ) {
859 7
				if ( 'mode' === $key ) {
860 7
					$unique_field_filters['mode'] = $filter;
861 7
				} else if ( ! in_array( $filter, $unique_field_filters ) ) {
862 7
					$unique_field_filters[] = $filter;
863
				}
864
			}
865 32
			$parameters['search_criteria']['field_filters'] = $unique_field_filters;
866
867 32
			if ( ! empty( $parameters['search_criteria']['field_filters'] ) ) {
868 7
				gravityview()->log->notice( 'search_criteria/field_filters is not empty, third-party code may be using legacy search_criteria filters.' );
869
			}
870
871 32
			if ( gravityview()->plugin->supports( Plugin::FEATURE_GFQUERY ) ) {
872 32
				$query_class = $this->get_query_class();
873 32
				$query = new $query_class( $this->form->ID, $parameters['search_criteria'], $parameters['sorting'] );
874
875 32
				$query->limit( $parameters['paging']['page_size'] )
876 32
					->offset( ( ( $page - 1 ) * $parameters['paging']['page_size'] ) + $this->settings->get( 'offset' ) );
877
878
				/**
879
				 * Any joins?
880
				 */
881 32
				if ( Plugin::FEATURE_JOINS && count( $this->joins ) ) {
882
883
					$is_admin_and_can_view = $this->settings->get( 'admin_show_all_statuses' ) && \GVCommon::has_cap( 'gravityview_moderate_entries', $this->ID );
884
					
885
					foreach ( $this->joins as $join ) {
886
						$query = $join->as_query_join( $query );
887
888
						if ( true /** $this->settings->get( 'WHATEVER YOU CALL IT' ) **/ ) {
0 ignored issues
show
Bug introduced by
Avoid IF statements that are always true or false
Loading history...
889
890
							// Disable NULL outputs
891
							$condition = new \GF_Query_Condition(
892
								new \GF_Query_Column( $join->join_on_column->ID, $join->join_on->ID ),
0 ignored issues
show
Bug introduced by
The property ID does not seem to exist in GV\Source.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
893
								\GF_Query_Condition::NEQ,
894
								new \GF_Query_Literal( '' )
895
							);
896
897
							$query_parameters = $query->_introspect();
898
899
							$query->where( \GF_Query_Condition::_and( $query_parameters['where'], $condition ) );
900
						}
901
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
902
903
						if ( $this->settings->get( 'show_only_approved' ) && ! $is_admin_and_can_view ) {
904
905
							// Show only approved joined entries
906
							$condition = new \GF_Query_Condition(
907
								new \GF_Query_Column( \GravityView_Entry_Approval::meta_key, $join->join_on->ID ),
908
								\GF_Query_Condition::EQ,
909
								new \GF_Query_Literal( \GravityView_Entry_Approval_Status::APPROVED )
910
							);
911
912
							$query_parameters = $query->_introspect();
913
914
							$query->where( \GF_Query_Condition::_and( $query_parameters['where'], $condition ) );
915
						}
916
					}
917
				}
918
919
				/**
920
				 * @action `gravityview/view/query` Override the \GF_Query before the get() call.
921
				 * @param \GF_Query $query The current query object reference
922
				 * @param \GV\View $this The current view object
923
				 * @param \GV\Request $request The request object
924
				 */
925 32
				do_action_ref_array( 'gravityview/view/query', array( &$query, $this, $request ) );
926
927 32
				gravityview()->log->debug( 'GF_Query parameters: ', array( 'data' => Utils::gf_query_debug( $query ) ) );
928
929
				/**
930
				 * Map from Gravity Forms entries arrays to an Entry_Collection.
931
				 */
932 32
				if ( count( $this->joins ) ) {
933
					foreach ( $query->get() as $entry ) {
934
						$entries->add(
935
							Multi_Entry::from_entries( array_map( '\GV\GF_Entry::from_entry', $entry ) )
936
						);
937
					}
938
				} else {
939 32
					array_map( array( $entries, 'add' ), array_map( '\GV\GF_Entry::from_entry', $query->get() ) );
940
				}
941
942
				/**
943
				 * Add total count callback.
944
				 */
945 32
				$entries->add_count_callback( function() use ( $query ) {
946 19
					return $query->total_found;
947 32
				} );
948
			} else {
949
				$entries = $this->form->entries
0 ignored issues
show
Documentation introduced by
The property entries does not exist on object<GV\Form>. 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...
950
					->filter( \GV\GF_Entry_Filter::from_search_criteria( $parameters['search_criteria'] ) )
951
					->offset( $this->settings->get( 'offset' ) )
952
					->limit( $parameters['paging']['page_size'] )
953
					->page( $page );
954
955
				if ( ! empty( $parameters['sorting'] ) && ! empty( $parameters['sorting']['key'] ) ) {
956
					$field = new \GV\Field();
957
					$field->ID = $parameters['sorting']['key'];
958
					$direction = strtolower( $parameters['sorting']['direction'] ) == 'asc' ? \GV\Entry_Sort::ASC : \GV\Entry_Sort::DESC;
959
					$entries = $entries->sort( new \GV\Entry_Sort( $field, $direction ) );
960
				}
961
			}
962
		}
963
964
		/**
965
		 * @filter `gravityview/view/entries` Modify the entry fetching filters, sorts, offsets, limits.
966
		 * @param \GV\Entry_Collection $entries The entries for this view.
967
		 * @param \GV\View $view The view.
968
		 * @param \GV\Request $request The request.
969
		 */
970 32
		return apply_filters( 'gravityview/view/entries', $entries, $this, $request );
971
	}
972
973
	/**
974
	 * Last chance to configure the output.
975
	 *
976
	 * Used for CSV output, for example.
977
	 *
978
	 * @return void
979
	 */
980 1
	public static function template_redirect() {
981
		/**
982
		 * CSV output.
983
		 */
984 1
		if ( ! get_query_var( 'csv' ) ) {
985 1
			return;
986
		}
987
988 1
		if ( ! $view = gravityview()->request->is_view() ) {
989 1
			return;
990
		}
991
992 1
		if ( is_wp_error( $error = $view->can_render( array( 'csv' ) ) ) ) {
993 1
			gravityview()->log->error( 'Not rendering CSV: ' . $error->get_error_message() );
994 1
			return;
995
		}
996
997
		/**
998
		 * Modify the name of the generated CSV file. Name will be sanitized using sanitize_file_name() before output.
999
		 * @see sanitize_file_name()
1000
		 * @since 2.1
1001
		 * @param string   $filename File name used when downloading a CSV. Default is "{View title}.csv"
1002
		 * @param \GV\View $view Current View being rendered
1003
		 */
1004 1
		$filename = apply_filters( 'gravityview/output/csv/filename', get_the_title( $view->post ), $view );
1005
1006 1
		if ( ! defined( 'DOING_GRAVITYVIEW_TESTS' ) ) {
1007
			header( sprintf( 'Content-Disposition: attachment;filename="%s.csv"', sanitize_file_name( $filename ) ) );
1008
			header( 'Content-Transfer-Encoding: binary' );
1009
			header( 'Content-Type: text/csv' );
1010
		}
1011
1012 1
		ob_start();
1013 1
		$csv = fopen( 'php://output', 'w' );
1014
1015
		/**
1016
		 * Add da' BOM if GF uses it
1017
		 * @see GFExport::start_export()
1018
		 */
1019 1
		if ( apply_filters( 'gform_include_bom_export_entries', true, $view->form ? $view->form->form : 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...
1020
			fputs( $csv, "\xef\xbb\xbf" );
0 ignored issues
show
introduced by
Filesystem writes are forbidden, you should not be using fputs()
Loading history...
1021
		}
1022
1023 1
		$entries = $view->get_entries();
1024
1025 1
		$headers_done = false;
1026 1
		$allowed = $headers = array();
1027
1028 1
		foreach ( $view->fields->by_position( "directory_*" )->by_visible()->all() as $field ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal directory_* does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
1029 1
			$allowed[ $field->ID ] = $field;
1030
		}
1031
1032 1
		$renderer = new Field_Renderer();
1033
1034 1
		foreach ( $entries->all() as $entry ) {
1035
1036 1
			$return = array();
1037
1038
			/**
1039
			 * @filter `gravityview/csv/entry/fields` Whitelist more entry fields that are output in CSV requests.
1040
			 * @param[in,out] array $allowed The allowed ones, default by_visible, by_position( "context_*" ), i.e. as set in the View.
1041
			 * @param \GV\View $view The view.
1042
			 * @param \GV\Entry $entry WordPress representation of the item.
1043
			 */
1044 1
			$allowed_field_ids = apply_filters( 'gravityview/csv/entry/fields', array_keys( $allowed ), $view, $entry );
1045
1046 1
			foreach ( $allowed_field_ids as $field_id ) {
1047 1
				$source = is_numeric( $field_id ) ? $view->form : new \GV\Internal_Source();
1048
1049 1
				if ( isset( $allowed[ $field_id ] ) ) {
1050 1
					$field = $allowed[ $field_id ];
1051
				} else {
1052
					$field = is_numeric( $field_id ) ? \GV\GF_Field::by_id( $view->form, $field_id ) : \GV\Internal_Field::by_id( $field_id );
1053
				}
1054
1055 1
				$return[ $field->ID ] = $renderer->render( $field, $view, $source, $entry, gravityview()->request, '\GV\Field_CSV_Template' );
1056
1057 1
				if ( ! $headers_done ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $headers_done of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
1058 1
					$label = $field->get_label( $view, $source, $entry );
1059 1
					$headers[ $field->ID ] = $label ? $label : $field->ID;
1060
				}
1061
			}
1062
1063 1
			if ( ! $headers_done ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $headers_done of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
1064 1
				$headers_done = fputcsv( $csv, array_map( array( '\GV\Utils', 'strip_excel_formulas' ), array_values( $headers ) ) );
0 ignored issues
show
introduced by
Filesystem writes are forbidden, you should not be using fputcsv()
Loading history...
1065
			}
1066
1067 1
			fputcsv( $csv, array_map( array( '\GV\Utils', 'strip_excel_formulas' ), $return ) );
0 ignored issues
show
introduced by
Filesystem writes are forbidden, you should not be using fputcsv()
Loading history...
1068
		}
1069
1070 1
		fflush( $csv );
1071
1072 1
		echo rtrim( ob_get_clean() );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'rtrim'
Loading history...
1073
1074 1
		if ( ! defined( 'DOING_GRAVITYVIEW_TESTS' ) ) {
1075
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method template_redirect() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
1076
		}
1077 1
	}
1078
1079
	/**
1080
	 * Return the query class for this View.
1081
	 *
1082
	 * @return string The class name.
1083
	 */
1084 32
	public function get_query_class() {
1085
		/**
1086
		 * @filter `gravityview/query/class`
1087
		 * @param[in,out] string The query class. Default: GF_Query.
1088
		 * @param \GV\View $this The View.
1089
		 */
1090 32
		$query_class = apply_filters( 'gravityview/query/class', '\GF_Query', $this );
1091 32
		return $query_class;
1092
	}
1093
1094 109
	public function __get( $key ) {
1095 109
		if ( $this->post ) {
1096 109
			$raw_post = $this->post->filter( 'raw' );
1097 109
			return $raw_post->{$key};
1098
		}
1099
		return isset( $this->{$key} ) ? $this->{$key} : null;
1100
	}
1101
}
1102