-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-stream-wrapper-registry.php
228 lines (214 loc) · 7.22 KB
/
wp-stream-wrapper-registry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* WP Stream Wrapper Registry
*
* This registry keeps track of the scheme and implementation class of each
* registered wrapper. The WordPress registry differs from PHP's native
* registry. The WordPress registry builds on top of PHPs native support and
* adds more functionality. For example, PHP does not currently provide native
* capabilities to ask "what wrapper class is responsible for handling this
* URI or scheme?" This ability is vital to providing functions specific to
* WordPress.
*
* It is important to note that the WP Wrapper Registry does not register PHPs
* built-in wrappers such as 'http' or 'ssl'. These wrapeprs may still be used
* with PHP functions normally, but any functions that rely on the WP Registry
* will fail because the wrapper will not be found. For example, the call
* WP_Stream_Wrapper_Registry::valid_scheme('http') would return false
* despite PHP's built-in http wrapper being available. In most cases this
* will not be an issue because PHP's wrappers do not contain attributes
* specific to WordPress.
*
* @package WP_Stream_Wrappers
* @author Jon Stacey <jon@jonsview.com>
* @copyright 2010 Jon Stacey
* @license http://wordpress.org/about/gpl/
* @link http://github.com/jmstacey/wp-stream-wrappers
* @version 1.0.0
* @since 1.0.0
*/
/**
* Initializes WP Stream Wrapper Registry
*
* Builds the stream wrapper registry. This is a helper function that simply
* makes sure that the registry gets built before use.
*
* @since 1.0.0
*/
function wp_stream_wrapper_registry_init() {
$registry = WP_Stream_Wrapper_Registry::get_registry();
}
/**
* WP Stream Wrapper Registry
*
* Stores information about registered WordPress stream wrappers in
* a single location for easy access and reference.
*
* @package WP_Stream_Wrappers
* @author Jon Stacey <jon@jonsview.com>
* @version 1.0.0
* @since 1.0.0
*/
class WP_Stream_Wrapper_Registry {
/**
* The wrapper registry object
*
* The stream wrapper registry singleton.
*
* @var object
* @access private
*/
private static $registry;
/**
* The wrapper registry
*
* An array containining registered stream wrappers in the following
* format:
*
* $registry[$scheme][$info];
* $scheme is the string version of the scheme (e.g. 'http')
* $info is an array with additional metadata as follows:
* 'name' => 'wrapper name',
* 'class' => 'wrapper implementation class name',
* 'description' => 'brief description of wrapper.'
*
* @var array
* @access private
*/
private static $stream_wrappers = array();
/**
* Initializes the WP_Stream_Wrapper_Regisitry singleton object
*
* Action: register_stream_wrapper
* This action tells stream wrapper plugins that it's time to register
* themselves.
*
* @access private
* @since 1.0.0
*/
private function __construct() {
do_action('register_stream_wrapper');
}
/**
* Gets the entire stream wrapper registry
*
* Returns the entire stream wrapper registry, or initializes a new
* registry if one does not exist.
*
* @return object
* A singleton object for the stream wrapper registry.
*
* @access public
* @static
* @see WP_Stream_Wrapper_Registry::get_stream_wrappers()
* @since 1.0.0
*/
public static function get_registry() {
if (!isset(self::$registry)) {
self::$registry = new WP_Stream_Wrapper_Registry();
}
return self::$registry;
}
/**
* Registers given stream wrapper
*
* Registers the given stream wrapper and populates the stream
* wrapper registry with its metadata.
*
* Here is an example of what wrapper metadata might look like for
* a wrapper implementing the sample scheme (e.g. sample://resource).
* <code>
* $wrapper_metadata = array(
* 'name' => 'Sample wrapper',
* 'class' => 'WP_Stream_Wrapper_Sample',
* 'description' => 'A sample WordPress stream wrapper.'
* );
* </code>
*
* @todo consider tracking WP_Errors in this class for later display
* to admin use in agregate form using admin_notices action hook.
*
* @param string
* String containing the scheme implemented by wrapper (e.g. 'sample').
* @param array
* Stream wrapper metadata array. See array structure example above.
*
* @return mixed
* returns true on success or a WP_Error object on failure.
*
* @access public
* @static
* @link http://www.php.net/manual/en/function.stream-wrapper-unregister.php
* @link http://php.net/manual/en/function.stream-wrapper-register.php
* @see WP_Stream_Wrapper_Registry::unregister_wrapper()
* @since 1.0.0
*/
public static function register_wrapper($scheme, $metadata) {
if (in_array($scheme, stream_get_wrappers(), true)) {
// We override wrappers silently.
stream_wrapper_unregister($scheme);
}
if (stream_wrapper_register($scheme, $metadata['class'])) {
// If PHP accepted the registration, add it to our registry.
self::$stream_wrappers[$scheme] = $metadata;
return true;
} else {
return new WP_Error('stream-wrapper-registration-error', sprintf(__("Unable to register wrapper implementing scheme '%s'"), $scheme));
}
}
/**
* Unregisters given stream wrapper
*
* Unregisters the given stream wrapper and removes its metadata
* from the wrapper registry.
*
* @param string $scheme
* the scheme implemented by the wrapper to unregister.
*
* @return bool
* true on success, or false on failure.
*
* @access public
* @static
* @see WP_Stream_Wrapper_Registry::register_wrapper()
* @since 1.0.0
*/
public static function unregister_wrapper($scheme) {
if (in_array($scheme, stream_get_wrappers(), true)) {
stream_wrapper_unregister($scheme);
unset(self::$stream_wrappers[$scheme]);
return true;
} else {
return false;
}
}
/**
* Gets the stream wrappers registration array
*
* Returns the heart of the stream wrappers registry: the
* array of registerd stream wrapper metadata.
*
* @return array
* An array containing all metadata for registered wrappers.
*
* @access public
* @static
* @see WP_Stream_Wrapper_Registry::register_wrapper()
* @since 1.0.0
*/
public static function get_stream_wrappers() {
return self::$stream_wrappers;
}
/**
* Prevents users from cloning the registry
*
* This registry must remain singular throughout execution. Multiple
* copies of the registry could make things very confusing. Additionally,
* no use cases can currentlybe conceived in which an identical copy of
* the registry would be needed.
*/
public function __clone() {
trigger_error('Cloning the WP Stream Wrapper Registry is not allowed.', E_USER_ERROR);
}
}
?>