20 lines
492 B
PHP
20 lines
492 B
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace KbMarkdownImporter\Repository;
|
|
|
|
final class ProductRepository
|
|
{
|
|
public function ensure(string $name, string $slug = ''): int
|
|
{
|
|
$slug = $slug ? sanitize_title($slug) : sanitize_title($name);
|
|
$term = term_exists($slug, 'kb_product');
|
|
|
|
if (! $term) {
|
|
$term = wp_insert_term($name, 'kb_product', ['slug' => $slug]);
|
|
}
|
|
|
|
return is_wp_error($term) ? 0 : (int) ($term['term_id'] ?? $term);
|
|
}
|
|
}
|