| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin Class. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\WP_Admin; |
| 9 |
|
| 10 |
use Activitypub\Comment; |
| 11 |
use Activitypub\Collection\Actors; |
| 12 |
use Activitypub\Collection\Extra_Fields; |
| 13 |
use Activitypub\Model\Blog; |
| 14 |
|
| 15 |
use function Activitypub\count_followers; |
| 16 |
use function Activitypub\get_content_visibility; |
| 17 |
use function Activitypub\is_user_type_disabled; |
| 18 |
use function Activitypub\site_supports_blocks; |
| 19 |
use function Activitypub\user_can_activitypub; |
| 20 |
use function Activitypub\was_comment_received; |
| 21 |
|
| 22 |
/** |
| 23 |
* ActivityPub Admin Class. |
| 24 |
* |
| 25 |
* @author Matthias Pfefferle |
| 26 |
*/ |
| 27 |
class Admin { |
| 28 |
/** |
| 29 |
* Initialize the class, registering WordPress hooks, |
| 30 |
*/ |
| 31 |
public static function init() { |
| 32 |
\add_action( 'load-comment.php', array( self::class, 'edit_comment' ) ); |
| 33 |
\add_action( 'load-post.php', array( self::class, 'edit_post' ) ); |
| 34 |
\add_action( 'load-edit.php', array( self::class, 'list_posts' ) ); |
| 35 |
\add_filter( 'page_row_actions', array( self::class, 'row_actions' ), 10, 2 ); |
| 36 |
\add_filter( 'post_row_actions', array( self::class, 'row_actions' ), 10, 2 ); |
| 37 |
\add_action( 'personal_options_update', array( self::class, 'save_user_settings' ) ); |
| 38 |
\add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_scripts' ) ); |
| 39 |
\add_action( 'admin_notices', array( self::class, 'admin_notices' ) ); |
| 40 |
|
| 41 |
\add_filter( 'comment_row_actions', array( self::class, 'comment_row_actions' ), 10, 2 ); |
| 42 |
\add_filter( 'manage_edit-comments_columns', array( static::class, 'manage_comment_columns' ) ); |
| 43 |
\add_action( 'manage_comments_custom_column', array( static::class, 'manage_comments_custom_column' ), 9, 2 ); |
| 44 |
\add_filter( 'admin_comment_types_dropdown', array( static::class, 'comment_types_dropdown' ) ); |
| 45 |
|
| 46 |
\add_filter( 'manage_posts_columns', array( static::class, 'manage_post_columns' ), 10, 2 ); |
| 47 |
\add_action( 'manage_posts_custom_column', array( self::class, 'manage_posts_custom_column' ), 10, 2 ); |
| 48 |
|
| 49 |
\add_filter( 'manage_users_columns', array( self::class, 'manage_users_columns' ) ); |
| 50 |
\add_filter( 'manage_users_custom_column', array( self::class, 'manage_users_custom_column' ), 10, 3 ); |
| 51 |
\add_filter( 'bulk_actions-users', array( self::class, 'user_bulk_options' ) ); |
| 52 |
\add_filter( 'handle_bulk_actions-users', array( self::class, 'handle_bulk_request' ), 10, 3 ); |
| 53 |
|
| 54 |
if ( user_can_activitypub( \get_current_user_id() ) ) { |
| 55 |
\add_action( 'show_user_profile', array( self::class, 'add_profile' ) ); |
| 56 |
} |
| 57 |
|
| 58 |
\add_filter( 'dashboard_glance_items', array( self::class, 'dashboard_glance_items' ) ); |
| 59 |
\add_filter( 'plugin_action_links_' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'add_plugin_settings_link' ) ); |
| 60 |
\add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ), 10, 2 ); |
| 61 |
|
| 62 |
if ( site_supports_blocks() ) { |
| 63 |
\add_action( 'tool_box', array( self::class, 'tool_box' ) ); |
| 64 |
} |
| 65 |
|
| 66 |
\add_action( 'admin_print_footer_scripts-settings_page_activitypub', array( self::class, 'open_help_tab' ) ); |
| 67 |
|
| 68 |
\add_action( 'wp_dashboard_setup', array( self::class, 'add_dashboard_widgets' ) ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Display admin menu notices about configuration problems or conflicts. |
| 73 |
*/ |
| 74 |
public static function admin_notices() { |
| 75 |
$current_screen = get_current_screen(); |
| 76 |
if ( ! $current_screen ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
if ( 'edit' === $current_screen->base && Extra_Fields::is_extra_fields_post_type( $current_screen->post_type ) ) { |
| 80 |
?> |
| 81 |
<div class="notice" style="margin: 0; background: none; border: none; box-shadow: none; padding: 15px 0 0 0; font-size: 14px;"> |
| 82 |
<?php |
| 83 |
esc_html_e( 'These are extra fields that are used for your ActivityPub profile. You can use your homepage, social profiles, pronouns, age, anything you want.', 'activitypub' ); |
| 84 |
?> |
| 85 |
</div> |
| 86 |
<?php |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Display one admin menu notice about configuration problems or conflicts. |
| 92 |
* |
| 93 |
* @param string $admin_notice The notice to display. |
| 94 |
* @param string $level The level of the notice (error, warning, success, info). |
| 95 |
*/ |
| 96 |
private static function show_admin_notice( $admin_notice, $level ) { |
| 97 |
?> |
| 98 |
|
| 99 |
<div class="notice notice-<?php echo esc_attr( $level ); ?>"> |
| 100 |
<p><?php echo wp_kses( $admin_notice, 'data' ); ?></p> |
| 101 |
</div> |
| 102 |
|
| 103 |
<?php |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Load user settings page |
| 108 |
*/ |
| 109 |
public static function followers_list_page() { |
| 110 |
// User has to be able to publish posts. |
| 111 |
if ( user_can_activitypub( \get_current_user_id() ) ) { |
| 112 |
\load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/user-followers-list.php' ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Load user following list page |
| 118 |
*/ |
| 119 |
public static function following_list_page() { |
| 120 |
// User has to be able to publish posts. |
| 121 |
if ( user_can_activitypub( \get_current_user_id() ) ) { |
| 122 |
\load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/user-following-list.php' ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Adds the follower list to the Help tab. |
| 128 |
*/ |
| 129 |
public static function add_followers_list_help_tab() { |
| 130 |
// todo. |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Adds the following list to the Help tab. |
| 135 |
*/ |
| 136 |
public static function add_following_list_help_tab() { |
| 137 |
// todo. |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Render user settings. |
| 142 |
*/ |
| 143 |
public static function add_profile() { |
| 144 |
wp_enqueue_media(); |
| 145 |
wp_enqueue_script( 'activitypub-header-image' ); |
| 146 |
|
| 147 |
wp_nonce_field( 'activitypub-user-settings', '_apnonce' ); |
| 148 |
do_settings_sections( 'activitypub_user_settings' ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Save the user settings. |
| 153 |
* |
| 154 |
* Handles the saving of the ActivityPub settings. |
| 155 |
* |
| 156 |
* @param int $user_id The user ID. |
| 157 |
*/ |
| 158 |
public static function save_user_settings( $user_id ) { |
| 159 |
if ( ! isset( $_REQUEST['_apnonce'] ) ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
$nonce = sanitize_text_field( wp_unslash( $_REQUEST['_apnonce'] ) ); |
| 164 |
if ( |
| 165 |
! wp_verify_nonce( $nonce, 'activitypub-user-settings' ) || |
| 166 |
! current_user_can( 'edit_user', $user_id ) |
| 167 |
) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
// User options that should be processed with `sanitize_textarea_field()`. |
| 172 |
$textarea_field_user_options = array( |
| 173 |
'activitypub_also_known_as', |
| 174 |
'activitypub_description', |
| 175 |
); |
| 176 |
|
| 177 |
foreach ( $textarea_field_user_options as $option ) { |
| 178 |
if ( ! empty( $_POST[ $option ] ) ) { |
| 179 |
\update_user_option( $user_id, $option, sanitize_textarea_field( wp_unslash( $_POST[ $option ] ) ) ); |
| 180 |
} else { |
| 181 |
\delete_user_option( $user_id, $option ); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// User options that should be processed with `sanitize_text_field()`. |
| 186 |
$text_field_user_options = array( |
| 187 |
'activitypub_header_image', |
| 188 |
); |
| 189 |
|
| 190 |
foreach ( $text_field_user_options as $option ) { |
| 191 |
if ( ! empty( $_POST[ $option ] ) ) { |
| 192 |
\update_user_option( $user_id, $option, sanitize_text_field( wp_unslash( $_POST[ $option ] ) ) ); |
| 193 |
} else { |
| 194 |
\delete_user_option( $user_id, $option ); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
// User options that have a default value and therefore can't be empty (Empty triggers the default value). |
| 199 |
$required_user_options = array( |
| 200 |
'activitypub_mailer_new_dm', |
| 201 |
'activitypub_mailer_new_follower', |
| 202 |
'activitypub_mailer_new_mention', |
| 203 |
); |
| 204 |
|
| 205 |
foreach ( $required_user_options as $option ) { |
| 206 |
\update_user_option( $user_id, $option, sanitize_text_field( wp_unslash( $_POST[ $option ] ?? 0 ) ) ); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Enqueue the admin scripts and styles. |
| 212 |
* |
| 213 |
* @param string $hook_suffix The current page. |
| 214 |
*/ |
| 215 |
public static function enqueue_scripts( $hook_suffix ) { |
| 216 |
wp_register_script( |
| 217 |
'activitypub-header-image', |
| 218 |
plugins_url( |
| 219 |
'assets/js/activitypub-header-image.js', |
| 220 |
ACTIVITYPUB_PLUGIN_FILE |
| 221 |
), |
| 222 |
array( 'jquery' ), |
| 223 |
ACTIVITYPUB_PLUGIN_VERSION, |
| 224 |
false |
| 225 |
); |
| 226 |
|
| 227 |
if ( false !== strpos( $hook_suffix, 'activitypub' ) ) { |
| 228 |
wp_enqueue_style( |
| 229 |
'activitypub-admin-styles', |
| 230 |
plugins_url( |
| 231 |
'assets/css/activitypub-admin.css', |
| 232 |
ACTIVITYPUB_PLUGIN_FILE |
| 233 |
), |
| 234 |
array(), |
| 235 |
ACTIVITYPUB_PLUGIN_VERSION |
| 236 |
); |
| 237 |
wp_enqueue_script( |
| 238 |
'activitypub-admin-script', |
| 239 |
plugins_url( |
| 240 |
'assets/js/activitypub-admin.js', |
| 241 |
ACTIVITYPUB_PLUGIN_FILE |
| 242 |
), |
| 243 |
array( 'jquery', 'wp-util' ), |
| 244 |
ACTIVITYPUB_PLUGIN_VERSION, |
| 245 |
false |
| 246 |
); |
| 247 |
|
| 248 |
// Plugin cards in help tab. |
| 249 |
\wp_enqueue_script( 'plugin-install' ); |
| 250 |
\add_thickbox(); |
| 251 |
\wp_enqueue_script( 'updates' ); |
| 252 |
} |
| 253 |
|
| 254 |
if ( 'index.php' === $hook_suffix ) { |
| 255 |
wp_enqueue_style( |
| 256 |
'activitypub-admin-styles', |
| 257 |
plugins_url( |
| 258 |
'assets/css/activitypub-admin.css', |
| 259 |
ACTIVITYPUB_PLUGIN_FILE |
| 260 |
), |
| 261 |
array(), |
| 262 |
ACTIVITYPUB_PLUGIN_VERSION |
| 263 |
); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Hook into the edit_comment functionality. |
| 269 |
* |
| 270 |
* Disables the edit_comment capability for federated comments. |
| 271 |
*/ |
| 272 |
public static function edit_comment() { |
| 273 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 274 |
$comment_id = \absint( $_GET['c'] ?? 0 ); |
| 275 |
if ( Comment::was_received( $comment_id ) ) { |
| 276 |
$path = 'edit-comments.php'; |
| 277 |
|
| 278 |
switch ( \wp_get_comment_status( $comment_id ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 279 |
case 'spam': |
| 280 |
$path = 'edit-comments.php?comment_status=spam'; |
| 281 |
break; |
| 282 |
|
| 283 |
case 'trash': |
| 284 |
$path = 'edit-comments.php?comment_status=trash'; |
| 285 |
break; |
| 286 |
|
| 287 |
case 'unapproved': |
| 288 |
$path = 'edit-comments.php?comment_status=moderated'; |
| 289 |
break; |
| 290 |
} |
| 291 |
|
| 292 |
// Redirect to the appropriate comments page. |
| 293 |
\wp_safe_redirect( \admin_url( $path ) ); |
| 294 |
exit; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Hook into the edit_post functionality. |
| 300 |
* |
| 301 |
* Disables the edit_post capability for federated posts. |
| 302 |
*/ |
| 303 |
public static function edit_post() { |
| 304 |
// Disable the edit_post capability for federated posts. |
| 305 |
\add_filter( |
| 306 |
'user_has_cap', |
| 307 |
function ( $all_caps, $caps, $arg ) { |
| 308 |
if ( 'edit_post' !== $arg[0] ) { |
| 309 |
return $all_caps; |
| 310 |
} |
| 311 |
|
| 312 |
$post = get_post( $arg[2] ); |
| 313 |
|
| 314 |
if ( ! Extra_Fields::is_extra_field_post_type( $post->post_type ) ) { |
| 315 |
return $all_caps; |
| 316 |
} |
| 317 |
|
| 318 |
if ( get_current_user_id() !== (int) $post->post_author ) { |
| 319 |
return false; |
| 320 |
} |
| 321 |
|
| 322 |
return $all_caps; |
| 323 |
}, |
| 324 |
1, |
| 325 |
3 |
| 326 |
); |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Add ActivityPub specific actions/filters to the post list view. |
| 331 |
*/ |
| 332 |
public static function list_posts() { |
| 333 |
// Show only the user's extra fields. |
| 334 |
\add_action( |
| 335 |
'pre_get_posts', |
| 336 |
function ( $query ) { |
| 337 |
if ( $query->get( 'post_type' ) === 'ap_extrafield' ) { |
| 338 |
$query->set( 'author', get_current_user_id() ); |
| 339 |
} |
| 340 |
} |
| 341 |
); |
| 342 |
|
| 343 |
// Remove all views for the extra fields. |
| 344 |
$screen_id = get_current_screen()->id; |
| 345 |
|
| 346 |
add_filter( |
| 347 |
"views_{$screen_id}", |
| 348 |
function ( $views ) { |
| 349 |
if ( Extra_Fields::is_extra_fields_post_type( get_current_screen()->post_type ) ) { |
| 350 |
return array(); |
| 351 |
} |
| 352 |
|
| 353 |
return $views; |
| 354 |
} |
| 355 |
); |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Comment row actions. |
| 360 |
* |
| 361 |
* @param array $actions The existing actions. |
| 362 |
* @param int|\WP_Comment $comment The comment object or ID. |
| 363 |
* |
| 364 |
* @return array The modified actions. |
| 365 |
*/ |
| 366 |
public static function comment_row_actions( $actions, $comment ) { |
| 367 |
if ( was_comment_received( $comment ) ) { |
| 368 |
unset( $actions['edit'], $actions['quickedit'] ); |
| 369 |
} |
| 370 |
|
| 371 |
if ( in_array( get_comment_type( $comment ), Comment::get_comment_type_slugs(), true ) ) { |
| 372 |
unset( $actions['reply'] ); |
| 373 |
} |
| 374 |
|
| 375 |
return $actions; |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Add a column "activitypub". |
| 380 |
* |
| 381 |
* This column shows if the user has the capability to use ActivityPub. |
| 382 |
* |
| 383 |
* @param array $columns The columns. |
| 384 |
* |
| 385 |
* @return array The columns extended by the activitypub. |
| 386 |
*/ |
| 387 |
public static function manage_users_columns( $columns ) { |
| 388 |
$columns['activitypub'] = __( 'ActivityPub', 'activitypub' ); |
| 389 |
return $columns; |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Add "comment-type" and "protocol" as column in WP-Admin. |
| 394 |
* |
| 395 |
* @param array $columns The list of column names. |
| 396 |
* |
| 397 |
* @return array The extended list of column names. |
| 398 |
*/ |
| 399 |
public static function manage_comment_columns( $columns ) { |
| 400 |
$columns['comment_type'] = esc_attr__( 'Comment-Type', 'activitypub' ); |
| 401 |
$columns['comment_protocol'] = esc_attr__( 'Protocol', 'activitypub' ); |
| 402 |
|
| 403 |
return $columns; |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Add "post_content" as column for Extra-Fields in WP-Admin. |
| 408 |
* |
| 409 |
* @param array $columns The list of column names. |
| 410 |
* @param string $post_type The post type. |
| 411 |
* |
| 412 |
* @return array The extended list of column names. |
| 413 |
*/ |
| 414 |
public static function manage_post_columns( $columns, $post_type ) { |
| 415 |
if ( Extra_Fields::is_extra_fields_post_type( $post_type ) ) { |
| 416 |
$after_key = 'title'; |
| 417 |
$index = array_search( $after_key, array_keys( $columns ), true ); |
| 418 |
$columns = array_slice( $columns, 0, $index + 1 ) + array( 'extra_field_content' => esc_attr__( 'Content', 'activitypub' ) ) + $columns; |
| 419 |
} |
| 420 |
|
| 421 |
return $columns; |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Add "comment-type" and "protocol" as column in WP-Admin. |
| 426 |
* |
| 427 |
* @param array $column The column to implement. |
| 428 |
* @param int $comment_id The comment id. |
| 429 |
*/ |
| 430 |
public static function manage_comments_custom_column( $column, $comment_id ) { |
| 431 |
if ( 'comment_type' === $column && ! defined( 'WEBMENTION_PLUGIN_DIR' ) ) { |
| 432 |
echo esc_attr( ucfirst( get_comment_type( $comment_id ) ) ); |
| 433 |
} elseif ( 'comment_protocol' === $column ) { |
| 434 |
$protocol = get_comment_meta( $comment_id, 'protocol', true ); |
| 435 |
|
| 436 |
if ( $protocol ) { |
| 437 |
echo esc_attr( ucfirst( str_replace( 'activitypub', 'ActivityPub', $protocol ) ) ); |
| 438 |
} else { |
| 439 |
esc_attr_e( 'Local', 'activitypub' ); |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Add the new ActivityPub comment types to the comment types dropdown. |
| 446 |
* |
| 447 |
* @param array $types The existing comment types. |
| 448 |
* |
| 449 |
* @return array The extended comment types. |
| 450 |
*/ |
| 451 |
public static function comment_types_dropdown( $types ) { |
| 452 |
foreach ( Comment::get_comment_types() as $comment_type ) { |
| 453 |
$types[ $comment_type['type'] ] = esc_html( $comment_type['label'] ); |
| 454 |
} |
| 455 |
|
| 456 |
return $types; |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* Return the results for the activitypub column. |
| 461 |
* |
| 462 |
* @param string $output Custom column output. Default empty. |
| 463 |
* @param string $column_name Column name. |
| 464 |
* @param int $user_id ID of the currently-listed user. |
| 465 |
* |
| 466 |
* @return string The column contents. |
| 467 |
*/ |
| 468 |
public static function manage_users_custom_column( $output, $column_name, $user_id ) { |
| 469 |
if ( 'activitypub' !== $column_name ) { |
| 470 |
return $output; |
| 471 |
} |
| 472 |
|
| 473 |
if ( \user_can( $user_id, 'activitypub' ) ) { |
| 474 |
return '<span aria-hidden="true">✓</span><span class="screen-reader-text">' . esc_html__( 'ActivityPub enabled for this author', 'activitypub' ) . '</span>'; |
| 475 |
} else { |
| 476 |
return '<span aria-hidden="true">✗</span><span class="screen-reader-text">' . esc_html__( 'ActivityPub disabled for this author', 'activitypub' ) . '</span>'; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
/** |
| 481 |
* Add a column "extra_field_content" to the post list view. |
| 482 |
* |
| 483 |
* @param string $column_name The column name. |
| 484 |
* @param int $post_id The post ID. |
| 485 |
* |
| 486 |
* @return void |
| 487 |
*/ |
| 488 |
public static function manage_posts_custom_column( $column_name, $post_id ) { |
| 489 |
if ( 'extra_field_content' === $column_name ) { |
| 490 |
$post = get_post( $post_id ); |
| 491 |
if ( Extra_Fields::is_extra_fields_post_type( $post->post_type ) ) { |
| 492 |
echo esc_attr( wp_strip_all_tags( $post->post_content ) ); |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
/** |
| 498 |
* Add options to the Bulk dropdown on the users page. |
| 499 |
* |
| 500 |
* @param array $actions The existing bulk options. |
| 501 |
* |
| 502 |
* @return array The extended bulk options. |
| 503 |
*/ |
| 504 |
public static function user_bulk_options( $actions ) { |
| 505 |
$actions['add_activitypub_cap'] = __( 'Enable for ActivityPub', 'activitypub' ); |
| 506 |
$actions['remove_activitypub_cap'] = __( 'Disable for ActivityPub', 'activitypub' ); |
| 507 |
|
| 508 |
return $actions; |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* Handle bulk activitypub requests. |
| 513 |
* |
| 514 |
* * `add_activitypub_cap` - Add the activitypub capability to the selected users. |
| 515 |
* * `remove_activitypub_cap` - Remove the activitypub capability from the selected users. |
| 516 |
* |
| 517 |
* @param string $send_back The URL to send the user back to. |
| 518 |
* @param string $action The requested action. |
| 519 |
* @param array $users The selected users. |
| 520 |
* |
| 521 |
* @return string The URL to send the user back to. |
| 522 |
*/ |
| 523 |
public static function handle_bulk_request( $send_back, $action, $users ) { |
| 524 |
if ( |
| 525 |
'remove_activitypub_cap' !== $action && |
| 526 |
'add_activitypub_cap' !== $action |
| 527 |
) { |
| 528 |
return $send_back; |
| 529 |
} |
| 530 |
|
| 531 |
foreach ( $users as $user_id ) { |
| 532 |
$user = new \WP_User( $user_id ); |
| 533 |
if ( 'add_activitypub_cap' === $action ) { |
| 534 |
$user->add_cap( 'activitypub' ); |
| 535 |
} elseif ( 'remove_activitypub_cap' === $action ) { |
| 536 |
$user->remove_cap( 'activitypub' ); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
return $send_back; |
| 541 |
} |
| 542 |
|
| 543 |
/** |
| 544 |
* Add ActivityPub infos to the dashboard glance items. |
| 545 |
* |
| 546 |
* @param array $items The existing glance items. |
| 547 |
* |
| 548 |
* @return array The extended glance items. |
| 549 |
*/ |
| 550 |
public static function dashboard_glance_items( $items ) { |
| 551 |
\add_filter( 'number_format_i18n', '\Activitypub\custom_large_numbers', 10, 2 ); |
| 552 |
|
| 553 |
if ( user_can_activitypub( \get_current_user_id() ) ) { |
| 554 |
$follower_count = sprintf( |
| 555 |
// translators: %s: number of followers. |
| 556 |
_n( |
| 557 |
'%s Follower', |
| 558 |
'%s Followers', |
| 559 |
count_followers( \get_current_user_id() ), |
| 560 |
'activitypub' |
| 561 |
), |
| 562 |
\number_format_i18n( count_followers( \get_current_user_id() ) ) |
| 563 |
); |
| 564 |
$items['activitypub-followers-user'] = sprintf( |
| 565 |
'<a class="activitypub-followers" href="%1$s" title="%2$s">%3$s</a>', |
| 566 |
\esc_url( \admin_url( 'users.php?page=activitypub-followers-list' ) ), |
| 567 |
\esc_attr__( 'Your followers', 'activitypub' ), |
| 568 |
\esc_html( $follower_count ) |
| 569 |
); |
| 570 |
} |
| 571 |
|
| 572 |
if ( ! is_user_type_disabled( 'blog' ) && current_user_can( 'manage_options' ) ) { |
| 573 |
$follower_count = sprintf( |
| 574 |
// translators: %s: number of followers. |
| 575 |
_n( |
| 576 |
'%s Follower (Blog)', |
| 577 |
'%s Followers (Blog)', |
| 578 |
count_followers( Actors::BLOG_USER_ID ), |
| 579 |
'activitypub' |
| 580 |
), |
| 581 |
\number_format_i18n( count_followers( Actors::BLOG_USER_ID ) ) |
| 582 |
); |
| 583 |
$items['activitypub-followers-blog'] = sprintf( |
| 584 |
'<a class="activitypub-followers" href="%1$s" title="%2$s">%3$s</a>', |
| 585 |
\esc_url( \admin_url( 'options-general.php?page=activitypub&tab=followers' ) ), |
| 586 |
\esc_attr__( 'The Blog\'s followers', 'activitypub' ), |
| 587 |
\esc_html( $follower_count ) |
| 588 |
); |
| 589 |
} |
| 590 |
|
| 591 |
\remove_filter( 'number_format_i18n', '\Activitypub\custom_large_numbers' ); |
| 592 |
|
| 593 |
return $items; |
| 594 |
} |
| 595 |
|
| 596 |
/** |
| 597 |
* Add a "Fediverse Preview ⁂" link to the row actions. |
| 598 |
* |
| 599 |
* @param array $actions The existing actions. |
| 600 |
* @param \WP_Post $post The post object. |
| 601 |
* |
| 602 |
* @return array The modified actions. |
| 603 |
*/ |
| 604 |
public static function row_actions( $actions, $post ) { |
| 605 |
// check if the post is enabled for ActivityPub. |
| 606 |
if ( |
| 607 |
! \post_type_supports( \get_post_type( $post ), 'activitypub' ) || |
| 608 |
! in_array( $post->post_status, array( 'pending', 'draft', 'future', 'publish' ), true ) || |
| 609 |
! \current_user_can( 'edit_post', $post->ID ) || |
| 610 |
ACTIVITYPUB_CONTENT_VISIBILITY_LOCAL === get_content_visibility( $post->ID ) || |
| 611 |
( site_supports_blocks() && \use_block_editor_for_post_type( $post->post_type ) ) |
| 612 |
) { |
| 613 |
return $actions; |
| 614 |
} |
| 615 |
|
| 616 |
$preview_url = add_query_arg( 'activitypub', 'true', \get_preview_post_link( $post ) ); |
| 617 |
|
| 618 |
$actions['activitypub'] = sprintf( |
| 619 |
'<a href="%s" target="_blank">%s</a>', |
| 620 |
\esc_url( $preview_url ), |
| 621 |
\esc_html__( 'Fediverse Preview ⁂', 'activitypub' ) |
| 622 |
); |
| 623 |
|
| 624 |
return $actions; |
| 625 |
} |
| 626 |
|
| 627 |
/** |
| 628 |
* Add plugin settings link. |
| 629 |
* |
| 630 |
* @param array $actions The current actions. |
| 631 |
*/ |
| 632 |
public static function add_plugin_settings_link( $actions ) { |
| 633 |
$actions[] = \sprintf( |
| 634 |
'<a href="%1s">%2s</a>', |
| 635 |
\menu_page_url( 'activitypub', false ), |
| 636 |
\__( 'Settings', 'activitypub' ) |
| 637 |
); |
| 638 |
|
| 639 |
return $actions; |
| 640 |
} |
| 641 |
|
| 642 |
/** |
| 643 |
* Display plugin upgrade notice to users. |
| 644 |
* |
| 645 |
* @param array $data The plugin data. |
| 646 |
* @param object $update The plugin update data. |
| 647 |
*/ |
| 648 |
public static function plugin_update_message( $data, $update ) { |
| 649 |
if ( ! isset( $update->upgrade_notice ) ) { |
| 650 |
return; |
| 651 |
} |
| 652 |
|
| 653 |
echo '<br>' . wp_strip_all_tags( $update->upgrade_notice ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 654 |
} |
| 655 |
|
| 656 |
/** |
| 657 |
* Adds meta box on wp-admin/tools.php. |
| 658 |
*/ |
| 659 |
public static function tool_box() { |
| 660 |
\load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/toolbox.php' ); |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* Open the help tab. |
| 665 |
* |
| 666 |
* This function is used to open the help tab, |
| 667 |
* it is triggered by the hash in the URL. |
| 668 |
*/ |
| 669 |
public static function open_help_tab() { |
| 670 |
// get all tabs registered for the ActivityPub settings page. |
| 671 |
$tabs = \get_current_screen()->get_help_tabs(); |
| 672 |
$ids = \array_values( \wp_list_pluck( $tabs, 'id' ) ); |
| 673 |
$ids = \array_map( |
| 674 |
function ( $id ) { |
| 675 |
return '#tab-link-' . $id; |
| 676 |
}, |
| 677 |
$ids |
| 678 |
); |
| 679 |
?> |
| 680 |
<script type="text/javascript"> |
| 681 |
function activitypub_open_help_tab(event) { |
| 682 |
const allowed_ids = <?php echo \wp_json_encode( $ids ); ?>; |
| 683 |
|
| 684 |
if ( allowed_ids.includes( window.location.hash ) ) { |
| 685 |
const delay = ( event && event.type === 'hashchange' ) ? 0 : 200; |
| 686 |
|
| 687 |
setTimeout( function() { |
| 688 |
document.getElementById( 'contextual-help-link' ).click(); |
| 689 |
document.querySelector( window.location.hash + ' > a[href^="#tab-panel-"]' ).click(); |
| 690 |
}, delay ); |
| 691 |
} |
| 692 |
} |
| 693 |
window.addEventListener( 'DOMContentLoaded', activitypub_open_help_tab ); |
| 694 |
window.addEventListener( 'hashchange', activitypub_open_help_tab ); |
| 695 |
</script> |
| 696 |
<?php |
| 697 |
} |
| 698 |
|
| 699 |
/** |
| 700 |
* Add Dashboard widgets. |
| 701 |
*/ |
| 702 |
public static function add_dashboard_widgets() { |
| 703 |
\wp_add_dashboard_widget( 'activitypub_blog', \__( 'ActivityPub Plugin News', 'activitypub' ), array( self::class, 'blog_dashboard_widget' ) ); |
| 704 |
if ( user_can_activitypub( \get_current_user_id() ) && ! is_user_type_disabled( 'user' ) ) { |
| 705 |
\wp_add_dashboard_widget( 'activitypub_profile', \__( 'ActivityPub Author profile', 'activitypub' ), array( self::class, 'profile_dashboard_widget' ) ); |
| 706 |
} |
| 707 |
if ( ! is_user_type_disabled( 'blog' ) ) { |
| 708 |
\wp_add_dashboard_widget( 'activitypub_blog_profile', \__( 'ActivityPub Blog profile', 'activitypub' ), array( self::class, 'blogprofile_dashboard_widget' ) ); |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
/** |
| 713 |
* Add the `ActivityPub.blog` feed as a Dashboard widget. |
| 714 |
*/ |
| 715 |
public static function blog_dashboard_widget() { |
| 716 |
echo '<div class="rss-widget">'; |
| 717 |
\wp_widget_rss_output( |
| 718 |
array( |
| 719 |
'url' => 'https://activitypub.blog/feed/', |
| 720 |
'items' => 3, |
| 721 |
'show_summary' => 1, |
| 722 |
'show_author' => 0, |
| 723 |
'show_date' => 1, |
| 724 |
) |
| 725 |
); |
| 726 |
echo '</div>'; |
| 727 |
} |
| 728 |
|
| 729 |
/** |
| 730 |
* Add the ActivityPub Author profile as a Dashboard widget. |
| 731 |
*/ |
| 732 |
public static function profile_dashboard_widget() { |
| 733 |
$user = Actors::get_by_id( \get_current_user_id() ); |
| 734 |
?> |
| 735 |
<p> |
| 736 |
<?php \esc_html_e( 'People can follow you by using your author name:', 'activitypub' ); ?> |
| 737 |
</p> |
| 738 |
<p><label for="activitypub-user-identifier"><?php \esc_html_e( 'Username', 'activitypub' ); ?></label><input type="text" class="large-text code" id="activitypub-user-identifier" value="<?php echo \esc_attr( $user->get_webfinger() ); ?>" readonly /></p> |
| 739 |
<p><label for="activitypub-user-url"><?php \esc_html_e( 'Profile URL', 'activitypub' ); ?></label><input type="text" class="large-text code" id="activitypub-user-url" value="<?php echo \esc_attr( $user->get_url() ); ?>" readonly /></p> |
| 740 |
<p> |
| 741 |
<?php \esc_html_e( 'Authors who can not access this settings page will find their username on the "Edit Profile" page.', 'activitypub' ); ?> |
| 742 |
<a href="<?php echo \esc_url( \admin_url( '/profile.php#activitypub' ) ); ?>"> |
| 743 |
<?php \esc_html_e( 'Customize username on "Edit Profile" page.', 'activitypub' ); ?> |
| 744 |
</a> |
| 745 |
</p> |
| 746 |
<?php |
| 747 |
} |
| 748 |
|
| 749 |
/** |
| 750 |
* Add the ActivityPub Blog profile as a Dashboard widget. |
| 751 |
*/ |
| 752 |
public static function blogprofile_dashboard_widget() { |
| 753 |
$user = new Blog(); |
| 754 |
?> |
| 755 |
<p> |
| 756 |
<?php \esc_html_e( 'People can follow your blog by using:', 'activitypub' ); ?> |
| 757 |
</p> |
| 758 |
<p><label for="activitypub-user-identifier"><?php \esc_html_e( 'Username', 'activitypub' ); ?></label><input type="text" class="large-text code" id="activitypub-user-identifier" value="<?php echo \esc_attr( $user->get_webfinger() ); ?>" readonly /></p> |
| 759 |
<p><label for="activitypub-user-url"><?php \esc_html_e( 'Profile URL', 'activitypub' ); ?></label><input type="text" class="large-text code" id="activitypub-user-url" value="<?php echo \esc_attr( $user->get_url() ); ?>" readonly /></p> |
| 760 |
<p> |
| 761 |
<?php \esc_html_e( 'This blog profile will federate all posts written on your blog, regardless of the author who posted it.', 'activitypub' ); ?> |
| 762 |
<?php if ( current_user_can( 'manage_options' ) ) : ?> |
| 763 |
<a href="<?php echo \esc_url( \admin_url( '/options-general.php?page=activitypub&tab=blog-profile' ) ); ?>"> |
| 764 |
<?php \esc_html_e( 'Customize the blog profile.', 'activitypub' ); ?> |
| 765 |
</a> |
| 766 |
<?php endif; ?> |
| 767 |
</p> |
| 768 |
<?php |
| 769 |
} |
| 770 |
} |
| 771 |
|