wordpress-event-bridge-for-.../includes/table/class-event-sources.php

239 lines
5.8 KiB
PHP
Raw Permalink Normal View History

2024-11-18 16:07:09 +01:00
<?php
/**
* Event Sources Table-Class file.
*
* This table display the event sources (=followed ActivityPub actors) that are used for
* importing (caching and displaying) remote events to the WordPress site.
*
2024-12-05 17:50:17 +01:00
* @package Event_Bridge_For_ActivityPub
* @since 1.0.0
* @license AGPL-3.0-or-later
2024-11-18 16:07:09 +01:00
*/
2024-12-05 17:50:17 +01:00
namespace Event_Bridge_For_ActivityPub\Table;
2024-11-18 16:07:09 +01:00
use WP_List_Table;
use Event_Bridge_For_ActivityPub\ActivityPub\Collection\Event_Sources as Event_Sources_Collection;
2024-11-18 16:07:09 +01:00
use function Activitypub\object_to_uri;
if ( ! \class_exists( '\WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
/**
* Event Sources Table-Class.
2024-11-18 16:07:09 +01:00
*/
class Event_Sources extends WP_List_Table {
/**
* Constructor.
*/
public function __construct() {
parent::__construct(
array(
2024-12-07 20:20:17 +01:00
'singular' => \__( 'Event Source', 'event-bridge-for-activitypub' ),
'plural' => \__( 'Event Sources', 'event-bridge-for-activitypub' ),
2024-11-18 16:07:09 +01:00
'ajax' => false,
)
);
}
/**
* Get columns.
*
* @return array
*/
public function get_columns() {
return array(
'cb' => '<input type="checkbox" />',
'icon' => \__( 'Icon', 'event-bridge-for-activitypub' ),
'name' => \__( 'Name', 'event-bridge-for-activitypub' ),
'active' => \__( 'Active', 'event-bridge-for-activitypub' ),
'url' => \__( 'URL', 'event-bridge-for-activitypub' ),
'published' => \__( 'Followed', 'event-bridge-for-activitypub' ),
'modified' => \__( 'Last updated', 'event-bridge-for-activitypub' ),
2024-11-18 16:07:09 +01:00
);
}
/**
* Returns sortable columns.
*
* @return array
*/
public function get_sortable_columns() {
return array(
'name' => array( 'name', true ),
'modified' => array( 'modified', false ),
'published' => array( 'published', false ),
2024-11-18 16:07:09 +01:00
);
}
/**
* Prepare items.
*/
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;
$args = array();
// phpcs:disable WordPress.Security.NonceVerification.Recommended
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'] ) );
}
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
$event_sources = Event_Sources_Collection::get_event_sources_with_count( $per_page, $page_num, $args );
2024-11-18 16:07:09 +01:00
$actors = $event_sources['actors'];
$counter = $event_sources['total'];
$this->items = array();
$this->set_pagination_args(
array(
'total_items' => $counter,
'total_pages' => ceil( $counter / $per_page ),
'per_page' => $per_page,
)
);
foreach ( $actors as $actor ) {
$item = array(
'icon' => esc_attr( $actor->get_icon_url() ),
'name' => esc_attr( $actor->get_name() ),
'url' => esc_attr( object_to_uri( $actor->get_id() ) ),
'active' => esc_attr( get_post_meta( $actor->get__id(), 'event_source_active', true ) ),
2024-11-18 16:07:09 +01:00
'identifier' => esc_attr( $actor->get_id() ),
'published' => esc_attr( $actor->get_published() ),
'modified' => esc_attr( $actor->get_updated() ),
);
$this->items[] = $item;
}
}
/**
* Returns bulk actions.
*
* @return array
*/
public function get_bulk_actions() {
return array(
2024-12-07 20:20:17 +01:00
'delete' => __( 'Delete', 'event-bridge-for-activitypub' ),
2024-11-18 16:07:09 +01:00
);
}
/**
* Column default.
*
* @param array $item Item.
* @param string $column_name Column name.
* @return string
*/
public function column_default( $item, $column_name ) {
if ( ! array_key_exists( $column_name, $item ) ) {
2024-12-07 20:20:17 +01:00
return __( 'None', 'event-bridge-for-activitypub' );
2024-11-18 16:07:09 +01:00
}
return $item[ $column_name ];
}
/**
* Column avatar.
*
* @param array $item Item.
* @return string
*/
public function column_icon( $item ) {
2024-11-18 16:07:09 +01:00
return sprintf(
'<img src="%s" width="25px;" />',
$item['icon']
);
}
/**
* Column url.
*
* @param array $item Item.
* @return string
*/
public function column_url( $item ) {
return sprintf(
'<a href="%s" target="_blank">%s</a>',
esc_url( $item['url'] ),
$item['url']
);
}
/**
* Column cb.
*
* @param array $item Item.
* @return string
*/
public function column_cb( $item ) {
return sprintf( '<input type="checkbox" name="event_sources[]" value="%s" />', esc_attr( $item['identifier'] ) );
}
/**
* Column action.
*
* @param array $item Item.
* @return string
*/
public function column_active( $item ) {
if ( $item['active'] ) {
$action = 'true';
} else {
$action = 'false';
}
return sprintf(
'%s',
$action
);
2024-11-18 16:07:09 +01:00
}
/**
* Process action.
*/
public function process_action() {
if ( ! isset( $_REQUEST['event_sources'] ) || ! isset( $_REQUEST['_wpnonce'] ) ) {
2024-11-18 16:07:09 +01:00
return;
}
$nonce = sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) );
if ( ! wp_verify_nonce( $nonce, 'bulk-' . $this->_args['plural'] ) ) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
2024-11-18 16:07:09 +01:00
return;
}
$event_sources = $_REQUEST['event_sources']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
2024-11-18 16:07:09 +01:00
if ( 'delete' === $this->current_action() ) {
if ( ! is_array( $event_sources ) ) {
$event_sources = array( $event_sources );
2024-11-18 16:07:09 +01:00
}
foreach ( $event_sources as $event_source ) {
2024-12-15 13:08:26 +01:00
Event_Sources_Collection::remove_event_source( $event_source );
2024-11-18 16:07:09 +01:00
}
}
}
}