Edit File by line
/home/zeestwma/ajeebong.../wp-conte.../plugins/code-sni.../php
File: snippet-ops.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Functions to perform snippet operations
[2] Fix | Delete
*
[3] Fix | Delete
* @package Code_Snippets
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
namespace Code_Snippets;
[7] Fix | Delete
[8] Fix | Delete
use ParseError;
[9] Fix | Delete
use function Code_Snippets\Settings\get_self_option;
[10] Fix | Delete
use function Code_Snippets\Settings\update_self_option;
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Clean the cache where active snippets are stored.
[14] Fix | Delete
*
[15] Fix | Delete
* @param string $table_name Snippets table name.
[16] Fix | Delete
* @param array<string>|false $scopes List of scopes. Optional. If not provided, will flush the cache for all scopes.
[17] Fix | Delete
*
[18] Fix | Delete
* @return void
[19] Fix | Delete
*/
[20] Fix | Delete
function clean_active_snippets_cache( string $table_name, $scopes = false ) {
[21] Fix | Delete
$scope_groups = $scopes ? [ $scopes ] : [
[22] Fix | Delete
[ 'head-content', 'footer-content' ],
[23] Fix | Delete
[ 'global', 'single-use', 'front-end' ],
[24] Fix | Delete
[ 'global', 'single-use', 'admin' ],
[25] Fix | Delete
];
[26] Fix | Delete
[27] Fix | Delete
foreach ( $scope_groups as $scopes ) {
[28] Fix | Delete
wp_cache_delete( sprintf( 'active_snippets_%s_%s', sanitize_key( join( '_', $scopes ) ), $table_name ), CACHE_GROUP );
[29] Fix | Delete
}
[30] Fix | Delete
}
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* Flush all snippets caches for a given database table.
[34] Fix | Delete
*
[35] Fix | Delete
* @param string $table_name Snippets table name.
[36] Fix | Delete
*
[37] Fix | Delete
* @return void
[38] Fix | Delete
*/
[39] Fix | Delete
function clean_snippets_cache( string $table_name ) {
[40] Fix | Delete
wp_cache_delete( "all_snippet_tags_$table_name", CACHE_GROUP );
[41] Fix | Delete
wp_cache_delete( "all_snippets_$table_name", CACHE_GROUP );
[42] Fix | Delete
clean_active_snippets_cache( $table_name );
[43] Fix | Delete
}
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* Retrieve a list of snippets from the database.
[47] Fix | Delete
* Read operation.
[48] Fix | Delete
*
[49] Fix | Delete
* @param array<string> $ids The IDs of the snippets to fetch.
[50] Fix | Delete
* @param bool|null $network Retrieve multisite-wide snippets (true) or site-wide snippets (false).
[51] Fix | Delete
*
[52] Fix | Delete
* @return array<Snippet> List of Snippet objects.
[53] Fix | Delete
*
[54] Fix | Delete
* @since 2.0
[55] Fix | Delete
*/
[56] Fix | Delete
function get_snippets( array $ids = array(), ?bool $network = null ): array {
[57] Fix | Delete
global $wpdb;
[58] Fix | Delete
[59] Fix | Delete
// If only one ID has been passed in, defer to the get_snippet() function.
[60] Fix | Delete
$ids_count = count( $ids );
[61] Fix | Delete
if ( 1 === $ids_count ) {
[62] Fix | Delete
return array( get_snippet( $ids[0], $network ) );
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
$network = DB::validate_network_param( $network );
[66] Fix | Delete
$table_name = code_snippets()->db->get_table_name( $network );
[67] Fix | Delete
[68] Fix | Delete
$snippets = wp_cache_get( "all_snippets_$table_name", CACHE_GROUP );
[69] Fix | Delete
[70] Fix | Delete
// Fetch all snippets from the database if none are cached.
[71] Fix | Delete
if ( ! is_array( $snippets ) ) {
[72] Fix | Delete
$results = $wpdb->get_results( "SELECT * FROM $table_name", ARRAY_A );
[73] Fix | Delete
[74] Fix | Delete
$snippets = $results ?
[75] Fix | Delete
array_map(
[76] Fix | Delete
function ( $snippet_data ) use ( $network ) {
[77] Fix | Delete
$snippet_data['network'] = $network;
[78] Fix | Delete
return new Snippet( $snippet_data );
[79] Fix | Delete
},
[80] Fix | Delete
$results
[81] Fix | Delete
) :
[82] Fix | Delete
array();
[83] Fix | Delete
[84] Fix | Delete
$snippets = apply_filters( 'code_snippets/get_snippets', $snippets, $network );
[85] Fix | Delete
[86] Fix | Delete
if ( 0 === $ids_count ) {
[87] Fix | Delete
wp_cache_set( "all_snippets_$table_name", $snippets, CACHE_GROUP );
[88] Fix | Delete
}
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
// If a list of IDs are provided, narrow down the snippets list.
[92] Fix | Delete
if ( $ids_count > 0 ) {
[93] Fix | Delete
$ids = array_map( 'intval', $ids );
[94] Fix | Delete
return array_values(
[95] Fix | Delete
array_filter(
[96] Fix | Delete
$snippets,
[97] Fix | Delete
function ( Snippet $snippet ) use ( $ids ) {
[98] Fix | Delete
return in_array( $snippet->id, $ids, true );
[99] Fix | Delete
}
[100] Fix | Delete
)
[101] Fix | Delete
);
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
return $snippets;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
/**
[108] Fix | Delete
* Gets all used tags from the database.
[109] Fix | Delete
* Read operation.
[110] Fix | Delete
*
[111] Fix | Delete
* @since 2.0
[112] Fix | Delete
*/
[113] Fix | Delete
function get_all_snippet_tags() {
[114] Fix | Delete
global $wpdb;
[115] Fix | Delete
$table_name = code_snippets()->db->get_table_name();
[116] Fix | Delete
$cache_key = "all_snippet_tags_$table_name";
[117] Fix | Delete
[118] Fix | Delete
$tags = wp_cache_get( $cache_key, CACHE_GROUP );
[119] Fix | Delete
if ( $tags ) {
[120] Fix | Delete
return $tags;
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
// Grab all tags from the database.
[124] Fix | Delete
$tags = array();
[125] Fix | Delete
$all_tags = $wpdb->get_col( "SELECT tags FROM $table_name" );
[126] Fix | Delete
[127] Fix | Delete
// Merge all tags into a single array.
[128] Fix | Delete
foreach ( $all_tags as $snippet_tags ) {
[129] Fix | Delete
$snippet_tags = code_snippets_build_tags_array( $snippet_tags );
[130] Fix | Delete
$tags = array_merge( $snippet_tags, $tags );
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
// Remove duplicate tags.
[134] Fix | Delete
$tags = array_values( array_unique( $tags, SORT_REGULAR ) );
[135] Fix | Delete
wp_cache_set( $cache_key, $tags, CACHE_GROUP );
[136] Fix | Delete
return $tags;
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
/**
[140] Fix | Delete
* Make sure that the tags are a valid array.
[141] Fix | Delete
*
[142] Fix | Delete
* @param array|string $tags The tags to convert into an array.
[143] Fix | Delete
*
[144] Fix | Delete
* @return array<string> The converted tags.
[145] Fix | Delete
*
[146] Fix | Delete
* @since 2.0.0
[147] Fix | Delete
*/
[148] Fix | Delete
function code_snippets_build_tags_array( $tags ): array {
[149] Fix | Delete
[150] Fix | Delete
/* If there are no tags set, return an empty array. */
[151] Fix | Delete
if ( empty( $tags ) ) {
[152] Fix | Delete
return array();
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
/* If the tags are set as a string, convert them into an array. */
[156] Fix | Delete
if ( is_string( $tags ) ) {
[157] Fix | Delete
$tags = wp_strip_all_tags( $tags );
[158] Fix | Delete
$tags = str_replace( ', ', ',', $tags );
[159] Fix | Delete
$tags = explode( ',', $tags );
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
/* If we still don't have an array, just convert whatever we do have into one. */
[163] Fix | Delete
return (array) $tags;
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
/**
[167] Fix | Delete
* Retrieve a single snippets from the database.
[168] Fix | Delete
* Will return empty snippet object if no snippet ID is specified.
[169] Fix | Delete
* Read operation.
[170] Fix | Delete
*
[171] Fix | Delete
* @param int $id The ID of the snippet to retrieve. 0 to build a new snippet.
[172] Fix | Delete
* @param bool|null $network Retrieve a multisite-wide snippet (true) or site-wide snippet (false).
[173] Fix | Delete
*
[174] Fix | Delete
* @return Snippet A single snippet object.
[175] Fix | Delete
*
[176] Fix | Delete
* @since 2.0.0
[177] Fix | Delete
*/
[178] Fix | Delete
function get_snippet( int $id = 0, ?bool $network = null ): Snippet {
[179] Fix | Delete
global $wpdb;
[180] Fix | Delete
[181] Fix | Delete
$id = absint( $id );
[182] Fix | Delete
$network = DB::validate_network_param( $network );
[183] Fix | Delete
$table_name = code_snippets()->db->get_table_name( $network );
[184] Fix | Delete
[185] Fix | Delete
if ( 0 === $id ) {
[186] Fix | Delete
// If an invalid ID is provided, then return an empty snippet object.
[187] Fix | Delete
$snippet = new Snippet();
[188] Fix | Delete
[189] Fix | Delete
} else {
[190] Fix | Delete
$cached_snippets = wp_cache_get( "all_snippets_$table_name", CACHE_GROUP );
[191] Fix | Delete
[192] Fix | Delete
// Attempt to fetch snippet from the cached list, if it exists.
[193] Fix | Delete
if ( is_array( $cached_snippets ) ) {
[194] Fix | Delete
foreach ( $cached_snippets as $snippet ) {
[195] Fix | Delete
if ( $snippet->id === $id ) {
[196] Fix | Delete
return apply_filters( 'code_snippets/get_snippet', $snippet, $id, $network );
[197] Fix | Delete
}
[198] Fix | Delete
}
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
// Otherwise, retrieve the snippet from the database.
[202] Fix | Delete
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
[203] Fix | Delete
$snippet_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_name WHERE id = %d", $id ) );
[204] Fix | Delete
$snippet = new Snippet( $snippet_data );
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
$snippet->network = $network;
[208] Fix | Delete
return apply_filters( 'code_snippets/get_snippet', $snippet, $id, $network );
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
[212] Fix | Delete
/**
[213] Fix | Delete
* Ensure the list of shared network snippets is correct if one has been recently activated or deactivated.
[214] Fix | Delete
* Write operation.
[215] Fix | Delete
*
[216] Fix | Delete
* @access private
[217] Fix | Delete
*
[218] Fix | Delete
* @param Snippet[] $snippets Snippets that was recently updated.
[219] Fix | Delete
*
[220] Fix | Delete
* @return boolean Whether an update was performed.
[221] Fix | Delete
*/
[222] Fix | Delete
function update_shared_network_snippets( array $snippets ): bool {
[223] Fix | Delete
$shared_ids = [];
[224] Fix | Delete
$unshared_ids = [];
[225] Fix | Delete
[226] Fix | Delete
if ( ! is_multisite() ) {
[227] Fix | Delete
return false;
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
foreach ( $snippets as $snippet ) {
[231] Fix | Delete
if ( $snippet->network ) {
[232] Fix | Delete
if ( $snippet->shared_network ) {
[233] Fix | Delete
$shared_ids[] = $snippet->id;
[234] Fix | Delete
} else {
[235] Fix | Delete
$unshared_ids[] = $snippet->id;
[236] Fix | Delete
}
[237] Fix | Delete
}
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
if ( ! $shared_ids && ! $unshared_ids ) {
[241] Fix | Delete
return false;
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
$existing_shared_ids = get_site_option( 'shared_network_snippets', [] );
[245] Fix | Delete
$updated_shared_ids = array_values( array_diff( array_merge( $existing_shared_ids, $shared_ids ), $unshared_ids ) );
[246] Fix | Delete
[247] Fix | Delete
if ( $existing_shared_ids === $updated_shared_ids ) {
[248] Fix | Delete
return false;
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
update_site_option( 'shared_network_snippets', $updated_shared_ids );
[252] Fix | Delete
[253] Fix | Delete
// Deactivate the snippet on all sites if necessary.
[254] Fix | Delete
if ( $unshared_ids ) {
[255] Fix | Delete
$sites = get_sites( [ 'fields' => 'ids' ] );
[256] Fix | Delete
[257] Fix | Delete
foreach ( $sites as $site ) {
[258] Fix | Delete
switch_to_blog( $site );
[259] Fix | Delete
$active_shared_ids = get_option( 'active_shared_network_snippets' );
[260] Fix | Delete
[261] Fix | Delete
if ( is_array( $active_shared_ids ) ) {
[262] Fix | Delete
$active_shared_ids = array_diff( $active_shared_ids, $unshared_ids );
[263] Fix | Delete
update_option( 'active_shared_network_snippets', $active_shared_ids );
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
clean_active_snippets_cache( code_snippets()->db->ms_table );
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
restore_current_blog();
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
return true;
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
/**
[276] Fix | Delete
* Activates a snippet.
[277] Fix | Delete
* Write operation.
[278] Fix | Delete
*
[279] Fix | Delete
* @param int $id ID of the snippet to activate.
[280] Fix | Delete
* @param bool|null $network Whether the snippets are multisite-wide (true) or site-wide (false).
[281] Fix | Delete
*
[282] Fix | Delete
* @return Snippet|string Snippet object on success, error message on failure.
[283] Fix | Delete
* @since 2.0.0
[284] Fix | Delete
*/
[285] Fix | Delete
function activate_snippet( int $id, ?bool $network = null ) {
[286] Fix | Delete
global $wpdb;
[287] Fix | Delete
$network = DB::validate_network_param( $network );
[288] Fix | Delete
$table_name = code_snippets()->db->get_table_name( $network );
[289] Fix | Delete
[290] Fix | Delete
// Retrieve the snippet code from the database for validation before activating.
[291] Fix | Delete
$snippet = get_snippet( $id, $network );
[292] Fix | Delete
[293] Fix | Delete
if ( 0 === $snippet->id ) {
[294] Fix | Delete
// translators: %d: snippet identifier.
[295] Fix | Delete
return sprintf( __( 'Could not locate snippet with ID %d.', 'code-snippets' ), $id );
[296] Fix | Delete
}
[297] Fix | Delete
[298] Fix | Delete
if('php' == $snippet->type ){
[299] Fix | Delete
$validator = new Validator( $snippet->code );
[300] Fix | Delete
if ( $validator->validate() ) {
[301] Fix | Delete
return __( 'Could not activate snippet: code did not pass validation.', 'code-snippets' );
[302] Fix | Delete
}
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
$result = $wpdb->update(
[306] Fix | Delete
$table_name,
[307] Fix | Delete
array( 'active' => '1' ),
[308] Fix | Delete
array( 'id' => $id ),
[309] Fix | Delete
array( '%d' ),
[310] Fix | Delete
array( '%d' )
[311] Fix | Delete
);
[312] Fix | Delete
[313] Fix | Delete
if ( ! $result ) {
[314] Fix | Delete
return __( 'Could not activate snippet.', 'code-snippets' );
[315] Fix | Delete
}
[316] Fix | Delete
[317] Fix | Delete
update_shared_network_snippets( [ $snippet ] );
[318] Fix | Delete
do_action( 'code_snippets/activate_snippet', $snippet, $network );
[319] Fix | Delete
clean_snippets_cache( $table_name );
[320] Fix | Delete
return $snippet;
[321] Fix | Delete
}
[322] Fix | Delete
[323] Fix | Delete
/**
[324] Fix | Delete
* Activates multiple snippets.
[325] Fix | Delete
* Write operation.
[326] Fix | Delete
*
[327] Fix | Delete
* @param array<integer> $ids The IDs of the snippets to activate.
[328] Fix | Delete
* @param bool|null $network Whether the snippets are multisite-wide (true) or site-wide (false).
[329] Fix | Delete
*
[330] Fix | Delete
* @return Snippet[]|null Snippets which were successfully activated, or null on failure.
[331] Fix | Delete
*
[332] Fix | Delete
* @since 2.0.0
[333] Fix | Delete
*/
[334] Fix | Delete
function activate_snippets( array $ids, ?bool $network = null ): ?array {
[335] Fix | Delete
global $wpdb;
[336] Fix | Delete
$network = DB::validate_network_param( $network );
[337] Fix | Delete
$table_name = code_snippets()->db->get_table_name( $network );
[338] Fix | Delete
[339] Fix | Delete
$snippets = get_snippets( $ids, $network );
[340] Fix | Delete
[341] Fix | Delete
if ( ! $snippets ) {
[342] Fix | Delete
return null;
[343] Fix | Delete
}
[344] Fix | Delete
[345] Fix | Delete
// Loop through each snippet code and validate individually.
[346] Fix | Delete
$valid_ids = [];
[347] Fix | Delete
$valid_snippets = [];
[348] Fix | Delete
[349] Fix | Delete
foreach ( $snippets as $snippet ) {
[350] Fix | Delete
$validator = new Validator( $snippet->code );
[351] Fix | Delete
$code_error = $validator->validate();
[352] Fix | Delete
[353] Fix | Delete
if ( ! $code_error ) {
[354] Fix | Delete
$valid_ids[] = $snippet->id;
[355] Fix | Delete
$valid_snippets[] = $snippet;
[356] Fix | Delete
}
[357] Fix | Delete
}
[358] Fix | Delete
[359] Fix | Delete
// If there are no valid snippets, then we're done.
[360] Fix | Delete
if ( ! $valid_ids ) {
[361] Fix | Delete
return null;
[362] Fix | Delete
}
[363] Fix | Delete
[364] Fix | Delete
// Build a SQL query containing all IDs, as wpdb::update does not support OR conditionals.
[365] Fix | Delete
$ids_format = implode( ',', array_fill( 0, count( $valid_ids ), '%d' ) );
[366] Fix | Delete
[367] Fix | Delete
// phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
[368] Fix | Delete
$rows_updated = $wpdb->query( $wpdb->prepare( "UPDATE $table_name SET active = 1 WHERE id IN ($ids_format)", $valid_ids ) );
[369] Fix | Delete
[370] Fix | Delete
if ( ! $rows_updated ) {
[371] Fix | Delete
return null;
[372] Fix | Delete
}
[373] Fix | Delete
[374] Fix | Delete
update_shared_network_snippets( $valid_snippets );
[375] Fix | Delete
do_action( 'code_snippets/activate_snippets', $valid_snippets, $table_name );
[376] Fix | Delete
clean_snippets_cache( $table_name );
[377] Fix | Delete
return $valid_ids;
[378] Fix | Delete
}
[379] Fix | Delete
[380] Fix | Delete
/**
[381] Fix | Delete
* Deactivate a snippet.
[382] Fix | Delete
* Write operation.
[383] Fix | Delete
*
[384] Fix | Delete
* @param int $id ID of the snippet to deactivate.
[385] Fix | Delete
* @param bool|null $network Whether the snippets are multisite-wide (true) or site-wide (false).
[386] Fix | Delete
*
[387] Fix | Delete
* @return Snippet|null Snippet that was deactivated on success, or null on failure.
[388] Fix | Delete
*
[389] Fix | Delete
* @since 2.0.0
[390] Fix | Delete
*/
[391] Fix | Delete
function deactivate_snippet( int $id, ?bool $network = null ): ?Snippet {
[392] Fix | Delete
global $wpdb;
[393] Fix | Delete
$network = DB::validate_network_param( $network );
[394] Fix | Delete
$table = code_snippets()->db->get_table_name( $network );
[395] Fix | Delete
[396] Fix | Delete
// Set the snippet to inactive.
[397] Fix | Delete
$result = $wpdb->update(
[398] Fix | Delete
$table,
[399] Fix | Delete
array( 'active' => '0' ),
[400] Fix | Delete
array( 'id' => $id ),
[401] Fix | Delete
array( '%d' ),
[402] Fix | Delete
array( '%d' )
[403] Fix | Delete
);
[404] Fix | Delete
[405] Fix | Delete
if ( ! $result ) {
[406] Fix | Delete
return null;
[407] Fix | Delete
}
[408] Fix | Delete
[409] Fix | Delete
// Update the recently active list.
[410] Fix | Delete
$snippet = get_snippet( $id );
[411] Fix | Delete
$recently_active = [ $id => time() ] + get_self_option( $network, 'recently_activated_snippets', [] );
[412] Fix | Delete
update_self_option( $network, 'recently_activated_snippets', $recently_active );
[413] Fix | Delete
[414] Fix | Delete
update_shared_network_snippets( [ $snippet ] );
[415] Fix | Delete
do_action( 'code_snippets/deactivate_snippet', $id, $network );
[416] Fix | Delete
clean_snippets_cache( $table );
[417] Fix | Delete
[418] Fix | Delete
return $snippet;
[419] Fix | Delete
}
[420] Fix | Delete
[421] Fix | Delete
/**
[422] Fix | Delete
* Deletes a snippet from the database.
[423] Fix | Delete
* Write operation.
[424] Fix | Delete
*
[425] Fix | Delete
* @param int $id ID of the snippet to delete.
[426] Fix | Delete
* @param bool|null $network Delete from network-wide (true) or site-wide (false) table.
[427] Fix | Delete
*
[428] Fix | Delete
* @return bool Whether the snippet was deleted successfully.
[429] Fix | Delete
*
[430] Fix | Delete
* @since 2.0.0
[431] Fix | Delete
*/
[432] Fix | Delete
function delete_snippet( int $id, ?bool $network = null ): bool {
[433] Fix | Delete
global $wpdb;
[434] Fix | Delete
$network = DB::validate_network_param( $network );
[435] Fix | Delete
$table = code_snippets()->db->get_table_name( $network );
[436] Fix | Delete
[437] Fix | Delete
$snippet = get_snippet( $id, $network );
[438] Fix | Delete
[439] Fix | Delete
$result = $wpdb->delete(
[440] Fix | Delete
$table,
[441] Fix | Delete
array( 'id' => $id ),
[442] Fix | Delete
array( '%d' )
[443] Fix | Delete
);
[444] Fix | Delete
[445] Fix | Delete
if ( $result ) {
[446] Fix | Delete
do_action( 'code_snippets/delete_snippet', $snippet, $network );
[447] Fix | Delete
clean_snippets_cache( $table );
[448] Fix | Delete
code_snippets()->cloud_api->delete_snippet_from_transient_data( $id );
[449] Fix | Delete
}
[450] Fix | Delete
[451] Fix | Delete
return (bool) $result;
[452] Fix | Delete
}
[453] Fix | Delete
[454] Fix | Delete
/**
[455] Fix | Delete
* Trashes a snippet from the database.
[456] Fix | Delete
* Write operation.
[457] Fix | Delete
*
[458] Fix | Delete
* @param int $id ID of the snippet to trash.
[459] Fix | Delete
* @param bool|null $network Trash from network-wide (true) or site-wide (false) table.
[460] Fix | Delete
*
[461] Fix | Delete
* @return bool Whether the snippet was trashed successfully.
[462] Fix | Delete
*
[463] Fix | Delete
* @since 3.8.0
[464] Fix | Delete
*/
[465] Fix | Delete
function trash_snippet( int $id, ?bool $network = null ): bool {
[466] Fix | Delete
global $wpdb;
[467] Fix | Delete
$network = DB::validate_network_param( $network );
[468] Fix | Delete
$table = code_snippets()->db->get_table_name( $network );
[469] Fix | Delete
[470] Fix | Delete
$snippet = get_snippet( $id, $network );
[471] Fix | Delete
[472] Fix | Delete
$result = $wpdb->update(
[473] Fix | Delete
$table,
[474] Fix | Delete
array( 'active' => '-1' ),
[475] Fix | Delete
array( 'id' => $id ),
[476] Fix | Delete
array( '%d' )
[477] Fix | Delete
);
[478] Fix | Delete
[479] Fix | Delete
if ( $result ) {
[480] Fix | Delete
do_action( 'code_snippets/trash_snippet', $snippet, $network );
[481] Fix | Delete
clean_snippets_cache( $table );
[482] Fix | Delete
code_snippets()->cloud_api->delete_snippet_from_transient_data( $id );
[483] Fix | Delete
}
[484] Fix | Delete
[485] Fix | Delete
return (bool) $result;
[486] Fix | Delete
}
[487] Fix | Delete
[488] Fix | Delete
/**
[489] Fix | Delete
* Restore a trashed snippet by setting its active status back to 0 (inactive).
[490] Fix | Delete
* Write operation.
[491] Fix | Delete
*
[492] Fix | Delete
* @param int $id Snippet ID to restore.
[493] Fix | Delete
* @param bool|null $network Whether the snippet is multisite-wide (true) or site-wide (false).
[494] Fix | Delete
*
[495] Fix | Delete
* @return bool Whether the restore was successful.
[496] Fix | Delete
*
[497] Fix | Delete
* @since 3.8.0
[498] Fix | Delete
*/
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function