2023-10-22 22:20:53 +00:00
|
|
|
<?php
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Followers Table-Class file.
|
|
|
|
*
|
|
|
|
* @package Activitypub
|
|
|
|
*/
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
namespace Activitypub\Table;
|
|
|
|
|
|
|
|
use WP_List_Table;
|
|
|
|
use Activitypub\Collection\Users;
|
|
|
|
use Activitypub\Collection\Followers as FollowerCollection;
|
|
|
|
|
2024-02-08 12:31:25 +00:00
|
|
|
use function Activitypub\object_to_uri;
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
if ( ! \class_exists( '\WP_List_Table' ) ) {
|
|
|
|
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Followers Table-Class.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
class Followers extends WP_List_Table {
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* User ID.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
private $user_id;
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function __construct() {
|
|
|
|
if ( get_current_screen()->id === 'settings_page_activitypub' ) {
|
|
|
|
$this->user_id = Users::BLOG_USER_ID;
|
|
|
|
} else {
|
|
|
|
$this->user_id = \get_current_user_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct(
|
|
|
|
array(
|
|
|
|
'singular' => \__( 'Follower', 'activitypub' ),
|
|
|
|
'plural' => \__( 'Followers', 'activitypub' ),
|
|
|
|
'ajax' => false,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Get columns.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function get_columns() {
|
|
|
|
return array(
|
2023-12-08 23:23:11 +00:00
|
|
|
'cb' => '<input type="checkbox" />',
|
|
|
|
'avatar' => \__( 'Avatar', 'activitypub' ),
|
|
|
|
'post_title' => \__( 'Name', 'activitypub' ),
|
|
|
|
'username' => \__( 'Username', 'activitypub' ),
|
|
|
|
'url' => \__( 'URL', 'activitypub' ),
|
|
|
|
'published' => \__( 'Followed', 'activitypub' ),
|
|
|
|
'modified' => \__( 'Last updated', 'activitypub' ),
|
2023-10-22 22:20:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Returns sortable columns.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function get_sortable_columns() {
|
2024-10-09 12:44:17 +00:00
|
|
|
return array(
|
2023-12-08 23:23:11 +00:00
|
|
|
'post_title' => array( 'post_title', true ),
|
|
|
|
'modified' => array( 'modified', false ),
|
|
|
|
'published' => array( 'published', false ),
|
|
|
|
);
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Prepare items.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
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-12-08 23:23:11 +00:00
|
|
|
$args = array();
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended
|
2023-12-08 23:23:11 +00:00
|
|
|
if ( isset( $_GET['orderby'] ) ) {
|
|
|
|
$args['orderby'] = sanitize_text_field( wp_unslash( $_GET['orderby'] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $_GET['order'] ) ) {
|
|
|
|
$args['order'] = sanitize_text_field( wp_unslash( $_GET['order'] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $_GET['s'] ) && isset( $_REQUEST['_wpnonce'] ) ) {
|
|
|
|
$nonce = sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) );
|
|
|
|
if ( wp_verify_nonce( $nonce, 'bulk-' . $this->_args['plural'] ) ) {
|
|
|
|
$args['s'] = sanitize_text_field( wp_unslash( $_GET['s'] ) );
|
|
|
|
}
|
|
|
|
}
|
2024-10-09 12:44:17 +00:00
|
|
|
// phpcs:enable WordPress.Security.NonceVerification.Recommended
|
2023-12-08 23:23:11 +00:00
|
|
|
|
|
|
|
$followers_with_count = FollowerCollection::get_followers_with_count( $this->user_id, $per_page, $page_num, $args );
|
|
|
|
$followers = $followers_with_count['followers'];
|
|
|
|
$counter = $followers_with_count['total'];
|
2023-10-22 22:20:53 +00:00
|
|
|
|
|
|
|
$this->items = array();
|
|
|
|
$this->set_pagination_args(
|
|
|
|
array(
|
|
|
|
'total_items' => $counter,
|
|
|
|
'total_pages' => ceil( $counter / $per_page ),
|
|
|
|
'per_page' => $per_page,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $followers as $follower ) {
|
|
|
|
$item = array(
|
2023-12-08 23:23:11 +00:00
|
|
|
'icon' => esc_attr( $follower->get_icon_url() ),
|
|
|
|
'post_title' => esc_attr( $follower->get_name() ),
|
|
|
|
'username' => esc_attr( $follower->get_preferred_username() ),
|
2024-02-08 12:31:25 +00:00
|
|
|
'url' => esc_attr( object_to_uri( $follower->get_url() ) ),
|
2023-12-08 23:23:11 +00:00
|
|
|
'identifier' => esc_attr( $follower->get_id() ),
|
|
|
|
'published' => esc_attr( $follower->get_published() ),
|
|
|
|
'modified' => esc_attr( $follower->get_updated() ),
|
2023-10-22 22:20:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Returns bulk actions.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function get_bulk_actions() {
|
|
|
|
return array(
|
|
|
|
'delete' => __( 'Delete', 'activitypub' ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Column default.
|
|
|
|
*
|
|
|
|
* @param array $item Item.
|
|
|
|
* @param string $column_name Column name.
|
|
|
|
* @return string
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function column_default( $item, $column_name ) {
|
|
|
|
if ( ! array_key_exists( $column_name, $item ) ) {
|
|
|
|
return __( 'None', 'activitypub' );
|
|
|
|
}
|
|
|
|
return $item[ $column_name ];
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Column avatar.
|
|
|
|
*
|
|
|
|
* @param array $item Item.
|
|
|
|
* @return string
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function column_avatar( $item ) {
|
|
|
|
return sprintf(
|
|
|
|
'<img src="%s" width="25px;" />',
|
|
|
|
$item['icon']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Column url.
|
|
|
|
*
|
|
|
|
* @param array $item Item.
|
|
|
|
* @return string
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function column_url( $item ) {
|
|
|
|
return sprintf(
|
|
|
|
'<a href="%s" target="_blank">%s</a>',
|
2024-10-09 12:44:17 +00:00
|
|
|
esc_url( $item['url'] ),
|
2023-10-22 22:20:53 +00:00
|
|
|
$item['url']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Column cb.
|
|
|
|
*
|
|
|
|
* @param array $item Item.
|
|
|
|
* @return string
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function column_cb( $item ) {
|
|
|
|
return sprintf( '<input type="checkbox" name="followers[]" value="%s" />', esc_attr( $item['identifier'] ) );
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Process action.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function process_action() {
|
2023-12-08 23:23:11 +00:00
|
|
|
if ( ! isset( $_REQUEST['followers'] ) || ! isset( $_REQUEST['_wpnonce'] ) ) {
|
2024-10-09 12:44:17 +00:00
|
|
|
return;
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
2023-12-08 23:23:11 +00:00
|
|
|
$nonce = sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) );
|
|
|
|
if ( ! wp_verify_nonce( $nonce, 'bulk-' . $this->_args['plural'] ) ) {
|
2024-10-09 12:44:17 +00:00
|
|
|
return;
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! current_user_can( 'edit_user', $this->user_id ) ) {
|
2024-10-09 12:44:17 +00:00
|
|
|
return;
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
$followers = $_REQUEST['followers']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
|
|
|
|
|
|
|
|
if ( $this->current_action() === 'delete' ) {
|
|
|
|
if ( ! is_array( $followers ) ) {
|
|
|
|
$followers = array( $followers );
|
|
|
|
}
|
|
|
|
foreach ( $followers as $follower ) {
|
|
|
|
FollowerCollection::remove_follower( $this->user_id, $follower );
|
|
|
|
}
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Returns user count.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function get_user_count() {
|
|
|
|
return FollowerCollection::count_followers( $this->user_id );
|
|
|
|
}
|
|
|
|
}
|