Completed
Push — master ( ebef7a...94d09a )
by Zack
20:55 queued 16:56
created

GravityView_View_Data::add_view()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 77
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 38
nc 7
nop 2
dl 0
loc 77
rs 6.1476
c 0
b 0
f 0

How to fix   Long Method   

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 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
	protected $views = array();
13
14
	/**
15
	 *
16
	 * @param null $passed_post
17
	 */
18
	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
20
		if( !empty( $passed_post ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
21
22
			$id_or_id_array = $this->maybe_get_view_id( $passed_post );
23
24
			if( !empty( $id_or_id_array ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
25
				$this->add_view( $id_or_id_array );
26
			}
27
		}
28
29
	}
30
31
	/**
32
	 * @return boolean
33
	 */
34
	public function has_multiple_views() {
35
36
		//multiple views
37
		return count( $this->get_views() ) > 1 ? true : false;
38
	}
39
40
41
	/**
42
	 * Figure out what the View ID is for a variable, if any.
43
	 *
44
	 * Can be:
45
	 *      - WP_Post (Either a `gravityview` post type or not)
46
	 *      - Multi-dimensional array of WP_Post objects
47
	 *      - Array with `view_id` or `id` key(s) set
48
	 *      - String of content that may include GravityView shortcode
49
	 *      - Number representing the Post ID or View ID
50
	 *
51
	 * @param mixed $passed_post See method description
52
	 *
53
	 * @return int|null|array ID of the View. If there are multiple views in the content, array of IDs parsed.
54
	 */
55
	public function maybe_get_view_id( $passed_post ) {
56
57
		$ids = array();
58
59
		if( ! empty( $passed_post ) ) {
60
61
			if( is_numeric( $passed_post ) ) {
62
				$passed_post = get_post( $passed_post );
63
			}
64
65
			// Convert WP_Posts into WP_Posts[] array
66
			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...
67
				$passed_post = array( $passed_post );
68
			}
69
70
			if( is_array( $passed_post ) ) {
71
72
				foreach ( $passed_post as &$post) {
0 ignored issues
show
introduced by
No space before closing parenthesis is prohibited
Loading history...
73
					if( ( get_post_type( $post ) === 'gravityview' ) ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
74
						$ids[] = $post->ID;
75
					} else{
76
						// Parse the Post Content
77
						$id = $this->parse_post_content( $post->post_content );
78
						if( $id ) {
79
							$ids = array_merge( $ids, (array) $id );
80
						}
81
82
						// Parse the Post Meta
83
						$id = $this->parse_post_meta( $post->ID );
84
						if( $id ) {
85
							$ids = array_merge( $ids, (array) $id );
86
						}
87
					}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
88
89
				}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
90
91
			} else {
92
93
				if ( is_string( $passed_post ) ) {
94
95
					$id = $this->parse_post_content( $passed_post );
96
					if( $id ) {
97
						$ids = array_merge( $ids, (array) $id );
98
					}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
99
100
				} else {
101
					$id = $this->get_id_from_atts( $passed_post );
102
					$ids[] = intval( $id );
103
				}
104
			}
105
		}
106
107
		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...
108
			return NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
109
		}
110
111
		// If it's just one ID, return that.
112
		// Otherwise, return array of IDs
113
		return ( sizeof( $ids ) === 1 ) ? $ids[0] : $ids;
114
	}
115
116
	/**
117
	 * @return GravityView_View_Data
118
	 */
119
	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...
120
121
		if( empty( self::$instance ) ) {
122
			self::$instance = new GravityView_View_Data( $passed_post );
123
		}
124
125
		return self::$instance;
126
	}
127
128
	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...
129
		return $this->views;
130
	}
131
132
	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...
133
134
		if( ! is_numeric( $view_id) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
135
			do_action('gravityview_log_error', sprintf('GravityView_View_Data[get_view] $view_id passed is not numeric.', $view_id) );
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...
136
			return false;
137
		}
138
139
		// Backup: the view hasn't been fetched yet. Doing it now.
