Completed
Push — develop ( 3f13ed...151ba0 )
by Zack
30:06 queued 21:30
created

GravityView_View_Data::has_multiple_views()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 8 and the first side effect is on line 5.

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
3
/** If this file is called directly, abort. */
4
if ( ! defined( 'ABSPATH' ) ) {
5
	die;
6
}
7
8
class GravityView_View_Data {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
9
10
	static $instance = NULL;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $instance.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
11
12
	public $views = array();
13
14
	/**
15
	 *
16
	 * @param null $passed_post
17
	 */
18 48
	private function __construct( $passed_post = NULL ) {
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
19 48
		$this->views = new \GV\View_Collection();
20
21 48
		if ( ! empty( $passed_post ) ) {
22 15
			$id_or_id_array = $this->maybe_get_view_id( $passed_post );
0 ignored issues
show
Deprecated Code introduced by
The method GravityView_View_Data::maybe_get_view_id() has been deprecated.

This method has been deprecated.

Loading history...
23 15
			foreach( is_array( $id_or_id_array ) ? $id_or_id_array : array( $id_or_id_array ) as $view_id ) {
24 15
				if ( \GV\View::exists( $view_id ) && ! $this->views->contains( $view_id ) ) {
25
					$this->views->add( \GV\View::by_id( $view_id ) );
26
				}
27
			}
28
		}
29
30 48
	}
31
32
	/**
33
	 * @deprecated
34
	 * @see \GV\View_Collection::count
35
	 * @return boolean
36
	 */
37 19
	public function has_multiple_views() {
38 19
		return $this->views->count() > 1;
0 ignored issues
show
Bug introduced by
The method count cannot be called on $this->views (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
39
	}
40
41
42
	/**
43
	 * Figure out what the View ID is for a variable, if any.
44
	 *
45
	 * Can be:
46
	 *      - WP_Post (Either a `gravityview` post type or not)
47
	 *      - Multi-dimensional array of WP_Post objects
48
	 *      - Array with `view_id` or `id` key(s) set
49
	 *      - String of content that may include GravityView shortcode
50
	 *      - Number representing the Post ID or View ID
51
	 *
52
	 * @param mixed $passed_post See method description
53
	 *
54
	 * @deprecated
55
	 * @see \GV\View_Collection::from_post and \GV\Shortcode::parse
56
	 *
57
	 * @return int|null|array ID of the View. If there are multiple views in the content, array of IDs parsed.
58
	 */
59 16
	public function maybe_get_view_id( $passed_post ) {
60 16
		$ids = array();
61
62 16
		if ( ! empty( $passed_post ) ) {
63
64 16
			if ( is_numeric( $passed_post ) ) {
65
				$passed_post = get_post( $passed_post );
66
			}
67
68
			// Convert WP_Posts into WP_Posts[] array
69 16
			if ( $passed_post instanceof WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
70 16
				$passed_post = array( $passed_post );
71
			}
72
73 16
			if ( is_array( $passed_post ) ) {
74
75 16
				foreach ( $passed_post as &$post ) {
76 16
					$views = \GV\View_Collection::from_post( $post );
77 16
					foreach ( $views->all() as $view ) {
78 16
						$ids []= $view->ID;
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
79
80
						/** And as a side-effect... add each view to the global scope. */
81 16
						if ( ! $this->views->contains( $view->ID ) ) {
0 ignored issues
show
Bug introduced by
The method contains cannot be called on $this->views (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
82 16
							$this->views->add( $view );
0 ignored issues
show
Bug introduced by
The method add cannot be called on $this->views (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
83
						}
84
					}
85
				}
86
87
			} else {
88
89 1
				if ( is_string( $passed_post ) ) {
90 1
					$shortcodes = \GV\Shortcode::parse( $passed_post );
91 1
					foreach ( $shortcodes as $shortcode ) {
92 1
						if ( $shortcode->name == 'gravityview' && !empty( $shortcode->atts['id'] ) ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
93 1
							$ids []= $shortcode->atts['id'];
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
94
95
							/** And as a side-effect... add each view to the global scope. */
96 1
							if ( ! $this->views->contains( $shortcode->atts['id'] ) && \GV\View::exists( $shortcode->atts['id'] ) ) {
0 ignored issues
show
Bug introduced by
The method contains cannot be called on $this->views (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
97
								$this->views->add( $shortcode->atts['id'] );
0 ignored issues
show
Bug introduced by
The method add cannot be called on $this->views (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
98
							}
99
						}
100
					}
101
				} else {
102
					$id = $this->get_id_from_atts( $passed_post );
0 ignored issues
show
Deprecated Code introduced by
The method GravityView_View_Data::get_id_from_atts() has been deprecated with message: Dead code, was probably superceded by GravityView_View_Data::parse_post_content

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
103
					$ids[] = intval( $id );
104
				}
105
			}
106
		}
107
108 16
		if( empty($ids) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
109
			return NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
110
		}
111
112
		// If it's just one ID, return that.
113
		// Otherwise, return array of IDs
114 16
		return ( sizeof( $ids ) === 1 ) ? $ids[0] : $ids;
115
	}
116
117
	/**
118
	 * @return GravityView_View_Data
119
	 */
120 54
	public static function getInstance( $passed_post = NULL ) {
0 ignored issues
show
Coding Style introduced by
The function name getInstance is in camel caps, but expected get_instance instead as per the coding standard.
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
121
122 54
		if( empty( self::$instance ) ) {
123 48
			self::$instance = new GravityView_View_Data( $passed_post );
124
		}
125
126 54
		return self::$instance;
127
	}
128
129
	/**
130
	 * @deprecated
131
	 * @see \GV\View_Collection::all()
132
	 */
133 14
	function get_views() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
134 14
		if ( ! $this->views->count() ) {
0 ignored issues
show
Bug introduced by
The method count cannot be called on $this->views (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
135
			return array();
136
		}
137 14
		return array_combine(
138
			array_map( function ( $view ) { return $view->ID; }, $this->views->all() ),
0 ignored issues
show
Bug introduced by
The method all cannot be called on $this->views (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
139
			array_map( function ( $view ) { return $view->as_data(); }, $this->views->all() )
0 ignored issues
show
Bug introduced by
The method all cannot be called on $this->views (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
140
		);
141
	}
142
143
	/**
144
	 * @deprecated
145
	 * @see \GV\View_Collection::get()
146
	 */
147
	function get_view( $view_id, $atts = NULL ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
148
		if ( ! $view = $this->views->get( $view_id ) ) {
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->views (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
149
			if ( ! \GV\View::exists( $view_id ) ) {
150
				return false;
151
			}
152
153
			/** Emulate this weird side-effect below... */
154
			$view = \GV\View::by_id( $view_id );
155
			if ( $atts ) {
156
				$view->settings->update( $atts );
157
			}
158
			$this->views->add( $view );
0 ignored issues
show
Bug introduced by
The method add cannot be called on $this->views (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
159
		} elseif ( $atts ) {
160
			$view->settings->update( $atts );
161
		}
162
		return $view->as_data();
163
	}
164
165
	/**
166
	 * Determines if a post, identified by the specified ID, exist
167
	 * within the WordPress database.
168
	 *
169
	 * @see http://tommcfarlin.com/wordpress-post-exists-by-id/ Fastest check available
170
	 * @param    int    $view_id    The ID of the post to check
171
	 *
172
	 * @deprecated
173
	 * @see \GV\View::exists()
174
	 *
175
	 * @return   bool   True if the post exists; otherwise, false.
176
	 * @since    1.0.0
177
	 */
178 1
	function view_exists( $view_id ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
179 1
		return \GV\View::exists( $view_id );
180
	}
181
182
	/**
183
	 *
184
	 * Add a view to the views array
185
	 *
186
	 * @param int|array $view_id View ID or array of View IDs
187
	 * @param array|string $atts Combine other attributes (eg. from shortcode) with the view settings (optional)
188
	 *
189
	 * @deprecated
190
	 * @see \GV\View_Collection::append
191
	 *
192
	 * @return array|false All views if $view_id is array, a view data array if $view_id is an int, false on errors.
193
	 */
194
	function add_view( $view_id, $atts = NULL ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
195
		return \GV\Mocks\GravityView_View_Data_add_view( $view_id, $atts, $this );
196
	}
197
198
	/**
199
	 * Get the visible fields for a View
200
	 * @uses  gravityview_get_directory_fields() Fetch the configured fields for a View
201
	 * @uses  GravityView_View_Data::filter_fields() Only show visible fields
202
	 * @param  int $view_id View ID
203
	 *
204
	 * @deprecated
205
	 * @see \GV\View::$fields
206
	 *
207
	 * @return array|null Array of fields as passed by `gravityview_get_directory_fields()`
208
	 */
209 1
	function get_fields( $view_id ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
210 1
		if ( \GV\View::exists( $view_id ) ) {
211 1
			$view = \GV\View::by_id( $view_id );
212 1
			return $view->fields->by_visible()->as_configuration();
213
		}
214
	}
215
216
	/**
217
	 * Retrieves view ID from an array.
218
	 *
219
	 * @param array $atts
220
	 * @deprecated Dead code, was probably superceded by GravityView_View_Data::parse_post_content
221
	 *
222
	 * @return int|null A view ID cast to int, or null.
223
	 */
224 1
	function get_id_from_atts( $atts ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
225 1
		$settings = \GV\View_Settings::with_defaults();
226 1
		$settings->update( shortcode_parse_atts( $atts ) );
227 1
		$view_id = $settings->get( 'view_id' );
228 1
		$view_id = empty( $view_id ) ? $settings->get( 'id' ) : $view_id;
229 1
		return empty( $view_id ) ? null : $view_id;
230
	}
231
232
	/**
233
	 * Parse content to determine if there is a GV shortcode to allow for enqueing necessary files in the head.
234
	 *
235
	 * @uses gravityview_has_shortcode_r() Check whether shortcode exists (recursively)
236
	 * @uses shortcode_parse_atts() Parse each GV shortcode
237
	 * @uses  gravityview_get_template_settings() Get the settings for the View ID
238
	 * @param  string $content $post->post_content content
239
	 *
240
	 * @deprecated
241
	 * @see \GV\View_Collection::from_content
242
	 *
243
	 * @return int|null|array If a single View is found, the ID of the View. If there are multiple views in the content, array of IDs parsed. If not found, NULL
244
	 */
245 1
	public function parse_post_content( $content ) {
246 1
		$ids = array();
247 1
		foreach ( \GV\Shortcode::parse( $content ) as $shortcode ) {
248 1
			if ( $shortcode->name == 'gravityview' && is_numeric( $shortcode->atts['id'] ) ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
249 1
				if ( \GV\View::exists( $shortcode->atts['id'] ) && ! $this->views->contains( $shortcode->atts['id'] ) ) {
0 ignored issues
show
Bug introduced by
The method contains cannot be called on $this->views (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
250
					$this->views->add( \GV\View::by_id( $shortcode->atts['id'] ) );
0 ignored issues
show
Bug introduced by
The method add cannot be called on $this->views (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
251
				}
252
				/**
253
				 * The original function outputs the ID even though it wasn't added by ::add_view()
254
				 * Wether this is a bug or not remains a mystery. But we need to emulate this behavior
255
				 * until better times.
256
				 */
257 1
				$ids []= $shortcode->atts['id'];
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
258
			}
259
		}
260 1
		if ( empty ( $ids ) ) {
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
261
			return null;
262
		}
263 1
		return ( sizeof( $ids ) === 1 ) ? $ids[0] : $ids;
264
	}
265
266
	/**
267
	 * Checks if the passed post id has the passed View id embedded.
268
	 *
269
	 * Returns
270
	 *
271
	 * @since 1.6.1
272
	 *
273
	 * @param string $post_id Post ID where the View is embedded
274
	 * @param string $view_id View ID
275
	 * @param string $empty_is_valid If either $post_id or $view_id is empty consider valid. Default: false.
276
	 *
277
	 * @return bool|WP_Error If valid, returns true. If invalid, returns WP_Error containing error message.
278
	 */
279 1
	public static function is_valid_embed_id( $post_id = '', $view_id = '', $empty_is_valid = false ) {
280
281 1
		$message = NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
282
283
		// Not invalid if not set!
284 1
		if ( empty( $post_id ) || empty( $view_id ) ) {
285
286 1
			if( $empty_is_valid ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $empty_is_valid of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

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

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
287 1
				return true;
288
			}
289
290 1
			$message = esc_html__( 'The ID is required.', 'gravityview' );
291
		}
292
293 1
		if ( ! $message ) {
294 1
			$status = get_post_status( $post_id );
295
296
			// Nothing exists with that post ID.
297 1
			if ( ! is_numeric( $post_id ) ) {
298
				$message = esc_html__( 'You did not enter a number. The value entered should be a number, representing the ID of the post or page the View is embedded on.', 'gravityview' );
299
300
				// @todo Convert to generic article about Embed IDs
301
				$message .= ' ' . gravityview_get_link( 'https://docs.gravityview.co/article/222-the-search-widget', __( 'Learn more&hellip;', 'gravityview' ), 'target=_blank' );
302
			}
303
		}
304
305 1
		if ( ! $message ) {
306
307
			// Nothing exists with that post ID.
308 1
			if ( empty( $status ) || in_array( $status, array( 'revision', 'attachment' ) ) ) {
309
				$message = esc_html__( 'There is no post or page with that ID.', 'gravityview' );
310
			}
311
312
		}
313
314 1
		if ( ! $message && $post = get_post( $post_id ) ) {
315 1
			$views = GV\View_Collection::from_post( $post );
316
			$view_ids_in_post = array_map( function( $view ) { return $view->ID; }, $views->all() );
317
318
			// The post or page specified does not contain the shortcode.
319 1
			if ( false === in_array( $view_id, (array) $view_ids_in_post ) ) {
320 1
				$message = sprintf( esc_html__( 'The Post ID entered is not valid. You may have entered a post or page that does not contain the selected View. Make sure the post contains the following shortcode: %s', 'gravityview' ), '<br /><code>[gravityview id="' . intval( $view_id ) . '"]</code>' );
321
			}
322
		}
323
324 1
		if ( ! $message ) {
325
			// It's a View
326 1
			if ( \GV\View::exists( $post_id ) ) {
327
				$message = esc_html__( 'The ID is already a View.', 'gravityview' );;
328
			}
329
		}
330
331 1
		if ( $message ) {
332 1
			return new WP_Error( 'invalid_embed_id', $message );
333
		}
334
335 1
		return true;
336
	}
337
338
	/**
339
	 * Get a specific default setting
340
	 * @param  string  $key          The key of the setting array item
341
	 * @param  boolean $with_details Include details
342
	 * @return mixed|array                If using $with_details, return array. Otherwise, mixed.
343
	 */
344 1
	public static function get_default_arg( $key, $with_details = false ) {
345
346 1
		$args = \GV\View_Settings::defaults( $with_details );
347
348 1
		if ( ! isset( $args[ $key ] ) ) {
349
			return NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
350
		}
351
352 1
		return $args[ $key ];
353
	}
354
355
	/**
356
	 * Retrieve the default args for shortcode and theme function
357
	 *
358
	 * @param boolean $with_details True: Return array with full default settings information, including description, name, etc. False: Return an array with only key => value pairs.
359
	 * @param string $group Only fetch
360
	 *
361
	 * @return array $args Associative array of default settings for a View
362
	 *      @param[out] string $label Setting label shown in admin
363
	 *      @param[out] string $type Gravity Forms field type
364
	 *      @param[out] string $group The field group the setting is associated with. Default: "default"
365
	 *      @param[out] mixed  $value The default value for the setting
366
	 *      @param[out] string $tooltip Tooltip displayed for the setting
367
	 *      @param[out] boolean $show_in_shortcode Whether to show the setting in the shortcode configuration modal
368
	 *      @param[out] array  $options Array of values to use when generating select, multiselect, radio, or checkboxes fields
369
	 *      @param[out] boolean $full_width True: Display the input and label together when rendering. False: Display label and input in separate columns when rendering.
370
	 *
371
	 * @deprecated
372
	 * @see \GV\View_Settings::defaults()
373
	 */
374 136
	public static function get_default_args( $with_details = false, $group = NULL ) {
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
375 136
		return \GV\View_Settings::defaults( $with_details, $group );
376
	}
377
}
378