PluginProbe
MaxButtons – Create buttons / 7.13.1
MaxButtons – Create buttons v7.13.1
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 7.13.1, at classes/installation.php

418 lines 14.2 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 public 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 public 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 public static function hasAddOn($addon)
26 {
27 switch($addon)
28 {
29 case 'socialshare':
30 if (defined('MBSOCIAL_ROOT_FILE'))
31 {
32 return true;
33 }
34 break;
35 }
36
37 return false;
38 }
39
40 // This should be done - once! Removed in time as well.
41 public static function check_database()
42 {
43 $checked = get_option("MB_DBASECHECK", true);
44 if ($checked !== false) // removing this option.
45 delete_option('MB_DBASECHECK');
46
47 $version = MAXBUTTONS_VERSION_NUM;
48 $installed_version = MB()->get_installed_version();
49 if ($version !== $installed_version)
50 {
51 $table = maxUtils::get_table_name();
52
53 self::activate_plugin(); // always run the DBdelta on version mismatch.
54 self::clear();
55 }
56 }
57
58 public static function activate_plugin($gocreate = true)
59 {
60 static::create_database_table();
61 static::migrate();
62 static::upgradeUTF();
63 static::updateFA();
64
65 $button = MB()->getClass('button');
66 $button->reset_cache(); //refresh cache
67
68 update_option(MAXBUTTONS_VERSION_KEY, MAXBUTTONS_VERSION_NUM);
69 $created = get_option("MBFREE_CREATED");
70 if ($created == '' && $gocreate)
71 { update_option("MBFREE_CREATED", time());
72 update_option("MBFREE_HOMEURL", home_url());
73 }
74
75 }
76
77 /** Function to clear out obsolete and old options, items etc. */
78 public static function clear()
79 {
80 delete_option('MB_DBASECHECK');
81 }
82
83 /** MOVE Button Database records from FA4 to FA5 */
84 public static function updateFA()
85 {
86 global $wpdb;
87
88 $table_name = maxUtils::get_table_name();
89 $sql = ' SHOW COLUMNS FROM ' . $table_name . ' LIKE "icon"';
90 $result = $wpdb->get_results($sql, ARRAY_A);
91
92 // when the icon field does not exist. Otherwise this causes an activate notice.
93 if (count($result) == 0)
94 return;
95
96
97 $conversion_path = MB()->get_plugin_path() . '/assets/libraries/font-awesome-5/shims.json';
98 $conversion_array = json_decode(file_get_contents($conversion_path), ARRAY_A);
99
100
101 $sql = 'select id, icon from ' . maxUtils::get_table_name();
102 $result = $wpdb->get_results($sql, ARRAY_A);
103 if (count($result) == 0)
104 return;
105
106 foreach($result as $item)
107 {
108 $button_id = $item['id'];
109 if (! $button_id > 0)
110 { continue; }
111 // procedure from setupdata by button class
112 $icondata = maybe_unserialize($item['icon']);
113 if (! is_array($icondata) )
114 {
115 $icondata = json_decode($icondata, true);
116 if (isset($icondata['fa_icon_value']) && strlen($icondata['fa_icon_value']) > 0)
117 {
118 $fa_icon = $icondata['fa_icon_value'];
119 $fa_icon = str_replace('fa-', '', $fa_icon); // remove the prefix
120
121 // don't convert converted icons.
122 if (strpos($fa_icon, 'fas') === false && strpos($fa_icon, 'fab') === false && strpos($fa_icon,'far') === false)
123 {
124 $new_fa_icon = self::searchNewFA($fa_icon, $conversion_array);
125 $icondata['fa_icon_value'] = $new_fa_icon;
126 $wpdb->update(maxUtils::get_table_name(), array('icon' => json_encode($icondata)), array('id' => $button_id) );
127
128 }
129
130 }
131 }
132
133 }
134
135 }
136
137 /** Convert Font Awesome 4 to Font Awesome 5 via the conversion shims array
138 * @param $old The old value, without the fa- prefix
139 * @param $conversion_array The loaded FA5 shims library
140 */
141 public static function searchNewFA($old, $conversion_array)
142 {
143 $new = false;
144 foreach($conversion_array as $key => $items)
145 {
146 if ($items[0] == $old)
147 {
148 $new = (strlen($items[1]) > 0) ? $items[1] : 'fas'; // if not set, fas is the set
149 $new .= ' fa-';
150 $new .= (strlen($items[2]) > 0) ? $items[2] : $old ;
151 }
152
153 }
154
155 if (! $new)
156 {
157 $new = 'fas fa-'. $old;
158 }
159
160 return $new;
161 }
162
163
164 /** Move data from old version database to new version
165 *
166 * 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.
167 */
168 static function migrate()
169 {
170 global $wpdb;
171
172 $old_table = maxUtils::get_table_name(true);
173 $table = maxUtils::get_table_name();
174
175 if (! self::maxbuttons_database_table_exists($old_table))
176 {
177 return;
178 }
179
180 $sql = "SELECT id from $table";
181 $result = $wpdb->get_results($sql, ARRAY_A);
182 if(count($result) > 0) return; // don't do this if table already has data.
183
184 $sql = "SELECT * FROM $old_table";
185 $rows = $wpdb->get_results($sql, ARRAY_A);
186
187 if (count($rows) == 0 ) // no button in all table; all is good.
188 return true;
189
190
191 foreach($rows as $row)
192 {
193 $data = static::convertOldFields($row);
194 $id = $data["id"];
195 global $wpdb;
196 $wpdb->insert($table, array("id" => $id));
197
198 //$data = apply_filters("mb-migration-data",$data, $row);
199 $button = MB()->getClass('button');
200 $button->set($id);
201 $button->save($data);
202 }
203 }
204
205 /** Import fields from the old database format ( pre version 3.0 ) */
206 static function convertOldFields($row)
207 {
208 $data = array();
209
210 $data["id"] = (isset($row["id"])) ? $row["id"] : -1;
211 $data["name"] = $row["name"];
212 $data["status"] = isset($row["status"]) ? $row["status"] : 'publish'; // happens with downloadable packs.
213 $data["description"] = $row["description"];
214 $data["url"] = $row["url"];
215 $data["text"] = $row["text"];
216 $data["new_window"] = (isset($row["new_window"]) && $row["new_window"] != "") ? 1 : 0;
217 $data["nofollow"] = (isset($row["nofollow"]) && $row["nofollow"] != "") ? 1 : 0;
218
219 $data["font"] = $row["text_font_family"];
220 $data["font_size"] = $row["text_font_size"];
221 $data["font_style"] = $row["text_font_style"];
222 $data["font_weight"] = $row["text_font_weight"];
223 $data["text_shadow_offset_left"] = $row["text_shadow_offset_left"];
224 $data["text_shadow_offset_top"] = $row["text_shadow_offset_top"];
225 $data["text_shadow_width"] = $row["text_shadow_width"];
226 $data["padding_top"] = $row["text_padding_top"];
227 $data["padding_right"] = $row["text_padding_right"];
228 $data["padding_bottom"] = $row["text_padding_bottom"];
229 $data["padding_left"] = $row["text_padding_left"];
230
231 $data["radius_top_left"] = $row["border_radius_top_left"];
232 $data["radius_top_right"] = $row["border_radius_top_right"];
233 $data["radius_bottom_left"] = $row["border_radius_bottom_left"];
234 $data["radius_bottom_right"] = $row["border_radius_bottom_right"];
235 $data["border_style"] = $row["border_style"];
236 $data["border_width"] = $row["border_width"];
237 $data["box_shadow_offset_left"] = $row["box_shadow_offset_left"];
238 $data["box_shadow_offset_top"] = $row["box_shadow_offset_top"];
239 $data["box_shadow_width"] = $row["box_shadow_width"];
240
241 $data["text_color"] = $row["text_color"];
242 $data["text_shadow_color"] = $row["text_shadow_color"];
243 $data["gradient_start_color"] = $row["gradient_start_color"];
244 $data["gradient_end_color"] = $row["gradient_end_color"];
245 $data["border_color"] = $row["border_color"];
246 $data["box_shadow_color"] = $row["box_shadow_color"];
247
248 $data["text_color_hover"] = $row["text_color_hover"];
249 $data["text_shadow_color_hover"] = $row["text_shadow_color_hover"];
250 $data["gradient_start_color_hover"] = $row["gradient_start_color_hover"];
251 $data["gradient_end_color_hover"] = $row["gradient_end_color_hover"];
252 $data["border_color_hover"] = $row["border_color_hover"];
253 $data["box_shadow_color_hover"] = $row["box_shadow_color_hover"];
254
255 $data["gradient_stop"] = (isset($row["gradient_stop"])) ? $row["gradient_stop"] : 45;
256 $data["gradient_start_opacity"] = (isset($row["gradient_start_opacity"])) ? $row["gradient_start_opacity"] : 100;
257 $data["gradient_end_opacity"] = (isset($row["gradient_end_opacity"])) ? $row["gradient_end_opacity"] : 100;
258 $data["gradient_start_opacity_hover"] = (isset($row["gradient_start_opacity_hover"])) ? $row["gradient_start_opacity_hover"] : 100;
259 $data["gradient_end_opacity_hover"] = (isset($row["gradient_end_opacity_hover"])) ? $row["gradient_end_opacity_hover"] : 100;
260
261 $data["container_enabled"] = (isset($row["container_enabled"]) && $row["container_enabled"] != "") ? 1 : 0;
262 $data["container_center_div_wrap"] = (isset($row["container_center_div_wrap_enabled"]) && $row["container_center_div_wrap_enabled"] != "") ? 1 : 0;
263 $data["container_width"] = isset($row["container_width"]) ? $row["container_width"] : '';
264 $data["container_margin_top"] = isset($row["container_margin_top"]) ? $row["container_margin_top"] : '';
265 $data["container_margin_right"] = isset($row["container_margin_right"]) ? $row["container_margin_right"] : '';
266 $data["container_margin_bottom"] = isset($row["container_margin_bottom"]) ? $row["container_margin_right"] : '';
267 $data["container_margin_left"] = isset($row["container_margin_left"]) ? $row["container_margin_left"] : '';
268 $data["container_alignment"] = isset($row["container_alignment"]) ? $row["container_alignment"] : '';
269
270 $data["status"] = (isset($row["status"])) ? $row["status"] : 'publish';
271
272 $data["external_css"] = (isset($row["external_css"]) && $row["external_css"] != "") ? 1: 0;
273 $data["important_css"] = (isset($row["important_css"]) && $row["important_css"] != "") ? 1 : 0;
274
275 // icon
276 $data["use_fa_icon"] = (isset($row["use_font_awesome_icon"]) && $row["use_font_awesome_icon"] != '') ? 1 : 0;
277 $data["fa_icon_value"] = (isset($row["font_awesome_icon"])) ? $row["font_awesome_icon"] : '';
278 $data["fa_icon_size"] = (isset($row["font_awesome_icon_size"])) ? $row["font_awesome_icon_size"] : '';
279 $data["icon_url"] = (isset($row["icon_url"])) ? $row["icon_url"] : '';
280 $data["icon_alt"] = (isset($row["icon_alt"])) ? $row["icon_alt"] : '';
281 $data["icon_position"] = (isset($row["icon_position"])) ? $row["icon_position"] : '';
282 $data["icon_padding_top"] = (isset($row["icon_padding_top"])) ? $row["icon_padding_top"] : '';
283 $data["icon_padding_bottom"] = (isset($row["icon_padding_bottom"])) ? $row["icon_padding_bottom"] : '';
284 $data["icon_padding_left"] = (isset($row["icon_padding_left"])) ? $row["icon_padding_left"] : '';
285 $data["icon_padding_right"] = (isset($row["icon_padding_right"]))? $row["icon_padding_right"] : '';
286
287 // dimension
288 $data["button_width"] = (isset($row["width"])) ? $row["width"] : '';
289 $data["button_height"] = (isset($row["height"])) ? $row["height"] : '';
290
291 // colorPro
292 $data["icon_color"] = (isset($row["icon_color"])) ? $row["icon_color"] : '';
293 $data["icon_color_hover"] = (isset($row["icon_color_hover"])) ? $row["icon_color_hover"] : '';
294
295 // textPro
296 $data["text2"] = $row["text2"];
297 $data["font2"] = $row["text2_font_family"];
298 $data["font_size2"] = $row["text2_font_size"];
299 $data["font_style2"] = $row["text2_font_style"];
300 $data["font_weight2"] = $row["text2_font_weight"];
301 $data["padding_top2"] = $row["text2_padding_top"];
302 $data["padding_right2"] = $row["text2_padding_right"];
303 $data["padding_bottom2"] = $row["text2_padding_bottom"];
304 $data["padding_left2"] = $row["text2_padding_left"];
305
306 $data["text_align"] = (isset($row["text_align"])) ? $row["text_align"] : '';
307 $data["text_align2"] = (isset($row["text2_align"])) ? $row["text2_align"] : '';
308 // here old / new Pro Database fields.
309
310 return $data;
311 }
312
313 static function deactivate_plugin()
314 {
315 delete_option(MAXBUTTONS_VERSION_KEY);
316 }
317
318
319 static function maxbuttons_database_table_exists($table_name) {
320 global $wpdb;
321 return strtolower($wpdb->get_var("SHOW TABLES LIKE '$table_name'")) == strtolower($table_name);
322 }
323
324
325 static function create_database_table() {
326 //global $maxbuttons_installed_version;
327
328 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
329
330 $table_name = maxUtils::get_table_name();
331 $button = MB()->getClass('button');
332 $blocks = $button->getBlocks();
333
334 // IMPORTANT: There MUST be two spaces between the PRIMARY KEY keywords
335 // and the column name, and the column name MUST be in parenthesis.
336 $sql = "CREATE TABLE " . $table_name . " (
337 id int NOT NULL AUTO_INCREMENT,
338 name varchar(100) NULL,
339 status varchar(10) default 'publish' NOT NULL,
340 cache text,
341 ";
342
343
344 foreach($blocks as $block)
345 {
346 $block_name = $block->get_name();
347
348 $sql .= "" . $block_name . " TEXT NULL, \n ";
349 }
350
351 $sql .= "updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
352 created TIMESTAMP DEFAULT 0 NOT NULL,
353 PRIMARY KEY (id) )";
354
355 $result = dbDelta($sql); // always dbdelta
356
357 // Reset the cache if there were any left from before
358 $button->reset_cache();
359
360 // Collection table
361 $collection_table_name = maxUtils::get_collection_table_name();
362
363 $sql = "CREATE TABLE " . $collection_table_name . " (
364 meta_id int(11) NOT NULL AUTO_INCREMENT,
365 collection_id int(11) NOT NULL,
366 collection_key varchar(255),
367 collection_value text,
368 PRIMARY KEY (meta_id) )
369
370 ";
371
372 dbDelta($sql);
373
374 $collection_trans_table = maxUtils::get_coltrans_table_name();
375 $sql = "CREATE TABLE $collection_trans_table (
376 name varchar(1000),
377 value varchar(255),
378 expire int(11)
379 );
380 ";
381 $res = dbDelta($sql);
382 }
383
384 /** Attempt to upgrade UTF table to UTFmb4 - see this article https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/
385 */
386 public static function upgradeUTF()
387 {
388 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
389
390 if (! function_exists('maybe_convert_table_to_utf8mb4'))
391 return; // Versions before 4.2.0
392
393 $table_name = maxUtils::get_table_name();
394 $collection_table_name = maxUtils::get_collection_table_name();
395
396 maybe_convert_table_to_utf8mb4($table_name);
397 maybe_convert_table_to_utf8mb4($collection_table_name);
398 }
399
400 /** Routine for activation for WPMU - All blogs */
401 public static function call_function_for_each_site($function) {
402 global $wpdb;
403
404 // Hold this so we can switch back to it
405 $root_blog = $wpdb->blogid;
406
407 // Get all the blogs/sites in the network and invoke the function for each one
408 $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
409 foreach ($blog_ids as $blog_id) {
410 switch_to_blog($blog_id);
411 call_user_func($function);
412 }
413
414 // Now switch back to the root blog
415 switch_to_blog($root_blog);
416 }
417 } // class
418