| 1 |
<?php |
| 2 |
/** |
| 3 |
* Jetpack integration file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Integration; |
| 9 |
|
| 10 |
use Activitypub\Collection\Followers; |
| 11 |
use Activitypub\Collection\Following; |
| 12 |
use Activitypub\Http; |
| 13 |
use Automattic\Jetpack\Connection\Manager; |
| 14 |
|
| 15 |
use function Activitypub\is_activity_object; |
| 16 |
|
| 17 |
/** |
| 18 |
* Jetpack integration class. |
| 19 |
*/ |
| 20 |
class Jetpack { |
| 21 |
|
| 22 |
/** |
| 23 |
* Initialize the class, registering WordPress hooks. |
| 24 |
*/ |
| 25 |
public static function init() { |
| 26 |
if ( ! \defined( 'IS_WPCOM' ) ) { |
| 27 |
\add_filter( 'jetpack_sync_options_whitelist', array( self::class, 'add_sync_options' ) ); |
| 28 |
\add_filter( 'jetpack_sync_post_meta_whitelist', array( self::class, 'add_sync_meta' ) ); |
| 29 |
\add_filter( 'jetpack_sync_comment_meta_whitelist', array( self::class, 'add_sync_comment_meta' ) ); |
| 30 |
\add_filter( 'jetpack_sync_whitelisted_comment_types', array( self::class, 'add_comment_types' ) ); |
| 31 |
\add_filter( 'jetpack_json_api_comment_types', array( self::class, 'add_comment_types' ) ); |
| 32 |
\add_filter( 'jetpack_api_include_comment_types_count', array( self::class, 'add_comment_types' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
if ( |
| 36 |
( \defined( 'IS_WPCOM' ) && IS_WPCOM ) || |
| 37 |
( \class_exists( '\Automattic\Jetpack\Connection\Manager' ) && ( new Manager() )->is_user_connected() ) |
| 38 |
) { |
| 39 |
\add_filter( 'activitypub_following_row_actions', array( self::class, 'add_reader_link' ), 10, 2 ); |
| 40 |
\add_filter( 'pre_option_activitypub_following_ui', array( self::class, 'pre_option_activitypub_following_ui' ) ); |
| 41 |
} |
| 42 |
|
| 43 |
\add_action( 'load-post-new.php', array( self::class, 'adapt_post_share' ) ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Add ActivityPub options to the Jetpack sync allow list. |
| 48 |
* |
| 49 |
* @since 8.1.0 |
| 50 |
* |
| 51 |
* @param array $allow_list The Jetpack sync options allow list. |
| 52 |
* |
| 53 |
* @return array The allow list with ActivityPub options. |
| 54 |
*/ |
| 55 |
public static function add_sync_options( $allow_list ) { |
| 56 |
$allow_list[] = 'activitypub_blog_identifier'; |
| 57 |
$allow_list[] = 'activitypub_blog_description'; |
| 58 |
$allow_list[] = 'activitypub_header_image'; |
| 59 |
$allow_list[] = 'activitypub_actor_mode'; |
| 60 |
|
| 61 |
return $allow_list; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Add ActivityPub meta keys to the Jetpack sync allow list. |
| 66 |
* |
| 67 |
* @param array $allow_list The Jetpack sync allow list. |
| 68 |
* |
| 69 |
* @return array The Jetpack sync allow list with ActivityPub meta keys. |
| 70 |
*/ |
| 71 |
public static function add_sync_meta( $allow_list ) { |
| 72 |
$allow_list[] = Followers::FOLLOWER_META_KEY; |
| 73 |
$allow_list[] = Following::FOLLOWING_META_KEY; |
| 74 |
|
| 75 |
return $allow_list; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Add ActivityPub comment meta keys to the Jetpack sync allow list. |
| 80 |
* |
| 81 |
* @param array $allow_list The Jetpack sync allow list. |
| 82 |
* |
| 83 |
* @return array The Jetpack sync allow list with ActivityPub comment meta keys. |
| 84 |
*/ |
| 85 |
public static function add_sync_comment_meta( $allow_list ) { |
| 86 |
$allow_list[] = 'avatar_url'; |
| 87 |
|
| 88 |
return $allow_list; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Add custom comment types to the list of comment types. |
| 93 |
* |
| 94 |
* @param array $comment_types Default comment types. |
| 95 |
* |
| 96 |
* @return array The comment types with ActivityPub types added. |
| 97 |
*/ |
| 98 |
public static function add_comment_types( $comment_types ) { |
| 99 |
$comment_types[] = 'like'; |
| 100 |
$comment_types[] = 'quote'; |
| 101 |
$comment_types[] = 'repost'; |
| 102 |
|
| 103 |
return array_unique( $comment_types ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Add a "Reader" link to the bulk actions dropdown on the following list screen. |
| 108 |
* |
| 109 |
* @param array $actions The bulk actions. |
| 110 |
* @param array $item The current following item. |
| 111 |
* |
| 112 |
* @return array The bulk actions with the "Reader" link. |
| 113 |
*/ |
| 114 |
public static function add_reader_link( $actions, $item ) { |
| 115 |
// Do not show the link for pending follow requests. |
| 116 |
if ( 'pending' === $item['status'] ) { |
| 117 |
return $actions; |
| 118 |
} |
| 119 |
|
| 120 |
$feed = \get_post_meta( $item['id'], '_activitypub_actor_feed', true ); |
| 121 |
|
| 122 |
// Generate Reader URL based on environment. |
| 123 |
if ( \defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 124 |
if ( empty( $feed['feed_id'] ) ) { |
| 125 |
return $actions; // No feed_id available on WPCOM. |
| 126 |
} |
| 127 |
$url = sprintf( 'https://wordpress.com/reader/feeds/%d', (int) $feed['feed_id'] ); |
| 128 |
} else { |
| 129 |
$url = sprintf( 'https://wordpress.com/reader/feeds/lookup/%s', rawurlencode( $item['identifier'] ) ); |
| 130 |
} |
| 131 |
|
| 132 |
return array_merge( |
| 133 |
array( |
| 134 |
'reader' => sprintf( |
| 135 |
'<a href="%1$s" target="_blank">%2$s<span class="screen-reader-text"> %3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>', |
| 136 |
esc_url( $url ), |
| 137 |
esc_html__( 'View Feed', 'activitypub' ), |
| 138 |
/* translators: Hidden accessibility text. */ |
| 139 |
esc_html__( '(opens in a new tab)', 'activitypub' ) |
| 140 |
), |
| 141 |
), |
| 142 |
$actions |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Force the ActivityPub Following UI to be enabled when Jetpack is active. |
| 148 |
* |
| 149 |
* @return string '1' to enable the ActivityPub Following UI. |
| 150 |
*/ |
| 151 |
public static function pre_option_activitypub_following_ui() { |
| 152 |
return '1'; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Adapt the parameters for a post share request to be compatible with the Federated Reply block. |
| 157 |
*/ |
| 158 |
public static function adapt_post_share() { |
| 159 |
if ( ! isset( $_GET['is_post_share'], $_GET['url'] ) || ! $_GET['is_post_share'] ) { // phpcs:ignore WordPress.Security |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
$url = \sanitize_url( \wp_unslash( $_GET['url'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 164 |
|
| 165 |
if ( is_activity_object( Http::get_remote_object( $url ) ) ) { |
| 166 |
$args = array( |
| 167 |
'post_type' => 'post', |
| 168 |
'in_reply_to' => $url, |
| 169 |
); |
| 170 |
|
| 171 |
\wp_safe_redirect( \add_query_arg( $args, \admin_url( 'post-new.php' ) ) ); |
| 172 |
exit; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|