140
		if ( ! isset( $this->views[ $view_id ] ) ) {
141
			do_action('gravityview_log_debug', sprintf('GravityView_View_Data[get_view] View #%s not set yet.', $view_id) );
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...
142
			return $this->add_view( $view_id, $atts );
143
		}
144
145
		if ( empty( $this->views[ $view_id ] ) ) {
146
			do_action('gravityview_log_debug', sprintf('GravityView_View_Data[get_view] Returning; View #%s was empty.', $view_id) );
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...
147
			return false;
148
		}
149
150
		return $this->views[ $view_id ];
151
	}
152
153
	/**
154
	 * Determines if a post, identified by the specified ID, exist
155
	 * within the WordPress database.
156
	 *
157
	 * @see http://tommcfarlin.com/wordpress-post-exists-by-id/ Fastest check available
158
	 * @param    int    $view_id    The ID of the post to check
159
	 * @return   bool   True if the post exists; otherwise, false.
160
	 * @since    1.0.0
161
	 */
162
	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...
163
		return is_string( get_post_status( $view_id ) );
164
	}
165
166
	/**
167
	 *
168
	 * Add a view to the views array
169
	 *
170
	 * @param int|array $view_id View ID or array of View IDs
171
	 * @param array|string $atts Combine other attributes (eg. from shortcode) with the view settings (optional)
172
	 * @return array
173
	 */
174
	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...
175
176
		// Handle array of IDs
177
		if( is_array( $view_id ) ) {
178
			foreach( $view_id as $id ) {
179
180
				$this->add_view( $id, $atts );
181
			}
182
183
			return $this->views;
184
		}
185
186
		// The view has been set already; returning stored view.
187
		if ( !empty( $this->views[ $view_id ] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
188
			do_action('gravityview_log_debug', sprintf('GravityView_View_Data[add_view] Returning; View #%s already exists.', $view_id) );
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...
189
			return $this->views[ $view_id ];
190
		}
191
192
		if( ! $this->view_exists( $view_id ) ) {
193
			do_action('gravityview_log_debug', sprintf('GravityView_View_Data[add_view] Returning; View #%s does not exist.', $view_id) );
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...
194
			return false;
195
		}
196
197
		$form_id = gravityview_get_form_id( $view_id );
198
199
		if( empty( $form_id ) ) {
200
201
			do_action('gravityview_log_debug', sprintf('GravityView_View_Data[add_view] Returning; Post ID #%s does not have a connected form.', $view_id) );
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...
202
203
			return false;
204
		}
205
206
		// Get the settings for the View ID
207
		$view_settings = gravityview_get_template_settings( $view_id );
208
209
		do_action('gravityview_log_debug', sprintf('GravityView_View_Data[add_view] Settings pulled in from View #%s', $view_id), $view_settings );
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...
210
211
		// Merge the view settings with the defaults
212
		$view_defaults = wp_parse_args( $view_settings, self::get_default_args() );
213
214
		do_action('gravityview_log_debug', 'GravityView_View_Data[add_view] View Defaults after merging View Settings with the default args.', $view_defaults );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
215
216
		if( ! empty( $atts ) && is_array( $atts ) ) {
217
218
			do_action('gravityview_log_debug', 'GravityView_View_Data[add_view] $atts before merging  with the $view_defaults', $atts );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
219
220
			// Get the settings from the shortcode and merge them with defaults.
221
			$atts = shortcode_atts( $view_defaults, $atts );
222
223
			do_action('gravityview_log_debug', 'GravityView_View_Data[add_view] $atts after merging  with the $view_defaults', $atts );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
224
225
		} else {
226
227
			// If there are no passed $atts, the defaults will be used.
228
			$atts = $view_defaults;
229
230
		}
231
232
		unset( $atts['id'], $view_defaults, $view_settings );
233
234
		$data = array(
235
			'id' => $view_id,
236
			'view_id' => $view_id,
237
			'form_id' => $form_id,
238
			'template_id' => gravityview_get_template_id( $view_id ),
239
			'atts' => $atts,
240
			'fields' => $this->get_fields( $view_id ),
241
			'widgets' => get_post_meta( $view_id, '_gravityview_directory_widgets', true ),
242
			'form' => gravityview_get_form( $form_id ),
243
		);
244
245
		do_action('gravityview_log_debug', sprintf('GravityView_View_Data[add_view] View #%s being added.', $view_id), $data );
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...
246
247
		$this->views[ $view_id ] = $data;
248
249
		return $this->views[ $view_id ];
250
	}
