true, 'single' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ) ); } } /** * Enqueue the block editor assets. */ public static function enqueue_editor_assets() { // Check for our supported post types. $current_screen = \get_current_screen(); $ap_post_types = \get_post_types_by_support( 'activitypub' ); if ( ! $current_screen || ! in_array( $current_screen->post_type, $ap_post_types, true ) ) { return; } $asset_data = include ACTIVITYPUB_PLUGIN_DIR . 'build/editor-plugin/plugin.asset.php'; $plugin_url = plugins_url( 'build/editor-plugin/plugin.js', ACTIVITYPUB_PLUGIN_FILE ); wp_enqueue_script( 'activitypub-block-editor', $plugin_url, $asset_data['dependencies'], $asset_data['version'], true ); } /** * Enqueue the reply handle script if the in_reply_to GET param is set. */ public static function handle_in_reply_to_get_param() { // Only load the script if the in_reply_to GET param is set, action happens there, not here. // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( ! isset( $_GET['in_reply_to'] ) ) { return; } $asset_data = include ACTIVITYPUB_PLUGIN_DIR . 'build/reply-intent/plugin.asset.php'; $plugin_url = plugins_url( 'build/reply-intent/plugin.js', ACTIVITYPUB_PLUGIN_FILE ); wp_enqueue_script( 'activitypub-reply-intent', $plugin_url, $asset_data['dependencies'], $asset_data['version'], true ); } /** * Add data to the block editor. */ public static function add_data() { $context = is_admin() ? 'editor' : 'view'; $followers_handle = 'activitypub-followers-' . $context . '-script'; $follow_me_handle = 'activitypub-follow-me-' . $context . '-script'; $data = array( 'namespace' => 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' ); } /** * Register the blocks. */ 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 $data The array to filter. * @param array $keys The keys to keep. * @return array The filtered array. */ protected static function filter_array_by_keys( $data, $keys ) { return array_intersect_key( $data, 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 '
'; } /** * Render the follower block. * * @param array $attrs The block attributes. * * @return string The HTML to render. */ 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 string The HTML to render. */ public static function render_reply_block( $attrs ) { /** * Filter the reply block. * * @param string $html The HTML to render. * @param array $attrs The block attributes. */ 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 ); } /** * Render a follower. * * @param \Activitypub\Model\Follower $follower The follower to render. * * @return string The HTML to render. */ 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 ); } }