| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Builders; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Helpers\Indexable_Helper; |
| 6 |
use Yoast\WP\SEO\Helpers\Meta_Helper; |
| 7 |
use Yoast\WP\SEO\Helpers\Primary_Term_Helper; |
| 8 |
use Yoast\WP\SEO\Repositories\Primary_Term_Repository; |
| 9 |
|
| 10 |
/** |
| 11 |
* Primary term builder. |
| 12 |
* |
| 13 |
* Creates the primary term for a post. |
| 14 |
*/ |
| 15 |
class Primary_Term_Builder { |
| 16 |
|
| 17 |
/** |
| 18 |
* The primary term repository. |
| 19 |
* |
| 20 |
* @var Primary_Term_Repository |
| 21 |
*/ |
| 22 |
protected $repository; |
| 23 |
|
| 24 |
/** |
| 25 |
* The indexable helper. |
| 26 |
* |
| 27 |
* @var Indexable_Helper |
| 28 |
*/ |
| 29 |
private $indexable_helper; |
| 30 |
|
| 31 |
/** |
| 32 |
* The primary term helper. |
| 33 |
* |
| 34 |
* @var Primary_Term_Helper |
| 35 |
*/ |
| 36 |
private $primary_term; |
| 37 |
|
| 38 |
/** |
| 39 |
* The meta helper. |
| 40 |
* |
| 41 |
* @var Meta_Helper |
| 42 |
*/ |
| 43 |
private $meta; |
| 44 |
|
| 45 |
/** |
| 46 |
* Primary_Term_Builder constructor. |
| 47 |
* |
| 48 |
* @param Primary_Term_Repository $repository The primary term repository. |
| 49 |
* @param Indexable_Helper $indexable_helper The indexable helper. |
| 50 |
* @param Primary_Term_Helper $primary_term The primary term helper. |
| 51 |
* @param Meta_Helper $meta The meta helper. |
| 52 |
*/ |
| 53 |
public function __construct( |
| 54 |
Primary_Term_Repository $repository, |
| 55 |
Indexable_Helper $indexable_helper, |
| 56 |
Primary_Term_Helper $primary_term, |
| 57 |
Meta_Helper $meta |
| 58 |
) { |
| 59 |
$this->repository = $repository; |
| 60 |
$this->indexable_helper = $indexable_helper; |
| 61 |
$this->primary_term = $primary_term; |
| 62 |
$this->meta = $meta; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Formats and saves the primary terms for the post with the given post id. |
| 67 |
* |
| 68 |
* @param int $post_id The post ID. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public function build( $post_id ) { |
| 73 |
foreach ( $this->primary_term->get_primary_term_taxonomies( $post_id ) as $taxonomy ) { |
| 74 |
$this->save_primary_term( $post_id, $taxonomy->name ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Save the primary term for a specific taxonomy. |
| 80 |
* |
| 81 |
* @param int $post_id Post ID to save primary term for. |
| 82 |
* @param string $taxonomy Taxonomy to save primary term for. |
| 83 |
* |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
protected function save_primary_term( $post_id, $taxonomy ) { |
| 87 |
$term_id = $this->meta->get_value( 'primary_' . $taxonomy, $post_id ); |
| 88 |
|
| 89 |
$term_selected = ! empty( $term_id ); |
| 90 |
$primary_term_indexable = $this->repository->find_by_post_id_and_taxonomy( $post_id, $taxonomy, $term_selected ); |
| 91 |
|
| 92 |
// Removes the indexable when no term found. |
| 93 |
if ( ! $term_selected ) { |
| 94 |
if ( $primary_term_indexable ) { |
| 95 |
$primary_term_indexable->delete(); |
| 96 |
} |
| 97 |
|
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
$primary_term_indexable->term_id = $term_id; |
| 102 |
$primary_term_indexable->post_id = $post_id; |
| 103 |
$primary_term_indexable->taxonomy = $taxonomy; |
| 104 |
$primary_term_indexable->blog_id = \get_current_blog_id(); |
| 105 |
$this->indexable_helper->save_indexable( $primary_term_indexable ); |
| 106 |
} |
| 107 |
} |
| 108 |
|