GravityView  2.1.1
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  public $views = array();
13 
14  /**
15  *
16  * @param null $passed_post
17  */
18  private function __construct( $passed_post = NULL ) {
19  $this->views = new \GV\View_Collection();
20 
21  if ( ! empty( $passed_post ) ) {
22  $id_or_id_array = $this->maybe_get_view_id( $passed_post );
23  foreach( is_array( $id_or_id_array ) ? $id_or_id_array : array( $id_or_id_array ) as $view_id ) {
24  if ( \GV\View::exists( $view_id ) && ! $this->views->contains( $view_id ) ) {
25  $this->views->add( \GV\View::by_id( $view_id ) );
26  }
27  }
28  }
29 
30  }
31 
32  /**
33  * @deprecated
34  * @see \GV\View_Collection::count
35  * @return boolean
36  */
37  public function has_multiple_views() {
38  return $this->views->count() > 1;
39  }
40 
41 
42  /**
43  * Figure out what the View ID is for a variable, if any.
44  *
45  * Can be:
46  * - WP_Post (Either a `gravityview` post type or not)
47  * - Multi-dimensional array of WP_Post objects
48  * - Array with `view_id` or `id` key(s) set
49  * - String of content that may include GravityView shortcode
50  * - Number representing the Post ID or View ID
51  *
52  * @param mixed $passed_post See method description
53  *
54  * @deprecated
55  * @see \GV\View_Collection::from_post and \GV\Shortcode::parse
56  *
57  * @return int|null|array ID of the View. If there are multiple views in the content, array of IDs parsed.
58  */
59  public function maybe_get_view_id( $passed_post ) {
60  $ids = array();
61 
62  if ( ! empty( $passed_post ) ) {
63 
64  if ( is_numeric( $passed_post ) ) {
65  $passed_post = get_post( $passed_post );
66  }
67 
68  // Convert WP_Posts into WP_Posts[] array
69  if ( $passed_post instanceof WP_Post ) {
70  $passed_post = array( $passed_post );
71  }
72 
73  if ( is_array( $passed_post ) ) {
74 
75  foreach ( $passed_post as &$post ) {
77  foreach ( $views->all() as $view ) {
78  $ids []= $view->ID;
79 
80  /** And as a side-effect... add each view to the global scope. */
81  if ( ! $this->views->contains( $view->ID ) ) {
82  $this->views->add( $view );
83  }
84  }
85  }
86 
87  } else {
88 
89  if ( is_string( $passed_post ) ) {
90  $shortcodes = \GV\Shortcode::parse( $passed_post );
91  foreach ( $shortcodes as $shortcode ) {
92  if ( $shortcode->name == 'gravityview' && !empty( $shortcode->atts['id'] ) ) {
93  $ids []= $shortcode->atts['id'];
94 
95  /** And as a side-effect... add each view to the global scope. */
96  if ( ! $this->views->contains( $shortcode->atts['id'] ) && \GV\View::exists( $shortcode->atts['id'] ) ) {
97  $this->views->add( $shortcode->atts['id'] );
98  }
99  }
100  }
101  } else {
102  $id = $this->get_id_from_atts( $passed_post );
103  $ids[] = intval( $id );
104  }
105  }
106  }
107 
108  if( empty($ids) ) {
109  return NULL;
110  }
111 
112  // If it's just one ID, return that.
113  // Otherwise, return array of IDs
114  return ( sizeof( $ids ) === 1 ) ? $ids[0] : $ids;
115  }
116 
117  /**
118  * @return GravityView_View_Data
119  */
120  public static function getInstance( $passed_post = NULL ) {
121 
122  if( empty( self::$instance ) ) {
123  self::$instance = new GravityView_View_Data( $passed_post );
124  }
125 
126  return self::$instance;
127  }
128 
129  /**
130  * @deprecated
131  * @see \GV\View_Collection::all()
132  */
133  function get_views() {
134  if ( ! $this->views->count() ) {
135  return array();
136  }
137  return array_combine(
138  array_map( function ( $view ) { return $view->ID; }, $this->views->all() ),
139  array_map( function ( $view ) { return $view->as_data(); }, $this->views->all() )
140  );
141  }
142 
143  /**
144  * @deprecated
145  * @see \GV\View_Collection::get()
146  */
147  function get_view( $view_id, $atts = NULL ) {
148  if ( ! $view = $this->views->get( $view_id ) ) {
149  if ( ! \GV\View::exists( $view_id ) ) {
150  return false;
151  }
152 
153  /** Emulate this weird side-effect below... */
154  $view = \GV\View::by_id( $view_id );
155  if ( $atts ) {
156  $view->settings->update( $atts );
157  }
158  $this->views->add( $view );
159  } elseif ( $atts ) {
160  $view->settings->update( $atts );
161  }
162  return $view->as_data();
163  }
164 
165  /**
166  * Determines if a post, identified by the specified ID, exist
167  * within the WordPress database.
168  *
169  * @see http://tommcfarlin.com/wordpress-post-exists-by-id/ Fastest check available
170  * @param int $view_id The ID of the post to check
171  *
172  * @deprecated
173  * @see \GV\View::exists()
174  *
175  * @return bool True if the post exists; otherwise, false.
176  * @since 1.0.0
177  */
178  function view_exists( $view_id ) {
179  return \GV\View::exists( $view_id );
180  }
181 
182  /**
183  *
184  * Add a view to the views array
185  *
186  * @param int|array $view_id View ID or array of View IDs
187  * @param array|string $atts Combine other attributes (eg. from shortcode) with the view settings (optional)
188  *
189  * @deprecated
190  * @see \GV\View_Collection::append
191  *
192  * @return array|false All views if $view_id is array, a view data array if $view_id is an int, false on errors.
193  */
194  function add_view( $view_id, $atts = NULL ) {
196  }
197 
198  /**
199  * Get the visible fields for a View
200  * @uses gravityview_get_directory_fields() Fetch the configured fields for a View
201  * @uses GravityView_View_Data::filter_fields() Only show visible fields
202  * @param int $view_id View ID
203  *
204  * @deprecated
205  * @see \GV\View::$fields
206  *
207  * @return array|null Array of fields as passed by `gravityview_get_directory_fields()`
208  */
209  function get_fields( $view_id ) {
210  if ( \GV\View::exists( $view_id ) ) {
211  $view = \GV\View::by_id( $view_id );
212  return $view->fields->by_visible()->as_configuration();
213  }
214  }
215 
216  /**
217  * Retrieves view ID from an array.
218  *
219  * @param array $atts
220  * @deprecated Dead code, was probably superceded by GravityView_View_Data::parse_post_content
221  *
222  * @return int|null A view ID cast to int, or null.
223  */
224  function get_id_from_atts( $atts ) {
225  $settings = \GV\View_Settings::with_defaults();
226  $settings->update( shortcode_parse_atts( $atts ) );
227  $view_id = $settings->get( 'view_id' );
228  $view_id = empty( $view_id ) ? $settings->get( 'id' ) : $view_id;
229  return empty( $view_id ) ? null : $view_id;
230  }
231 
232  /**
233  * Parse content to determine if there is a GV shortcode to allow for enqueing necessary files in the head.
234  *
235  * @uses gravityview_has_shortcode_r() Check whether shortcode exists (recursively)
236  * @uses shortcode_parse_atts() Parse each GV shortcode
237  * @uses gravityview_get_template_settings() Get the settings for the View ID
238  * @param string $content $post->post_content content
239  *
240  * @deprecated
241  * @see \GV\View_Collection::from_content
242  *
243  * @return int|null|array If a single View is found, the ID of the View. If there are multiple views in the content, array of IDs parsed. If not found, NULL
244  */
245  public function parse_post_content( $content ) {
246  $ids = array();
247  foreach ( \GV\Shortcode::parse( $content ) as $shortcode ) {
248  if ( $shortcode->name == 'gravityview' && is_numeric( $shortcode->atts['id'] ) ) {
249  if ( \GV\View::exists( $shortcode->atts['id'] ) && ! $this->views->contains( $shortcode->atts['id'] ) ) {
250  $this->views->add( \GV\View::by_id( $shortcode->atts['id'] ) );
251  }
252  /**
253  * The original function outputs the ID even though it wasn't added by ::add_view()
254  * Wether this is a bug or not remains a mystery. But we need to emulate this behavior
255  * until better times.
256  */
257  $ids []= $shortcode->atts['id'];
258  }
259  }
260  if ( empty ( $ids ) ) {
261  return null;
262  }
263  return ( sizeof( $ids ) === 1 ) ? $ids[0] : $ids;
264  }
265 
266  /**
267  * Checks if the passed post id has the passed View id embedded.
268  *
269  * Returns
270  *
271  * @since 1.6.1
272  *
273  * @param string $post_id Post ID where the View is embedded
274  * @param string $view_id View ID
275  * @param string $empty_is_valid If either $post_id or $view_id is empty consider valid. Default: false.
276  *
277  * @return bool|WP_Error If valid, returns true. If invalid, returns WP_Error containing error message.
278  */
279  public static function is_valid_embed_id( $post_id = '', $view_id = '', $empty_is_valid = false ) {
280 
281  $message = NULL;
282 
283  // Not invalid if not set!
284  if ( empty( $post_id ) || empty( $view_id ) ) {
285 
286  if( $empty_is_valid ) {
287  return true;
288  }
289 
290  $message = esc_html__( 'The ID is required.', 'gravityview' );
291  }
292 
293  if ( ! $message ) {
294  $status = get_post_status( $post_id );
295 
296  // Nothing exists with that post ID.
297  if ( ! is_numeric( $post_id ) ) {
298  $message = esc_html__( 'You did not enter a number. The value entered should be a number, representing the ID of the post or page the View is embedded on.', 'gravityview' );
299 
300  // @todo Convert to generic article about Embed IDs
301  $message .= ' ' . gravityview_get_link( 'https://docs.gravityview.co/article/222-the-search-widget', __( 'Learn more&hellip;', 'gravityview' ), 'target=_blank' );
302  }
303  }
304 
305  if ( ! $message ) {
306 
307  // Nothing exists with that post ID.
308  if ( empty( $status ) || in_array( $status, array( 'revision', 'attachment' ) ) ) {
309  $message = esc_html__( 'There is no post or page with that ID.', 'gravityview' );
310  }
311 
312  }
313 
314  if ( ! $message && $post = get_post( $post_id ) ) {
316  $view_ids_in_post = array_map( function( $view ) { return $view->ID; }, $views->all() );
317 
318  // The post or page specified does not contain the shortcode.
319  if ( false === in_array( $view_id, (array) $view_ids_in_post ) ) {
320  $message = sprintf( esc_html__( 'The Post ID entered is not valid. You may have entered a post or page that does not contain the selected View. Make sure the post contains the following shortcode: %s', 'gravityview' ), '<br /><code>[gravityview id="' . intval( $view_id ) . '"]</code>' );
321  }
322  }
323 
324  if ( ! $message ) {
325  // It's a View
326  if ( \GV\View::exists( $post_id ) ) {
327  $message = esc_html__( 'The ID is already a View.', 'gravityview' );;
328  }
329  }
330 
331  if ( $message ) {
332  return new WP_Error( 'invalid_embed_id', $message );
333  }
334 
335  return true;
336  }
337 
338  /**
339  * Get a specific default setting
340  * @param string $key The key of the setting array item
341  * @param boolean $with_details Include details
342  * @return mixed|array If using $with_details, return array. Otherwise, mixed.
343  */
344  public static function get_default_arg( $key, $with_details = false ) {
345 
346  $args = \GV\View_Settings::defaults( $with_details );
347 
348  if ( ! isset( $args[ $key ] ) ) {
349  return NULL;
350  }
351 
352  return $args[ $key ];
353  }
354 
355  /**
356  * Retrieve the default args for shortcode and theme function
357  *
358  * @param boolean $with_details True: Return array with full default settings information, including description, name, etc. False: Return an array with only key => value pairs.
359  * @param string $group Only fetch
360  *
361  * @return array $args Associative array of default settings for a View
362  * @param[out] string $label Setting label shown in admin
363  * @param[out] string $type Gravity Forms field type
364  * @param[out] string $group The field group the setting is associated with. Default: "default"
365  * @param[out] mixed $value The default value for the setting
366  * @param[out] string $tooltip Tooltip displayed for the setting
367  * @param[out] boolean $show_in_shortcode Whether to show the setting in the shortcode configuration modal
368  * @param[out] array $options Array of values to use when generating select, multiselect, radio, or checkboxes fields
369  * @param[out] boolean $full_width True: Display the input and label together when rendering. False: Display label and input in separate columns when rendering.
370  *
371  * @deprecated
372  * @see \GV\View_Settings::defaults()
373  */
374  public static function get_default_args( $with_details = false, $group = NULL ) {
375  return \GV\View_Settings::defaults( $with_details, $group );
376  }
377 }
GravityView_View_Data_add_view( $view_id, $atts, $_this)
This file contains mock code for deprecated functions.
Definition: _mocks.php:15
__construct( $passed_post=NULL)
Definition: class-data.php:18
static getInstance( $passed_post=NULL)
Definition: class-data.php:120
static with_defaults( $detailed=false)
Retrieve an instance of the settings with default values.
get_view( $view_id, $atts=NULL)
Definition: class-data.php:147
add_view( $view_id, $atts=NULL)
Add a view to the views array.
Definition: class-data.php:194
view_exists( $view_id)
Determines if a post, identified by the specified ID, exist within the WordPress database.
Definition: class-data.php:178
gravityview_get_link( $href='', $anchor_text='', $atts=array())
Generate an HTML anchor tag with a list of supported attributes.
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:279
If this file is called directly, abort.
Definition: class-data.php:8
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:209
maybe_get_view_id( $passed_post)
Figure out what the View ID is for a variable, if any.
Definition: class-data.php:59
static exists( $view)
Determines if a view exists to begin with.
static by_id( $post_id)
Construct a instance from a post ID.
defaults()
Default settings.
static get_default_args( $with_details=false, $group=NULL)
Retrieve the default args for shortcode and theme function.
Definition: class-data.php:374
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:245
get_id_from_atts( $atts)
Retrieves view ID from an array.
Definition: class-data.php:224
global $post
static parse( $content)
Parse a string of content and figure out which ones there are.
static defaults( $detailed=false, $group=null)
Retrieve the default View settings.
static get_default_arg( $key, $with_details=false)
Get a specific default setting.
Definition: class-data.php:344