ACTIVITYPUB_REST_NAMESPACE, 'enabled' => array( 'site' => ! is_user_type_disabled( 'blog' ), 'users' => ! is_user_type_disabled( 'user' ), ), ); $js = sprintf( 'var _activityPubOptions = %s;', wp_json_encode( $data ) ); \wp_add_inline_script( $followers_handle, $js, 'before' ); \wp_add_inline_script( $follow_me_handle, $js, 'before' ); } public static function register_blocks() { \register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . '/build/followers', array( 'render_callback' => array( self::class, 'render_follower_block' ), ) ); \register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . '/build/follow-me', array( 'render_callback' => array( self::class, 'render_follow_me_block' ), ) ); \register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . '/build/reply', array( 'render_callback' => array( self::class, 'render_reply_block' ), ) ); } /** * Get the user ID from a user string. * * @param string $user_string The user string. Can be a user ID, 'site', or 'inherit'. * @return int|null The user ID, or null if the 'inherit' string is not supported in this context. */ private static function get_user_id( $user_string ) { if ( is_numeric( $user_string ) ) { return absint( $user_string ); } // if the user string is 'site', return the Blog User ID. if ( 'site' === $user_string ) { return User_Collection::BLOG_USER_ID; } // The only other value should be 'inherit', which means to use the query context to determine the User. if ( 'inherit' !== $user_string ) { return null; } // For a homepage/front page, if the Blog User is active, use it. if ( ( is_front_page() || is_home() ) && ! is_user_type_disabled( 'blog' ) ) { return User_Collection::BLOG_USER_ID; } // If we're in a loop, use the post author $author_id = get_the_author_meta( 'ID' ); if ( $author_id ) { return $author_id; } // For other pages, the queried object will clue us in. $queried_object = get_queried_object(); if ( ! $queried_object ) { return null; } // If we're on a user archive page, use that user's ID. if ( is_a( $queried_object, 'WP_User' ) ) { return $queried_object->ID; } // For a single post, use the post author's ID. if ( is_a( $queried_object, 'WP_Post' ) ) { return get_the_author_meta( 'ID' ); } // We won't properly account for some conditions, like tag archives. return null; } /** * Filter an array by a list of keys. * @param array $array The array to filter. * @param array $keys The keys to keep. * @return array The filtered array. */ protected static function filter_array_by_keys( $array, $keys ) { return array_intersect_key( $array, array_flip( $keys ) ); } /** * Render the follow me block. * @param array $attrs The block attributes. * @return string The HTML to render. */ public static function render_follow_me_block( $attrs ) { $user_id = self::get_user_id( $attrs['selectedUser'] ); $user = User_Collection::get_by_id( $user_id ); if ( is_wp_error( $user ) ) { if ( 'inherit' === $attrs['selectedUser'] ) { // If the user is 'inherit' and we couldn't determine the user, don't render anything. return ''; } else { // If the user is a specific ID and we couldn't find it, render an error message. return ''; } } $attrs['profileData'] = self::filter_array_by_keys( $user->to_array(), array( 'icon', 'name', 'webfinger' ) ); $wrapper_attributes = get_block_wrapper_attributes( array( 'aria-label' => __( 'Follow me on the Fediverse', 'activitypub' ), 'class' => 'activitypub-follow-me-block-wrapper', 'data-attrs' => wp_json_encode( $attrs ), ) ); // todo: render more than an empty div? return '
'; } public static function render_follower_block( $attrs ) { $followee_user_id = self::get_user_id( $attrs['selectedUser'] ); if ( is_null( $followee_user_id ) ) { return ''; } $user = User_Collection::get_by_id( $followee_user_id ); if ( is_wp_error( $user ) ) { return ''; } $per_page = absint( $attrs['per_page'] ); $follower_data = Followers::get_followers_with_count( $followee_user_id, $per_page ); $attrs['followerData']['total'] = $follower_data['total']; $attrs['followerData']['followers'] = array_map( function ( $follower ) { return self::filter_array_by_keys( $follower->to_array(), array( 'icon', 'name', 'preferredUsername', 'url' ) ); }, $follower_data['followers'] ); $wrapper_attributes = get_block_wrapper_attributes( array( 'aria-label' => __( 'Fediverse Followers', 'activitypub' ), 'class' => 'activitypub-follower-block', 'data-attrs' => wp_json_encode( $attrs ), ) ); $html = '
'; if ( $attrs['title'] ) { $html .= '

' . esc_html( $attrs['title'] ) . '

'; } $html .= '
'; return $html; } /** * Render the reply block. * * @param array $attrs The block attributes. * * @return void */ public static function render_reply_block( $attrs ) { return apply_filters( 'activitypub_reply_block', sprintf( '

%3$s

', esc_url( $attrs['url'] ), esc_attr__( 'This post is a response to the referenced content.', 'activitypub' ), // translators: %s is the URL of the post being replied to. sprintf( __( '↬%s', 'activitypub' ), \str_replace( array( 'https://', 'http://' ), '', $attrs['url'] ) ) ), $attrs ); } public static function render_follower( $follower ) { $external_svg = ''; $template = ' %s / @%s %s '; $data = $follower->to_array(); return sprintf( $template, esc_url( object_to_uri( $data['url'] ) ), esc_attr( $data['name'] ), esc_attr( $data['icon']['url'] ), esc_html( $data['name'] ), esc_html( $data['preferredUsername'] ), $external_svg ); } }