GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gv-collection-view.php
Go to the documentation of this file.
1 <?php
2 namespace GV;
3 
4 /** If this file is called directly, abort. */
5 if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6  die();
7 }
8 
9 /**
10  * A collection of \GV\View objects.
11  */
12 class View_Collection extends Collection {
13 
14  /**
15  * @inheritDoc
16  * @return View[]
17  */
18  public function all() {
19  return parent::all();
20  }
21 
22  /**
23  * Add a \GV\View to this collection.
24  *
25  * @param \GV\View $view The view to add to the internal array.
26  *
27  * @api
28  * @since 2.0
29  * @return void
30  */
31  public function add( $view ) {
32 
33  if ( ! $view instanceof View ) {
34  gravityview()->log->error( 'View_Collections can only contain objects of type \GV\View.' );
35  return;
36  }
37 
38  parent::add( $view );
39  }
40 
41  /**
42  * Get a \GV\View from this list.
43  *
44  * @param int $view_id The ID of the view to get.
45  *
46  * @api
47  * @since 2.0
48  *
49  * @return \GV\View|null The \GV\View with the $view_id as the ID, or null if not found.
50  */
51  public function get( $view_id ) {
52  foreach ( $this->all() as $view ) {
53  if ( $view->ID == $view_id ) {
54  return $view;
55  }
56  }
57  return null;
58  }
59 
60  /**
61  * Check whether \GV\View with an ID is already here.
62  *
63  * @param int $view_id The ID of the view to check.
64  *
65  * @api
66  * @since 2.0
67  *
68  * @return boolean Whether it exists or not.
69  */
70  public function contains( $view_id ) {
71  return ! is_null( $this->get( $view_id ) );
72  }
73 
74  /**
75  * Get a list of \GV\View objects inside the supplied \WP_Post.
76  *
77  * The post can be a gravityview post, which is the simplest case.
78  * The post can contain gravityview shortcodes as well.
79  * The post meta can contain gravityview shortcodes.
80  *
81  * @param \WP_Post $post The \WP_Post object to look into.
82  *
83  * @api
84  * @since 2.0
85  * @return \GV\View_Collection A \GV\View_Collection instance containing the views inside the supplied \WP_Post.
86  */
87  public static function from_post( \WP_Post $post ) {
88  $views = new self();
89 
90  $post_type = get_post_type( $post );
91 
92  if ( 'gravityview' === $post_type ) {
93  /** A straight up gravityview post. */
94  $views->add( View::from_post( $post ) );
95  } else {
96  $views->merge( self::from_content( $post->post_content ) );
97 
98  /**
99  * @filter `gravityview/view_collection/from_post/meta_keys` Define meta keys to parse to check for GravityView shortcode content.
100  *
101  * This is useful when using themes that store content that may contain shortcodes in custom post meta.
102  *
103  * @since 2.0
104  * @since 2.0.7 Added $views parameter, passed by reference
105  *
106  * @param array $meta_keys Array of key values to check. If empty, do not check. Default: empty array
107  * @param \WP_Post $post The post that is being checked
108  * @param \GV\View_Collection $views The current View Collection object, passed as reference
109  */
110  $meta_keys = apply_filters_ref_array( 'gravityview/view_collection/from_post/meta_keys', array( array(), $post, &$views ) );
111 
112  /**
113  * @filter `gravityview/data/parse/meta_keys`
114  * @deprecated
115  * @see The `gravityview/view_collection/from_post/meta_keys` filter.
116  */
117  $meta_keys = (array) apply_filters_deprecated( 'gravityview/data/parse/meta_keys', array( $meta_keys, $post->ID ), '2.0.7', 'gravityview/view_collection/from_post/meta_keys' );
118 
119  /** What about inside post meta values? */
120  foreach ( $meta_keys as $meta_key ) {
121  $views = self::merge_deep( $views, $post->{$meta_key} );
122  }
123  }
124 
125  return $views;
126  }
127 
128  /**
129  * Process meta values when stored singular (string) or multiple (array). Supports nested arrays and JSON strings.
130  *
131  * @since 2.1
132  *
133  * @param \GV\View_Collection $views Existing View Collection to merge with
134  * @param string|array $meta_value Value to parse. Normally the value of $post->{$meta_key}.
135  *
136  * @return \GV\View_Collection $views View Collection containing any additional Views found
137  */
138  private static function merge_deep( $views, $meta_value ) {
139 
140  $meta_value = gv_maybe_json_decode( $meta_value, true );
141 
142  if ( is_array( $meta_value ) ) {
143  foreach ( $meta_value as $index => $item ) {
144  $meta_value[ $index ] = self::merge_deep( $views, $item );
145  }
146  }
147 
148  if ( is_string( $meta_value ) ) {
149  $views->merge( self::from_content( $meta_value ) );
150  }
151 
152  return $views;
153  }
154 
155  /**
156  * Get a list of detected \GV\View objects inside the supplied content.
157  *
158  * The content can have a shortcode, this is the simplest case.
159  *
160  * @param string $content The content to look into.
161  *
162  * @api
163  * @since 2.0
164  * @return \GV\View_Collection A \GV\View_Collection instance containing the views inside the supplied \WP_Post.
165  */
166  public static function from_content( $content ) {
167  $views = new self();
168 
169  /** Let's find us some [gravityview] shortcodes perhaps. */
170  foreach ( Shortcode::parse( $content ) as $shortcode ) {
171  if ( $shortcode->name != 'gravityview' || empty( $shortcode->atts['id'] ) ) {
172  continue;
173  }
174 
175  if ( is_numeric( $shortcode->atts['id'] ) ) {
176  $view = View::by_id( $shortcode->atts['id'] );
177  if ( ! $view ) {
178  continue;
179  }
180 
181  $view->settings->update( $shortcode->atts );
182  $views->add( $view );
183  }
184  }
185 
186  return $views;
187  }
188 }
static merge_deep( $views, $meta_value)
Process meta values when stored singular (string) or multiple (array).
static from_content( $content)
Get a list of detected objects inside the supplied content.
If this file is called directly, abort.
contains( $view_id)
Check whether with an ID is already here.
static from_post(\WP_Post $post)
Get a list of objects inside the supplied .
global $post
Definition: delete-entry.php:7
if(empty( $field_settings['content'])) $content
Definition: custom.php:37
gv_maybe_json_decode( $value, $assoc=false, $depth=512, $options=0)
If content is JSON, decode it.
If this file is called directly, abort.
gravityview()
The main GravityView wrapper function.
If this file is called directly, abort.
add( $view)
Add a to this collection.