PluginProbe
Code Snippets / 2.14.0
Code Snippets v2.14.0
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / class-db.php

class-db.php in Code Snippets 2.14.0, at php/class-db.php

189 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
11 * Unprefixed site-wide table name
12 */
13 const TABLE_NAME = 'snippets';
14
15 /**
16 * Unprefixed network-wide table name
17 */
18 const MS_TABLE_NAME = 'ms_snippets';
19
20 /**
21 * Side-wide table name
22 *
23 * @var string
24 */
25 public $table;
26
27 /**
28 * Network-wide table name
29 *
30 * @var string
31 */
32 public $ms_table;
33
34 /**
35 * Class constructor
36 */
37 public function __construct() {
38 $this->set_table_vars();
39 }
40
41 /**
42 * Register the snippet table names with WordPress
43 *
44 * @since 2.0
45 * @uses $wpdb
46 */
47 function set_table_vars() {
48 global $wpdb;
49
50 $this->table = $wpdb->prefix . self::TABLE_NAME;
51 $this->ms_table = $wpdb->base_prefix . self::MS_TABLE_NAME;
52
53 /* Register the snippet table names with WordPress */
54 $wpdb->snippets = $this->table;
55 $wpdb->ms_snippets = $this->ms_table;
56
57 $wpdb->tables[] = self::TABLE_NAME;
58 $wpdb->ms_global_tables[] = self::MS_TABLE_NAME;
59 }
60
61 /**
62 * Validate the multisite parameter of the get_table_name() function
63 *
64 * @param bool|null $network
65 *
66 * @return bool
67 */
68 function validate_network_param( $network ) {
69
70 /* If multisite is not active, then the parameter should always be false */
71 if ( ! is_multisite() ) {
72 return false;
73 }
74
75 /* If $multisite is null, try to base it on the current admin page */
76 if ( is_null( $network ) && function_exists( 'is_network_admin' ) ) {
77 $network = is_network_admin();
78 }
79
80 return $network;
81 }
82
83 /**
84 * Return the appropriate snippet table name
85 *
86 * @param string|bool|null $multisite Retrieve the multisite table name or the site table name?
87 *
88 * @return string The snippet table name
89 * @since 2.0
90 *
91 */
92 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 /* Retrieve the table name from $wpdb depending on the value of $multisite */
103
104 return ( $multisite ? $this->ms_table : $this->table );
105 }
106
107 /**
108 * Create the snippet tables if they do not already exist
109 */
110 public function create_missing_tables() {
111 global $wpdb;
112
113 /* Create the network snippets table if it doesn't exist */
114 if ( is_multisite() && $wpdb->get_var( "SHOW TABLES LIKE '$this->ms_table'" ) !== $this->ms_table ) {
115 $this->create_table( $this->ms_table );
116 }
117
118 /* Create the table if it doesn't exist */
119 if ( $wpdb->get_var( "SHOW TABLES LIKE '$this->table'" ) !== $this->table ) {
120 $this->create_table( $this->table );
121 }
122 }
123
124 /**
125 * Create the snippet tables, or upgrade them if they already exist
126 */
127 public function create_or_upgrade_tables() {
128 if ( is_multisite() ) {
129 $this->create_table( $this->ms_table );
130 }
131
132 $this->create_table( $this->table );
133 }
134
135 /**
136 * Create a snippet table if it does not already exist
137 *
138 * @param $table_name
139 */
140 public static function create_missing_table( $table_name ) {
141 global $wpdb;
142
143 if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) === $table_name ) {
144 return;
145 }
146
147 self::create_table( $table_name );
148 }
149
150 /**
151 * Create a single snippet table
152 *
153 * @param string $table_name The name of the table to create
154 *
155 * @return bool whether the table creation was successful
156 * @since 1.6
157 * @uses dbDelta() to apply the SQL code
158 */
159 public static function create_table( $table_name ) {
160 global $wpdb;
161 $charset_collate = $wpdb->get_charset_collate();
162
163 /* Create the database table */
164 $sql = "CREATE TABLE $table_name (
165 id BIGINT(20) NOT NULL AUTO_INCREMENT,
166 name TINYTEXT NOT NULL DEFAULT '',
167 description TEXT NOT NULL DEFAULT '',
168 code LONGTEXT NOT NULL DEFAULT '',
169 tags LONGTEXT NOT NULL DEFAULT '',
170 scope VARCHAR(15) NOT NULL DEFAULT 'global',
171 priority SMALLINT NOT NULL DEFAULT 10,
172 active TINYINT(1) NOT NULL DEFAULT 0,
173 modified DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
174 PRIMARY KEY (id)
175 ) $charset_collate;";
176
177 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
178 dbDelta( $sql );
179
180 $success = empty( $wpdb->last_error );
181
182 if ( $success ) {
183 do_action( 'code_snippets/create_table', $table_name );
184 }
185
186 return $success;
187 }
188 }
189