| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Watchers; |
| 4 |
|
| 5 |
use WP_Term; |
| 6 |
use WPSEO_Meta; |
| 7 |
use WPSEO_Primary_Term; |
| 8 |
use Yoast\WP\SEO\Builders\Primary_Term_Builder; |
| 9 |
use Yoast\WP\SEO\Conditionals\Migrations_Conditional; |
| 10 |
use Yoast\WP\SEO\Helpers\Primary_Term_Helper; |
| 11 |
use Yoast\WP\SEO\Helpers\Site_Helper; |
| 12 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 13 |
use Yoast\WP\SEO\Repositories\Primary_Term_Repository; |
| 14 |
|
| 15 |
/** |
| 16 |
* Primary Term watcher. |
| 17 |
* |
| 18 |
* Watches Posts to save the primary term when set. |
| 19 |
*/ |
| 20 |
class Primary_Term_Watcher implements Integration_Interface { |
| 21 |
|
| 22 |
/** |
| 23 |
* The primary term repository. |
| 24 |
* |
| 25 |
* @var Primary_Term_Repository |
| 26 |
*/ |
| 27 |
protected $repository; |
| 28 |
|
| 29 |
/** |
| 30 |
* Represents the site helper. |
| 31 |
* |
| 32 |
* @var Site_Helper |
| 33 |
*/ |
| 34 |
protected $site; |
| 35 |
|
| 36 |
/** |
| 37 |
* The primary term helper. |
| 38 |
* |
| 39 |
* @var Primary_Term_Helper |
| 40 |
*/ |
| 41 |
protected $primary_term; |
| 42 |
|
| 43 |
/** |
| 44 |
* The primary term builder. |
| 45 |
* |
| 46 |
* @var Primary_Term_Builder |
| 47 |
*/ |
| 48 |
protected $primary_term_builder; |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns the conditionals based on which this loadable should be active. |
| 52 |
* |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
public static function get_conditionals() { |
| 56 |
return [ Migrations_Conditional::class ]; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Primary_Term_Watcher constructor. |
| 61 |
* |
| 62 |
* @codeCoverageIgnore It sets dependencies. |
| 63 |
* |
| 64 |
* @param Primary_Term_Repository $repository The primary term repository. |
| 65 |
* @param Site_Helper $site The site helper. |
| 66 |
* @param Primary_Term_Helper $primary_term The primary term helper. |
| 67 |
* @param Primary_Term_Builder $primary_term_builder The primary term builder. |
| 68 |
*/ |
| 69 |
public function __construct( |
| 70 |
Primary_Term_Repository $repository, |
| 71 |
Site_Helper $site, |
| 72 |
Primary_Term_Helper $primary_term, |
| 73 |
Primary_Term_Builder $primary_term_builder |
| 74 |
) { |
| 75 |
$this->repository = $repository; |
| 76 |
$this->site = $site; |
| 77 |
$this->primary_term = $primary_term; |
| 78 |
$this->primary_term_builder = $primary_term_builder; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Initializes the integration. |
| 83 |
* |
| 84 |
* This is the place to register hooks and filters. |
| 85 |
*/ |
| 86 |
public function register_hooks() { |
| 87 |
\add_action( 'save_post', [ $this, 'save_primary_terms' ], \PHP_INT_MAX ); |
| 88 |
\add_action( 'delete_post', [ $this, 'delete_primary_terms' ] ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Saves all selected primary terms. |
| 93 |
* |
| 94 |
* @param int $post_id Post ID to save primary terms for. |
| 95 |
*/ |
| 96 |
public function save_primary_terms( $post_id ) { |
| 97 |
// Bail if this is a multisite installation and the site has been switched. |
| 98 |
if ( $this->site->is_multisite_and_switched() ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
$taxonomies = $this->primary_term->get_primary_term_taxonomies( $post_id ); |
| 103 |
|
| 104 |
foreach ( $taxonomies as $taxonomy ) { |
| 105 |
$this->save_primary_term( $post_id, $taxonomy ); |
| 106 |
} |
| 107 |
|
| 108 |
$this->primary_term_builder->build( $post_id ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Saves the primary term for a specific taxonomy. |
| 113 |
* |
| 114 |
* @param int $post_id Post ID to save primary term for. |
| 115 |
* @param WP_Term $taxonomy Taxonomy to save primary term for. |
| 116 |
*/ |
| 117 |
protected function save_primary_term( $post_id, $taxonomy ) { |
| 118 |
$primary_term = \filter_input( \INPUT_POST, WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term', \FILTER_SANITIZE_NUMBER_INT ); |
| 119 |
|
| 120 |
// We accept an empty string here because we need to save that if no terms are selected. |
| 121 |
if ( $primary_term && \check_admin_referer( 'save-primary-term', WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_nonce' ) !== null ) { |
| 122 |
$primary_term_object = new WPSEO_Primary_Term( $taxonomy->name, $post_id ); |
| 123 |
$primary_term_object->set_primary_term( $primary_term ); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Deletes primary terms for a post. |
| 129 |
* |
| 130 |
* @param int $post_id The post to delete the terms of. |
| 131 |
* |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function delete_primary_terms( $post_id ) { |
| 135 |
foreach ( $this->primary_term->get_primary_term_taxonomies( $post_id ) as $taxonomy ) { |
| 136 |
$primary_term = $this->repository->find_by_post_id_and_taxonomy( $post_id, $taxonomy->name, false ); |
| 137 |
|
| 138 |
if ( ! $primary_term ) { |
| 139 |
continue; |
| 140 |
} |
| 141 |
|
| 142 |
$primary_term->delete(); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|