wpseek.com
A WordPress-centric search engine for devs and theme authors



build_variation_for_navigation_link › WordPress Function

Since5.9.0
Deprecatedn/a
build_variation_for_navigation_link ( $entity, $kind )
Parameters: (2)
  • (WP_Taxonomy|WP_Post_Type) $entity post type or taxonomy entity.
    Required: Yes
  • (string) $kind string of value 'taxonomy' or 'post-type'.
    Required: Yes
Returns:
  • (array)
Defined at:
Codex:

Returns a navigation link variation



Source

function build_variation_for_navigation_link( $entity, $kind ) {
	$title       = '';
	$description = '';

	// Get default labels based on entity type
	$default_labels = null;
	if ( $entity instanceof WP_Post_Type ) {
		$default_labels = WP_Post_Type::get_default_labels();
	} elseif ( $entity instanceof WP_Taxonomy ) {
		$default_labels = WP_Taxonomy::get_default_labels();
	}

	// Get title and check if it's default
	$is_default_title = false;
	if ( property_exists( $entity->labels, 'item_link' ) ) {
		$title = $entity->labels->item_link;
		if ( isset( $default_labels['item_link'] ) ) {
			$is_default_title = in_array( $title, $default_labels['item_link'], true );
		}
	}

	// Get description and check if it's default
	$is_default_description = false;
	if ( property_exists( $entity->labels, 'item_link_description' ) ) {
		$description = $entity->labels->item_link_description;
		if ( isset( $default_labels['item_link_description'] ) ) {
			$is_default_description = in_array( $description, $default_labels['item_link_description'], true );
		}
	}

	// Calculate singular name once (used for both title and description)
	$singular = isset( $entity->labels->singular_name ) ? $entity->labels->singular_name : ucfirst( $entity->name );

	// Set default title if needed
	if ( $is_default_title || '' === $title ) {
		/* translators: %s: Singular label of the entity. */
		$title = sprintf( __( '%s link' ), $singular );
	}

	// Default description if needed.
	// Use a single space character instead of an empty string to prevent fallback to the
	// block.json default description ("Add a page, link, or another item to your navigation.").
	// An empty string would be treated as missing and trigger the fallback, while a single
	// space appears blank in the UI but prevents the fallback behavior.
	// We avoid generating descriptions like "A link to a %s" to prevent grammatical errors
	// (e.g., "A link to a event" should be "A link to an event").
	if ( $is_default_description || '' === $description ) {
		$description = ' ';
	}

	$variation = array(
		'name'        => $entity->name,
		'title'       => $title,
		'description' => $description,
		'attributes'  => array(
			'type' => $entity->name,
			'kind' => $kind,
		),
	);

	// Tweak some value for the variations.
	$variation_overrides = array(
		'post_tag'    => array(
			'name'       => 'tag',
			'attributes' => array(
				'type' => 'tag',
				'kind' => $kind,
			),
		),
		'post_format' => array(
			// The item_link and item_link_description for post formats is the
			// same as for tags, so need to be overridden.
			'title'       => __( 'Post Format Link' ),
			'description' => __( 'A link to a post format' ),
			'attributes'  => array(
				'type' => 'post_format',
				'kind' => $kind,
			),
		),
	);

	if ( array_key_exists( $entity->name, $variation_overrides ) ) {
		$variation = array_merge(
			$variation,
			$variation_overrides[ $entity->name ]
		);
	}

	return $variation;
}