@ 2022-01-15T02:11:34Z <?php
function wp_create_links( $link_data ) {
$links = array();
if ( isset( $link_data ) && is_array( $link_data ) ) {
foreach ( $link_data as $key => $link ) {
$attributes = array();
if ( isset( $link['attributes'] ) && is_array( $link['attributes'] ) ) {
$attributes = $link['attributes'];
}
// URL check.
if ( empty( $link['url'] ) || ! is_string( $link['url'] ) || '' === trim( $link['url'] ) ) {
continue;
}
// Label check.
if ( empty( $link['label'] ) || ! is_string( $link['label'] ) || '' === trim( $link['label'] ) ) {
continue;
}
// Current check.
if ( ! empty( $link['is_current'] ) && true === $link['is_current'] ) {
$attributes['aria-current'] = 'page';
if ( empty( $attributes['class'] ) ) {
$attributes['class'] = 'current';
} else {
$attributes['class'] .= ' current';
}
}
// Collapse attributes to attribute="value" or boolean attributes.
$attributes = array_map(
function( $attribute, $value ) {
// Mark invalid/empty attributes for removal.
if ( empty( $attribute ) || ! is_string( $attribute ) ) {
return false;
}
// Convert `int` and `float` values to `string`.
if ( is_int( $value ) || is_float( $value ) ) {
$value = (string) $value;
}
// Mark non-string values for removal.
if ( ! is_string( $value ) ) {
return false;
}
// On an empty `value`, assume this is a boolean attribute.
if ( empty( $value ) ) {
return $attribute;
}
return sprintf(
'%s="%s"',
$attribute,
$value
);
},
array_keys( $attributes ),
array_values( $attributes )
);
// Clean up attributes.
$attributes = array_filter( $attributes );
// Generate link.
$links[ $key ] = sprintf(
'<a href="%s"%s>%s</a>',
$link['url'],
! empty( $attributes ) ? ' ' . implode( ' ', $attributes ) : '',
$link['label']
);
}
}
return $links;
}
// Usage
$links_data = array(
'my_link' => array(
'url' => 'https://example.org',
'label' => 'My Example Link',
'is_current' => true,
'attributes' => array(
'class' => 'menu-link',
'rel' => 'author me',
),
),
'my_other_link' => array(
'url' => 'https://example.com',
'label' => 'My Other Example Link',
'is_current' => true,
'attributes' => array(
'class' => 'menu-link',
'rel' => 'noreferer',
),
),
);
$links = wp_create_links( $links_data );
// Want to wrap them?
foreach ( $links as $id => $link ) {
printf( '<li id="%s">%s</li>', $id, $link );
echo "\n";
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.1.6 0.008 0.000 17.52 8.1.5 0.004 0.004 17.46 8.1.4 0.007 0.000 17.54 8.1.3 0.005 0.003 17.74 8.1.2 0.005 0.003 17.68 8.1.1 0.000 0.009 17.59 8.1.0 0.000 0.008 17.36 8.0.19 0.005 0.005 16.87 8.0.18 0.004 0.004 16.83 8.0.17 0.008 0.000 16.97 8.0.16 0.009 0.003 16.95 8.0.15 0.007 0.000 16.93 8.0.14 0.007 0.000 16.73 8.0.13 0.000 0.007 16.75 8.0.12 0.000 0.008 16.89 8.0.11 0.006 0.003 16.84 8.0.10 0.005 0.005 16.91 8.0.9 0.000 0.008 16.81 8.0.8 0.000 0.007 16.98 8.0.7 0.005 0.003 16.82 8.0.6 0.007 0.000 16.92 8.0.5 0.005 0.003 16.82 8.0.3 0.004 0.004 16.94 8.0.2 0.004 0.004 16.97 8.0.1 0.004 0.003 16.96 7.4.29 0.007 0.000 16.59 7.4.28 0.010 0.000 16.54 7.4.27 0.004 0.004 16.54 7.4.26 0.008 0.000 16.61 7.4.25 0.003 0.003 16.53 7.4.24 0.004 0.004 16.55 7.4.23 0.003 0.003 16.49 7.4.22 0.007 0.000 16.47 7.4.21 0.008 0.000 16.52 7.4.20 0.007 0.000 16.48 7.4.19 0.000 0.007 16.68 7.4.18 0.003 0.003 16.47 7.4.16 0.003 0.003 16.54 7.4.15 0.003 0.003 16.60 7.4.14 0.003 0.003 16.26 7.4.13 0.000 0.008 16.57 7.4.12 0.005 0.002 16.50 7.4.11 0.003 0.003 16.31 7.4.10 0.004 0.004 16.43 7.4.9 0.005 0.003 16.45 7.4.8 0.006 0.003 16.46 7.4.7 0.004 0.004 16.45 7.4.6 0.004 0.004 16.36 7.4.5 0.007 0.000 16.42 7.4.4 0.000 0.007 16.35 7.4.3 0.000 0.008 16.46 7.4.2 0.000 0.007 16.31 7.4.1 0.008 0.000 16.50 7.4.0 0.007 0.000 16.48
preferences:dark mode live preview
12.79 ms | 406 KiB | 5 Q