Completed
Push — develop ( f33cb6...e8781c )
by Gennady
15:41 queued 09:44
created

View::get_entries()   C

Complexity

Conditions 13
Paths 29

Size

Total Lines 90

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 15.6406

Importance

Changes 0
Metric Value
cc 13
nc 29
nop 1
dl 0
loc 90
ccs 30
cts 40
cp 0.75
crap 15.6406
rs 5.5115
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 74
	public function __construct() {
82 74
		$this->settings = new View_Settings();
83 74
		$this->fields = new Field_Collection();
84 74
		$this->widgets = new Widget_Collection();
85 74
	}
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
	 * A renderer filter for the View post type content.
200
	 *
201
	 * @param string $content Should be empty, as we don't store anything there.
202
	 *
203
	 * @return string $content The view content as output by the renderers.
204
	 */
205 10
	public static function content( $content ) {
206 10
		$request = gravityview()->request;
207
208
		// Plugins may run through the content in the header. WP SEO does this for its OpenGraph functionality.
209 10
		if ( ! defined( 'DOING_GRAVITYVIEW_TESTS' ) ) {
210
			if ( ! did_action( 'loop_start' ) ) {
211
				gravityview()->log->debug( 'Not processing yet: loop_start hasn\'t run yet. Current action: {action}', array( 'action' => current_filter() ) );
212
				return $content;
213
			}
214
215
			//	We don't want this filter to run infinite loop on any post content fields
216
			remove_filter( 'the_content', array( __CLASS__, __METHOD__ ) );
217
		}
218
219
		/**
220
		 * This is not a View. Bail.
221
		 *
222
		 * Shortcodes and oEmbeds and whatnot will be handled
223
		 *  elsewhere.
224
		 */
225 10
		if ( ! $view = $request->is_view() ) {
226 5
			return $content;
227
		}
228
229
		/**
230
		 * Check permissions.
231
		 */
232 5
		while ( $error = $view->can_render( null, $request ) ) {
233 1
			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...
234
				break;
235
236 1
			switch ( str_replace( 'gravityview/', '', $error->get_error_code() ) ) {
237 1
				case 'post_password_required':
238 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...
239 1
				case 'no_form_attached':
240
					/**
241
					 * This View has no data source. There's nothing to show really.
242
					 * ...apart from a nice message if the user can do anything about it.
243
					 */
244
					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...
245
						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...
246
					}
247
					break;
248 1
				case 'no_direct_access':
249 1
				case 'embed_only':
250 1
				case 'not_public':
251 1
					return __( 'You are not allowed to view this content.', 'gravityview' );
252
			}
253
254
			return $content;
255
		}
256
257 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...
258
259
		/**
260
		 * Editing a single entry.
261
		 */
262 5
		if ( $entry = $request->is_edit_entry() ) {
263
			if ( $entry['status'] != 'active' ) {
0 ignored issues
show
introduced by
Found "!= '". Use Yoda Condition checks, you must
Loading history...
264
				gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) );
265
				return __( 'You are not allowed to view this content.', 'gravityview' );
266
			}
267
268
			if ( apply_filters( 'gravityview_custom_entry_slug', false ) && $entry->slug != get_query_var( \GV\Entry::get_endpoint_name() ) ) {
269
				gravityview()->log->error( 'Entry ID #{entry_id} was accessed by a bad slug', array( 'entry_id' => $entry->ID ) );
270
				return __( 'You are not allowed to view this content.', 'gravityview' );
271
			}
272
273
			if ( $view->settings->get( 'show_only_approved' ) && ! $is_admin_and_can_view ) {
274
				if ( ! \GravityView_Entry_Approval_Status::is_approved( gform_get_meta( $entry->ID, \GravityView_Entry_Approval::meta_key ) )  ) {
275
					gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $entry->ID ) );
276
					return __( 'You are not allowed to view this content.', 'gravityview' );
277
				}
278
			}
279
280
			$renderer = new Edit_Entry_Renderer();
281
			return $renderer->render( $entry, $view, $request );
282
283
		/**
284
		 * Viewing a single entry.
285
		 */
286 5
		} else if ( $entry = $request->is_entry() ) {
287 1
			if ( $entry['status'] != 'active' ) {
0 ignored issues
show
introduced by
Found "!= '". Use Yoda Condition checks, you must
Loading history...
288 1
				gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) );