251
252
	/**
253
	 * Get the visible fields for a View
254
	 * @uses  gravityview_get_directory_fields() Fetch the configured fields for a View
255
	 * @uses  GravityView_View_Data::filter_fields() Only show visible fields
256
	 * @param  int $view_id View ID
257
	 * @return array          Array of fields as passed by `gravityview_get_directory_fields()`
258
	 */
259
	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...
260
261
		$dir_fields = gravityview_get_directory_fields( $view_id );
262
		do_action( 'gravityview_log_debug', '[render_view] Fields: ', $dir_fields );
263
264
		// remove fields according to visitor visibility permissions (if logged-in)
265
		$dir_fields = $this->filter_fields( $dir_fields );
266
		do_action( 'gravityview_log_debug', '[render_view] Fields after visibility filter: ', $dir_fields );
267
268
		return $dir_fields;
269
	}
270
271
	/**
272
	 * Filter area fields based on specified conditions
273
	 *
274
	 * @access public
275
	 * @param array $dir_fields
276
	 * @return array
277
	 */
278
	private function filter_fields( $dir_fields ) {
279
280
		if( empty( $dir_fields ) || !is_array( $dir_fields ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
281
			return $dir_fields;
282
		}
283
284
		foreach( $dir_fields as $area => $fields ) {
285
286
			foreach( (array)$fields as $uniqid => $properties ) {
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
287
288
				if( $this->hide_field_check_conditions( $properties ) ) {
289
					unset( $dir_fields[ $area ][ $uniqid ] );
290
				}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
291
292
			}
293
		}
294
295
		return $dir_fields;
296
297
	}
298
299
300
	/**
301
	 * Check whether a certain field should not be presented based on its own properties.
302
	 *
303
	 * @access public
304
	 * @param array $properties
305
	 * @return boolean True: (field should be hidden) or False: (field should be presented)
306
	 */
307
	private function hide_field_check_conditions( $properties ) {
308
309
		// logged-in visibility
310
		if( ! empty( $properties['only_loggedin'] ) && ! GVCommon::has_cap( $properties['only_loggedin_cap'] ) ) {
311
			return true;
312
		}
313
314
		return false;
315
	}
316
317
	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...
318
319
		$atts = is_array( $atts ) ? $atts : shortcode_parse_atts( $atts );
320
321
		// Get the settings from the shortcode and merge them with defaults.
322
		$atts = wp_parse_args( $atts, self::get_default_args() );
323
324
		$view_id = ! empty( $atts['view_id'] ) ? (int)$atts['view_id'] : NULL;
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
325
326
		if( empty( $view_id ) && !empty( $atts['id'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
327
			$view_id = (int)$atts['id'];
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
328
		}
329
330
		if( empty( $view_id ) ) {
331
			do_action('gravityview_log_error', 'GravityView_View_Data[get_id_from_atts] Returning; no ID defined (Atts)', $atts );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
332
			return;
333
		}
334
335
		return $view_id;
336
	}
337
338
	/**
339
	 * Parse content to determine if there is a GV shortcode to allow for enqueing necessary files in the head.
340
	 *
341
	 * @uses gravityview_has_shortcode_r() Check whether shortcode exists (recursively)
342
	 * @uses shortcode_parse_atts() Parse each GV shortcode
343
	 * @uses  gravityview_get_template_settings() Get the settings for the View ID
344
	 * @param  string $content $post->post_content content
345
	 * @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
346
	 */
347
	function parse_post_content( $content ) {
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...
348
349
		/**
350
		 * @hack This is so that the shortcode is registered for the oEmbed preview in the Admin
351
		 * @since 1.6
352
		 */
353
		if( ! shortcode_exists('gravityview') && class_exists( 'GravityView_Shortcode' ) ) {
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...
354
			new GravityView_Shortcode;
355
		}
356
357
		$shortcodes = gravityview_has_shortcode_r( $content, 'gravityview' );
358
359
		if( empty( $shortcodes ) ) {
360
			return NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
361
		}
362
363
		do_action('gravityview_log_debug', 'GravityView_View_Data[parse_post_content] Parsing content, found shortcodes:', $shortcodes );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
364
365
		$ids = array();
366
367
		foreach ($shortcodes as $key => $shortcode) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
368
369
			$args = shortcode_parse_atts( $shortcode[3] );
370
371
			if( empty( $args['id'] ) ) {
372
				do_action('gravityview_log_error', sprintf( 'GravityView_View_Data[parse_post_content] Returning; no ID defined in shortcode atts for Post #%s (Atts)', $post->ID ), $shortcode );
0 ignored issues
show
Bug introduced by
The variable $post does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
373
				continue;
374
			}
375
376
			do_action('gravityview_log_debug', sprintf('GravityView_View_Data[parse_post_content] Adding view #%s with shortcode args', $args['id']), $args );
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...
377
378
			// Store the View to the object for later fetching.
379
			$this->add_view( $args['id'], $args );
380
381
			$ids[] = $args['id'];
382
		}
383
384
		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...
385
			return NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
386
		}
387
388
		// If it's just one ID, return that.
389
		// Otherwise, return array of IDs
390
		return ( sizeof( $ids ) === 1 ) ? $ids[0] : $ids;
391
392
	}
