PluginProbe
ActivityPub / 9.0.0
ActivityPub v9.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 / wp-admin / class-dashboard.php

class-dashboard.php in ActivityPub 9.0.0, at includes/wp-admin/class-dashboard.php

215 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Dashboard Widgets Class.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\WP_Admin;
9
10 use Activitypub\Collection\Actors;
11 use Activitypub\Model\Blog;
12
13 use function Activitypub\is_user_type_disabled;
14 use function Activitypub\user_can_activitypub;
15
16 /**
17 * Dashboard Widgets Class.
18 *
19 * Provides all ActivityPub dashboard widgets.
20 */
21 class Dashboard {
22
23 /**
24 * Initialize the class, registering WordPress hooks.
25 */
26 public static function init() {
27 \add_action( 'wp_dashboard_setup', array( self::class, 'add_dashboard_widgets' ) );
28 \add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_scripts' ) );
29 }
30
31 /**
32 * Add Dashboard widgets.
33 */
34 public static function add_dashboard_widgets() {
35 // Plugin news widget.
36 \wp_add_dashboard_widget(
37 'activitypub_blog',
38 \__( 'ActivityPub Plugin News', 'activitypub' ),
39 array( self::class, 'render_news_widget' )
40 );
41
42 // Author profile widget.
43 if ( user_can_activitypub( \get_current_user_id() ) && ! is_user_type_disabled( 'user' ) ) {
44 \wp_add_dashboard_widget(
45 'activitypub_profile',
46 \__( 'ActivityPub Author Profile', 'activitypub' ),
47 array( self::class, 'render_author_profile_widget' )
48 );
49 }
50
51 // Blog profile widget.
52 if ( ! is_user_type_disabled( 'blog' ) ) {
53 \wp_add_dashboard_widget(
54 'activitypub_blog_profile',
55 \__( 'ActivityPub Blog Profile', 'activitypub' ),
56 array( self::class, 'render_blog_profile_widget' )
57 );
58 }
59
60 // Stats widget.
61 if ( self::user_can_see_stats() ) {
62 \wp_add_dashboard_widget(
63 'activitypub_stats',
64 \__( 'Fediverse Stats', 'activitypub' ),
65 array( self::class, 'render_stats_widget' ),
66 null,
67 null,
68 'normal',
69 'high'
70 );
71 }
72 }
73
74 /**
75 * Enqueue scripts for the dashboard widgets.
76 *
77 * @param string $hook The current admin page.
78 */
79 public static function enqueue_scripts( $hook ) {
80 if ( 'index.php' !== $hook ) {
81 return;
82 }
83
84 // Only enqueue if user has access to stats.
85 if ( ! self::user_can_see_stats() ) {
86 return;
87 }
88
89 $asset_file = ACTIVITYPUB_PLUGIN_DIR . 'build/dashboard-stats/index.asset.php';
90
91 if ( ! \file_exists( $asset_file ) ) {
92 return;
93 }
94
95 $asset = require $asset_file;
96
97 $dependencies = $asset['dependencies'];
98 $dependencies[] = 'wp-dom-ready';
99
100 \wp_enqueue_script(
101 'activitypub-dashboard-stats',
102 \plugins_url( 'build/dashboard-stats/index.js', ACTIVITYPUB_PLUGIN_FILE ),
103 $dependencies,
104 $asset['version'],
105 true
106 );
107
108 /*
109 * Pass the resolved REST URLs to the JS so it uses get_rest_url() — which
110 * is filtered on environments like WordPress.com that remap the REST
111 * namespace — rather than a hardcoded `/activitypub/1.0/…` path that only
112 * works on the default namespace.
113 */
114 \wp_localize_script(
115 'activitypub-dashboard-stats',
116 'activitypubDashboardStats',
117 array(
118 'blogStatsUrl' => \get_rest_url( null, ACTIVITYPUB_REST_NAMESPACE . '/admin/stats/' . Actors::BLOG_USER_ID ),
119 'userStatsUrl' => \get_rest_url( null, ACTIVITYPUB_REST_NAMESPACE . '/admin/stats/' ),
120 )
121 );
122
123 \wp_enqueue_style(
124 'activitypub-dashboard-stats',
125 \plugins_url( 'build/dashboard-stats/style-index.css', ACTIVITYPUB_PLUGIN_FILE ),
126 array( 'wp-components' ),
127 $asset['version']
128 );
129
130 // Add inline script to initialize the widget.
131 \wp_add_inline_script(
132 'activitypub-dashboard-stats',
133 'wp.domReady( function() { if ( activitypub && activitypub.dashboardStats ) { activitypub.dashboardStats.initialize( "activitypub-stats-widget-root" ); } } );'
134 );
135 }
136
137 /**
138 * Check if the current user can see the stats widget.
139 *
140 * @return bool True if user has access.
141 */
142 private static function user_can_see_stats() {
143 $has_user_access = user_can_activitypub( \get_current_user_id() ) && ! is_user_type_disabled( 'user' );
144 $has_blog_access = ! is_user_type_disabled( 'blog' ) && \current_user_can( 'manage_options' );
145
146 return $has_user_access || $has_blog_access;
147 }
148
149 /**
150 * Render the ActivityPub.blog news feed widget.
151 */
152 public static function render_news_widget() {
153 echo '<div class="rss-widget">';
154 \wp_widget_rss_output(
155 array(
156 'url' => 'https://activitypub.blog/feed/',
157 'items' => 3,
158 'show_summary' => 1,
159 'show_author' => 0,
160 'show_date' => 1,
161 )
162 );
163 echo '</div>';
164 }
165
166 /**
167 * Render the ActivityPub Author profile widget.
168 */
169 public static function render_author_profile_widget() {
170 $user = Actors::get_by_id( \get_current_user_id() );
171 ?>
172 <p>
173 <?php \esc_html_e( 'People can follow you by using your author name:', 'activitypub' ); ?>
174 </p>
175 <p><label for="activitypub-user-identifier"><?php \esc_html_e( 'Username', 'activitypub' ); ?></label><input type="text" class="large-text code" id="activitypub-user-identifier" value="<?php echo \esc_attr( $user->get_webfinger() ); ?>" readonly /></p>
176 <p><label for="activitypub-user-url"><?php \esc_html_e( 'Profile URL', 'activitypub' ); ?></label><input type="text" class="large-text code" id="activitypub-user-url" value="<?php echo \esc_attr( $user->get_url() ); ?>" readonly /></p>
177 <p>
178 <?php \esc_html_e( 'Authors who can not access this settings page will find their username on the "Edit Profile" page.', 'activitypub' ); ?>
179 <a href="<?php echo \esc_url( \admin_url( '/profile.php#activitypub' ) ); ?>">
180 <?php \esc_html_e( 'Customize username on "Edit Profile" page.', 'activitypub' ); ?>
181 </a>
182 </p>
183 <?php
184 }
185
186 /**
187 * Render the ActivityPub Blog profile widget.
188 */
189 public static function render_blog_profile_widget() {
190 $user = new Blog();
191 ?>
192 <p>
193 <?php \esc_html_e( 'People can follow your blog by using:', 'activitypub' ); ?>
194 </p>
195 <p><label for="activitypub-user-identifier"><?php \esc_html_e( 'Username', 'activitypub' ); ?></label><input type="text" class="large-text code" id="activitypub-user-identifier" value="<?php echo \esc_attr( $user->get_webfinger() ); ?>" readonly /></p>
196 <p><label for="activitypub-user-url"><?php \esc_html_e( 'Profile URL', 'activitypub' ); ?></label><input type="text" class="large-text code" id="activitypub-user-url" value="<?php echo \esc_attr( $user->get_url() ); ?>" readonly /></p>
197 <p>
198 <?php \esc_html_e( 'This blog profile will federate all posts written on your blog, regardless of the author who posted it.', 'activitypub' ); ?>
199 <?php if ( \current_user_can( 'manage_options' ) ) : ?>
200 <a href="<?php echo \esc_url( \admin_url( '/options-general.php?page=activitypub&tab=blog-profile' ) ); ?>">
201 <?php \esc_html_e( 'Customize the blog profile.', 'activitypub' ); ?>
202 </a>
203 <?php endif; ?>
204 </p>
205 <?php
206 }
207
208 /**
209 * Render the stats widget container.
210 */
211 public static function render_stats_widget() {
212 echo '<div id="activitypub-stats-widget-root"></div>';
213 }
214 }
215