289 1
				return __( 'You are not allowed to view this content.', 'gravityview' );
290
			}
291
292 1
			if ( apply_filters( 'gravityview_custom_entry_slug', false ) && $entry->slug != get_query_var( \GV\Entry::get_endpoint_name() ) ) {
293 1
				gravityview()->log->error( 'Entry ID #{entry_id} was accessed by a bad slug', array( 'entry_id' => $entry->ID ) );
294 1
				return __( 'You are not allowed to view this content.', 'gravityview' );
295
			}
296
297 1
			if ( $view->settings->get( 'show_only_approved' ) && ! $is_admin_and_can_view ) {
298 1
				if ( ! \GravityView_Entry_Approval_Status::is_approved( gform_get_meta( $entry->ID, \GravityView_Entry_Approval::meta_key ) )  ) {
299 1
					gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $entry->ID ) );
300 1
					return __( 'You are not allowed to view this content.', 'gravityview' );
301
				}
302
			}
303
304 1
			$error = \GVCommon::check_entry_display( $entry->as_entry() );
305
306 1
			if( is_wp_error( $error ) ) {
307
				gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing: {message}', array( 'entry_id' => $entry->ID, 'message' => $error->get_error_message() ) );
308
				return __( 'You are not allowed to view this content.', 'gravityview' );
309
			}
310
311 1
			$renderer = new Entry_Renderer();
312 1
			return $renderer->render( $entry, $view, $request );
313
314
		/**
315
		 * Plain old View.
316
		 */
317
		} else {
318 4
			$renderer = new View_Renderer();
319 4
			return $renderer->render( $view, $request );
320
		}
321
322
		return $content;
0 ignored issues
show
Unused Code introduced by
return $content; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
323
	}
324
325
	/**
326
	 * Checks whether this view can be accessed or not.
327
	 *
328
	 * @param string[]    $context The context we're asking for access from.
329
	 *                             Can any and as many of one of:
330
	 *                                 edit      An edit context.
331
	 *                                 single    A single context.
332
	 *                                 cpt       The custom post type single page acessed.
333
	 *                                 shortcode Embedded as a shortcode.
334
	 *                                 oembed    Embedded as an oEmbed.
335
	 *                                 rest      A REST call.
336
	 * @param \GV\Request $request The request
337
	 *
338
	 * @return bool|\WP_Error An error if this View shouldn't be rendered here.
339
	 */
340 14
	public function can_render( $context = null, $request = null ) {
341 14
		if ( ! $request ) {
342
			$request = gravityview()->request;
343
		}
344
345 14
		if ( ! is_array( $context ) ) {
346 5
			$context = array();
347
		}
348
349 14
		if ( in_array( 'rest', $context ) ) {
350
			// REST
351 5
			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...
352 1
				return new \WP_Error( 'gravityview/rest_disabled' );
353 5
			} 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...
354 1
				return new \WP_Error( 'gravityview/rest_disabled' );
355
			}
356
		}
357
358
		/**
359
		 * This View is password protected. Nothing to do here.
360
		 */
361 14
		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...
362 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...
363 3
			return new \WP_Error( 'gravityview/post_password_required' );
364
		}
365
366 14
		if ( ! $this->form ) {
367
			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...
368
			return new \WP_Error( 'gravityview/no_form_attached' );
369
		}
370
371 14
		if ( ! in_array( 'shortcode', $context ) ) {
372
			/**
373
			 * Is this View directly accessible via a post URL?
374
			 *
375
			 * @see https://codex.wordpress.org/Function_Reference/register_post_type#public
376
			 */
377
378
			/**
379
			 * @filter `gravityview_direct_access` Should Views be directly accessible, or only visible using the shortcode?
380
			 * @deprecated
381
			 * @param[in,out] boolean `true`: allow Views to be accessible directly. `false`: Only allow Views to be embedded. Default: `true`
382
			 * @param int $view_id The ID of the View currently being requested. `0` for general setting
383
			 */
384 10
			$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...
385
386
			/**
387
			 * @filter `gravityview/request/output/direct` Should this View be directly accessbile?
388
			 * @since 2.0
389
			 * @param[in,out] boolean Accessible or not. Default: accessbile.
390
			 * @param \GV\View $view The View we're trying to directly render here.
391
			 * @param \GV\Request $request The current request.
392
			 */
393 10
			if ( ! apply_filters( 'gravityview/view/output/direct', $direct_access, $this, $request ) ) {
394
				return new \WP_Error( 'gravityview/no_direct_access' );
395
			}
396
397
			/**
398
			 * Is this View an embed-only View? If so, don't allow rendering here,
399
			 *  as this is a direct request.
400
			 */
401 10
			if ( $this->settings->get( 'embed_only' ) && ! \GVCommon::has_cap( 'read_private_gravityviews' ) ) {
402 1
				return new \WP_Error( 'gravityview/embed_only' );
403
			}
404
		}
