PluginProbe
ActivityPub / 2.5.0
ActivityPub v2.5.0
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / class-migration.php

class-migration.php in ActivityPub 2.5.0, at includes/class-migration.php

288 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub;
3
4 use Activitypub\Activitypub;
5 use Activitypub\Model\Blog;
6 use Activitypub\Collection\Followers;
7
8 /**
9 * ActivityPub Migration Class
10 *
11 * @author Matthias Pfefferle
12 */
13 class Migration {
14 /**
15 * Initialize the class, registering WordPress hooks
16 */
17 public static function init() {
18 \add_action( 'activitypub_migrate', array( self::class, 'async_migration' ) );
19
20 self::maybe_migrate();
21 }
22
23 /**
24 * Get the target version.
25 *
26 * This is the version that the database structure will be updated to.
27 * It is the same as the plugin version.
28 *
29 * @return string The target version.
30 */
31 public static function get_target_version() {
32 return get_plugin_version();
33 }
34
35 /**
36 * The current version of the database structure.
37 *
38 * @return string The current version.
39 */
40 public static function get_version() {
41 return get_option( 'activitypub_db_version', 0 );
42 }
43
44 /**
45 * Locks the database migration process to prevent simultaneous migrations.
46 *
47 * @return void
48 */
49 public static function lock() {
50 \update_option( 'activitypub_migration_lock', \time() );
51 }
52
53 /**
54 * Unlocks the database migration process.
55 *
56 * @return void
57 */
58 public static function unlock() {
59 \delete_option( 'activitypub_migration_lock' );
60 }
61
62 /**
63 * Whether the database migration process is locked.
64 *
65 * @return boolean
66 */
67 public static function is_locked() {
68 $lock = \get_option( 'activitypub_migration_lock' );
69
70 if ( ! $lock ) {
71 return false;
72 }
73
74 $lock = (int) $lock;
75
76 if ( $lock < \time() - 1800 ) {
77 self::unlock();
78 return false;
79 }
80
81 return true;
82 }
83
84 /**
85 * Whether the database structure is up to date.
86 *
87 * @return bool True if the database structure is up to date, false otherwise.
88 */
89 public static function is_latest_version() {
90 return (bool) version_compare(
91 self::get_version(),
92 self::get_target_version(),
93 '=='
94 );
95 }
96
97 /**
98 * Updates the database structure if necessary.
99 */
100 public static function maybe_migrate() {
101 if ( self::is_latest_version() ) {
102 return;
103 }
104
105 if ( self::is_locked() ) {
106 return;
107 }
108
109 self::lock();
110
111 $version_from_db = self::get_version();
112
113 // check for inital migration
114 if ( ! $version_from_db ) {
115 self::add_default_settings();
116 $version_from_db = self::get_target_version();
117 }
118
119 // schedule the async migration
120 if ( ! \wp_next_scheduled( 'activitypub_migrate', $version_from_db ) ) {
121 \wp_schedule_single_event( \time(), 'activitypub_migrate', $version_from_db );
122 }
123 if ( version_compare( $version_from_db, '0.17.0', '<' ) ) {
124 self::migrate_from_0_16();
125 }
126 if ( version_compare( $version_from_db, '1.3.0', '<' ) ) {
127 self::migrate_from_1_2_0();
128 }
129 if ( version_compare( $version_from_db, '2.1.0', '<' ) ) {
130 self::migrate_from_2_0_0();
131 }
132 if ( version_compare( $version_from_db, '2.3.0', '<' ) ) {
133 self::migrate_from_2_2_0();
134 }
135
136 update_option( 'activitypub_db_version', self::get_target_version() );
137
138 self::unlock();
139 }
140
141 /**
142 * Asynchronously migrates the database structure.
143 *
144 * @param string $version_from_db The version from which to migrate.
145 */
146 public static function async_migration( $version_from_db ) {
147 if ( version_compare( $version_from_db, '1.0.0', '<' ) ) {
148 self::migrate_from_0_17();
149 }
150 }
151
152 /**
153 * Updates the custom template to use shortcodes instead of the deprecated templates.
154 *
155 * @return void
156 */
157 private static function migrate_from_0_16() {
158 // Get the custom template.
159 $old_content = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
160
161 // If the old content exists but is a blank string, we're going to need a flag to updated it even
162 // after setting it to the default contents.
163 $need_update = false;
164
165 // If the old contents is blank, use the defaults.
166 if ( '' === $old_content ) {
167 $old_content = ACTIVITYPUB_CUSTOM_POST_CONTENT;
168 $need_update = true;
169 }
170
171 // Set the new content to be the old content.
172 $content = $old_content;
173
174 // Convert old templates to shortcodes.
175 $content = \str_replace( '%title%', '[ap_title]', $content );
176 $content = \str_replace( '%excerpt%', '[ap_excerpt]', $content );
177 $content = \str_replace( '%content%', '[ap_content]', $content );
178 $content = \str_replace( '%permalink%', '[ap_permalink type="html"]', $content );
179 $content = \str_replace( '%shortlink%', '[ap_shortlink type="html"]', $content );
180 $content = \str_replace( '%hashtags%', '[ap_hashtags]', $content );
181 $content = \str_replace( '%tags%', '[ap_hashtags]', $content );
182
183 // Store the new template if required.
184 if ( $content !== $old_content || $need_update ) {
185 \update_option( 'activitypub_custom_post_content', $content );
186 }
187 }
188
189 /**
190 * Updates the DB-schema of the followers-list
191 *
192 * @return void
193 */
194 public static function migrate_from_0_17() {
195 // migrate followers
196 foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) {
197 $followers = get_user_meta( $user_id, 'activitypub_followers', true );
198
199 if ( $followers ) {
200 foreach ( $followers as $actor ) {
201 Followers::add_follower( $user_id, $actor );
202 }
203 }
204 }
205
206 Activitypub::flush_rewrite_rules();
207 }
208
209 /**
210 * Clear the cache after updating to 1.3.0
211 *
212 * @return void
213 */
214 private static function migrate_from_1_2_0() {
215 $user_ids = \get_users(
216 array(
217 'fields' => 'ID',
218 'capability__in' => array( 'publish_posts' ),
219 )
220 );
221
222 foreach ( $user_ids as $user_id ) {
223 wp_cache_delete( sprintf( Followers::CACHE_KEY_INBOXES, $user_id ), 'activitypub' );
224 }
225 }
226
227 /**
228 * Unschedule Hooks after updating to 2.0.0
229 *
230 * @return void
231 */
232 private static function migrate_from_2_0_0() {
233 wp_clear_scheduled_hook( 'activitypub_send_post_activity' );
234 wp_clear_scheduled_hook( 'activitypub_send_update_activity' );
235 wp_clear_scheduled_hook( 'activitypub_send_delete_activity' );
236
237 wp_unschedule_hook( 'activitypub_send_post_activity' );
238 wp_unschedule_hook( 'activitypub_send_update_activity' );
239 wp_unschedule_hook( 'activitypub_send_delete_activity' );
240
241 $object_type = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );
242 if ( 'article' === $object_type ) {
243 \update_option( 'activitypub_object_type', 'wordpress-post-format' );
244 }
245 }
246
247 /**
248 * Add the ActivityPub capability to all users that can publish posts
249 * Delete old meta to store followers
250 *
251 * @return void
252 */
253 private static function migrate_from_2_2_0() {
254 // add the ActivityPub capability to all users that can publish posts
255 self::add_activitypub_capability();
256 }
257
258 /**
259 * Set the defaults needed for the plugin to work
260 *
261 * * Add the ActivityPub capability to all users that can publish posts
262 *
263 * @return void
264 */
265 public static function add_default_settings() {
266 self::add_activitypub_capability();
267 }
268
269 /**
270 * Add the ActivityPub capability to all users that can publish posts
271 *
272 * @return void
273 */
274 private static function add_activitypub_capability() {
275 // get all WP_User objects that can publish posts
276 $users = \get_users(
277 array(
278 'capability__in' => array( 'publish_posts' ),
279 )
280 );
281
282 // add ActivityPub capability to all users that can publish posts
283 foreach ( $users as $user ) {
284 $user->add_cap( 'activitypub' );
285 }
286 }
287 }
288