PluginProbe
Code Snippets / 3.1.0
Code Snippets v3.1.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 / snippet-ops.php

snippet-ops.php in Code Snippets 3.1.0, at php/snippet-ops.php

551 lines 15.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions to perform snippet operations
4 *
5 * @package Code_Snippets
6 */
7
8 namespace Code_Snippets;
9
10 /**
11 * Clean the cache where active snippets are stored.
12 *
13 * @param string $table_name Snippets table name.
14 * @param array|false $scopes List of scopes. Optional. If not provided, will flush the cache for all scopes.
15 *
16 * @return void
17 */
18 function clean_active_snippets_cache( $table_name, $scopes = false ) {
19 $scope_groups = $scopes ? [ $scopes ] : [
20 [ 'head-content', 'footer-content' ],
21 [ 'global', 'single-use', 'front-end' ],
22 [ 'global', 'single-use', 'admin' ],
23 ];
24
25 foreach ( $scope_groups as $scopes ) {
26 wp_cache_delete( sprintf( 'active_snippets_%s_%s', sanitize_key( join( '_', $scopes ) ), $table_name ), CACHE_GROUP );
27 }
28 }
29
30 /**
31 * Flush all snippets caches for a given database table.
32 *
33 * @param string $table_name Snippets table name.
34 *
35 * @return void
36 */
37 function clean_snippets_cache( $table_name ) {
38 wp_cache_delete( "all_snippet_tags_$table_name", CACHE_GROUP );
39 wp_cache_delete( "all_snippets_$table_name", CACHE_GROUP );
40 clean_active_snippets_cache( $table_name );
41 }
42
43 /**
44 * Retrieve a list of snippets from the database.
45 * Read operation.
46 *
47 * @param array $ids The IDs of the snippets to fetch.
48 * @param bool|null|string $multisite Retrieve multisite-wide snippets (true) or site-wide snippets (false).
49 *
50 * @return array List of Snippet objects.
51 *
52 * @since 2.0
53 */
54 function get_snippets( array $ids = array(), $multisite = null ) {
55 global $wpdb;
56
57 // If only one ID has been passed in, defer to the get_snippet() function.
58 $ids_count = count( $ids );
59 if ( 1 === $ids_count ) {
60 return array( get_snippet( $ids[0], $multisite ) );
61 }
62
63 $db = code_snippets()->db;
64 $multisite = $db->validate_network_param( $multisite );
65 $table_name = $db->get_table_name( $multisite );
66 $snippets = wp_cache_get( "all_snippets_$table_name", CACHE_GROUP );
67
68 // Fetch all snippets from the database if none are cached.
69 if ( ! is_array( $snippets ) ) {
70 $results = $wpdb->get_results( "SELECT * FROM $table_name", ARRAY_A ); // db call ok.
71
72 $snippets = $results ?
73 array_map(
74 function ( $snippet ) use ( $multisite ) {
75 $snippet['network'] = $multisite;
76 return new Snippet( $snippet );
77 },
78 $results
79 ) :
80 array();
81
82 $snippets = apply_filters( 'code_snippets/get_snippets', $snippets, $multisite );
83
84 if ( 0 === $ids_count ) {
85 wp_cache_set( "all_snippets_$table_name", $snippets, CACHE_GROUP );
86 }
87 }
88
89 // If a list of IDs are provided, narrow down the snippets list.
90 if ( $ids_count > 0 ) {
91 return array_filter(
92 $snippets,
93 function ( Snippet $snippet ) use ( $ids ) {
94 return in_array( $snippet->id, $ids, true );
95 }
96 );
97 }
98
99 return $snippets;
100 }
101
102 /**
103 * Gets all used tags from the database.
104 * Read operation.
105 *
106 * @since 2.0
107 */
108 function get_all_snippet_tags() {
109 global $wpdb;
110 $table_name = code_snippets()->db->get_table_name();
111 $cache_key = "all_snippet_tags_$table_name";
112
113 $tags = wp_cache_get( $cache_key, CACHE_GROUP );
114 if ( $tags ) {
115 return $tags;
116 }
117
118 // Grab all tags from the database.
119 $tags = array();
120 $all_tags = $wpdb->get_col( "SELECT tags FROM $table_name" ); // db call ok.
121
122 // Merge all tags into a single array.
123 foreach ( $all_tags as $snippet_tags ) {
124 $snippet_tags = code_snippets_build_tags_array( $snippet_tags );
125 $tags = array_merge( $snippet_tags, $tags );
126 }
127
128 // Remove duplicate tags.
129 $tags = array_values( array_unique( $tags, SORT_REGULAR ) );
130 wp_cache_set( $cache_key, $tags, CACHE_GROUP );
131 return $tags;
132 }
133
134 /**
135 * Make sure that the tags are a valid array.
136 *
137 * @param mixed $tags The tags to convert into an array.
138 *
139 * @return array The converted tags.
140 *
141 * @since 2.0.0
142 */
143 function code_snippets_build_tags_array( $tags ) {
144
145 /* If there are no tags set, return an empty array. */
146 if ( empty( $tags ) ) {
147 return array();
148 }
149
150 /* If the tags are set as a string, convert them into an array. */
151 if ( is_string( $tags ) ) {
152 $tags = wp_strip_all_tags( $tags );
153 $tags = str_replace( ', ', ',', $tags );
154 $tags = explode( ',', $tags );
155 }
156
157 /* If we still don't have an array, just convert whatever we do have into one. */
158 return (array) $tags;
159 }
160
161 /**
162 * Retrieve a single snippets from the database.
163 * Will return empty snippet object if no snippet ID is specified.
164 * Read operation.
165 *
166 * @param int $id The ID of the snippet to retrieve. 0 to build a new snippet.
167 * @param boolean|null $multisite Retrieve a multisite-wide snippet (true) or site-wide snippet (false).
168 *
169 * @return Snippet A single snippet object.
170 * @since 2.0.0
171 */
172 function get_snippet( $id = 0, $multisite = null ) {
173 global $wpdb;
174
175 $id = absint( $id );
176 $multisite = DB::validate_network_param( $multisite );
177 $table_name = code_snippets()->db->get_table_name( $multisite );
178
179 if ( 0 === $id ) {
180 // If an invalid ID is provided, then return an empty snippet object.
181 $snippet = new Snippet();
182
183 } else {
184 $cached_snippets = wp_cache_get( "all_snippets_$table_name", CACHE_GROUP );
185
186 // Attempt to fetch snippet from the cached list, if it exists.
187 if ( is_array( $cached_snippets ) ) {
188 foreach ( $cached_snippets as $snippet ) {
189 if ( $snippet->id === $id ) {
190 return apply_filters( 'code_snippets/get_snippet', $snippet, $id, $multisite );
191 }
192 }
193 }
194
195 // Otherwise, retrieve the snippet from the database.
196 $snippet = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_name WHERE id = %d", $id ) ); // cache pass, db call ok.
197 $snippet = new Snippet( $snippet );
198 }
199
200 $snippet->network = $multisite;
201 return apply_filters( 'code_snippets/get_snippet', $snippet, $id, $multisite );
202 }
203
204 /**
205 * Activates a snippet.
206 * Write operation.
207 *
208 * @param int $id ID of the snippet to activate.
209 * @param bool|null $multisite Whether the snippets are multisite-wide (true) or site-wide (false).
210 *
211 * @return boolean
212 * @since 2.0.0
213 */
214 function activate_snippet( $id, $multisite = null ) {
215 global $wpdb;
216 $db = code_snippets()->db;
217 $table_name = $db->get_table_name( $multisite );
218
219 // Retrieve the snippet code from the database for validation before activating.
220 $snippet = get_snippet( $id );
221 if ( ! $snippet ) {
222 return false;
223 }
224
225 $validator = new Validator( $snippet->code );
226 if ( $validator->validate() ) {
227 return false;
228 }
229
230 $wpdb->update(
231 $table_name,
232 array( 'active' => '1' ),
233 array( 'id' => $id ),
234 array( '%d' ),
235 array( '%d' )
236 ); // db call ok.
237
238 /* Remove snippet from shared network snippet list if it was Network Activated */
239 if ( $table_name === $db->ms_table ) {
240 $shared_network_snippets = get_site_option( 'shared_network_snippets' );
241 if ( $shared_network_snippets ) {
242 $shared_network_snippets = array_diff( $shared_network_snippets, array( $id ) );
243 update_site_option( 'shared_network_snippets', $shared_network_snippets );
244 }
245 }
246
247 do_action( 'code_snippets/activate_snippet', $id, $multisite );
248 clean_snippets_cache( $table_name );
249 return true;
250 }
251
252 /**
253 * Activates multiple snippets.
254 * Write operation.
255 *
256 * @param array $ids The IDs of the snippets to activate.
257 * @param bool|null $multisite Whether the snippets are multisite-wide (true) or site-wide (false).
258 *
259 * @return array The IDs of the snippets which were successfully activated.
260 *
261 * @since 2.0.0
262 */
263 function activate_snippets( array $ids, $multisite = null ) {
264 global $wpdb;
265 $db = code_snippets()->db;
266 $multisite = DB::validate_network_param( $multisite );
267 $table_name = $db->get_table_name( $multisite );
268 $snippets = get_snippets( $ids, $multisite );
269
270 if ( ! $snippets ) {
271 return array();
272 }
273
274 // Loop through each snippet code and validate individually.
275 $valid_ids = array();
276
277 foreach ( $snippets as $snippet ) {
278 $validator = new Validator( $snippet->code );
279 $code_error = $validator->validate();
280
281 if ( ! $code_error ) {
282 $valid_ids[] = $snippet->id;
283 }
284 }
285
286 // If there are no valid snippets, then we're done.
287 if ( ! $valid_ids ) {
288 return $valid_ids;
289 }
290
291 // Build a SQL query containing all IDs, as wpdb::update does not support OR conditionals.
292 $ids_format = implode( ',', array_fill( 0, count( $valid_ids ), '%d' ) );
293 // phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
294 $wpdb->query( $wpdb->prepare( "UPDATE $table_name SET active = 1 WHERE id IN ($ids_format)", $valid_ids ) ); // db call ok.
295
296 // Remove any snippets from shared network snippet list if they were Network Activated.
297 if ( $table_name === $db->ms_table ) {
298 $shared_network_snippets = get_site_option( 'shared_network_snippets' );
299
300 if ( $shared_network_snippets ) {
301 $shared_network_snippets = array_diff( $shared_network_snippets, $valid_ids );
302 update_site_option( 'shared_network_snippets', $shared_network_snippets );
303 clean_active_snippets_cache( $db->ms_table );
304 }
305 }
306
307 do_action( 'code_snippets/activate_snippets', $valid_ids, $multisite );
308 clean_snippets_cache( $table_name );
309 return $valid_ids;
310 }
311
312 /**
313 * Deactivate a snippet.
314 * Write operation.
315 *
316 * @param int $id ID of the snippet to deactivate.
317 * @param bool|null $multisite Whether the snippets are multisite-wide (true) or site-wide (false).
318 *
319 * @uses $wpdb to set the snippets' active status.
320 * @since 2.0.0
321 */
322 function deactivate_snippet( $id, $multisite = null ) {
323 global $wpdb;
324 $db = code_snippets()->db;
325 $table = $db->get_table_name( $multisite );
326
327 // Set the snippet to active.
328 $wpdb->update(
329 $table,
330 array( 'active' => '0' ),
331 array( 'id' => $id ),
332 array( '%d' ),
333 array( '%d' )
334 ); // db call ok.
335
336 // Update the recently active list.
337 $recently_active = array( $id => time() );
338
339 if ( $table === $db->table ) {
340
341 update_option(
342 'recently_activated_snippets',
343 $recently_active + (array) get_option( 'recently_activated_snippets', array() )
344 );
345 } elseif ( $table === $db->ms_table ) {
346
347 update_site_option(
348 'recently_activated_snippets',
349 $recently_active + (array) get_site_option( 'recently_activated_snippets', array() )
350 );
351 }
352
353 do_action( 'code_snippets/deactivate_snippet', $id, $multisite );
354 clean_snippets_cache( $table );
355 }
356
357 /**
358 * Deletes a snippet from the database.
359 * Write operation.
360 *
361 * @param int $id ID of the snippet to delete.
362 * @param bool|null $multisite Delete from network-wide (true) or site-wide (false) table.
363 *
364 * @since 2.0.0
365 */
366 function delete_snippet( $id, $multisite = null ) {
367 global $wpdb;
368 $table = code_snippets()->db->get_table_name( $multisite );
369
370 $wpdb->delete(
371 $table,
372 array( 'id' => $id ),
373 array( '%d' )
374 ); // db call ok.
375
376 do_action( 'code_snippets/delete_snippet', $id, $multisite );
377 clean_snippets_cache( $table );
378 }
379
380 /**
381 * Saves a snippet to the database.
382 * Write operation.
383 *
384 * @param Snippet $snippet The snippet to add/update to the database.
385 *
386 * @return int ID of the snippet
387 *
388 * @since 2.0.0
389 */
390 function save_snippet( Snippet $snippet ) {
391 global $wpdb;
392 $table = code_snippets()->db->get_table_name( $snippet->network );
393
394 // Update the last modification date if necessary.
395 $snippet->update_modified();
396
397 // Build array of data to insert.
398 $data = array(
399 'name' => $snippet->name,
400 'description' => $snippet->desc,
401 'code' => $snippet->code,
402 'tags' => $snippet->tags_list,
403 'scope' => $snippet->scope,
404 'priority' => $snippet->priority,
405 'active' => intval( $snippet->active ),
406 'modified' => $snippet->modified,
407 );
408
409 // Create a new snippet if the ID is not set.
410 if ( 0 === $snippet->id ) {
411 $wpdb->insert( $table, $data, '%s' ); // db call ok.
412 $snippet->id = $wpdb->insert_id;
413
414 do_action( 'code_snippets/create_snippet', $snippet->id, $table );
415 } else {
416
417 // Otherwise, update the snippet data.
418 $wpdb->update( $table, $data, array( 'id' => $snippet->id ), null, array( '%d' ) ); // db call ok.
419
420 do_action( 'code_snippets/update_snippet', $snippet->id, $table );
421 }
422
423 clean_snippets_cache( $table );
424 return $snippet->id;
425 }
426
427 /**
428 * Update a snippet entry given a list of fields.
429 * Write operation.
430 *
431 * @param int $snippet_id ID of the snippet to update.
432 * @param array $fields An array of fields mapped to their values.
433 * @param bool|null $network Update in network-wide (true) or site-wide (false) table.
434 */
435 function update_snippet_fields( $snippet_id, $fields, $network = null ) {
436 global $wpdb;
437
438 $table = code_snippets()->db->get_table_name( $network );
439
440 // Build a new snippet object for the validation.
441 $snippet = new Snippet();
442 $snippet->id = $snippet_id;
443
444 // Validate fields through the snippet class and copy them into a clean array.
445 $clean_fields = array();
446
447 foreach ( $fields as $field => $value ) {
448
449 if ( $snippet->set_field( $field, $value ) ) {
450 $clean_fields[ $field ] = $snippet->$field;
451 }
452 }
453
454 // Update the snippet in the database.
455 $wpdb->update( $table, $clean_fields, array( 'id' => $snippet->id ), null, array( '%d' ) ); // db call ok.
456
457 do_action( 'code_snippets/update_snippet', $snippet->id, $table );
458 clean_snippets_cache( $table );
459 }
460
461 /**
462 * Execute a snippet.
463 * Execute operation.
464 *
465 * Code must NOT be escaped, as it will be executed directly.
466 *
467 * @param string $code Snippet code to execute.
468 * @param int $id Snippet ID.
469 * @param bool $catch_output Whether to attempt to suppress the output of execution using buffers.
470 *
471 * @return mixed Result of the code execution
472 * @since 2.0.0
473 */
474 function execute_snippet( $code, $id = 0, $catch_output = true ) {
475
476 if ( empty( $code ) || defined( 'CODE_SNIPPETS_SAFE_MODE' ) && CODE_SNIPPETS_SAFE_MODE ) {
477 return false;
478 }
479
480 if ( $catch_output ) {
481 ob_start();
482 }
483
484 $result = eval( $code );
485
486 if ( $catch_output ) {
487 ob_end_clean();
488 }
489
490 do_action( 'code_snippets/after_execute_snippet', $id, $code, $result );
491
492 return $result;
493 }
494
495 /**
496 * Run the active snippets.
497 * Read-write-execute operation.
498 *
499 * @return bool true on success, false on failure.
500 *
501 * @since 2.0.0
502 */
503 function execute_active_snippets() {
504 global $wpdb;
505
506 // Bail early if safe mode is active.
507 if ( defined( 'CODE_SNIPPETS_SAFE_MODE' ) && CODE_SNIPPETS_SAFE_MODE || ! apply_filters( 'code_snippets/execute_snippets', true ) ) {
508 return false;
509 }
510
511 $db = code_snippets()->db;
512 $scopes = array( 'global', 'single-use', is_admin() ? 'admin' : 'front-end' );
513 $data = $db->fetch_active_snippets( $scopes );
514
515 foreach ( $data as $table_name => $active_snippets ) {
516
517 // Loop through the returned snippets and execute the PHP code.
518 foreach ( $active_snippets as $snippet ) {
519 $snippet_id = intval( $snippet['id'] );
520 $code = $snippet['code'];
521
522 // If the snippet is a single-use snippet, deactivate it before execution to ensure that the process always happens.
523 if ( 'single-use' === $snippet['scope'] ) {
524 $active_shared_ids = get_option( 'active_shared_network_snippets', array() );
525
526 if ( $table_name === $db->ms_table && is_array( $active_shared_ids ) && in_array( $snippet_id, $active_shared_ids, true ) ) {
527 unset( $active_shared_ids[ array_search( $snippet_id, $active_shared_ids, true ) ] );
528 $active_shared_ids = array_values( $active_shared_ids );
529 update_option( 'active_shared_network_snippets', $active_shared_ids );
530 clean_active_snippets_cache( $table_name );
531 } else {
532 $wpdb->update(
533 $table_name,
534 array( 'active' => '0' ),
535 array( 'id' => $snippet_id ),
536 array( '%d' ),
537 array( '%d' )
538 ); // db call ok.
539 clean_snippets_cache( $table_name );
540 }
541 }
542
543 if ( apply_filters( 'code_snippets/allow_execute_snippet', true, $snippet_id, $table_name ) ) {
544 execute_snippet( $code, $snippet_id );
545 }
546 }
547 }
548
549 return true;
550 }
551