| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Watchers; |
| 4 |
|
| 5 |
use WP_Post; |
| 6 |
use WPSEO_Meta; |
| 7 |
use Yoast\WP\SEO\Builders\Indexable_Hierarchy_Builder; |
| 8 |
use Yoast\WP\SEO\Conditionals\Admin\Doing_Post_Quick_Edit_Save_Conditional; |
| 9 |
use Yoast\WP\SEO\Conditionals\Migrations_Conditional; |
| 10 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 11 |
use Yoast\WP\SEO\Helpers\Post_Type_Helper; |
| 12 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 13 |
use Yoast\WP\SEO\Models\Indexable; |
| 14 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 15 |
use Yoast\WP\SEO\Repositories\Primary_Term_Repository; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class Primary_Category_Quick_Edit_Watcher |
| 19 |
*/ |
| 20 |
class Primary_Category_Quick_Edit_Watcher implements Integration_Interface { |
| 21 |
|
| 22 |
/** |
| 23 |
* Holds the options helper. |
| 24 |
* |
| 25 |
* @var Options_Helper |
| 26 |
*/ |
| 27 |
protected $options_helper; |
| 28 |
|
| 29 |
/** |
| 30 |
* Holds the primary term repository. |
| 31 |
* |
| 32 |
* @var Primary_Term_Repository |
| 33 |
*/ |
| 34 |
protected $primary_term_repository; |
| 35 |
|
| 36 |
/** |
| 37 |
* The post type helper. |
| 38 |
* |
| 39 |
* @var Post_Type_Helper |
| 40 |
*/ |
| 41 |
protected $post_type_helper; |
| 42 |
|
| 43 |
/** |
| 44 |
* The indexable repository. |
| 45 |
* |
| 46 |
* @var Indexable_Repository |
| 47 |
*/ |
| 48 |
protected $indexable_repository; |
| 49 |
|
| 50 |
/** |
| 51 |
* The indexable hierarchy builder. |
| 52 |
* |
| 53 |
* @var Indexable_Hierarchy_Builder |
| 54 |
*/ |
| 55 |
protected $indexable_hierarchy_builder; |
| 56 |
|
| 57 |
/** |
| 58 |
* Primary_Category_Quick_Edit_Watcher constructor. |
| 59 |
* |
| 60 |
* @param Options_Helper $options_helper The options helper. |
| 61 |
* @param Primary_Term_Repository $primary_term_repository The primary term repository. |
| 62 |
* @param Post_Type_Helper $post_type_helper The post type helper. |
| 63 |
* @param Indexable_Repository $indexable_repository The indexable repository. |
| 64 |
* @param Indexable_Hierarchy_Builder $indexable_hierarchy_builder The indexable hierarchy repository. |
| 65 |
*/ |
| 66 |
public function __construct( |
| 67 |
Options_Helper $options_helper, |
| 68 |
Primary_Term_Repository $primary_term_repository, |
| 69 |
Post_Type_Helper $post_type_helper, |
| 70 |
Indexable_Repository $indexable_repository, |
| 71 |
Indexable_Hierarchy_Builder $indexable_hierarchy_builder |
| 72 |
) { |
| 73 |
$this->options_helper = $options_helper; |
| 74 |
$this->primary_term_repository = $primary_term_repository; |
| 75 |
$this->post_type_helper = $post_type_helper; |
| 76 |
$this->indexable_repository = $indexable_repository; |
| 77 |
$this->indexable_hierarchy_builder = $indexable_hierarchy_builder; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Initializes the integration. |
| 82 |
* |
| 83 |
* This is the place to register hooks and filters. |
| 84 |
* |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public function register_hooks() { |
| 88 |
\add_action( 'set_object_terms', [ $this, 'validate_primary_category' ], 10, 4 ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Returns the conditionals based on which this loadable should be active. |
| 93 |
* |
| 94 |
* @return array The conditionals. |
| 95 |
*/ |
| 96 |
public static function get_conditionals() { |
| 97 |
return [ Migrations_Conditional::class, Doing_Post_Quick_Edit_Save_Conditional::class ]; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Validates if the current primary category is still present. If not just remove the post meta for it. |
| 102 |
* |
| 103 |
* @param int $object_id Object ID. |
| 104 |
* @param array $terms Unused. An array of object terms. |
| 105 |
* @param array $tt_ids An array of term taxonomy IDs. |
| 106 |
* @param string $taxonomy Taxonomy slug. |
| 107 |
* |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
public function validate_primary_category( $object_id, $terms, $tt_ids, $taxonomy ) { |
| 111 |
$post = \get_post( $object_id ); |
| 112 |
if ( $post === null ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
$main_taxonomy = $this->options_helper->get( 'post_types-' . $post->post_type . '-maintax' ); |
| 117 |
if ( ! $main_taxonomy || $main_taxonomy === '0' ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
if ( $main_taxonomy !== $taxonomy ) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
$primary_category = $this->get_primary_term_id( $post->ID, $main_taxonomy ); |
| 126 |
if ( $primary_category === false ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
// The primary category isn't removed. |
| 131 |
if ( \in_array( (string) $primary_category, $tt_ids, true ) ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
$this->remove_primary_term( $post->ID, $main_taxonomy ); |
| 136 |
|
| 137 |
// Rebuild the post hierarchy for this post now the primary term has been changed. |
| 138 |
$this->build_post_hierarchy( $post ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Returns the primary term id of a post. |
| 143 |
* |
| 144 |
* @param int $post_id The post ID. |
| 145 |
* @param string $main_taxonomy The main taxonomy. |
| 146 |
* |
| 147 |
* @return int|false The ID of the primary term, or `false` if the post ID is invalid. |
| 148 |
*/ |
| 149 |
private function get_primary_term_id( $post_id, $main_taxonomy ) { |
| 150 |
$primary_term = $this->primary_term_repository->find_by_post_id_and_taxonomy( $post_id, $main_taxonomy, false ); |
| 151 |
|
| 152 |
if ( $primary_term ) { |
| 153 |
return $primary_term->term_id; |
| 154 |
} |
| 155 |
|
| 156 |
return \get_post_meta( $post_id, WPSEO_Meta::$meta_prefix . 'primary_' . $main_taxonomy, true ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Removes the primary category. |
| 161 |
* |
| 162 |
* @param int $post_id The post id to set primary taxonomy for. |
| 163 |
* @param string $main_taxonomy Name of the taxonomy that is set to be the primary one. |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
private function remove_primary_term( $post_id, $main_taxonomy ) { |
| 168 |
$primary_term = $this->primary_term_repository->find_by_post_id_and_taxonomy( $post_id, $main_taxonomy, false ); |
| 169 |
if ( $primary_term ) { |
| 170 |
$primary_term->delete(); |
| 171 |
} |
| 172 |
|
| 173 |
// Remove it from the post meta. |
| 174 |
\delete_post_meta( $post_id, WPSEO_Meta::$meta_prefix . 'primary_' . $main_taxonomy ); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Builds the hierarchy for a post. |
| 179 |
* |
| 180 |
* @param WP_Post $post The post. |
| 181 |
* |
| 182 |
* @return void |
| 183 |
*/ |
| 184 |
public function build_post_hierarchy( $post ) { |
| 185 |
if ( $this->post_type_helper->is_excluded( $post->post_type ) ) { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
$indexable = $this->indexable_repository->find_by_id_and_type( $post->ID, 'post' ); |
| 190 |
|
| 191 |
if ( $indexable instanceof Indexable ) { |
| 192 |
$this->indexable_hierarchy_builder->build( $indexable ); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|