feat: import taxonomy term records

This commit is contained in:
Keith Solomon
2026-04-29 20:32:56 -05:00
parent 6d11934fcc
commit 592e6e7403
4 changed files with 536 additions and 2 deletions
+101 -1
View File
@@ -811,6 +811,47 @@ if ( ! function_exists( 'wp_update_term' ) ) {
}
}
if ( ! function_exists( 'get_terms' ) ) {
/**
* Minimal terms query for unit tests.
*
* @param array<string, mixed> $args Query args.
* @return array<int, object>
*/
function get_terms( array $args = array() ) {
$terms = array_values( $GLOBALS['wpcs_test_terms'] ?? array() );
if ( isset( $args['taxonomy'] ) ) {
$taxonomies = is_array( $args['taxonomy'] ) ? $args['taxonomy'] : array( $args['taxonomy'] );
$terms = array_filter(
$terms,
static function ( array $term ) use ( $taxonomies ): bool {
return in_array( $term['taxonomy'] ?? '', $taxonomies, true );
}
);
}
if ( isset( $args['meta_key'], $args['meta_value'] ) ) {
$terms = array_filter(
$terms,
static function ( array $term ) use ( $args ): bool {
$values = $GLOBALS['wpcs_test_term_meta'][ (int) $term['term_id'] ][ (string) $args['meta_key'] ] ?? array();
foreach ( $values as $value ) {
if ( (string) $args['meta_value'] === (string) $value ) {
return true;
}
}
return false;
}
);
}
return array_values( array_map( static fn( array $term ): object => (object) $term, $terms ) );
}
}
if ( ! function_exists( 'get_term_by' ) ) {
/**
* Minimal term reader for unit tests.
@@ -821,13 +862,15 @@ if ( ! function_exists( 'get_term_by' ) ) {
* @return array<string, mixed>|false
*/
function get_term_by( $field, $value, $taxonomy ) {
$field = 'id' === $field ? 'term_id' : $field;
foreach ( $GLOBALS['wpcs_test_terms'] ?? array() as $term ) {
if ( (string) ( $term['taxonomy'] ?? '' ) !== (string) $taxonomy ) {
continue;
}
if ( isset( $term[ $field ] ) && (string) $value === (string) $term[ $field ] ) {
return $term;
return (object) $term;
}
}
@@ -835,6 +878,63 @@ if ( ! function_exists( 'get_term_by' ) ) {
}
}
if ( ! function_exists( 'update_term_meta' ) ) {
/**
* Minimal term meta updater for unit tests.
*
* @param int $term_id Term ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
* @return bool
*/
function update_term_meta( $term_id, $meta_key, $meta_value ) {
$GLOBALS['wpcs_test_term_meta'][ (int) $term_id ][ (string) $meta_key ] = array( $meta_value );
return true;
}
}
if ( ! function_exists( 'get_term_meta' ) ) {
/**
* Minimal term meta reader for unit tests.
*
* @param int $term_id Term ID.
* @param string $key Meta key.
* @param bool $single Whether to return single value.
* @return mixed
*/
function get_term_meta( $term_id, $key = '', $single = false ) {
$meta = $GLOBALS['wpcs_test_term_meta'][ (int) $term_id ] ?? array();
if ( '' === $key ) {
return $meta;
}
$values = $meta[ $key ] ?? array();
if ( $single ) {
return $values[0] ?? '';
}
return $values;
}
}
if ( ! function_exists( 'delete_term_meta' ) ) {
/**
* Minimal term meta deleter for unit tests.
*
* @param int $term_id Term ID.
* @param string $meta_key Meta key.
* @return bool
*/
function delete_term_meta( $term_id, $meta_key ) {
unset( $GLOBALS['wpcs_test_term_meta'][ (int) $term_id ][ (string) $meta_key ] );
return true;
}
}
if ( ! function_exists( 'wp_set_object_terms' ) ) {
/**
* Minimal object term relationship setter for unit tests.