393
394
	/**
395
	 * Parse specific custom fields (Post Meta) to determine if there is a GV shortcode to allow for enqueuing necessary files in the head.
396
	 * @since 1.15.1
397
	 * @uses \GravityView_View_Data::parse_post_content
398
	 * @param int $post_id WP_Post ID
399
	 * @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, or meta not parsed, NULL
400
	 */
401
	private function parse_post_meta( $post_id ) {
402
403
		/**
404
		 * @filter `gravityview/data/parse/meta_keys` Define meta keys to parse to check for GravityView shortcode content
405
		 * This is useful when using themes that store content that may contain shortcodes in custom post meta
406
		 * @param[in,out] array $meta_keys Array of key values to check. If empty, do not check. Default: empty array
407
		 * @param[in] int $post_id ID of the post being checked
408
		 */
409
		$meta_keys = (array)apply_filters( 'gravityview/data/parse/meta_keys', array(), $post_id );
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
410
411
		if( empty( $meta_keys ) ) {
412
			return NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
413
		}
414
415
		do_action( 'gravityview_log_debug', 'GravityView_View_Data[parse_post_meta] Search for GravityView shortcodes on the following custom fields keys:', $meta_keys );
416
417
		$meta_content = '';
418
419
		foreach( $meta_keys as $key ) {
420
			$meta = get_post_meta( $post_id, $key , true );
421
			if( ! is_string( $meta ) ) {
422
				continue;
423
			}
424
			$meta_content .= $meta . ' ';
425
		}
426
427
		if( empty( $meta_content ) ) {
428
			do_action('gravityview_log_error', sprintf( 'GravityView_View_Data[parse_post_meta] Returning; Empty custom fields for Post #%s (Custom fields keys:)', $post_id ), $meta_keys );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
429
			return NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
430
		}
431
432
		do_action( 'gravityview_log_debug', 'GravityView_View_Data[parse_post_meta] Combined content retrieved from custom fields:', $meta_content );
433
434
		return $this->parse_post_content( $meta_content );
435
436
	}
437
438
	/**
439
	 * Checks if the passed post id has the passed View id embedded.
440
	 *
441
	 * Returns
442
	 *
443
	 * @since 1.6.1
444
	 *
445
	 * @param string $post_id Post ID where the View is embedded
446
	 * @param string $view_id View ID
447
	 *
448
	 * @return bool|WP_Error If valid, returns true. If invalid, returns WP_Error containing error message.
449
	 */
