| 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 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 |
* @phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching |
| 114 |
*/ |
| 115 |
public static function table_exists( $table_name ) { |
| 116 |
global $wpdb; |
| 117 |
return $wpdb->get_var( sprintf( "SHOW TABLES LIKE '%s'", $table_name ) ) === $table_name; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Create the snippet tables if they do not already exist |
| 122 |
*/ |
| 123 |
public function create_missing_tables() { |
| 124 |
|
| 125 |
/* Create the network snippets table if it doesn't exist */ |
| 126 |
if ( is_multisite() && ! self::table_exists( $this->ms_table ) ) { |
| 127 |
$this->create_table( $this->ms_table ); |
| 128 |
} |
| 129 |
|
| 130 |
/* Create the table if it doesn't exist */ |
| 131 |
if ( ! self::table_exists( $this->table ) ) { |
| 132 |
$this->create_table( $this->table ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Create the snippet tables, or upgrade them if they already exist |
| 138 |
*/ |
| 139 |
public function create_or_upgrade_tables() { |
| 140 |
if ( is_multisite() ) { |
| 141 |
$this->create_table( $this->ms_table ); |
| 142 |
} |
| 143 |
|
| 144 |
$this->create_table( $this->table ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Create a snippet table if it does not already exist |
| 149 |
* |
| 150 |
* @param string $table_name Name of database table. |
| 151 |
*/ |
| 152 |
public static function create_missing_table( $table_name ) { |
| 153 |
|
| 154 |
if ( self::table_exists( $table_name ) ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
self::create_table( $table_name ); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Create a single snippet table. |
| 163 |
* |
| 164 |
* @param string $table_name The name of the table to create. |
| 165 |
* |
| 166 |
* @return bool Whether the table creation was successful. |
| 167 |
* @since 1.6 |
| 168 |
* @uses dbDelta() to apply the SQL code |
| 169 |
*/ |
| 170 |
public static function create_table( $table_name ) { |
| 171 |
global $wpdb; |
| 172 |
$charset_collate = $wpdb->get_charset_collate(); |
| 173 |
|
| 174 |
/* Create the database table */ |
| 175 |
$sql = "CREATE TABLE $table_name ( |
| 176 |
id BIGINT(20) NOT NULL AUTO_INCREMENT, |
| 177 |
name TINYTEXT NOT NULL DEFAULT '', |
| 178 |
description TEXT NOT NULL DEFAULT '', |
| 179 |
code LONGTEXT NOT NULL DEFAULT '', |
| 180 |
tags LONGTEXT NOT NULL DEFAULT '', |
| 181 |
scope VARCHAR(15) NOT NULL DEFAULT 'global', |
| 182 |
priority SMALLINT NOT NULL DEFAULT 10, |
| 183 |
active TINYINT(1) NOT NULL DEFAULT 0, |
| 184 |
modified DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 185 |
PRIMARY KEY (id), |
| 186 |
KEY scope (scope), |
| 187 |
KEY active (active) |
| 188 |
) $charset_collate;"; |
| 189 |
|
| 190 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 191 |
dbDelta( $sql ); |
| 192 |
|
| 193 |
$success = empty( $wpdb->last_error ); |
| 194 |
|
| 195 |
if ( $success ) { |
| 196 |
do_action( 'code_snippets/create_table', $table_name ); |
| 197 |
} |
| 198 |
|
| 199 |
return $success; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Build a list of formatting placeholders for an array of data. |
| 204 |
* |
| 205 |
* @param int $count Length of data. |
| 206 |
* @param string $placeholder Placeholder to use. Defaults to string placeholder. |
| 207 |
* |
| 208 |
* @return string List of placeholders, ready for inclusion in query. |
| 209 |
*/ |
| 210 |
private static function build_format_list( $count, $placeholder = '%s' ) { |
| 211 |
return implode( ',', array_fill( 0, $count, $placeholder ) ); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Generate the SQL for fetching active snippets from the database |
| 216 |
* |
| 217 |
* @param array|string $scopes List of scopes to retrieve in. |
| 218 |
* @param string $select_list List of table columns in SQL format. |
| 219 |
* |
| 220 |
* @return array List of SQL queries |
| 221 |
*/ |
| 222 |
public function fetch_active_snippets( $scopes, $select_list = 'id, code, scope' ) { |
| 223 |
global $wpdb; |
| 224 |
$db = code_snippets()->db; |
| 225 |
|
| 226 |
$queries = array(); |
| 227 |
|
| 228 |
if ( ! is_array( $scopes ) ) { |
| 229 |
$scopes = array( $scopes ); |
| 230 |
} |
| 231 |
|
| 232 |
$scopes_format = self::build_format_list( count( $scopes ) ); |
| 233 |
$select = "SELECT $select_list FROM"; |
| 234 |
$where = "WHERE scope IN ($scopes_format)"; |
| 235 |
$order = 'ORDER BY priority ASC, id ASC'; |
| 236 |
|
| 237 |
/* Fetch snippets from site table */ |
| 238 |
if ( self::table_exists( $db->table ) ) { |
| 239 |
$queries[ $db->table ] = $wpdb->prepare( "$select $db->table $where AND active=1 $order", $scopes ); |
| 240 |
} |
| 241 |
|
| 242 |
/* Fetch snippets from the network table */ |
| 243 |
if ( is_multisite() && self::table_exists( $db->ms_table ) ) { |
| 244 |
$active_shared_ids = get_option( 'active_shared_network_snippets', array() ); |
| 245 |
|
| 246 |
/* If there are active shared snippets, include them in the query */ |
| 247 |
if ( is_array( $active_shared_ids ) && count( $active_shared_ids ) ) { |
| 248 |
$ids_format = self::build_format_list( count( $active_shared_ids ), '%d' ); |
| 249 |
$sql = "$select $db->ms_table $where AND (active=1 OR id IN ($ids_format)) $order"; |
| 250 |
|
| 251 |
$queries[ $db->ms_table ] = $wpdb->prepare( $sql, array_merge( $scopes, $active_shared_ids ) ); |
| 252 |
|
| 253 |
} else { |
| 254 |
$queries[ $db->ms_table ] = $wpdb->prepare( "$select $db->ms_table $where AND active=1 $order", $scopes ); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
$active_snippets = array(); |
| 259 |
|
| 260 |
foreach ( $queries as $table => $query ) { |
| 261 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching |
| 262 |
$results = $wpdb->get_results( $query, 'ARRAY_A' ); |
| 263 |
$active_snippets[ $table ] = is_array( $results ) ? $results : array(); |
| 264 |
} |
| 265 |
|
| 266 |
return $active_snippets; |
| 267 |
} |
| 268 |
} |
| 269 |
|