PluginProbe
Responsive Menu – Create Mobile-Friendly Menu / 3.0.4
Responsive Menu – Create Mobile-Friendly Menu v3.0.4
4.7.3 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.10 4.1.11 4.1.12 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 All 90 releases
responsive-menu / src / app / Database / Migration.php

Migration.php in Responsive Menu – Create Mobile-Friendly Menu 3.0.4, at src/app/Database/Migration.php

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