wordpress-activitypub/includes/table/migrate-list.php

156 lines
5 KiB
PHP
Raw Normal View History

2022-10-02 06:49:48 +02:00
<?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 {
2022-10-07 23:41:46 +02:00
2022-10-02 06:49:48 +02:00
public function get_columns() {
return array(
2022-10-07 23:41:46 +02:00
'cb' => '<input type="checkbox" />',
2022-10-02 06:49:48 +02:00
'post_author' => \__( 'Post Author (user_id)', 'activitypub' ),
'title' => \__( 'Post', 'activitypub' ),
'comments' => \__( 'Comments', 'activitypub' ),
2022-10-07 23:41:46 +02:00
'date' => \__( 'Publication Date', 'activitypub' ),
2022-10-02 06:49:48 +02:00
'migrate' => \__( 'Migrate', 'activitypub' ),
);
}
public function get_sortable_columns() {
2022-10-02 07:17:47 +02:00
return array();
2022-10-02 06:49:48 +02:00
}
public function prepare_items() {
$columns = $this->get_columns();
$hidden = array( 'post_author', 'migrate' );
$sortable = $this->get_sortable_columns();
$this->process_action();
$this->_column_headers = array( $columns, $hidden, $sortable );
2022-10-05 21:58:12 +02:00
foreach ( \Activitypub\Tools\Posts::get_posts_to_migrate() as $post ) {
2022-10-02 06:49:48 +02:00
$this->items[] = array(
'post_author' => $post->post_author,
2022-10-02 07:17:47 +02:00
'title' => \sprintf(
'<a href="%1s">%2s</a>',
\get_permalink( $post->ID ),
$post->post_title
2022-10-02 06:49:48 +02:00
),
'comments' => $post->comment_count,
2022-10-07 23:41:46 +02:00
'date' => $post->post_date,
2022-10-02 06:49:48 +02:00
'migrate' => \get_post_meta( $post->ID, '_activitypub_permalink_compat', true ),
);
}
2022-10-02 07:17:47 +02:00
// pagination
$per_page = $this->get_items_per_page( 'elements_per_page', 10 );
$current_page = $this->get_pagenum();
$total_items = count( $this->items );
$table_data = array_slice( $this->items, ( ( $current_page - 1 ) * $per_page ), $per_page );
$this->set_pagination_args(
2022-10-02 06:49:48 +02:00
array(
2022-10-02 07:17:47 +02:00
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil( $total_items / $per_page ),
2022-10-02 06:49:48 +02:00
)
);
2022-10-02 07:17:47 +02:00
// actions
2022-10-02 06:49:48 +02:00
if ( isset( $_REQUEST['_wpnonce'] ) ) {
$nonce = \esc_attr( $_REQUEST['_wpnonce'] );
}
// delete
2022-10-07 23:41:46 +02:00
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'] ) );
2022-10-02 06:49:48 +02:00
\delete_post_meta( \url_to_postid( $_REQUEST['post_url'] ), '_activitypub_permalink_compat' );
}
}
// delete and announce
2022-10-07 23:41:46 +02:00
if ( isset( $_REQUEST['action'] ) && 'activitypub_tools' === $_REQUEST['page'] && 'delete' === $_REQUEST['action'] ) {
if ( wp_verify_nonce( $nonce, 'activitypub_migrate_actions' ) ) {
2022-10-05 21:58:12 +02:00
\Activitypub\Tools\Posts::migrate_post( rawurldecode( $_REQUEST['post_url'] ), absint( $_REQUEST['post_author'] ) );
2022-10-02 06:49:48 +02:00
\delete_post_meta( \url_to_postid( $_REQUEST['post_url'] ), '_activitypub_permalink_compat' );
}
}
}
2022-10-07 23:41:46 +02:00
public function single_row( $item ) {
$inline_styles = ( $item['comments'] > 0 ) ? 'warning' : ''; ?>
<tr class="<?php echo $inline_styles; ?>"><?php $this->single_row_columns( $item ); ?></tr><?php
}
/**
* Render the bulk edit checkbox
*
* @param array $item
*
* @return string
*/
function column_cb( $item ) {
return sprintf(
'<input type="checkbox" name="selected[]" value="%s" />', $item['migrate']
);
}
2022-10-02 06:49:48 +02:00
/**
* 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 ) {
2022-10-07 23:41:46 +02:00
case 'cb':
2022-10-02 06:49:48 +02:00
case 'post_author':
case 'title':
case 'comments':
2022-10-07 23:41:46 +02:00
case 'date':
2022-10-02 06:49:48 +02:00
case 'migrate':
return $item[ $column_name ];
default:
2022-10-02 07:17:47 +02:00
return print_r( $item, true );
2022-10-02 06:49:48 +02:00
}
2022-10-02 07:17:47 +02:00
}
2022-10-02 06:49:48 +02:00
2022-10-02 07:17:47 +02:00
public function column_title( $item ) {
2022-10-07 23:41:46 +02:00
$migrate_action_nonce = wp_create_nonce( 'activitypub_migrate_actions' );
2022-10-02 06:49:48 +02:00
$actions = array(
2022-10-07 23:41:46 +02:00
'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>',
2022-10-02 07:17:47 +02:00
esc_attr( $_REQUEST['page'] ),
2022-10-07 23:41:46 +02:00
'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' ),
2022-10-02 07:17:47 +02:00
$item['post_author'],
\rawurlencode( $item['migrate'] ),
2022-10-07 23:41:46 +02:00
$migrate_action_nonce,
__( 'Migrate post', 'activitypub' )
2022-10-02 07:17:47 +02:00
),
2022-10-07 23:41:46 +02:00
'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>',
2022-10-02 07:17:47 +02:00
esc_attr( $_REQUEST['page'] ),
2022-10-07 23:41:46 +02:00
'delete_notice',
$item['post_author'],
\rawurlencode( $item['migrate'] ),
$migrate_action_nonce,
'delete aria-button-ui-if-js',
__( 'Delete this notice and backwards compatibility', 'activitypub' ),
2022-10-02 07:17:47 +02:00
$item['post_author'],
\rawurlencode( $item['migrate'] ),
2022-10-07 23:41:46 +02:00
$migrate_action_nonce,
__( 'Remove notice', 'activitypub' )
2022-10-02 07:17:47 +02:00
),
);
return sprintf( '%1$s %2$s', $item['title'], $this->row_actions( $actions, true ) );
2022-10-02 06:49:48 +02:00
}
}