| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Repositories; |
| 4 |
|
| 5 |
use Yoast\WP\Lib\Model; |
| 6 |
use Yoast\WP\Lib\ORM; |
| 7 |
use Yoast\WP\SEO\Models\Primary_Term; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Primary_Term_Repository. |
| 11 |
*/ |
| 12 |
class Primary_Term_Repository { |
| 13 |
|
| 14 |
/** |
| 15 |
* Starts a query for this repository. |
| 16 |
* |
| 17 |
* @return ORM |
| 18 |
*/ |
| 19 |
public function query() { |
| 20 |
return Model::of_type( 'Primary_Term' ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Retrieves a primary term by a post ID and taxonomy. |
| 25 |
* |
| 26 |
* @param int $post_id The post the indexable is based upon. |
| 27 |
* @param string $taxonomy The taxonomy the indexable belongs to. |
| 28 |
* @param bool $auto_create Optional. Creates an indexable if it does not exist yet. |
| 29 |
* |
| 30 |
* @return Primary_Term|null Instance of a primary term. |
| 31 |
*/ |
| 32 |
public function find_by_post_id_and_taxonomy( $post_id, $taxonomy, $auto_create = true ) { |
| 33 |
/** |
| 34 |
* Instance of the primary term. |
| 35 |
* |
| 36 |
* @var Primary_Term $primary_term_indexable |
| 37 |
*/ |
| 38 |
$primary_term_indexable = $this->query() |
| 39 |
->where( 'post_id', $post_id ) |
| 40 |
->where( 'taxonomy', $taxonomy ) |
| 41 |
->find_one(); |
| 42 |
|
| 43 |
if ( $auto_create && ! $primary_term_indexable ) { |
| 44 |
$primary_term_indexable = $this->query()->create(); |
| 45 |
} |
| 46 |
|
| 47 |
return $primary_term_indexable; |
| 48 |
} |
| 49 |
} |
| 50 |
|