Remove migrate code

This commit is contained in:
Django Doucet 2023-01-29 20:22:48 -07:00
parent a159d70658
commit c653575594
4 changed files with 0 additions and 411 deletions

View file

@ -15,9 +15,7 @@ class Admin {
\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( 'wp_ajax_migrate_post', array( '\Activitypub\Admin', 'migrate_post_action' ) );
\add_filter( 'comment_row_actions', array( '\Activitypub\Admin', 'reply_comments_actions' ), 10, 2 );
\add_filter( 'views_tools_page_activitypub_tools', array( '\Activitypub\Table\Migrate_List', 'get_activitypub_tools_views' ), 10 );
\add_action( 'admin_enqueue_scripts', array( '\Activitypub\Admin', 'enqueue_scripts' ) );
}
@ -39,7 +37,6 @@ class Admin {
\add_action( 'load-' . $followers_list_page, array( '\Activitypub\Admin', 'add_followers_list_help_tab' ) );
\add_management_page( \__( 'ActivityPub Management', 'activitypub' ), \__( 'ActivityPub Tools', 'activitypub' ), 'manage_options', 'activitypub_tools', array( '\Activitypub\Admin', 'migrate_tools_page' ) );
}
/**
@ -78,13 +75,6 @@ class Admin {
\load_template( \dirname( __FILE__ ) . '/../templates/followers-list.php' );
}
/**
* Load ActivityPub Tools page
*/
public static function migrate_tools_page() {
\load_template( \dirname( __FILE__ ) . '/../templates/tools-page.php' );
}
/**
* Register ActivityPub settings
*/
@ -157,27 +147,6 @@ 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' );
if ( empty( $activitypub_db_version ) || \version_compare( $plugin_data['Version'], $activitypub_db_version, '>' ) ) {
// Check for specific migrations
if ( version_compare( '0.13.4', $activitypub_db_version, '>' ) ) {
\Activitypub\Tools\Posts::mark_posts_to_migrate();
}
}
\update_option( 'activitypub_version', $plugin_data['Version'] );
//\delete_option( 'activitypub_version' );
}
public static function add_settings_help_tab() {
require_once \dirname( __FILE__ ) . '/help.php';
}
@ -193,20 +162,6 @@ class Admin {
\Activitypub\get_identifier_settings( $user->ID );
}
/**
* Migrate post (Ajax)
*/
public static function migrate_post_action() {
if ( wp_verify_nonce( $_POST['nonce'], 'activitypub_migrate_actions' ) ) {
\Activitypub\Tools\Posts::migrate_post( rawurldecode( $_POST['post_url'] ), absint( $_POST['post_author'] ) );
\delete_post_meta( \url_to_postid( $_POST['post_url'] ), '_activitypub_permalink_compat' );
} else {
$error = new WP_Error( 'nonce_failure', __( 'Unauthorized', 'activitypub' ) );
wp_send_json_error( $error );
}
wp_die();
}
/**
* hook comment_row_actions
* Reply to ActivityPub Comments from the edit-comments.php screen

View file

@ -1,100 +0,0 @@
<?php
namespace Activitypub\Tools;
/**
* ActivityPub Migrate DB-Class
*
* @author Django Doucet
*/
class Posts {
/**
* Marks ActivityPub Posts for backwards compatibility
*/
public static function mark_posts_to_migrate() {
$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_to_migrate() {
$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',
);
return \get_posts( $args );
}
public static function get_posts_with_activitypub_comments() {
$args = array(
'comment_type' => 'activitypub',
'status' => 'all',
);
$activitypub_comments = \get_comments( $args );
$activitypub_comments_posts = array();
foreach ( $activitypub_comments as $comment ) {
$meta = \get_post_meta( $comment->comment_post_ID, '_activitypub_permalink_compat', true );
if ( ! $meta ) {
continue;
}
$activitypub_comments_posts[] = $comment->comment_post_ID;
}
return ( empty( \array_unique( $activitypub_comments_posts ) ) ? $activitypub_comments_posts : \get_posts( \array_unique( $activitypub_comments_posts ) ) );
}
public static function count_posts_to_migrate() {
$posts = self::get_posts_to_migrate();
return \count( $posts );
}
public static function count_posts_with_comments_to_migrate() {
$posts = self::get_posts_with_activitypub_comments();
return \count( $posts );
}
/**
* migrate_post
* Federate Delete
* Federate Announce
*
* @param str post_url (_activitypub_permalink_compat)
* @param str post_author (user_id)
*/
public static function migrate_post( $post_url, $post_author ) {
self::delete_url( $post_url, $post_author );
self::announce_url( $post_url, $post_author );
}
/**
* announce_url
* send Announce (obj)
*
* @param str post_url (post_id)
* @param int user_id
*/
public static function announce_url( $post_url, $user_id ) {
\wp_schedule_single_event( \time(), 'activitypub_send_announce_activity', array( $post_url, $user_id ) );
}
/**
* delete_url
* Send a Delete activity to the Fediverse
*
* @param str post_url (_activitypub_permalink_compat)
* @param str post_author (user_id)
*/
public static function delete_url( $post_url, $post_author ) {
\wp_schedule_single_event( \time(), 'activitypub_send_delete_url_activity', array( $post_url, $post_author ) );
}
}

