Completed
Push — master ( 79386d...2cfa6f )
by Zack
18:13 queued 12:52
created

connector-functions.php ➔ gravityview_get_directory_widgets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Set of functions to separate main plugin from Gravity Forms API and other methods
4
 *
5
 * @package   GravityView
6
 * @license   GPL2+
7
 * @author    Katz Web Services, Inc.
8
 * @link      http://gravityview.co
9
 * @copyright Copyright 2014, Katz Web Services, Inc.
10
 *
11
 * @since 1.0.0
12
 */
13
14
15
16
17
/**
18
 * Returns the form object for a given Form ID.
19
 * @see GVCommon::get_form()
20
 * @access public
21
 * @param mixed $form_id
22
 * @return mixed False: no form ID specified or Gravity Forms isn't active. Array: Form returned from Gravity Forms
23
 */
24
function gravityview_get_form( $form_id ) {
25
	return GVCommon::get_form( $form_id );
26
}
27
28
29
/**
30
 * Get the form array for an entry based only on the entry ID
31
 * @see GVCommon::get_form_from_entry_id
32
 * @param  int|string $entry_slug Entry slug
33
 * @return array           Gravity Forms form array
34
 */
35
function gravityview_get_form_from_entry_id( $entry_slug ) {
36
	return GVCommon::get_form_from_entry_id( $entry_slug );
37
}
38
39
40
/**
41
 * Alias of GFAPI::get_forms()
42
 *
43
 * @see GFAPI::get_forms()
44
 *
45
 * @param bool $active Status of forms. Default: `true`
46
 * @param bool $trash Include forms in trash? Default: `false`
47
 *
48
 * @return array Empty array if GFAPI class isn't available or no forms. Otherwise, the array of Forms
49
 */
50
function gravityview_get_forms( $active = true, $trash = false ) {
51
	return GVCommon::get_forms( $active, $trash );
52
}
53
54
/**
55
 * Return array of fields' id and label, for a given Form ID
56
 *
57
 * @see GVCommon::get_form_fields()
58
 * @access public
59
 * @param string|array $form_id (default: '') or $form object
0 ignored issues
show
Bug introduced by
There is no parameter named $form_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
60
 * @return array
61
 */
62
function gravityview_get_form_fields( $form = '', $add_default_properties = false, $include_parent_field = true ) {
63
	return GVCommon::get_form_fields( $form, $add_default_properties, $include_parent_field );
64
}
65
66
/**
67
 * get extra fields from entry meta
68
 * @param  string $form_id (default: '')
69
 * @return array
70
 */
71
function gravityview_get_entry_meta( $form_id, $only_default_column = true ) {
72
	return GVCommon::get_entry_meta( $form_id, $only_default_column );
73
}
74
75
/**
76
 * Wrapper for the Gravity Forms GFFormsModel::search_lead_ids() method
77
 *
78
 * @see  GFEntryList::leads_page()
79
 * @param  int $form_id ID of the Gravity Forms form
80
 * @since  1.1.6
81
 * @return array          Array of entry IDs
82
 */
83
function gravityview_get_entry_ids( $form_id, $search_criteria = array() ) {
84
	return GVCommon::get_entry_ids( $form_id, $search_criteria );
85
}
86
87
88
/**
89
 * Retrieve entries given search, sort, paging criteria
90
 *
91
 * @see  GFAPI::get_entries()
92
 * @see GFFormsModel::get_field_filters_where()
93
 * @access public
94
 * @param int|array $form_ids The ID of the form or an array IDs of the Forms. Zero for all forms.
95
 * @param mixed $passed_criteria (default: null)
96
 * @param mixed &$total (default: null)
97
 * @return mixed False: Error fetching entries. Array: Multi-dimensional array of Gravity Forms entry arrays
98
 */
99
function gravityview_get_entries( $form_ids = null, $passed_criteria = null, &$total = null ) {
100
	return GVCommon::get_entries( $form_ids, $passed_criteria, $total );
101
}
102
103
/**
104
 * Return a single entry object
105
 *
106
 * Since 1.4, supports custom entry slugs. The way that GravityView fetches an entry based on the custom slug is by searching `gravityview_unique_id` meta. The `$entry_slug` is fetched by getting the current query var set by `is_single_entry()`
107
 *
108
 * @access public
109
 * @param mixed $entry_id
0 ignored issues
show
Bug introduced by
There is no parameter named $entry_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
110
 * @param boolean $force_allow_ids Force the get_entry() method to allow passed entry IDs, even if the `gravityview_custom_entry_slug_allow_id` filter returns false.
111
 * @param boolean $check_entry_display Check whether the entry is visible for the current View configuration. Default: true {@since 1.14}
112
 * @return array|boolean
113
 */
114
function gravityview_get_entry( $entry_slug, $force_allow_ids = false, $check_entry_display = true ) {
115
	return GVCommon::get_entry( $entry_slug, $force_allow_ids, $check_entry_display );
116
}
117
118
/**
119
 * Retrieve the label of a given field id (for a specific form)
120
 *
121
 * @access public
122
 * @param mixed $form
123
 * @param mixed $field_id
124
 * @return string
125
 */
