| 1 |
<?php |
| 2 |
|
| 3 |
namespace ResponsiveMenu\Database; |
| 4 |
use ResponsiveMenu\Collections\OptionsCollection as OptionsCollection; |
| 5 |
use ResponsiveMenu\Database\Database as Database; |
| 6 |
use ResponsiveMenu\Services\OptionService as OptionService; |
| 7 |
|
| 8 |
class Migration { |
| 9 |
|
| 10 |
protected $db; |
| 11 |
|
| 12 |
protected $current_version; |
| 13 |
protected $old_version; |
| 14 |
protected $old_options; |
| 15 |
protected $defaults; |
| 16 |
|
| 17 |
protected static $table = 'responsive_menu'; |
| 18 |
protected static $version_var = 'RMVer'; |
| 19 |
|
| 20 |
public function __construct(Database $db, OptionService $service, $defaults, $current_version, $old_version, $old_options) { |
| 21 |
$this->db = $db; |
| 22 |
$this->service = $service; |
| 23 |
$this->defaults = $defaults; |
| 24 |
$this->current_version = $current_version; |
| 25 |
$this->old_version = $old_version; |
| 26 |
$this->old_options = $old_options; |
| 27 |
} |
| 28 |
|
| 29 |
public function addNewOptions() { |
| 30 |
$options = $this->service->all(); |
| 31 |
if($options->isEmpty()) |
| 32 |
$this->service->createOptions($this->defaults); |
| 33 |
else |
| 34 |
$this->service->createOptions($this->getNewOptions($options)); |
| 35 |
} |
| 36 |
|
| 37 |
public function tidyUpOptions() { |
| 38 |
foreach($this->getOptionsToDelete() as $delete) |
| 39 |
$this->db->delete(self::$table, array('name' => $delete)); |
| 40 |
} |
| 41 |
|
| 42 |
public function getNewOptions(OptionsCollection $options) { |
| 43 |
$current = []; |
| 44 |
foreach($options->all() as $converted) |
| 45 |
$current[$converted->getName()] = $converted->getValue(); |
| 46 |
return array_diff_key($this->defaults, $current); |
| 47 |
} |
| 48 |
|
| 49 |
/* |
| 50 |
Can't be tested :-( */ |
| 51 |
public function setup() { |
| 52 |
# Create the database table if it doesn't exist |
| 53 |
if(!$this->isVersion3()): |
| 54 |
$sql = "CREATE TABLE " . $this->db->getPrefix() . self::$table . " ( |
| 55 |
name varchar(50) NOT NULL, |
| 56 |
value varchar(5000) DEFAULT NULL, |
| 57 |
created_at datetime NOT NULL, |
| 58 |
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 59 |
PRIMARY KEY (name) |
| 60 |
) " . $this->db->getCharset() . ";"; |
| 61 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 62 |
dbDelta($sql); |
| 63 |
$this->synchronise(); |
| 64 |
endif; |
| 65 |
} |
| 66 |
|
| 67 |
public function synchronise() { |
| 68 |
|
| 69 |
# First Thing we need to do is migrate any old options |
| 70 |
if($this->old_options && !$this->isVersion3()) |
| 71 |
$this->migrateVersion2Options(); |
| 72 |
|
| 73 |
if($this->needsUpdate()): |
| 74 |
|
| 75 |
# Now we can add any new options |
| 76 |
$this->addNewOptions(); |
| 77 |
|
| 78 |
# Finally delete any that are no longer used |
| 79 |
$this->tidyUpOptions(); |
| 80 |
|
| 81 |
# And Update Version |
| 82 |
$this->updateVersion(); |
| 83 |
|
| 84 |
endif; |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
public function needsUpdate() { |
| 89 |
return version_compare($this->old_version, $this->current_version, '<'); |
| 90 |
} |
| 91 |
|
| 92 |
protected function updateVersion() { |
| 93 |
update_option(self::$version_var, $this->current_version); |
| 94 |
$this->old_version = $this->current_version; |
| 95 |
} |
| 96 |
|
| 97 |
public function isVersion3() { |
| 98 |
return substr($this->old_version, 0, 1) == 3; |
| 99 |
} |
| 100 |
|
| 101 |
public function migrateVersion2Options() { |
| 102 |
$this->service->createOptions($this->getMigratedOptions()); |
| 103 |
$this->addNewOptions(); |
| 104 |
$this->updateVersion(); |
| 105 |
} |
| 106 |
|
| 107 |
public function getOptionsToDelete() { |
| 108 |
return array_diff( |
| 109 |
array_map(function($a) { return $a->getName(); }, $this->service->all()->all()), |
| 110 |
array_keys($this->defaults) |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
public function getMigratedOptions() { |
| 115 |
$old_options = $this->old_options; |
| 116 |
|
| 117 |
$new_options = [ |
| 118 |
'menu_to_use' => $old_options['RM'] ? $old_options['RM'] : '', |
| 119 |
'breakpoint' => $old_options['RMBreak'] ? $old_options['RMBreak'] : '', |
| 120 |
'menu_depth' => $old_options['RMDepth'] ? $old_options['RMDepth'] : '', |
| 121 |
'button_top' => $old_options['RMTop'] ? $old_options['RMTop'] : '', |
| 122 |
'button_distance_from_side' => $old_options['RMRight'] ? $old_options['RMRight'] : '', |
| 123 |
'menu_to_hide' => $old_options['RMCss'] ? $old_options['RMCss'] : '', |
| 124 |
'menu_title' => $old_options['RMTitle'] ? $old_options['RMTitle'] : '', |
| 125 |
'button_line_colour' => $old_options['RMLineCol'] ? $old_options['RMLineCol'] : '', |
| 126 |
'button_background_colour' => $old_options['RMClickBkg'] ? $old_options['RMClickBkg'] : '', |
| 127 |
'button_title' => $old_options['RMClickTitle'] ? $old_options['RMClickTitle'] : '', |
| 128 |
'button_transparent_background' => $old_options['RMBkgTran'] ? 'on' : '', |
| 129 |
'menu_font' => $old_options['RMFont'] ? $old_options['RMFont'] : '', |
| 130 |
'button_position_type' => $old_options['RMPos'] ? 'fixed' : '', |
| 131 |
'menu_title_image' => $old_options['RMImage'] ? $old_options['RMImage'] : '', |
| 132 |
'menu_width' => $old_options['RMWidth'] ? $old_options['RMWidth'] : '', |
| 133 |
'menu_item_background_colour' => $old_options['RMBkg'] ? $old_options['RMBkg'] : '', |
| 134 |
'menu_background_colour' => $old_options['RMBkg'] ? $old_options['RMBkg'] : '', |
| 135 |
'menu_sub_arrow_background_colour' => $old_options['RMBkg'] ? $old_options['RMBkg'] : '', |
| 136 |
'menu_item_background_hover_colour' => $old_options['RMBkgHov'] ? $old_options['RMBkgHov'] : '', |
| 137 |
'menu_sub_arrow_background_hover_colour' => $old_options['RMBkgHov'] ? $old_options['RMBkgHov'] : '', |
| 138 |
'menu_title_colour' => $old_options['RMTitleCol'] ? $old_options['RMTitleCol'] : '', |
| 139 |
'menu_link_colour' => $old_options['RMTextCol'] ? $old_options['RMTextCol'] : '', |
| 140 |
'menu_sub_arrow_shape_colour' => $old_options['RMTextCol'] ? $old_options['RMTextCol'] : '', |
| 141 |
'menu_item_border_colour' => $old_options['RMBorCol'] ? $old_options['RMBorCol'] : '', |
| 142 |
'menu_item_border_colour_hover' => $old_options['RMBorCol'] ? $old_options['RMBorCol'] : '', |
| 143 |
'menu_sub_arrow_border_colour' => $old_options['RMBorCol'] ? $old_options['RMBorCol'] : '', |
| 144 |
'menu_sub_arrow_border_hover_colour' => $old_options['RMBorCol'] ? $old_options['RMBorCol'] : '', |
| 145 |
'menu_link_hover_colour' => $old_options['RMTextColHov'] ? $old_options['RMTextColHov'] : '', |
| 146 |
'menu_sub_arrow_shape_hover_colour' => $old_options['RMTextColHov'] ? $old_options['RMTextColHov'] : '', |
| 147 |
'menu_title_hover_colour' => $old_options['RMTitleColHov'] ? $old_options['RMTitleColHov'] : '', |
| 148 |
'animation_type' => $old_options['RMAnim'] == 'push' ? 'push' : '', |
| 149 |
'page_wrapper' => $old_options['RMPushCSS'] ? $old_options['RMPushCSS'] : '', |
| 150 |
'menu_title_background_colour' => $old_options['RMTitleBkg'] ? $old_options['RMTitleBkg'] : '', |
| 151 |
'menu_title_background_hover_colour' => $old_options['RMTitleBkg'] ? $old_options['RMTitleBkg'] : '', |
| 152 |
'menu_font_size' => $old_options['RMFontSize'] ? $old_options['RMFontSize'] : '', |
| 153 |
'menu_title_font_size' => $old_options['RMTitleSize'] ? $old_options['RMTitleSize'] : '', |
| 154 |
'button_font_size' => $old_options['RMBtnSize'] ? $old_options['RMBtnSize'] : '', |
| 155 |
'menu_current_item_background_colour' => $old_options['RMCurBkg'] ? $old_options['RMCurBkg'] : '', |
| 156 |
'menu_current_link_colour' => $old_options['RMCurCol'] ? $old_options['RMCurCol'] : '', |
| 157 |
'animation_speed' => $old_options['RMAnimSpd'] ? $old_options['RMAnimSpd'] : '', |
| 158 |
'transition_speed' => $old_options['RMTranSpd'] ? $old_options['RMTranSpd'] : '', |
| 159 |
'menu_text_alignment' => $old_options['RMTxtAlign'] ? $old_options['RMTxtAlign'] : '', |
| 160 |
'auto_expand_all_submenus' => $old_options['RMExpand'] ? 'on' : '', |
| 161 |
'menu_links_height' => $old_options['RMLinkHeight'] ? $old_options['RMLinkHeight'] + 24 : '', |
| 162 |
'submenu_arrow_height' => $old_options['RMLinkHeight'] ? $old_options['RMLinkHeight'] + 24 : '', |
| 163 |
'submenu_arrow_width' => $old_options['RMLinkHeight'] ? $old_options['RMLinkHeight'] + 24 : '', |
| 164 |
'external_files' => $old_options['RMExternal'] ? 'on' : '', |
| 165 |
'menu_appear_from' => $old_options['RMSide'] ? $old_options['RMSide'] : '', |
| 166 |
'scripts_in_footer' => $old_options['RMFooter'] ? 'on' : '', |
| 167 |
'button_image' => $old_options['RMClickImg'] ? $old_options['RMClickImg'] : '', |
| 168 |
'minify_scripts' => $old_options['RMMinify'] ? 'on' : '', |
| 169 |
'menu_close_on_link_click' => $old_options['RMClickClose'] ? 'on' : '', |
| 170 |
'menu_minimum_width' => $old_options['RMMinWidth'] ? $old_options['RMMinWidth'] : '', |
| 171 |
'menu_maximum_width' => $old_options['RMMaxWidth'] ? $old_options['RMMaxWidth'] : '', |
| 172 |
'auto_expand_current_submenus' => $old_options['RMExpandPar'] ? 'on' : '', |
| 173 |
'menu_item_click_to_trigger_submenu' => $old_options['RMIgnParCli'] ? 'on' : '', |
| 174 |
'menu_close_on_body_click' => $old_options['RMCliToClo'] ? 'on' : '', |
| 175 |
'menu_title_link' => $old_options['RMTitleLink'] ? $old_options['RMTitleLink'] : '', |
| 176 |
'menu_additional_content' => $old_options['RMHtml'] ? $old_options['RMHtml'] : '', |
| 177 |
'shortcode' => $old_options['RMShortcode'] ? 'on' : '', |
| 178 |
'button_line_height' => $old_options['RMLineHeight'] ? $old_options['RMLineHeight'] : '', |
| 179 |
'button_line_width' => $old_options['RMLineWidth'] ? $old_options['RMLineWidth'] : '', |
| 180 |
'button_line_margin' => $old_options['RMLineMargin'] ? $old_options['RMLineMargin'] : '', |
| 181 |
'button_image_when_clicked' => $old_options['RMClickImgClicked'] ? $old_options['RMClickImgClicked'] : '', |
| 182 |
'accordion_animation' => $old_options['RMAccordion'] ? 'on' : '', |
| 183 |
'active_arrow_shape' => $old_options['RMArShpA'] ? json_decode($old_options['RMArShpA']) : '', |
| 184 |
'inactive_arrow_shape' => $old_options['RMArShpI'] ? json_decode($old_options['RMArShpI']) : '', |
| 185 |
'active_arrow_image' => $old_options['RMArImgA'] ? $old_options['RMArImgA'] : '', |
| 186 |
'inactive_arrow_image' => $old_options['RMArImgI'] ? $old_options['RMArImgI'] : '', |
| 187 |
'button_push_with_animation' => $old_options['RMPushBtn'] ? 'on' : '', |
| 188 |
'menu_current_item_background_hover_colour' => $old_options['RMCurBkgHov'] ? $old_options['RMCurBkgHov'] : '', |
| 189 |
'menu_current_link_hover_colour' => $old_options['RMCurColHov'] ? $old_options['RMCurColHov'] : '', |
| 190 |
'custom_walker' => $old_options['RMWalker'] ? $old_options['RMWalker'] : '', |
| 191 |
'button_left_or_right' => $old_options['RMLoc'] ? $old_options['RMLoc'] : '', |
| 192 |
'theme_location_menu' => $old_options['RMThemeLocation'] ? $old_options['RMThemeLocation'] : '', |
| 193 |
'button_title_position' => $old_options['RMClickTitlePos'] ? $old_options['RMClickTitlePos'] : '', |
| 194 |
]; |
| 195 |
|
| 196 |
$to_save = []; |
| 197 |
|
| 198 |
foreach(array_filter($new_options) as $key => $val) |
| 199 |
$to_save[$key] = $val; |
| 200 |
|
| 201 |
return $to_save; |
| 202 |
} |
| 203 |
|
| 204 |
} |
| 205 |
|