Last active
December 16, 2024 01:40
-
-
Save westonruter/6088b04ff28dd9c509be53d0685076f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Constructs a CSS selector for the current tag. | |
* | |
* @param WP_HTML_Processor $processor Processor. | |
* @return string Selector. | |
*/ | |
function get_css_selector( WP_HTML_Processor $processor ): string { | |
$selector = join( ' > ', $processor->get_breadcrumbs() ); | |
$id = $processor->get_attribute( 'id' ); | |
if ( is_string( $id ) ) { | |
$selector .= '#' . $id; | |
} | |
$class_attr = $processor->get_attribute( 'class' ); | |
if ( is_string( $class_attr ) ) { | |
$selector .= join( | |
'', | |
array_map( | |
static function ( string $class_name ): string { | |
return '.' . $class_name; | |
}, | |
array_filter( | |
(array) preg_split( '/\s+/', trim( $class_attr ) ) | |
) | |
) | |
); | |
} | |
return $selector; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment