PluginProbe
ActivityPub / 2.1.0
ActivityPub v2.1.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.1.0, at includes/class-migration.php

219 lines 5.3 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_User;
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_schedule_migration', array( self::class, 'maybe_migrate' ) );
19 }
20
21 /**
22 * Get the target version.
23 *
24 * This is the version that the database structure will be updated to.
25 * It is the same as the plugin version.
26 *
27 * @return string The target version.
28 */
29 public static function get_target_version() {
30 return get_plugin_version();
31 }
32
33 /**
34 * The current version of the database structure.
35 *
36 * @return string The current version.
37 */
38 public static function get_version() {
39 return get_option( 'activitypub_db_version', 0 );
40 }
41
42 /**
43 * Locks the database migration process to prevent simultaneous migrations.
44 *
45 * @return void
46 */
47 public static function lock() {
48 \update_option( 'activitypub_migration_lock', \time() );
49 }
50
51 /**
52 * Unlocks the database migration process.
53 *
54 * @return void
55 */
56 public static function unlock() {
57 \delete_option( 'activitypub_migration_lock' );
58 }
59
60 /**
61 * Whether the database migration process is locked.
62 *
63 * @return boolean
64 */
65 public static function is_locked() {
66 $lock = \get_option( 'activitypub_migration_lock' );
67
68 if ( ! $lock ) {
69 return false;
70 }
71
72 $lock = (int) $lock;
73
74 if ( $lock < \time() - 1800 ) {
75 self::unlock();
76 return false;
77 }
78
79 return true;
80 }
81
82 /**
83 * Whether the database structure is up to date.
84 *
85 * @return bool True if the database structure is up to date, false otherwise.
86 */
87 public static function is_latest_version() {
88 return (bool) version_compare(
89 self::get_version(),
90 self::get_target_version(),
91 '=='
92 );
93 }
94
95 /**
96 * Updates the database structure if necessary.
97 */
98 public static function maybe_migrate() {
99 if ( self::is_latest_version() ) {
100 return;
101 }
102
103 if ( self::is_locked() ) {
104 return;
105 }
106
107 self::lock();
108
109 $version_from_db = self::get_version();
110
111 if ( version_compare( $version_from_db, '0.17.0', '<' ) ) {
112 self::migrate_from_0_16();
113 }
114 if ( version_compare( $version_from_db, '1.0.0', '<' ) ) {
115 self::migrate_from_0_17();
116 }
117 if ( version_compare( $version_from_db, '1.3.0', '<' ) ) {
118 self::migrate_from_1_2_0();
119 }
120 if ( version_compare( $version_from_db, '2.1.0', '<' ) ) {
121 self::migrate_from_2_0_0();
122 }
123
124 update_option( 'activitypub_db_version', self::get_target_version() );
125
126 self::unlock();
127 }
128
129 /**
130 * Updates the DB-schema of the followers-list
131 *
132 * @return void
133 */
134 private static function migrate_from_0_17() {
135 // migrate followers
136 foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) {
137 $followers = get_user_meta( $user_id, 'activitypub_followers', true );
138
139 if ( $followers ) {
140 foreach ( $followers as $actor ) {
141 Followers::add_follower( $user_id, $actor );
142 }
143 }
144 }
145
146 Activitypub::flush_rewrite_rules();
147 }
148
149 /**
150 * Updates the custom template to use shortcodes instead of the deprecated templates.
151 *
152 * @return void
153 */
154 private static function migrate_from_0_16() {
155 // Get the custom template.
156 $old_content = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
157
158 // If the old content exists but is a blank string, we're going to need a flag to updated it even
159 // after setting it to the default contents.
160 $need_update = false;
161
162 // If the old contents is blank, use the defaults.
163 if ( '' === $old_content ) {
164 $old_content = ACTIVITYPUB_CUSTOM_POST_CONTENT;
165 $need_update = true;
166 }
167
168 // Set the new content to be the old content.
169 $content = $old_content;
170
171 // Convert old templates to shortcodes.
172 $content = \str_replace( '%title%', '[ap_title]', $content );
173 $content = \str_replace( '%excerpt%', '[ap_excerpt]', $content );
174 $content = \str_replace( '%content%', '[ap_content]', $content );
175 $content = \str_replace( '%permalink%', '[ap_permalink type="html"]', $content );
176 $content = \str_replace( '%shortlink%', '[ap_shortlink type="html"]', $content );
177 $content = \str_replace( '%hashtags%', '[ap_hashtags]', $content );
178 $content = \str_replace( '%tags%', '[ap_hashtags]', $content );
179
180 // Store the new template if required.
181 if ( $content !== $old_content || $need_update ) {
182 \update_option( 'activitypub_custom_post_content', $content );
183 }
184 }
185
186 /**
187 * Clear the cache after updating to 1.3.0
188 *
189 * @return void
190 */
191 private static function migrate_from_1_2_0() {
192 $user_ids = get_users(
193 array(
194 'fields' => 'ID',
195 'capability__in' => array( 'publish_posts' ),
196 )
197 );
198
199 foreach ( $user_ids as $user_id ) {
200 wp_cache_delete( sprintf( Followers::CACHE_KEY_INBOXES, $user_id ), 'activitypub' );
201 }
202 }
203
204 /**
205 * Unschedule Hooks after updating to 2.0.0
206 *
207 * @return void
208 */
209 private static function migrate_from_2_0_0() {
210 wp_clear_scheduled_hook( 'activitypub_send_post_activity' );
211 wp_clear_scheduled_hook( 'activitypub_send_update_activity' );
212 wp_clear_scheduled_hook( 'activitypub_send_delete_activity' );
213
214 wp_unschedule_hook( 'activitypub_send_post_activity' );
215 wp_unschedule_hook( 'activitypub_send_update_activity' );
216 wp_unschedule_hook( 'activitypub_send_delete_activity' );
217 }
218 }
219