| @@ -9,12 +9,49 @@ | ||
| 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 | + /** | |
| 17 | 54 | * Constructor |
| 18 | 55 | * |
| 19 | 56 | * @since 2.4 |
| 20 | 57 | * |
| @@ -20,16 +57,18 @@ | ||
| 20 | 57 | * |
| 21 | 58 | * @param object $polylang |
| 22 | 59 | */ |
| 23 | 60 | public function __construct( &$polylang ) { |
| 24 | - $this->model = &$polylang->model; | |
| 25 | - $this->curlang = &$polylang->curlang; | |
| 61 | + $this->model = &$polylang->model; | |
| 62 | + $this->curlang = &$polylang->curlang; | |
| 26 | 63 | $this->filter_lang = &$polylang->filter_lang; |
| 27 | - $this->pref_lang = &$polylang->pref_lang; | |
| 64 | + $this->pref_lang = &$polylang->pref_lang; | |
| 28 | 65 | |
| 29 | 66 | // Saving terms |
| 30 | 67 | add_action( 'create_term', array( $this, 'save_term' ), 999, 3 ); |
| 31 | 68 | add_action( 'edit_term', array( $this, 'save_term' ), 999, 3 ); // After PLL_Admin_Filters_Term |
| 69 | + add_filter( 'pre_term_name', array( $this, 'set_pre_term_name' ) ); | |
| 70 | + add_filter( 'pre_term_slug', array( $this, 'set_pre_term_slug' ), 10, 2 ); | |
| 32 | 71 | |
| 33 | 72 | // Adds cache domain when querying terms |
| 34 | 73 | add_filter( 'get_terms_args', array( $this, 'get_terms_args' ), 10, 2 ); |
| 35 | 74 | |
| @@ -48,8 +87,9 @@ | ||
| 48 | 87 | * @since 1.5.4 |
| 49 | 88 | * |
| 50 | 89 | * @param int $term_id |
| 51 | 90 | * @param string $taxonomy |
| 91 | + * @return void | |
| 52 | 92 | */ |
| 53 | 93 | protected function set_default_language( $term_id, $taxonomy ) { |
| 54 | 94 | if ( ! $this->model->term->get_language( $term_id ) ) { |
| 55 | 95 | if ( ! isset( $this->pref_lang ) && ! empty( $_REQUEST['lang'] ) && $lang = $this->model->get_language( sanitize_key( $_REQUEST['lang'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| @@ -68,16 +108,17 @@ | ||
| 68 | 108 | } |
| 69 | 109 | } |
| 70 | 110 | |
| 71 | 111 | /** |
| 72 | - * Called when a category or post tag is created or edited | |
| 73 | - * Does nothing except on taxonomies which are filterable | |
| 112 | + * Called when a category or post tag is created or edited. | |
| 113 | + * Does nothing except on taxonomies which are filterable. | |
| 74 | 114 | * |
| 75 | 115 | * @since 0.1 |
| 76 | 116 | * |
| 77 | - * @param int $term_id | |
| 78 | - * @param int $tt_id Term taxonomy id | |
| 79 | - * @param string $taxonomy | |
| 117 | + * @param int $term_id Term id of the term being saved. | |
| 118 | + * @param int $tt_id Term taxonomy id. | |
| 119 | + * @param string $taxonomy Taxonomy name. | |
| 120 | + * @return void | |
| 80 | 121 | */ |
| 81 | 122 | public function save_term( $term_id, $tt_id, $taxonomy ) { |
| 82 | 123 | if ( $this->model->is_translated_taxonomy( $taxonomy ) ) { |
| 83 | 124 | |
| @@ -87,15 +128,15 @@ | ||
| 87 | 128 | $this->set_default_language( $term_id, $taxonomy ); |
| 88 | 129 | } |
| 89 | 130 | |
| 90 | 131 | /** |
| 91 | - * Fires after the term language and translations are saved | |
| 132 | + * Fires after the term language and translations are saved. | |
| 92 | 133 | * |
| 93 | 134 | * @since 1.2 |
| 94 | 135 | * |
| 95 | - * @param int $term_id term id | |
| 96 | - * @param string $taxonomy taxonomy name | |
| 97 | - * @param array $translations the list of translations term ids | |
| 136 | + * @param int $term_id Term id. | |
| 137 | + * @param string $taxonomy Taxonomy name. | |
| 138 | + * @param int[] $translations The list of translations term ids. | |
| 98 | 139 | */ |
| 99 | 140 | do_action( 'pll_save_term', $term_id, $taxonomy, $this->model->term->get_translations( $term_id ) ); |
| 100 | 141 | } |
| 101 | 142 | } |
| @@ -100,17 +141,19 @@ | ||
| 100 | 141 | } |
| 101 | 142 | } |
| 102 | 143 | |
| 103 | 144 | /** |
| 104 | - * Get the language(s) to filter get_terms | |
| 145 | + * Get the language(s) to filter WP_Term_Query. | |
| 105 | 146 | * |
| 106 | 147 | * @since 1.7.6 |
| 107 | 148 | * |
| 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 | |
| 149 | + * @param string[] $taxonomies Queried taxonomies. | |
| 150 | + * @param array $args WP_Term_Query arguments. | |
| 151 | + * @return PLL_Language|string|false The language(s) to use in the filter, false otherwise. | |
| 111 | 152 | */ |
| 112 | 153 | protected function get_queried_language( $taxonomies, $args ) { |
| 154 | + global $pagenow; | |
| 155 | + | |
| 113 | 156 | // Does nothing except on taxonomies which are filterable |
| 114 | 157 | // Since WP 4.7, make sure not to filter wp_get_object_terms() |
| 115 | 158 | if ( ! $this->model->is_translated_taxonomy( $taxonomies ) || ! empty( $args['object_ids'] ) ) { |
| 116 | 159 | return false; |
| @@ -121,9 +164,9 @@ | ||
| 121 | 164 | return $args['lang']; |
| 122 | 165 | } |
| 123 | 166 | |
| 124 | 167 | // 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'] ) ) { | |
| 168 | + if ( 'edit-tags.php' === $pagenow && empty( $args['class'] ) ) { | |
| 126 | 169 | return $this->filter_lang; |
| 127 | 170 | } |
| 128 | 171 | |
| 129 | 172 | return $this->curlang; |
| @@ -129,19 +172,19 @@ | ||
| 129 | 172 | return $this->curlang; |
| 130 | 173 | } |
| 131 | 174 | |
| 132 | 175 | /** |
| 133 | - * Adds language dependent cache domain when querying terms | |
| 134 | - * Useful as the 'lang' parameter is not included in cache key by WordPress | |
| 176 | + * Adds language dependent cache domain when querying terms. | |
| 177 | + * Useful as the 'lang' parameter is not included in cache key by WordPress. | |
| 135 | 178 | * |
| 136 | 179 | * @since 1.3 |
| 137 | 180 | * |
| 138 | - * @param array $args | |
| 139 | - * @param array $taxonomies | |
| 140 | - * @return array modified arguments | |
| 181 | + * @param array $args WP_Term_Query arguments. | |
| 182 | + * @param string[] $taxonomies Queried taxonomies. | |
| 183 | + * @return array Modified arguments. | |
| 141 | 184 | */ |
| 142 | 185 | public function get_terms_args( $args, $taxonomies ) { |
| 143 | - // Don't break _get_term_hierarchy() | |
| 186 | + // Don't break _get_term_hierarchy(). | |
| 144 | 187 | if ( 'all' === $args['get'] && 'id' === $args['orderby'] && 'id=>parent' === $args['fields'] ) { |
| 145 | 188 | $args['lang'] = ''; |
| 146 | 189 | } |
| 147 | 190 | |
| @@ -161,12 +204,12 @@ | ||
| 161 | 204 | * Filters categories and post tags by language(s) when needed on admin side |
| 162 | 205 | * |
| 163 | 206 | * @since 0.2 |
| 164 | 207 | * |
| 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 | |
| 208 | + * @param string[] $clauses List of sql clauses. | |
| 209 | + * @param string[] $taxonomies List of taxonomies. | |
| 210 | + * @param array $args WP_Term_Query arguments. | |
| 211 | + * @return string[] Modified sql clauses. | |
| 169 | 212 | */ |
| 170 | 213 | public function terms_clauses( $clauses, $taxonomies, $args ) { |
| 171 | 214 | $lang = $this->get_queried_language( $taxonomies, $args ); |
| 172 | 215 | return $this->model->terms_clauses( $clauses, $lang ); |
| @@ -172,14 +215,15 @@ | ||
| 172 | 215 | return $this->model->terms_clauses( $clauses, $lang ); |
| 173 | 216 | } |
| 174 | 217 | |
| 175 | 218 | /** |
| 176 | - * Sets the WP_Term_Query language when doing a WP_Query | |
| 177 | - * Needed since WP 4.9 | |
| 219 | + * Sets the WP_Term_Query language when doing a WP_Query. | |
| 220 | + * Needed since WP 4.9. | |
| 178 | 221 | * |
| 179 | 222 | * @since 2.3.2 |
| 180 | 223 | * |
| 181 | - * @param object $query WP_Query object | |
| 224 | + * @param WP_Query $query WP_Query object. | |
| 225 | + * @return void | |
| 182 | 226 | */ |
| 183 | 227 | public function set_tax_query_lang( $query ) { |
| 184 | 228 | $this->tax_query_lang = isset( $query->query_vars['lang'] ) ? $query->query_vars['lang'] : ''; |
| 185 | 229 | } |
| @@ -184,12 +228,14 @@ | ||
| 184 | 228 | $this->tax_query_lang = isset( $query->query_vars['lang'] ) ? $query->query_vars['lang'] : ''; |
| 185 | 229 | } |
| 186 | 230 | |
| 187 | 231 | /** |
| 188 | - * Removes the WP_Term_Query language filter for WP_Query | |
| 189 | - * Needed since WP 4.9 | |
| 232 | + * Removes the WP_Term_Query language filter for WP_Query. | |
| 233 | + * Needed since WP 4.9. | |
| 190 | 234 | * |
| 191 | 235 | * @since 2.3.2 |
| 236 | + * | |
| 237 | + * @return void | |
| 192 | 238 | */ |
| 193 | 239 | public function unset_tax_query_lang() { |
| 194 | 240 | unset( $this->tax_query_lang ); |
| 195 | 241 | } |
| @@ -200,10 +246,84 @@ | ||
| 200 | 246 | * |
| 201 | 247 | * @since 0.1 |
| 202 | 248 | * |
| 203 | 249 | * @param int $term_id |
| 250 | + * @return void | |
| 204 | 251 | */ |
| 205 | 252 | public function delete_term( $term_id ) { |
| 206 | 253 | $this->model->term->delete_translation( $term_id ); |
| 207 | 254 | $this->model->term->delete_language( $term_id ); |
| 255 | + } | |
| 256 | + | |
| 257 | + /** | |
| 258 | + * Stores the term name for use in pre_term_slug | |
| 259 | + * | |
| 260 | + * @since 0.9.5 | |
| 261 | + * | |
| 262 | + * @param string $name term name | |
| 263 | + * @return string unmodified term name | |
| 264 | + */ | |
| 265 | + public function set_pre_term_name( $name ) { | |
| 266 | + return $this->pre_term_name = $name; | |
| 267 | + } | |
| 268 | + | |
| 269 | + /** | |
| 270 | + * Appends language slug to the term slug if needed. | |
| 271 | + * | |
| 272 | + * @since 3.3 | |
| 273 | + * | |
| 274 | + * @param string $slug Term slug. | |
| 275 | + * @param string $taxonomy Term taxonomy. | |
| 276 | + * @return string Slug with a language suffix if found. | |
| 277 | + */ | |
| 278 | + public function set_pre_term_slug( $slug, $taxonomy ) { | |
| 279 | + if ( ! $this->model->is_translated_taxonomy( $taxonomy ) ) { | |
| 280 | + return $slug; | |
| 281 | + } | |
| 282 | + | |
| 283 | + if ( ! $slug ) { | |
| 284 | + $slug = sanitize_title( $this->pre_term_name ); | |
| 285 | + } | |
| 286 | + | |
| 287 | + if ( ! term_exists( $slug, $taxonomy ) ) { | |
| 288 | + return $slug; | |
| 289 | + } | |
| 290 | + | |
| 291 | + /** | |
| 292 | + * Filters the subsequently inserted term language. | |
| 293 | + * | |
| 294 | + * @since 3.3 | |
| 295 | + * | |
| 296 | + * @param PLL_Language|null $lang Found language object, null otherwise. | |
| 297 | + * @param string $taxonomy Term taonomy. | |
| 298 | + * @param string $slug Term slug | |
| 299 | + */ | |
| 300 | + $lang = apply_filters( 'pll_inserted_term_language', null, $taxonomy, $slug ); | |
| 301 | + | |
| 302 | + if ( ! $lang instanceof PLL_Language ) { | |
| 303 | + return $slug; | |
| 304 | + } | |
| 305 | + | |
| 306 | + $parent = 0; | |
| 307 | + if ( is_taxonomy_hierarchical( $taxonomy ) ) { | |
| 308 | + /** | |
| 309 | + * Filters the subsequently inserted term parent. | |
| 310 | + * | |
| 311 | + * @since 3.3 | |
| 312 | + * | |
| 313 | + * @param int $parent Parent term ID, 0 if none. | |
| 314 | + * @param string $taxonomy Term taxonomy. | |
| 315 | + * @param string $slug Term slug | |
| 316 | + */ | |
| 317 | + $parent = apply_filters( 'pll_inserted_term_parent', 0, $taxonomy, $slug ); | |
| 318 | + } | |
| 319 | + | |
| 320 | + $term_id = (int) $this->model->term_exists_by_slug( $slug, $lang, $taxonomy, $parent ); | |
| 321 | + | |
| 322 | + // If no term exist in the given language with that slug, it can be created. | |
| 323 | + if ( ! $term_id ) { | |
| 324 | + $slug .= '-' . $lang->slug; | |
| 325 | + } | |
| 326 | + | |
| 327 | + return $slug; | |
| 208 | 328 | } |
| 209 | 329 | } |