405
406
		/** Private, pending, draft, etc. */
407 14
		$public_states = get_post_stati( array( 'public' => true ) );
408 14
		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...
409 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...
410 3
			return new \WP_Error( 'gravityview/not_public' );
411
		}
412 14
	}
413
414
	/**
415
	 * Get joins associated with a view
416
	 *
417
	 * @param \WP_Post $post GravityView CPT to get joins for
418
	 *
419
	 * @since 2.0.11
420
	 *
421
	 * @return \GV\Join[] Array of \GV\Join instances
422
	 */
423 74
	public static function get_joins( $post ) {
424
425 74
		if ( ! gravityview()->plugin->supports( Plugin::FEATURE_JOINS ) ) {
426
			gravityview()->log->error( 'Cannot get joined forms; joins feature not supported.' );
427
			return array();
428
		}
429
430 74
		if ( ! $post || 'gravityview' !== get_post_type( $post ) ) {
431
			gravityview()->log->error( 'Only "gravityview" post types can be \GV\View instances.' );
432
			return array();
433
		}
434
435 74
		$joins_meta = get_post_meta( $post->ID, '_gravityview_form_joins', true );
436
437 74
		if ( empty( $joins_meta ) ) {
438 70
			return array();
439
		}
440
441 4
		$joins = array();
442
443 4
		foreach ( $joins_meta as $meta ) {
444 4
			if ( ! is_array( $meta ) || count( $meta ) != 4 ) {
0 ignored issues
show
introduced by
Found "!= 4". Use Yoda Condition checks, you must
Loading history...
445
				continue;
446
			}
447
448 4
			list( $join, $join_column, $join_on, $join_on_column ) = $meta;
449
450 4
			$join    = GF_Form::by_id( $join );
451 4
			$join_on = GF_Form::by_id( $join_on );
452
453 4
			$join_column    = is_numeric( $join_column ) ? GF_Field::by_id( $join, $join_column ) : Internal_Field( $join_column );
454 4
			$join_on_column = is_numeric( $join_on_column ) ? GF_Field::by_id( $join_on, $join_on_column ) : Internal_Field( $join_on_column );
455
456 4
			$joins [] = new Join( $join, $join_column, $join_on, $join_on_column );
457
		}
458
459 4
		return $joins;
460
	}
461
462
	/**
463
	 * Get joined forms associated with a view
464
	 *
465
	 * @since 2.0.11
466
	 *
467
	 * @param int $post_id ID of the View
468
	 *
469
	 * @return \GV\GF_Form[] Array of \GV\GF_Form instances
470
	 */
471
	public static function get_joined_forms( $post_id = 0 ) {
472
473
		if ( ! gravityview()->plugin->supports( Plugin::FEATURE_JOINS ) ) {
474
			gravityview()->log->error( 'Cannot get joined forms; joins feature not supported.' );
475
			return array();
476
		}
477
478
		if ( empty( $post_id ) ) {
479
			gravityview()->log->error( 'Cannot get joined forms; $post_id was empty' );
480
			return array();
481
		}
482
483
		$joins_meta = get_post_meta( $post_id, '_gravityview_form_joins', true );
484
485
		if ( empty( $joins_meta ) ) {
486
			return array();
487
		}
488
489
		$forms_ids = array();
490
491
		foreach ( $joins_meta  as $meta ) {
492
			if ( ! is_array( $meta ) || count( $meta ) != 4 ) {
0 ignored issues
show
introduced by
Found "!= 4". Use Yoda Condition checks, you must
Loading history...
493
				continue;
494
			}
495
496
			list( $join, $join_column, $join_on, $join_on_column ) = $meta;
497
498
			$forms_ids [] = GF_Form::by_id( $join_on );
499
		}
500
501
		return ( !empty( $forms_ids) ) ? $forms_ids : null;
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
502
	}
