← All changes
|
src/integrations/watchers/primary-term-watcher.php
+24
-8
18.5.1
→
trunk
View file →
| @@ -81,8 +81,10 @@ | ||
| 81 | 81 | /** |
| 82 | 82 | * Initializes the integration. |
| 83 | 83 | * |
| 84 | 84 | * This is the place to register hooks and filters. |
| 85 | + * | |
| 86 | + * @return void | |
| 85 | 87 | */ |
| 86 | 88 | public function register_hooks() { |
| 87 | 89 | \add_action( 'save_post', [ $this, 'save_primary_terms' ], \PHP_INT_MAX ); |
| 88 | 90 | \add_action( 'delete_post', [ $this, 'delete_primary_terms' ] ); |
| @@ -91,8 +93,10 @@ | ||
| 91 | 93 | /** |
| 92 | 94 | * Saves all selected primary terms. |
| 93 | 95 | * |
| 94 | 96 | * @param int $post_id Post ID to save primary terms for. |
| 97 | + * | |
| 98 | + * @return void | |
| 95 | 99 | */ |
| 96 | 100 | public function save_primary_terms( $post_id ) { |
| 97 | 101 | // Bail if this is a multisite installation and the site has been switched. |
| 98 | 102 | if ( $this->site->is_multisite_and_switched() ) { |
| @@ -112,16 +116,28 @@ | ||
| 112 | 116 | * Saves the primary term for a specific taxonomy. |
| 113 | 117 | * |
| 114 | 118 | * @param int $post_id Post ID to save primary term for. |
| 115 | 119 | * @param WP_Term $taxonomy Taxonomy to save primary term for. |
| 120 | + * | |
| 121 | + * @return void | |
| 116 | 122 | */ |
| 117 | 123 | 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 ); | |
| 124 | + if ( isset( $_POST[ WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term' ] ) && \is_string( $_POST[ WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term' ] ) ) { | |
| 125 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are casting to an integer. | |
| 126 | + $primary_term_id = (int) \wp_unslash( $_POST[ WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term' ] ); | |
| 119 | 127 | |
| 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 ); | |
| 128 | + if ( $primary_term_id <= 0 ) { | |
| 129 | + $primary_term = ''; | |
| 130 | + } | |
| 131 | + else { | |
| 132 | + $primary_term = (string) $primary_term_id; | |
| 133 | + } | |
| 134 | + | |
| 135 | + // We accept an empty string here because we need to save that if no terms are selected. | |
| 136 | + if ( \check_admin_referer( 'save-primary-term', WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_nonce' ) !== null ) { | |
| 137 | + $primary_term_object = new WPSEO_Primary_Term( $taxonomy->name, $post_id ); | |
| 138 | + $primary_term_object->set_primary_term( $primary_term ); | |
| 139 | + } | |
| 124 | 140 | } |
| 125 | 141 | } |
| 126 | 142 | |
| 127 | 143 | /** |
| @@ -132,14 +148,14 @@ | ||
| 132 | 148 | * @return void |
| 133 | 149 | */ |
| 134 | 150 | public function delete_primary_terms( $post_id ) { |
| 135 | 151 | 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 ); | |
| 152 | + $primary_term_indexable = $this->repository->find_by_post_id_and_taxonomy( $post_id, $taxonomy->name, false ); | |
| 137 | 153 | |
| 138 | - if ( ! $primary_term ) { | |
| 154 | + if ( ! $primary_term_indexable ) { | |
| 139 | 155 | continue; |
| 140 | 156 | } |
| 141 | 157 | |
| 142 | - $primary_term->delete(); | |
| 158 | + $primary_term_indexable->delete(); | |
| 143 | 159 | } |
| 144 | 160 | } |
| 145 | 161 | } |