PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.12
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.12
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 2.0.12, at inc/functions/upgrades.php

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