503
504
	/**
505
	 * Construct a \GV\View instance from a \WP_Post.
506
	 *
507
	 * @param \WP_Post $post The \WP_Post instance to wrap.
508
	 *
509
	 * @api
510
	 * @since 2.0
511
	 * @return \GV\View|null An instance around this \WP_Post if valid, null otherwise.
512
	 */
513 75
	public static function from_post( $post ) {
514
515 75
		if ( ! $post || 'gravityview' !== get_post_type( $post ) ) {
516 2
			gravityview()->log->error( 'Only gravityview post types can be \GV\View instances.' );
517 2
			return null;
518
		}
519
520 75
		if ( $view = Utils::get( self::$cache, "View::from_post:{$post->ID}" ) ) {
521 35
			return $view;
522
		}
523
524 75
		$view = new self();
525 75
		$view->post = $post;
526
527
		/** Get connected form. */
528 75
		$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...
529 75
		if ( ! $view->form ) {
530
			gravityview()->log->error( 'View #{view_id} tried attaching non-existent Form #{form_id} to it.', array(
531
				'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...
532
				'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...
533
			) );
534
		}
535
536 75
		$view->joins = $view->get_joins( $post );
537
538
		/**
539
		 * @filter `gravityview/configuration/fields` Filter the View fields' configuration array.
540
		 * @since 1.6.5
541
		 *
542
		 * @deprecated Use `gravityview/view/configuration/fields` or `gravityview/view/fields` filters.
543
		 *
544
		 * @param $fields array Multi-array of fields with first level being the field zones.
545
		 * @param $view_id int The View the fields are being pulled for.
546
		 */
547 75
		$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...
548
549
		/**
550
		 * @filter `gravityview/view/configuration/fields` Filter the View fields' configuration array.
551
		 * @since 2.0
552
		 *
553
		 * @param array $fields Multi-array of fields with first level being the field zones.
554
		 * @param \GV\View $view The View the fields are being pulled for.
555
		 */
556 75
		$configuration = apply_filters( 'gravityview/view/configuration/fields', $configuration, $view );
557
558
		/**
559
		 * @filter `gravityview/view/fields` Filter the Field Collection for this View.
560
		 * @since 2.0
561
		 *
562
		 * @param \GV\Field_Collection $fields A collection of fields.
563
		 * @param \GV\View $view The View the fields are being pulled for.
564
		 */
565 75
		$view->fields = apply_filters( 'gravityview/view/fields', Field_Collection::from_configuration( $configuration ), $view );
566
567
		/**
568
		 * @filter `gravityview/view/configuration/widgets` Filter the View widgets' configuration array.
569
		 * @since 2.0
570
		 *
571
		 * @param array $fields Multi-array of widgets with first level being the field zones.
572
		 * @param \GV\View $view The View the widgets are being pulled for.
573
		 */
574 75
		$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...
575
576
		/**
577
		 * @filter `gravityview/view/widgets` Filter the Widget Collection for this View.
578
		 * @since 2.0
579
		 *
580
		 * @param \GV\Widget_Collection $widgets A collection of widgets.
581
		 * @param \GV\View $view The View the widgets are being pulled for.
582
		 */
583 75
		$view->widgets = apply_filters( 'gravityview/view/widgets', Widget_Collection::from_configuration( $configuration ), $view );
584
585
		/** View configuration. */
586 75
		$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...
587
588
		/** Add the template name into the settings. */
589 75
		$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...
590
591
		/** View basics. */
592 75
		$view->settings->update( array(
593 75
			'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...
594
		) );
595
596 75
		self::$cache[ "View::from_post:{$post->ID}" ] = &$view;
597
598 75
		return $view;
599
	}
600
601
	/**
602
	 * Flush the view cache.
603
	 *
604
	 * @param int $view_id The View to reset cache for. Optional. Default: resets everything.
605
	 *
606
	 * @internal
607
	 */
608 86
	public static function _flush_cache( $view_id = null ) {
609 86
		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...
610 79
			unset( self::$cache[ "View::from_post:$view_id" ] );
611 79
			return;
612
		}
613 68
		self::$cache = array();
614 68
	}
