Edit File by line
/home/zeestwma/ajeebong.../wp-conte.../plugins/code-sni.../php
File: class-db.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Code_Snippets;
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Functions used to manage the database tables.
[5] Fix | Delete
*
[6] Fix | Delete
* @package Code_Snippets
[7] Fix | Delete
*/
[8] Fix | Delete
class DB {
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* Unprefixed site-wide table name.
[12] Fix | Delete
*/
[13] Fix | Delete
public const TABLE_NAME = 'snippets';
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* Unprefixed network-wide table name.
[17] Fix | Delete
*/
[18] Fix | Delete
public const MS_TABLE_NAME = 'ms_snippets';
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Side-wide table name.
[22] Fix | Delete
*
[23] Fix | Delete
* @var string
[24] Fix | Delete
*/
[25] Fix | Delete
public string $table;
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Network-wide table name.
[29] Fix | Delete
*
[30] Fix | Delete
* @var string
[31] Fix | Delete
*/
[32] Fix | Delete
public string $ms_table;
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* Class constructor.
[36] Fix | Delete
*/
[37] Fix | Delete
public function __construct() {
[38] Fix | Delete
$this->set_table_vars();
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* Register the snippet table names with WordPress.
[43] Fix | Delete
*
[44] Fix | Delete
* @since 2.0
[45] Fix | Delete
*/
[46] Fix | Delete
public function set_table_vars() {
[47] Fix | Delete
global $wpdb;
[48] Fix | Delete
[49] Fix | Delete
$this->table = $wpdb->prefix . self::TABLE_NAME;
[50] Fix | Delete
$this->ms_table = $wpdb->base_prefix . self::MS_TABLE_NAME;
[51] Fix | Delete
[52] Fix | Delete
// Register the snippet table names with WordPress.
[53] Fix | Delete
$wpdb->snippets = $this->table;
[54] Fix | Delete
$wpdb->ms_snippets = $this->ms_table;
[55] Fix | Delete
[56] Fix | Delete
$wpdb->tables[] = self::TABLE_NAME;
[57] Fix | Delete
$wpdb->ms_global_tables[] = self::MS_TABLE_NAME;
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* Validate a provided 'network' or 'multisite' param, converting it to a boolean.
[62] Fix | Delete
*
[63] Fix | Delete
* @param bool|null $network Network argument value.
[64] Fix | Delete
*
[65] Fix | Delete
* @return bool Sanitized value.
[66] Fix | Delete
*/
[67] Fix | Delete
public static function validate_network_param( ?bool $network = null ): bool {
[68] Fix | Delete
[69] Fix | Delete
// If multisite is not active, then assume the value is false.
[70] Fix | Delete
if ( ! is_multisite() ) {
[71] Fix | Delete
return false;
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
// If $multisite is null, try to base it on the current admin page.
[75] Fix | Delete
if ( is_null( $network ) && function_exists( 'is_network_admin' ) ) {
[76] Fix | Delete
return is_network_admin();
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
return (bool) $network;
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
/**
[83] Fix | Delete
* Return the appropriate snippet table name
[84] Fix | Delete
*
[85] Fix | Delete
* @param bool|null $is_network Whether retrieve the multisite table name (true) or the site table name (false).
[86] Fix | Delete
*
[87] Fix | Delete
* @return string The snippet table name
[88] Fix | Delete
* @since 2.0
[89] Fix | Delete
*/
[90] Fix | Delete
public function get_table_name( ?bool $is_network = null ): string {
[91] Fix | Delete
$is_network = is_bool( $is_network ) ? $is_network : self::validate_network_param( $is_network );
[92] Fix | Delete
return $is_network ? $this->ms_table : $this->table;
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* Determine whether a database table exists.
[97] Fix | Delete
*
[98] Fix | Delete
* @param string $table_name Name of database table to check.
[99] Fix | Delete
* @param boolean $refresh Rerun the query, instead of using a cached value.
[100] Fix | Delete
*
[101] Fix | Delete
* @return bool Whether the database table exists.
[102] Fix | Delete
*/
[103] Fix | Delete
public static function table_exists( string $table_name, bool $refresh = false ): bool {
[104] Fix | Delete
global $wpdb;
[105] Fix | Delete
static $checked = array();
[106] Fix | Delete
[107] Fix | Delete
if ( $refresh || ! isset( $checked[ $table_name ] ) ) {
[108] Fix | Delete
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching, caching is handled through $checked variable.
[109] Fix | Delete
$result = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) ) );
[110] Fix | Delete
$checked[ $table_name ] = $result === $table_name;
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
return $checked[ $table_name ];
[114] Fix | Delete
}
[115] Fix | Delete
[116] Fix | Delete
/**
[117] Fix | Delete
* Create the snippet tables if they do not already exist
[118] Fix | Delete
*/
[119] Fix | Delete
public function create_missing_tables() {
[120] Fix | Delete
[121] Fix | Delete
// Create the network snippets table if it doesn't exist.
[122] Fix | Delete
if ( is_multisite() && ! self::table_exists( $this->ms_table ) ) {
[123] Fix | Delete
$this->create_table( $this->ms_table );
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
// Create the table if it doesn't exist.
[127] Fix | Delete
if ( ! self::table_exists( $this->table ) ) {
[128] Fix | Delete
$this->create_table( $this->table );
[129] Fix | Delete
}
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
/**
[133] Fix | Delete
* Create the snippet tables, or upgrade them if they already exist
[134] Fix | Delete
*/
[135] Fix | Delete
public function create_or_upgrade_tables() {
[136] Fix | Delete
if ( is_multisite() ) {
[137] Fix | Delete
$this->create_table( $this->ms_table );
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
$this->create_table( $this->table );
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
/**
[144] Fix | Delete
* Create a snippet table if it does not already exist
[145] Fix | Delete
*
[146] Fix | Delete
* @param string $table_name Name of database table.
[147] Fix | Delete
*/
[148] Fix | Delete
public static function create_missing_table( string $table_name ) {
[149] Fix | Delete
if ( ! self::table_exists( $table_name ) ) {
[150] Fix | Delete
self::create_table( $table_name );
[151] Fix | Delete
}
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
/**
[155] Fix | Delete
* Create a single snippet table.
[156] Fix | Delete
*
[157] Fix | Delete
* @param string $table_name The name of the table to create.
[158] Fix | Delete
*
[159] Fix | Delete
* @return bool Whether the table creation was successful.
[160] Fix | Delete
* @since 1.6
[161] Fix | Delete
* @uses dbDelta() to apply the SQL code
[162] Fix | Delete
*/
[163] Fix | Delete
public static function create_table( string $table_name ): bool {
[164] Fix | Delete
global $wpdb;
[165] Fix | Delete
$charset_collate = $wpdb->get_charset_collate();
[166] Fix | Delete
[167] Fix | Delete
/* Create the database table */
[168] Fix | Delete
$sql = "CREATE TABLE $table_name (
[169] Fix | Delete
id BIGINT(20) NOT NULL AUTO_INCREMENT,
[170] Fix | Delete
name TINYTEXT NOT NULL,
[171] Fix | Delete
description TEXT NOT NULL,
[172] Fix | Delete
code LONGTEXT NOT NULL,
[173] Fix | Delete
tags LONGTEXT NOT NULL,
[174] Fix | Delete
scope VARCHAR(15) NOT NULL DEFAULT 'global',
[175] Fix | Delete
condition_id BIGINT(20) NOT NULL DEFAULT 0,
[176] Fix | Delete
priority SMALLINT NOT NULL DEFAULT 10,
[177] Fix | Delete
active TINYINT(1) NOT NULL DEFAULT 0,
[178] Fix | Delete
modified DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
[179] Fix | Delete
revision BIGINT(20) NOT NULL DEFAULT 1,
[180] Fix | Delete
cloud_id VARCHAR(255) NULL,
[181] Fix | Delete
PRIMARY KEY (id),
[182] Fix | Delete
KEY scope (scope),
[183] Fix | Delete
KEY active (active)
[184] Fix | Delete
) $charset_collate;";
[185] Fix | Delete
[186] Fix | Delete
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
[187] Fix | Delete
dbDelta( $sql );
[188] Fix | Delete
[189] Fix | Delete
$success = empty( $wpdb->last_error );
[190] Fix | Delete
[191] Fix | Delete
if ( $success ) {
[192] Fix | Delete
do_action( 'code_snippets/create_table', $table_name );
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
return $success;
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
/**
[199] Fix | Delete
* Fetch a list of active snippets from a database table.
[200] Fix | Delete
*
[201] Fix | Delete
* @param string $table_name Name of table to fetch snippets from.
[202] Fix | Delete
* @param array<string> $scopes List of scopes to include in query.
[203] Fix | Delete
* @param boolean $active_only Whether to only fetch active snippets from the table.
[204] Fix | Delete
*
[205] Fix | Delete
* @return array<string, array<string, mixed>>|false List of active snippets, if any could be retrieved.
[206] Fix | Delete
*
[207] Fix | Delete
* @phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
[208] Fix | Delete
*/
[209] Fix | Delete
private static function fetch_snippets_from_table( string $table_name, array $scopes, bool $active_only = true ) {
[210] Fix | Delete
global $wpdb;
[211] Fix | Delete
[212] Fix | Delete
$cache_key = sprintf( 'active_snippets_%s_%s', sanitize_key( join( '_', $scopes ) ), $table_name );
[213] Fix | Delete
$cached_snippets = wp_cache_get( $cache_key, CACHE_GROUP );
[214] Fix | Delete
[215] Fix | Delete
if ( is_array( $cached_snippets ) ) {
[216] Fix | Delete
return $cached_snippets;
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
if ( ! self::table_exists( $table_name ) ) {
[220] Fix | Delete
return false;
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
$scopes_format = implode( ',', array_fill( 0, count( $scopes ), '%s' ) );
[224] Fix | Delete
$extra_where = $active_only ? 'AND active=1' : '';
[225] Fix | Delete
[226] Fix | Delete
$snippets = $wpdb->get_results(
[227] Fix | Delete
$wpdb->prepare(
[228] Fix | Delete
"
[229] Fix | Delete
SELECT id, code, scope, active, priority
[230] Fix | Delete
FROM $table_name
[231] Fix | Delete
WHERE scope IN ($scopes_format) $extra_where
[232] Fix | Delete
ORDER BY priority, id",
[233] Fix | Delete
$scopes
[234] Fix | Delete
),
[235] Fix | Delete
'ARRAY_A'
[236] Fix | Delete
);
[237] Fix | Delete
[238] Fix | Delete
// Cache the full list of snippets.
[239] Fix | Delete
if ( is_array( $snippets ) ) {
[240] Fix | Delete
wp_cache_set( $cache_key, $snippets, CACHE_GROUP );
[241] Fix | Delete
return $snippets;
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
return false;
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
/**
[248] Fix | Delete
* Sort the active snippets by priority, table, and ID.
[249] Fix | Delete
*
[250] Fix | Delete
* @param array $active_snippets List of active snippets to sort.
[251] Fix | Delete
*/
[252] Fix | Delete
private function sort_active_snippets( array &$active_snippets ): void {
[253] Fix | Delete
$comparisons = [
[254] Fix | Delete
function ( array $a, array $b ) {
[255] Fix | Delete
return $a['priority'] <=> $b['priority'];
[256] Fix | Delete
},
[257] Fix | Delete
function ( array $a, array $b ) {
[258] Fix | Delete
$a_table = $a['table'] === $this->ms_table ? 0 : 1;
[259] Fix | Delete
$b_table = $b['table'] === $this->ms_table ? 0 : 1;
[260] Fix | Delete
return $a_table <=> $b_table;
[261] Fix | Delete
},
[262] Fix | Delete
function ( array $a, array $b ) {
[263] Fix | Delete
return $a['id'] <=> $b['id'];
[264] Fix | Delete
},
[265] Fix | Delete
];
[266] Fix | Delete
[267] Fix | Delete
usort(
[268] Fix | Delete
$active_snippets,
[269] Fix | Delete
static function ( $a, $b ) use ( $comparisons ) {
[270] Fix | Delete
foreach ( $comparisons as $comparison ) {
[271] Fix | Delete
$result = $comparison( $a, $b );
[272] Fix | Delete
if ( 0 !== $result ) {
[273] Fix | Delete
return $result;
[274] Fix | Delete
}
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
return 0;
[278] Fix | Delete
}
[279] Fix | Delete
);
[280] Fix | Delete
}
[281] Fix | Delete
[282] Fix | Delete
/**
[283] Fix | Delete
* Generate the SQL for fetching active snippets from the database.
[284] Fix | Delete
*
[285] Fix | Delete
* @param string[] $scopes List of scopes to retrieve in.
[286] Fix | Delete
*
[287] Fix | Delete
* @return array{
[288] Fix | Delete
* id: int,
[289] Fix | Delete
* code: string,
[290] Fix | Delete
* scope: string,
[291] Fix | Delete
* table: string,
[292] Fix | Delete
* network: bool,
[293] Fix | Delete
* priority: int,
[294] Fix | Delete
* } List of active snippets.
[295] Fix | Delete
*/
[296] Fix | Delete
public function fetch_active_snippets( array $scopes ): array {
[297] Fix | Delete
$active_snippets = [];
[298] Fix | Delete
[299] Fix | Delete
// Fetch the active snippets for the current site, if there are any.
[300] Fix | Delete
$snippets = $this->fetch_snippets_from_table( $this->table, $scopes, true );
[301] Fix | Delete
if ( $snippets ) {
[302] Fix | Delete
foreach ( $snippets as $snippet ) {
[303] Fix | Delete
$active_snippets[] = [
[304] Fix | Delete
'id' => intval( $snippet['id'] ),
[305] Fix | Delete
'code' => $snippet['code'],
[306] Fix | Delete
'scope' => $snippet['scope'],
[307] Fix | Delete
'table' => $this->table,
[308] Fix | Delete
'network' => false,
[309] Fix | Delete
'priority' => intval( $snippet['priority'] ),
[310] Fix | Delete
];
[311] Fix | Delete
}
[312] Fix | Delete
}
[313] Fix | Delete
[314] Fix | Delete
// If multisite is enabled, fetch all snippets from the network table, and filter down to only active snippets.
[315] Fix | Delete
if ( is_multisite() ) {
[316] Fix | Delete
$ms_snippets = $this->fetch_snippets_from_table( $this->ms_table, $scopes, false );
[317] Fix | Delete
[318] Fix | Delete
if ( $ms_snippets ) {
[319] Fix | Delete
$active_shared_ids = get_option( 'active_shared_network_snippets', [] );
[320] Fix | Delete
$active_shared_ids = is_array( $active_shared_ids )
[321] Fix | Delete
? array_map( 'intval', $active_shared_ids )
[322] Fix | Delete
: [];
[323] Fix | Delete
[324] Fix | Delete
foreach ( $ms_snippets as $snippet ) {
[325] Fix | Delete
$id = intval( $snippet['id'] );
[326] Fix | Delete
[327] Fix | Delete
if ( ! $snippet['active'] && ! in_array( $id, $active_shared_ids, true ) ) {
[328] Fix | Delete
continue;
[329] Fix | Delete
}
[330] Fix | Delete
[331] Fix | Delete
$active_snippets[] = [
[332] Fix | Delete
'id' => $id,
[333] Fix | Delete
'code' => $snippet['code'],
[334] Fix | Delete
'scope' => $snippet['scope'],
[335] Fix | Delete
'table' => $this->ms_table,
[336] Fix | Delete
'network' => true,
[337] Fix | Delete
'priority' => intval( $snippet['priority'] ),
[338] Fix | Delete
];
[339] Fix | Delete
}
[340] Fix | Delete
[341] Fix | Delete
$this->sort_active_snippets( $active_snippets );
[342] Fix | Delete
}
[343] Fix | Delete
}
[344] Fix | Delete
[345] Fix | Delete
return $active_snippets;
[346] Fix | Delete
}
[347] Fix | Delete
}
[348] Fix | Delete
[349] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function