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