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