View file

@ -1,207 +0,0 @@
<?php
namespace Activitypub\Table;
if ( ! \class_exists( '\WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class Migrate_List extends \WP_List_Table {
public function get_columns() {
return array(
'cb' => '<input type="checkbox" />',
'post_author' => \__( 'Post Author (user_id)', 'activitypub' ),
'title' => \__( 'Post', 'activitypub' ),
'comments' => \__( 'Comments', 'activitypub' ),
'date' => \__( 'Publication Date', 'activitypub' ),
'migrate' => \__( 'Migrate', 'activitypub' ),
);
}
public function get_sortable_columns() {
return array();
}
public function get_bulk_actions() {
$actions = array(
'delete' => 'Remove backwards compatibility',
);
return $actions;
}
public static function get_activitypub_tools_views() {
$posts_count = \Activitypub\Tools\Posts::count_posts_to_migrate();
$comments_posts_count = \Activitypub\Tools\Posts::count_posts_with_comments_to_migrate();
$activitypub_tools_page = 'tools.php?page=activitypub_tools';
$view_slugs = array(
array( 'all', null, __( 'All', 'activitypub' ), $posts_count ),
array( 'comments', 'activitypub', __( 'Posts with Comments', 'activitypub' ), $comments_posts_count ),
);
$post_status_var = get_query_var( 'comments' );
$view_count = count( $view_slugs );
for ( $x = 0; $x < $view_count; $x++ ) {
$class = ( $post_status_var === $view_slugs[ $x ][1] ) ? ' class="current"' : '';
$post_status_temp = $view_slugs[ $x ][1];
if ( ! empty( $post_status_temp ) ) {
$post_status_temp = '&comments=' . $view_slugs[ $x ][1];
}
$views[ $view_slugs[ $x ][0] ] = sprintf(
'<a href="' . $activitypub_tools_page . $post_status_temp . '"' . $class . ' >' . $view_slugs[ $x ][2] . ' <span class="count">(%s)</span></a>',
$view_slugs[ $x ][3]
);
}
return $views;
}
public function prepare_items() {
$columns = $this->get_columns();
$hidden = array( 'post_author', 'migrate' );
$sortable = $this->get_sortable_columns();
$this->_column_headers = array( $columns, $hidden, $sortable );
$items = array();
$this->process_action();
if ( 'activitypub_tools' === $_REQUEST['page'] ) {
if ( isset( $_REQUEST['comments'] ) && 'activitypub' === $_REQUEST['comments'] ) {
foreach ( \Activitypub\Tools\Posts::get_posts_with_activitypub_comments() as $ap_post ) {
$items[] = array(
'post_author' => $ap_post->post_author,
'title' => \sprintf(
'<a href="%1s">%2s</a>',
\get_permalink( $ap_post->ID ),
$ap_post->post_title
),
'comments' => $ap_post->comment_count,
'date' => $ap_post->post_date,
'migrate' => \get_post_meta( $ap_post->ID, '_activitypub_permalink_compat', true ),
);
}
} else {
foreach ( \Activitypub\Tools\Posts::get_posts_to_migrate() as $post ) {
$items[] = array(
'post_author' => $post->post_author,
'title' => \sprintf(
'<a href="%1s">%2s</a>',
\get_permalink( $post->ID ),
$post->post_title
),
'comments' => $post->comment_count,
'date' => $post->post_date,
'migrate' => \get_post_meta( $post->ID, '_activitypub_permalink_compat', true ),
);
}
}
}
// pagination
$per_page = $this->get_items_per_page( 'elements_per_page', 10 );
$current_page = $this->get_pagenum();
$total_items = count( $items );
$table_data = array_slice( $items, ( ( $current_page - 1 ) * $per_page ), $per_page );
$this->set_pagination_args(
array(
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil( $total_items / $per_page ),
)
);
$this->items = $table_data;
// actions
if ( isset( $_REQUEST['_wpnonce'] ) ) {
$nonce = \esc_attr( $_REQUEST['_wpnonce'] );
}
// delete
if ( isset( $_REQUEST['action'] ) && 'activitypub_tools' === $_REQUEST['page'] && 'delete_notice' === $_REQUEST['action'] ) {
if ( wp_verify_nonce( $nonce, 'activitypub_migrate_actions' ) ) {
\Activitypub\Tools\Posts::delete_url( rawurldecode( $_REQUEST['post_url'] ), absint( $_REQUEST['post_author'] ) );
\delete_post_meta( \url_to_postid( $_REQUEST['post_url'] ), '_activitypub_permalink_compat' );
}
}
// delete and announce
if ( isset( $_REQUEST['action'] ) && 'activitypub_tools' === $_REQUEST['page'] && 'delete' === $_REQUEST['action'] ) {
if ( wp_verify_nonce( $nonce, 'activitypub_migrate_actions' ) ) {
\Activitypub\Tools\Posts::migrate_post( rawurldecode( $_REQUEST['post_url'] ), absint( $_REQUEST['post_author'] ) );
\delete_post_meta( \url_to_postid( $_REQUEST['post_url'] ), '_activitypub_permalink_compat' );
}
}
}
public function single_row( $item ) {
$inline_styles = ( $item['comments'] > 0 ) ? 'warning' : ''; ?>
<tr class="<?php echo esc_attr( $inline_styles ); ?>"><?php $this->single_row_columns( $item ); ?></tr>
<?php
}
/**
* Render the bulk edit checkbox
*
* @param array $item
*
* @return string
*/
public function column_cb( $item ) {
return sprintf(
'<input type="checkbox" name="selected[]" value="%s" />',
$item['migrate']
);
}
/**
* Render a column when no column specific method exists.
*
* @param array $item
* @param string $column_name
*
* @return mixed
*/
public function column_default( $item, $column_name ) {
switch ( $column_name ) {
case 'cb':
case 'post_author':
case 'title':
case 'comments':
case 'date':
case 'migrate':
return $item[ $column_name ];
default:
return print_r( $item, true );
}
}
public function column_title( $item ) {
$migrate_action_nonce = wp_create_nonce( 'activitypub_migrate_actions' );
$actions = array(
'delete' => sprintf(
'<a href="?page=%s&action=%s&post_author=%s&post_url=%s&_wpnonce=%s" class="%s" title="%s" data-post_author="%s" data-post_url="%s" data-nonce="%s">%s</a>',
esc_attr( $_REQUEST['page'] ),
'delete', // using this id for style reasons
$item['post_author'],
\rawurlencode( $item['migrate'] ),
$migrate_action_nonce,
'delete_annouce aria-button-ui-if-js',
__( 'Delete the federated post, and re-share the post with a new id', 'activitypub' ),
$item['post_author'],
\rawurlencode( $item['migrate'] ),
$migrate_action_nonce,
__( 'Migrate post', 'activitypub' )
),
'delete_notice' => sprintf(
'<a href="?page=%s&action=%s&post_author=%s&post_url=%s&_wpnonce=%s" class="%s" title="%s" data-post_author="%s" data-post_url="%s" data-nonce="%s">%s</a>',
esc_attr( $_REQUEST['page'] ),
'delete_notice',
$item['post_author'],
\rawurlencode( $item['migrate'] ),
$migrate_action_nonce,
'delete aria-button-ui-if-js',
__( 'Delete this notice and backwards compatibility', 'activitypub' ),
$item['post_author'],
\rawurlencode( $item['migrate'] ),
$migrate_action_nonce,
__( 'Remove notice', 'activitypub' )
),
);
return sprintf( '%1$s %2$s', $item['title'], $this->row_actions( $actions, true ) );
}
}

View file

@ -1,59 +0,0 @@
<?php
$action_nonce = wp_create_nonce( 'activitypub_action' );
if ( isset( $_REQUEST['post_url'] ) && $_REQUEST['page'] === "activitypub_tools" ) {
$post_url = $_REQUEST['post_url'];
$user_id = get_current_user_id();
if ( isset( $_REQUEST['submit_delete'] ) && wp_verify_nonce( $action_nonce, 'activitypub_action' ) ) {
\Activitypub\Migrate\Posts::delete_url( rawurldecode( $_REQUEST['post_url'] ), $user_id );
}
}
?>
<style>
#the-list tr.warning {
background-color: #fcf9e8;
box-shadow: inset 0 -1px 0 rgba(0,0,0,.1);
}
#the-list tr.warning th.check-column {
border-left: 4px solid #d63638;
}
</style>
<div class="wrap">
<h1><?php \esc_html_e( 'Manage ActivityPub posts (Fediverse)', 'activitypub' ); ?></h1>
<?php if ( \Activitypub\Tools\Posts::count_posts_to_migrate() > 0 ): ?>
<div class="notice notice-warning">
<p><?php \printf(
\__( 'The following table lists ActivityPub posts which have been marked as backwards compatible, and ready for migration. <br>
Migration here means updating the Activity ID by federating a Delete activity removing the original post from the fediverse, and then Sharing the new post ID with an Announce activity. <br>
Posts with comments should be treated with care, existing interactions in the fediverse will be lost.', 'activitypub' ),
\esc_attr( \Activitypub\Tools\Posts::count_posts_to_migrate() )
); ?></p>
</div>
<p><?php \printf(
\__( 'You currently have %s posts marked for review.', 'activitypub' ),
\esc_attr( \Activitypub\Tools\Posts::count_posts_to_migrate() )
); ?></p>
<?php $token_table = new \Activitypub\Table\Migrate_List(); ?>
<form method="POST" id="table">
<?php
$token_table->views();
$token_table->prepare_items();
$token_table->display();
?>
</form>
<?php endif; ?>
<hr class="separator">
<form method="POST" id="delete">
<div>
<p><?php _e( 'Manually Delete a URL', 'activitypub' ); ?></p>
<input type="hidden" name="page" value="activitypub_tools">
<input type="hidden" name="_wpnonce" value="<?php echo $action_nonce ?>">
<input type="text" class="post_url" id="url" name="post_url" size="40" value="">
<input class="button button-primary" type="submit" value="<?php _e( 'Delete Federated URL', 'activitypub' ); ?>" name="submit_delete" id="submit_delete"><br></p>
</div>
</form>
</div>