| @@ -9,27 +9,82 @@ | ||
| 9 | 9 | * |
| 10 | 10 | * @since 2.4 |
| 11 | 11 | */ |
| 12 | 12 | class PLL_CRUD_Terms { |
| 13 | - public $model, $curlang, $filter_lang, $pref_lang; | |
| 13 | + /** | |
| 14 | + * @var PLL_Model | |
| 15 | + */ | |
| 16 | + public $model; | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * Current language (used to filter the content). | |
| 20 | + * | |
| 21 | + * @var PLL_Language|null | |
| 22 | + */ | |
| 23 | + public $curlang; | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * Language selected in the admin language filter. | |
| 27 | + * | |
| 28 | + * @var PLL_Language|null | |
| 29 | + */ | |
| 30 | + public $filter_lang; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Preferred language to assign to new contents. | |
| 34 | + * | |
| 35 | + * @var PLL_Language|null | |
| 36 | + */ | |
| 37 | + public $pref_lang; | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * Stores the 'lang' query var from WP_Query. | |
| 41 | + * | |
| 42 | + * @var string|null | |
| 43 | + */ | |
| 14 | 44 | private $tax_query_lang; |
| 15 | 45 | |
| 16 | 46 | /** |
| 47 | + * Stores the term name before creating a slug if needed. | |
| 48 | + * | |
| 49 | + * @var string | |
| 50 | + */ | |
| 51 | + private $pre_term_name = ''; | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Stores the term ID before creating a slug if needed. | |
| 55 | + * | |
| 56 | + * @var int | |
| 57 | + */ | |
| 58 | + private $pre_term_id = 0; | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * Reference to the Polylang options array. | |
| 62 | + * | |
| 63 | + * @var array | |
| 64 | + */ | |
| 65 | + protected $options; | |
| 66 | + | |
| 67 | + /** | |
| 17 | 68 | * Constructor |
| 18 | 69 | * |
| 19 | 70 | * @since 2.4 |
| 20 | 71 | * |
| 21 | - * @param object $polylang | |
| 72 | + * @param object $polylang The Polylang object. | |
| 22 | 73 | */ |
| 23 | 74 | public function __construct( &$polylang ) { |
| 24 | - $this->model = &$polylang->model; | |
| 25 | - $this->curlang = &$polylang->curlang; | |
| 75 | + $this->options = &$polylang->options; | |
| 76 | + $this->model = &$polylang->model; | |
| 77 | + $this->curlang = &$polylang->curlang; | |
| 26 | 78 | $this->filter_lang = &$polylang->filter_lang; |
| 27 | - $this->pref_lang = &$polylang->pref_lang; | |
| 79 | + $this->pref_lang = &$polylang->pref_lang; | |
| 28 | 80 | |
| 29 | 81 | // Saving terms |
| 30 | 82 | add_action( 'create_term', array( $this, 'save_term' ), 999, 3 ); |
| 31 | 83 | add_action( 'edit_term', array( $this, 'save_term' ), 999, 3 ); // After PLL_Admin_Filters_Term |
| 84 | + add_filter( 'pre_term_name', array( $this, 'set_pre_term_name' ) ); | |
| 85 | + add_filter( 'pre_term_slug', array( $this, 'set_pre_term_slug' ), 10, 2 ); | |
| 86 | + add_filter( 'pre_term_term_id', array( $this, 'set_pre_term_id' ) ); | |
| 32 | 87 | |
| 33 | 88 | // Adds cache domain when querying terms |
| 34 | 89 | add_filter( 'get_terms_args', array( $this, 'get_terms_args' ), 10, 2 ); |
| 35 | 90 | |
| @@ -38,18 +93,19 @@ | ||
| 38 | 93 | add_action( 'pre_get_posts', array( $this, 'set_tax_query_lang' ), 999 ); |
| 39 | 94 | add_action( 'posts_selection', array( $this, 'unset_tax_query_lang' ), 0 ); |
| 40 | 95 | |
| 41 | 96 | // Deleting terms |
| 42 | - add_action( 'pre_delete_term', array( $this, 'delete_term' ) ); | |
| 97 | + add_action( 'pre_delete_term', array( $this, 'delete_term' ), 10, 2 ); | |
| 43 | 98 | } |
| 44 | 99 | |
| 45 | 100 | /** |
| 46 | - * Allows to set a language by default for terms if it has no language yet | |
| 101 | + * Allows to set a language by default for terms if it has no language yet. | |
| 47 | 102 | * |
| 48 | 103 | * @since 1.5.4 |
| 49 | 104 | * |
| 50 | - * @param int $term_id | |
| 51 | - * @param string $taxonomy | |
| 105 | + * @param int $term_id Term ID. | |
| 106 | + * @param string $taxonomy Taxonomy name. | |
| 107 | + * @return void | |
| 52 | 108 | */ |
| 53 | 109 | protected function set_default_language( $term_id, $taxonomy ) { |
| 54 | 110 | if ( ! $this->model->term->get_language( $term_id ) ) { |
| 55 | 111 | if ( ! isset( $this->pref_lang ) && ! empty( $_REQUEST['lang'] ) && $lang = $this->model->get_language( sanitize_key( $_REQUEST['lang'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| @@ -60,24 +116,28 @@ | ||
| 60 | 116 | $this->model->term->set_language( $term_id, $parent_lang ); |
| 61 | 117 | } elseif ( isset( $this->pref_lang ) ) { |
| 62 | 118 | // Always defined on admin, never defined on frontend |
| 63 | 119 | $this->model->term->set_language( $term_id, $this->pref_lang ); |
| 64 | - } else { | |
| 120 | + } elseif ( ! empty( $this->curlang ) ) { | |
| 65 | 121 | // Only on frontend due to the previous test always true on admin |
| 66 | 122 | $this->model->term->set_language( $term_id, $this->curlang ); |
| 123 | + } else { | |
| 124 | + // In all other cases set to default language. | |
| 125 | + $this->model->term->set_language( $term_id, $this->options['default_lang'] ); | |
| 67 | 126 | } |
| 68 | 127 | } |
| 69 | 128 | } |
| 70 | 129 | |
| 71 | 130 | /** |
| 72 | - * Called when a category or post tag is created or edited | |
| 73 | - * Does nothing except on taxonomies which are filterable | |
| 131 | + * Called when a category or post tag is created or edited. | |
| 132 | + * Does nothing except on taxonomies which are filterable. | |
| 74 | 133 | * |
| 75 | 134 | * @since 0.1 |
| 76 | 135 | * |
| 77 | - * @param int $term_id | |
| 78 | - * @param int $tt_id Term taxonomy id | |
| 79 | - * @param string $taxonomy | |
| 136 | + * @param int $term_id Term id of the term being saved. | |
| 137 | + * @param int $tt_id Term taxonomy id. | |
| 138 | + * @param string $taxonomy Taxonomy name. | |
| 139 | + * @return void | |
| 80 | 140 | */ |
| 81 | 141 | public function save_term( $term_id, $tt_id, $taxonomy ) { |
| 82 | 142 | if ( $this->model->is_translated_taxonomy( $taxonomy ) ) { |
| 83 | 143 | |
| @@ -87,15 +147,15 @@ | ||
| 87 | 147 | $this->set_default_language( $term_id, $taxonomy ); |
| 88 | 148 | } |
| 89 | 149 | |
| 90 | 150 | /** |
| 91 | - * Fires after the term language and translations are saved | |
| 151 | + * Fires after the term language and translations are saved. | |
| 92 | 152 | * |
| 93 | 153 | * @since 1.2 |
| 94 | 154 | * |
| 95 | - * @param int $term_id term id | |
| 96 | - * @param string $taxonomy taxonomy name | |
| 97 | - * @param array $translations the list of translations term ids | |
| 155 | + * @param int $term_id Term id. | |
| 156 | + * @param string $taxonomy Taxonomy name. | |
| 157 | + * @param int[] $translations The list of translations term ids. | |
| 98 | 158 | */ |
| 99 | 159 | do_action( 'pll_save_term', $term_id, $taxonomy, $this->model->term->get_translations( $term_id ) ); |
| 100 | 160 | } |
| 101 | 161 | } |
| @@ -100,17 +160,19 @@ | ||
| 100 | 160 | } |
| 101 | 161 | } |
| 102 | 162 | |
| 103 | 163 | /** |
| 104 | - * Get the language(s) to filter get_terms | |
| 164 | + * Get the language(s) to filter WP_Term_Query. | |
| 105 | 165 | * |
| 106 | 166 | * @since 1.7.6 |
| 107 | 167 | * |
| 108 | - * @param array $taxonomies queried taxonomies | |
| 109 | - * @param array $args get_terms arguments | |
| 110 | - * @return object|string|bool the language(s) to use in the filter, false otherwise | |
| 168 | + * @param string[] $taxonomies Queried taxonomies. | |
| 169 | + * @param array $args WP_Term_Query arguments. | |
| 170 | + * @return PLL_Language|string|false The language(s) to use in the filter, false otherwise. | |
| 111 | 171 | */ |
| 112 | 172 | protected function get_queried_language( $taxonomies, $args ) { |
| 173 | + global $pagenow; | |
| 174 | + | |
| 113 | 175 | // Does nothing except on taxonomies which are filterable |
| 114 | 176 | // Since WP 4.7, make sure not to filter wp_get_object_terms() |
| 115 | 177 | if ( ! $this->model->is_translated_taxonomy( $taxonomies ) || ! empty( $args['object_ids'] ) ) { |
| 116 | 178 | return false; |
| @@ -121,9 +183,9 @@ | ||
| 121 | 183 | return $args['lang']; |
| 122 | 184 | } |
| 123 | 185 | |
| 124 | 186 | // On tags page, everything should be filtered according to the admin language filter except the parent dropdown |
| 125 | - if ( 'edit-tags.php' === $GLOBALS['pagenow'] && empty( $args['class'] ) ) { | |
| 187 | + if ( 'edit-tags.php' === $pagenow && empty( $args['class'] ) ) { | |
| 126 | 188 | return $this->filter_lang; |
| 127 | 189 | } |
| 128 | 190 | |
| 129 | 191 | return $this->curlang; |
| @@ -129,19 +191,19 @@ | ||
| 129 | 191 | return $this->curlang; |
| 130 | 192 | } |
| 131 | 193 | |
| 132 | 194 | /** |
| 133 | - * Adds language dependent cache domain when querying terms | |
| 134 | - * Useful as the 'lang' parameter is not included in cache key by WordPress | |
| 195 | + * Adds language dependent cache domain when querying terms. | |
| 196 | + * Useful as the 'lang' parameter is not included in cache key by WordPress. | |
| 135 | 197 | * |
| 136 | 198 | * @since 1.3 |
| 137 | 199 | * |
| 138 | - * @param array $args | |
| 139 | - * @param array $taxonomies | |
| 140 | - * @return array modified arguments | |
| 200 | + * @param array $args WP_Term_Query arguments. | |
| 201 | + * @param string[] $taxonomies Queried taxonomies. | |
| 202 | + * @return array Modified arguments. | |
| 141 | 203 | */ |
| 142 | 204 | public function get_terms_args( $args, $taxonomies ) { |
| 143 | - // Don't break _get_term_hierarchy() | |
| 205 | + // Don't break _get_term_hierarchy(). | |
| 144 | 206 | if ( 'all' === $args['get'] && 'id' === $args['orderby'] && 'id=>parent' === $args['fields'] ) { |
| 145 | 207 | $args['lang'] = ''; |
| 146 | 208 | } |
| 147 | 209 | |
| @@ -161,12 +223,12 @@ | ||
| 161 | 223 | * Filters categories and post tags by language(s) when needed on admin side |
| 162 | 224 | * |
| 163 | 225 | * @since 0.2 |
| 164 | 226 | * |
| 165 | - * @param array $clauses list of sql clauses | |
| 166 | - * @param array $taxonomies list of taxonomies | |
| 167 | - * @param array $args get_terms arguments | |
| 168 | - * @return array modified sql clauses | |
| 227 | + * @param string[] $clauses List of sql clauses. | |
| 228 | + * @param string[] $taxonomies List of taxonomies. | |
| 229 | + * @param array $args WP_Term_Query arguments. | |
| 230 | + * @return string[] Modified sql clauses. | |
| 169 | 231 | */ |
| 170 | 232 | public function terms_clauses( $clauses, $taxonomies, $args ) { |
| 171 | 233 | $lang = $this->get_queried_language( $taxonomies, $args ); |
| 172 | 234 | return $this->model->terms_clauses( $clauses, $lang ); |
| @@ -172,24 +234,27 @@ | ||
| 172 | 234 | return $this->model->terms_clauses( $clauses, $lang ); |
| 173 | 235 | } |
| 174 | 236 | |
| 175 | 237 | /** |
| 176 | - * Sets the WP_Term_Query language when doing a WP_Query | |
| 177 | - * Needed since WP 4.9 | |
| 238 | + * Sets the WP_Term_Query language when doing a WP_Query. | |
| 239 | + * Needed since WP 4.9. | |
| 178 | 240 | * |
| 179 | 241 | * @since 2.3.2 |
| 180 | 242 | * |
| 181 | - * @param object $query WP_Query object | |
| 243 | + * @param WP_Query $query WP_Query object. | |
| 244 | + * @return void | |
| 182 | 245 | */ |
| 183 | 246 | public function set_tax_query_lang( $query ) { |
| 184 | - $this->tax_query_lang = isset( $query->query_vars['lang'] ) ? $query->query_vars['lang'] : ''; | |
| 247 | + $this->tax_query_lang = $query->query_vars['lang'] ?? ''; | |
| 185 | 248 | } |
| 186 | 249 | |
| 187 | 250 | /** |
| 188 | - * Removes the WP_Term_Query language filter for WP_Query | |
| 189 | - * Needed since WP 4.9 | |
| 251 | + * Removes the WP_Term_Query language filter for WP_Query. | |
| 252 | + * Needed since WP 4.9. | |
| 190 | 253 | * |
| 191 | 254 | * @since 2.3.2 |
| 255 | + * | |
| 256 | + * @return void | |
| 192 | 257 | */ |
| 193 | 258 | public function unset_tax_query_lang() { |
| 194 | 259 | unset( $this->tax_query_lang ); |
| 195 | 260 | } |
| @@ -199,11 +264,65 @@ | ||
| 199 | 264 | * Deletes language and translations |
| 200 | 265 | * |
| 201 | 266 | * @since 0.1 |
| 202 | 267 | * |
| 203 | - * @param int $term_id | |
| 268 | + * @param int $term_id Id of the term to delete. | |
| 269 | + * @param string $taxonomy Name of the taxonomy. | |
| 270 | + * @return void | |
| 204 | 271 | */ |
| 205 | - public function delete_term( $term_id ) { | |
| 272 | + public function delete_term( $term_id, $taxonomy ) { | |
| 273 | + if ( ! $this->model->is_translated_taxonomy( $taxonomy ) ) { | |
| 274 | + return; | |
| 275 | + } | |
| 276 | + | |
| 277 | + // Delete translation and relationships only if the term is translatable. | |
| 206 | 278 | $this->model->term->delete_translation( $term_id ); |
| 207 | 279 | $this->model->term->delete_language( $term_id ); |
| 280 | + } | |
| 281 | + | |
| 282 | + /** | |
| 283 | + * Stores the term name for use in pre_term_slug | |
| 284 | + * | |
| 285 | + * @since 0.9.5 | |
| 286 | + * | |
| 287 | + * @param string $name term name | |
| 288 | + * @return string unmodified term name | |
| 289 | + */ | |
| 290 | + public function set_pre_term_name( $name ) { | |
| 291 | + $this->pre_term_name = is_string( $name ) ? $name : ''; | |
| 292 | + | |
| 293 | + return $name; | |
| 294 | + } | |
| 295 | + | |
| 296 | + /** | |
| 297 | + * Appends language slug to the term slug if needed. | |
| 298 | + * | |
| 299 | + * @since 3.3 | |
| 300 | + * | |
| 301 | + * @param string $slug Term slug. | |
| 302 | + * @param string $taxonomy Term taxonomy. | |
| 303 | + * @return string Slug with a language suffix if found. | |
| 304 | + */ | |
| 305 | + public function set_pre_term_slug( $slug, $taxonomy ) { | |
| 306 | + if ( ! $this->model->is_translated_taxonomy( $taxonomy ) || ! is_string( $slug ) ) { | |
| 307 | + return $slug; | |
| 308 | + } | |
| 309 | + | |
| 310 | + $term_slug = new PLL_Term_Slug( $this->model, $slug, $taxonomy, $this->pre_term_name, $this->pre_term_id ); | |
| 311 | + | |
| 312 | + return $term_slug->get_suffixed_slug( '-' ); | |
| 313 | + } | |
| 314 | + | |
| 315 | + /** | |
| 316 | + * Stores the term ID to use in `pre_term_slug`. | |
| 317 | + * | |
| 318 | + * @since 3.7.7 | |
| 319 | + * | |
| 320 | + * @param int $term_id Term ID. | |
| 321 | + * @return int Unmodified term ID. | |
| 322 | + */ | |
| 323 | + public function set_pre_term_id( $term_id ) { | |
| 324 | + $this->pre_term_id = is_numeric( $term_id ) ? (int) $term_id : 0; | |
| 325 | + | |
| 326 | + return $term_id; | |
| 208 | 327 | } |
| 209 | 328 | } |