PluginProbe
Code Snippets / 2.10.0
Code Snippets v2.10.0
4.0.0-beta.2 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 All 65 releases
code-snippets / php / class-db.php

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

155 lines 3.8 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 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 = 'snippets';
31 $this->ms_table = 'ms_snippets';
32
33 /* Register the snippet table names with WordPress */
34 $wpdb->tables[] = $this->table;
35 $wpdb->ms_global_tables[] = $this->ms_table;
36
37 /* Setup initial table variables */
38 $wpdb->snippets = $this->table = $wpdb->prefix . $this->table;
39 $wpdb->ms_snippets = $this->ms_table = $wpdb->base_prefix . $this->ms_table;
40 }
41
42 /**
43 * Validate the multisite parameter of the get_table_name() function
44 *
45 * @param bool|null $network
46 *
47 * @return bool
48 */
49 function validate_network_param( $network ) {
50
51 /* If multisite is not active, then the parameter should always be false */
52 if ( ! is_multisite() ) {
53 return false;
54 }
55
56 /* If $multisite is null, try to base it on the current admin page */
57 if ( is_null( $network ) && function_exists( 'is_network_admin' ) ) {
58 $network = is_network_admin();
59 }
60
61 return $network;
62 }
63
64 /**
65 * Return the appropriate snippet table name
66 *
67 * @since 2.0
68 *
69 * @param string|bool|null $multisite Retrieve the multisite table name or the site table name?
70 *
71 * @return string The snippet table name
72 */
73 function get_table_name( $multisite = null ) {
74 global $wpdb;
75
76 /* If the first parameter is a string, assume it is a table name */
77 if ( is_string( $multisite ) ) {
78 return $multisite;
79 }
80
81 /* Validate the multisite parameter */
82 $multisite = $this->validate_network_param( $multisite );
83
84 /* Retrieve the table name from $wpdb depending on the value of $multisite */
85 return ( $multisite ? $wpdb->ms_snippets : $wpdb->snippets );
86 }
87
88 /**
89 * Create the snippet tables
90 * This function will only execute once per page load, except if $redo is true
91 *
92 * @since 1.7.1
93 *
94 * @param bool $upgrade Run the table creation code even if the table exists
95 */
96 function create_tables( $upgrade = false ) {
97 global $wpdb;
98
99 /* Set the table name variables if not yet defined */
100 if ( ! isset( $wpdb->snippets, $wpdb->ms_snippets ) ) {
101 $this->set_table_vars();
102 }
103
104 if ( is_multisite() ) {
105
106 /* Create the network snippets table if it doesn't exist, or upgrade it */
107 if ( $upgrade || $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->ms_snippets'" ) !== $wpdb->ms_snippets ) {
108 $this->create_table( $wpdb->ms_snippets );
109 }
110 }
111
112 /* Create the table if it doesn't exist, or upgrade it */
113 if ( $upgrade || $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->snippets'" ) !== $wpdb->snippets ) {
114 $this->create_table( $wpdb->snippets );
115 }
116 }
117
118 /**
119 * Create a single snippet table
120 *
121 * @since 1.6
122 * @uses dbDelta() to apply the SQL code
123 *
124 * @param string $table_name The name of the table to create
125 * @return bool whether the table creation was successful
126 */
127 function create_table( $table_name ) {
128 global $wpdb;
129 $charset_collate = $wpdb->get_charset_collate();
130
131 /* Create the database table */
132 $sql = "CREATE TABLE $table_name (
133 id bigint(20) NOT NULL AUTO_INCREMENT,
134 name tinytext NOT NULL default '',
135 description text NOT NULL default '',
136 code longtext NOT NULL default '',
137 tags longtext NOT NULL default '',
138 scope varchar(15) NOT NULL default 'global',
139 active tinyint(1) NOT NULL default 0,
140 PRIMARY KEY (id)
141 ) $charset_collate;";
142
143 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
144 dbDelta( $sql );
145
146 $success = empty( $wpdb->last_error );
147
148 if ( $success ) {
149 do_action( 'code_snippets/create_table', $table_name );
150 }
151
152 return $success;
153 }
154 }
155