Completed
Push — develop ( 4708e6...ef8039 )
by Zack
26:21 queued 06:21
created

GravityView_View_Data::get_view()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
nc 9
nop 2
dl 0
loc 22
ccs 0
cts 11
cp 0
crap 42
rs 8.9457
c 0
b 0
f 0
1
<?php
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;
11
12
	public $views = array();
13
14
	/**
15
	 *
16
	 * @param null $passed_post
17
	 */
18 60
	private function __construct( $passed_post = NULL ) {
19 60
		$this->views = new \GV\View_Collection();
20
21 60
		if ( ! empty( $passed_post ) ) {
22 24
			$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 24
			foreach( is_array( $id_or_id_array ) ? $id_or_id_array : array( $id_or_id_array ) as $view_id ) {
24 24
				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 60
	}
31
32
	/**
33
	 * @deprecated
34
	 * @see \GV\View_Collection::count
35
	 * @return boolean
36
	 */
37 25
	public function has_multiple_views() {
38 25
		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 25
	public function maybe_get_view_id( $passed_post ) {
60 25
		$ids = array();
61
62 25
		if ( ! empty( $passed_post ) ) {
63
64 25
			if ( is_numeric( $passed_post ) ) {
65
				$passed_post = get_post( $passed_post );
66
			}
67
68
			// Convert WP_Posts into WP_Posts[] array
69 25
			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 25
				$passed_post = array( $passed_post );
71
			}
72
73 25
			if ( is_array( $passed_post ) ) {
74
75 25
				foreach ( $passed_post as &$post ) {
76 25
					$views = \GV\View_Collection::from_post( $post );
77 25
					foreach ( $views->all() as $view ) {
78 25
						$ids []= $view->ID;
79
80
						/** And as a side-effect... add each view to the global scope. */
81 25
						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 25
							$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'] ) ) {
93 1
							$ids []= $shortcode->atts['id'];
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 25
		if( empty($ids) ) {
109
			return NULL;
110
		}
111
112
		// If it's just one ID, return that.
113
		// Otherwise, return array of IDs
114 25
		return ( sizeof( $ids ) === 1 ) ? $ids[0] : $ids;
115
	}
116
117
	/**
118
	 * @return GravityView_View_Data
119
	 */
120 63
	public static function getInstance( $passed_post = NULL ) {
121
122 63
		if( empty( self::$instance ) ) {
123 60
			self::$instance = new GravityView_View_Data( $passed_post );
124
		}
125
126 63
		return self::$instance;
127
	}
128
129
	/**
130
	 * @deprecated
131
	 * @see \GV\View_Collection::all()
132
	 */
133 23
	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 23
		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 23
		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...
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
163
		if ( ! $view ) {
164
			return false;
165
		}
166
167
		return $view->as_data();
168
	}
169
170
	/**
171
	 * Determines if a post, identified by the specified ID, exist
172
	 * within the WordPress database.
173
	 *
174
	 * @see http://tommcfarlin.com/wordpress-post-exists-by-id/ Fastest check available
175
	 * @param    int    $view_id    The ID of the post to check
176
	 *
177
	 * @deprecated
178 1
	 * @see \GV\View::exists()
179 1
	 *
180
	 * @return   bool   True if the post exists; otherwise, false.
181
	 * @since    1.0.0
182
	 */
183
	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...
184
		return \GV\View::exists( $view_id );
185
	}
186
187
	/**
188
	 *
189
	 * Add a view to the views array
190
	 *
191
	 * @param int|array $view_id View ID or array of View IDs
192
	 * @param array|string $atts Combine other attributes (eg. from shortcode) with the view settings (optional)
193
	 *
194
	 * @deprecated
195
	 * @see \GV\View_Collection::append
196
	 *
197
	 * @return array|false All views if $view_id is array, a view data array if $view_id is an int, false on errors.
198
	 */
199
	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...
200
		return \GV\Mocks\GravityView_View_Data_add_view( $view_id, $atts, $this );
201
	}
202
203
	/**
204
	 * Get the visible fields for a View
205
	 * @uses  gravityview_get_directory_fields() Fetch the configured fields for a View
206
	 * @uses  GravityView_View_Data::filter_fields() Only show visible fields
207
	 * @param  int $view_id View ID
208
	 *
209 1
	 * @deprecated
210 1
	 * @see \GV\View::$fields
211 1
	 *
212 1
	 * @return array|null Array of fields as passed by `gravityview_get_directory_fields()`
213
	 */
214
	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...
215
		if ( \GV\View::exists( $view_id ) ) {
216
			$view = \GV\View::by_id( $view_id );
217
			return $view->fields->by_visible( $view )->as_configuration();
218
		}
219
	}
220
221
	/**
222
	 * Retrieves view ID from an array.
223
	 *
224 1
	 * @param array $atts
225 1
	 * @deprecated Dead code, was probably superceded by GravityView_View_Data::parse_post_content
226 1
	 *
227 1
	 * @return int|null A view ID cast to int, or null.
228 1
	 */
229 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...
230
		$settings = \GV\View_Settings::with_defaults();
231
		$settings->update( shortcode_parse_atts( $atts ) );
232
		$view_id = $settings->get( 'view_id' );
233
		$view_id = empty( $view_id ) ? $settings->get( 'id' ) : $view_id;
234
		return empty( $view_id ) ? null : $view_id;
235
	}
236
237
	/**
238
	 * Parse content to determine if there is a GV shortcode to allow for enqueing necessary files in the head.
239
	 *
240
	 * @uses gravityview_has_shortcode_r() Check whether shortcode exists (recursively)
241
	 * @uses shortcode_parse_atts() Parse each GV shortcode
242
	 * @uses  gravityview_get_template_settings() Get the settings for the View ID
243
	 * @param  string $content $post->post_content content
244
	 *
245 1
	 * @deprecated
246 1
	 * @see \GV\View_Collection::from_content
247 1
	 *
248 1
	 * @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
249 1
	 */
250
	public function parse_post_content( $content ) {
251
		$ids = array();
252
		foreach ( \GV\Shortcode::parse( $content ) as $shortcode ) {
253
			if ( $shortcode->name == 'gravityview' && is_numeric( $shortcode->atts['id'] ) ) {
254
				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...
255
					$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...
256
				}
257 1
				/**
258
				 * The original function outputs the ID even though it wasn't added by ::add_view()
259
				 * Wether this is a bug or not remains a mystery. But we need to emulate this behavior
260 1
				 * until better times.
261
				 */
262
				$ids []= $shortcode->atts['id'];
263 1
			}
264
		}
265
		if ( empty ( $ids ) ) {
266
			return null;
267
		}
268
		return ( sizeof( $ids ) === 1 ) ? $ids[0] : $ids;
269
	}
