PluginProbe
PostNL for WooCommerce / 4.0.1
PostNL for WooCommerce v4.0.1
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / migration / wcpn-upgrade-migration.php

wcpn-upgrade-migration.php in PostNL for WooCommerce 4.0.1, at migration/wcpn-upgrade-migration.php

103 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace migration;
4
5 abstract class WCPN_Upgrade_Migration
6 {
7 /**
8 * @var array
9 */
10 protected $optionSettingsMap = [];
11
12 protected function __construct()
13 {
14 $this->import();
15 $this->migrate();
16 $this->setOptionSettingsMap();
17 $this->save();
18 }
19
20 /**
21 * Import any dependencies you might need using this function
22 */
23 protected function import(): void {}
24
25 /**
26 * Put the migration logic in this function.
27 */
28 protected function migrate(): void {}
29
30 /**
31 *
32 */
33 protected function setOptionSettingsMap(): void {}
34
35 /**
36 * @param array $map
37 * @param array|null $newSettings
38 * @param array|null $oldSettings
39 *
40 * @return array|null
41 */
42 protected function migrateSettings(array $map, ?array $newSettings, array $oldSettings = null): ?array
43 {
44 $oldSettings = $oldSettings ?? $newSettings;
45
46 if (! $oldSettings) {
47 return null;
48 }
49
50 foreach ($map as $oldSetting => $newSetting) {
51 if (array_key_exists($oldSetting, $oldSettings)) {
52 $newSettings[$newSetting] = $oldSettings[$oldSetting];
53 }
54
55 if (array_key_exists($oldSetting, $newSettings)) {
56 unset($newSettings[$oldSetting]);
57 }
58 }
59
60 return $newSettings;
61 }
62
63 /**
64 * @param array $map
65 * @param array $newSettings
66 *
67 * @return array
68 */
69 protected function removeOldSettings(array $map, array $newSettings): array
70 {
71 foreach ($map as $oldSetting => $newSetting) {
72 if (array_key_exists($oldSetting, $newSettings)) {
73 unset($newSettings[$oldSetting]);
74 }
75 }
76 return $newSettings;
77 }
78
79 protected function save()
80 {
81 foreach ($this->optionSettingsMap as $option => $settings) {
82 if (get_option($option) === false) {
83 add_option($option, $settings);
84 } else {
85 update_option($option, $settings);
86 }
87 }
88 }
89
90 /**
91 * Get settings array. Falls back to empty array of get_option returns a falsy value. Not compatible with
92 * non-array settings.
93 *
94 * @param string $settingName
95 *
96 * @return array
97 */
98 protected function getSettings(string $settingName): array
99 {
100 return get_option($settingName) ?: [];
101 }
102 }
103