615
616
	/**
617
	 * Construct a \GV\View instance from a post ID.
618
	 *
619
	 * @param int|string $post_id The post ID.
620
	 *
621
	 * @api
622
	 * @since 2.0
623
	 * @return \GV\View|null An instance around this \WP_Post or null if not found.
624
	 */
625 36
	public static function by_id( $post_id ) {
626 36
		if ( ! $post_id || ! $post = get_post( $post_id ) ) {
627 3
			return null;
628
		}
629 36
		return self::from_post( $post );
630
	}
631
632
	/**
633
	 * Determines if a view exists to begin with.
634
	 *
635
	 * @param int|\WP_Post|null $view The WordPress post ID, a \WP_Post object or null for global $post;
636
	 *
637
	 * @api
638
	 * @since 2.0
639
	 * @return bool Whether the post exists or not.
640
	 */
641 4
	public static function exists( $view ) {
642 4
		return get_post_type( $view ) == 'gravityview';
643
	}
644
645
	/**
646
	 * ArrayAccess compatibility layer with GravityView_View_Data::$views
647
	 *
648
	 * @internal
649
	 * @deprecated
650
	 * @since 2.0
651
	 * @return bool Whether the offset exists or not, limited to GravityView_View_Data::$views element keys.
652
	 */
653 10
	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...
654 10
		$data_keys = array( 'id', 'view_id', 'form_id', 'template_id', 'atts', 'fields', 'widgets', 'form' );
655 10
		return in_array( $offset, $data_keys );
656
	}
657
658
	/**
659
	 * ArrayAccess compatibility layer with GravityView_View_Data::$views
660
	 *
661
	 * Maps the old keys to the new data;
662
	 *
663
	 * @internal
664
	 * @deprecated
665
	 * @since 2.0
666
	 *
667
	 * @return mixed The value of the requested view data key limited to GravityView_View_Data::$views element keys.
668
	 */
669 10
	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...
670
671 10
		gravityview()->log->notice( 'This is a \GV\View object should not be accessed as an array.' );
672
673 10
		if ( ! isset( $this[ $offset ] ) ) {
674
			return null;
675
		}
676
677 10
		switch ( $offset ) {
678 10
			case 'id':
679 10
			case 'view_id':
680 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...
681 10
			case 'form':
682 10
				return $this->form;
683 1
			case 'form_id':
684 1
				return $this->form ? $this->form->ID : null;
685 1
			case 'atts':
686
				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...
687 1
			case 'template_id':
688 1
				return $this->settings->get( 'template' );
689
			case 'widgets':
690
				return $this->widgets->as_configuration();
691
		}
692
	}
693
694
	/**
695
	 * ArrayAccess compatibility layer with GravityView_View_Data::$views
696
	 *
697
	 * @internal
698
	 * @deprecated
699
	 * @since 2.0
700
	 *
701
	 * @return void
702
	 */
703 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...
704 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.' );
705 1
	}
706
707
	/**
708
	 * ArrayAccess compatibility layer with GravityView_View_Data::$views
709
	 *
710
	 * @internal
711
	 * @deprecated
712
	 * @since 2.0
713
	 * @return void
714
	 */
715 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...
716 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.' );
717 1
	}
718
719
	/**
720
	 * Be compatible with the old data object.
721
	 *
722
	 * Some external code expects an array (doing things like foreach on this, or array_keys)
723
	 *  so let's return an array in the old format for such cases. Do not use unless using
724
	 *  for back-compatibility.
725
	 *
726
	 * @internal
727
	 * @deprecated
728
	 * @since 2.0
729
	 * @return array
730
	 */
731 5
	public function as_data() {
732
		return array(
733 5
			'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...
734 5
			'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...
735 5
			'form_id' => $this->form ? $this->form->ID : null,
736 5
			'form' => $this->form ? gravityview_get_form( $this->form->ID ) : null,
737 5
			'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...
738 5
			'fields' => $this->fields->by_visible()->as_configuration(),
739 5
			'template_id' => $this->settings->get( 'template' ),
740 5
			'widgets' => $this->widgets->as_configuration(),
741
		);
742
	}
743
744
	/**
745
	 * Retrieve the entries for the current view and request.
746
	 *
747
	 * @param \GV\Request The request. Usued for now.
748
	 *
749
	 * @return \GV\Entry_Collection The entries.
750
	 */
