PluginProbe
ActivityPub / 1.0.0
ActivityPub v1.0.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 / includes / class-blocks.php

class-blocks.php in ActivityPub 1.0.0, at includes/class-blocks.php

123 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub;
3
4 use Activitypub\Collection\Followers;
5 use Activitypub\is_user_type_disabled;
6
7 class Blocks {
8 public static function init() {
9 // this is already being called on the init hook, so just add it.
10 self::register_blocks();
11 \add_action( 'wp_enqueue_scripts', array( self::class, 'add_data' ) );
12 \add_action( 'enqueue_block_editor_assets', array( self::class, 'add_data' ) );
13 }
14
15 public static function add_data() {
16 $context = is_admin() ? 'editor' : 'view';
17 $followers_handle = 'activitypub-followers-' . $context . '-script';
18 $follow_me_handle = 'activitypub-follow-me-' . $context . '-script';
19 $data = array(
20 'namespace' => ACTIVITYPUB_REST_NAMESPACE,
21 'enabled' => array(
22 'site' => ! is_user_type_disabled( 'blog' ),
23 'users' => ! is_user_type_disabled( 'user' ),
24 ),
25 );
26 $js = sprintf( 'var _activityPubOptions = %s;', wp_json_encode( $data ) );
27 \wp_add_inline_script( $followers_handle, $js, 'before' );
28 \wp_add_inline_script( $follow_me_handle, $js, 'before' );
29 }
30
31 public static function register_blocks() {
32 \register_block_type_from_metadata(
33 ACTIVITYPUB_PLUGIN_DIR . '/build/followers',
34 array(
35 'render_callback' => array( self::class, 'render_follower_block' ),
36 )
37 );
38 \register_block_type_from_metadata(
39 ACTIVITYPUB_PLUGIN_DIR . '/build/follow-me',
40 array(
41 'render_callback' => array( self::class, 'render_follow_me_block' ),
42 )
43 );
44 }
45
46 private static function get_user_id( $user_string ) {
47 if ( is_numeric( $user_string ) ) {
48 return absint( $user_string );
49 }
50 // any other non-numeric falls back to 0, including the `site` string used in the UI
51 return 0;
52 }
53
54 /**
55 * Render the follow me block.
56 * @param array $attrs The block attributes.
57 * @return string The HTML to render.
58 */
59 public static function render_follow_me_block( $attrs ) {
60 $wrapper_attributes = get_block_wrapper_attributes(
61 array(
62 'aria-label' => __( 'Follow me on the Fediverse', 'activitypub' ),
63 'class' => 'activitypub-follow-me-block-wrapper',
64 'data-attrs' => wp_json_encode( $attrs ),
65 )
66 );
67 // todo: render more than an empty div?
68 return '<div ' . $wrapper_attributes . '></div>';
69 }
70
71 public static function render_follower_block( $attrs ) {
72 $followee_user_id = self::get_user_id( $attrs['selectedUser'] );
73 $per_page = absint( $attrs['per_page'] );
74 $followers = Followers::get_followers( $followee_user_id, $per_page );
75 $title = $attrs['title'];
76 $wrapper_attributes = get_block_wrapper_attributes(
77 array(
78 'aria-label' => __( 'Fediverse Followers', 'activitypub' ),
79 'class' => 'activitypub-follower-block',
80 'data-attrs' => wp_json_encode( $attrs ),
81 )
82 );
83
84 $html = '<div ' . $wrapper_attributes . '>';
85 if ( $title ) {
86 $html .= '<h3>' . $title . '</h3>';
87 }
88 $html .= '<ul>';
89 foreach ( $followers as $follower ) {
90 $html .= '<li>' . self::render_follower( $follower ) . '</li>';
91 }
92 // We are only pagination on the JS side. Could be revisited but we gotta ship!
93 $html .= '</ul></div>';
94 return $html;
95 }
96
97 public static function render_follower( $follower ) {
98 $external_svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" class="components-external-link__icon css-rvs7bx esh4a730" aria-hidden="true" focusable="false"><path d="M18.2 17c0 .7-.6 1.2-1.2 1.2H7c-.7 0-1.2-.6-1.2-1.2V7c0-.7.6-1.2 1.2-1.2h3.2V4.2H7C5.5 4.2 4.2 5.5 4.2 7v10c0 1.5 1.2 2.8 2.8 2.8h10c1.5 0 2.8-1.2 2.8-2.8v-3.6h-1.5V17zM14.9 3v1.5h3.7l-6.4 6.4 1.1 1.1 6.4-6.4v3.7h1.5V3h-6.3z"></path></svg>';
99 $template =
100 '<a href="%s" title="%s" class="components-external-link activitypub-link" target="_blank" rel="external noreferrer noopener">
101 <img width="40" height="40" src="%s" class="avatar activitypub-avatar" />
102 <span class="activitypub-actor">
103 <strong class="activitypub-name">%s</strong>
104 <span class="sep">/</span>
105 <span class="activitypub-handle">@%s</span>
106 </span>
107 %s
108 </a>';
109
110 $data = $follower->to_array();
111
112 return sprintf(
113 $template,
114 esc_url( $data['url'] ),
115 esc_attr( $data['name'] ),
116 esc_attr( $data['icon']['url'] ),
117 esc_html( $data['name'] ),
118 esc_html( $data['preferredUsername'] ),
119 $external_svg
120 );
121 }
122 }
123