2023-04-21 14:56:22 +02:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Table;
|
|
|
|
|
|
|
|
use WP_List_Table;
|
2023-07-03 11:20:44 +02:00
|
|
|
use Activitypub\Collection\Users;
|
2023-04-21 14:56:22 +02:00
|
|
|
use Activitypub\Collection\Followers as FollowerCollection;
|
|
|
|
|
|
|
|
if ( ! \class_exists( '\WP_List_Table' ) ) {
|
|
|
|
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
class Followers extends WP_List_Table {
|
2023-05-24 16:32:00 +02:00
|
|
|
private $user_id;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if ( get_current_screen()->id === 'settings_page_activitypub' ) {
|
2023-07-03 11:20:44 +02:00
|
|
|
$this->user_id = Users::BLOG_USER_ID;
|
2023-05-24 16:32:00 +02:00
|
|
|
} else {
|
|
|
|
$this->user_id = \get_current_user_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct(
|
|
|
|
array(
|
|
|
|
'singular' => \__( 'Follower', 'activitypub' ),
|
|
|
|
'plural' => \__( 'Followers', 'activitypub' ),
|
|
|
|
'ajax' => false,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-21 14:56:22 +02:00
|
|
|
public function get_columns() {
|
|
|
|
return array(
|
2023-05-02 14:39:25 +02:00
|
|
|
'cb' => '<input type="checkbox" />',
|
|
|
|
'avatar' => \__( 'Avatar', 'activitypub' ),
|
|
|
|
'name' => \__( 'Name', 'activitypub' ),
|
|
|
|
'username' => \__( 'Username', 'activitypub' ),
|
2023-06-28 09:56:18 +02:00
|
|
|
'url' => \__( 'URL', 'activitypub' ),
|
2023-06-23 14:54:29 +02:00
|
|
|
'updated' => \__( 'Last updated', 'activitypub' ),
|
|
|
|
//'errors' => \__( 'Errors', 'activitypub' ),
|
|
|
|
//'latest-error' => \__( 'Latest Error Message', 'activitypub' ),
|
2023-04-21 14:56:22 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_sortable_columns() {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function prepare_items() {
|
|
|
|
$columns = $this->get_columns();
|
|
|
|
$hidden = array();
|
|
|
|
|
|
|
|
$this->process_action();
|
|
|
|
$this->_column_headers = array( $columns, $hidden, $this->get_sortable_columns() );
|
|
|
|
|
|
|
|
$page_num = $this->get_pagenum();
|
|
|
|
$per_page = 20;
|
|
|
|
|
2023-07-06 14:42:18 +02:00
|
|
|
$followers = FollowerCollection::get_followers( $this->user_id, $per_page, $page_num );
|
2023-06-28 09:56:18 +02:00
|
|
|
$counter = FollowerCollection::count_followers( $this->user_id );
|
2023-04-21 14:56:22 +02:00
|
|
|
|
|
|
|
$this->items = array();
|
|
|
|
$this->set_pagination_args(
|
|
|
|
array(
|
|
|
|
'total_items' => $counter,
|
|
|
|
'total_pages' => round( $counter / $per_page ),
|
|
|
|
'per_page' => $per_page,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2023-06-15 11:45:25 +02:00
|
|
|
foreach ( $followers as $follower ) {
|
2023-04-21 14:56:22 +02:00
|
|
|
$item = array(
|
2023-06-23 14:54:29 +02:00
|
|
|
'icon' => esc_attr( $follower->get_icon_url() ),
|
2023-05-02 14:39:25 +02:00
|
|
|
'name' => esc_attr( $follower->get_name() ),
|
2023-06-23 14:54:29 +02:00
|
|
|
'username' => esc_attr( $follower->get_preferred_username() ),
|
2023-06-28 09:56:18 +02:00
|
|
|
'url' => esc_attr( $follower->get_url() ),
|
|
|
|
'identifier' => esc_attr( $follower->get_id() ),
|
2023-06-23 14:54:29 +02:00
|
|
|
'updated' => esc_attr( $follower->get_updated() ),
|
2023-05-02 14:39:25 +02:00
|
|
|
'errors' => $follower->count_errors(),
|
|
|
|
'latest-error' => $follower->get_latest_error_message(),
|
2023-04-21 14:56:22 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_bulk_actions() {
|
|
|
|
return array(
|
2023-04-21 16:25:15 +02:00
|
|
|
'delete' => __( 'Delete', 'activitypub' ),
|
2023-04-21 14:56:22 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function column_default( $item, $column_name ) {
|
|
|
|
if ( ! array_key_exists( $column_name, $item ) ) {
|
|
|
|
return __( 'None', 'activitypub' );
|
|
|
|
}
|
|
|
|
return $item[ $column_name ];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function column_avatar( $item ) {
|
|
|
|
return sprintf(
|
|
|
|
'<img src="%s" width="25px;" />',
|
2023-06-23 14:54:29 +02:00
|
|
|
$item['icon']
|
2023-04-21 14:56:22 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-28 09:56:18 +02:00
|
|
|
public function column_url( $item ) {
|
2023-04-27 09:57:50 +02:00
|
|
|
return sprintf(
|
|
|
|
'<a href="%s" target="_blank">%s</a>',
|
2023-06-28 09:56:18 +02:00
|
|
|
$item['url'],
|
|
|
|
$item['url']
|
2023-04-27 09:57:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-21 14:56:22 +02:00
|
|
|
public function column_cb( $item ) {
|
2023-04-21 16:25:15 +02:00
|
|
|
return sprintf( '<input type="checkbox" name="followers[]" value="%s" />', esc_attr( $item['identifier'] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function process_action() {
|
2023-04-21 16:40:46 +02:00
|
|
|
if ( ! isset( $_REQUEST['followers'] ) || ! isset( $_REQUEST['_apnonce'] ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! wp_verify_nonce( $_REQUEST['_apnonce'], 'activitypub-followers-list' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:32:00 +02:00
|
|
|
if ( ! current_user_can( 'edit_user', $this->user_id ) ) {
|
2023-04-21 16:40:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$followers = $_REQUEST['followers']; // phpcs:ignore
|
2023-04-21 16:25:15 +02:00
|
|
|
|
|
|
|
switch ( $this->current_action() ) {
|
|
|
|
case 'delete':
|
2023-06-16 16:56:30 +02:00
|
|
|
if ( ! is_array( $followers ) ) {
|
|
|
|
$followers = array( $followers );
|
|
|
|
}
|
|
|
|
foreach ( $followers as $follower ) {
|
2023-06-28 09:56:18 +02:00
|
|
|
FollowerCollection::remove_follower( $this->user_id, $follower );
|
2023-06-16 16:56:30 +02:00
|
|
|
}
|
2023-04-21 16:25:15 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-04-21 14:56:22 +02:00
|
|
|
}
|
2023-05-24 16:32:00 +02:00
|
|
|
|
|
|
|
public function get_user_count() {
|
|
|
|
return FollowerCollection::count_followers( $this->user_id );
|
|
|
|
}
|
2023-04-21 14:56:22 +02:00
|
|
|
}
|