PluginProbe
Code Snippets / 2.13.0
Code Snippets v2.13.0
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 3.0.1 All 64 releases
code-snippets / php / class-snippet.php

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

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