%1$s',
esc_html( $query ),
esc_url( $search_url ),
esc_attr(
sprintf(
/* translators: %s: search query */
__( 'Search Google for “%s”', '4wp-seo-helper' ),
$query
)
)
);
if ( ! empty( $landing['page'] ) ) {
$page_label = self::page_label( (string) $landing['page'] );
$meta = [];
if ( isset( $landing['clicks'] ) && (int) $landing['clicks'] > 0 ) {
$meta[] = sprintf(
/* translators: %d: click count */
_n( '%d click', '%d clicks', (int) $landing['clicks'], '4wp-seo-helper' ),
(int) $landing['clicks']
);
}
if ( isset( $landing['position'] ) && (float) $landing['position'] > 0 ) {
$meta[] = sprintf(
/* translators: %s: average position */
__( 'pos. %s', '4wp-seo-helper' ),
number_format_i18n( (float) $landing['position'], 1 )
);
}
$html .= sprintf(
'%3$s',
esc_url( (string) $landing['page'] ),
esc_attr( implode( ' · ', $meta ) ),
esc_html( $page_label )
);
}
$html .= '';
return wp_kses( $html, self::allowed_html() );
}
public static function search_url( string $query ): string {
return 'https://www.google.com/search?q=' . rawurlencode( trim( $query ) );
}
private static function page_label( string $url ): string {
$path = (string) wp_parse_url( $url, PHP_URL_PATH );
$path = untrailingslashit( $path );
if ( '' === $path || '/' === $path ) {
return '/';
}
return $path;
}
/**
* @return array>
*/
public static function allowed_html(): array {
return [
'span' => [
'class' => true,
'title' => true,
],
'a' => [
'href' => true,
'class' => true,
'target' => true,
'rel' => true,
'title' => true,
'aria-label' => true,
],
];
}
}