Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
wpcomsh_activitypub_sync_plugin_activation
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
7.01
1<?php
2/**
3 * ActivityPub plugin compatibility file.
4 *
5 * @package wpcomsh
6 */
7
8use Automattic\Jetpack\Connection\Manager;
9
10/**
11 * Pass extra data to WordPress.com when the ActivityPub plugin is activated.
12 *
13 * Hooked at priority 20 on the `jetpack_sync_before_enqueue_activated_plugin` filter,
14 * which means the Sync Plugins module's `expand_plugin_data()` (priority 10) has
15 * already expanded the original positional args into the three-element array below.
16 *
17 * When the activated plugin is ActivityPub, this function appends a fourth element
18 * containing the Jetpack connection owner's ActivityPub actor URI and WebFinger handle
19 * so they can be synced to WordPress.com.
20 *
21 * @param array|false $args {
22 *     Positional activated_plugin hook arguments. False if a previous filter aborted.
23 *
24 *     @type string $0 Plugin path relative to the plugins directory (e.g. 'activitypub/activitypub.php').
25 *     @type bool   $1 Whether the plugin was network-activated. Default false.
26 *     @type array  $2 Plugin header data added by `expand_plugin_data()` (keys: 'name', 'version').
27 *     @type array  $3 Optional. Added by this function when the plugin is ActivityPub.
28 *                     Contains 'actor' (URI) and 'WebFinger' (acct handle).
29 * }
30 *
31 * @return array|false The (possibly augmented) args, or false if a previous filter aborted.
32 */
33function wpcomsh_activitypub_sync_plugin_activation( $args ) {
34    if ( ! is_array( $args ) || ! isset( $args[0] ) ) {
35        return $args;
36    }
37
38    $plugin_name = 'activitypub/activitypub.php';
39    if ( $plugin_name !== $args[0] ) {
40        return $args;
41    }
42
43    if ( ! class_exists( 'Activitypub\Collection\Actors' ) ) {
44        return $args;
45    }
46
47    $connection_owner_id = ( new Manager() )->get_connection_owner_id();
48    if ( ! $connection_owner_id ) {
49        return $args;
50    }
51
52    // @phan-suppress-next-line PhanUndeclaredClassMethod We're checking the class exists above, and that class exists in the ActivityPub plugin.
53    $actor = Activitypub\Collection\Actors::get_by_id( $connection_owner_id );
54    if ( ! is_wp_error( $actor ) ) {
55        $args[] = array(
56            'actor'     => $actor->get_id(),
57            'webfinger' => $actor->get_webfinger(),
58        );
59    }
60
61    return $args;
62}
63add_filter( 'jetpack_sync_before_enqueue_activated_plugin', 'wpcomsh_activitypub_sync_plugin_activation', 20, 1 );