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
|
|
|
*/ |
12
|
|
|
class Widget_Collection extends Collection { |
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
|
6 |
|
public function add( $widget ) { |
23
|
6 |
|
if ( ! $widget instanceof Widget ) { |
24
|
|
|
gravityview()->log->error( 'Widget_Collections can only contain objects of type \GV\Widget.' ); |
25
|
|
|
return; |
26
|
|
|
} |
27
|
6 |
|
parent::add( $widget ); |
28
|
6 |
|
} |
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
|
15 |
|
public function by_position( $position ) { |
61
|
15 |
|
$widgets = new self(); |
62
|
|
|
|
63
|
15 |
|
$search = implode( '.*', array_map( 'preg_quote', explode( '*', $position ) ) ); |
64
|
|
|
|
65
|
15 |
|
foreach ( $this->all() as $widget ) { |
66
|
3 |
|
if ( preg_match( "#^{$search}$#", $widget->position ) ) { |
67
|
3 |
|
$widgets->add( $widget ); |
68
|
|
|
} |
69
|
|
|
} |
70
|
15 |
|
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
|
4 |
|
public function by_id( $id ) { |
84
|
4 |
|
$widgets = new self(); |
85
|
|
|
|
86
|
4 |
|
foreach ( $this->all() as $widget ) { |
87
|
4 |
|
if ( $id == $widget->get_widget_id() ) { |
88
|
4 |
|
$widgets->add( $widget ); |
89
|
|
|
} |
90
|
|
|
} |
91
|
4 |
|
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
|
68 |
|
public static function from_configuration( $configuration ) { |
120
|
68 |
|
$widgets = new self(); |
121
|
68 |
|
foreach ( $configuration as $position => $_widgets ) { |
122
|
|
|
|
123
|
6 |
|
if ( empty( $_widgets ) || ! is_array( $_widgets ) ) { |
124
|
|
|
continue; |
125
|
|
|
} |
126
|
|
|
|
127
|
6 |
|
foreach ( $_widgets as $uid => $_configuration ) { |
128
|
6 |
|
if ( ! $widget = Widget::from_configuration( $_configuration ) ) { |
129
|
|
|
continue; |
130
|
|
|
} |
131
|
|
|
|
132
|
6 |
|
$widget->UID = $uid; |
133
|
6 |
|
$widget->position = $position; |
134
|
|
|
|
135
|
6 |
|
$widgets->add( $widget ); |
136
|
|
|
} |
137
|
|
|
} |
138
|
68 |
|
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
|
7 |
|
public function as_configuration() { |
147
|
7 |
|
$configuration = array(); |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @var \GV\Widget $widget |
151
|
|
|
*/ |
152
|
7 |
|
foreach ( $this->all() as $widget ) { |
153
|
3 |
|
if ( empty( $configuration[ $widget->position ] ) ) { |
154
|
3 |
|
$configuration[ $widget->position ] = array(); |
155
|
|
|
} |
156
|
|
|
|
157
|
3 |
|
$configuration[ $widget->position ][ $widget->UID ] = $widget->as_configuration(); |
158
|
|
|
} |
159
|
7 |
|
return $configuration; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.