126
function gravityview_get_field_label( $form, $field_id, $field_value = '' ) {
127
	return GVCommon::get_field_label( $form, $field_id, $field_value );
128
}
129
130
131
/**
132
 * Returns the field details array of a specific form given the field id
133
 *
134
 * Alias of GFFormsModel::get_field
135
 *
136
 * @uses GVCommon::get_field
137
 * @see GFFormsModel::get_field
138
 * @access public
139
 * @param array $form
140
 * @param string|int $field_id
141
 * @return GF_Field|null Returns NULL if field with ID $field_id doesn't exist.
142
 */
143
function gravityview_get_field( $form, $field_id ) {
144
	return GVCommon::get_field( $form, $field_id );
145
}
146
147
148
/**
149
 * Check whether the post is GravityView
150
 *
151
 * - Check post type. Is it `gravityview`?
152
 * - Check shortcode
153
 *
154
 * @param  WP_Post      $post WordPress post object
155
 * @return boolean           True: yep, GravityView; No: not!
156
 */
157
function has_gravityview_shortcode( $post = NULL ) {
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
158
	return GVCommon::has_gravityview_shortcode( $post );
159
}
160
161
/**
162
 * Placeholder until the recursive has_shortcode() patch is merged
163
 * @see https://core.trac.wordpress.org/ticket/26343#comment:10
164
 */
165
function gravityview_has_shortcode_r( $content, $tag = 'gravityview' ) {
166
	return GVCommon::has_shortcode_r( $content, $tag );
167
}
168
169
/**
170
 * Get the views for a particular form
171
 * @param  int $form_id Gravity Forms form ID
172
 * @return array          Array with view details
173
 */
174
function gravityview_get_connected_views( $form_id ) {
175
	return GVCommon::get_connected_views( $form_id );
176
}
177
178
/**
179
 * Get the connected form ID from a View ID
180
 *
181
 * @see GVCommon::get_meta_form_id
182
 *
183
 * @param int $view_id ID of the View you want the form of
184
 *
185
 * @return false|string ID of the connected Form, if exists. Empty string if not. False if not the View ID isn't valid.
186
 */
187
function gravityview_get_form_id( $view_id ) {
188
	return GVCommon::get_meta_form_id( $view_id );
189
}
190
191
/**
192
 * Get the template ID (`list`, `table`, `datatables`, `map`) for a View
193
 *
194
 * @see GravityView_Template::template_id
195
 *
196
 * @param int $view_id The ID of the View to get the layout of
0 ignored issues
show
Bug introduced by
There is no parameter named $view_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
197
 *
198
 * @return string GravityView_Template::template_id value. Empty string if not.
199
 */
200
function gravityview_get_template_id( $post_id ) {
201
	return GVCommon::get_meta_template_id( $post_id );
202
}
203
204
/**
205
 * Get all the settings for a View
206
 *
207
 * @uses  GravityView_View_Data::get_default_args() Parses the settings with the plugin defaults as backups.
208
 * @param  int $post_id View ID
209
 * @return array          Associative array of settings with plugin defaults used if not set by the View
210
 */
211
function gravityview_get_template_settings( $post_id ) {
212
	return GVCommon::get_template_settings( $post_id );
213
}
214
215
/**
216
 * Get the setting for a View
217
 *
218
 * If the setting isn't set by the View, it returns the plugin default.
219
 *
220
 * @param  int $post_id View ID
221
 * @param  string $key     Key for the setting
222
 * @return mixed|null          Setting value, or NULL if not set.
223
 */
224
function gravityview_get_template_setting( $post_id, $key ) {
225
	return GVCommon::get_template_setting( $post_id, $key );
226
}
227
228
/**
229
 * Get all available preset templates
230
 * @since 1.13.2
231
 * @return array Templates
232
 */
233
function gravityview_get_registered_templates() {
234
235
	/**
236
	 * @filter `gravityview_register_directory_template` Fetch available View templates
237
	 * @param array $templates Templates to show
238
	 */
239
	$templates = apply_filters( 'gravityview_register_directory_template', array() );
240
241
	return $templates;
242
}
243
244
/**
245
 * Get the field configuration for the View
246
 *
247
 * array(
248
 *
249
 * 	[other zones]
250
 *
251
 * 	'directory_list-title' => array(
252
 *
253
 *   	[other fields]
254
 *
255
 *  	'5372653f25d44' => array(
256
 *  		'id' => string '9' (length=1)
257
 *  		'label' => string 'Screenshots' (length=11)
258
 *			'show_label' => string '1' (length=1)
259
 *			'custom_label' => string '' (length=0)
260
 *			'custom_class' => string 'gv-gallery' (length=10)
261
 * 			'only_loggedin' => string '0' (length=1)
262
 *			'only_loggedin_cap' => string 'read' (length=4)
263
 *  	)
264
 *
265
 * 		[other fields]
266
 *  )
267
 *
268
 * 	[other zones]
269
 * )
270
 *
271
 * @since 1.17.4 Added $apply_filter parameter
272
 *
273
 * @param  int $post_id View ID
274
 * @param  bool $apply_filter Whether to apply the `gravityview/configuration/fields` filter [Default: true]
275
 * @return array          Multi-array of fields with first level being the field zones. See code comment.
276
 */
