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' ), ) ); } private static function get_user_id( $user_string ) { if ( is_numeric( $user_string ) ) { return absint( $user_string ); } // any other non-numeric falls back to 0, including the `site` string used in the UI return 0; } /** * 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 ) ) { $attrs['profileData'] = self::filter_array_by_keys( $user->to_array(), array( 'icon', 'name', 'resource' ) ); } $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'] ); $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 = '