PluginProbe
Code Snippets / 2.10.0
Code Snippets v2.10.0
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / class-snippet.php

class-snippet.php in Code Snippets 2.10.0, at php/class-snippet.php

337 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * A snippet object
5 *
6 * @since 2.4.0
7 * @package Code_Snippets
8 *
9 * @property int $id The database ID
10 * @property string $name The display name
11 * @property string $desc The formatted description
12 * @property string $code The executable code
13 * @property array $tags An array of the tags
14 * @property int $scope The scope number
15 * @property bool $active The active status
16 * @property bool $network true if is multisite-wide snippet, false if site-wide
17 * @property bool $shared_network Whether the snippet is a shared network snippet
18 *
19 * @property-read array $tags_list The tags in string list format
20 * @property-read string $scope_name The name of the scope
21 * @property-read string $scope_icon The dashicon used to represent the current scope
22 */
23 class Code_Snippet {
24
25 /**
26 * The snippet metadata fields.
27 * Initialized with default values.
28 * @var array
29 */
30 private $fields = array(
31 'id' => 0,
32 'name' => '',
33 'desc' => '',
34 'code' => '',
35 'tags' => array(),
36 'scope' => 'global',
37 'active' => false,
38 'network' => null,
39 'shared_network' => null,
40 );
41
42 private static $field_aliases = array(
43 'description' => 'desc',
44 );
45
46 /**
47 * Constructor function
48 * @param array|object $fields Initial snippet fields
49 */
50 public function __construct( $fields = null ) {
51 $this->set_fields( $fields );
52 }
53
54 /**
55 * Set all of the snippet fields from an array or object.
56 * Invalid fields will be ignored
57 *
58 * @param array|object $fields List of fields
59 */
60 public function set_fields( $fields ) {
61
62 /* Only accept arrays or objects */
63 if ( ! $fields || is_string( $fields ) ) {
64 return;
65 }
66
67 /* Convert objects into arrays */
68 if ( is_object( $fields ) ) {
69 $fields = get_object_vars( $fields );
70 }
71
72 /* Loop through the passed fields and set them */
73 foreach ( $fields as $field => $value ) {
74 $this->set_field( $field, $value );
75 }
76 }
77
78 public function get_fields() {
79 return $this->fields;
80 }
81
82 /**
83 * Internal function for validating the name of a field
84 *
85 * @param string $field A field name
86 *
87 * @return string The validated field name
88 */
89 private function validate_field_name( $field ) {
90
91 /* If a field alias is set, remap it to the valid field name */
92 if ( isset( self::$field_aliases[ $field ] ) ) {
93 return self::$field_aliases[ $field ];
94 }
95
96 return $field;
97 }
98
99 /**
100 * Check if a field is set
101 * @param string $field The field name
102 * @return bool Whether the field is set
103 */
104 public function __isset( $field ) {
105 $field = $this->validate_field_name( $field );
106 return isset( $this->fields[ $field ] ) || method_exists( $this, 'get_' . $field );
107 }
108
109 /**
110 * Retrieve a field's value
111 * @param string $field The field name
112 * @return mixed The field value
113 */
114 public function __get( $field ) {
115 $field = $this->validate_field_name( $field );
116
117 if ( method_exists( $this, 'get_' . $field ) ) {
118 return call_user_func( array( $this, 'get_' . $field ) );
119 }
120
121 return $this->fields[ $field ];
122 }
123
124 /**
125 * Set the value of a field
126 *
127 * @throws ErrorException When an invalid $field is undefined for the class
128 *
129 * @param string $field The field name
130 * @param mixed $value The field value
131 */
132 public function __set( $field, $value ) {
133 $field = $this->validate_field_name( $field );
134
135 if ( ! $this->is_allowed_field( $field ) ) {
136
137 if ( WP_DEBUG ) {
138 trigger_error( 'Trying to set invalid property on Snippets class: ' . esc_html( $field ), E_WARNING );
139 }
140
141 return;
142 }
143
144 /* Check if the field value should be filtered */
145 if ( method_exists( $this, 'prepare_' . $field ) ) {
146 $value = call_user_func( array( $this, 'prepare_' . $field ), $value );
147 }
148
149 $this->fields[ $field ] = $value;
150 }
151
152 /**
153 * Retrieve the list of fields allowed to be written to
154 *
155 * @return array
156 */
157 public function get_allowed_fields() {
158 return array_keys( $this->fields ) + array_keys( self::$field_aliases );
159 }
160
161 /**
162 * Determine whether a field is allowed to be written to
163 *
164 * @param string $field The field name
165 *
166 * @return bool true if the is allowed, false if invalid
167 */
168 public function is_allowed_field( $field ) {
169 return array_key_exists( $field, $this->fields ) || array_key_exists( $field, self::$field_aliases );
170 }
171
172 /**
173 * Safely set the value for a field.
174 * If the field name is invalid, false will be returned instead of an error thrown
175 *
176 * @param string $field The field name
177 * @param mixed $value The field value
178 *
179 * @return bool true if the field was set successfully, false if the field name is invalid
180 */
181 public function set_field( $field, $value ) {
182 if ( ! $this->is_allowed_field( $field ) ) {
183 return false;
184 }
185
186 $this->__set( $field, $value );
187 return true;
188 }
189
190 /**
191 * Prepare the ID by ensuring it is an absolute integer
192 * @param int $id
193 * @return int
194 */
195 private function prepare_id( $id ) {
196 return absint( $id );
197 }
198
199 /**
200 * Prepare the code by removing php tags from beginning and end
201 * @param string $code
202 * @return string
203 */
204 private function prepare_code( $code ) {
205
206 /* Remove <?php and <? from beginning of snippet */
207 $code = preg_replace( '|^[\s]*<\?(php)?|', '', $code );
208
209 /* Remove ?> from end of snippet */
210 $code = preg_replace( '|\?>[\s]*$|', '', $code );
211
212 return $code;
213 }
214
215 /**
216 * Prepare the scope by ensuring that it is a valid number
217 * @param int $scope The field as provided
218 * @return int The field in the correct format
219 */
220 private function prepare_scope( $scope ) {
221 $scopes = self::get_all_scopes();
222
223 if ( in_array( $scope, $scopes ) ) {
224 return $scope;
225 }
226
227 if ( is_numeric( $scope ) && isset( $scopes[ $scope ] ) ) {
228 return $scopes[ $scope ];
229 }
230
231 return $this->fields['scope'];
232 }
233
234 /**
235 * Prepare the snippet tags by ensuring they are in the correct format
236 * @param string|array $tags The tags as provided
237 * @return array The tags as an array
238 */
239 private function prepare_tags( $tags ) {
240 return code_snippets_build_tags_array( $tags );
241 }
242
243 /**
244 * Prepare the active field by ensuring it is the correct type
245 * @param bool|int $active The field as provided
246 * @return bool The field in the correct format
247 */
248 private function prepare_active( $active ) {
249
250 if ( is_bool( $active ) ) {
251 return $active;
252 }
253
254 return $active ? true : false;
255 }
256
257 /**
258 * If $network is anything other than true, set it to false
259 * @param bool $network The provided field
260 * @return bool The filtered field
261 */
262 private function prepare_network( $network ) {
263
264 if ( null === $network && function_exists( 'is_network_admin' ) ) {
265 return is_network_admin();
266 }
267
268 return true === $network;
269 }
270
271 /**
272 * Retrieve the tags in list format
273 * @return string The tags seperated by a comma and a space
274 */
275 private function get_tags_list() {
276 return implode( ', ', $this->fields['tags'] );
277 }
278
279 /**
280 * Retrieve a list of all available scopes
281 * @return array
282 */
283 public static function get_all_scopes() {
284 return array( 'global', 'admin', 'front-end', 'single-use' );
285 }
286
287 /**
288 * Retrieve a list of all scope icons
289 * @return array
290 */
291 public static function get_scope_icons() {
292 return array(
293 'global' => 'admin-site',
294 'admin' => 'admin-tools',
295 'front-end' => 'admin-appearance',
296 'single-use' => 'clock',
297 );
298 }
299
300 /**
301 * Retrieve the string representation of the scope
302 * @return string The name of the scope
303 */
304 private function get_scope_name() {
305 return $this->scope;
306 }
307
308 /**
309 * Retrieve the icon used for the current scope
310 * @return string a dashicon name
311 */
312 private function get_scope_icon() {
313 $icons = self::get_scope_icons();
314 return $icons[ $this->scope ];
315 }
316
317 /**
318 * Determine if the snippet is a shared network snippet
319 * @return bool
320 */
321 private function get_shared_network() {
322
323 if ( isset( $this->fields['shared_network'] ) ) {
324 return $this->fields['shared_network'];
325 }
326
327 if ( ! is_multisite() || ! $this->fields['network'] ) {
328 $this->fields['shared_network'] = false;
329 } else {
330 $shared_network_snippets = get_site_option( 'shared_network_snippets', array() );
331 $this->fields['shared_network'] = in_array( $this->fields['id'], $shared_network_snippets );
332 }
333
334 return $this->fields['shared_network'];
335 }
336 }
337