GravityView  2.9.2
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[in,out] array $meta_keys Array of key values to check. If empty, do not check. Default: empty array
107  * @param[in] \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  if ( function_exists( 'apply_filters_deprecated' ) ) {
113  $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' );
114  } else {
115  /**
116  * @filter `gravityview/data/parse/meta_keys`
117  * @deprecated
118  * @todo Require WP 4.6.0 so that `apply_filters_deprecated` is always available
119  * @see The `gravityview/view_collection/from_post/meta_keys` filter.
120  */
121  $meta_keys = (array) apply_filters( 'gravityview/data/parse/meta_keys', $meta_keys, $post->ID );
122  }
123 
124  /** What about inside post meta values? */
125  foreach ( $meta_keys as $meta_key ) {
126  $views = self::merge_deep( $views, $post->{$meta_key} );
127  }
128  }
129 
130  return $views;
131  }
132 
133  /**
134  * Process meta values when stored singular (string) or multiple (array). Supports nested arrays and JSON strings.
135  *
136  * @since 2.1
137  *
138  * @param \GV\View_Collection $views Existing View Collection to merge with
139  * @param string|array $meta_value Value to parse. Normally the value of $post->{$meta_key}.
140  *
141  * @return \GV\View_Collection $views View Collection containing any additional Views found
142  */
143  private static function merge_deep( $views, $meta_value ) {
144 
145  $meta_value = gv_maybe_json_decode( $meta_value, true );
146 
147  if ( is_array( $meta_value ) ) {
148  foreach ( $meta_value as $index => $item ) {
149  $meta_value[ $index ] = self::merge_deep( $views, $item );
150  }
151  }
152 
153  if ( is_string( $meta_value ) ) {
154  $views->merge( self::from_content( $meta_value ) );
155  }
156 
157  return $views;
158  }
159 
160  /**
161  * Get a list of detected \GV\View objects inside the supplied content.
162  *
163  * The content can have a shortcode, this is the simplest case.
164  *
165  * @param string $content The content to look into.
166  *
167  * @api
168  * @since 2.0
169  * @return \GV\View_Collection A \GV\View_Collection instance containing the views inside the supplied \WP_Post.
170  */
171  public static function from_content( $content ) {
172  $views = new self();
173 
174  /** Let's find us some [gravityview] shortcodes perhaps. */
175  foreach ( Shortcode::parse( $content ) as $shortcode ) {
176  if ( $shortcode->name != 'gravityview' || empty( $shortcode->atts['id'] ) ) {
177  continue;
178  }
179 
180  if ( is_numeric( $shortcode->atts['id'] ) ) {
181  $view = View::by_id( $shortcode->atts['id'] );
182  if ( ! $view ) {
183  continue;
184  }
185 
186  $view->settings->update( $shortcode->atts );
187  $views->add( $view );
188  }
189  }
190 
191  return $views;
192  }
193 }
all()
Get all the settings.
global $post
Definition: edit-entry.php:7
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 .
gravityview()
Definition: _stubs.php:26
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.
If this file is called directly, abort.
add( $view)
Add a to this collection.