| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Functions used to manage the database tables |
| 5 |
* |
| 6 |
* @package Code_Snippets |
| 7 |
*/ |
| 8 |
class Code_Snippets_DB { |
| 9 |
|
| 10 |
public $table; |
| 11 |
|
| 12 |
public $ms_table; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class constructor |
| 16 |
*/ |
| 17 |
function __construct() { |
| 18 |
$this->set_table_vars(); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Register the snippet table names with WordPress |
| 23 |
* |
| 24 |
* @since 2.0 |
| 25 |
* @uses $wpdb |
| 26 |
*/ |
| 27 |
function set_table_vars() { |
| 28 |
global $wpdb; |
| 29 |
|
| 30 |
$this->table = $wpdb->prefix . 'snippets'; |
| 31 |
$this->ms_table = $wpdb->base_prefix . 'ms_snippets'; |
| 32 |
|
| 33 |
/* Register the snippet table names with WordPress */ |
| 34 |
$wpdb->tables[] = $wpdb->snippets = $this->table; |
| 35 |
$wpdb->ms_global_tables[] = $wpdb->ms_snippets = $this->ms_table; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Validate the multisite parameter of the get_table_name() function |
| 40 |
* |
| 41 |
* @param bool|null $network |
| 42 |
* |
| 43 |
* @return bool |
| 44 |
*/ |
| 45 |
function validate_network_param( $network ) { |
| 46 |
|
| 47 |
/* If multisite is not active, then the parameter should always be false */ |
| 48 |
if ( ! is_multisite() ) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
/* If $multisite is null, try to base it on the current admin page */ |
| 53 |
if ( is_null( $network ) && function_exists( 'is_network_admin' ) ) { |
| 54 |
$network = is_network_admin(); |
| 55 |
} |
| 56 |
|
| 57 |
return $network; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Return the appropriate snippet table name |
| 62 |
* |
| 63 |
* @since 2.0 |
| 64 |
* |
| 65 |
* @param string|bool|null $multisite Retrieve the multisite table name or the site table name? |
| 66 |
* |
| 67 |
* @return string The snippet table name |
| 68 |
*/ |
| 69 |
function get_table_name( $multisite = null ) { |
| 70 |
|
| 71 |
/* If the first parameter is a string, assume it is a table name */ |
| 72 |
if ( is_string( $multisite ) ) { |
| 73 |
return $multisite; |
| 74 |
} |
| 75 |
|
| 76 |
/* Validate the multisite parameter */ |
| 77 |
$multisite = $this->validate_network_param( $multisite ); |
| 78 |
|
| 79 |
/* Retrieve the table name from $wpdb depending on the value of $multisite */ |
| 80 |
|
| 81 |
return ( $multisite ? $this->ms_table : $this->table ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Create the snippet tables if they do not already exist |
| 86 |
*/ |
| 87 |
public function create_missing_tables() { |
| 88 |
global $wpdb; |
| 89 |
|
| 90 |
/* Create the network snippets table if it doesn't exist */ |
| 91 |
if ( is_multisite() && $wpdb->get_var( "SHOW TABLES LIKE '$this->ms_table'" ) !== $this->ms_table ) { |
| 92 |
$this->create_table( $this->ms_table ); |
| 93 |
} |
| 94 |
|
| 95 |
/* Create the table if it doesn't exist */ |
| 96 |
if ( $wpdb->get_var( "SHOW TABLES LIKE '$this->table'" ) !== $this->table ) { |
| 97 |
$this->create_table( $this->table ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Create the snippet tables, or upgrade them if they already exist |
| 103 |
*/ |
| 104 |
public function create_or_upgrade_tables() { |
| 105 |
$this->create_table( $this->ms_table ); |
| 106 |
$this->create_table( $this->table ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Create a snippet table if it does not already exist |
| 111 |
* |
| 112 |
* @param $table_name |
| 113 |
*/ |
| 114 |
public static function create_missing_table( $table_name ) { |
| 115 |
global $wpdb; |
| 116 |
|
| 117 |
if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) === $table_name ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
self::create_table( $table_name ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Create a single snippet table |
| 126 |
* |
| 127 |
* @since 1.6 |
| 128 |
* @uses dbDelta() to apply the SQL code |
| 129 |
* |
| 130 |
* @param string $table_name The name of the table to create |
| 131 |
* |
| 132 |
* @return bool whether the table creation was successful |
| 133 |
*/ |
| 134 |
public static function create_table( $table_name ) { |
| 135 |
global $wpdb; |
| 136 |
$charset_collate = $wpdb->get_charset_collate(); |
| 137 |
|
| 138 |
/* Create the database table */ |
| 139 |
$sql = "CREATE TABLE $table_name ( |
| 140 |
id BIGINT(20) NOT NULL AUTO_INCREMENT, |
| 141 |
name TINYTEXT NOT NULL DEFAULT '', |
| 142 |
description TEXT NOT NULL DEFAULT '', |
| 143 |
code LONGTEXT NOT NULL DEFAULT '', |
| 144 |
tags LONGTEXT NOT NULL DEFAULT '', |
| 145 |
scope VARCHAR(15) NOT NULL DEFAULT 'global', |
| 146 |
priority SMALLINT NOT NULL DEFAULT 10, |
| 147 |
active TINYINT(1) NOT NULL DEFAULT 0, |
| 148 |
PRIMARY KEY (id) |
| 149 |
) $charset_collate;"; |
| 150 |
|
| 151 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 152 |
dbDelta( $sql ); |
| 153 |
|
| 154 |
$success = empty( $wpdb->last_error ); |
| 155 |
|
| 156 |
if ( $success ) { |
| 157 |
do_action( 'code_snippets/create_table', $table_name ); |
| 158 |
} |
| 159 |
|
| 160 |
return $success; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Occasionally the dbDelta process seems to fail to create new columns on some sites, |
| 165 |
* so attempt to create columns which appear to be missing |
| 166 |
* |
| 167 |
* @param string $table_name |
| 168 |
*/ |
| 169 |
public function create_missing_columns( $table_name ) { |
| 170 |
global $wpdb; |
| 171 |
|
| 172 |
$columns = array( |
| 173 |
'priority' => 'SMALLINT NOT NULL DEFAULT 10', |
| 174 |
); |
| 175 |
|
| 176 |
foreach ( $columns as $column => $creation_sql ) { |
| 177 |
if ( $wpdb->get_var( "SHOW COLUMNS FROM {$table_name} LIKE '{$column}';" ) !== $column ) { |
| 178 |
$wpdb->query( "ALTER TABLE {$table_name} ADD {$column} {$creation_sql};" ); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|