| 1 |
<?php |
| 2 |
/** |
| 3 |
* ActivityPub Welcome Fields Class. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\WP_Admin; |
| 9 |
|
| 10 |
use function Activitypub\user_can_activitypub; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Welcome_Fields. |
| 14 |
*/ |
| 15 |
class Welcome_Fields { |
| 16 |
/** |
| 17 |
* Initialize the welcome fields. |
| 18 |
*/ |
| 19 |
public static function init() { |
| 20 |
\add_action( 'load-settings_page_activitypub', array( self::class, 'register_welcome_fields' ) ); |
| 21 |
|
| 22 |
\add_action( 'add_option_activitypub_checklist_health_check_issues', array( self::class, 'resolve_checklist' ) ); |
| 23 |
\add_action( 'update_option_activitypub_checklist_health_check_issues', array( self::class, 'resolve_checklist' ) ); |
| 24 |
\add_action( 'add_option_activitypub_checklist_fediverse_intro_visited', array( self::class, 'resolve_checklist' ) ); |
| 25 |
\add_action( 'update_option_activitypub_checklist_fediverse_intro_visited', array( self::class, 'resolve_checklist' ) ); |
| 26 |
\add_action( 'add_option_activitypub_checklist_settings_visited', array( self::class, 'resolve_checklist' ) ); |
| 27 |
\add_action( 'update_option_activitypub_checklist_settings_visited', array( self::class, 'resolve_checklist' ) ); |
| 28 |
\add_action( 'add_option_activitypub_checklist_profile_setup_visited', array( self::class, 'resolve_checklist' ) ); |
| 29 |
\add_action( 'update_option_activitypub_checklist_profile_setup_visited', array( self::class, 'resolve_checklist' ) ); |
| 30 |
\add_action( 'add_option_activitypub_checklist_blocks_visited', array( self::class, 'resolve_checklist' ) ); |
| 31 |
\add_action( 'update_option_activitypub_checklist_blocks_visited', array( self::class, 'resolve_checklist' ) ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Register welcome fields. |
| 36 |
*/ |
| 37 |
public static function register_welcome_fields() { |
| 38 |
\add_settings_section( |
| 39 |
'activitypub_welcome_header', |
| 40 |
'', |
| 41 |
array( self::class, 'render_welcome_header_section' ), |
| 42 |
'activitypub_welcome' |
| 43 |
); |
| 44 |
|
| 45 |
\add_settings_section( |
| 46 |
'activitypub_onboarding_steps', |
| 47 |
'', |
| 48 |
array( self::class, 'render_onboarding_steps_section' ), |
| 49 |
'activitypub_welcome' |
| 50 |
); |
| 51 |
|
| 52 |
\add_settings_section( |
| 53 |
'activitypub_welcome_footer', |
| 54 |
'', |
| 55 |
array( self::class, 'render_welcome_footer_section' ), |
| 56 |
'activitypub_welcome' |
| 57 |
); |
| 58 |
|
| 59 |
\add_action( 'activitypub_onboarding_steps', array( self::class, 'render_step_plugin_installed' ), 10 ); |
| 60 |
\add_action( 'activitypub_onboarding_steps', array( self::class, 'render_step_site_health' ), 20 ); |
| 61 |
\add_action( 'activitypub_onboarding_steps', array( self::class, 'render_step_fediverse_intro' ), 30 ); |
| 62 |
\add_action( 'activitypub_onboarding_steps', array( self::class, 'render_step_profile_mode' ), 40 ); |
| 63 |
\add_action( 'activitypub_onboarding_steps', array( self::class, 'render_step_profile_setup' ), 50 ); |
| 64 |
\add_action( 'activitypub_onboarding_steps', array( self::class, 'render_step_features' ), 60 ); |
| 65 |
|
| 66 |
/* |
| 67 |
* Keep track of health check issues. This is used to determine if the site health step is complete. |
| 68 |
* |
| 69 |
* This needs to happen after onboarding steps were registered, so that we can compare the number of completed |
| 70 |
* steps with the number of registered steps. |
| 71 |
*/ |
| 72 |
\update_option( 'activitypub_checklist_health_check_issues', (string) Health_Check::count_results( 'critical' ) ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Render welcome header section. |
| 77 |
*/ |
| 78 |
public static function render_welcome_header_section() { |
| 79 |
$completed_steps = self::get_completed_steps_count(); |
| 80 |
$total_steps = self::get_total_steps_count(); |
| 81 |
$progress_percentage = \min( 100, \round( ( $completed_steps / $total_steps ) * 100 ) ); |
| 82 |
?> |
| 83 |
<div id="activitypub-welcome-checklist" class="activitypub-welcome-container"> |
| 84 |
<div class="activitypub-welcome-header"> |
| 85 |
<div class="activitypub-progress-circle"> |
| 86 |
<div class="activitypub-progress-circle-content"> |
| 87 |
<span><?php echo \esc_html( $completed_steps ); ?>/<?php echo \esc_html( $total_steps ); ?></span> |
| 88 |
</div> |
| 89 |
<svg class="activitypub-progress-ring" width="120" height="120"> |
| 90 |
<circle class="activitypub-progress-ring-bg" cx="60" cy="60" r="54" /> |
| 91 |
<circle class="activitypub-progress-ring-circle" cx="60" cy="60" r="54" |
| 92 |
stroke-dasharray="339.292" |
| 93 |
stroke-dashoffset="<?php echo \esc_attr( 339.292 - ( 339.292 * $progress_percentage / 100 ) ); ?>" /> |
| 94 |
</svg> |
| 95 |
</div> |
| 96 |
<h2 class="activitypub-welcome-title"><?php \esc_html_e( 'Welcome to the Fediverse!', 'activitypub' ); ?></h2> |
| 97 |
<p class="activitypub-welcome-subtitle"><?php \esc_html_e( 'Get connected in just a few steps.', 'activitypub' ); ?></p> |
| 98 |
</div> |
| 99 |
<?php |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get the count of completed steps. |
| 104 |
*/ |
| 105 |
private static function get_completed_steps_count() { |
| 106 |
$count = 1; // Plugin is already installed. |
| 107 |
|
| 108 |
// We're looking for 0 issues. |
| 109 |
if ( self::has_step( 'site_health' ) && '0' === \get_option( 'activitypub_checklist_health_check_issues', (string) Health_Check::count_results( 'critical' ) ) ) { |
| 110 |
++$count; |
| 111 |
} |
| 112 |
|
| 113 |
// Check other completed steps. |
| 114 |
if ( self::has_step( 'fediverse_intro' ) && '1' === \get_option( 'activitypub_checklist_fediverse_intro_visited' ) ) { |
| 115 |
++$count; |
| 116 |
} |
| 117 |
|
| 118 |
if ( self::has_step( 'profile_mode' ) && '1' === \get_option( 'activitypub_checklist_settings_visited' ) ) { |
| 119 |
++$count; |
| 120 |
} |
| 121 |
|
| 122 |
if ( self::has_step( 'profile_setup' ) && '1' === \get_option( 'activitypub_checklist_profile_setup_visited' ) ) { |
| 123 |
++$count; |
| 124 |
} |
| 125 |
|
| 126 |
if ( self::has_step( 'features' ) && '1' === \get_option( 'activitypub_checklist_blocks_visited' ) ) { |
| 127 |
++$count; |
| 128 |
} |
| 129 |
|
| 130 |
return $count; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get the total number of steps. |
| 135 |
*/ |
| 136 |
private static function get_total_steps_count() { |
| 137 |
global $wp_filter; |
| 138 |
|
| 139 |
$count = 0; |
| 140 |
|
| 141 |
if ( isset( $wp_filter['activitypub_onboarding_steps'] ) ) { |
| 142 |
$pattern = '/^' . \preg_quote( self::class, '/' ) . '/'; |
| 143 |
foreach ( $wp_filter['activitypub_onboarding_steps']->callbacks as $callbacks ) { |
| 144 |
$matching_keys = \preg_grep( $pattern, \array_keys( $callbacks ) ); |
| 145 |
$count += \count( $matching_keys ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return $count; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Get the next incomplete step. |
| 154 |
*/ |
| 155 |
private static function get_next_incomplete_step() { |
| 156 |
if ( self::has_step( 'site_health' ) && '0' !== \get_option( 'activitypub_checklist_health_check_issues', (string) Health_Check::count_results( 'critical' ) ) ) { |
| 157 |
return 'site_health'; |
| 158 |
} |
| 159 |
|
| 160 |
if ( self::has_step( 'fediverse_intro' ) && ! \get_option( 'activitypub_checklist_fediverse_intro_visited', false ) ) { |
| 161 |
return 'fediverse_intro'; |
| 162 |
} |
| 163 |
|
| 164 |
if ( self::has_step( 'profile_mode' ) && ! \get_option( 'activitypub_checklist_settings_visited', false ) ) { |
| 165 |
return 'profile_mode'; |
| 166 |
} |
| 167 |
|
| 168 |
if ( self::has_step( 'profile_setup' ) && ! \get_option( 'activitypub_checklist_profile_setup_visited', false ) ) { |
| 169 |
return 'profile_setup'; |
| 170 |
} |
| 171 |
|
| 172 |
if ( self::has_step( 'features' ) && ! \get_option( 'activitypub_checklist_blocks_visited', false ) ) { |
| 173 |
return 'features'; |
| 174 |
} |
| 175 |
|
| 176 |
return ''; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Check if a step exists. |
| 181 |
* |
| 182 |
* @param string $step Step slug. |
| 183 |
* @return bool |
| 184 |
*/ |
| 185 |
private static function has_step( $step ) { |
| 186 |
return \has_action( 'activitypub_onboarding_steps', array( self::class, 'render_step_' . $step ) ); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Render onboarding steps section. |
| 191 |
*/ |
| 192 |
public static function render_onboarding_steps_section() { |
| 193 |
?> |
| 194 |
<div class="activitypub-onboarding-steps"> |
| 195 |
<?php |
| 196 |
\do_action( 'activitypub_onboarding_steps' ); |
| 197 |
?> |
| 198 |
</div> |
| 199 |
<?php |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Render plugin installed step. |
| 204 |
*/ |
| 205 |
public static function render_step_plugin_installed() { |
| 206 |
?> |
| 207 |
<div class="activitypub-onboarding-step activitypub-step-completed"> |
| 208 |
<div class="step-indicator"> |
| 209 |
<span class="step-icon dashicons dashicons-yes"></span> |
| 210 |
</div> |
| 211 |
<div class="step-content"> |
| 212 |
<div class="step-text"> |
| 213 |
<h3><?php \esc_html_e( 'Plugin installed', 'activitypub' ); ?></h3> |
| 214 |
</div> |
| 215 |
</div> |
| 216 |
</div> |
| 217 |
<?php |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Render site health step. |
| 222 |
*/ |
| 223 |
public static function render_step_site_health() { |
| 224 |
$health_issues = Health_Check::count_results(); |
| 225 |
$total_issues = $health_issues['critical'] + $health_issues['recommended']; |
| 226 |
$checked = '0' === \get_option( 'activitypub_checklist_health_check_issues', (string) $health_issues['critical'] ); |
| 227 |
$step_class = $checked ? 'activitypub-step-completed' : ''; |
| 228 |
$next_step = self::get_next_incomplete_step(); |
| 229 |
$button_class = ( 'site_health' === $next_step ) ? 'button-primary' : 'button-secondary'; |
| 230 |
?> |
| 231 |
<div class="activitypub-onboarding-step <?php echo \esc_attr( $step_class ); ?>"> |
| 232 |
<div class="step-indicator"> |
| 233 |
<?php if ( $checked ) : ?> |
| 234 |
<span class="step-icon dashicons dashicons-yes"></span> |
| 235 |
<?php else : ?> |
| 236 |
<span class="step-icon dashicons dashicons-warning"></span> |
| 237 |
<?php endif; ?> |
| 238 |
</div> |
| 239 |
<div class="step-content"> |
| 240 |
<div class="step-text"> |
| 241 |
<h3><?php \esc_html_e( 'Check your site’s health', 'activitypub' ); ?></h3> |
| 242 |
<p> |
| 243 |
<?php |
| 244 |
echo \esc_html( |
| 245 |
\sprintf( |
| 246 |
/* translators: %d: Number of issues. */ |
| 247 |
\_n( '%d issue needs your attention.', '%d issues need your attention.', $total_issues, 'activitypub' ), |
| 248 |
$total_issues |
| 249 |
) |
| 250 |
); |
| 251 |
?> |
| 252 |
</p> |
| 253 |
</div> |
| 254 |
<div class="step-action"> |
| 255 |
<a href="<?php echo \esc_url( \admin_url( 'site-health.php' ) ); ?>" class="button <?php echo \esc_attr( $button_class ); ?>"> |
| 256 |
<?php \esc_html_e( 'Review issues', 'activitypub' ); ?> |
| 257 |
</a> |
| 258 |
</div> |
| 259 |
</div> |
| 260 |
</div> |
| 261 |
<?php |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Render the Fediverse-Intro step. |
| 266 |
*/ |
| 267 |
public static function render_step_fediverse_intro() { |
| 268 |
$checked = '1' === \get_option( 'activitypub_checklist_fediverse_intro_visited', false ); |
| 269 |
$step_class = $checked ? 'activitypub-step-completed' : ''; |
| 270 |
$next_step = self::get_next_incomplete_step(); |
| 271 |
$button_class = ( 'fediverse_intro' === $next_step ) ? 'button-primary' : 'button-secondary'; |
| 272 |
?> |
| 273 |
<div class="activitypub-onboarding-step <?php echo \esc_attr( $step_class ); ?>"> |
| 274 |
<div class="step-indicator"> |
| 275 |
<?php if ( $checked ) : ?> |
| 276 |
<span class="step-icon dashicons dashicons-yes"></span> |
| 277 |
<?php else : ?> |
| 278 |
<span class="step-icon dashicons dashicons-video-alt3"></span> |
| 279 |
<?php endif; ?> |
| 280 |
</div> |
| 281 |
<div class="step-content"> |
| 282 |
<div class="step-text"> |
| 283 |
<h3><?php \esc_html_e( 'New to the Fediverse? Start Here', 'activitypub' ); ?></h3> |
| 284 |
<p><?php \esc_html_e( 'Learn what the Fediverse is and why it matters.', 'activitypub' ); ?></p> |
| 285 |
</div> |
| 286 |
<div class="step-action"> |
| 287 |
<a href="<?php echo \esc_url( \admin_url( 'options-general.php?page=activitypub&help-tab=getting-started#tab-link-getting-started' ) ); ?>" class="button <?php echo \esc_attr( $button_class ); ?>"> |
| 288 |
<?php \esc_html_e( 'View intro', 'activitypub' ); ?> |
| 289 |
</a> |
| 290 |
</div> |
| 291 |
</div> |
| 292 |
</div> |
| 293 |
<?php |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Render the Profile Mode step. |
| 298 |
*/ |
| 299 |
public static function render_step_profile_mode() { |
| 300 |
$checked = '1' === \get_option( 'activitypub_checklist_settings_visited', false ); |
| 301 |
$step_class = $checked ? 'activitypub-step-completed' : ''; |
| 302 |
$next_step = self::get_next_incomplete_step(); |
| 303 |
$button_class = ( 'profile_mode' === $next_step ) ? 'button-primary' : 'button-secondary'; |
| 304 |
?> |
| 305 |
<div class="activitypub-onboarding-step <?php echo \esc_attr( $step_class ); ?>"> |
| 306 |
<div class="step-indicator"> |
| 307 |
<?php if ( $checked ) : ?> |
| 308 |
<span class="step-icon dashicons dashicons-yes"></span> |
| 309 |
<?php else : ?> |
| 310 |
<span class="step-icon dashicons dashicons-groups"></span> |
| 311 |
<?php endif; ?> |
| 312 |
</div> |
| 313 |
<div class="step-content"> |
| 314 |
<div class="step-text"> |
| 315 |
<h3><?php \esc_html_e( 'Select how you want to share', 'activitypub' ); ?></h3> |
| 316 |
<p><?php \esc_html_e( 'Pick your preferred user mode for connecting with others.', 'activitypub' ); ?></p> |
| 317 |
</div> |
| 318 |
<div class="step-action"> |
| 319 |
<a href="<?php echo \esc_url( \admin_url( 'options-general.php?page=activitypub&tab=settings' ) ); ?>" class="button <?php echo \esc_attr( $button_class ); ?>"> |
| 320 |
<?php \esc_html_e( 'Choose mode', 'activitypub' ); ?> |
| 321 |
</a> |
| 322 |
</div> |
| 323 |
</div> |
| 324 |
</div> |
| 325 |
<?php |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Render the Profile Setup step. |
| 330 |
*/ |
| 331 |
public static function render_step_profile_setup() { |
| 332 |
$user_can_activitypub = user_can_activitypub( \get_current_user_id() ); |
| 333 |
$checked = '1' === \get_option( 'activitypub_checklist_profile_setup_visited', false ); |
| 334 |
$step_class = $checked ? 'activitypub-step-completed' : ''; |
| 335 |
$next_step = self::get_next_incomplete_step(); |
| 336 |
$button_class = ( 'profile_setup' === $next_step ) ? 'button-primary' : 'button-secondary'; |
| 337 |
?> |
| 338 |
<div class="activitypub-onboarding-step <?php echo \esc_attr( $step_class ); ?>"> |
| 339 |
<div class="step-indicator"> |
| 340 |
<?php if ( $checked ) : ?> |
| 341 |
<span class="step-icon dashicons dashicons-yes"></span> |
| 342 |
<?php else : ?> |
| 343 |
<span class="step-icon dashicons dashicons-admin-users"></span> |
| 344 |
<?php endif; ?> |
| 345 |
</div> |
| 346 |
<div class="step-content"> |
| 347 |
<div class="step-text"> |
| 348 |
<h3><?php \esc_html_e( 'Set up your public profile', 'activitypub' ); ?></h3> |
| 349 |
<p><?php \esc_html_e( 'Configure your display name and how you appear to others.', 'activitypub' ); ?></p> |
| 350 |
</div> |
| 351 |
<div class="step-action"> |
| 352 |
<?php if ( true === $user_can_activitypub ) : ?> |
| 353 |
<a href="<?php echo \esc_url( \admin_url( '/profile.php#activitypub' ) ); ?>" class="button <?php echo \esc_attr( $button_class ); ?>"> |
| 354 |
<?php \esc_html_e( 'Edit profile', 'activitypub' ); ?> |
| 355 |
</a> |
| 356 |
<?php elseif ( ACTIVITYPUB_BLOG_MODE === \get_option( 'activitypub_actor_mode' ) ) : ?> |
| 357 |
<a href="<?php echo \esc_url( \admin_url( '/options-general.php?page=activitypub&tab=blog-profile' ) ); ?>" class="button <?php echo \esc_attr( $button_class ); ?>"> |
| 358 |
<?php \esc_html_e( 'Edit profile', 'activitypub' ); ?> |
| 359 |
</a> |
| 360 |
<?php else : ?> |
| 361 |
<button class="button <?php echo \esc_attr( $button_class ); ?>" disabled> |
| 362 |
<?php \esc_html_e( 'Edit profile', 'activitypub' ); ?> |
| 363 |
</button> |
| 364 |
<?php endif; ?> |
| 365 |
</div> |
| 366 |
</div> |
| 367 |
</div> |
| 368 |
<?php |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Render the Features step. |
| 373 |
*/ |
| 374 |
public static function render_step_features() { |
| 375 |
$checked = '1' === \get_option( 'activitypub_checklist_blocks_visited', false ); |
| 376 |
$step_class = $checked ? 'activitypub-step-completed' : ''; |
| 377 |
$next_step = self::get_next_incomplete_step(); |
| 378 |
$button_class = ( 'features' === $next_step ) ? 'button-primary' : 'button-secondary'; |
| 379 |
?> |
| 380 |
<div class="activitypub-onboarding-step <?php echo \esc_attr( $step_class ); ?>"> |
| 381 |
<div class="step-indicator"> |
| 382 |
<?php if ( $checked ) : ?> |
| 383 |
<span class="step-icon dashicons dashicons-yes"></span> |
| 384 |
<?php else : ?> |
| 385 |
<span class="step-icon dashicons dashicons-info"></span> |
| 386 |
<?php endif; ?> |
| 387 |
</div> |
| 388 |
<div class="step-content"> |
| 389 |
<div class="step-text"> |
| 390 |
<h3><?php \esc_html_e( 'Learn more about Fediverse features', 'activitypub' ); ?></h3> |
| 391 |
<p><?php \esc_html_e( 'Discover blocks, privacy, and more.', 'activitypub' ); ?></p> |
| 392 |
</div> |
| 393 |
<div class="step-action"> |
| 394 |
<a href="<?php echo \esc_url( \admin_url( 'options-general.php?page=activitypub&help-tab=editor-blocks#tab-link-editor-blocks' ) ); ?>" class="button <?php echo \esc_attr( $button_class ); ?>"> |
| 395 |
<?php \esc_html_e( 'Explore features', 'activitypub' ); ?> |
| 396 |
</a> |
| 397 |
</div> |
| 398 |
</div> |
| 399 |
</div> |
| 400 |
<?php |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Render welcome footer section. |
| 405 |
*/ |
| 406 |
public static function render_welcome_footer_section() { |
| 407 |
?> |
| 408 |
</div><!-- closing welcome-container div --> |
| 409 |
<div class="activitypub-welcome-footer"> |
| 410 |
<a href="<?php echo \esc_url( \admin_url( 'options-general.php?page=activitypub&welcome=0' ) ); ?>" class="skip-steps-link"> |
| 411 |
<?php \esc_html_e( 'Skip these steps', 'activitypub' ); ?> |
| 412 |
</a> |
| 413 |
</div> |
| 414 |
<?php |
| 415 |
} |
| 416 |
|
| 417 |
/** |
| 418 |
* Resolve the welcome checklist. |
| 419 |
*/ |
| 420 |
public static function resolve_checklist() { |
| 421 |
if ( self::get_total_steps_count() === self::get_completed_steps_count() ) { |
| 422 |
\update_user_meta( \get_current_user_id(), 'activitypub_show_welcome_tab', 0 ); |
| 423 |
} |
| 424 |
} |
| 425 |
} |
| 426 |
|