751 24
	public function get_entries( $request = null ) {
752 24
		$entries = new \GV\Entry_Collection();
753 24
		if ( $this->form ) {
754
			/**
755
			 * @todo: Stop using _frontend and use something like $request->get_search_criteria() instead
756
			 */
757 24
			$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...
758 24
			$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...
759 24
			$parameters = \GVCommon::calculate_get_entries_criteria( $parameters, $this->form->ID );
760
761 24
			if ( $request instanceof REST\Request ) {
762 3
				$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...
763 3
				$paging_parameters = wp_parse_args( $request->get_paging(), array(
764 3
						'paging' => array( 'page_size' => $atts['page_size'] ),
765
					) );
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...
766 3
				$parameters['paging'] = $paging_parameters['paging'];
767
			}
768
769 24
			$page = Utils::get( $parameters['paging'], 'current_page' ) ?
770 24
				: ( ( ( $parameters['paging']['offset'] - $this->settings->get( 'offset' ) ) / $parameters['paging']['page_size'] ) + 1 );
771
772 24
			if ( gravityview()->plugin->supports( Plugin::FEATURE_GFQUERY ) ) {
773
				/**
774
				 * New \GF_Query stuff :)
775
				 */
776 24
				$query = new \GF_Query( $this->form->ID, $parameters['search_criteria'], $parameters['sorting'] );
777
778 24
				$query->limit( $parameters['paging']['page_size'] )
779 24
					->offset( ( ( $page - 1 ) * $parameters['paging']['page_size'] ) + $this->settings->get( 'offset' ) );
780
781
				/**
782
				 * Any joins?
783
				 */
784 24
				if ( Plugin::FEATURE_JOINS && count( $this->joins ) ) {
785 4
					foreach ( $this->joins as $join ) {
786 4
						$query = $join->as_query_join( $query );
787
					}
788
				}
789
790
				/**
791
				 * @action `gravityview/view/query` Override the \GF_Query before the get() call.
792
				 * @param \GF_Query $query The current query object
793
				 * @param \GV\View $this The current view object
794
				 * @param \GV\Request $request The request object
795
				 */
796 24
				do_action( 'gravityview/view/query', $query, $this, $request );
797
798
				/**
799
				 * Map from Gravity Forms entries arrays to an Entry_Collection.
800
				 */
801 24
				if ( count( $this->joins ) ) {
802 4
					foreach ( $query->get() as $entry ) {
803 4
						$entries->add(
804 4
							Multi_Entry::from_entries( array_map( '\GV\GF_Entry::from_entry', $entry ) )
805
						);
806
					}
807
				} else {
808 20
					array_map( array( $entries, 'add' ), array_map( '\GV\GF_Entry::from_entry', $query->get() ) );
809
				}
810
811
				/**
812
				 * Add total count callback.
813
				 */
814 24
				$entries->add_count_callback( function() use ( $query ) {
815 19
					return $query->total_found;
816 24
				} );
817
			} else {
818
				$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...
819
					->filter( \GV\GF_Entry_Filter::from_search_criteria( $parameters['search_criteria'] ) )
820
					->offset( $this->settings->get( 'offset' ) )
821
					->limit( $parameters['paging']['page_size'] )
822
					->page( $page );
823
824
				if ( ! empty( $parameters['sorting'] ) && ! empty( $parameters['sorting']['key'] ) ) {
825
					$field = new \GV\Field();
826
					$field->ID = $parameters['sorting']['key'];
827
					$direction = strtolower( $parameters['sorting']['direction'] ) == 'asc' ? \GV\Entry_Sort::ASC : \GV\Entry_Sort::DESC;
828
					$entries = $entries->sort( new \GV\Entry_Sort( $field, $direction ) );
829
				}
830
			}
831
		}
832
833
		/**
834
		 * @filter `gravityview/view/entries` Modify the entry fetching filters, sorts, offsets, limits.
835
		 * @param \GV\Entry_Collection $entries The entries for this view.
836
		 * @param \GV\View $view The view.
837
		 * @param \GV\Request $request The request.
838
		 */
839 24
		return apply_filters( 'gravityview/view/entries', $entries, $this, $request );
840
	}
841
842 74
	public function __get( $key ) {
843 74
		if ( $this->post ) {
844 74
			$raw_post = $this->post->filter( 'raw' );
845 74
			return $raw_post->{$key};
846
		}
847
		return isset( $this->{$key} ) ? $this->{$key} : null;
848
	}
849
}
850