450
	public static function is_valid_embed_id( $post_id = '', $view_id = '', $empty_is_valid = true ) {
451
452
		$message = NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
453
454
		// Not invalid if not set!
455
		if( empty( $post_id ) || empty( $view_id ) ) {
456
457
			if( $empty_is_valid ) {
458
				return true;
459
			}
460
461
			$message = esc_html__( 'The ID is required.', 'gravityview' );
462
		}
463
464
		if( ! $message ) {
465
			$status = get_post_status( $post_id );
466
467
			// Nothing exists with that post ID.
468
			if ( ! is_numeric( $post_id ) ) {
469
				$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' );
470
471
				// @todo Convert to generic article about Embed IDs
472
				$message .= ' ' . gravityview_get_link( 'http://docs.gravityview.co/article/222-the-search-widget', __( 'Learn more&hellip;', 'gravityview' ), 'target=_blank' );
473
			}
474
		}
475
476
		if( ! $message ) {
477
478
			// Nothing exists with that post ID.
479
			if ( empty( $status ) || in_array( $status, array( 'revision', 'attachment' ) ) ) {
480
				$message = esc_html__( 'There is no post or page with that ID.', 'gravityview' );
481
			}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
482
483
		}
484
485
		if( ! $message ) {
486
			$view_ids_in_post = GravityView_View_Data::getInstance()->maybe_get_view_id( $post_id );
487
488
			// The post or page specified does not contain the shortcode.
489
			if ( false === in_array( $view_id, (array) $view_ids_in_post ) ) {
490
				$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>' );
491
			}
492
		}
493
494
		if( ! $message ) {
495
496
			// It's a View
497
			if( 'gravityview' === get_post_type( $post_id ) ) {
498
				$message = esc_html__( 'The ID is already a View.', 'gravityview' );;
499
			}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
500
501
		}
502
503
		if( $message ) {
504
			return new WP_Error( 'invalid_embed_id', $message );
505
		}
506
507
		return true;
508
	}
509
510
	/**
511
	 * Get a specific default setting
512
	 * @param  string  $key          The key of the setting array item
513
	 * @param  boolean $with_details Include details
514
	 * @return mixed|array                If using $with_details, return array. Otherwise, mixed.
515
	 */
516
	public static function get_default_arg( $key, $with_details = false ) {
517
518
		$args = self::get_default_args( $with_details );
519
520
		if( !isset( $args[ $key ] ) ) { return NULL; }
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
521
522
		return $args[ $key ];
523
	}
524
525
	/**
526
	 * Retrieve the default args for shortcode and theme function
527
	 *
528
	 * @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.
529
	 * @param string $group Only fetch
530
	 *
531
	 * @return array $args Associative array of default settings for a View
532
	 *      @param[out] string $label Setting label shown in admin
533
	 *      @param[out] string $type Gravity Forms field type
534
	 *      @param[out] string $group The field group the setting is associated with. Default: "default"
535
	 *      @param[out] mixed  $value The default value for the setting
536
	 *      @param[out] string $tooltip Tooltip displayed for the setting
537
	 *      @param[out] boolean $show_in_shortcode Whether to show the setting in the shortcode configuration modal
538
	 *      @param[out] array  $options Array of values to use when generating select, multiselect, radio, or checkboxes fields
539
	 *      @param[out] boolean $full_width True: Display the input and label together when rendering. False: Display label and input in separate columns when rendering.
540
	 */
541
	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...
542
543
		/**
544
		 * @filter `gravityview_default_args` Modify the default settings for new Views
545
		 * @param[in,out] array $default_args Array of default args.
546
		 */