270
271
	/**
272
	 * Checks if the passed post id has the passed View id embedded.
273
	 *
274
	 * Returns
275
	 *
276
	 * @since 1.6.1
277
	 *
278
	 * @param string $post_id Post ID where the View is embedded
279 1
	 * @param string $view_id View ID
280
	 * @param string $empty_is_valid If either $post_id or $view_id is empty consider valid. Default: false.
281 1
	 *
282
	 * @return bool|WP_Error If valid, returns true. If invalid, returns WP_Error containing error message.
283
	 */
284 1
	public static function is_valid_embed_id( $post_id = '', $view_id = '', $empty_is_valid = false ) {
285
286 1
		$message = NULL;
287 1
288
		// Not invalid if not set!
289
		if ( empty( $post_id ) || empty( $view_id ) ) {
290 1
291
			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...
292
				return true;
293 1
			}
294 1
295
			$message = esc_html__( 'The ID is required.', 'gravityview' );
296
		}
297 1
298
		if ( ! $message ) {
299
			$status = get_post_status( $post_id );
300
301
			// Nothing exists with that post ID.
302
			if ( ! is_numeric( $post_id ) ) {
303
				$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' );
304
305 1
				// @todo Convert to generic article about Embed IDs
306
				$message .= ' ' . gravityview_get_link( 'https://docs.gravityview.co/article/222-the-search-widget', __( 'Learn more&hellip;', 'gravityview' ), 'target=_blank' );
307
			}
308 1
		}
309
310
		if ( ! $message ) {
311
312
			// Nothing exists with that post ID.
313
			if ( empty( $status ) || in_array( $status, array( 'revision', 'attachment' ) ) ) {
314 1
				$message = esc_html__( 'There is no post or page with that ID.', 'gravityview' );
315 1
			}
316
317
		}
318
319 1
		if ( ! $message && $post = get_post( $post_id ) ) {
320 1
			$views = GV\View_Collection::from_post( $post );
321
			$view_ids_in_post = array_map( function( $view ) { return $view->ID; }, $views->all() );
322
323
			// The post or page specified does not contain the shortcode.
324 1
			if ( false === in_array( $view_id, (array) $view_ids_in_post ) ) {
325
				$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>' );
326 1
			}
327
		}
328
329
		if ( ! $message ) {
330
			// It's a View
331 1
			if ( \GV\View::exists( $post_id ) ) {
332 1
				$message = esc_html__( 'The ID is already a View.', 'gravityview' );;
333
			}
334
		}
335 1
336
		if ( $message ) {
337
			return new WP_Error( 'invalid_embed_id', $message );
338
		}
339
340
		return true;
341
	}
342
343
	/**
344 1
	 * Get a specific default setting
345
	 * @param  string  $key          The key of the setting array item
346 1
	 * @param  boolean $with_details Include details
347
	 * @return mixed|array                If using $with_details, return array. Otherwise, mixed.
348 1
	 */
349
	public static function get_default_arg( $key, $with_details = false ) {
350
351
		$args = \GV\View_Settings::defaults( $with_details );
352 1
353
		if ( ! isset( $args[ $key ] ) ) {
354
			return NULL;
355
		}
356
357
		return $args[ $key ];
358
	}
359
360
	/**
361
	 * Retrieve the default args for shortcode and theme function
362
	 *
363
	 * @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.
364
	 * @param string $group Only fetch
365
	 *
366
	 * @return array $args Associative array of default settings for a View
367
	 *      @param[out] string $label Setting label shown in admin
368
	 *      @param[out] string $type Gravity Forms field type
369
	 *      @param[out] string $group The field group the setting is associated with. Default: "default"
370
	 *      @param[out] mixed  $value The default value for the setting
371
	 *      @param[out] string $tooltip Tooltip displayed for the setting
372
	 *      @param[out] boolean $show_in_shortcode Whether to show the setting in the shortcode configuration modal
373
	 *      @param[out] array  $options Array of values to use when generating select, multiselect, radio, or checkboxes fields
374 102
	 *      @param[out] boolean $full_width True: Display the input and label together when rendering. False: Display label and input in separate columns when rendering.
375 102
	 *
376
	 * @deprecated
377
	 * @see \GV\View_Settings::defaults()
378
	 */
379
	public static function get_default_args( $with_details = false, $group = NULL ) {
380
		return \GV\View_Settings::defaults( $with_details, $group );
381
	}
382
}
383