GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gv-collection-widget.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\Widget objects.
11  */
13  /**
14  * Add a \GV\Widet to this collection.
15  *
16  * @param \GV\Widget $widget The widget to add to the internal array.
17  *
18  * @api
19  * @since 2.0
20  * @return void
21  */
22  public function add( $widget ) {
23  if ( ! $widget instanceof Widget ) {
24  gravityview()->log->error( 'Widget_Collections can only contain objects of type \GV\Widget.' );
25  return;
26  }
27  parent::add( $widget );
28  }
29 
30  /**
31  * Get a \GV\Widget from this list by UID.
32  *
33  * @param int $widget_uid The UID of the widget in the collection to get.
34  *
35  * @api
36  * @since 2.0
37  *
38  * @return \GV\Widget|null The \GV\Widget with the $widget_uid as the UID, or null if not found.
39  */
40  public function get( $widget_uid ) {
41  foreach ( $this->all() as $widget ) {
42  if ( $widget->UID == $widget_uid ) {
43  return $widget;
44  }
45  }
46  return null;
47  }
48 
49  /**
50  * Get a copy of this \GV\Widget_Collection filtered by position.
51  *
52  * @param string $position The position to get the widgets for.
53  * Can be a wildcard *
54  *
55  * @api
56  * @since
57  *
58  * @return \GV\Widget_Collection A filtered collection of \GV\Widget, filtered by position.
59  */
60  public function by_position( $position ) {
61  $widgets = new self();
62 
63  $search = implode( '.*', array_map( 'preg_quote', explode( '*', $position ) ) );
64 
65  foreach ( $this->all() as $widget ) {
66  if ( preg_match( "#^{$search}$#", $widget->position ) ) {
67  $widgets->add( $widget );
68  }
69  }
70  return $widgets;
71  }
72 
73  /**
74  * Get a copy of this \GV\Widget_Collection filtered by ID.
75  *
76  * @param string $id The IDs to get the widgets for.
77  *
78  * @api
79  * @since
80  *
81  * @return \GV\Widget_Collection A filtered collection of \GV\Widget, filtered by ID.
82  */
83  public function by_id( $id ) {
84  $widgets = new self();
85 
86  foreach ( $this->all() as $widget ) {
87  if ( $id == $widget->get_widget_id() ) {
88  $widgets->add( $widget );
89  }
90  }
91  return $widgets;
92  }
93 
94  /**
95  * Parse a configuration array into a Widget_Collection.
96  *
97  * @param array $configuration The configuration, structured like so:
98  *
99  * array(
100  *
101  * [other zones]
102  *
103  * 'footer_right' => array(
104  *
105  * [other widgets]
106  *
107  * '5372653f25d44' => array(
108  * @see \GV\Widget::as_configuration() for structure
109  * )
110  *
111  * [other widgets]
112  * )
113  *
114  * [other zones]
115  * )
116  *
117  * @return \GV\Widget_Collection A collection of widgets.
118  */
119  public static function from_configuration( $configuration ) {
120  $widgets = new self();
121  foreach ( $configuration as $position => $_widgets ) {
122 
123  if ( empty( $_widgets ) || ! is_array( $_widgets ) ) {
124  continue;
125  }
126 
127  foreach ( $_widgets as $uid => $_configuration ) {
128  if ( ! $widget = Widget::from_configuration( $_configuration ) ) {
129  continue;
130  }
131 
132  $widget->UID = $uid;
133  $widget->position = $position;
134 
135  $widgets->add( $widget );
136  }
137  }
138  return $widgets;
139  }
140 
141  /**
142  * Return a configuration array for this widget collection.
143  *
144  * @return array See \GV\Widget_Collection::from_configuration() for structure.
145  */
146  public function as_configuration() {
147  $configuration = array();
148 
149  /**
150  * @var \GV\Widget $widget
151  */
152  foreach ( $this->all() as $widget ) {
153  if ( empty( $configuration[ $widget->position ] ) ) {
154  $configuration[ $widget->position ] = array();
155  }
156 
157  $configuration[ $widget->position ][ $widget->UID ] = $widget->as_configuration();
158  }
159  return $configuration;
160  }
161 }
If this file is called directly, abort.
If this file is called directly, abort.
static from_configuration( $configuration)
Parse a configuration array into a Widget_Collection.
by_id( $id)
Get a copy of this filtered by ID.
gravityview()
The main GravityView wrapper function.
add( $widget)
Add a to this collection.
by_position( $position)
Get a copy of this filtered by position.
If this file is called directly, abort.