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