PluginProbe
ActivityPub / 9.2.0
ActivityPub v9.2.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-settings.php

class-settings.php in ActivityPub 9.2.0, at includes/wp-admin/class-settings.php

456 lines 16.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Settings file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\WP_Admin;
9
10 use Activitypub\Collection\Actors;
11
12 use function Activitypub\user_can_activitypub;
13
14 /**
15 * ActivityPub Settings Class.
16 */
17 class Settings {
18 /**
19 * Initialize the class, registering WordPress hooks,
20 */
21 public static function init() {
22 \add_action( 'admin_menu', array( self::class, 'add_settings_page' ) );
23
24 \add_action( 'load-settings_page_activitypub', array( self::class, 'handle_welcome_query_arg' ) );
25 \add_action( 'load-settings_page_activitypub', array( self::class, 'handle_switch_object_type' ) );
26 }
27
28 /**
29 * Load settings page.
30 */
31 public static function settings_page() {
32 $show_welcome_tab = \get_user_meta( \get_current_user_id(), 'activitypub_show_welcome_tab', true );
33 $show_advanced_tab = \get_user_meta( \get_current_user_id(), 'activitypub_show_advanced_tab', true );
34 $settings_tabs = array();
35 $settings_tab = array(
36 'label' => \__( 'Settings', 'activitypub' ),
37 'template' => ACTIVITYPUB_PLUGIN_DIR . 'templates/settings.php',
38 );
39
40 if ( $show_welcome_tab ) {
41 $settings_tabs['welcome'] = array(
42 'label' => \__( 'Welcome', 'activitypub' ),
43 'template' => ACTIVITYPUB_PLUGIN_DIR . 'templates/welcome.php',
44 );
45 }
46
47 $settings_tabs['settings'] = $settings_tab;
48
49 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
50 if ( ( isset( $_GET['tab'] ) && 'advanced' === $_GET['tab'] ) || $show_advanced_tab ) {
51 $settings_tabs['advanced'] = array(
52 'label' => \__( 'Advanced', 'activitypub' ),
53 'template' => ACTIVITYPUB_PLUGIN_DIR . 'templates/advanced-settings.php',
54 );
55 }
56
57 // Add blocked actors tab for site-wide blocking.
58 $settings_tabs['blocked-actors'] = array(
59 'label' => \__( 'Blocked Actors', 'activitypub' ),
60 'template' => ACTIVITYPUB_PLUGIN_DIR . 'templates/blocked-actors-list.php',
61 );
62
63 if ( user_can_activitypub( Actors::BLOG_USER_ID ) ) {
64 $settings_tabs['blog-profile'] = array(
65 'label' => \__( 'Blog Profile', 'activitypub' ),
66 'template' => ACTIVITYPUB_PLUGIN_DIR . 'templates/blog-settings.php',
67 );
68 $settings_tabs['followers'] = array(
69 'label' => \__( 'Followers', 'activitypub' ),
70 'template' => ACTIVITYPUB_PLUGIN_DIR . 'templates/followers-list.php',
71 );
72
73 if ( '1' === \get_option( 'activitypub_following_ui', '0' ) ) {
74 $settings_tabs['following'] = array(
75 'label' => \__( 'Following', 'activitypub' ),
76 'template' => ACTIVITYPUB_PLUGIN_DIR . 'templates/following-list.php',
77 );
78 }
79 }
80
81 /**
82 * Filters the tabs displayed in the ActivityPub settings.
83 *
84 * @param array $settings_tabs The tabs to display.
85 */
86 $settings_tabs = \apply_filters( 'activitypub_admin_settings_tabs', $settings_tabs );
87
88 if ( empty( $settings_tabs ) ) {
89 \_doing_it_wrong( __FUNCTION__, 'No settings tabs found. There should be at least one tab to show a settings page.', '7.0.0' );
90 $settings_tabs['settings'] = $settings_tab;
91 }
92
93 $tab_keys = \array_keys( $settings_tabs );
94 $default_tab = \reset( $tab_keys );
95 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
96 $tab = isset( $_GET['tab'] ) ? \sanitize_key( $_GET['tab'] ) : $default_tab;
97
98 if ( ! isset( $settings_tabs[ $tab ] ) ) {
99 $tab = $default_tab;
100 }
101
102 switch ( $tab ) {
103 case 'blog-profile':
104 \wp_enqueue_media();
105 \wp_enqueue_script( 'activitypub-header-image' );
106 break;
107 case 'settings':
108 \update_option( 'activitypub_checklist_settings_visited', '1' );
109 break;
110 default:
111 if ( isset( $_GET['help-tab'] ) && 'getting-started' === $_GET['help-tab'] ) { // phpcs:ignore WordPress.Security.NonceVerification
112 \update_option( 'activitypub_checklist_fediverse_intro_visited', '1' );
113 } elseif ( isset( $_GET['help-tab'] ) && 'editor-blocks' === $_GET['help-tab'] ) { // phpcs:ignore WordPress.Security.NonceVerification
114 \update_option( 'activitypub_checklist_blocks_visited', '1' );
115 }
116 break;
117 }
118
119 // Only show tabs if there are more than one.
120 $labels = array();
121 if ( \count( $settings_tabs ) > 1 ) {
122 $labels = \wp_list_pluck( $settings_tabs, 'label' );
123 }
124
125 $args = \array_fill_keys( \array_keys( $labels ), '' );
126 $args[ $tab ] = 'active';
127 $args['tabs'] = $labels;
128
129 \load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/admin-header.php', true, $args );
130 \load_template( $settings_tabs[ $tab ]['template'] );
131 }
132
133 /**
134 * Adds the ActivityPub settings to the Help tab.
135 */
136 public static function add_settings_help_tab() {
137 // Getting Started / Introduction to the Fediverse.
138 \get_current_screen()->add_help_tab(
139 array(
140 'id' => 'getting-started',
141 'title' => \__( 'Getting Started', 'activitypub' ),
142 'content' => self::get_help_tab_template( 'getting-started' ),
143 )
144 );
145
146 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
147 if ( 'following' === \sanitize_text_field( \wp_unslash( $_GET['tab'] ?? '' ) ) ) {
148 self::add_following_help_tab();
149 }
150
151 // Core Features.
152 \get_current_screen()->add_help_tab(
153 array(
154 'id' => 'core-features',
155 'title' => \__( 'Core Features', 'activitypub' ),
156 'content' => self::get_help_tab_template( 'core-features' ),
157 )
158 );
159
160 // Editor Blocks.
161 \get_current_screen()->add_help_tab(
162 array(
163 'id' => 'editor-blocks',
164 'title' => \__( 'Editor Blocks', 'activitypub' ),
165 'content' => self::get_help_tab_template( 'editor-blocks' ),
166 )
167 );
168
169 // Account Migration.
170 \get_current_screen()->add_help_tab(
171 array(
172 'id' => 'account-migration',
173 'title' => \__( 'Account Migration', 'activitypub' ),
174 'content' => self::get_help_tab_template( 'account-migration' ),
175 )
176 );
177
178 // Show only if templating is enabled.
179 $object_type = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );
180 if ( 'note' === $object_type ) {
181 // Template Tags.
182 \get_current_screen()->add_help_tab(
183 array(
184 'id' => 'template-tags',
185 'title' => \__( 'Template Tags', 'activitypub' ),
186 'content' => self::get_help_tab_template( 'template-tags' ),
187 )
188 );
189 }
190
191 // Recommended Plugins.
192 if ( ! empty( self::get_recommended_plugins() ) ) {
193 \get_current_screen()->add_help_tab(
194 array(
195 'id' => 'recommended-plugins',
196 'title' => \__( 'Recommended Plugins', 'activitypub' ),
197 'content' =>
198 '<h2>' . \esc_html__( 'Supercharge Your Fediverse Experience', 'activitypub' ) . '</h2>' .
199 '<p>' . \esc_html__( 'Enhance your WordPress ActivityPub setup with these hand-picked plugins, each adding unique capabilities for a richer Fediverse experience.', 'activitypub' ) . '</p>' .
200 self::render_recommended_plugins_list(),
201 )
202 );
203 }
204
205 // Troubleshooting.
206 \get_current_screen()->add_help_tab(
207 array(
208 'id' => 'troubleshooting',
209 'title' => \__( 'Troubleshooting', 'activitypub' ),
210 'content' => self::get_help_tab_template( 'troubleshooting' ),
211 )
212 );
213
214 // Glossary.
215 \get_current_screen()->add_help_tab(
216 array(
217 'id' => 'glossary',
218 'title' => \__( 'Glossary', 'activitypub' ),
219 'content' => self::get_help_tab_template( 'glossary' ),
220 )
221 );
222
223 // Resources.
224 \get_current_screen()->add_help_tab(
225 array(
226 'id' => 'resources',
227 'title' => \__( 'Resources', 'activitypub' ),
228 'content' => self::get_help_tab_template( 'resources' ),
229 )
230 );
231
232 // Enhanced Help Sidebar.
233 \get_current_screen()->set_help_sidebar(
234 '<p><strong>' . \__( 'For more information:', 'activitypub' ) . '</strong></p>' . "\n" .
235 '<p><a href="https://wordpress.org/support/plugin/activitypub/">' . \esc_html__( 'Get support', 'activitypub' ) . '</a></p>' . "\n" .
236 '<p><a href="https://github.com/Automattic/wordpress-activitypub/issues">' . \esc_html__( 'Report an issue', 'activitypub' ) . '</a></p>' . "\n" .
237 '<p><a href="https://github.com/Automattic/wordpress-activitypub/tree/trunk/docs">' . \esc_html__( 'Documentation', 'activitypub' ) . '</a></p>' . "\n" .
238 '<p><a href="https://github.com/Automattic/wordpress-activitypub/releases">' . \esc_html__( 'View latest changes', 'activitypub' ) . '</a></p>'
239 );
240 }
241
242 /**
243 * Adds the ActivityPub help tab to the users page.
244 */
245 public static function add_following_help_tab() {
246 \get_current_screen()->add_help_tab(
247 array(
248 'id' => 'starter-kit',
249 'title' => \__( 'Starter Kits', 'activitypub' ),
250 'content' => \sprintf(
251 '<h2>%s</h2>' .
252 '<p>%s</p>' .
253 '<p>%s</p>',
254 \__( 'Starter Kits', 'activitypub' ),
255 \__( 'Starter kits are curated lists of accounts that help you quickly build your fediverse network. Import a starter kit to automatically follow a collection of interesting accounts in specific topics or communities.', 'activitypub' ),
256 // translators: %s: Importer URL.
257 \wp_kses_post( \sprintf( \__( 'To import a starter kit, go to <strong>Tools &#8594; Import</strong> and look for <a href="%s">the &#8220;Starter Kit&#8221; option</a>.', 'activitypub' ), \admin_url( 'admin.php?import=starter-kit' ) ) )
258 ),
259 )
260 );
261 }
262
263 /**
264 * Adds the ActivityPub help tab to the users page.
265 */
266 public static function add_users_help_tab() {
267 \get_current_screen()->add_help_tab(
268 array(
269 'id' => 'activitypub',
270 'title' => \__( 'ActivityPub', 'activitypub' ),
271 'content' => self::get_help_tab_template( 'users' ),
272 // Add to the end of the list.
273 'priority' => 20,
274 )
275 );
276 }
277
278 /**
279 * Handle 'welcome' query arg.
280 */
281 public static function handle_welcome_query_arg() {
282 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
283 if ( isset( $_GET['welcome'] ) ) {
284 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
285 $welcome_checked = empty( \sanitize_text_field( \wp_unslash( $_GET['welcome'] ) ) ) ? 0 : 1;
286 \update_user_meta( \get_current_user_id(), 'activitypub_show_welcome_tab', $welcome_checked );
287 \wp_safe_redirect( \admin_url( 'options-general.php?page=activitypub&tab=settings' ) );
288 exit;
289 }
290 }
291
292 /**
293 * Handle switching from legacy template mode to automatic object type.
294 *
295 * @since 8.0.0
296 */
297 public static function handle_switch_object_type() {
298 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
299 if ( ! isset( $_GET['activitypub_update_object_type'] ) ) {
300 return;
301 }
302
303 \check_admin_referer( 'activitypub_switch_object_type' );
304
305 if ( ! \current_user_can( 'manage_options' ) ) {
306 return;
307 }
308
309 \update_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );
310
311 \wp_safe_redirect( \admin_url( 'options-general.php?page=activitypub&tab=settings' ) );
312 exit;
313 }
314
315 /**
316 * Returns an array of recommended plugins for ActivityPub.
317 */
318 public static function get_recommended_plugins() {
319 $plugins = array();
320
321 if ( ! \is_plugin_active( 'friends/friends.php' ) ) {
322 $plugins['friends'] = array(
323 'slug' => 'friends',
324 'author' => 'Alex Kirk',
325 'author_url' => 'https://profiles.wordpress.org/akirk/',
326 'icon' => 'https://ps.w.org/friends/assets/icon-256x256.png',
327 'name' => \__( 'Friends', 'activitypub' ),
328 'description' => \__( 'Follow people on Mastodon or similar platforms and display their posts on your WordPress, making your site a true Fediverse instance.', 'activitypub' ),
329 'install_url' => \admin_url( 'plugin-install.php?tab=plugin-information&plugin=friends&TB_iframe=true' ),
330 );
331 }
332
333 if ( ! \is_plugin_active( 'event-bridge-for-activitypub/event-bridge-for-activitypub.php' ) ) {
334 $plugins['event_bridge'] = array(
335 'slug' => 'event-bridge-for-activitypub',
336 'author' => 'André Menrath',
337 'author_url' => 'https://profiles.wordpress.org/andremenrath/',
338 'icon' => 'https://ps.w.org/event-bridge-for-activitypub/assets/icon-256x256.gif',
339 'name' => \__( 'Event Bridge for ActivityPub', 'activitypub' ),
340 'description' => \__( 'Make your events discoverable and federate them across decentralized platforms like Mastodon or Gancio.', 'activitypub' ),
341 'install_url' => \admin_url( 'plugin-install.php?tab=plugin-information&plugin=event-bridge-for-activitypub&TB_iframe=true' ),
342 );
343 }
344
345 if ( ! \is_plugin_active( 'enable-mastodon-apps/enable-mastodon-apps.php' ) ) {
346 $plugins['enable_mastodon_apps'] = array(
347 'slug' => 'enable-mastodon-apps',
348 'author' => 'Alex Kirk',
349 'author_url' => 'https://profiles.wordpress.org/akirk/',
350 'icon' => 'https://ps.w.org/enable-mastodon-apps/assets/icon-256x256.png',
351 'name' => \__( 'Enable Mastodon Apps', 'activitypub' ),
352 'description' => \__( 'Allow Mastodon apps to interact with your WordPress site, letting you write posts from your favorite app.', 'activitypub' ),
353 'install_url' => \admin_url( 'plugin-install.php?tab=plugin-information&plugin=enable-mastodon-apps&TB_iframe=true' ),
354 );
355 }
356
357 if ( ! \is_plugin_active( 'hum/hum.php' ) ) {
358 $plugins['hum'] = array(
359 'slug' => 'hum',
360 'author' => 'Will Norris',
361 'author_url' => 'https://profiles.wordpress.org/willnorris/',
362 'icon' => 'https://s.w.org/plugins/geopattern-icon/hum.svg',
363 'name' => \__( 'Hum', 'activitypub' ),
364 'description' => \__( 'A personal URL shortener for WordPress, perfect for sharing short links on the Fediverse.', 'activitypub' ),
365 'install_url' => \admin_url( 'plugin-install.php?tab=plugin-information&plugin=hum&TB_iframe=true' ),
366 );
367 }
368
369 if ( ! \is_plugin_active( 'webfinger/webfinger.php' ) ) {
370 $plugins['webfinger'] = array(
371 'slug' => 'webfinger',
372 'author' => 'Matthias Pfefferle',
373 'author_url' => 'https://profiles.wordpress.org/pfefferle/',
374 'icon' => 'https://ps.w.org/webfinger/assets/icon-256x256.png',
375 'name' => \__( 'WebFinger', 'activitypub' ),
376 'description' => \__( 'WebFinger protocol support for better discovery and compatibility.', 'activitypub' ),
377 'install_url' => \admin_url( 'plugin-install.php?tab=plugin-information&plugin=webfinger&TB_iframe=true' ),
378 );
379 }
380
381 if ( ! \is_plugin_active( 'nodeinfo/nodeinfo.php' ) ) {
382 $plugins['nodeinfo'] = array(
383 'slug' => 'nodeinfo',
384 'author' => 'Matthias Pfefferle',
385 'author_url' => 'https://profiles.wordpress.org/pfefferle/',
386 'icon' => 'https://ps.w.org/nodeinfo/assets/icon-256x256.png',
387 'name' => \__( 'NodeInfo', 'activitypub' ),
388 'description' => \__( 'Advanced NodeInfo protocol support for better discovery and compatibility.', 'activitypub' ),
389 'install_url' => \admin_url( 'plugin-install.php?tab=plugin-information&plugin=nodeinfo&TB_iframe=true' ),
390 );
391 }
392
393 return $plugins;
394 }
395
396 /**
397 * Render recommended plugins as a beautiful, rich showcase for the help tab.
398 */
399 public static function render_recommended_plugins_list() {
400 $plugins = self::get_recommended_plugins();
401
402 \ob_start();
403
404 echo '<div class="plugin-list widefat">';
405
406 foreach ( $plugins as $plugin ) :
407 ?>
408 <div class="plugin-card plugin-card-<?php echo \esc_attr( $plugin['slug'] ); ?>">
409 <div class="plugin-card-top">
410 <div class="name column-name">
411 <h3>
412 <a href="<?php echo \esc_url( $plugin['install_url'] ); ?>" class="thickbox open-plugin-details-modal">
413 <?php echo \esc_html( $plugin['name'] ); ?>
414 <img src="<?php echo \esc_url( $plugin['icon'] ); ?>" class="plugin-icon" alt="">
415 </a>
416 </h3>
417 </div>
418 <div class="action-links">
419 <ul class="plugin-action-buttons">
420 <li>
421 <a href="<?php echo \esc_url( $plugin['install_url'] ); ?>" class="button thickbox open-plugin-details-modal"><?php \esc_html_e( 'More Details', 'activitypub' ); ?></a>
422 </li>
423 </ul>
424 </div>
425 <div class="desc column-description">
426 <p><?php echo \esc_html( $plugin['description'] ); ?></p>
427 <p class="authors"> <cite>By <a href="<?php echo \esc_url( $plugin['author_url'] ); ?>"><?php echo \esc_html( $plugin['author'] ); ?></a></cite></p>
428 </div>
429 </div>
430 </div>
431 <?php
432 endforeach;
433
434 echo '</div>';
435
436 return \ob_get_clean();
437 }
438
439 /**
440 * Loads a help tab template.
441 *
442 * @param string $template_name The template file name (without ".php").
443 * @return string Rendered template output.
444 */
445 private static function get_help_tab_template( $template_name ) {
446 $template_path = ACTIVITYPUB_PLUGIN_DIR . 'templates/help-tab/' . $template_name . '.php';
447 if ( ! \file_exists( $template_path ) ) {
448 return '';
449 }
450
451 \ob_start();
452 \load_template( $template_path, false );
453 return \ob_get_clean();
454 }
455 }
456