PluginProbe
MaxButtons – Create buttons / 6.26
MaxButtons – Create buttons v6.26
6.23 6.24 6.25 6.26 6.26.1 6.27 6.28 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.1.1 7.1.2 7.1.3 7.10 7.11 7.13 7.13.1 7.13.2 7.13.3 All 100 releases
maxbuttons / classes / installation.php

installation.php in MaxButtons – Create buttons 6.26, at classes/installation.php

322 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace MaxButtons;
3 defined('ABSPATH') or die('No direct access permitted');
4
5 class maxInstall
6 {
7 static function activation_hook($network_wide) {
8 if ($network_wide) {
9 static::call_function_for_each_site( array('self','activate_plugin') );
10 }
11 else {
12 static::activate_plugin();
13 }
14 }
15 static function deactivation_hook($network_wide) {
16
17 if ($network_wide) {
18 static::call_function_for_each_site( array('self','deactivate_plugin') );
19 }
20 else {
21 static::deactivate_plugin();
22 }
23 }
24
25 // This should be done - once! Removed in time as well.
26 static function check_database()
27 {
28 $checked = get_option("MB_DBASECHECK", true);
29 if ($checked !== false) // removing this option.
30 delete_option('MB_DBASECHECK');
31
32 $version = MAXBUTTONS_VERSION_NUM;
33 $installed_version = MB()->get_installed_version();
34 if ($version !== $installed_version)
35 {
36 $table = maxUtils::get_table_name();
37
38 self::activate_plugin(); // always run the DBdelta on version mismatch.
39 self::clear();
40 }
41 }
42
43 static function activate_plugin($gocreate = true)
44 {
45 $button = new maxButton();
46 $button->reset_cache(); //refresh cache
47
48 static::create_database_table();
49 static::migrate();
50 static::upgradeUTF();
51 update_option(MAXBUTTONS_VERSION_KEY, MAXBUTTONS_VERSION_NUM);
52 $created = get_option("MBFREE_CREATED");
53 if ($created == '' && $gocreate)
54 { update_option("MBFREE_CREATED", time());
55 update_option("MBFREE_HOMEURL", home_url());
56 }
57
58 }
59
60 /** Function to clear out obsolete and old options, items etc. */
61 static function clear()
62 {
63 delete_option('MB_DBASECHECK');
64 }
65
66 /** Move data from old version database to new version
67
68 Check if new database table is empty ( aka new ) to prevent migrating the same data multiple times then copy all rows from old table to the new one.
69 */
70 static function migrate()
71 {
72 global $wpdb;
73
74 $old_table = maxUtils::get_table_name(true);
75 $table = maxUtils::get_table_name();
76
77 if (! self::maxbuttons_database_table_exists($old_table))
78 {
79 return;
80 }
81
82 $sql = "SELECT id from $table";
83 $result = $wpdb->get_results($sql, ARRAY_A);
84 if(count($result) > 0) return; // don't do this if table already has data.
85
86 $sql = "SELECT * FROM $old_table";
87 $rows = $wpdb->get_results($sql, ARRAY_A);
88
89 if (count($rows) == 0 ) // no button in all table; all is good.
90 return true;
91
92
93 foreach($rows as $row)
94 {
95 $data = static::convertOldFields($row);
96 $id = $data["id"];
97 global $wpdb;
98 $wpdb->insert($table, array("id" => $id));
99
100 //$data = apply_filters("mb-migration-data",$data, $row);
101 $button = new maxButton();
102 $button->set($id);
103 $button->save($data);
104 }
105 }
106
107 /** Import fields from the old database format ( pre version 3.0 ) */
108 static function convertOldFields($row)
109 {
110 $data = array();
111
112
113 $data["id"] = (isset($row["id"])) ? $row["id"] : -1;
114 $data["name"] = $row["name"];
115 $data["status"] = isset($row["status"]) ? $row["status"] : 'publish'; // happens with downloadable packs.
116 $data["description"] = $row["description"];
117 $data["url"] = $row["url"];
118 $data["text"] = $row["text"];
119 $data["new_window"] = (isset($row["new_window"]) && $row["new_window"] != "") ? 1 : 0;
120 $data["nofollow"] = (isset($row["nofollow"]) && $row["nofollow"] != "") ? 1 : 0;
121
122 $data["font"] = $row["text_font_family"];
123 $data["font_size"] = $row["text_font_size"];
124 $data["font_style"] = $row["text_font_style"];
125 $data["font_weight"] = $row["text_font_weight"];
126 $data["text_shadow_offset_left"] = $row["text_shadow_offset_left"];
127 $data["text_shadow_offset_top"] = $row["text_shadow_offset_top"];
128 $data["text_shadow_width"] = $row["text_shadow_width"];
129 $data["padding_top"] = $row["text_padding_top"];
130 $data["padding_right"] = $row["text_padding_right"];
131 $data["padding_bottom"] = $row["text_padding_bottom"];
132 $data["padding_left"] = $row["text_padding_left"];
133
134 $data["radius_top_left"] = $row["border_radius_top_left"];
135 $data["radius_top_right"] = $row["border_radius_top_right"];
136 $data["radius_bottom_left"] = $row["border_radius_bottom_left"];
137 $data["radius_bottom_right"] = $row["border_radius_bottom_right"];
138 $data["border_style"] = $row["border_style"];
139 $data["border_width"] = $row["border_width"];
140 $data["box_shadow_offset_left"] = $row["box_shadow_offset_left"];
141 $data["box_shadow_offset_top"] = $row["box_shadow_offset_top"];
142 $data["box_shadow_width"] = $row["box_shadow_width"];
143
144 $data["text_color"] = $row["text_color"];
145 $data["text_shadow_color"] = $row["text_shadow_color"];
146 $data["gradient_start_color"] = $row["gradient_start_color"];
147 $data["gradient_end_color"] = $row["gradient_end_color"];
148 $data["border_color"] = $row["border_color"];
149 $data["box_shadow_color"] = $row["box_shadow_color"];
150
151 $data["text_color_hover"] = $row["text_color_hover"];
152 $data["text_shadow_color_hover"] = $row["text_shadow_color_hover"];
153 $data["gradient_start_color_hover"] = $row["gradient_start_color_hover"];
154 $data["gradient_end_color_hover"] = $row["gradient_end_color_hover"];
155 $data["border_color_hover"] = $row["border_color_hover"];
156 $data["box_shadow_color_hover"] = $row["box_shadow_color_hover"];
157
158 $data["gradient_stop"] = (isset($row["gradient_stop"])) ? $row["gradient_stop"] : 45;
159 $data["gradient_start_opacity"] = (isset($row["gradient_start_opacity"])) ? $row["gradient_start_opacity"] : 100;
160 $data["gradient_end_opacity"] = (isset($row["gradient_end_opacity"])) ? $row["gradient_end_opacity"] : 100;
161 $data["gradient_start_opacity_hover"] = (isset($row["gradient_start_opacity_hover"])) ? $row["gradient_start_opacity_hover"] : 100;
162 $data["gradient_end_opacity_hover"] = (isset($row["gradient_end_opacity_hover"])) ? $row["gradient_end_opacity_hover"] : 100;
163
164 $data["container_enabled"] = (isset($row["container_enabled"]) && $row["container_enabled"] != "") ? 1 : 0;
165 $data["container_center_div_wrap"] = (isset($row["container_center_div_wrap_enabled"]) && $row["container_center_div_wrap_enabled"] != "") ? 1 : 0;
166 $data["container_width"] = isset($row["container_width"]) ? $row["container_width"] : '';
167 $data["container_margin_top"] = isset($row["container_margin_top"]) ? $row["container_margin_top"] : '';
168 $data["container_margin_right"] = isset($row["container_margin_right"]) ? $row["container_margin_right"] : '';
169 $data["container_margin_bottom"] = isset($row["container_margin_bottom"]) ? $row["container_margin_right"] : '';
170 $data["container_margin_left"] = isset($row["container_margin_left"]) ? $row["container_margin_left"] : '';
171 $data["container_alignment"] = isset($row["container_alignment"]) ? $row["container_alignment"] : '';
172
173 $data["status"] = (isset($row["status"])) ? $row["status"] : 'publish';
174
175 $data["external_css"] = (isset($row["external_css"]) && $row["external_css"] != "") ? 1: 0;
176 $data["important_css"] = (isset($row["important_css"]) && $row["important_css"] != "") ? 1 : 0;
177
178 // icon
179 $data["use_fa_icon"] = (isset($row["use_font_awesome_icon"]) && $row["use_font_awesome_icon"] != '') ? 1 : 0;
180 $data["fa_icon_value"] = (isset($row["font_awesome_icon"])) ? $row["font_awesome_icon"] : '';
181 $data["fa_icon_size"] = (isset($row["font_awesome_icon_size"])) ? $row["font_awesome_icon_size"] : '';
182 $data["icon_url"] = (isset($row["icon_url"])) ? $row["icon_url"] : '';
183 $data["icon_alt"] = (isset($row["icon_alt"])) ? $row["icon_alt"] : '';
184 $data["icon_position"] = (isset($row["icon_position"])) ? $row["icon_position"] : '';
185 $data["icon_padding_top"] = (isset($row["icon_padding_top"])) ? $row["icon_padding_top"] : '';
186 $data["icon_padding_bottom"] = (isset($row["icon_padding_bottom"])) ? $row["icon_padding_bottom"] : '';
187 $data["icon_padding_left"] = (isset($row["icon_padding_left"])) ? $row["icon_padding_left"] : '';
188 $data["icon_padding_right"] = (isset($row["icon_padding_right"]))? $row["icon_padding_right"] : '';
189
190 // dimension
191 $data["button_width"] = (isset($row["width"])) ? $row["width"] : '';
192 $data["button_height"] = (isset($row["height"])) ? $row["height"] : '';
193
194 // colorPro
195 $data["icon_color"] = (isset($row["icon_color"])) ? $row["icon_color"] : '';
196 $data["icon_color_hover"] = (isset($row["icon_color_hover"])) ? $row["icon_color_hover"] : '';
197
198 // textPro
199 $data["text2"] = $row["text2"];
200 $data["font2"] = $row["text2_font_family"];
201 $data["font_size2"] = $row["text2_font_size"];
202 $data["font_style2"] = $row["text2_font_style"];
203 $data["font_weight2"] = $row["text2_font_weight"];
204 $data["padding_top2"] = $row["text2_padding_top"];
205 $data["padding_right2"] = $row["text2_padding_right"];
206 $data["padding_bottom2"] = $row["text2_padding_bottom"];
207 $data["padding_left2"] = $row["text2_padding_left"];
208
209 $data["text_align"] = (isset($row["text_align"])) ? $row["text_align"] : '';
210 $data["text_align2"] = (isset($row["text2_align"])) ? $row["text2_align"] : '';
211 // here old / new Pro Database fields.
212
213 return $data;
214 }
215
216 static function deactivate_plugin()
217 {
218 delete_option(MAXBUTTONS_VERSION_KEY);
219 }
220
221
222 static function maxbuttons_database_table_exists($table_name) {
223 global $wpdb;
224 return strtolower($wpdb->get_var("SHOW TABLES LIKE '$table_name'")) == strtolower($table_name);
225 }
226
227
228 static function create_database_table() {
229 //global $maxbuttons_installed_version;
230
231 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
232
233 $table_name = maxUtils::get_table_name();
234 $button = new maxButton();
235 $blocks = $button->getBlocks();
236
237 // IMPORTANT: There MUST be two spaces between the PRIMARY KEY keywords
238 // and the column name, and the column name MUST be in parenthesis.
239 $sql = "CREATE TABLE " . $table_name . " (
240 id int NOT NULL AUTO_INCREMENT,
241 name varchar(100) NULL,
242 status varchar(10) default 'publish' NOT NULL,
243 cache text,
244 ";
245
246
247 foreach($blocks as $block)
248 {
249 $block_name = $block->get_name();
250
251 $sql .= "" . $block_name . " TEXT NULL, \n ";
252 }
253
254 $sql .= "updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
255 created TIMESTAMP DEFAULT 0 NOT NULL,
256 PRIMARY KEY (id) )";
257
258 $result = dbDelta($sql); // always dbdelta
259
260 // Reset the cache if there were any left from before
261 $button->reset_cache();
262
263 // Collection table
264 $collection_table_name = maxUtils::get_collection_table_name();
265
266 $sql = "CREATE TABLE " . $collection_table_name . " (
267 meta_id int(11) NOT NULL AUTO_INCREMENT,
268 collection_id int(11) NOT NULL,
269 collection_key varchar(255),
270 collection_value text,
271 PRIMARY KEY (meta_id) )
272
273 ";
274
275 dbDelta($sql);
276
277 $collection_trans_table = maxUtils::get_coltrans_table_name();
278 $sql = "CREATE TABLE $collection_trans_table (
279 name varchar(1000),
280 value varchar(255),
281 expire int(11)
282 );
283 ";
284 $res = dbDelta($sql);
285 }
286
287 /** Attempt to upgrade UTF table to UTFmb4 - see this article https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/
288 */
289 public static function upgradeUTF()
290 {
291 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
292
293 if (! function_exists('maybe_convert_table_to_utf8mb4'))
294 return; // Versions before 4.2.0
295
296 $table_name = maxUtils::get_table_name();
297 $collection_table_name = maxUtils::get_collection_table_name();
298
299 maybe_convert_table_to_utf8mb4($table_name);
300 maybe_convert_table_to_utf8mb4($collection_table_name);
301 }
302
303 /** Routine for activation for WPMU - All blogs */
304 public static function call_function_for_each_site($function) {
305 global $wpdb;
306
307 // Hold this so we can switch back to it
308 $root_blog = $wpdb->blogid;
309
310 // Get all the blogs/sites in the network and invoke the function for each one
311 $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
312 foreach ($blog_ids as $blog_id) {
313 switch_to_blog($blog_id);
314 call_user_func($function);
315 }
316
317 // Now switch back to the root blog
318 switch_to_blog($root_blog);
319 }
320 } // class
321
322