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

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