From 5d8e3a9ba9cd4462c05fb0eb3031dd37c71b03ae Mon Sep 17 00:00:00 2001 From: Django Doucet Date: Thu, 29 Sep 2022 17:46:42 -0600 Subject: [PATCH] Migrate / Update script --- activitypub.php | 16 ++++++ includes/class-admin.php | 28 ++++++++++ includes/class-migrate.php | 97 +++++++++++++++++++++++++++++++++++ includes/model/class-post.php | 5 +- 4 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 includes/class-migrate.php diff --git a/activitypub.php b/activitypub.php index f30ef42..9c6b525 100644 --- a/activitypub.php +++ b/activitypub.php @@ -22,11 +22,14 @@ function init() { \defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || \define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(?:(?<=\s)|^)#(\w*[A-Za-z_]+\w*)' ); \defined( 'ACTIVITYPUB_ALLOWED_HTML' ) || \define( 'ACTIVITYPUB_ALLOWED_HTML', '

    1. ' );
       	\defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "

      %title%

      \n\n%content%\n\n

      %hashtags%

      \n\n

      %shortlink%

      " ); + \defined( 'ACTIVITYPUB_PLUGIN' ) || \define( 'ACTIVITYPUB_PLUGIN', __FILE__ ); + require_once \dirname( __FILE__ ) . '/includes/table/followers-list.php'; require_once \dirname( __FILE__ ) . '/includes/class-signature.php'; require_once \dirname( __FILE__ ) . '/includes/peer/class-followers.php'; require_once \dirname( __FILE__ ) . '/includes/functions.php'; + require_once \dirname( __FILE__ ) . '/includes/class-migrate.php'; require_once \dirname( __FILE__ ) . '/includes/model/class-activity.php'; require_once \dirname( __FILE__ ) . '/includes/model/class-post.php'; @@ -123,6 +126,19 @@ function flush_rewrite_rules() { \flush_rewrite_rules(); } \register_activation_hook( __FILE__, '\Activitypub\flush_rewrite_rules' ); + +/** + * Activate plugin; + */ +function activate_plugin() { + if( ! function_exists('get_plugin_data') ){ + require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); + } + $plugin_data = \get_plugin_data( __FILE__ ); + \add_option( 'activitypub_version', $plugin_data['Version'] ); +} +\register_activation_hook( __FILE__, '\Activitypub\activate_plugin' ); + \register_deactivation_hook( __FILE__, '\flush_rewrite_rules' ); /** diff --git a/includes/class-admin.php b/includes/class-admin.php index 17476b3..7c3968b 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -13,6 +13,7 @@ class Admin { public static function init() { \add_action( 'admin_menu', array( '\Activitypub\Admin', 'admin_menu' ) ); \add_action( 'admin_init', array( '\Activitypub\Admin', 'register_settings' ) ); + \add_action( 'admin_init', array( '\Activitypub\Admin', 'version_check' ), 1 ); \add_action( 'show_user_profile', array( '\Activitypub\Admin', 'add_fediverse_profile' ) ); \add_action( 'admin_enqueue_scripts', array( '\Activitypub\Admin', 'scripts_reply_comments' ), 10, 2 ); \add_filter( 'comment_row_actions', array( '\Activitypub\Admin', 'reply_comments_actions' ), 10, 2 ); @@ -124,6 +125,33 @@ class Admin { ); } + /** + * Update ActivityPub plugin + */ + public static function version_check() { + if( ! function_exists('get_plugin_data') ){ + require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); + } + $plugin_data = \get_plugin_data( ACTIVITYPUB_PLUGIN ); + $activitypub_db_version = \get_option( 'activitypub_version' ); + + // Needs update + if ( empty( $activitypub_db_version ) || $plugin_data['Version'] > $activitypub_db_version ) { + // Check for specific migrations + + if ( '0.13.5' > $activitypub_db_version ) { + // This updates post_meta with _activitypub_permalink_compat. + // Posts that have this meta will be backwards compatible with their permalink based ActivityPub ID (URI) + + // This will create false positives, where the permalink has changed (slug, permalink structure) since federation, + // for those cases a delete_url will allow for federating a delete based on the federated object ID + + \Activitypub\Migrate\Posts::backcompat_posts(); + } + } + \update_option( 'activitypub_version', $plugin_data['Version'] ); + } + public static function add_settings_help_tab() { \get_current_screen()->add_help_tab( array( diff --git a/includes/class-migrate.php b/includes/class-migrate.php new file mode 100644 index 0000000..b3a3d76 --- /dev/null +++ b/includes/class-migrate.php @@ -0,0 +1,97 @@ + -1, + 'post_type' => $post_types, + 'order' => 'ASC', + ); + $posts_to_migrate = \get_posts( $args ); + foreach ( $posts_to_migrate as $post ) { + $permalink = \get_permalink( $post->ID ); + \update_post_meta( $post->ID, '_activitypub_permalink_compat', $permalink ); + } + } + + public static function get_posts( $args = null ) { + $post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ); + $args = array( + 'numberposts' => -1, + 'post_type' => $post_types, + 'meta_key' => '_activitypub_permalink_compat', + 'order' => 'ASC', + ); + $posts_to_migrate = \get_posts( $args ); + return $posts_to_migrate; + } + + // public static function get_posts_with_comments( $args = null ) { + // $page = ( \get_query_var( 'page' ) ? \get_query_var( 'page' ) : 1 ); + // if ( is_null( $args ) ) { + // $args = array( + // 'number' => '10', + // 'offset' => $page, + // 'type' => 'activitypub', + // 'order' => 'ASC', + // ); + // } + // $comments = \get_comments( $args ); + // $compat = array(); + // foreach ( $comments as $comment ) { + // $post_id = $comment->comment_post_ID; + // if ( \get_post_meta( $post_id, '_activitypub_permalink_compat', true ) ) { + // // if has url needs migration + // $compat[] = $post_id; + // } + // } + // $posts_migrate = \get_posts( \array_unique( $compat ) ); + // return $posts_migrate; + // } + + public static function count_posts() { + $posts = self::get_posts(); + return \count( $posts ); + } + + /** + * migrate_post + * first send Delete (obj) + * then send Announce (obj) + * + * @param int $post_id + * @param str $url + */ + // public static function migrate_post( $post_id, $url ) { + // self::delete_url( $post_id, $url ); + // $post = \get_post( $post_id ); + // $activitypub_post = new \Activitypub\Model\Post( $post ); + // \wp_schedule_single_event( \time() + 1, 'send_announce_activity', array( $activitypub_post ) ); + // //return \count( $posts ); + // } + + /** + * delete_url + * Send a Delete activity to the Fediverse + * + * @param str $activitypub_permalink_compat + */ + // public static function delete_url( $post_id, $url ) { + // $post = \get_post( $post_id ); + // $activitypub_post = new \Activitypub\Model\Post( $post ); + // \wp_schedule_single_event( \time(), 'activitypub_send_delete_activity', array( $activitypub_post, $url ) ); + // \wp_schedule_single_event( \time() + 1, 'delete_post_meta', array( $post_id, '_activitypub_permalink_compat' ) ); + // // return admin_notice?; + // } +} diff --git a/includes/model/class-post.php b/includes/model/class-post.php index c7780e8..9003e25 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -17,7 +17,7 @@ class Post { private $object_type; private $deleted; private $updated; - private $slug; + private $permalink; public function __construct( $post = null ) { $this->post = \get_post( $post ); @@ -32,6 +32,7 @@ class Post { $this->replies = $this->generate_replies(); $this->updated = $this->generate_updated(); $this->delete = $this->get_deleted(); + $this->permalink = $this->get_the_post_link(); } public function __call( $method, $params ) { @@ -56,7 +57,7 @@ class Post { 'attributedTo' => \get_author_posts_url( $post->post_author ), 'summary' => $this->summary, 'inReplyTo' => null, - 'url' => \get_permalink( $post->ID ), + 'url' => $this->permalink, 'content' => $this->content, 'contentMap' => array( \strstr( \get_locale(), '_', true ) => $this->content,