547
		$default_settings = apply_filters( 'gravityview_default_args', array(
548
			'id' => array(
549
				'label' => __('View ID', 'gravityview'),
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...
550
				'type' => 'number',
551
				'group'	=> 'default',
552
				'value' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
553
				'tooltip' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
554
				'show_in_shortcode' => false,
555
			),
556
			'page_size' => array(
557
				'label' 	=> __('Number of entries per page', 'gravityview'),
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...
558
				'type' => 'number',
559
				'class'	=> 'small-text',
560
				'group'	=> 'default',
561
				'value' => 25,
562
				'show_in_shortcode' => true,
563
			),
564
			'lightbox' => array(
565
				'label' => __( 'Enable lightbox for images', 'gravityview' ),
566
				'type' => 'checkbox',
567
				'group'	=> 'default',
568
				'value' => 1,
569
				'tooltip' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
570
				'show_in_shortcode' => true,
571
			),
572
			'show_only_approved' => array(
573
				'label' => __( 'Show only approved entries', 'gravityview' ),
574
				'type' => 'checkbox',
575
				'group'	=> 'default',
576
				'value' => 0,
577
				'show_in_shortcode' => false,
578
			),
579
			'hide_until_searched' => array(
580
				'label' => __( 'Hide View data until search is performed', 'gravityview' ),
581
				'type' => 'checkbox',
582
				'group'	=> 'default',
583
				'tooltip' => __( 'When enabled it will only show any View entries after a search is performed.', 'gravityview' ),
584
				'value' => 0,
585
				'show_in_shortcode' => false,
586
			),
587
			'hide_empty' => array(
588
				'label' 	=> __( 'Hide empty fields', 'gravityview' ),
589
				'group'	=> 'default',
590
				'type'	=> 'checkbox',
591
				'value' => 1,
592
				'show_in_shortcode' => false,
593
			),
594
			'user_edit' => array(
595
				'label'	=> __( 'Allow User Edit', 'gravityview' ),
596
				'group'	=> 'default',
597
				'desc'	=> __('Allow logged-in users to edit entries they created.', 'gravityview'),
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...
598
				'value'	=> 0,
599
				'tooltip' => __('Display "Edit Entry" fields to non-administrator users if they created the entry. Edit Entry fields will always be displayed to site administrators.', 'gravityview'),
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...
600
				'type'	=> 'checkbox',
601
				'show_in_shortcode' => true,
602
			),
603
			'user_delete' => array(
604
				'label'	=> __( 'Allow User Delete', 'gravityview' ),
605
				'group'	=> 'default',
606
				'desc'	=> __('Allow logged-in users to delete entries they created.', 'gravityview'),
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...
607
				'value'	=> 0,
608
				'tooltip' => __('Display "Delete Entry" fields to non-administrator users if they created the entry. Delete Entry fields will always be displayed to site administrators.', 'gravityview'),
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...
609
				'type'	=> 'checkbox',
610
				'show_in_shortcode' => true,
611
			),
612
			'sort_field' => array(
613
				'label'	=> __('Sort by field', 'gravityview'),
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...
614
				'type' => 'select',
615
				'value' => '',
616
				'group'	=> 'sort',
617
				'options' => array(
618
					'' => __( 'Default', 'gravityview'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
619
					'date_created' => __( 'Date Created', 'gravityview'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
620
				),
621
				'show_in_shortcode' => true,
622
			),
623
			'sort_direction' => array(
624
				'label' 	=> __('Sort direction', 'gravityview'),
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...
625
				'type' => 'select',
626
				'value' => 'ASC',
627
				'group'	=> 'sort',
628
				'options' => array(
629
					'ASC' => __('ASC', 'gravityview'),
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...
630
					'DESC' => __('DESC', 'gravityview'),
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...
631
					//'RAND' => __('Random', 'gravityview'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
632
				),
633
				'show_in_shortcode' => true,
634
			),
635
			'sort_columns' => array(
636
				'label' 	=> __( 'Enable sorting by column', 'gravityview' ),
637
				'left_label' => __( 'Column Sorting', 'gravityview' ),
638
				'type' => 'checkbox',
639
				'value' => false,
640
				'group'	=> 'sort',
641
				'tooltip' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
642
				'show_in_shortcode' => true,
643
				'show_in_template' => array( 'default_table' ),
644
			),
645
			'start_date' => array(
646
				'label' 	=> __('Filter by Start Date', 'gravityview'),
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...
647
				'class'	=> 'gv-datepicker',
648
				'desc'	=> __('Show entries submitted after this date. Supports relative dates, such as "-1 week" or "-1 month".', 'gravityview' ),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
649
				'type' => 'text',
650
				'value' => '',
651
				'group'	=> 'filter',
652
				'show_in_shortcode' => true,
653
			),
654
			'end_date' => array(
655
				'label' 	=> __('Filter by End Date', 'gravityview'),
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...
656
				'class'	=> 'gv-datepicker',
657
				'desc'	=> __('Show entries submitted before this date. Supports relative dates, such as "now" or "-3 days".', 'gravityview' ),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
658
				'type' => 'text',
659
				'value' => '',
660
				'group'	=> 'filter',
661
				'show_in_shortcode' => true,
662
			),
663
			'class' => array(
664
				'label' 	=> __('CSS Class', 'gravityview'),
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...
665
				'desc'	=> __('CSS class to add to the wrapping HTML container.', 'gravityview'),
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...
666
				'group'	=> 'default',
667
				'type' => 'text',
668
				'value' => '',
669
				'show_in_shortcode' => false,
670
			),
671
			'search_value' => array(
672
				'label' 	=> __('Search Value', 'gravityview'),
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...
673
				'desc'	=> __('Define a default search value for the View', 'gravityview'),
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...
674
				'type' => 'text',
675
				'value' => '',
676
				'group'	=> 'filter',
677
				'show_in_shortcode' => false,
678
			),
679
			'search_field' => array(
680
				'label' 	=> __('Search Field', 'gravityview'),
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...
681
				'desc'	=> __('If Search Value is set, you can define a specific field to search in. Otherwise, all fields will be searched.', 'gravityview'),
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...
682
				'type' => 'number',
683
				'value' => '',
684
				'group'	=> 'filter',
685
				'show_in_shortcode' => false,
686
			),
687
			'single_title' => array(
688
				'label'	=> __('Single Entry Title', 'gravityview'),
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...
689
				'type'	=> 'text',
690
				'desc'	=> __('When viewing a single entry, change the title of the page to this setting. Otherwise, the title will not change between the Multiple Entries and Single Entry views.', 'gravityview'),
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...
691
				'group'	=> 'default',
692
				'value'	=> '',
693
				'show_in_shortcode' => false,
694
				'full_width' => true,
695
			),
696
			'back_link_label' => array(
697
				'label'	=> __('Back Link Label', 'gravityview'),
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...
698
				'group'	=> 'default',
699
				'desc'	=> __('The text of the link that returns to the multiple entries view.', 'gravityview'),
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...
700
				'type'	=> 'text',
701
				'value'	=> '',
702
				'show_in_shortcode' => false,
703
				'full_width' => true,
704
			),
705
			'embed_only' => array(
706
				'label'	=> __('Prevent Direct Access', 'gravityview'),
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...
707
				'group'	=> 'default',
708
				'desc'	=> __('Only allow access to this View when embedded using the shortcode.', 'gravityview'),
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...
709
				'type'	=> 'checkbox',
710
				'value'	=> '',
711
				'show_in_shortcode' => false,
712
				'full_width' => true,
713
			),
714
		));
715
716
		// By default, we only want the key => value pairing, not the whole array.
717
		if( empty( $with_details ) ) {
718
719
			$defaults = array();
720
721
			foreach( $default_settings as $key => $value ) {
722
				$defaults[ $key ] = $value['value'];
723
			}
724
725
			return $defaults;
726
727
		}
728
		// But sometimes, we want all the details.
729
		else {
730
731
			foreach ($default_settings as $key => $value) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
732
733
				// If the $group argument is set for the method,
734
				// ignore any settings that aren't in that group.
735
				if( !empty( $group ) && is_string( $group ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
736
					if( empty( $value['group'] ) || $value['group'] !== $group ) {
737
						unset( $default_settings[ $key ] );
738
					}
739
				}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
740
741
			}
742
743
			return $default_settings;
744
745
		}
746
	}
747
748
749
}
750