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

192 lines 4.0 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 'settings' => 2,
23 'restrictions' => 2,
24 'plugin_meta' => 2,
25 'user_meta' => 2,
26 ] );
27 }
28
29 /**
30 * Get all data versions.
31 *
32 * @return int[]
33 */
34 function get_data_versions() {
35 $versioning = \get_option( 'content_control_data_versioning', [] );
36
37 return wp_parse_args( $versioning, current_data_versions() );
38 }
39
40 /**
41 * Set the data version.
42 *
43 * @param string $key Data key.
44 * @param int $version Data version.
45 *
46 * @return bool
47 */
48 function set_data_version( $key, $version ) {
49 $versioning = get_data_versions();
50
51 $versioning[ $key ] = $version;
52
53 return set_data_versions( $versioning );
54 }
55
56 /**
57 * Set the data version.
58 *
59 * @param int $versioning Data versions.
60 *
61 * @return bool
62 */
63 function set_data_versions( $versioning ) {
64 $versioning = wp_parse_args( $versioning, get_data_versions() );
65
66 return \update_option( 'content_control_data_versioning', $versioning );
67 }
68
69 /**
70 * Get the current data version.
71 *
72 * @param string $key Type of data to get version for.
73 *
74 * @return int|bool
75 */
76 function get_data_version( $key ) {
77 $versioning = get_data_versions();
78
79 return isset( $versioning[ $key ] ) ? $versioning[ $key ] : false;
80 }
81
82 add_action( 'content_control/update_version', __NAMESPACE__ . '\maybe_force_v2_migrations' );
83
84 /**
85 * Checks if user is upgrading from < 2.0.0.
86 *
87 * Sets data versioning to 1 as they didn't exist before.
88 *
89 * @param string $old_version Old version.
90 */
91 function maybe_force_v2_migrations( $old_version ) {
92 if ( version_compare( $old_version, '2.0.0', '<' ) ) {
93 $versioning = get_data_versions();
94
95 // Forces updates for all data types to v2.
96 $versioning = wp_parse_args( [
97 'settings' => 1,
98 'restrictions' => 1,
99 'plugin_meta' => 1,
100 'user_meta' => 1,
101 ], $versioning );
102
103 \update_option( 'content_control_data_versioning', $versioning );
104 }
105 }
106
107 /**
108 * Get the name of an upgrade.
109 *
110 * @param string|\ContentControl\Base\Upgrade $upgrade Upgrade to get name for.
111 *
112 * @return string
113 */
114 function get_upgrade_name( $upgrade ) {
115 if ( is_object( $upgrade ) ) {
116 $upgrade = $upgrade::TYPE . '-' . $upgrade::VERSION;
117 }
118
119 return $upgrade;
120 }
121
122 /**
123 * Get the completed upgrades.
124 *
125 * @return string[]
126 */
127 function get_completed_upgrades() {
128 return \get_option( 'content_control_completed_upgrades', [] );
129 }
130
131 /**
132 * Set the completed upgrades.
133 *
134 * @param string[] $upgrades Completed upgrades.
135 *
136 * @return bool
137 */
138 function set_completed_upgrades( $upgrades ) {
139 return \update_option( 'content_control_completed_upgrades', $upgrades );
140 }
141
142 /**
143 * Mark an upgrade as complete.
144 *
145 * @param \ContentControl\Base\Upgrade $upgrade Upgrade to mark as complete.
146 *
147 * @return void
148 */
149 function mark_upgrade_complete( $upgrade ) {
150 $upgrade_name = get_upgrade_name( $upgrade );
151
152 $upgrades = get_completed_upgrades();
153
154 if ( ! in_array( $upgrade_name, $upgrades, true ) ) {
155 $upgrades[] = $upgrade_name;
156 }
157
158 set_completed_upgrades( $upgrades );
159
160 // Update the data version.
161 set_data_version( $upgrade::TYPE, $upgrade::VERSION );
162
163 /**
164 * Fires when an upgrade is marked as complete.
165 *
166 * @param string $upgrade Upgrade type.
167 */
168 do_action( 'content_control/upgrade_complete', $upgrade );
169
170 /**
171 * Fires when a specific upgrade is marked as complete.
172 *
173 * @param string $upgrade Upgrade type.
174 */
175 do_action( "content_control/upgrade_complete/{$upgrade_name}" );
176 }
177
178 /**
179 * Check if an upgrade has been completed.
180 *
181 * @param string|\ContentControl\Base\Upgrade $upgrade Upgrade to check.
182 *
183 * @return bool
184 */
185 function is_upgrade_complete( $upgrade ) {
186 $upgrade = get_upgrade_name( $upgrade );
187
188 $upgrades = get_completed_upgrades();
189
190 return in_array( $upgrade, $upgrades, true );
191 }
192