| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
namespace MaxButtons; |
| 4 |
defined('ABSPATH') or die('No direct access permitted'); |
| 5 |
|
| 6 |
class maxInstall |
| 7 |
{ |
| 8 |
public static function activation_hook($network_wide) { |
| 9 |
if ($network_wide) { |
| 10 |
static::call_function_for_each_site( array('self','activate_plugin') ); |
| 11 |
} |
| 12 |
else { |
| 13 |
static::activate_plugin(); |
| 14 |
} |
| 15 |
} |
| 16 |
public static function deactivation_hook($network_wide) { |
| 17 |
|
| 18 |
if ($network_wide) { |
| 19 |
static::call_function_for_each_site( array('self','deactivate_plugin') ); |
| 20 |
} |
| 21 |
else { |
| 22 |
static::deactivate_plugin(); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
public static function hasAddOn($addon) |
| 27 |
{ |
| 28 |
switch($addon) |
| 29 |
{ |
| 30 |
case 'socialshare': |
| 31 |
if (defined('MBSOCIAL_ROOT_FILE')) |
| 32 |
{ |
| 33 |
return true; |
| 34 |
} |
| 35 |
break; |
| 36 |
} |
| 37 |
|
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
// This should be done - once! Removed in time as well. |
| 42 |
public static function check_database() |
| 43 |
{ |
| 44 |
|
| 45 |
$version = MAXBUTTONS_VERSION_NUM; |
| 46 |
$installed_version = MB()->get_installed_version(); |
| 47 |
if ($version !== $installed_version) |
| 48 |
{ |
| 49 |
$table = maxUtils::get_table_name(); |
| 50 |
|
| 51 |
self::activate_plugin(); // always run the DBdelta on version mismatch. |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public static function activate_plugin($gocreate = true) |
| 56 |
{ |
| 57 |
static::create_database_table(); |
| 58 |
static::migrate(); |
| 59 |
static::upgradeUTF(); |
| 60 |
static::updateFA(); |
| 61 |
static::migrateResponsive(); |
| 62 |
|
| 63 |
$button = MB()->getClass('button'); |
| 64 |
$button->reset_cache(); //refresh cache |
| 65 |
|
| 66 |
update_option(MAXBUTTONS_VERSION_KEY, MAXBUTTONS_VERSION_NUM); |
| 67 |
$created = get_option("MBFREE_CREATED"); |
| 68 |
if ($created == '' && $gocreate) |
| 69 |
{ update_option("MBFREE_CREATED", time()); |
| 70 |
update_option("MBFREE_HOMEURL", home_url()); |
| 71 |
} |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
/** MOVE Button Database records from FA4 to FA5 */ |
| 76 |
public static function updateFA() |
| 77 |
{ |
| 78 |
global $wpdb; |
| 79 |
|
| 80 |
$table_name = maxUtils::get_table_name(); |
| 81 |
$sql = ' SHOW COLUMNS FROM ' . $table_name . ' LIKE "icon"'; |
| 82 |
$result = $wpdb->get_results($sql, ARRAY_A); |
| 83 |
|
| 84 |
// when the icon field does not exist. Otherwise this causes an activate notice. |
| 85 |
if (count($result) == 0) |
| 86 |
return; |
| 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), true); |
| 90 |
|
| 91 |
|
| 92 |
$sql = 'select id, icon from ' . maxUtils::get_table_name(); |
| 93 |
$result = $wpdb->get_results($sql, ARRAY_A); |
| 94 |
if (count($result) == 0) |
| 95 |
return; |
| 96 |
|
| 97 |
foreach($result as $item) |
| 98 |
{ |
| 99 |
$button_id = $item['id']; |
| 100 |
if (! $button_id > 0) |
| 101 |
{ continue; } |
| 102 |
// procedure from setupdata by button class |
| 103 |
$icondata = maybe_unserialize($item['icon']); |
| 104 |
if ( false === $icondata) // unserialize failed? |
| 105 |
{ |
| 106 |
return; |
| 107 |
} |
| 108 |
if (! is_array($icondata) && ! is_null($icondata) ) |
| 109 |
{ |
| 110 |
|
| 111 |
$icondata = json_decode($icondata, true); |
| 112 |
if (isset($icondata['fa_icon_value']) && strlen($icondata['fa_icon_value']) > 0) |
| 113 |
{ |
| 114 |
$fa_icon = $icondata['fa_icon_value']; |
| 115 |
$fa_icon = str_replace('fa-', '', $fa_icon); // remove the prefix |
| 116 |
|
| 117 |
// don't convert converted icons. |
| 118 |
if (strpos($fa_icon, 'fas') === false && strpos($fa_icon, 'fab') === false && strpos($fa_icon,'far') === false) |
| 119 |
{ |
| 120 |
$new_fa_icon = self::searchNewFA($fa_icon, $conversion_array); |
| 121 |
$icondata['fa_icon_value'] = $new_fa_icon; |
| 122 |
$wpdb->update(maxUtils::get_table_name(), array('icon' => json_encode($icondata)), array('id' => $button_id) ); |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
/** Convert Font Awesome 4 to Font Awesome 5 via the conversion shims array |
| 134 |
* @param $old The old value, without the fa- prefix |
| 135 |
* @param $conversion_array The loaded FA5 shims library |
| 136 |
*/ |
| 137 |
public static function searchNewFA($old, $conversion_array) |
| 138 |
{ |
| 139 |
$new = false; |
| 140 |
foreach($conversion_array as $key => $items) |
| 141 |
{ |
| 142 |
if ($items[0] == $old) |
| 143 |
{ |
| 144 |
$new = (strlen($items[1]) > 0) ? $items[1] : 'fas'; // if not set, fas is the set |
| 145 |
$new .= ' fa-'; |
| 146 |
$new .= (strlen($items[2]) > 0) ? $items[2] : $old ; |
| 147 |
} |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
if (! $new) |
| 152 |
{ |
| 153 |
$new = 'fas fa-'. $old; |
| 154 |
} |
| 155 |
|
| 156 |
return $new; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
/** Move data from old version database to new version |
| 161 |
* |
| 162 |
* 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. |
| 163 |
*/ |
| 164 |
public static function migrate() |
| 165 |
{ |
| 166 |
global $wpdb; |
| 167 |
|
| 168 |
$old_table = maxUtils::get_table_name(true); |
| 169 |
$table = maxUtils::get_table_name(); |
| 170 |
|
| 171 |
if (! self::maxbuttons_database_table_exists($old_table)) |
| 172 |
{ |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
$sql = "SELECT id from $table"; |
| 177 |
$result = $wpdb->get_results($sql, ARRAY_A); |
| 178 |
if(count($result) > 0) return; // don't do this if table already has data. |
| 179 |
|
| 180 |
$sql = "SELECT * FROM $old_table"; |
| 181 |
$rows = $wpdb->get_results($sql, ARRAY_A); |
| 182 |
|
| 183 |
if (count($rows) == 0 ) // no button in all table; all is good. |
| 184 |
return true; |
| 185 |
|
| 186 |
|
| 187 |
foreach($rows as $row) |
| 188 |
{ |
| 189 |
$data = static::convertOldFields($row); |
| 190 |
$id = $data["id"]; |
| 191 |
global $wpdb; |
| 192 |
$wpdb->insert($table, array("id" => $id)); |
| 193 |
|
| 194 |
//$data = apply_filters("mb-migration-data",$data, $row); |
| 195 |
$button = MB()->getClass('button'); |
| 196 |
$button->set($id); |
| 197 |
$button->save($data); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/** Import fields from the old database format ( pre version 3.0 ) */ |
| 202 |
public static function convertOldFields($row) |
| 203 |
{ |
| 204 |
$data = array(); |
| 205 |
|
| 206 |
$data["id"] = (isset($row["id"])) ? $row["id"] : -1; |
| 207 |
$data["name"] = $row["name"]; |
| 208 |
$data["status"] = isset($row["status"]) ? $row["status"] : 'publish'; // happens with downloadable packs. |
| 209 |
$data["description"] = $row["description"]; |
| 210 |
$data["url"] = $row["url"]; |
| 211 |
$data["text"] = $row["text"]; |
| 212 |
$data["new_window"] = (isset($row["new_window"]) && $row["new_window"] != "") ? 1 : 0; |
| 213 |
$data["nofollow"] = (isset($row["nofollow"]) && $row["nofollow"] != "") ? 1 : 0; |
| 214 |
|
| 215 |
$data["font"] = $row["text_font_family"]; |
| 216 |
$data["font_size"] = $row["text_font_size"]; |
| 217 |
$data["font_style"] = $row["text_font_style"]; |
| 218 |
$data["font_weight"] = $row["text_font_weight"]; |
| 219 |
$data["text_shadow_offset_left"] = $row["text_shadow_offset_left"]; |
| 220 |
$data["text_shadow_offset_top"] = $row["text_shadow_offset_top"]; |
| 221 |
$data["text_shadow_width"] = $row["text_shadow_width"]; |
| 222 |
$data["padding_top"] = $row["text_padding_top"]; |
| 223 |
$data["padding_right"] = $row["text_padding_right"]; |
| 224 |
$data["padding_bottom"] = $row["text_padding_bottom"]; |
| 225 |
$data["padding_left"] = $row["text_padding_left"]; |
| 226 |
|
| 227 |
$data["radius_top_left"] = $row["border_radius_top_left"]; |
| 228 |
$data["radius_top_right"] = $row["border_radius_top_right"]; |
| 229 |
$data["radius_bottom_left"] = $row["border_radius_bottom_left"]; |
| 230 |
$data["radius_bottom_right"] = $row["border_radius_bottom_right"]; |
| 231 |
$data["border_style"] = $row["border_style"]; |
| 232 |
$data["border_width"] = $row["border_width"]; |
| 233 |
$data["box_shadow_offset_left"] = $row["box_shadow_offset_left"]; |
| 234 |
$data["box_shadow_offset_top"] = $row["box_shadow_offset_top"]; |
| 235 |
$data["box_shadow_width"] = $row["box_shadow_width"]; |
| 236 |
|
| 237 |
$data["text_color"] = $row["text_color"]; |
| 238 |
$data["text_shadow_color"] = $row["text_shadow_color"]; |
| 239 |
$data["gradient_start_color"] = $row["gradient_start_color"]; |
| 240 |
$data["gradient_end_color"] = $row["gradient_end_color"]; |
| 241 |
$data["border_color"] = $row["border_color"]; |
| 242 |
$data["box_shadow_color"] = $row["box_shadow_color"]; |
| 243 |
|
| 244 |
$data["text_color_hover"] = $row["text_color_hover"]; |
| 245 |
$data["text_shadow_color_hover"] = $row["text_shadow_color_hover"]; |
| 246 |
$data["gradient_start_color_hover"] = $row["gradient_start_color_hover"]; |
| 247 |
$data["gradient_end_color_hover"] = $row["gradient_end_color_hover"]; |
| 248 |
$data["border_color_hover"] = $row["border_color_hover"]; |
| 249 |
$data["box_shadow_color_hover"] = $row["box_shadow_color_hover"]; |
| 250 |
|
| 251 |
$data["gradient_stop"] = (isset($row["gradient_stop"])) ? $row["gradient_stop"] : 45; |
| 252 |
$data["gradient_start_opacity"] = (isset($row["gradient_start_opacity"])) ? $row["gradient_start_opacity"] : 100; |
| 253 |
$data["gradient_end_opacity"] = (isset($row["gradient_end_opacity"])) ? $row["gradient_end_opacity"] : 100; |
| 254 |
$data["gradient_start_opacity_hover"] = (isset($row["gradient_start_opacity_hover"])) ? $row["gradient_start_opacity_hover"] : 100; |
| 255 |
$data["gradient_end_opacity_hover"] = (isset($row["gradient_end_opacity_hover"])) ? $row["gradient_end_opacity_hover"] : 100; |
| 256 |
|
| 257 |
$data["container_enabled"] = (isset($row["container_enabled"]) && $row["container_enabled"] != "") ? 1 : 0; |
| 258 |
$data["container_center_div_wrap"] = (isset($row["container_center_div_wrap_enabled"]) && $row["container_center_div_wrap_enabled"] != "") ? 1 : 0; |
| 259 |
$data["container_width"] = isset($row["container_width"]) ? $row["container_width"] : ''; |
| 260 |
$data["container_margin_top"] = isset($row["container_margin_top"]) ? $row["container_margin_top"] : ''; |
| 261 |
$data["container_margin_right"] = isset($row["container_margin_right"]) ? $row["container_margin_right"] : ''; |
| 262 |
$data["container_margin_bottom"] = isset($row["container_margin_bottom"]) ? $row["container_margin_right"] : ''; |
| 263 |
$data["container_margin_left"] = isset($row["container_margin_left"]) ? $row["container_margin_left"] : ''; |
| 264 |
$data["container_alignment"] = isset($row["container_alignment"]) ? $row["container_alignment"] : ''; |
| 265 |
|
| 266 |
$data["status"] = (isset($row["status"])) ? $row["status"] : 'publish'; |
| 267 |
|
| 268 |
$data["external_css"] = (isset($row["external_css"]) && $row["external_css"] != "") ? 1: 0; |
| 269 |
$data["important_css"] = (isset($row["important_css"]) && $row["important_css"] != "") ? 1 : 0; |
| 270 |
|
| 271 |
// icon |
| 272 |
$data["use_fa_icon"] = (isset($row["use_font_awesome_icon"]) && $row["use_font_awesome_icon"] != '') ? 1 : 0; |
| 273 |
$data["fa_icon_value"] = (isset($row["font_awesome_icon"])) ? $row["font_awesome_icon"] : ''; |
| 274 |
$data["fa_icon_size"] = (isset($row["font_awesome_icon_size"])) ? $row["font_awesome_icon_size"] : ''; |
| 275 |
$data["icon_url"] = (isset($row["icon_url"])) ? $row["icon_url"] : ''; |
| 276 |
$data["icon_alt"] = (isset($row["icon_alt"])) ? $row["icon_alt"] : ''; |
| 277 |
$data["icon_position"] = (isset($row["icon_position"])) ? $row["icon_position"] : ''; |
| 278 |
$data["icon_padding_top"] = (isset($row["icon_padding_top"])) ? $row["icon_padding_top"] : ''; |
| 279 |
$data["icon_padding_bottom"] = (isset($row["icon_padding_bottom"])) ? $row["icon_padding_bottom"] : ''; |
| 280 |
$data["icon_padding_left"] = (isset($row["icon_padding_left"])) ? $row["icon_padding_left"] : ''; |
| 281 |
$data["icon_padding_right"] = (isset($row["icon_padding_right"]))? $row["icon_padding_right"] : ''; |
| 282 |
|
| 283 |
// dimension |
| 284 |
$data["button_width"] = (isset($row["width"])) ? $row["width"] : ''; |
| 285 |
$data["button_height"] = (isset($row["height"])) ? $row["height"] : ''; |
| 286 |
|
| 287 |
// colorPro |
| 288 |
$data["icon_color"] = (isset($row["icon_color"])) ? $row["icon_color"] : ''; |
| 289 |
$data["icon_color_hover"] = (isset($row["icon_color_hover"])) ? $row["icon_color_hover"] : ''; |
| 290 |
|
| 291 |
// textPro |
| 292 |
$data["text2"] = $row["text2"]; |
| 293 |
$data["font2"] = $row["text2_font_family"]; |
| 294 |
$data["font_size2"] = $row["text2_font_size"]; |
| 295 |
$data["font_style2"] = $row["text2_font_style"]; |
| 296 |
$data["font_weight2"] = $row["text2_font_weight"]; |
| 297 |
$data["padding_top2"] = $row["text2_padding_top"]; |
| 298 |
$data["padding_right2"] = $row["text2_padding_right"]; |
| 299 |
$data["padding_bottom2"] = $row["text2_padding_bottom"]; |
| 300 |
$data["padding_left2"] = $row["text2_padding_left"]; |
| 301 |
|
| 302 |
$data["text_align"] = (isset($row["text_align"])) ? $row["text_align"] : ''; |
| 303 |
$data["text_align2"] = (isset($row["text2_align"])) ? $row["text2_align"] : ''; |
| 304 |
// here old / new Pro Database fields. |
| 305 |
|
| 306 |
return $data; |
| 307 |
} |
| 308 |
|
| 309 |
// Movements version < 8.0 to 8.0 > |
| 310 |
public static function migrateResponsive() |
| 311 |
{ |
| 312 |
global $wpdb; |
| 313 |
|
| 314 |
$table = maxUtils::get_table_name(); |
| 315 |
|
| 316 |
$sql = "SELECT id, responsive from $table"; |
| 317 |
$results = $wpdb->get_results($sql, ARRAY_A); |
| 318 |
|
| 319 |
$fieldMap = array( |
| 320 |
'button_width' => array('dimension', 'button_width'), // check to add px to this val or not. |
| 321 |
'button_width_unit' => array('dimension', 'button_size_unit_width'), // px must be converted to pixel, % prob. to percent! |
| 322 |
'button_height' => array('dimension', 'button_height'), |
| 323 |
'button_height_unit' => array('dimension', 'button_size_unit_height'), // see above |
| 324 |
'container_width' => array('container','container_width'), |
| 325 |
'container_float' => array('container','container_alignment' ), |
| 326 |
'container_width_unit' => array('container', 'container_width_unit'), |
| 327 |
'font_size' => array('text', 'font_size'), |
| 328 |
); |
| 329 |
|
| 330 |
// The name of the sizes and it's min-width, max-width. |
| 331 |
$sizeNames = array( |
| 332 |
'phone' => array(0, 480), |
| 333 |
'phone_land' => array(321, 480), |
| 334 |
'phone_portrait' => array(0, 320), |
| 335 |
'ipad' => array(768, 1024), |
| 336 |
'medium_phone' => array(480, 768), |
| 337 |
'ipad_land' => array(768, 1024), |
| 338 |
'ipad_portrait' => array(768, 1024), |
| 339 |
'desktop' => array(1224,0), |
| 340 |
'large_desktop' => array(1824,0), |
| 341 |
//custom |
| 342 |
); |
| 343 |
|
| 344 |
foreach ($results as $row) |
| 345 |
{ |
| 346 |
|
| 347 |
$id = $row['id']; |
| 348 |
$responsive_data = $row['responsive']; |
| 349 |
if (is_null($responsive_data)) |
| 350 |
{ |
| 351 |
continue; |
| 352 |
} |
| 353 |
|
| 354 |
$respdata = json_decode($row['responsive'], true); |
| 355 |
if (isset($respdata['auto_responsive'])) |
| 356 |
unset($respdata['auto_responsive']); |
| 357 |
|
| 358 |
// perhaps have some data? |
| 359 |
if (! isset($respdata['media_query'])) |
| 360 |
{ |
| 361 |
continue; // no responsive data. |
| 362 |
} |
| 363 |
elseif (count($respdata['media_query']) == 0) |
| 364 |
{ |
| 365 |
continue; // nope |
| 366 |
} |
| 367 |
else |
| 368 |
{ |
| 369 |
$mediaq = $respdata['media_query']; |
| 370 |
if (isset($mediaq['auto_responsive'])) |
| 371 |
unset($mediaq['auto_responsive']); |
| 372 |
|
| 373 |
$button = new maxButton(); |
| 374 |
$button->set($id); |
| 375 |
$button_data = $button->get(); |
| 376 |
$screens = $button->getResponsiveScreens(); |
| 377 |
if (isset($screens['new'])) |
| 378 |
unset($screens['new']); |
| 379 |
if (isset($screens['default'])) |
| 380 |
unset($screens['default']); |
| 381 |
|
| 382 |
// screens. loop the sizes. |
| 383 |
$responsive = array(); |
| 384 |
foreach($mediaq as $qsize => $qdata) |
| 385 |
{ |
| 386 |
|
| 387 |
$fielddata = $qdata; |
| 388 |
if (count($fielddata) == 1) // seems there is an extra dimension here. |
| 389 |
$fielddata = array_shift($qdata); |
| 390 |
|
| 391 |
$next_id = count($screens) + 1; |
| 392 |
$screen = new Screen('rs' . $next_id, array('name' => $qsize) ); |
| 393 |
|
| 394 |
$responsive['screens'][] = $screen->id; |
| 395 |
|
| 396 |
// find qsize, add screen, width, height. |
| 397 |
if (isset($sizeNames[$qsize])) |
| 398 |
{ |
| 399 |
list($minwidth, $maxwidth) = $sizeNames[$qsize]; |
| 400 |
} |
| 401 |
else // not set, must be custom, use custom_midiwdh |
| 402 |
{ |
| 403 |
$minwidth = isset($fielddata['mq_custom_minwidth']) ? $fielddata['mq_custom_minwidth'] : 0; |
| 404 |
$maxwidth = isset($fielddata['mq_custom_maxwidth']) ? $fielddata['mq_custom_maxwidth'] : 0; |
| 405 |
} |
| 406 |
|
| 407 |
$responsive[$screen->getFieldID('min_width')] = $minwidth; |
| 408 |
$responsive[$screen->getFieldID('max_width')] = $maxwidth; |
| 409 |
$responsive[$screen->getFieldID('screen_name')] = $qsize; |
| 410 |
|
| 411 |
if (isset($fielddata['mq_hide']) && $fielddata['mq_hide'] == 'none') |
| 412 |
{ |
| 413 |
$responsive[$screen->getFieldID('hide_screen')] = 1; |
| 414 |
} |
| 415 |
|
| 416 |
$button_data['responsive'] = $responsive; |
| 417 |
|
| 418 |
// convert fields. |
| 419 |
foreach($fielddata as $fname => $fvalue) |
| 420 |
{ |
| 421 |
if ($fvalue === '') |
| 422 |
continue; |
| 423 |
|
| 424 |
$fname = str_replace('mq_', '', $fname); |
| 425 |
if (isset($fieldMap[$fname])) |
| 426 |
{ |
| 427 |
|
| 428 |
list($block, $field) = $fieldMap[$fname]; |
| 429 |
|
| 430 |
if (strpos($fname, '_unit') >= 0) |
| 431 |
{ |
| 432 |
if ($fvalue == 'px') |
| 433 |
$fvalue = 'pixel'; |
| 434 |
elseif($fvalue == '%') |
| 435 |
$fvalue = 'percent'; |
| 436 |
} |
| 437 |
|
| 438 |
if ($fname == 'container_float') |
| 439 |
{ |
| 440 |
switch($fvalue) |
| 441 |
{ |
| 442 |
case 'left': |
| 443 |
$fvalue ='float: left'; |
| 444 |
break; |
| 445 |
case 'right': |
| 446 |
$fvalue ='float: right'; |
| 447 |
break; |
| 448 |
case 'none': |
| 449 |
$fvalue ='float: none'; |
| 450 |
break; |
| 451 |
} |
| 452 |
} |
| 453 |
// echo "$fname set, doing $block and $field for $fvalue <BR>"; |
| 454 |
$button_data[$block][$screen->getFieldID($field)] = $fvalue; |
| 455 |
} |
| 456 |
|
| 457 |
} |
| 458 |
|
| 459 |
$screens[] = $screen; |
| 460 |
} |
| 461 |
|
| 462 |
// doing responsive |
| 463 |
} |
| 464 |
|
| 465 |
// ok ok |
| 466 |
foreach($button_data as $block_name => $block_data) |
| 467 |
{ |
| 468 |
if (is_array($block_data)) // id, document_id returned are not data blocks |
| 469 |
$button->setData($block_name, $block_data); |
| 470 |
} |
| 471 |
|
| 472 |
$button_data['name'] = $button->getName(); |
| 473 |
$button_data['status'] = $button->getStatus(); |
| 474 |
$button_data['updated'] = $button->getUpdated(false); |
| 475 |
|
| 476 |
$button->update($button_data); |
| 477 |
|
| 478 |
} // button |
| 479 |
|
| 480 |
} |
| 481 |
|
| 482 |
public static function deactivate_plugin() |
| 483 |
{ |
| 484 |
delete_option(MAXBUTTONS_VERSION_KEY); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
public static function maxbuttons_database_table_exists($table_name) { |
| 489 |
global $wpdb; |
| 490 |
$dbaseTable = $wpdb->get_var("SHOW TABLES LIKE '$table_name'"); |
| 491 |
if (is_null($dbaseTable)) |
| 492 |
return false; |
| 493 |
else |
| 494 |
return strtolower($dbaseTable) == strtolower($table_name); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
public static function create_database_table() { |
| 499 |
|
| 500 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 501 |
|
| 502 |
$table_name = maxUtils::get_table_name(); |
| 503 |
$button = MB()->getClass('button'); |
| 504 |
$blocks = $button->getBlocks(); |
| 505 |
|
| 506 |
// IMPORTANT: There MUST be two spaces between the PRIMARY KEY keywords |
| 507 |
// and the column name, and the column name MUST be in parenthesis. |
| 508 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 509 |
id int(14) NOT NULL AUTO_INCREMENT, |
| 510 |
name varchar(100) NULL, |
| 511 |
status varchar(10) default 'publish' NOT NULL, |
| 512 |
cache text, |
| 513 |
"; |
| 514 |
|
| 515 |
foreach($blocks as $block) |
| 516 |
{ |
| 517 |
$block_name = $block->get_name(); |
| 518 |
|
| 519 |
$sql .= "" . $block_name . " TEXT NULL, \n "; |
| 520 |
} |
| 521 |
|
| 522 |
$sql .= "updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 523 |
created TIMESTAMP DEFAULT 0 NOT NULL, |
| 524 |
PRIMARY KEY (id) )"; |
| 525 |
|
| 526 |
$result = dbDelta($sql); // always dbdelta |
| 527 |
|
| 528 |
// Reset the cache if there were any left from before |
| 529 |
$button->reset_cache(); |
| 530 |
|
| 531 |
// Collection table |
| 532 |
$collection_table_name = maxUtils::get_collection_table_name(); |
| 533 |
|
| 534 |
$sql = "CREATE TABLE " . $collection_table_name . " ( |
| 535 |
meta_id int(11) NOT NULL AUTO_INCREMENT, |
| 536 |
collection_id int(11) NOT NULL, |
| 537 |
collection_key varchar(255), |
| 538 |
collection_value text, |
| 539 |
PRIMARY KEY (meta_id) ) |
| 540 |
"; |
| 541 |
|
| 542 |
dbDelta($sql); |
| 543 |
|
| 544 |
$collection_trans_table = maxUtils::get_coltrans_table_name(); |
| 545 |
$sql = "CREATE TABLE " . $collection_trans_table . " ( |
| 546 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 547 |
name varchar(1000), |
| 548 |
value varchar(255), |
| 549 |
expire int(11), |
| 550 |
PRIMARY KEY (id) )"; |
| 551 |
|
| 552 |
$res = dbDelta($sql); |
| 553 |
} |
| 554 |
|
| 555 |
/** Attempt to upgrade UTF table to UTFmb4 - see this article https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/ |
| 556 |
*/ |
| 557 |
public static function upgradeUTF() |
| 558 |
{ |
| 559 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 560 |
|
| 561 |
if (! function_exists('maybe_convert_table_to_utf8mb4')) |
| 562 |
return; // Versions before 4.2.0 |
| 563 |
|
| 564 |
$table_name = maxUtils::get_table_name(); |
| 565 |
$collection_table_name = maxUtils::get_collection_table_name(); |
| 566 |
|
| 567 |
maybe_convert_table_to_utf8mb4($table_name); |
| 568 |
maybe_convert_table_to_utf8mb4($collection_table_name); |
| 569 |
} |
| 570 |
|
| 571 |
/** Routine for activation for WPMU - All blogs */ |
| 572 |
public static function call_function_for_each_site($function) { |
| 573 |
global $wpdb; |
| 574 |
|
| 575 |
// Hold this so we can switch back to it |
| 576 |
$root_blog = $wpdb->blogid; |
| 577 |
|
| 578 |
// Get all the blogs/sites in the network and invoke the function for each one |
| 579 |
$blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 580 |
foreach ($blog_ids as $blog_id) { |
| 581 |
switch_to_blog($blog_id); |
| 582 |
call_user_func($function); |
| 583 |
} |
| 584 |
|
| 585 |
// Now switch back to the root blog |
| 586 |
switch_to_blog($root_blog); |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
} // class |
| 591 |
|