2023-04-27 14:34:54 +02:00
|
|
|
<?php
|
|
|
|
namespace Activitypub;
|
|
|
|
|
2023-07-11 14:34:11 +02:00
|
|
|
use Activitypub\Model\Blog_User;
|
2023-05-11 09:45:01 +02:00
|
|
|
use Activitypub\Collection\Followers;
|
2023-05-03 14:50:36 +02:00
|
|
|
|
2023-05-10 14:18:56 +02:00
|
|
|
/**
|
|
|
|
* ActivityPub Migration Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*/
|
2023-04-27 14:34:54 +02:00
|
|
|
class Migration {
|
2023-05-10 14:18:56 +02:00
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks
|
|
|
|
*/
|
2023-04-28 09:54:09 +02:00
|
|
|
public static function init() {
|
|
|
|
\add_action( 'activitypub_schedule_migration', array( self::class, 'maybe_migrate' ) );
|
|
|
|
}
|
|
|
|
|
2023-07-11 14:34:11 +02:00
|
|
|
/**
|
|
|
|
* Get the target version.
|
|
|
|
*
|
|
|
|
* This is the version that the database structure will be updated to.
|
|
|
|
* It is the same as the plugin version.
|
|
|
|
*
|
|
|
|
* @return string The target version.
|
|
|
|
*/
|
2023-04-27 14:34:54 +02:00
|
|
|
public static function get_target_version() {
|
2023-05-02 09:29:29 +02:00
|
|
|
return get_plugin_version();
|
2023-04-27 14:34:54 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 14:34:11 +02:00
|
|
|
/**
|
|
|
|
* The current version of the database structure.
|
|
|
|
*
|
|
|
|
* @return string The current version.
|
|
|
|
*/
|
2023-04-27 14:34:54 +02:00
|
|
|
public static function get_version() {
|
2023-06-23 15:41:19 +02:00
|
|
|
return get_option( 'activitypub_db_version', 0 );
|
2023-04-27 14:34:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the database structure is up to date.
|
|
|
|
*
|
2023-07-11 14:34:11 +02:00
|
|
|
* @return bool True if the database structure is up to date, false otherwise.
|
2023-04-27 14:34:54 +02:00
|
|
|
*/
|
|
|
|
public static function is_latest_version() {
|
|
|
|
return (bool) version_compare(
|
|
|
|
self::get_version(),
|
|
|
|
self::get_target_version(),
|
|
|
|
'=='
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the database structure if necessary.
|
|
|
|
*/
|
|
|
|
public static function maybe_migrate() {
|
|
|
|
if ( self::is_latest_version() ) {
|
2023-04-27 14:45:38 +02:00
|
|
|
return;
|
2023-04-27 14:34:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$version_from_db = self::get_version();
|
|
|
|
|
2023-05-02 09:37:11 +02:00
|
|
|
if ( version_compare( $version_from_db, '0.16.0', '<' ) ) {
|
2023-05-02 09:27:35 +02:00
|
|
|
self::migrate_from_0_16();
|
2023-04-27 14:45:38 +02:00
|
|
|
}
|
2023-05-02 09:37:11 +02:00
|
|
|
if ( version_compare( $version_from_db, '0.17.0', '<' ) ) {
|
|
|
|
self::migrate_from_0_17();
|
|
|
|
}
|
2023-04-27 14:34:54 +02:00
|
|
|
|
2023-05-02 09:27:35 +02:00
|
|
|
update_option( 'activitypub_db_version', self::get_target_version() );
|
2023-04-27 14:34:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-02 09:27:35 +02:00
|
|
|
* Updates the DB-schema of the followers-list
|
2023-04-27 14:34:54 +02:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2023-05-02 14:35:53 +02:00
|
|
|
private static function migrate_from_0_17() {
|
2023-07-11 14:34:11 +02:00
|
|
|
// migrate followers
|
2023-04-27 14:34:54 +02:00
|
|
|
foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) {
|
2023-04-28 18:13:16 +02:00
|
|
|
$followers = get_user_meta( $user_id, 'activitypub_followers', true );
|
2023-04-27 14:34:54 +02:00
|
|
|
|
2023-04-28 18:13:59 +02:00
|
|
|
if ( $followers ) {
|
2023-05-10 14:18:56 +02:00
|
|
|
foreach ( $followers as $actor ) {
|
2023-07-05 15:31:45 +02:00
|
|
|
Followers::add_follower( $user_id, $actor );
|
2023-04-27 14:34:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-02 09:27:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the custom template to use shortcodes instead of the deprecated templates.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2023-05-02 14:35:53 +02:00
|
|
|
private static function migrate_from_0_16() {
|
2023-05-02 09:27:35 +02:00
|
|
|
// Get the custom template.
|
|
|
|
$old_content = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
|
|
|
|
|
|
|
|
// If the old content exists but is a blank string, we're going to need a flag to updated it even
|
|
|
|
// after setting it to the default contents.
|
|
|
|
$need_update = false;
|
|
|
|
|
|
|
|
// If the old contents is blank, use the defaults.
|
|
|
|
if ( '' === $old_content ) {
|
|
|
|
$old_content = ACTIVITYPUB_CUSTOM_POST_CONTENT;
|
|
|
|
$need_update = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the new content to be the old content.
|
|
|
|
$content = $old_content;
|
|
|
|
|
|
|
|
// Convert old templates to shortcodes.
|
|
|
|
$content = \str_replace( '%title%', '[ap_title]', $content );
|
|
|
|
$content = \str_replace( '%excerpt%', '[ap_excerpt]', $content );
|
|
|
|
$content = \str_replace( '%content%', '[ap_content]', $content );
|
|
|
|
$content = \str_replace( '%permalink%', '[ap_permalink type="html"]', $content );
|
|
|
|
$content = \str_replace( '%shortlink%', '[ap_shortlink type="html"]', $content );
|
|
|
|
$content = \str_replace( '%hashtags%', '[ap_hashtags]', $content );
|
|
|
|
$content = \str_replace( '%tags%', '[ap_hashtags]', $content );
|
|
|
|
|
|
|
|
// Store the new template if required.
|
|
|
|
if ( $content !== $old_content || $need_update ) {
|
|
|
|
\update_option( 'activitypub_custom_post_content', $content );
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 14:34:54 +02:00
|
|
|
}
|