277
function gravityview_get_directory_fields( $post_id, $apply_filter = true ) {
278
	return GVCommon::get_directory_fields( $post_id, $apply_filter );
279
}
280
281
/**
282
 * Get the widgets, as configured for a View
283
 *
284
 * @since 1.17.4
285
 *
286
 * @param int $post_id
287
 *
288
 * @return array
289
 */
290
function gravityview_get_directory_widgets( $post_id ) {
291
	return get_post_meta( $post_id, '_gravityview_directory_widgets', true );
292
}
293
294
/**
295
 * Set the widgets, as configured for a View
296
 *
297
 * @since 1.17.4
298
 *
299
 * @param int $post_id
300
 * @param array $widgets array of widgets
301
 *
302
 * @return int|bool
303
 */
304
function gravityview_set_directory_widgets( $post_id, $widgets = array() ) {
305
	return update_post_meta( $post_id, '_gravityview_directory_widgets', $widgets );
306
}
307
308
/**
309
 * Render dropdown (select) with the list of sortable fields from a form ID
310
 *
311
 * @access public
312
 * @param  int $formid Form ID
313
 * @param string $current Field ID of field used to sort
314
 * @return string         html
315
 */
316
function gravityview_get_sortable_fields( $formid, $current = '' ) {
317
	return GVCommon::get_sortable_fields( $formid, $current );
318
}
319
320
321
/**
322
 * Returns the GF Form field type for a certain field(id) of a form
323
 * @param  object $form     Gravity Forms form
324
 * @param  mixed $field_id Field ID or Field array
325
 * @return string field type
326
 */
327
function gravityview_get_field_type(  $form = null , $field_id = '' ) {
0 ignored issues
show
introduced by
Expected 0 spaces between argument "$form" and comma; 1 found
Loading history...
328
	return GVCommon::get_field_type(  $form, $field_id );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 2 found
Loading history...
329
}
330
331
332
/**
333
 * Theme function to get a GravityView view
334
 *
335
 * @access public
336
 * @param string $view_id (default: '')
337
 * @param array $atts (default: array())
338
 * @return string HTML of the output. Empty string if $view_id is empty.
339
 */
340
function get_gravityview( $view_id = '', $atts = array() ) {
341
	if( !empty( $view_id ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
342
		$atts['id'] = $view_id;
343
		$args = wp_parse_args( $atts, GravityView_View_Data::get_default_args() );
344
		$GravityView_frontend = GravityView_frontend::getInstance();
345
		$GravityView_frontend->setGvOutputData( GravityView_View_Data::getInstance( $view_id ) );
346
		$GravityView_frontend->set_context_view_id( $view_id );
347
		$GravityView_frontend->set_entry_data();
348
		return $GravityView_frontend->render_view( $args );
349
	}
350
	return '';
351
}
352
353
/**
354
 * Theme function to render a GravityView view
355
 *
356
 * @access public
357
 * @param string $view_id (default: '')
358
 * @param array $atts (default: array())
359
 * @return void
360
 */
361
function the_gravityview( $view_id = '', $atts = array() ) {
362
	echo get_gravityview( $view_id, $atts );
363
}
364
365
366
/**
367
 * Theme function to identify if it is a Single Entry View
368
 *
369
 * @since  1.5.4
370
 * @return bool|string False if not, single entry slug if true
371
 */
372
function gravityview_is_single_entry() {
373
	return GravityView_frontend::is_single_entry();
374
}
375
376
/**
377
 * Determine whether a View has a single checkbox or single radio input
378
 * @see GravityView_frontend::add_scripts_and_styles()
379
 * @since 1.15
380
 * @param array $form Gravity Forms form
381
 * @param array $view_fields GravityView fields array
382
 */
383
function gravityview_view_has_single_checkbox_or_radio( $form, $view_fields ) {
384
385
	if( $form_fields = GFFormsModel::get_fields_by_type( $form, array( 'checkbox', 'radio' ) ) ) {
386
387
		/** @var GF_Field_Radio|GF_Field_Checkbox $form_field */
388
		foreach( $form_fields as $form_field ) {
389
			$field_id = $form_field->id;
390
			foreach( $view_fields as $zone ) {
391
392
				// ACF compatibility; ACF-added fields aren't arrays
393
				if ( ! is_array( $zone ) ) { continue; }
394
395
				foreach( $zone as $field ) {
396
					// If it's an input, not the parent and the parent ID matches a checkbox or radio
397
					if( ( strpos( $field['id'], '.' ) > 0 ) && floor( $field['id'] ) === floor( $field_id ) ) {
398
						return true;
399
					}
400
				}
401
			}
402
		}
403
	}
404
405
	return false;
406
}