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