PluginProbe
ActivityPub / 2.0.0
ActivityPub v2.0.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.0.0, at includes/class-migration.php

201 lines 4.7 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
121 update_option( 'activitypub_db_version', self::get_target_version() );
122
123 self::unlock();
124 }
125
126 /**
127 * Updates the DB-schema of the followers-list
128 *
129 * @return void
130 */
131 private static function migrate_from_0_17() {
132 // migrate followers
133 foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) {
134 $followers = get_user_meta( $user_id, 'activitypub_followers', true );
135
136 if ( $followers ) {
137 foreach ( $followers as $actor ) {
138 Followers::add_follower( $user_id, $actor );
139 }
140 }
141 }
142
143 Activitypub::flush_rewrite_rules();
144 }
145
146 /**
147 * Updates the custom template to use shortcodes instead of the deprecated templates.
148 *
149 * @return void
150 */
151 private static function migrate_from_0_16() {
152 // Get the custom template.
153 $old_content = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
154
155 // If the old content exists but is a blank string, we're going to need a flag to updated it even
156 // after setting it to the default contents.
157 $need_update = false;
158
159 // If the old contents is blank, use the defaults.
160 if ( '' === $old_content ) {
161 $old_content = ACTIVITYPUB_CUSTOM_POST_CONTENT;
162 $need_update = true;
163 }
164
165 // Set the new content to be the old content.
166 $content = $old_content;
167
168 // Convert old templates to shortcodes.
169 $content = \str_replace( '%title%', '[ap_title]', $content );
170 $content = \str_replace( '%excerpt%', '[ap_excerpt]', $content );
171 $content = \str_replace( '%content%', '[ap_content]', $content );
172 $content = \str_replace( '%permalink%', '[ap_permalink type="html"]', $content );
173 $content = \str_replace( '%shortlink%', '[ap_shortlink type="html"]', $content );
174 $content = \str_replace( '%hashtags%', '[ap_hashtags]', $content );
175 $content = \str_replace( '%tags%', '[ap_hashtags]', $content );
176
177 // Store the new template if required.
178 if ( $content !== $old_content || $need_update ) {
179 \update_option( 'activitypub_custom_post_content', $content );
180 }
181 }
182
183 /**
184 * Clear the cache after updating to 1.3.0
185 *
186 * @return void
187 */
188 private static function migrate_from_1_2_0() {
189 $user_ids = get_users(
190 array(
191 'fields' => 'ID',
192 'capability__in' => array( 'publish_posts' ),
193 )
194 );
195
196 foreach ( $user_ids as $user_id ) {
197 wp_cache_delete( sprintf( Followers::CACHE_KEY_INBOXES, $user_id ), 'activitypub' );
198 }
199 }
200 }
201