true, 'single' => true, 'type' => 'string', 'sanitize_callback' => function ( $warning ) { if ( $warning ) { return \sanitize_text_field( $warning ); } return null; }, ) ); \register_post_meta( $post_type, 'activitypub_content_visibility', array( 'type' => 'string', 'single' => true, 'show_in_rest' => true, 'sanitize_callback' => function ( $value ) { $schema = array( 'type' => 'string', 'enum' => array( ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC, ACTIVITYPUB_CONTENT_VISIBILITY_QUIET_PUBLIC, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE, ACTIVITYPUB_CONTENT_VISIBILITY_LOCAL ), 'default' => ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC, ); if ( is_wp_error( rest_validate_enum( $value, $schema, '' ) ) ) { return $schema['default']; } return $value; }, ) ); } } /** * 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 ); } /** * Output ActivityPub options as a script tag. */ public static function inject_activitypub_options() { $data = array( 'namespace' => ACTIVITYPUB_REST_NAMESPACE, 'defaultAvatarUrl' => ACTIVITYPUB_PLUGIN_URL . 'assets/img/mp.jpg', 'enabled' => array( 'site' => ! is_user_type_disabled( 'blog' ), 'users' => ! is_user_type_disabled( 'user' ), ), ); printf( "\n", wp_json_encode( $data ) ); } /** * 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' ), ) ); \register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . '/build/reactions', array( 'render_callback' => array( self::class, 'render_post_reactions_block' ), ) ); } /** * Render the post reactions block. * * @param array $attrs The block attributes. * * @return string The HTML to render. */ public static function render_post_reactions_block( $attrs ) { if ( ! isset( $attrs['postId'] ) ) { $attrs['postId'] = get_the_ID(); } $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'activitypub-reactions-block', 'data-attrs' => wp_json_encode( $attrs ), ) ); return sprintf( '
', $wrapper_attributes ); } /** * 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 Actors::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 Actors::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 = Actors::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( '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 = Actors::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 = '', 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://' ), '', esc_url( $attrs['url'] ) ) ) ); } $html .= '
.*?
#is', $data['post_content'], $matches ); $blocks = \array_map( function ( $paragraph ) { return '' . PHP_EOL . $paragraph . PHP_EOL . '' . PHP_EOL; }, $matches[0] ?? array() ); $data['post_content'] = \rtrim( \implode( PHP_EOL, $blocks ), PHP_EOL ); // Add reply block if it's a reply. if ( null !== $post->object->inReplyTo ) { $reply_block = \sprintf( '' . PHP_EOL, \esc_url( $post->object->inReplyTo ) ); $data['post_content'] = $reply_block . $data['post_content']; } return $data; } }