| 8280 | |
| 8281 | /** |
| 8282 | * Returns an array of links. |
| 8283 | * |
| 8284 | * @since 5.9.0 |
| 8285 | * |
| 8286 | * @param array $link_data { |
| 8287 | * The data for the links. |
| 8288 | * |
| 8289 | * @type string $url The link's URL. |
| 8290 | * @type string $label The link's label. Can include markup. |
| 8291 | * @type bool $is_current If true, will apply: |
| 8292 | * `class="current"` and `aria-current='page'` |
| 8293 | * @type array $attributes An associative array of additional attributes |
| 8294 | * in 'attribute' => 'value' format. |
| 8295 | * } |
| 8296 | * @return array |
| 8297 | */ |
| 8298 | function generate_links( $link_data ) { |
| 8299 | $links = array(); |
| 8300 | |
| 8301 | if ( isset( $link_data ) && is_array( $link_data ) ) { |
| 8302 | foreach ( $link_data as $status => $link ) { |
| 8303 | $attributes = isset( $link['attributes'] ) && is_array( $link['attributes'] ) ? $link['attributes'] : array(); |
| 8304 | |
| 8305 | // URL check. |
| 8306 | if ( ! isset( $link['url'] ) || |
| 8307 | ! is_string( $link['url'] ) || |
| 8308 | empty( $link['url'] ) || |
| 8309 | '' === str_replace( ' ', '', $link['url'] ) |
| 8310 | ) { |
| 8311 | continue; |
| 8312 | } |
| 8313 | |
| 8314 | // Label check. |
| 8315 | if ( ! isset( $link['label'] ) || |
| 8316 | ! is_string( $link['label'] ) || |
| 8317 | empty( $link['label'] ) || |
| 8318 | '' === str_replace( ' ', '', $link['label'] ) |
| 8319 | ) { |
| 8320 | continue; |
| 8321 | } |
| 8322 | |
| 8323 | // Current check. |
| 8324 | if ( ! empty( $link['is_current'] ) && is_bool( $link['is_current'] ) ) { |
| 8325 | $attributes['aria-current'] = 'page'; |
| 8326 | $attributes['class'] = isset( $attributes['class'] ) ? |
| 8327 | $attributes['class'] . ' current' : 'current'; |
| 8328 | } |
| 8329 | |
| 8330 | // Collapse attributes to attribute="value" or as a boolean attribute. |
| 8331 | $attributes = array_map( |
| 8332 | function( $attribute, $value ) { |
| 8333 | // Mark invalid/empty attributes for removal. |
| 8334 | if ( ! is_string( $attribute ) || empty( $attribute ) || |
| 8335 | ( ! is_string( $value ) && ! is_int( $value ) && ! is_null( $value ) ) |
| 8336 | ) { |
| 8337 | return false; |
| 8338 | } |
| 8339 | |
| 8340 | // Output as a boolean attribute. |
| 8341 | if ( empty( $value ) ) { |
| 8342 | return $attribute; |
| 8343 | } |
| 8344 | |
| 8345 | return sprintf( '%s="%s"', $attribute, $value ); |
| 8346 | }, |
| 8347 | array_keys( $attributes ), |
| 8348 | array_values( $attributes ) |
| 8349 | ); |
| 8350 | |
| 8351 | // Removals. |
| 8352 | $attributes = array_filter( $attributes ); |
| 8353 | |
| 8354 | // Generate link. |
| 8355 | $links[ $status ] = sprintf( |
| 8356 | '<a href="%s"%s>%s</a>', |
| 8357 | $link['url'], |
| 8358 | ! empty( $attributes ) ? ' ' . implode( ' ', $attributes ) : '', |
| 8359 | $link['label'] |
| 8360 | ); |
| 8361 | } |
| 8362 | } |
| 8363 | |
| 8364 | return $links; |
| 8365 | } |