change migration script to match plugin version

/cc @akirk
This commit is contained in:
Matthias Pfefferle 2023-05-02 09:27:35 +02:00
parent be73f99b59
commit 22946ec779
4 changed files with 78 additions and 58 deletions

View file

@ -150,3 +150,38 @@ function enable_buddypress_features() {
Integration\Buddypress::init();
}
add_action( 'bp_include', '\Activitypub\enable_buddypress_features' );
/**
* `get_plugin_data` wrapper
*
* @return array The plugin metadata array
*/
function get_plugin_meta( $default_headers = array() ) {
if ( ! $default_headers ) {
$default_headers = array(
'Name' => 'Plugin Name',
'PluginURI' => 'Plugin URI',
'Version' => 'Version',
'Description' => 'Description',
'Author' => 'Author',
'AuthorURI' => 'Author URI',
'TextDomain' => 'Text Domain',
'DomainPath' => 'Domain Path',
'Network' => 'Network',
'RequiresWP' => 'Requires at least',
'RequiresPHP' => 'Requires PHP',
'UpdateURI' => 'Update URI',
);
}
return \get_file_data( __FILE__, $default_headers, 'plugin' );
}
/**
* Plugin Version Number used for caching.
*/
function get_plugin_version() {
$meta = get_plugin_meta( array( 'Version' => 'Version' ) );
return $meta['Version'];
}

View file

@ -54,8 +54,6 @@ class Admin {
switch ( $tab ) {
case 'settings':
Post::upgrade_post_content_template();
\load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/settings.php' );
break;
case 'welcome':

View file

@ -2,19 +2,12 @@
namespace Activitypub;
class Migration {
/**
* Which internal datastructure version we are running on.
*
* @var int
*/
private static $target_version = '1.0.0';
public static function init() {
\add_action( 'activitypub_schedule_migration', array( self::class, 'maybe_migrate' ) );
}
public static function get_target_version() {
return self::$target_version;
return plugin_version();
}
public static function get_version() {
@ -45,20 +38,19 @@ class Migration {
$version_from_db = self::get_version();
if ( version_compare( $version_from_db, '1.0.0', '<' ) ) {
self::migrate_to_1_0_0();
self::migrate_from_0_17();
self::migrate_from_0_16();
}
update_option( 'activitypub_db_version', self::$target_version );
update_option( 'activitypub_db_version', self::get_target_version() );
}
/**
* The Migration for Plugin Version 1.0.0 and DB Version 1.0.0
*
* @since 5.0.0
* Updates the DB-schema of the followers-list
*
* @return void
*/
public static function migrate_to_1_0_0() {
public static function migrate_from_0_17() {
foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) {
$followers = get_user_meta( $user_id, 'activitypub_followers', true );
@ -69,4 +61,41 @@ class Migration {
}
}
}
/**
* Updates the custom template to use shortcodes instead of the deprecated templates.
*
* @return void
*/
public static function migrate_from_0_16() {
// 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 );
}
}
}

View file

@ -511,48 +511,6 @@ class Post {
return "[ap_content]\n\n[ap_hashtags]\n\n[ap_permalink type=\"html\"]";
}
// Upgrade from old template codes to shortcodes.
$content = self::upgrade_post_content_template();
return $content;
}
/**
* Updates the custom template to use shortcodes instead of the deprecated templates.
*
* @return string the updated template content
*/
public static function upgrade_post_content_template() {
// 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 );
}
return $content;
}
}