PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / trunk
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / inc / functions / upgrades.php

upgrades.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More trunk, at inc/functions/upgrades.php

228 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Backward compatibility functions.
4 *
5 * @package ContentControl
6 */
7
8 namespace ContentControl;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 use function wp_parse_args;
15 use function apply_filters;
16
17 /**
18 * Get the current data versions.
19 *
20 * @return int[]
21 */
22 function current_data_versions() {
23 return apply_filters( 'content_control/current_data_versions', [
24 'backup' => 2,
25 'settings' => 2,
26 'restrictions' => 2,
27 'plugin_meta' => 2,
28 'user_meta' => 2,
29 ] );
30 }
31
32 /**
33 * Get all data versions.
34 *
35 * @return int[]
36 */
37 function get_data_versions() {
38 $versioning = \get_option( 'content_control_data_versioning', [] );
39
40 return wp_parse_args( $versioning, current_data_versions() );
41 }
42
43 /**
44 * Set the data version.
45 *
46 * @param string $key Data key.
47 * @param int $version Data version.
48 *
49 * @return bool
50 */
51 function set_data_version( $key, $version ) {
52 $versioning = get_data_versions();
53
54 $versioning[ $key ] = $version;
55
56 return set_data_versions( $versioning );
57 }
58
59 /**
60 * Set the data version.
61 *
62 * @param int[] $versioning Data versions.
63 *
64 * @return bool
65 */
66 function set_data_versions( $versioning ) {
67 $versioning = wp_parse_args( $versioning, get_data_versions() );
68
69 return \update_option( 'content_control_data_versioning', $versioning );
70 }
71
72 /**
73 * Get the current data version.
74 *
75 * @param string $key Type of data to get version for.
76 *
77 * @return int|bool
78 */
79 function get_data_version( $key ) {
80 $versioning = get_data_versions();
81
82 switch ( $key ) {
83 // Fallthrough.
84 case 'restrictions':
85 // If set to v1 and there are no v1 restrictions, set to v2.
86 if ( 1 === $versioning[ $key ] ) {
87 $v1_restrictions = get_v1_restrictions();
88
89 if ( false === $v1_restrictions ) {
90 $versioning[ $key ] = 2;
91 set_data_versions( $versioning );
92 return 2;
93 }
94 }
95
96 break;
97
98 case 'settings':
99 // If set to v1 and there are no v1 settings, set to v2.
100 if ( 1 === $versioning[ $key ] ) {
101 $v1_settings = \get_option( 'jp_cc_settings', [] );
102
103 if ( empty( $v1_settings ) ) {
104 $versioning[ $key ] = 2;
105 $versioning['backup'] = 2; // Backup is always the same as settings.
106 set_data_versions( $versioning );
107 return 2;
108 }
109 }
110
111 break;
112 }
113
114 return isset( $versioning[ $key ] ) ? $versioning[ $key ] : false;
115 }
116
117 /**
118 * Checks if user is upgrading from < 2.0.0.
119 *
120 * Sets data versioning to 1 as they didn't exist before.
121 *
122 * @param string $old_version Old version.
123 *
124 * @return void
125 */
126 function maybe_force_v2_migrations( $old_version ) {
127 if ( version_compare( $old_version, '2.0.0', '<' ) ) {
128 $versioning = get_data_versions();
129
130 // Forces updates for all data types to v2.
131 $versioning = wp_parse_args( [
132 'backup' => 1,
133 'settings' => 1,
134 'restrictions' => 1,
135 'plugin_meta' => 1,
136 'user_meta' => 1,
137 ], $versioning );
138
139 \update_option( 'content_control_data_versioning', $versioning );
140 }
141 }
142
143 /**
144 * Get the name of an upgrade.
145 *
146 * @param string|\ContentControl\Base\Upgrade $upgrade Upgrade to get name for.
147 *
148 * @return string
149 */
150 function get_upgrade_name( $upgrade ) {
151 if ( is_object( $upgrade ) ) {
152 $upgrade = $upgrade::TYPE . '-' . $upgrade::VERSION;
153 }
154
155 return $upgrade;
156 }
157
158 /**
159 * Get the completed upgrades.
160 *
161 * @return string[]
162 */
163 function get_completed_upgrades() {
164 return \get_option( 'content_control_completed_upgrades', [] );
165 }
166
167 /**
168 * Set the completed upgrades.
169 *
170 * @param string[] $upgrades Completed upgrades.
171 *
172 * @return bool
173 */
174 function set_completed_upgrades( $upgrades ) {
175 return \update_option( 'content_control_completed_upgrades', $upgrades );
176 }
177
178 /**
179 * Mark an upgrade as complete.
180 *
181 * @param \ContentControl\Base\Upgrade $upgrade Upgrade to mark as complete.
182 *
183 * @return void
184 */
185 function mark_upgrade_complete( $upgrade ) {
186 $upgrade_name = get_upgrade_name( $upgrade );
187
188 $upgrades = get_completed_upgrades();
189
190 if ( ! in_array( $upgrade_name, $upgrades, true ) ) {
191 $upgrades[] = $upgrade_name;
192 }
193
194 set_completed_upgrades( $upgrades );
195
196 // Update the data version.
197 set_data_version( $upgrade::TYPE, $upgrade::VERSION );
198
199 /**
200 * Fires when an upgrade is marked as complete.
201 *
202 * @param \ContentControl\Base\Upgrade $upgrade Upgrade type.
203 */
204 do_action( 'content_control/upgrade_complete', $upgrade );
205
206 /**
207 * Fires when a specific upgrade is marked as complete.
208 *
209 * @param \ContentControl\Base\Upgrade $upgrade Upgrade type.
210 */
211 do_action( "content_control/upgrade_complete/{$upgrade_name}", $upgrade );
212 }
213
214 /**
215 * Check if an upgrade has been completed.
216 *
217 * @param string|\ContentControl\Base\Upgrade $upgrade Upgrade to check.
218 *
219 * @return bool
220 */
221 function is_upgrade_complete( $upgrade ) {
222 $upgrade = get_upgrade_name( $upgrade );
223
224 $upgrades = get_completed_upgrades();
225
226 return in_array( $upgrade, $upgrades, true );
227 }
228