PluginProbe
ActivityPub / 7.7.0
ActivityPub v7.7.0
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 / integration / class-jetpack.php

class-jetpack.php in ActivityPub 7.7.0, at integration/class-jetpack.php

157 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_post_meta_whitelist', array( self::class, 'add_sync_meta' ) );
28 \add_filter( 'jetpack_sync_comment_meta_whitelist', array( self::class, 'add_sync_comment_meta' ) );
29 \add_filter( 'jetpack_sync_whitelisted_comment_types', array( self::class, 'add_comment_types' ) );
30 \add_filter( 'jetpack_json_api_comment_types', array( self::class, 'add_comment_types' ) );
31 \add_filter( 'jetpack_api_include_comment_types_count', array( self::class, 'add_comment_types' ) );
32 }
33
34 if (
35 ( \defined( 'IS_WPCOM' ) && IS_WPCOM ) ||
36 ( \class_exists( '\Automattic\Jetpack\Connection\Manager' ) && ( new Manager() )->is_user_connected() )
37 ) {
38 \add_filter( 'activitypub_following_row_actions', array( self::class, 'add_reader_link' ), 10, 2 );
39 \add_filter( 'pre_option_activitypub_following_ui', array( self::class, 'pre_option_activitypub_following_ui' ) );
40 }
41
42 \add_action( 'load-post-new.php', array( self::class, 'adapt_post_share' ) );
43 }
44
45 /**
46 * Add ActivityPub meta keys to the Jetpack sync allow list.
47 *
48 * @param array $allow_list The Jetpack sync allow list.
49 *
50 * @return array The Jetpack sync allow list with ActivityPub meta keys.
51 */
52 public static function add_sync_meta( $allow_list ) {
53 $allow_list[] = Followers::FOLLOWER_META_KEY;
54 $allow_list[] = Following::FOLLOWING_META_KEY;
55
56 return $allow_list;
57 }
58
59 /**
60 * Add ActivityPub comment meta keys to the Jetpack sync allow list.
61 *
62 * @param array $allow_list The Jetpack sync allow list.
63 *
64 * @return array The Jetpack sync allow list with ActivityPub comment meta keys.
65 */
66 public static function add_sync_comment_meta( $allow_list ) {
67 $allow_list[] = 'avatar_url';
68
69 return $allow_list;
70 }
71
72 /**
73 * Add custom comment types to the list of comment types.
74 *
75 * @param array $comment_types Default comment types.
76 *
77 * @return array The comment types with ActivityPub types added.
78 */
79 public static function add_comment_types( $comment_types ) {
80 $comment_types[] = 'like';
81 $comment_types[] = 'quote';
82 $comment_types[] = 'repost';
83
84 return array_unique( $comment_types );
85 }
86
87 /**
88 * Add a "Reader" link to the bulk actions dropdown on the following list screen.
89 *
90 * @param array $actions The bulk actions.
91 * @param array $item The current following item.
92 *
93 * @return array The bulk actions with the "Reader" link.
94 */
95 public static function add_reader_link( $actions, $item ) {
96 // Do not show the link for pending follow requests.
97 if ( 'pending' === $item['status'] ) {
98 return $actions;
99 }
100
101 $feed = \get_post_meta( $item['id'], '_activitypub_actor_feed', true );
102
103 // Generate Reader URL based on environment.
104 if ( \defined( 'IS_WPCOM' ) && IS_WPCOM ) {
105 if ( empty( $feed['feed_id'] ) ) {
106 return $actions; // No feed_id available on WPCOM.
107 }
108 $url = sprintf( 'https://wordpress.com/reader/feeds/%d', (int) $feed['feed_id'] );
109 } else {
110 $url = sprintf( 'https://wordpress.com/reader/feeds/lookup/%s', rawurlencode( $item['identifier'] ) );
111 }
112
113 return array_merge(
114 array(
115 'reader' => sprintf(
116 '<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>',
117 esc_url( $url ),
118 esc_html__( 'View Feed', 'activitypub' ),
119 /* translators: Hidden accessibility text. */
120 esc_html__( '(opens in a new tab)', 'activitypub' )
121 ),
122 ),
123 $actions
124 );
125 }
126
127 /**
128 * Force the ActivityPub Following UI to be enabled when Jetpack is active.
129 *
130 * @return string '1' to enable the ActivityPub Following UI.
131 */
132 public static function pre_option_activitypub_following_ui() {
133 return '1';
134 }
135
136 /**
137 * Adapt the parameters for a post share request to be compatible with the Federated Reply block.
138 */
139 public static function adapt_post_share() {
140 if ( ! isset( $_GET['is_post_share'], $_GET['url'] ) || ! $_GET['is_post_share'] ) { // phpcs:ignore WordPress.Security
141 return;
142 }
143
144 $url = \sanitize_url( \wp_unslash( $_GET['url'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
145
146 if ( is_activity_object( Http::get_remote_object( $url ) ) ) {
147 $args = array(
148 'post_type' => 'post',
149 'in_reply_to' => $url,
150 );
151
152 \wp_safe_redirect( \add_query_arg( $args, \admin_url( 'post-new.php' ) ) );
153 exit;
154 }
155 }
156 }
157