PluginProbe
Polylang / 3.8.5
Polylang v3.8.5
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / src / Capabilities / Create / Term.php

Term.php in Polylang 3.8.5, at src/Capabilities/Create/Term.php

78 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 namespace WP_Syntex\Polylang\Capabilities\Create;
7
8 use PLL_Language;
9 use WP_Syntex\Polylang\Capabilities\Capabilities;
10
11 /**
12 * Class to manage the language context for terms creation or update.
13 *
14 * @since 3.8
15 */
16 class Term extends Abstract_Object {
17 /**
18 * Returns the language to set for a post creation.
19 *
20 * @since 3.8
21 *
22 * @param int $id The term ID for which to set the language. Default `0`.
23 * @param string $taxonomy The taxonomy for which to set the language. Default `''`.
24 * @return PLL_Language The language context.
25 */
26 public function get_language( int $id = 0, string $taxonomy = '' ): PLL_Language {
27 /** @var PLL_Language $default_language The default language is always defined. */
28 $default_language = $this->model->get_default_language();
29 $user = Capabilities::get_user();
30
31 if ( ! empty( $_GET['new_lang'] ) && $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
32 // Defined only on admin.
33 return $lang;
34 }
35 if ( ! empty( $_POST['term_lang_choice'] ) && is_string( $_POST['term_lang_choice'] ) && $lang = $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
36 return $lang;
37 }
38 if ( ! empty( $_POST['inline_lang_choice'] ) && is_string( $_POST['inline_lang_choice'] ) && $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
39 return $lang;
40 }
41 if ( ! isset( $this->pref_lang ) && ! empty( $_REQUEST['lang'] ) && $lang = $this->model->get_language( sanitize_key( $_REQUEST['lang'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
42 // Testing $this->pref_lang makes this test pass only on frontend.
43 return $lang;
44 }
45 if ( $this->request && $lang = $this->request->get_language() ) {
46 // REST request.
47 return $lang;
48 }
49 if ( ( $term = get_term( $id, $taxonomy ) ) && ! empty( $term->parent ) && $parent_lang = $this->model->term->get_language( $term->parent ) ) {
50 // Sets language from term parent if exists thanks to Scott Kingsley Clark.
51 return $parent_lang;
52 }
53 if ( isset( $this->pref_lang ) && $user->can_translate( $this->pref_lang ) ) {
54 // Always defined on admin, never defined on frontend.
55 return $this->pref_lang;
56 }
57 if ( ! empty( $this->curlang ) ) {
58 // Only on frontend due to the previous test always true on admin.
59 return $this->curlang;
60 }
61 if ( $user->is_translator() ) {
62 // Use default language if user can translate into it...
63 if ( $user->can_translate( $default_language ) ) {
64 return $default_language;
65 }
66
67 // ... or its preferred one.
68 $preferred_language = $this->model->get_language( $user->get_preferred_language_slug() );
69 if ( $preferred_language ) {
70 return $preferred_language;
71 }
72 }
73
74 // In all other cases use default language because we must have a language to set.
75 return $default_language;
76 }
77 }
78