| 1 |
<?php |
| 2 |
|
| 3 |
namespace AgeGate\Update\Migration; |
| 4 |
|
| 5 |
use Asylum\Utility\Arr; |
| 6 |
use Asylum\Utility\Storage; |
| 7 |
use Asylum\Utility\HtmlToMarkdown; |
| 8 |
use League\HTMLToMarkdown\HtmlConverter; |
| 9 |
|
| 10 |
class Migrate |
| 11 |
{ |
| 12 |
private $mapTwo = [ |
| 13 |
"restrictions.min_age" => "restriction.default_age", |
| 14 |
"restrictions.restriction_type" => "restriction.type", |
| 15 |
"restrictions.multi_age" => "restriction.multi_age", |
| 16 |
"restrictions.stepped" => "restriction.stepped", |
| 17 |
"restrictions.input_type" => "restriction.input_type", |
| 18 |
|
| 19 |
"restrictions.remember" => "restriction.remember", |
| 20 |
"restrictions.remember_days" => "restriction.remember_length.remember_length", |
| 21 |
"restrictions.remember_timescale" => "restriction.remember_length.remember_time", |
| 22 |
"restrictions.remember_auto_check" => "restriction.remember_auto_check", |
| 23 |
"restrictions.ignore_logged" => "restriction.ignore_logged", |
| 24 |
"restrictions.rechallenge" => "restriction.rechallenge", |
| 25 |
"restrictions.fail_link_title" => null, |
| 26 |
"restrictions.fail_link" => "restriction.redirect", |
| 27 |
"restrictions.button_order" => "restriction.button_order", |
| 28 |
|
| 29 |
// |
| 30 |
"messages.instruction" => 'message.headline', |
| 31 |
"messages.messaging" => 'message.subheadline', |
| 32 |
"messages.invalid_input_msg" => 'message.error_invalid', |
| 33 |
"messages.under_age_msg" => 'message.error_failed', |
| 34 |
"messages.generic_error_msg" => "message.error_generic", |
| 35 |
"messages.remember_me_text" => "message.label_remember", |
| 36 |
"messages.yes_no_message" => "message.label_buttons", |
| 37 |
"messages.yes_text" => "message.label_yes", |
| 38 |
"messages.no_text" => "message.label_no", |
| 39 |
"messages.button_text" => "message.label_submit", |
| 40 |
"messages.cookie_message" => "message.label_no_cookies", |
| 41 |
"messages.text_day" => "message.label_day", |
| 42 |
"messages.text_month" => "message.label_month", |
| 43 |
"messages.text_year" => "message.label_year", |
| 44 |
"messages.aria_label" => "message.label_aria", |
| 45 |
|
| 46 |
// |
| 47 |
"validation_messages.validate_required" => null, |
| 48 |
"validation_messages.validate_numeric" => null, |
| 49 |
"validation_messages.validate_max_len" => null, |
| 50 |
"validation_messages.validate_min_len" => null, |
| 51 |
"validation_messages.validate_min_numeric" => null, |
| 52 |
"validation_messages.validate_max_numeric" => null, |
| 53 |
"validation_messages.validate_min_age" => null, |
| 54 |
"validation_messages.validate_date" => null, |
| 55 |
|
| 56 |
// |
| 57 |
"appearance.logo" => "appearance.logo", |
| 58 |
"appearance.background_colour" => "appearance.background_color", |
| 59 |
"appearance.background_opacity" => "appearance.background_opacity", |
| 60 |
"appearance.background_image" => "appearance.background_image", |
| 61 |
"appearance.background_image_opacity" => "appearance.background_image_opacity", |
| 62 |
"appearance.foreground_colour" => "appearance.foreground_color", |
| 63 |
"appearance.foreground_opacity" => "appearance.foreground_opacity", |
| 64 |
"appearance.text_colour" => "appearance.text_color", |
| 65 |
"appearance.styling" => "appearance.enqueue_css", |
| 66 |
"appearance.device_width" => "appearance.viewport", |
| 67 |
"appearance.switch_title" => "appearance.switch_title", |
| 68 |
"appearance.custom_title" => "appearance.custom_title", |
| 69 |
"appearance.auto_tab" => "appearance.input_auto_tab", |
| 70 |
"appearance.title_separator" => null, |
| 71 |
"appearance.title_format" => null, |
| 72 |
"appearance.transition" => "appearance.exit_transition", // check values |
| 73 |
"appearance.background_pos_x" => "appearance.background_position.x", |
| 74 |
"appearance.background_pos_y" => "appearance.background_position.y", |
| 75 |
"appearance.blur_amount" => "appearance.blur", |
| 76 |
|
| 77 |
// |
| 78 |
|
| 79 |
"advanced.save_to_file" => "advanced.css_file", // if this and that are true, move to new location |
| 80 |
"advanced.custom_css" => "advanced.css", |
| 81 |
"advanced.dev_notify" => "tools.dev_versions", |
| 82 |
"advanced.dev_hide_warning" => "tools.dev_warning", |
| 83 |
"advanced.anonymous_age_gate" => "advanced.anonymous", |
| 84 |
"advanced.endpoint" => null, // only REST |
| 85 |
"advanced.use_default_lang" => null, // default |
| 86 |
"advanced.use_meta_box" => null, // |
| 87 |
"advanced.inherit_taxonomies.category" => null, // need updating |
| 88 |
// "advanced.custom_bots" => "advanced.user_agents", |
| 89 |
"advanced.enable_quicktags" => null, |
| 90 |
"advanced.full_nav" => "advanced.toolbar", |
| 91 |
"advanced.enable_import_export" => null, |
| 92 |
"advanced.filter_qs" => null, |
| 93 |
"advanced.post_to_self" => null, |
| 94 |
"advanced.js_hooks" => null, |
| 95 |
"advanced.cookie_name" => "advanced.cookie_name", |
| 96 |
"advanced.rta_tag" => "advanced.rta", |
| 97 |
"advanced.munge_options" => "advanced.munge", |
| 98 |
"advanced.trap_focus" => "advanced.focus", |
| 99 |
"advanced.disable_right" => "advanced.dev_tools", |
| 100 |
"advanced.preload_images" => "advanced.preload", |
| 101 |
]; |
| 102 |
|
| 103 |
private $mapTwoSpecial = [ |
| 104 |
"advanced.use_js" => ["key" => "advanced.method", "callback" => "getMethod"], // set, js, not set, standard |
| 105 |
// // will need a loop to content.disable.<posttype> |
| 106 |
// "access.post", |
| 107 |
// "access.page", |
| 108 |
// "access.attachment", |
| 109 |
// "access.revision", |
| 110 |
// "access.nav_menu_item", |
| 111 |
// "access.custom_css", |
| 112 |
// "access.customize_changeset", |
| 113 |
// "access.oembed_cache", |
| 114 |
// "access.user_request", |
| 115 |
// "access.wp_block", |
| 116 |
// "access.wp_template", |
| 117 |
// "access.wp_template_part", |
| 118 |
// "access.wp_global_styles", |
| 119 |
// "access.wp_navigation", |
| 120 |
// "access.acf-field-group", |
| 121 |
// "access.acf-field", |
| 122 |
|
| 123 |
"restrictions.date_format" => [ |
| 124 |
'key' => "restriction.date_format", |
| 125 |
'callback' => 'getDateFormat', |
| 126 |
], |
| 127 |
|
| 128 |
"restrictions.inherit_category" => [ |
| 129 |
'key' => '', |
| 130 |
'callback' => 'getInherited', // Work out what the hell is happening! |
| 131 |
], |
| 132 |
"messages.additional" => [ |
| 133 |
'key' => "message.content", |
| 134 |
'callback' => 'getMarkdown', // convert to markdown! |
| 135 |
], |
| 136 |
|
| 137 |
// // Languages :??? |
| 138 |
// "restrictions.lang.fr.min_age", |
| 139 |
// "restrictions.lang.fr.date_format", |
| 140 |
// "restrictions.lang.fr.fail_link_title", |
| 141 |
// "restrictions.lang.fr.fail_link", |
| 142 |
]; |
| 143 |
|
| 144 |
public function mapSimpleSettings($settings) |
| 145 |
{ |
| 146 |
$legacy = [ |
| 147 |
'restrictions' => get_option('wp_age_gate_restrictions', []), |
| 148 |
'messages' => get_option('wp_age_gate_messages', []), |
| 149 |
'validation_messages' => get_option('wp_age_gate_validation_messages', []), |
| 150 |
'appearance' => get_option('wp_age_gate_appearance', []), |
| 151 |
'advanced' => get_option('wp_age_gate_advanced', []), |
| 152 |
'access' => get_option('wp_age_gate_access', []), |
| 153 |
]; |
| 154 |
|
| 155 |
$v3 = Arr::dot($settings); |
| 156 |
$v2 = Arr::dot($legacy); |
| 157 |
|
| 158 |
|
| 159 |
// map all the 1-to-1 settings |
| 160 |
foreach ($v2 as $key => $option) { |
| 161 |
if (array_key_exists($key, $this->mapTwo)) { |
| 162 |
if ($this->mapTwo[$key]) { |
| 163 |
$v3[$this->mapTwo[$key]] = $option; |
| 164 |
}; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
if (array_key_exists($key, $this->mapTwoSpecial)) { |
| 169 |
if ($this->mapTwoSpecial[$key]) { |
| 170 |
$mapped = $this->{$this->mapTwoSpecial[$key]['callback']}($option, $v2); |
| 171 |
|
| 172 |
if (!is_array($mapped)) { |
| 173 |
$v3[$this->mapTwoSpecial[$key]['key']] = $mapped; |
| 174 |
} else { |
| 175 |
$v3 = array_merge($v3, $mapped); |
| 176 |
} |
| 177 |
}; |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
foreach ($legacy['access'] ?? [] as $type => $value) { |
| 183 |
$v3['content.disable.' . $type] = $value; |
| 184 |
} |
| 185 |
|
| 186 |
// Move custom CSS to customizer |
| 187 |
|
| 188 |
// languages... |
| 189 |
foreach ($legacy as $key => $options) { |
| 190 |
foreach($options['lang'] ?? [] as $code => $options) { |
| 191 |
foreach ($options as $option => $value) { |
| 192 |
if (array_key_exists($key . '.' . $option, $this->mapTwo)) { |
| 193 |
if ($this->mapTwo[$key . '.' . $option]) { |
| 194 |
|
| 195 |
$parts = explode('.', $this->mapTwo[$key . '.' . $option] ?? ''); |
| 196 |
array_splice($parts, 1, 0, [$code]); |
| 197 |
$v3Key = implode('.', $parts); |
| 198 |
|
| 199 |
$v3[$v3Key] = $value; |
| 200 |
}; |
| 201 |
} |
| 202 |
|
| 203 |
if (array_key_exists($key . '.' . $option, $this->mapTwoSpecial)) { |
| 204 |
if ($this->mapTwoSpecial[$key . '.' . $option]) { |
| 205 |
$mapped = $this->{$this->mapTwoSpecial[$key . '.' . $option]['callback']}($option, $v2); |
| 206 |
|
| 207 |
$parts = explode('.', $this->mapTwoSpecial[$key . '.' . $option]['key'] ?? ''); |
| 208 |
array_splice($parts, 1, 0, [$code]); |
| 209 |
$v3Key = implode('.', $parts); |
| 210 |
|
| 211 |
if (!is_array($mapped)) { |
| 212 |
$v3[$v3Key] = $mapped; |
| 213 |
} else { |
| 214 |
$v3 = array_merge($v3, $mapped); |
| 215 |
} |
| 216 |
}; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
// dump($key . '.' . $option); |
| 222 |
|
| 223 |
// $parts = explode('.', $this->mapTwo[$key . '.' . $option] ?? ''); |
| 224 |
|
| 225 |
// array_splice($parts, 1, 0, [$code]); |
| 226 |
// dump(implode('.', $parts)); |
| 227 |
// dump($option, $value); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
// default updates to V@ styles? |
| 233 |
$v3['advanced.css_type'] = 'v2'; |
| 234 |
|
| 235 |
$v3['advanced.user_agents'] = implode("\r\n", $legacy['advanced']['custom_bots'] ?? []); |
| 236 |
|
| 237 |
return Arr::undot($v3); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Determine the method |
| 242 |
* |
| 243 |
* @param string|int $value |
| 244 |
* @return strong |
| 245 |
*/ |
| 246 |
private function getMethod($value) |
| 247 |
{ |
| 248 |
return $value == 1 ? 'js' : 'standard'; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Convert old html to markdown |
| 253 |
* |
| 254 |
* @param string $value |
| 255 |
* @return string |
| 256 |
*/ |
| 257 |
private function getMarkdown($value) |
| 258 |
{ |
| 259 |
if (class_exists('DomDocument')) { |
| 260 |
$converter = new HtmlConverter(); |
| 261 |
} else { |
| 262 |
$converter = new HtmlToMarkdown; |
| 263 |
} |
| 264 |
|
| 265 |
return preg_replace('/(\[.*\])\(\"(.*)\"\)/', '$1($2)', stripslashes(wp_strip_all_tags($converter->convert(html_entity_decode(html_entity_decode($value)))))); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Map old bad term inheritance to new, better version |
| 270 |
* |
| 271 |
* @param mixed $value |
| 272 |
* @param array $all |
| 273 |
* @return void |
| 274 |
*/ |
| 275 |
private function getInherited($value, $all) |
| 276 |
{ |
| 277 |
if (!$value) { |
| 278 |
return false; |
| 279 |
} |
| 280 |
|
| 281 |
$data = [ |
| 282 |
'content.inherit' => 1, |
| 283 |
]; |
| 284 |
|
| 285 |
$all = Arr::undot($all); |
| 286 |
foreach ($all['advanced']['inherit_taxonomies'] as $tax) { |
| 287 |
$taxonomy = get_taxonomy($tax); |
| 288 |
$terms = get_terms([ |
| 289 |
'taxonomy' => $tax, |
| 290 |
]); |
| 291 |
|
| 292 |
foreach ($terms as $term) { |
| 293 |
foreach ($taxonomy->object_type as $type) { |
| 294 |
$data['content.terms.' . $term->term_id . '.' . $type] = 1; |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
return $data; |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Determine the new date formate |
| 304 |
* |
| 305 |
* @param string $value |
| 306 |
* @return string |
| 307 |
*/ |
| 308 |
private function getDateFormat($value) |
| 309 |
{ |
| 310 |
switch ($value) { |
| 311 |
case 'ddmmyyyy': |
| 312 |
return 'DD MM YYYY'; |
| 313 |
case 'mmddyyyy': |
| 314 |
return 'MM DD YYYY'; |
| 315 |
} |
| 316 |
|
| 317 |
return 'DD MM YYYY'; |
| 318 |
} |
| 319 |
} |
| 320 |
|