| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets; |
| 4 |
|
| 5 |
/** |
| 6 |
* Functions used to manage the database tables |
| 7 |
* |
| 8 |
* @package Code_Snippets |
| 9 |
*/ |
| 10 |
class DB { |
| 11 |
|
| 12 |
/** |
| 13 |
* Unprefixed site-wide table name |
| 14 |
*/ |
| 15 |
const TABLE_NAME = 'snippets'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Unprefixed network-wide table name |
| 19 |
*/ |
| 20 |
const MS_TABLE_NAME = 'ms_snippets'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Side-wide table name |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
public $table; |
| 28 |
|
| 29 |
/** |
| 30 |
* Network-wide table name |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
public $ms_table; |
| 35 |
|
| 36 |
/** |
| 37 |
* Class constructor |
| 38 |
*/ |
| 39 |
public function __construct() { |
| 40 |
$this->set_table_vars(); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Register the snippet table names with WordPress |
| 45 |
* |
| 46 |
* @since 2.0 |
| 47 |
*/ |
| 48 |
public function set_table_vars() { |
| 49 |
global $wpdb; |
| 50 |
|
| 51 |
$this->table = $wpdb->prefix . self::TABLE_NAME; |
| 52 |
$this->ms_table = $wpdb->base_prefix . self::MS_TABLE_NAME; |
| 53 |
|
| 54 |
/* Register the snippet table names with WordPress */ |
| 55 |
$wpdb->snippets = $this->table; |
| 56 |
$wpdb->ms_snippets = $this->ms_table; |
| 57 |
|
| 58 |
$wpdb->tables[] = self::TABLE_NAME; |
| 59 |
$wpdb->ms_global_tables[] = self::MS_TABLE_NAME; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Validate the multisite parameter of the get_table_name() function |
| 64 |
* |
| 65 |
* @param bool|null $network Value of multisite parameter – true for multisite, false for single-site. |
| 66 |
* |
| 67 |
* @return bool Validated value of multisite parameter. |
| 68 |
*/ |
| 69 |
public static function validate_network_param( $network ) { |
| 70 |
|
| 71 |
/* If multisite is not active, then the parameter should always be false */ |
| 72 |
if ( ! is_multisite() ) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
/* If $multisite is null, try to base it on the current admin page */ |
| 77 |
if ( is_null( $network ) && function_exists( 'is_network_admin' ) ) { |
| 78 |
$network = is_network_admin(); |
| 79 |
} |
| 80 |
|
| 81 |
return $network; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Return the appropriate snippet table name |
| 86 |
* |
| 87 |
* @param string|bool|null $multisite Whether retrieve the multisite table name (true) or the site table name (false). |
| 88 |
* |
| 89 |
* @return string The snippet table name |
| 90 |
* @since 2.0 |
| 91 |
*/ |
| 92 |
public function get_table_name( $multisite = null ) { |
| 93 |
|
| 94 |
/* If the first parameter is a string, assume it is a table name */ |
| 95 |
if ( is_string( $multisite ) ) { |
| 96 |
return $multisite; |
| 97 |
} |
| 98 |
|
| 99 |
/* Validate the multisite parameter */ |
| 100 |
$multisite = $this->validate_network_param( $multisite ); |
| 101 |
|
| 102 |
/* return the correct table name depending on the value of $multisite */ |
| 103 |
return $multisite ? $this->ms_table : $this->table; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Determine whether a database table exists. |
| 108 |
* |
| 109 |
* @param string $table_name Name of database table to check. |
| 110 |
* |
| 111 |
* @return bool Whether the database table exists. |
| 112 |
*/ |
| 113 |
public static function table_exists( $table_name ) { |
| 114 |
global $wpdb; |
| 115 |
return $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) === $table_name; // cache pass, db call ok. |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Create the snippet tables if they do not already exist |
| 120 |
*/ |
| 121 |
public function create_missing_tables() { |
| 122 |
|
| 123 |
/* Create the network snippets table if it doesn't exist */ |
| 124 |
if ( is_multisite() && ! self::table_exists( $this->ms_table ) ) { |
| 125 |
$this->create_table( $this->ms_table ); |
| 126 |
} |
| 127 |
|
| 128 |
/* Create the table if it doesn't exist */ |
| 129 |
if ( ! self::table_exists( $this->table ) ) { |
| 130 |
$this->create_table( $this->table ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Create the snippet tables, or upgrade them if they already exist |
| 136 |
*/ |
| 137 |
public function create_or_upgrade_tables() { |
| 138 |
if ( is_multisite() ) { |
| 139 |
$this->create_table( $this->ms_table ); |
| 140 |
} |
| 141 |
|
| 142 |
$this->create_table( $this->table ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Create a snippet table if it does not already exist |
| 147 |
* |
| 148 |
* @param string $table_name Name of database table. |
| 149 |
*/ |
| 150 |
public static function create_missing_table( $table_name ) { |
| 151 |
|
| 152 |
if ( self::table_exists( $table_name ) ) { |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
self::create_table( $table_name ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Create a single snippet table. |
| 161 |
* |
| 162 |
* @param string $table_name The name of the table to create. |
| 163 |
* |
| 164 |
* @return bool Whether the table creation was successful. |
| 165 |
* @since 1.6 |
| 166 |
* @uses dbDelta() to apply the SQL code |
| 167 |
*/ |
| 168 |
public static function create_table( $table_name ) { |
| 169 |
global $wpdb; |
| 170 |
$charset_collate = $wpdb->get_charset_collate(); |
| 171 |
|
| 172 |
/* Create the database table */ |
| 173 |
$sql = "CREATE TABLE $table_name ( |
| 174 |
id BIGINT(20) NOT NULL AUTO_INCREMENT, |
| 175 |
name TINYTEXT NOT NULL DEFAULT '', |
| 176 |
description TEXT NOT NULL DEFAULT '', |
| 177 |
code LONGTEXT NOT NULL DEFAULT '', |
| 178 |
tags LONGTEXT NOT NULL DEFAULT '', |
| 179 |
scope VARCHAR(15) NOT NULL DEFAULT 'global', |
| 180 |
priority SMALLINT NOT NULL DEFAULT 10, |
| 181 |
active TINYINT(1) NOT NULL DEFAULT 0, |
| 182 |
modified DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 183 |
PRIMARY KEY (id), |
| 184 |
KEY scope (scope), |
| 185 |
KEY active (active) |
| 186 |
) $charset_collate;"; |
| 187 |
|
| 188 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 189 |
dbDelta( $sql ); |
| 190 |
|
| 191 |
$success = empty( $wpdb->last_error ); |
| 192 |
|
| 193 |
if ( $success ) { |
| 194 |
do_action( 'code_snippets/create_table', $table_name ); |
| 195 |
} |
| 196 |
|
| 197 |
return $success; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Build a list of formatting placeholders for an array of data. |
| 202 |
* |
| 203 |
* @param int $count Length of data. |
| 204 |
* @param string $placeholder Placeholder to use. Defaults to string placeholder. |
| 205 |
* |
| 206 |
* @return string List of placeholders, ready for inclusion in query. |
| 207 |
*/ |
| 208 |
private static function build_format_list( $count, $placeholder = '%s' ) { |
| 209 |
return implode( ',', array_fill( 0, $count, $placeholder ) ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Fetch a list of active snippets from a database table. |
| 214 |
* |
| 215 |
* @param string $table_name Name of table to fetch snippets from. |
| 216 |
* @param array $scopes List of scopes to include in query. |
| 217 |
* @param array $active_only Whether to only fetch active snippets from the table. |
| 218 |
* |
| 219 |
* @return array|false List of active snippets, if any could be retrieved. |
| 220 |
* |
| 221 |
* @phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 222 |
*/ |
| 223 |
private static function fetch_snippets_from_table( $table_name, array $scopes, $active_only = true ) { |
| 224 |
global $wpdb; |
| 225 |
|
| 226 |
$cache_key = sprintf( 'active_snippets_%s_%s', sanitize_key( join( '_', $scopes ) ), $table_name ); |
| 227 |
$cached_snippets = wp_cache_get( $cache_key, CACHE_GROUP ); |
| 228 |
|
| 229 |
if ( is_array( $cached_snippets ) ) { |
| 230 |
return $cached_snippets; |
| 231 |
} |
| 232 |
|
| 233 |
if ( ! self::table_exists( $table_name ) ) { |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
$scopes_format = self::build_format_list( count( $scopes ) ); |
| 238 |
$extra_where = $active_only ? 'AND active=1' : ''; |
| 239 |
|
| 240 |
$snippets = $wpdb->get_results( |
| 241 |
$wpdb->prepare( |
| 242 |
" |
| 243 |
SELECT id, code, scope, active |
| 244 |
FROM $table_name |
| 245 |
WHERE scope IN ($scopes_format) $extra_where |
| 246 |
ORDER BY priority, id", |
| 247 |
$scopes |
| 248 |
), |
| 249 |
'ARRAY_A' |
| 250 |
); // db call ok. |
| 251 |
|
| 252 |
// Cache the full list of snippets. |
| 253 |
if ( is_array( $snippets ) ) { |
| 254 |
wp_cache_set( $cache_key, $snippets, CACHE_GROUP ); |
| 255 |
return $snippets; |
| 256 |
} |
| 257 |
|
| 258 |
return false; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Generate the SQL for fetching active snippets from the database. |
| 263 |
* |
| 264 |
* @param array|string $scopes List of scopes to retrieve in. |
| 265 |
* |
| 266 |
* @return array List of active snippets, indexed by table. |
| 267 |
*/ |
| 268 |
public function fetch_active_snippets( $scopes ) { |
| 269 |
$active_snippets = array(); |
| 270 |
|
| 271 |
// Ensure that the list of scopes is an array. |
| 272 |
if ( ! is_array( $scopes ) ) { |
| 273 |
$scopes = array( $scopes ); |
| 274 |
} |
| 275 |
|
| 276 |
// Fetch the active snippets for the current site, if there are any. |
| 277 |
$snippets = $this->fetch_snippets_from_table( $this->table, $scopes ); |
| 278 |
if ( $snippets ) { |
| 279 |
$active_snippets[ $this->table ] = $snippets; |
| 280 |
} |
| 281 |
|
| 282 |
// If multisite is enabled, fetch all snippets from the network table, and filter down to only active snippets. |
| 283 |
if ( is_multisite() ) { |
| 284 |
$active_shared_ids = (array) get_option( 'active_shared_network_snippets', array() ); |
| 285 |
$ms_snippets = $this->fetch_snippets_from_table( $this->ms_table, $scopes, false ); |
| 286 |
|
| 287 |
if ( $ms_snippets ) { |
| 288 |
$active_snippets[ $this->ms_table ] = array_filter( |
| 289 |
$ms_snippets, |
| 290 |
function ( $snippet ) use ( $active_shared_ids ) { |
| 291 |
return $snippet['active'] || in_array( intval( $snippet['id'] ), $active_shared_ids, true ); |
| 292 |
} |
| 293 |
); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
return $active_snippets; |
| 298 |
} |
| 299 |
} |
| 300 |
|