| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions to perform snippet operations |
| 4 |
* |
| 5 |
* @package Code_Snippets |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Code_Snippets; |
| 9 |
|
| 10 |
use Code_Snippets\REST_API\Snippets_REST_Controller; |
| 11 |
use ParseError; |
| 12 |
use function Code_Snippets\Settings\get_self_option; |
| 13 |
use function Code_Snippets\Settings\update_self_option; |
| 14 |
|
| 15 |
/** |
| 16 |
* Clean the cache where active snippets are stored. |
| 17 |
* |
| 18 |
* @param string $table_name Snippets table name. |
| 19 |
* @param array<string>|false $scopes List of scopes. Optional. If not provided, will flush the cache for all scopes. |
| 20 |
* |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
function clean_active_snippets_cache( string $table_name, $scopes = false ) { |
| 24 |
$scope_groups = $scopes ? [ $scopes ] : [ |
| 25 |
[ 'head-content', 'footer-content' ], |
| 26 |
[ 'global', 'single-use', 'front-end' ], |
| 27 |
[ 'global', 'single-use', 'admin' ], |
| 28 |
]; |
| 29 |
|
| 30 |
foreach ( $scope_groups as $scopes ) { |
| 31 |
wp_cache_delete( sprintf( 'active_snippets_%s_%s', sanitize_key( join( '_', $scopes ) ), $table_name ), CACHE_GROUP ); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Flush all snippets caches for a given database table. |
| 37 |
* |
| 38 |
* @param string $table_name Snippets table name. |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
function clean_snippets_cache( string $table_name ) { |
| 43 |
wp_cache_delete( "all_snippet_tags_$table_name", CACHE_GROUP ); |
| 44 |
wp_cache_delete( "all_snippets_$table_name", CACHE_GROUP ); |
| 45 |
clean_active_snippets_cache( $table_name ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Retrieve a list of snippets from the database. |
| 50 |
* Read operation. |
| 51 |
* |
| 52 |
* @param array<string> $ids The IDs of the snippets to fetch. |
| 53 |
* @param bool|null $network Retrieve multisite-wide snippets (true) or site-wide snippets (false). |
| 54 |
* |
| 55 |
* @return array<Snippet> List of Snippet objects. |
| 56 |
* |
| 57 |
* @since 2.0 |
| 58 |
*/ |
| 59 |
function get_snippets( array $ids = array(), bool $network = null ): array { |
| 60 |
global $wpdb; |
| 61 |
|
| 62 |
// If only one ID has been passed in, defer to the get_snippet() function. |
| 63 |
$ids_count = count( $ids ); |
| 64 |
if ( 1 === $ids_count ) { |
| 65 |
return array( get_snippet( $ids[0], $network ) ); |
| 66 |
} |
| 67 |
|
| 68 |
$network = DB::validate_network_param( $network ); |
| 69 |
$table_name = code_snippets()->db->get_table_name( $network ); |
| 70 |
|
| 71 |
$snippets = wp_cache_get( "all_snippets_$table_name", CACHE_GROUP ); |
| 72 |
|
| 73 |
// Fetch all snippets from the database if none are cached. |
| 74 |
if ( ! is_array( $snippets ) ) { |
| 75 |
$results = $wpdb->get_results( "SELECT * FROM $table_name", ARRAY_A ); |
| 76 |
|
| 77 |
$snippets = $results ? |
| 78 |
array_map( |
| 79 |
function ( $snippet_data ) use ( $network ) { |
| 80 |
$snippet_data['network'] = $network; |
| 81 |
return new Snippet( $snippet_data ); |
| 82 |
}, |
| 83 |
$results |
| 84 |
) : |
| 85 |
array(); |
| 86 |
|
| 87 |
$snippets = apply_filters( 'code_snippets/get_snippets', $snippets, $network ); |
| 88 |
|
| 89 |
if ( 0 === $ids_count ) { |
| 90 |
wp_cache_set( "all_snippets_$table_name", $snippets, CACHE_GROUP ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// If a list of IDs are provided, narrow down the snippets list. |
| 95 |
if ( $ids_count > 0 ) { |
| 96 |
$ids = array_map( 'intval', $ids ); |
| 97 |
return array_filter( |
| 98 |
$snippets, |
| 99 |
function ( Snippet $snippet ) use ( $ids ) { |
| 100 |
return in_array( $snippet->id, $ids, true ); |
| 101 |
} |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
return $snippets; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Gets all used tags from the database. |
| 110 |
* Read operation. |
| 111 |
* |
| 112 |
* @since 2.0 |
| 113 |
*/ |
| 114 |
function get_all_snippet_tags() { |
| 115 |
global $wpdb; |
| 116 |
$table_name = code_snippets()->db->get_table_name(); |
| 117 |
$cache_key = "all_snippet_tags_$table_name"; |
| 118 |
|
| 119 |
$tags = wp_cache_get( $cache_key, CACHE_GROUP ); |
| 120 |
if ( $tags ) { |
| 121 |
return $tags; |
| 122 |
} |
| 123 |
|
| 124 |
// Grab all tags from the database. |
| 125 |
$tags = array(); |
| 126 |
$all_tags = $wpdb->get_col( "SELECT tags FROM $table_name" ); |
| 127 |
|
| 128 |
// Merge all tags into a single array. |
| 129 |
foreach ( $all_tags as $snippet_tags ) { |
| 130 |
$snippet_tags = code_snippets_build_tags_array( $snippet_tags ); |
| 131 |
$tags = array_merge( $snippet_tags, $tags ); |
| 132 |
} |
| 133 |
|
| 134 |
// Remove duplicate tags. |
| 135 |
$tags = array_values( array_unique( $tags, SORT_REGULAR ) ); |
| 136 |
wp_cache_set( $cache_key, $tags, CACHE_GROUP ); |
| 137 |
return $tags; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Make sure that the tags are a valid array. |
| 142 |
* |
| 143 |
* @param array|string $tags The tags to convert into an array. |
| 144 |
* |
| 145 |
* @return array<string> The converted tags. |
| 146 |
* |
| 147 |
* @since 2.0.0 |
| 148 |
*/ |
| 149 |
function code_snippets_build_tags_array( $tags ): array { |
| 150 |
|
| 151 |
/* If there are no tags set, return an empty array. */ |
| 152 |
if ( empty( $tags ) ) { |
| 153 |
return array(); |
| 154 |
} |
| 155 |
|
| 156 |
/* If the tags are set as a string, convert them into an array. */ |
| 157 |
if ( is_string( $tags ) ) { |
| 158 |
$tags = wp_strip_all_tags( $tags ); |
| 159 |
$tags = str_replace( ', ', ',', $tags ); |
| 160 |
$tags = explode( ',', $tags ); |
| 161 |
} |
| 162 |
|
| 163 |
/* If we still don't have an array, just convert whatever we do have into one. */ |
| 164 |
return (array) $tags; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Retrieve a single snippets from the database. |
| 169 |
* Will return empty snippet object if no snippet ID is specified. |
| 170 |
* Read operation. |
| 171 |
* |
| 172 |
* @param int $id The ID of the snippet to retrieve. 0 to build a new snippet. |
| 173 |
* @param bool|null $network Retrieve a multisite-wide snippet (true) or site-wide snippet (false). |
| 174 |
* |
| 175 |
* @return Snippet A single snippet object. |
| 176 |
* |
| 177 |
* @since 2.0.0 |
| 178 |
*/ |
| 179 |
function get_snippet( int $id = 0, bool $network = null ): Snippet { |
| 180 |
global $wpdb; |
| 181 |
|
| 182 |
$id = absint( $id ); |
| 183 |
$network = DB::validate_network_param( $network ); |
| 184 |
$table_name = code_snippets()->db->get_table_name( $network ); |
| 185 |
|
| 186 |
if ( 0 === $id ) { |
| 187 |
// If an invalid ID is provided, then return an empty snippet object. |
| 188 |
$snippet = new Snippet(); |
| 189 |
|
| 190 |
} else { |
| 191 |
$cached_snippets = wp_cache_get( "all_snippets_$table_name", CACHE_GROUP ); |
| 192 |
|
| 193 |
// Attempt to fetch snippet from the cached list, if it exists. |
| 194 |
if ( is_array( $cached_snippets ) ) { |
| 195 |
foreach ( $cached_snippets as $snippet ) { |
| 196 |
if ( $snippet->id === $id ) { |
| 197 |
return apply_filters( 'code_snippets/get_snippet', $snippet, $id, $network ); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// Otherwise, retrieve the snippet from the database. |
| 203 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching |
| 204 |
$snippet_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_name WHERE id = %d", $id ) ); |
| 205 |
$snippet = new Snippet( $snippet_data ); |
| 206 |
} |
| 207 |
|
| 208 |
$snippet->network = $network; |
| 209 |
return apply_filters( 'code_snippets/get_snippet', $snippet, $id, $network ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Ensure the list of shared network snippets is correct if one has been recently activated or deactivated. |
| 214 |
* Write operation. |
| 215 |
* |
| 216 |
* @access private |
| 217 |
* |
| 218 |
* @param Snippet[] $snippets Snippets that was recently updated. |
| 219 |
* |
| 220 |
* @return boolean Whether an update was performed. |
| 221 |
*/ |
| 222 |
function update_shared_network_snippets( array $snippets ): bool { |
| 223 |
$shared = []; |
| 224 |
$unshared = []; |
| 225 |
|
| 226 |
if ( ! is_multisite() ) { |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
foreach ( $snippets as $snippet ) { |
| 231 |
if ( $snippet->shared_network ) { |
| 232 |
if ( $snippet->active ) { |
| 233 |
$shared[] = $snippet; |
| 234 |
} else { |
| 235 |
$unshared[] = $snippet; |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if ( ! $shared && ! $unshared ) { |
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
$shared_snippets = get_site_option( 'shared_network_snippets', [] ); |
| 245 |
$updated_shared_snippets = array_values( array_diff( array_merge( $shared_snippets, $shared ), $unshared ) ); |
| 246 |
|
| 247 |
if ( $shared_snippets === $updated_shared_snippets ) { |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
update_site_option( 'shared_network_snippets', $updated_shared_snippets ); |
| 252 |
|
| 253 |
// Deactivate the snippet on all sites if necessary. |
| 254 |
if ( $unshared ) { |
| 255 |
$sites = get_sites( [ 'fields' => 'ids' ] ); |
| 256 |
|
| 257 |
foreach ( $sites as $site ) { |
| 258 |
switch_to_blog( $site ); |
| 259 |
$active_shared_snippets = get_option( 'active_shared_network_snippets' ); |
| 260 |
|
| 261 |
if ( is_array( $active_shared_snippets ) ) { |
| 262 |
$active_shared_snippets = array_diff( $active_shared_snippets, $unshared ); |
| 263 |
update_option( 'active_shared_network_snippets', $active_shared_snippets ); |
| 264 |
} |
| 265 |
|
| 266 |
clean_active_snippets_cache( code_snippets()->db->ms_table ); |
| 267 |
} |
| 268 |
|
| 269 |
restore_current_blog(); |
| 270 |
} |
| 271 |
|
| 272 |
return true; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Activates a snippet. |
| 277 |
* Write operation. |
| 278 |
* |
| 279 |
* @param int $id ID of the snippet to activate. |
| 280 |
* @param bool|null $network Whether the snippets are multisite-wide (true) or site-wide (false). |
| 281 |
* |
| 282 |
* @return Snippet|string Snippet object on success, error message on failure. |
| 283 |
* @since 2.0.0 |
| 284 |
*/ |
| 285 |
function activate_snippet( int $id, bool $network = null ) { |
| 286 |
global $wpdb; |
| 287 |
$network = DB::validate_network_param( $network ); |
| 288 |
$table_name = code_snippets()->db->get_table_name( $network ); |
| 289 |
|
| 290 |
// Retrieve the snippet code from the database for validation before activating. |
| 291 |
$snippet = get_snippet( $id, $network ); |
| 292 |
|
| 293 |
if ( 0 === $snippet->id ) { |
| 294 |
// translators: %d: snippet identifier. |
| 295 |
return sprintf( __( 'Could not locate snippet with ID %d.', 'code-snippets' ), $id ); |
| 296 |
} |
| 297 |
|
| 298 |
$validator = new Validator( $snippet->code ); |
| 299 |
if ( $validator->validate() ) { |
| 300 |
return __( 'Could not activate snippet: code did not pass validation.', 'code-snippets' ); |
| 301 |
} |
| 302 |
|
| 303 |
$result = $wpdb->update( |
| 304 |
$table_name, |
| 305 |
array( 'active' => '1' ), |
| 306 |
array( 'id' => $id ), |
| 307 |
array( '%d' ), |
| 308 |
array( '%d' ) |
| 309 |
); |
| 310 |
|
| 311 |
if ( ! $result ) { |
| 312 |
return __( 'Could not activate snippet.', 'code-snippets' ); |
| 313 |
} |
| 314 |
|
| 315 |
update_shared_network_snippets( [ $snippet ] ); |
| 316 |
do_action( 'code_snippets/activate_snippet', $snippet ); |
| 317 |
clean_snippets_cache( $table_name ); |
| 318 |
return $snippet; |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Activates multiple snippets. |
| 323 |
* Write operation. |
| 324 |
* |
| 325 |
* @param array<integer> $ids The IDs of the snippets to activate. |
| 326 |
* @param bool|null $network Whether the snippets are multisite-wide (true) or site-wide (false). |
| 327 |
* |
| 328 |
* @return Snippet[]|null Snippets which were successfully activated, or null on failure. |
| 329 |
* |
| 330 |
* @since 2.0.0 |
| 331 |
*/ |
| 332 |
function activate_snippets( array $ids, bool $network = null ) { |
| 333 |
global $wpdb; |
| 334 |
$network = DB::validate_network_param( $network ); |
| 335 |
$table_name = code_snippets()->db->get_table_name( $network ); |
| 336 |
|
| 337 |
$snippets = get_snippets( $ids, $network ); |
| 338 |
|
| 339 |
if ( ! $snippets ) { |
| 340 |
return null; |
| 341 |
} |
| 342 |
|
| 343 |
// Loop through each snippet code and validate individually. |
| 344 |
$valid_ids = []; |
| 345 |
$valid_snippets = []; |
| 346 |
|
| 347 |
foreach ( $snippets as $snippet ) { |
| 348 |
$validator = new Validator( $snippet->code ); |
| 349 |
$code_error = $validator->validate(); |
| 350 |
|
| 351 |
if ( ! $code_error ) { |
| 352 |
$valid_ids[] = $snippet->id; |
| 353 |
$valid_snippets[] = $snippet; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
// If there are no valid snippets, then we're done. |
| 358 |
if ( ! $valid_ids ) { |
| 359 |
return null; |
| 360 |
} |
| 361 |
|
| 362 |
// Build a SQL query containing all IDs, as wpdb::update does not support OR conditionals. |
| 363 |
$ids_format = implode( ',', array_fill( 0, count( $valid_ids ), '%d' ) ); |
| 364 |
|
| 365 |
// phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 366 |
$rows_updated = $wpdb->query( $wpdb->prepare( "UPDATE $table_name SET active = 1 WHERE id IN ($ids_format)", $valid_ids ) ); |
| 367 |
|
| 368 |
if ( ! $rows_updated ) { |
| 369 |
return null; |
| 370 |
} |
| 371 |
|
| 372 |
update_shared_network_snippets( $valid_snippets ); |
| 373 |
do_action( 'code_snippets/activate_snippets', $valid_snippets, $table_name ); |
| 374 |
clean_snippets_cache( $table_name ); |
| 375 |
return $valid_ids; |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Deactivate a snippet. |
| 380 |
* Write operation. |
| 381 |
* |
| 382 |
* @param int $id ID of the snippet to deactivate. |
| 383 |
* @param bool|null $network Whether the snippets are multisite-wide (true) or site-wide (false). |
| 384 |
* |
| 385 |
* @return Snippet|null Snippet that was deactivated on success, or null on failure. |
| 386 |
* |
| 387 |
* @since 2.0.0 |
| 388 |
*/ |
| 389 |
function deactivate_snippet( int $id, bool $network = null ) { |
| 390 |
global $wpdb; |
| 391 |
$network = DB::validate_network_param( $network ); |
| 392 |
$table = code_snippets()->db->get_table_name( $network ); |
| 393 |
|
| 394 |
// Set the snippet to active. |
| 395 |
$result = $wpdb->update( |
| 396 |
$table, |
| 397 |
array( 'active' => '0' ), |
| 398 |
array( 'id' => $id ), |
| 399 |
array( '%d' ), |
| 400 |
array( '%d' ) |
| 401 |
); |
| 402 |
|
| 403 |
if ( ! $result ) { |
| 404 |
return null; |
| 405 |
} |
| 406 |
|
| 407 |
// Update the recently active list. |
| 408 |
$snippet = get_snippet( $id ); |
| 409 |
$recently_active = [ $id => time() ] + get_self_option( $network, 'recently_activated_snippets', [] ); |
| 410 |
update_self_option( $network, 'recently_activated_snippets', $recently_active ); |
| 411 |
|
| 412 |
update_shared_network_snippets( [ $snippet ] ); |
| 413 |
do_action( 'code_snippets/deactivate_snippet', $id, $network ); |
| 414 |
clean_snippets_cache( $table ); |
| 415 |
|
| 416 |
return $snippet; |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Deletes a snippet from the database. |
| 421 |
* Write operation. |
| 422 |
* |
| 423 |
* @param int $id ID of the snippet to delete. |
| 424 |
* @param bool|null $network Delete from network-wide (true) or site-wide (false) table. |
| 425 |
* |
| 426 |
* @return bool Whether the operation completed successfully. |
| 427 |
* |
| 428 |
* @since 2.0.0 |
| 429 |
*/ |
| 430 |
function delete_snippet( int $id, bool $network = null ): bool { |
| 431 |
global $wpdb; |
| 432 |
$network = DB::validate_network_param( $network ); |
| 433 |
$table = code_snippets()->db->get_table_name( $network ); |
| 434 |
|
| 435 |
$result = $wpdb->delete( |
| 436 |
$table, |
| 437 |
array( 'id' => $id ), |
| 438 |
array( '%d' ) |
| 439 |
); |
| 440 |
|
| 441 |
if ( $result ) { |
| 442 |
do_action( 'code_snippets/delete_snippet', $id, $network ); |
| 443 |
clean_snippets_cache( $table ); |
| 444 |
} |
| 445 |
|
| 446 |
return (bool) $result; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
/** |
| 451 |
* Test snippet code for errors, augmenting the snippet object. |
| 452 |
* |
| 453 |
* @param Snippet $snippet Snippet object. |
| 454 |
*/ |
| 455 |
function test_snippet_code( Snippet $snippet ) { |
| 456 |
$snippet->code_error = null; |
| 457 |
|
| 458 |
if ( 'php' !== $snippet->type ) { |
| 459 |
return; |
| 460 |
} |
| 461 |
|
| 462 |
$validator = new Validator( $snippet->code ); |
| 463 |
$result = $validator->validate(); |
| 464 |
|
| 465 |
if ( $result ) { |
| 466 |
$snippet->code_error = [ $result['message'], $result['line'] ]; |
| 467 |
} |
| 468 |
|
| 469 |
if ( ! $snippet->code_error && 'single-use' !== $snippet->scope ) { |
| 470 |
$result = execute_snippet( $snippet->code, $snippet->id, true ); |
| 471 |
|
| 472 |
if ( $result instanceof ParseError ) { |
| 473 |
$snippet->code_error = [ |
| 474 |
ucfirst( rtrim( $result->getMessage(), '.' ) ) . '.', |
| 475 |
$result->getLine(), |
| 476 |
]; |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* Saves a snippet to the database. |
| 483 |
* Write operation. |
| 484 |
* |
| 485 |
* @param Snippet|array<string, mixed> $snippet The snippet to add/update to the database. |
| 486 |
* |
| 487 |
* @return Snippet|null Updated snippet. |
| 488 |
* |
| 489 |
* @since 2.0.0 |
| 490 |
*/ |
| 491 |
function save_snippet( $snippet ) { |
| 492 |
global $wpdb; |
| 493 |
$table = code_snippets()->db->get_table_name( $snippet->network ); |
| 494 |
|
| 495 |
if ( ! $snippet instanceof Snippet ) { |
| 496 |
$snippet = new Snippet( $snippet ); |
| 497 |
} |
| 498 |
|
| 499 |
// Update the last modification date if necessary. |
| 500 |
$snippet->update_modified(); |
| 501 |
|
| 502 |
if ( 'php' === $snippet->type ) { |
| 503 |
// Remove tags from beginning and end of snippet. |
| 504 |
$snippet->code = preg_replace( '|^\s*<\?(php)?|', '', $snippet->code ); |
| 505 |
$snippet->code = preg_replace( '|\?>\s*$|', '', $snippet->code ); |
| 506 |
|
| 507 |
// Deactivate snippet if code contains errors. |
| 508 |
if ( $snippet->active && 'single-use' !== $snippet->scope ) { |
| 509 |
test_snippet_code( $snippet ); |
| 510 |
|
| 511 |
if ( $snippet->code_error ) { |
| 512 |
$snippet->active = 0; |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
// Increment the revision number. |
| 518 |
$snippet->increment_revision(); |
| 519 |
|
| 520 |
// Build the list of data to insert. Shared network snippets are always considered inactive. |
| 521 |
$data = [ |
| 522 |
'name' => $snippet->name, |
| 523 |
'description' => $snippet->desc, |
| 524 |
'code' => $snippet->code, |
| 525 |
'tags' => $snippet->tags_list, |
| 526 |
'scope' => $snippet->scope, |
| 527 |
'priority' => $snippet->priority, |
| 528 |
'active' => intval( $snippet->active && ! $snippet->shared_network ), |
| 529 |
'modified' => $snippet->modified, |
| 530 |
'revision' => $snippet->revision, |
| 531 |
'cloud_id' => $snippet->cloud_id ? $snippet->cloud_id : null, |
| 532 |
]; |
| 533 |
|
| 534 |
// Create a new snippet if the ID is not set. |
| 535 |
if ( 0 === $snippet->id ) { |
| 536 |
$result = $wpdb->insert( $table, $data, '%s' ); |
| 537 |
if ( false === $result ) { |
| 538 |
return null; |
| 539 |
} |
| 540 |
|
| 541 |
$snippet->id = $wpdb->insert_id; |
| 542 |
do_action( 'code_snippets/create_snippet', $snippet, $table ); |
| 543 |
} else { |
| 544 |
|
| 545 |
// Otherwise, update the snippet data. |
| 546 |
$result = $wpdb->update( $table, $data, [ 'id' => $snippet->id ], null, [ '%d' ] ); |
| 547 |
if ( false === $result ) { |
| 548 |
return null; |
| 549 |
} |
| 550 |
|
| 551 |
do_action( 'code_snippets/update_snippet', $snippet, $table ); |
| 552 |
} |
| 553 |
|
| 554 |
update_shared_network_snippets( [ $snippet ] ); |
| 555 |
clean_snippets_cache( $table ); |
| 556 |
return $snippet; |
| 557 |
} |
| 558 |
|
| 559 |
/** |
| 560 |
* Execute a snippet. |
| 561 |
* Execute operation. |
| 562 |
* |
| 563 |
* Code must NOT be escaped, as it will be executed directly. |
| 564 |
* |
| 565 |
* @param string $code Snippet code to execute. |
| 566 |
* @param integer $id Snippet ID. |
| 567 |
* @param boolean $force Force snippet execution, even if save mode is active. |
| 568 |
* |
| 569 |
* @return ParseError|mixed Code error if encountered during execution, or result of snippet execution otherwise. |
| 570 |
* |
| 571 |
* @since 2.0.0 |
| 572 |
*/ |
| 573 |
function execute_snippet( string $code, int $id = 0, bool $force = false ) { |
| 574 |
if ( empty( $code ) || ! $force && defined( 'CODE_SNIPPETS_SAFE_MODE' ) && CODE_SNIPPETS_SAFE_MODE ) { |
| 575 |
return false; |
| 576 |
} |
| 577 |
|
| 578 |
ob_start(); |
| 579 |
|
| 580 |
try { |
| 581 |
$result = eval( $code ); |
| 582 |
} catch ( ParseError $parse_error ) { |
| 583 |
$result = $parse_error; |
| 584 |
} |
| 585 |
|
| 586 |
ob_end_clean(); |
| 587 |
|
| 588 |
do_action( 'code_snippets/after_execute_snippet', $code, $id, $result ); |
| 589 |
return $result; |
| 590 |
} |
| 591 |
|
| 592 |
/** |
| 593 |
* Run the active snippets. |
| 594 |
* Read-write-execute operation. |
| 595 |
* |
| 596 |
* @return bool true on success, false on failure. |
| 597 |
* |
| 598 |
* @since 2.0.0 |
| 599 |
*/ |
| 600 |
function execute_active_snippets(): bool { |
| 601 |
global $wpdb; |
| 602 |
|
| 603 |
// Bail early if safe mode is active. |
| 604 |
if ( defined( 'CODE_SNIPPETS_SAFE_MODE' ) && CODE_SNIPPETS_SAFE_MODE || |
| 605 |
! apply_filters( 'code_snippets/execute_snippets', true ) ) { |
| 606 |
return false; |
| 607 |
} |
| 608 |
|
| 609 |
$db = code_snippets()->db; |
| 610 |
$scopes = array( 'global', 'single-use', is_admin() ? 'admin' : 'front-end' ); |
| 611 |
$data = $db->fetch_active_snippets( $scopes ); |
| 612 |
|
| 613 |
// Detect if a snippet is currently being edited, and if so, spare it from execution. |
| 614 |
$edit_id = 0; |
| 615 |
$edit_table = $db->table; |
| 616 |
|
| 617 |
if ( wp_is_json_request() && ! empty( $_SERVER['REQUEST_URI'] ) ) { |
| 618 |
$url = wp_parse_url( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ); |
| 619 |
|
| 620 |
if ( isset( $url['path'] ) && false !== strpos( $url['path'], Snippets_REST_Controller::get_prefixed_base_route() ) ) { |
| 621 |
$path_parts = explode( '/', $url['path'] ); |
| 622 |
$edit_id = intval( end( $path_parts ) ); |
| 623 |
|
| 624 |
if ( ! empty( $url['query'] ) ) { |
| 625 |
wp_parse_str( $url['query'], $path_params ); |
| 626 |
$edit_table = isset( $path_params['network'] ) && rest_sanitize_boolean( $path_params['network'] ) ? |
| 627 |
$db->ms_table : $db->table; |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
foreach ( $data as $table_name => $active_snippets ) { |
| 633 |
|
| 634 |
// Loop through the returned snippets and execute the PHP code. |
| 635 |
foreach ( $active_snippets as $snippet ) { |
| 636 |
$snippet_id = intval( $snippet['id'] ); |
| 637 |
$code = $snippet['code']; |
| 638 |
|
| 639 |
// If the snippet is a single-use snippet, deactivate it before execution to ensure that the process always happens. |
| 640 |
if ( 'single-use' === $snippet['scope'] ) { |
| 641 |
$active_shared_ids = get_option( 'active_shared_network_snippets', array() ); |
| 642 |
|
| 643 |
if ( $table_name === $db->ms_table && is_array( $active_shared_ids ) && in_array( $snippet_id, $active_shared_ids, true ) ) { |
| 644 |
unset( $active_shared_ids[ array_search( $snippet_id, $active_shared_ids, true ) ] ); |
| 645 |
$active_shared_ids = array_values( $active_shared_ids ); |
| 646 |
update_option( 'active_shared_network_snippets', $active_shared_ids ); |
| 647 |
clean_active_snippets_cache( $table_name ); |
| 648 |
} else { |
| 649 |
$wpdb->update( |
| 650 |
$table_name, |
| 651 |
array( 'active' => '0' ), |
| 652 |
array( 'id' => $snippet_id ), |
| 653 |
array( '%d' ), |
| 654 |
array( '%d' ) |
| 655 |
); |
| 656 |
clean_snippets_cache( $table_name ); |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
if ( apply_filters( 'code_snippets/allow_execute_snippet', true, $snippet_id, $table_name ) && |
| 661 |
! ( $edit_id === $snippet_id && $table_name === $edit_table ) ) { |
| 662 |
execute_snippet( $code, $snippet_id ); |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
return true; |
| 668 |
} |
| 669 |
|
| 670 |
/** |
| 671 |
* Retrieve a single snippets from the database using its cloud ID. |
| 672 |
* |
| 673 |
* Read operation. |
| 674 |
* |
| 675 |
* @param string $cloud_id The Cloud ID of the snippet to retrieve. |
| 676 |
* @param boolean|null $multisite Retrieve a multisite-wide snippet (true) or site-wide snippet (false). |
| 677 |
* |
| 678 |
* @return Snippet|null A single snippet object or null if no snippet was found. |
| 679 |
* |
| 680 |
* @since 3.5.0 |
| 681 |
*/ |
| 682 |
function get_snippet_by_cloud_id( $cloud_id, $multisite = null ) { |
| 683 |
global $wpdb; |
| 684 |
|
| 685 |
$multisite = DB::validate_network_param( $multisite ); |
| 686 |
$table_name = code_snippets()->db->get_table_name( $multisite ); |
| 687 |
|
| 688 |
$cached_snippets = wp_cache_get( "all_snippets_$table_name", CACHE_GROUP ); |
| 689 |
|
| 690 |
// Attempt to fetch snippet from the cached list, if it exists. |
| 691 |
if ( is_array( $cached_snippets ) ) { |
| 692 |
foreach ( $cached_snippets as $snippet ) { |
| 693 |
if ( $snippet->cloud_id === $cloud_id ) { |
| 694 |
return apply_filters( 'code_snippets/get_snippet_by_cloud_id', $snippet, $cloud_id, $multisite ); |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
// Otherwise, search for the snippet from the database. |
| 700 |
$snippet_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_name WHERE cloud_id = %s", $cloud_id ) ); // cache pass, db call ok. |
| 701 |
$snippet = $snippet_data ? new Snippet( $snippet_data ) : null; |
| 702 |
|
| 703 |
return apply_filters( 'code_snippets/get_snippet_by_cloud_id', $snippet, $cloud_id, $multisite ); |
| 704 |
} |
| 705 |
|
| 706 |
/** |
| 707 |
* Update a snippet entry given a list of fields. |
| 708 |
* Write operation. |
| 709 |
* |
| 710 |
* @param int $snippet_id ID of the snippet to update. |
| 711 |
* @param array<string, mixed> $fields An array of fields mapped to their values. |
| 712 |
* @param bool|null $network Update in network-wide (true) or site-wide (false) table. |
| 713 |
*/ |
| 714 |
function update_snippet_fields( $snippet_id, $fields, $network = null ) { |
| 715 |
global $wpdb; |
| 716 |
|
| 717 |
$table = code_snippets()->db->get_table_name( $network ); |
| 718 |
|
| 719 |
// Build a new snippet object for the validation. |
| 720 |
$snippet = new Snippet(); |
| 721 |
$snippet->id = $snippet_id; |
| 722 |
|
| 723 |
// Validate fields through the snippet class and copy them into a clean array. |
| 724 |
$clean_fields = array(); |
| 725 |
|
| 726 |
foreach ( $fields as $field => $value ) { |
| 727 |
|
| 728 |
if ( $snippet->set_field( $field, $value ) ) { |
| 729 |
$clean_fields[ $field ] = $snippet->$field; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
// Update the snippet in the database. |
| 734 |
$wpdb->update( $table, $clean_fields, array( 'id' => $snippet->id ), null, array( '%d' ) ); |
| 735 |
|
| 736 |
do_action( 'code_snippets/update_snippet', $snippet->id, $table ); |
| 737 |
clean_snippets_cache( $table ); |
| 738 |
} |
| 739 |
|