Migrate / Update script

This commit is contained in:
Django Doucet 2022-09-29 17:46:42 -06:00
parent e5080f8693
commit 5d8e3a9ba9
4 changed files with 144 additions and 2 deletions

View file

@ -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', '<strong><a><p><ul><ol><li><code><blockquote><pre><img>' );
\defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "<p><strong>%title%</strong></p>\n\n%content%\n\n<p>%hashtags%</p>\n\n<p>%shortlink%</p>" );
\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' );
/**

View file

@ -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(

View file

@ -0,0 +1,97 @@
<?php
namespace Activitypub\Migrate;
/**
* ActivityPub Migrate DB-Class
*
* @author Django Doucet
*/
class Posts {
/**
* Updates ActivityPub Posts for backwards compatibility
*/
public static function backcompat_posts() {
$post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) );
$args = array(
'numberposts' => -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?;
// }
}

View file

@ -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,