GravityView  1.22.6
The best, easiest way to display Gravity Forms entries on your website.
class-data.php
Go to the documentation of this file.
1 <?php
2 
3 /** If this file is called directly, abort. */
4 if ( ! defined( 'ABSPATH' ) ) {
5  die;
6 }
7 
9 
10  static $instance = NULL;
11 
12  protected $views = array();
13 
14  /**
15  *
16  * @param null $passed_post
17  */
18  private function __construct( $passed_post = NULL ) {
19  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
20  /** Reset the new frontend request views, since we now have duplicate state. */
21  gravityview()->request = new \GV\Dummy_Request();
22  }
23 
24  if( !empty( $passed_post ) ) {
25 
26  $id_or_id_array = $this->maybe_get_view_id( $passed_post );
27 
28  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
29  foreach( is_array( $id_or_id_array ) ? $id_or_id_array : array( $id_or_id_array ) as $view_id ) {
30  if ( \GV\View::exists( $view_id ) && ! gravityview()->views->contains( $view_id ) ) {
31  gravityview()->views->add( \GV\View::by_id( $view_id ) );
32  }
33  }
34  } else if ( ! empty( $id_or_id_array ) ) {
35  $this->add_view( $id_or_id_array );
36  }
37  }
38 
39  }
40 
41  /**
42  * @deprecated
43  * @see \GV\View_Collection::count via `gravityview()->request->views->count()` or `gravityview()->views->count()`
44  * @return boolean
45  */
46  public function has_multiple_views() {
47  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
48  return gravityview()->views->count() > 1;
49  }
50 
51  //multiple views
52  return count( $this->get_views() ) > 1 ? true : false;
53  }
54 
55 
56  /**
57  * Figure out what the View ID is for a variable, if any.
58  *
59  * Can be:
60  * - WP_Post (Either a `gravityview` post type or not)
61  * - Multi-dimensional array of WP_Post objects
62  * - Array with `view_id` or `id` key(s) set
63  * - String of content that may include GravityView shortcode
64  * - Number representing the Post ID or View ID
65  *
66  * @param mixed $passed_post See method description
67  *
68  * @deprecated
69  * @see \GV\View_Collection::from_post and \GV\Shortcode::parse
70  *
71  * @return int|null|array ID of the View. If there are multiple views in the content, array of IDs parsed.
72  */
73  public function maybe_get_view_id( $passed_post ) {
74  $ids = array();
75  if( ! empty( $passed_post ) ) {
76 
77  if( is_numeric( $passed_post ) ) {
78  $passed_post = get_post( $passed_post );
79  }
80 
81  // Convert WP_Posts-like objects into WP_Posts[] array
82  if ( is_object( $passed_post ) && ! array_diff( array( 'ID', 'post_type' ), array_keys( get_object_vars( $passed_post ) ) ) ) {
83  $passed_post = array( $passed_post );
84  }
85 
86  if( is_array( $passed_post ) ) {
87 
88  foreach ( $passed_post as &$post) {
89  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) && is_object( $post ) && property_exists( $post, 'ID' ) ) {
90  $post = get_post( $post ); // Convert into real WP_Post, thanks globals
92  foreach ( $views->all() as $view ) {
93  $ids []= $view->ID;
94 
95  /** And as a side-effect... add each view to the global scope. */
96  if ( ! gravityview()->views->contains( $view->ID ) ) {
97  gravityview()->views->add( $view );
98  }
99  }
100  } else {
101  /** Deprecated, see \GV\View_Collection::from_post */
102  if( ( get_post_type( $post ) === 'gravityview' ) ) {
103  $ids[] = $post->ID;
104  } else{
105  // Parse the Post Content
106  $id = $this->parse_post_content( $post->post_content );
107  if( $id ) {
108  $ids = array_merge( $ids, (array) $id );
109  }
110 
111  // Parse the Post Meta
112  $id = $this->parse_post_meta( $post->ID );
113  if( $id ) {
114  $ids = array_merge( $ids, (array) $id );
115  }
116  }
117  }
118 
119  }
120 
121  } else {
122 
123  if ( is_string( $passed_post ) ) {
124 
125  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
126  $shortcodes = \GV\Shortcode::parse( $passed_post );
127  foreach ( $shortcodes as $shortcode ) {
128  if ( $shortcode->name == 'gravityview' && !empty( $shortcode->atts['id'] ) ) {
129  $ids []= $shortcode->atts['id'];
130 
131  /** And as a side-effect... add each view to the global scope. */
132  if ( ! gravityview()->views->contains( $shortcode->atts['id'] ) && \GV\View::exists( $shortcode->atts['id'] ) ) {
133  gravityview()->views->add( $shortcode->atts['id'] );
134  }
135  }
136  }
137  } else {
138  /** Deprecated, use \GV\Shortcode::parse. */
139  $id = $this->parse_post_content( $passed_post );
140  if( $id ) {
141  $ids = array_merge( $ids, (array) $id );
142  }
143  }
144 
145  } else {
146  $id = $this->get_id_from_atts( $passed_post );
147  $ids[] = intval( $id );
148  }
149  }
150  }
151 
152  if( empty($ids) ) {
153  return NULL;
154  }
155 
156  // If it's just one ID, return that.
157  // Otherwise, return array of IDs
158  return ( sizeof( $ids ) === 1 ) ? $ids[0] : $ids;
159  }
160 
161  /**
162  * @return GravityView_View_Data
163  */
164  public static function getInstance( $passed_post = NULL ) {
165 
166  if( empty( self::$instance ) ) {
167  self::$instance = new GravityView_View_Data( $passed_post );
168  }
169 
170  return self::$instance;
171  }
172 
173  /**
174  * @deprecated
175  * @see \GV\View_Collection::all() via `gravityview()->views` or `gravityview()->request->views`.
176  */
177  function get_views() {
178  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
179  if ( ! gravityview()->views->count() ) {
180  return array();
181  }
182  return array_combine(
183  array_map( function ( $view ) { return $view->ID; }, gravityview()->views->all() ),
184  array_map( function ( $view ) { return $view->as_data(); }, gravityview()->views->all() )
185  );
186  }
187  return $this->views;
188  }
189 
190  /**
191  * @deprecated
192  * @see \GV\View_Collection::get() via `gravityview()->views` or `gravityview()->request->views`.
193  */
194  function get_view( $view_id, $atts = NULL ) {
195  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
196  if ( ! $view = gravityview()->views->get( $view_id ) ) {
197  if ( ! \GV\View::exists( $view_id ) ) {
198  return false;
199  }
200 
201  /** Emulate this weird side-effect below... */
202  $view = \GV\View::by_id( $view_id );
203  if ( $atts ) {
204  $view->settings->update( $atts );
205  }
206  gravityview()->views->add( $view );
207  }
208  return $view->as_data();
209  }
210 
211  if( ! is_numeric( $view_id) ) {
212  do_action('gravityview_log_error', sprintf('GravityView_View_Data[get_view] $view_id passed is not numeric.', $view_id) );
213  return false;
214  }
215 
216  // Backup: the view hasn't been fetched yet. Doing it now.
217  if ( ! isset( $this->views[ $view_id ] ) ) {
218  do_action('gravityview_log_debug', sprintf('GravityView_View_Data[get_view] View #%s not set yet.', $view_id) );
219  return $this->add_view( $view_id, $atts );
220  }
221 
222  if ( empty( $this->views[ $view_id ] ) ) {
223  do_action('gravityview_log_debug', sprintf('GravityView_View_Data[get_view] Returning; View #%s was empty.', $view_id) );
224  return false;
225  }
226 
227  return $this->views[ $view_id ];
228  }
229 
230  /**
231  * Determines if a post, identified by the specified ID, exist
232  * within the WordPress database.
233  *
234  * @see http://tommcfarlin.com/wordpress-post-exists-by-id/ Fastest check available
235  * @param int $view_id The ID of the post to check
236  *
237  * @deprecated
238  * @see \GV\View::exists()
239  *
240  * @return bool True if the post exists; otherwise, false.
241  * @since 1.0.0
242  */
243  function view_exists( $view_id ) {
244  return ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) && \GV\View::exists( $view_id ) ) || is_string( get_post_status( $view_id ) );
245  }
246 
247  /**
248  *
249  * Add a view to the views array
250  *
251  * @param int|array $view_id View ID or array of View IDs
252  * @param array|string $atts Combine other attributes (eg. from shortcode) with the view settings (optional)
253  *
254  * @deprecated
255  * @see \GV\View_Collection::append with the request \GV\View_Collection available via `gravityview()->request->views`
256  * or the `gravityview()->views` shortcut.
257  *
258  * @return array|false All views if $view_id is array, a view data array if $view_id is an int, false on errors.
259  */
260  function add_view( $view_id, $atts = NULL ) {
261 
262  /** Deprecated. Do not edit. */
263  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
265  }
266 
267  // Handle array of IDs
268  if( is_array( $view_id ) ) {
269  foreach( $view_id as $id ) {
270 
271  $this->add_view( $id, $atts );
272  }
273 
274  return $this->get_views();
275  }
276 
277  // The view has been set already; returning stored view.
278  if ( !empty( $this->views[ $view_id ] ) ) {
279  do_action('gravityview_log_debug', sprintf('GravityView_View_Data[add_view] Returning; View #%s already exists.', $view_id) );
280  return $this->views[ $view_id ];
281  }
282 
283  if( ! $this->view_exists( $view_id ) ) {
284  do_action('gravityview_log_debug', sprintf('GravityView_View_Data[add_view] Returning; View #%s does not exist.', $view_id) );
285  return false;
286  }
287 
288  $form_id = gravityview_get_form_id( $view_id );
289 
290  if( empty( $form_id ) ) {
291 
292  do_action('gravityview_log_debug', sprintf('GravityView_View_Data[add_view] Returning; Post ID #%s does not have a connected form.', $view_id) );
293 
294  return false;
295  }
296 
297  // Get the settings for the View ID
299 
300  do_action('gravityview_log_debug', sprintf('GravityView_View_Data[add_view] Settings pulled in from View #%s', $view_id), $view_settings );
301 
302  // Merge the view settings with the defaults
303  $view_defaults = wp_parse_args( $view_settings, defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ? \GV\View_Settings::defaults() : self::get_default_args() );
304 
305  do_action('gravityview_log_debug', 'GravityView_View_Data[add_view] View Defaults after merging View Settings with the default args.', $view_defaults );
306 
307  if( ! empty( $atts ) && is_array( $atts ) ) {
308 
309  do_action('gravityview_log_debug', 'GravityView_View_Data[add_view] $atts before merging with the $view_defaults', $atts );
310 
311  // Get the settings from the shortcode and merge them with defaults.
312  $atts = shortcode_atts( $view_defaults, $atts );
313 
314  do_action('gravityview_log_debug', 'GravityView_View_Data[add_view] $atts after merging with the $view_defaults', $atts );
315 
316  } else {
317 
318  // If there are no passed $atts, the defaults will be used.
319  $atts = $view_defaults;
320 
321  }
322 
323  unset( $atts['id'], $view_defaults, $view_settings );
324 
325  $data = array(
326  'id' => $view_id,
327  'view_id' => $view_id,
328  'form_id' => $form_id,
329  'template_id' => gravityview_get_template_id( $view_id ),
330  'atts' => $atts,
331  'fields' => $this->get_fields( $view_id ),
332  'widgets' => gravityview_get_directory_widgets( $view_id ),
333  'form' => gravityview_get_form( $form_id ),
334  );
335 
336  do_action('gravityview_log_debug', sprintf('GravityView_View_Data[add_view] View #%s being added.', $view_id), $data );
337 
338  $this->views[ $view_id ] = $data;
339 
340  return $this->views[ $view_id ];
341  }
342 
343  /**
344  * Get the visible fields for a View
345  * @uses gravityview_get_directory_fields() Fetch the configured fields for a View
346  * @uses GravityView_View_Data::filter_fields() Only show visible fields
347  * @param int $view_id View ID
348  *
349  * @deprecated
350  * @see \GV\View::$fields
351  *
352  * @return array|null Array of fields as passed by `gravityview_get_directory_fields()`
353  */
354  function get_fields( $view_id ) {
356  do_action( 'gravityview_log_debug', '[render_view] Fields: ', $dir_fields );
357 
358  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
359  if ( \GV\View::exists( $view_id ) ) {
360  $view = \GV\View::by_id( $view_id );
361  return $view->fields->by_visible()->as_configuration();
362  }
363  }
364 
365  // remove fields according to visitor visibility permissions (if logged-in)
366  $dir_fields = $this->filter_fields( $dir_fields );
367  do_action( 'gravityview_log_debug', '[render_view] Fields after visibility filter: ', $dir_fields );
368 
369  return $dir_fields;
370  }
371 
372  /**
373  * Filter area fields based on specified conditions
374  *
375  * @deprecated
376  *
377  * @param array $dir_fields
378  * @return array
379  */
380  private function filter_fields( $dir_fields ) {
381 
382  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
383  throw new Exception( __METHOD__ . ' should not be called anymore. Why was it?' );
384  }
385 
386  if( empty( $dir_fields ) || !is_array( $dir_fields ) ) {
387  return $dir_fields;
388  }
389 
390  foreach( $dir_fields as $area => $fields ) {
391 
392  foreach( (array)$fields as $uniqid => $properties ) {
393 
394  if( $this->hide_field_check_conditions( $properties ) ) {
395  unset( $dir_fields[ $area ][ $uniqid ] );
396  }
397 
398  }
399  }
400 
401  return $dir_fields;
402 
403  }
404 
405 
406  /**
407  * Check whether a certain field should not be presented based on its own properties.
408  *
409  * @deprecated
410  *
411  * @param array $properties
412  * @return boolean True: (field should be hidden) or False: (field should be presented)
413  */
414  private function hide_field_check_conditions( $properties ) {
415 
416  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
417  throw new Exception( __METHOD__ . ' should not be called anymore. Why was it?' );
418  }
419 
420  // logged-in visibility
421  if( ! empty( $properties['only_loggedin'] ) && ! GVCommon::has_cap( $properties['only_loggedin_cap'] ) ) {
422  return true;
423  }
424 
425  return false;
426  }
427 
428  /**
429  * Retrieves view ID from an array.
430  *
431  * @param array $atts
432  * @deprecated Dead code, was probably superceded by GravityView_View_Data::parse_post_content
433  *
434  * @return int|null A view ID cast to int, or null.
435  */
436  function get_id_from_atts( $atts ) {
437 
438  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
439  $settings = new \GV\View_Settings();
440  $settings->update( \GV\View_Settings::defaults() );
441  $settings->update( shortcode_parse_atts( $atts ) );
442  $view_id = $settings->get( 'view_id' );
443  $view_id = empty( $view_id ) ? $settings->get( 'id' ) : $view_id;
444  return empty( $view_id ) ? null : $view_id;
445  }
446 
447  $atts = is_array( $atts ) ? $atts : shortcode_parse_atts( $atts );
448 
449  // Get the settings from the shortcode and merge them with defaults.
450  $atts = wp_parse_args( $atts, defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ? \GV\View_Settings::defaults() : self::get_default_args() );
451 
452  $view_id = ! empty( $atts['view_id'] ) ? (int)$atts['view_id'] : NULL;
453 
454  if( empty( $view_id ) && !empty( $atts['id'] ) ) {
455  $view_id = (int)$atts['id'];
456  }
457 
458  if( empty( $view_id ) ) {
459  do_action('gravityview_log_error', 'GravityView_View_Data[get_id_from_atts] Returning; no ID defined (Atts)', $atts );
460  return;
461  }
462 
463  return $view_id;
464  }
465 
466  /**
467  * Parse content to determine if there is a GV shortcode to allow for enqueing necessary files in the head.
468  *
469  * @uses gravityview_has_shortcode_r() Check whether shortcode exists (recursively)
470  * @uses shortcode_parse_atts() Parse each GV shortcode
471  * @uses gravityview_get_template_settings() Get the settings for the View ID
472  * @param string $content $post->post_content content
473  *
474  * @deprecated
475  * @see \GV\View_Collection::from_content
476  *
477  * @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
478  */
479  public function parse_post_content( $content ) {
480  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
481  $ids = array();
482  foreach ( \GV\Shortcode::parse( $content ) as $shortcode ) {
483  if ( $shortcode->name == 'gravityview' && is_numeric( $shortcode->atts['id'] ) ) {
484  if ( \GV\View::exists( $shortcode->atts['id'] ) && ! gravityview()->views->contains( $shortcode->atts['id'] ) ) {
485  gravityview()->views->add( \GV\View::by_id( $shortcode->atts['id'] ) );
486  }
487  /**
488  * The original function outputs the ID even though it wasn't added by ::add_view()
489  * Wether this is a bug or not remains a mystery. But we need to emulate this behavior
490  * until better times.
491  */
492  $ids []= $shortcode->atts['id'];
493  }
494  }
495  if ( empty ( $ids ) ) {
496  return null;
497  }
498  return ( sizeof( $ids ) === 1 ) ? $ids[0] : $ids;
499  }
500 
501  /**
502  * @hack This is so that the shortcode is registered for the oEmbed preview in the Admin
503  * @since 1.6
504  */
505  if( ! shortcode_exists('gravityview') && class_exists( 'GravityView_Shortcode' ) ) {
507  }
508 
509  $shortcodes = gravityview_has_shortcode_r( $content, 'gravityview' );
510 
511  if( empty( $shortcodes ) ) {
512  return NULL;
513  }
514 
515  do_action('gravityview_log_debug', 'GravityView_View_Data[parse_post_content] Parsing content, found shortcodes:', $shortcodes );
516 
517  $ids = array();
518 
519  foreach ($shortcodes as $key => $shortcode) {
520 
521  $shortcode[3] = htmlspecialchars_decode( $shortcode[3], ENT_QUOTES );
522 
523  $args = shortcode_parse_atts( $shortcode[3] );
524 
525  if( empty( $args['id'] ) ) {
526  do_action('gravityview_log_error', 'GravityView_View_Data[parse_post_content] Returning; no ID defined in shortcode atts', $shortcode );
527  continue;
528  }
529 
530  do_action('gravityview_log_debug', sprintf('GravityView_View_Data[parse_post_content] Adding view #%s with shortcode args', $args['id']), $args );
531 
532  // Store the View to the object for later fetching.
533  $this->add_view( $args['id'], $args );
534 
535  $ids[] = $args['id'];
536  }
537 
538  if( empty($ids) ) {
539  return NULL;
540  }
541 
542  // If it's just one ID, return that.
543  // Otherwise, return array of IDs
544  return ( sizeof( $ids ) === 1 ) ? $ids[0] : $ids;
545 
546  }
547 
548  /**
549  * Parse specific custom fields (Post Meta) to determine if there is a GV shortcode to allow for enqueuing necessary files in the head.
550  * @since 1.15.1
551  *
552  * @deprecated
553  * @see \GV\View_Collection::from_post
554  *
555  * @uses \GravityView_View_Data::parse_post_content
556  * @param int $post_id WP_Post ID
557  * @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
558  */
559  private function parse_post_meta( $post_id ) {
560  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
561  /** Is private and no longer used in future mode. */
562  throw new RuntimeException( __CLASS__ . '::parse_post_meta is no more. Why was it called?' );
563  }
564 
565  /**
566  * @filter `gravityview/data/parse/meta_keys` Define meta keys to parse to check for GravityView shortcode content
567  * This is useful when using themes that store content that may contain shortcodes in custom post meta
568  * @param[in,out] array $meta_keys Array of key values to check. If empty, do not check. Default: empty array
569  * @param[in] int $post_id ID of the post being checked
570  */
571  $meta_keys = (array)apply_filters( 'gravityview/data/parse/meta_keys', array(), $post_id );
572 
573  if( empty( $meta_keys ) ) {
574  return NULL;
575  }
576 
577  do_action( 'gravityview_log_debug', 'GravityView_View_Data[parse_post_meta] Search for GravityView shortcodes on the following custom fields keys:', $meta_keys );
578 
579  $meta_content = '';
580 
581  foreach( $meta_keys as $key ) {
582  $meta = get_post_meta( $post_id, $key , true );
583  if( ! is_string( $meta ) ) {
584  continue;
585  }
586  $meta_content .= $meta . ' ';
587  }
588 
589  if( empty( $meta_content ) ) {
590  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 );
591  return NULL;
592  }
593 
594  do_action( 'gravityview_log_debug', 'GravityView_View_Data[parse_post_meta] Combined content retrieved from custom fields:', $meta_content );
595 
596  return $this->parse_post_content( $meta_content );
597 
598  }
599 
600  /**
601  * Checks if the passed post id has the passed View id embedded.
602  *
603  * Returns
604  *
605  * @since 1.6.1
606  *
607  * @param string $post_id Post ID where the View is embedded
608  * @param string $view_id View ID
609  * @param string $empty_is_valid If either $post_id or $view_id is empty consider valid. Default: false.
610  *
611  * @return bool|WP_Error If valid, returns true. If invalid, returns WP_Error containing error message.
612  */
613  public static function is_valid_embed_id( $post_id = '', $view_id = '', $empty_is_valid = false ) {
614 
615  $message = NULL;
616 
617  // Not invalid if not set!
618  if( empty( $post_id ) || empty( $view_id ) ) {
619 
620  if( $empty_is_valid ) {
621  return true;
622  }
623 
624  $message = esc_html__( 'The ID is required.', 'gravityview' );
625  }
626 
627  if( ! $message ) {
628  $status = get_post_status( $post_id );
629 
630  // Nothing exists with that post ID.
631  if ( ! is_numeric( $post_id ) ) {
632  $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' );
633 
634  // @todo Convert to generic article about Embed IDs
635  $message .= ' ' . gravityview_get_link( 'http://docs.gravityview.co/article/222-the-search-widget', __( 'Learn more&hellip;', 'gravityview' ), 'target=_blank' );
636  }
637  }
638 
639  if( ! $message ) {
640 
641  // Nothing exists with that post ID.
642  if ( empty( $status ) || in_array( $status, array( 'revision', 'attachment' ) ) ) {
643  $message = esc_html__( 'There is no post or page with that ID.', 'gravityview' );
644  }
645 
646  }
647 
648  if( ! $message ) {
649  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) && $post = get_post( $post_id ) ) {
651  $view_ids_in_post = array_map( function( $view ) { return $view->ID; }, $views->all() );
652  } else {
653  /** ::maybe_get_view_id deprecated. */
654  $view_ids_in_post = GravityView_View_Data::getInstance()->maybe_get_view_id( $post_id );
655  }
656 
657  // The post or page specified does not contain the shortcode.
658  if ( false === in_array( $view_id, (array) $view_ids_in_post ) ) {
659  $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>' );
660  }
661  }
662 
663  if( ! $message ) {
664 
665  // It's a View
666  if ( ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) && \GV\View::exists( $post_id ) )
667  || 'gravityview' === get_post_type( $post_id ) ) {
668  $message = esc_html__( 'The ID is already a View.', 'gravityview' );;
669  }
670  }
671 
672  if( $message ) {
673  return new WP_Error( 'invalid_embed_id', $message );
674  }
675 
676  return true;
677  }
678 
679  /**
680  * Get a specific default setting
681  * @param string $key The key of the setting array item
682  * @param boolean $with_details Include details
683  * @return mixed|array If using $with_details, return array. Otherwise, mixed.
684  */
685  public static function get_default_arg( $key, $with_details = false ) {
686 
687  $args = defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ? \GV\View_Settings::defaults( $with_details ) : self::get_default_args( $with_details );
688 
689  if( !isset( $args[ $key ] ) ) { return NULL; }
690 
691  return $args[ $key ];
692  }
693 
694  /**
695  * Retrieve the default args for shortcode and theme function
696  *
697  * @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.
698  * @param string $group Only fetch
699  *
700  * @return array $args Associative array of default settings for a View
701  * @param[out] string $label Setting label shown in admin
702  * @param[out] string $type Gravity Forms field type
703  * @param[out] string $group The field group the setting is associated with. Default: "default"
704  * @param[out] mixed $value The default value for the setting
705  * @param[out] string $tooltip Tooltip displayed for the setting
706  * @param[out] boolean $show_in_shortcode Whether to show the setting in the shortcode configuration modal
707  * @param[out] array $options Array of values to use when generating select, multiselect, radio, or checkboxes fields
708  * @param[out] boolean $full_width True: Display the input and label together when rendering. False: Display label and input in separate columns when rendering.
709  *
710  * @deprecated
711  * @see \GV\View_Settings::defaults()
712  */
713  public static function get_default_args( $with_details = false, $group = NULL ) {
714  if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
715  return \GV\View_Settings::defaults( $with_details, $group );
716  }
717 
718  $default_settings = array(
719  'id' => array(
720  'label' => __('View ID', 'gravityview'),
721  'type' => 'number',
722  'group' => 'default',
723  'value' => NULL,
724  'tooltip' => NULL,
725  'show_in_shortcode' => false,
726  ),
727  'page_size' => array(
728  'label' => __('Number of entries per page', 'gravityview'),
729  'type' => 'number',
730  'class' => 'small-text',
731  'group' => 'default',
732  'value' => 25,
733  'show_in_shortcode' => true,
734  ),
735  'offset' => array(
736  'label' => __('Offset entries starting from', 'gravityview'),
737  'type' => 'number',
738  'class' => 'small-text',
739  'group' => 'default',
740  'value' => 0,
741  'show_in_shortcode' => false,
742  ),
743  'lightbox' => array(
744  'label' => __( 'Enable lightbox for images', 'gravityview' ),
745  'type' => 'checkbox',
746  'group' => 'default',
747  'value' => 1,
748  'tooltip' => NULL,
749  'show_in_shortcode' => true,
750  ),
751  'show_only_approved' => array(
752  'label' => __( 'Show only approved entries', 'gravityview' ),
753  'type' => 'checkbox',
754  'group' => 'default',
755  'value' => 0,
756  'show_in_shortcode' => true,
757  ),
758  'admin_show_all_statuses' => array(
759  'label' => __( 'Show all entries to administrators', 'gravityview' ),
760  'desc' => __('Administrators will be able to see entries with any approval status.', 'gravityview'),
761  'tooltip' => __('Logged-out visitors and non-administrators will only see approved entries, while administrators will see entries with all statuses. This makes it easier for administrators to moderate entries from a View.', 'gravityview'),
762  'requires' => 'show_only_approved',
763  'type' => 'checkbox',
764  'group' => 'default',
765  'value' => 0,
766  'show_in_shortcode' => false,
767  ),
768  'hide_until_searched' => array(
769  'label' => __( 'Hide View data until search is performed', 'gravityview' ),
770  'type' => 'checkbox',
771  'group' => 'default',
772  'tooltip' => __( 'When enabled it will only show any View entries after a search is performed.', 'gravityview' ),
773  'value' => 0,
774  'show_in_shortcode' => false,
775  ),
776  'hide_empty' => array(
777  'label' => __( 'Hide empty fields', 'gravityview' ),
778  'group' => 'default',
779  'type' => 'checkbox',
780  'value' => 1,
781  'show_in_shortcode' => false,
782  ),
783  'user_edit' => array(
784  'label' => __( 'Allow User Edit', 'gravityview' ),
785  'group' => 'default',
786  'desc' => __('Allow logged-in users to edit entries they created.', 'gravityview'),
787  'value' => 0,
788  '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'),
789  'type' => 'checkbox',
790  'show_in_shortcode' => true,
791  ),
792  'user_delete' => array(
793  'label' => __( 'Allow User Delete', 'gravityview' ),
794  'group' => 'default',
795  'desc' => __('Allow logged-in users to delete entries they created.', 'gravityview'),
796  'value' => 0,
797  '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'),
798  'type' => 'checkbox',
799  'show_in_shortcode' => true,
800  ),
801  'sort_field' => array(
802  'label' => __('Sort by field', 'gravityview'),
803  'type' => 'select',
804  'value' => '',
805  'group' => 'sort',
806  'options' => array(
807  '' => __( 'Default', 'gravityview'),
808  'date_created' => __( 'Date Created', 'gravityview'),
809  ),
810  'show_in_shortcode' => true,
811  ),
812  'sort_direction' => array(
813  'label' => __('Sort direction', 'gravityview'),
814  'type' => 'select',
815  'value' => 'ASC',
816  'group' => 'sort',
817  'options' => array(
818  'ASC' => __('ASC', 'gravityview'),
819  'DESC' => __('DESC', 'gravityview'),
820  ),
821  'show_in_shortcode' => true,
822  ),
823  'sort_columns' => array(
824  'label' => __( 'Enable sorting by column', 'gravityview' ),
825  'left_label' => __( 'Column Sorting', 'gravityview' ),
826  'type' => 'checkbox',
827  'value' => false,
828  'group' => 'sort',
829  'tooltip' => NULL,
830  'show_in_shortcode' => true,
831  'show_in_template' => array( 'default_table', 'preset_business_data', 'preset_issue_tracker', 'preset_resume_board', 'preset_job_board' ),
832  ),
833  'start_date' => array(
834  'label' => __('Filter by Start Date', 'gravityview'),
835  'class' => 'gv-datepicker',
836  'desc' => __('Show entries submitted after this date. Supports relative dates, such as "-1 week" or "-1 month".', 'gravityview' ),
837  'type' => 'text',
838  'value' => '',
839  'group' => 'filter',
840  'show_in_shortcode' => true,
841  ),
842  'end_date' => array(
843  'label' => __('Filter by End Date', 'gravityview'),
844  'class' => 'gv-datepicker',
845  'desc' => __('Show entries submitted before this date. Supports relative dates, such as "now" or "-3 days".', 'gravityview' ),
846  'type' => 'text',
847  'value' => '',
848  'group' => 'filter',
849  'show_in_shortcode' => true,
850  ),
851  'class' => array(
852  'label' => __('CSS Class', 'gravityview'),
853  'desc' => __('CSS class to add to the wrapping HTML container.', 'gravityview'),
854  'group' => 'default',
855  'type' => 'text',
856  'value' => '',
857  'show_in_shortcode' => false,
858  ),
859  'search_value' => array(
860  'label' => __('Search Value', 'gravityview'),
861  'desc' => __('Define a default search value for the View', 'gravityview'),
862  'type' => 'text',
863  'value' => '',
864  'group' => 'filter',
865  'show_in_shortcode' => false,
866  ),
867  'search_field' => array(
868  'label' => __('Search Field', 'gravityview'),
869  'desc' => __('If Search Value is set, you can define a specific field to search in. Otherwise, all fields will be searched.', 'gravityview'),
870  'type' => 'number',
871  'value' => '',
872  'group' => 'filter',
873  'show_in_shortcode' => false,
874  ),
875  'single_title' => array(
876  'label' => __('Single Entry Title', 'gravityview'),
877  'type' => 'text',
878  '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'),
879  'group' => 'default',
880  'value' => '',
881  'show_in_shortcode' => false,
882  'full_width' => true,
883  ),
884  'back_link_label' => array(
885  'label' => __('Back Link Label', 'gravityview'),
886  'group' => 'default',
887  'desc' => __('The text of the link that returns to the multiple entries view.', 'gravityview'),
888  'type' => 'text',
889  'value' => '',
890  'show_in_shortcode' => false,
891  'full_width' => true,
892  ),
893  'embed_only' => array(
894  'label' => __('Prevent Direct Access', 'gravityview'),
895  'group' => 'default',
896  'desc' => __('Only allow access to this View when embedded using the shortcode.', 'gravityview'),
897  'type' => 'checkbox',
898  'value' => '',
899  'tooltip' => false,
900  'show_in_shortcode' => false,
901  'full_width' => true,
902  ),
903  'post_id' => array(
904  'type' => 'number',
905  'value' => '',
906  'show_in_shortcode' => false,
907  ),
908  );
909 
910  if ( version_compare( \GFCommon::$version, '2.3-beta-4', '>=' ) ) {
911  $default_settings['sort_direction']['options']['RAND'] = __( 'Random', 'gravityview' );
912  }
913 
914  /**
915  * @filter `gravityview_default_args` Modify the default settings for new Views
916  * @param[in,out] array $default_args Array of default args.
917  * @deprecated
918  * @see filter `gravityview/view/settings/defaults`
919  */
920  $default_settings = apply_filters( 'gravityview_default_args', $default_settings );
921 
922  // By default, we only want the key => value pairing, not the whole array.
923  if( empty( $with_details ) ) {
924 
925  $defaults = array();
926 
927  foreach( $default_settings as $key => $value ) {
928  $defaults[ $key ] = $value['value'];
929  }
930 
931  return $defaults;
932 
933  }
934  // But sometimes, we want all the details.
935  else {
936 
937  foreach ($default_settings as $key => $value) {
938 
939  // If the $group argument is set for the method,
940  // ignore any settings that aren't in that group.
941  if( !empty( $group ) && is_string( $group ) ) {
942  if( empty( $value['group'] ) || $value['group'] !== $group ) {
943  unset( $default_settings[ $key ] );
944  }
945  }
946 
947  }
948 
949  return $default_settings;
950 
951  }
952  }
953 
954 
955 }
__construct( $passed_post=NULL)
Definition: class-data.php:18
static getInstance( $passed_post=NULL)
Definition: class-data.php:164
gravityview_get_directory_fields( $post_id, $apply_filter=true)
Get the field configuration for the View.
get_view( $view_id, $atts=NULL)
Definition: class-data.php:194
add_view( $view_id, $atts=NULL)
Add a view to the views array.
Definition: class-data.php:260
gravityview_get_form( $form_id)
Returns the form object for a given Form ID.
gravityview_get_directory_widgets( $post_id)
Get the widgets, as configured for a View.
gravityview_get_template_settings( $post_id)
Get all the settings for a View.
view_exists( $view_id)
Determines if a post, identified by the specified ID, exist within the WordPress database.
Definition: class-data.php:243
gravityview_get_link( $href='', $anchor_text='', $atts=array())
Generate an HTML anchor tag with a list of supported attributes.
$view_settings
static from_post(\WP_Post $post)
Get a list of objects inside the supplied .
static is_valid_embed_id( $post_id='', $view_id='', $empty_is_valid=false)
Checks if the passed post id has the passed View id embedded.
Definition: class-data.php:613
If this file is called directly, abort.
Definition: class-data.php:8
parse_post_meta( $post_id)
Parse specific custom fields (Post Meta) to determine if there is a GV shortcode to allow for enqueui...
Definition: class-data.php:559
if(empty( $field_settings['content'])) $content
Definition: custom.php:37
get_fields( $view_id)
Get the visible fields for a View gravityview_get_directory_fields() Fetch the configured fields for...
Definition: class-data.php:354
maybe_get_view_id( $passed_post)
Figure out what the View ID is for a variable, if any.
Definition: class-data.php:73
static exists( $view)
Determines if a view exists to begin with.
static by_id( $post_id)
Construct a instance from a post ID.
gravityview_get_form_id( $view_id)
Get the connected form ID from a View ID.
gravityview_has_shortcode_r( $content, $tag='gravityview')
Placeholder until the recursive has_shortcode() patch is merged.
filter_fields( $dir_fields)
Filter area fields based on specified conditions.
Definition: class-data.php:380
static get_default_args( $with_details=false, $group=NULL)
Retrieve the default args for shortcode and theme function.
Definition: class-data.php:713
parse_post_content( $content)
Parse content to determine if there is a GV shortcode to allow for enqueing necessary files in the he...
Definition: class-data.php:479
GravityView_View_Data_add_view( $view_id, $atts)
This file contains mock code for deprecated functions.
Definition: _mocks.php:15
hide_field_check_conditions( $properties)
Check whether a certain field should not be presented based on its own properties.
Definition: class-data.php:414
if(empty( $created_by)) $form_id
get_id_from_atts( $atts)
Retrieves view ID from an array.
Definition: class-data.php:436
global $post
gravityview()
The main GravityView wrapper function.
static parse( $content)
Parse a string of content and figure out which ones there are.
gravityview_get_template_id( $post_id)
Get the template ID (list, table, datatables, map) for a View.
static defaults( $detailed=false, $group=null)
Retrieve the default View settings.
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
static get_default_arg( $key, $with_details=false)
Get a specific default setting.
Definition: class-data.php:685