| 1 |
<?php |
| 2 |
namespace Activitypub\Table; |
| 3 |
|
| 4 |
use WP_List_Table; |
| 5 |
use Activitypub\Collection\Users; |
| 6 |
use Activitypub\Collection\Followers as FollowerCollection; |
| 7 |
|
| 8 |
use function Activitypub\object_to_uri; |
| 9 |
|
| 10 |
if ( ! \class_exists( '\WP_List_Table' ) ) { |
| 11 |
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 12 |
} |
| 13 |
|
| 14 |
class Followers extends WP_List_Table { |
| 15 |
private $user_id; |
| 16 |
|
| 17 |
public function __construct() { |
| 18 |
if ( get_current_screen()->id === 'settings_page_activitypub' ) { |
| 19 |
$this->user_id = Users::BLOG_USER_ID; |
| 20 |
} else { |
| 21 |
$this->user_id = \get_current_user_id(); |
| 22 |
} |
| 23 |
|
| 24 |
parent::__construct( |
| 25 |
array( |
| 26 |
'singular' => \__( 'Follower', 'activitypub' ), |
| 27 |
'plural' => \__( 'Followers', 'activitypub' ), |
| 28 |
'ajax' => false, |
| 29 |
) |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
public function get_columns() { |
| 34 |
return array( |
| 35 |
'cb' => '<input type="checkbox" />', |
| 36 |
'avatar' => \__( 'Avatar', 'activitypub' ), |
| 37 |
'post_title' => \__( 'Name', 'activitypub' ), |
| 38 |
'username' => \__( 'Username', 'activitypub' ), |
| 39 |
'url' => \__( 'URL', 'activitypub' ), |
| 40 |
'published' => \__( 'Followed', 'activitypub' ), |
| 41 |
'modified' => \__( 'Last updated', 'activitypub' ), |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
public function get_sortable_columns() { |
| 46 |
$sortable_columns = array( |
| 47 |
'post_title' => array( 'post_title', true ), |
| 48 |
'modified' => array( 'modified', false ), |
| 49 |
'published' => array( 'published', false ), |
| 50 |
); |
| 51 |
|
| 52 |
return $sortable_columns; |
| 53 |
} |
| 54 |
|
| 55 |
public function prepare_items() { |
| 56 |
$columns = $this->get_columns(); |
| 57 |
$hidden = array(); |
| 58 |
|
| 59 |
$this->process_action(); |
| 60 |
$this->_column_headers = array( $columns, $hidden, $this->get_sortable_columns() ); |
| 61 |
|
| 62 |
$page_num = $this->get_pagenum(); |
| 63 |
$per_page = 20; |
| 64 |
|
| 65 |
$args = array(); |
| 66 |
|
| 67 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 68 |
if ( isset( $_GET['orderby'] ) ) { |
| 69 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 70 |
$args['orderby'] = sanitize_text_field( wp_unslash( $_GET['orderby'] ) ); |
| 71 |
} |
| 72 |
|
| 73 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 74 |
if ( isset( $_GET['order'] ) ) { |
| 75 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 76 |
$args['order'] = sanitize_text_field( wp_unslash( $_GET['order'] ) ); |
| 77 |
} |
| 78 |
|
| 79 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 80 |
if ( isset( $_GET['s'] ) && isset( $_REQUEST['_wpnonce'] ) ) { |
| 81 |
$nonce = sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ); |
| 82 |
if ( wp_verify_nonce( $nonce, 'bulk-' . $this->_args['plural'] ) ) { |
| 83 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 84 |
$args['s'] = sanitize_text_field( wp_unslash( $_GET['s'] ) ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
$followers_with_count = FollowerCollection::get_followers_with_count( $this->user_id, $per_page, $page_num, $args ); |
| 89 |
$followers = $followers_with_count['followers']; |
| 90 |
$counter = $followers_with_count['total']; |
| 91 |
|
| 92 |
$this->items = array(); |
| 93 |
$this->set_pagination_args( |
| 94 |
array( |
| 95 |
'total_items' => $counter, |
| 96 |
'total_pages' => ceil( $counter / $per_page ), |
| 97 |
'per_page' => $per_page, |
| 98 |
) |
| 99 |
); |
| 100 |
|
| 101 |
foreach ( $followers as $follower ) { |
| 102 |
$item = array( |
| 103 |
'icon' => esc_attr( $follower->get_icon_url() ), |
| 104 |
'post_title' => esc_attr( $follower->get_name() ), |
| 105 |
'username' => esc_attr( $follower->get_preferred_username() ), |
| 106 |
'url' => esc_attr( object_to_uri( $follower->get_url() ) ), |
| 107 |
'identifier' => esc_attr( $follower->get_id() ), |
| 108 |
'published' => esc_attr( $follower->get_published() ), |
| 109 |
'modified' => esc_attr( $follower->get_updated() ), |
| 110 |
); |
| 111 |
|
| 112 |
$this->items[] = $item; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
public function get_bulk_actions() { |
| 117 |
return array( |
| 118 |
'delete' => __( 'Delete', 'activitypub' ), |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
public function column_default( $item, $column_name ) { |
| 123 |
if ( ! array_key_exists( $column_name, $item ) ) { |
| 124 |
return __( 'None', 'activitypub' ); |
| 125 |
} |
| 126 |
return $item[ $column_name ]; |
| 127 |
} |
| 128 |
|
| 129 |
public function column_avatar( $item ) { |
| 130 |
return sprintf( |
| 131 |
'<img src="%s" width="25px;" />', |
| 132 |
$item['icon'] |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
public function column_url( $item ) { |
| 137 |
return sprintf( |
| 138 |
'<a href="%s" target="_blank">%s</a>', |
| 139 |
$item['url'], |
| 140 |
$item['url'] |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
public function column_cb( $item ) { |
| 145 |
return sprintf( '<input type="checkbox" name="followers[]" value="%s" />', esc_attr( $item['identifier'] ) ); |
| 146 |
} |
| 147 |
|
| 148 |
public function process_action() { |
| 149 |
if ( ! isset( $_REQUEST['followers'] ) || ! isset( $_REQUEST['_wpnonce'] ) ) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
$nonce = sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ); |
| 153 |
if ( ! wp_verify_nonce( $nonce, 'bulk-' . $this->_args['plural'] ) ) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
if ( ! current_user_can( 'edit_user', $this->user_id ) ) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
$followers = $_REQUEST['followers']; // phpcs:ignore |
| 162 |
|
| 163 |
switch ( $this->current_action() ) { |
| 164 |
case 'delete': |
| 165 |
if ( ! is_array( $followers ) ) { |
| 166 |
$followers = array( $followers ); |
| 167 |
} |
| 168 |
foreach ( $followers as $follower ) { |
| 169 |
FollowerCollection::remove_follower( $this->user_id, $follower ); |
| 170 |
} |
| 171 |
break; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
public function get_user_count() { |
| 176 |
return FollowerCollection::count_followers( $this->user_id ); |
| 177 |
} |
| 178 |
} |
| 179 |
|