ACF\CLI

JsonCommand::resolve_keys_for_typeprivateACF 6.8

Resolves a comma-separated list of keys or labels into ACF keys for a given post type.

Method of the class: JsonCommand{}

No Hooks.

Returns

array. List of ACF keys.

Usage

// private - for code of main (parent) class only
$result = $this->resolve_keys_for_type( $post_type, $arg );
$post_type(string) (required)
The item type (field group, post type, taxonomy, or options page).
$arg(string|null) (required)
Comma-separated keys/labels, or null for all.

Changelog

Since 6.8 Introduced.

JsonCommand::resolve_keys_for_type() code ACF 6.8.8

private function resolve_keys_for_type( $post_type, $arg ) {
	$posts = acf_get_internal_post_type_posts( $post_type );
	$posts = array_filter( $posts, 'acf_internal_post_object_contains_valid_key' );

	if ( ! $arg ) {
		return wp_list_pluck( $posts, 'key' );
	}

	$identifiers = array_filter( array_map( 'trim', explode( ',', $arg ) ) );
	$keys        = array();

	foreach ( $identifiers as $identifier ) {
		$found = false;

		foreach ( $posts as $post ) {
			if ( $post['key'] === $identifier || strcasecmp( $post['title'], $identifier ) === 0 ) {
				$keys[] = $post['key'];
				$found  = true;
				break;
			}
		}

		if ( ! $found ) {
			WP_CLI::warning( sprintf( 'No item found matching "%s". Skipping.', $identifier ) );
		}
	}

	return array_unique( $keys );
}