PluginProbe
Code Snippets / 2.12.0
Code Snippets v2.12.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.12.0, at php/class-db.php

209 lines 5.7 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
86 return ( $multisite ? $wpdb->ms_snippets : $wpdb->snippets );
87 }
88
89 /**
90 * Create the snippet tables
91 * This function will only execute once per page load, except if $redo is true
92 *
93 * @since 1.7.1
94 *
95 * @param bool $upgrade Run the table creation code even if the table exists
96 */
97 function create_tables( $upgrade = false ) {
98 global $wpdb;
99
100 /* Set the table name variables if not yet defined */
101 if ( ! isset( $wpdb->snippets, $wpdb->ms_snippets ) ) {
102 $this->set_table_vars();
103 }
104
105 if ( is_multisite() ) {
106
107 /* Create the network snippets table if it doesn't exist, or upgrade it */
108 if ( $upgrade || $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->ms_snippets'" ) !== $wpdb->ms_snippets ) {
109 $this->create_table( $wpdb->ms_snippets );
110 }
111 }
112
113 /* Create the table if it doesn't exist, or upgrade it */
114 if ( $upgrade || $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->snippets'" ) !== $wpdb->snippets ) {
115 $this->create_table( $wpdb->snippets );
116 }
117 }
118
119 /**
120 * Create a single snippet table
121 *
122 * @since 1.6
123 * @uses dbDelta() to apply the SQL code
124 *
125 * @param string $table_name The name of the table to create
126 *
127 * @return bool whether the table creation was successful
128 */
129 function create_table( $table_name ) {
130 global $wpdb;
131 $charset_collate = $wpdb->get_charset_collate();
132
133 /* Create the database table */
134 $sql = "CREATE TABLE $table_name (
135 id bigint(20) NOT NULL AUTO_INCREMENT,
136 name tinytext NOT NULL default '',
137 description text NOT NULL default '',
138 code longtext NOT NULL default '',
139 tags longtext NOT NULL default '',
140 scope varchar(15) NOT NULL default 'global',
141 priority smallint NOT NULL default 10,
142 active tinyint(1) NOT NULL default 0,
143 PRIMARY KEY (id)
144 ) $charset_collate;";
145
146 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
147 dbDelta( $sql );
148
149 $success = empty( $wpdb->last_error );
150
151 if ( $success ) {
152 do_action( 'code_snippets/create_table', $table_name );
153 }
154
155 return $success;
156 }
157
158 /**
159 * Add sample snippet content to the database
160 *
161 * @param bool $network
162 */
163 public function create_sample_content( $network = false ) {
164
165 $snippets = array(
166
167 array(
168 'name' => __( 'Example HTML shortcode', 'code-snippets' ),
169 'code' => sprintf(
170 "\nadd_shortcode( 'shortcode_name', function () { ?>\n\n\t<p>%s</p>\n\n<?php } );",
171 strip_tags( __( 'write your HTML shortcode content here', 'code-snippets' ) )
172 ),
173 'desc' => __( 'This is an example snippet for demonstrating how to add an HTML shortcode.', 'code-snippets' ),
174 'tags' => array( 'shortcode' ),
175 ),
176
177 array(
178 'name' => __( 'Example CSS snippet', 'code-snippets' ),
179 'code' => sprintf(
180 "\nadd_action( 'wp_head', function () { ?>\n\t<style>\n\n\t\t/* %s */\n\n\t</style>\n<?php } );\n",
181 strip_tags( __( 'write your CSS code here', 'code-snippets' ) )
182 ),
183 'desc' => __( 'This is an example snippet for demonstrating how to add custom CSS code to your website.', 'code-snippets' ),
184 'tags' => array( 'css' ),
185 'scope' => 'front-end',
186 ),
187
188 array(
189 'name' => __( 'Example JavaScript snippet', 'code-snippets' ),
190 'code' => sprintf(
191 "\nadd_action( 'wp_head', function () { ?>\n\t<script>\n\n\t\t/* %s */\n\n\t</script>\n<?php } );\n",
192 strip_tags( __( 'write your JavaScript code here', 'code-snippets' ) )
193 ),
194 'desc' => __( 'This is an example snippet for demonstrating how to add custom JavaScript code to your website.', 'code-snippets' ),
195 'tags' => array( 'javascript' ),
196 'scope' => 'front-end',
197 ),
198 );
199
200 foreach ( $snippets as $snippet ) {
201 $snippet = new Code_Snippet( $snippet );
202 $snippet->desc .= ' ' . __( 'You can remove it, or edit it to add your own content.', 'code-snippets' );
203 $snippet->network = $network;
204
205 save_snippet( $snippet );
206 }
207 }
208 }
209