PluginProbe
ActivityPub / trunk
ActivityPub vtrunk
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / wp-admin / class-heartbeat.php

class-heartbeat.php in ActivityPub trunk, at includes/wp-admin/class-heartbeat.php

109 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ActivityPub Heartbeat API Integration.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\WP_Admin;
9
10 use Activitypub\Collection\Actors;
11 use Activitypub\Collection\Following;
12
13 /**
14 * Heartbeat API integration for ActivityPub.
15 */
16 class Heartbeat {
17
18 /**
19 * Initialize the Heartbeat API integration.
20 */
21 public static function init() {
22 \add_action( 'admin_print_scripts-settings_page_activitypub', array( self::class, 'enqueue_scripts' ) );
23 \add_action( 'admin_print_scripts-users_page_activitypub-following-list', array( self::class, 'enqueue_scripts' ) );
24
25 \add_filter( 'heartbeat_received', array( self::class, 'heartbeat_received' ), 10, 2 );
26 }
27
28 /**
29 * Enqueue scripts and localize data for the Following list table.
30 */
31 public static function enqueue_scripts() {
32 $tab = \sanitize_text_field( \wp_unslash( $_GET['tab'] ?? 'welcome' ) ); // phpcs:ignore WordPress.Security.NonceVerification
33 if ( \get_current_screen()->id === 'settings_page_activitypub' && 'following' !== $tab ) {
34 return;
35 }
36
37 // Get the current user ID.
38 $user_id = \get_current_screen()->id === 'settings_page_activitypub'
39 ? Actors::BLOG_USER_ID
40 : \get_current_user_id();
41
42 // Bail if there are no pending follows.
43 if ( 0 === Following::count_pending( $user_id ) ) {
44 return;
45 }
46
47 // Enqueue the script.
48 \wp_enqueue_script(
49 'activitypub-following',
50 \plugins_url( 'assets/js/activitypub-following.js', ACTIVITYPUB_PLUGIN_FILE ),
51 array( 'jquery', 'heartbeat' ),
52 ACTIVITYPUB_PLUGIN_VERSION,
53 true
54 );
55
56 // Localize the script with necessary data.
57 \wp_localize_script(
58 'activitypub-following',
59 'ActivityPubFollowingSettings',
60 array( 'user_id' => $user_id )
61 );
62 }
63
64 /**
65 * Handle the Heartbeat API received event.
66 *
67 * @param array $response The Heartbeat response.
68 * @param array $data The Heartbeat data.
69 * @return array The filtered Heartbeat response.
70 */
71 public static function heartbeat_received( $response, $data ) {
72 // Check if this is our data.
73 if ( empty( $data['activitypub_following_check'] ) ) {
74 return $response;
75 }
76
77 $user_id = \absint( $data['activitypub_following_check']['user_id'] ?? 0 );
78 $pending_ids = \array_map( 'absint', $data['activitypub_following_check']['pending_ids'] ?? array() );
79
80 // Verify user can view this data.
81 if ( ! \current_user_can( 'edit_user', $user_id ) ) {
82 return $response;
83 }
84
85 // Initialize the response.
86 $response['activitypub_following'] = array(
87 'counts' => Following::count_by_status( $user_id ),
88 'message' => \__( 'Follow requests updated.', 'activitypub' ),
89 'no_items' => \__( 'No profiles found.', 'activitypub' ),
90 'updated_items' => array(),
91 );
92
93 // Check for status changes in the pending follow requests.
94 foreach ( $pending_ids as $post_id ) {
95 $status = Following::check_status( $user_id, $post_id );
96
97 // If the status has changed from pending to accepted.
98 if ( Following::ACCEPTED === $status ) {
99 $response['activitypub_following']['updated_items'][] = array(
100 'id' => $post_id,
101 'status' => $status,
102 );
103 }
104 }
105
106 return $response;
107 }
108 }
109