PluginProbe
ActivityPub / 1.0.5
ActivityPub v1.0.5
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 1.0.5, at includes/class-migration.php

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