@@ -373,7 +373,7 @@ function get_search_form( $args = array() ) {
373373/**
374374 * Retrieves the markup for an accessible tooltip.
375375 *
376- * Returns a button with an accessible name popover.
376+ * Returns a button with an accessible name popover hint .
377377 *
378378 * @since 7.1.0
379379 *
@@ -383,6 +383,8 @@ function get_search_form( $args = array() ) {
383383 *
384384 * @type string $id Unique ID for the popover element. Default is a
385385 * generated unique ID.
386+ * @type string $button Existing `button` or `a` markup. Used instead of generated button.
387+ * Default standard button HTML.
386388 * @type string $label Not used for tooltips.
387389 * @type string $close_label Not used for tooltips.
388390 * @type string $icon Dashicons icon class for the toggle button.
@@ -399,12 +401,9 @@ function wp_get_tooltip( $content, $args = array() ) {
399401}
400402
401403/**
402- * Retrieves the markup for an accessible tooltip or toggletip .
404+ * Retrieves the markup for an accessible toggle tip .
403405 *
404- * Returns a button and either a hover/focus triggered tooltip popover or an action
405- * triggered toggle tip. Enqueue the `wp-tooltip` style and script where it is used.
406- * Tooltips are used to show the accessible name of a control.
407- * Toggletips are be used for longer supporting text explaining context.
406+ * Returns a button and an action triggered toggle tip with `$content`.
408407 *
409408 * @since 7.1.0
410409 *
@@ -414,6 +413,8 @@ function wp_get_tooltip( $content, $args = array() ) {
414413 *
415414 * @type string $id Unique ID for the popover element. Default is a
416415 * generated unique ID.
416+ * @type string $button Existing `button` markup. Used instead of generated button.
417+ * Default standard button HTML.
417418 * @type string $label Accessible label for the toggle button.
418419 * Default 'Help', matching the default icon.
419420 * Ignored for tooltips.
@@ -424,9 +425,9 @@ function wp_get_tooltip( $content, $args = array() ) {
424425 * @type string $class Additional class(es) for the wrapping element.
425426 * Default empty.
426427 * }
427- * @return string Tooltip HTML markup, or an empty string when no content is provided.
428+ * @return string Toggletip HTML markup, or an empty string when no content is provided.
428429 */
429- function wp_get_toggletip ( $ content , $ args ) {
430+ function wp_get_toggletip ( $ content , $ args = array () ) {
430431 $ args ['type ' ] = 'toggletip ' ;
431432 return wp_get_tooltip_helper ( $ content , $ args );
432433}
@@ -447,6 +448,8 @@ function wp_get_toggletip( $content, $args ) {
447448 *
448449 * @type string $id Unique ID for the popover element. Default is a
449450 * generated unique ID.
451+ * @type string $button Existing `button` or `a` markup. Used instead of generated button.
452+ * Default standard button HTML.
450453 * @type string $label Accessible label for the toggle button.
451454 * Default 'Help', matching the default icon.
452455 * Ignored for tooltips.
@@ -469,7 +472,8 @@ function wp_get_tooltip_helper( $content, $args = array() ) {
469472 }
470473
471474 $ defaults = array (
472- 'id ' => '' ,
475+ 'id ' => wp_unique_id ( 'wp-tooltip- ' ),
476+ 'button ' => '<button type="button" aria-label="%3$s"><span class="dashicons %4$s" aria-hidden="true"></span></button> ' ,
473477 'label ' => __ ( 'Help ' ),
474478 'close_label ' => __ ( 'Close ' ),
475479 'icon ' => 'dashicons-editor-help ' ,
@@ -479,14 +483,43 @@ function wp_get_tooltip_helper( $content, $args = array() ) {
479483
480484 $ args = wp_parse_args ( $ args , $ defaults );
481485
482- $ id = '' !== $ args ['id ' ] ? $ args ['id ' ] : wp_unique_id ( 'wp-tooltip- ' );
483-
484486 $ classes = ( 'tooltip ' === $ args ['type ' ] ) ? 'wp-tooltip wp-is-tooltip ' : 'wp-tooltip wp-is-toggletip ' ;
485487 if ( '' !== $ args ['class ' ] ) {
486488 $ classes .= ' ' . $ args ['class ' ];
487489 }
488490
489- $ icon = '' !== $ args ['icon ' ] ? ' ' . $ args ['icon ' ] : '' ;
491+ $ icon = ( $ args ['icon ' ] ) ? trim ( $ args ['icon ' ] ) : $ defaults ['icon ' ];
492+ $ id = ( $ args ['id ' ] ) ? $ args ['id ' ] : $ defaults ['id ' ];
493+ $ button = ( $ args ['button ' ] ) ? $ args ['button ' ] : $ defaults ['button ' ];
494+ $ processed = false ;
495+ $ processor = new WP_HTML_Tag_Processor ( $ button );
496+ if ( true === $ processor ->next_tag ( 'button ' ) ) {
497+ $ processor ->add_class ( 'wp-tooltip__toggle ' );
498+ if ( 'tooltip ' !== $ args ['type ' ] ) {
499+ $ processor ->set_attribute ( 'popovertarget ' , '%2$s ' );
500+ $ processor ->set_attribute ( 'aria-haspopup ' , 'dialog ' );
501+ }
502+ $ button = $ processor ->get_updated_html ();
503+ $ processed = true ;
504+ } else {
505+ // Reset processor.
506+ $ processor = new WP_HTML_Tag_Processor ( $ button );
507+ if ( true === $ processor ->next_tag ( 'a ' ) && 'tooltip ' === $ args ['type ' ] ) {
508+ $ processor ->add_class ( 'wp-tooltip__toggle ' );
509+ $ button = $ processor ->get_updated_html ();
510+ $ processed = true ;
511+ }
512+ }
513+ if ( ! $ processed ) {
514+ // Button HTML passed was not valid.
515+ $ processor = new WP_HTML_Tag_Processor ( $ defaults ['button ' ] );
516+ $ processor ->add_class ( 'wp-tooltip__toggle ' );
517+ if ( 'tooltip ' !== $ args ['type ' ] ) {
518+ $ processor ->set_attribute ( 'popovertarget ' , '%2$s ' );
519+ $ processor ->set_attribute ( 'aria-haspopup ' , 'dialog ' );
520+ }
521+ $ button = $ processor ->get_updated_html ();
522+ }
490523
491524 /*
492525 * The markup only uses phrasing content so it is valid when nested
@@ -498,11 +531,9 @@ function wp_get_tooltip_helper( $content, $args = array() ) {
498531 // Tooltips are only used to visually display labels.
499532 $ label = wp_strip_all_tags ( $ content , true );
500533 $ markup = sprintf (
501- '<span class="%1$s"> ' .
502- '<button type="button" class="wp-tooltip__toggle" popovertarget="%2$s" aria-label="%3$s"> ' .
503- '<span class="dashicons%4$s" aria-hidden="true"></span> ' .
504- '</button> ' .
505- '<span popover="hint" id="%2$s" class="wp-tooltip__bubble" role="tooltip"> ' .
534+ '<span class="%1$s">
535+ ' . $ button . '
536+ <span popover="hint" id="%2$s" class="wp-tooltip__bubble" role="tooltip"> ' .
506537 '<span id="%2$s-text" class="wp-tooltip__text">%5$s</span> ' .
507538 '</span> ' .
508539 '</span> ' ,
@@ -519,11 +550,9 @@ function wp_get_tooltip_helper( $content, $args = array() ) {
519550 * attributes reproduce the accessible name and focus handling of the native element.
520551 */
521552 $ markup = sprintf (
522- '<span class="%1$s"> ' .
523- '<button type="button" class="wp-tooltip__toggle" popovertarget="%2$s" aria-label="%3$s" aria-haspopup="dialog"> ' .
524- '<span class="dashicons%4$s" aria-hidden="true"></span> ' .
525- '</button> ' .
526- '<span popover="auto" id="%2$s" class="wp-tooltip__bubble" role="dialog" aria-label="%3$s" tabindex="-1" autofocus> ' .
553+ '<span class="%1$s">
554+ ' . $ button . '
555+ <span popover="auto" id="%2$s" class="wp-tooltip__bubble" role="dialog" aria-label="%3$s" tabindex="-1" autofocus> ' .
527556 '<span id="%2$s-text" class="wp-tooltip__text">%5$s</span> ' .
528557 '<button type="button" class="wp-tooltip__close" popovertarget="%2$s" popovertargetaction="hide" aria-label="%6$s"> ' .
529558 '<span class="dashicons dashicons-no-alt" aria-hidden="true"></span> ' .
0 commit comments