From 22946ec7798d39c5975b987db7fd9ecfe0cafe12 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 2 May 2023 09:27:35 +0200 Subject: [PATCH] change migration script to match plugin version /cc @akirk --- activitypub.php | 35 +++++++++++++++++++++ includes/class-admin.php | 2 -- includes/class-migration.php | 57 ++++++++++++++++++++++++++--------- includes/model/class-post.php | 42 -------------------------- 4 files changed, 78 insertions(+), 58 deletions(-) diff --git a/activitypub.php b/activitypub.php index e754e93..6bf1783 100644 --- a/activitypub.php +++ b/activitypub.php @@ -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']; +} diff --git a/includes/class-admin.php b/includes/class-admin.php index 8e19c37..e7e2dc4 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -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': diff --git a/includes/class-migration.php b/includes/class-migration.php index f6e6286..21e315f 100644 --- a/includes/class-migration.php +++ b/includes/class-migration.php @@ -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 ); + } + } } diff --git a/includes/model/class-post.php b/includes/model/class-post.php index a69e139..db56a45 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -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; } }