PluginProbe
ActivityPub / 3.2.4
ActivityPub v3.2.4
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 / handler / class-announce.php

class-announce.php in ActivityPub 3.2.4, at includes/handler/class-announce.php

98 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub\Handler;
3
4 use Activitypub\Http;
5 use Activitypub\Comment;
6 use Activitypub\Collection\Interactions;
7
8 use function Activitypub\object_to_uri;
9 use function Activitypub\is_activity_public;
10
11 /**
12 * Handle Create requests
13 */
14 class Announce {
15 /**
16 * Initialize the class, registering WordPress hooks
17 */
18 public static function init() {
19 \add_action(
20 'activitypub_inbox_announce',
21 array( self::class, 'handle_announce' ),
22 10,
23 3
24 );
25 }
26
27 /**
28 * Handles "Announce" requests
29 *
30 * @param array $array The activity-object
31 * @param int $user_id The id of the local blog-user
32 * @param Activitypub\Activity $activity The activity object
33 *
34 * @return void
35 */
36 public static function handle_announce( $array, $user_id, $activity = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
37 // check if Activity is public or not
38 if ( ! is_activity_public( $array ) ) {
39 // @todo maybe send email
40 return;
41 }
42
43 if ( ! ACTIVITYPUB_DISABLE_REACTIONS ) {
44 self::maybe_save_announce( $array, $user_id, $activity );
45 }
46
47 if ( is_string( $array['object'] ) ) {
48 $object = Http::get_remote_object( $array['object'] );
49 } else {
50 $object = $array['object'];
51 }
52
53 if ( ! $object || is_wp_error( $object ) ) {
54 return;
55 }
56
57 if ( ! isset( $object['type'] ) ) {
58 return;
59 }
60
61 $type = \strtolower( $object['type'] );
62
63 \do_action( 'activitypub_inbox', $object, $user_id, $type, $activity );
64 \do_action( "activitypub_inbox_{$type}", $object, $user_id, $activity );
65 }
66
67 /**
68 * Try to save the Announce
69 *
70 * @param array $array The activity-object
71 * @param int $user_id The id of the local blog-user
72 * @param Activitypub\Activity $activity The activity object
73 *
74 * @return void
75 */
76 public static function maybe_save_announce( $array, $user_id, $activity ) { // phpcs:ignore
77 $url = object_to_uri( $array['object'] );
78
79 if ( empty( $url ) ) {
80 return;
81 }
82
83 $exists = Comment::object_id_to_comment( esc_url_raw( $url ) );
84 if ( $exists ) {
85 return;
86 }
87
88 $state = Interactions::add_reaction( $array );
89 $reaction = null;
90
91 if ( $state && ! is_wp_error( $state ) ) {
92 $reaction = get_comment( $state );
93 }
94
95 do_action( 'activitypub_handled_announce', $array, $user_id, $state, $reaction );
96 }
97 }
98