Skip to content

Commit 19d0ea1

Browse files
pfefferleobenland
andauthored
Directly link to a help tab. (#1598)
* Directly link to a help tab. This PR allows you to directly link to help tabs. The idea is to use it for the Onboarding as part of a launchpad. * phpcs fixes * Add changelog * use `{$hook_suffix}` action * use vanilla js * add allowlist (limited to activitypub help tabs) * fix phpcs issues * Backslashit * Increase selector specificity Only clicks anchors that are direct descendants of `hash`, where the href attribute begins with `#tab-panel-`. --------- Co-authored-by: Konstantin Obenland <[email protected]>
1 parent 24e17f9 commit 19d0ea1

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: added
3+
4+
Enable direct linking to Help Tabs.

includes/wp-admin/class-admin.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public static function init() {
6060
if ( site_supports_blocks() ) {
6161
\add_action( 'tool_box', array( self::class, 'tool_box' ) );
6262
}
63+
64+
\add_action( 'admin_print_footer_scripts-settings_page_activitypub', array( self::class, 'open_help_tab' ) );
6365
}
6466

6567
/**
@@ -609,4 +611,38 @@ public static function plugin_update_message( $data, $update ) {
609611
public static function tool_box() {
610612
\load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/toolbox.php' );
611613
}
614+
615+
/**
616+
* Open the help tab.
617+
*
618+
* This function is used to open the help tab,
619+
* it is triggered by the hash in the URL.
620+
*/
621+
public static function open_help_tab() {
622+
// get all tabs registered for the ActivityPub settings page.
623+
$tabs = \get_current_screen()->get_help_tabs();
624+
$ids = \array_values( \wp_list_pluck( $tabs, 'id' ) );
625+
$ids = \array_map(
626+
function ( $id ) {
627+
return '#tab-link-' . $id;
628+
},
629+
$ids
630+
);
631+
?>
632+
<script type="text/javascript">
633+
document.addEventListener('DOMContentLoaded', function() {
634+
// add allowed ids to the hash.
635+
const hash = window.location.hash;
636+
const allowed_ids = <?php echo \wp_json_encode( $ids ); ?>;
637+
if (allowed_ids.includes(hash)) {
638+
// Small delay to ensure the help tab is loaded.
639+
setTimeout(function() {
640+
document.getElementById('contextual-help-link').click();
641+
document.querySelector(hash + ' > a[href^="#tab-panel-"]').click();
642+
}, 500);
643+
}
644+
});
645+
</script>
646+
<?php
647+
}
612648
}

0 commit comments

Comments
 (0)