PluginProbe
Polylang / 3.7.4
Polylang v3.7.4
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 / include / default-term.php

default-term.php in Polylang 3.7.4, at include/default-term.php

227 lines 6.4 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 /**
7 * Manages filters and actions related to default terms.
8 *
9 * @since 3.7
10 */
11 class PLL_Default_Term {
12
13 /**
14 * A reference to the PLL_Model instance.
15 *
16 * @var PLL_Model
17 */
18 protected $model;
19
20 /**
21 * Preferred language to assign to new contents.
22 *
23 * @var PLL_Language|null
24 */
25 protected $curlang;
26
27 /**
28 * Array of registered taxonomy names for which Polylang manages languages and translations.
29 *
30 * @var string[]
31 */
32 protected $taxonomies;
33
34 /**
35 * Constructor: setups properties.
36 *
37 * @since 3.1
38 *
39 * @param object $polylang The Polylang object.
40 */
41 public function __construct( &$polylang ) {
42 $this->model = &$polylang->model;
43 $this->curlang = &$polylang->curlang;
44 $this->taxonomies = $this->model->get_translated_taxonomies();
45 }
46
47 /**
48 * Setups filters and actions needed.
49 *
50 * @since 3.1
51 *
52 * @return void
53 */
54 public function add_hooks() {
55 foreach ( $this->taxonomies as $taxonomy ) {
56 if ( 'category' === $taxonomy ) {
57 // Allows to get the default terms in all languages.
58 add_filter( 'option_default_' . $taxonomy, array( $this, 'option_default_term' ) );
59 add_action( 'update_option_default_' . $taxonomy, array( $this, 'update_option_default_term' ), 10, 2 );
60 }
61 }
62 add_action( 'pll_add_language', array( $this, 'handle_default_term_on_create_language' ) );
63
64 // The default term should be in the default language.
65 add_action( 'pll_update_default_lang', array( $this, 'update_default_term_language' ) );
66
67 // Prevents deleting all the translations of the default term.
68 add_filter( 'map_meta_cap', array( $this, 'fix_delete_default_term' ), 10, 4 );
69 }
70
71 /**
72 * Filters the default term to return the one in the right language.
73 *
74 * @since 1.2
75 *
76 * @param int $taxonomy_term_id The taxonomy term id.
77 * @return int A taxonomy term id.
78 */
79 public function option_default_term( $taxonomy_term_id ) {
80 if ( isset( $this->curlang ) && $tr = $this->model->term->get( $taxonomy_term_id, $this->curlang ) ) {
81 $taxonomy_term_id = $tr;
82 }
83 return $taxonomy_term_id;
84 }
85
86 /**
87 * Checks if the new default term is translated in all languages.
88 * If not, create the translations.
89 *
90 * @since 1.7
91 *
92 * @param int $old_value The old option value.
93 * @param int $value The new option value.
94 * @return void
95 */
96 public function update_option_default_term( $old_value, $value ) {
97 $default_cat_lang = $this->model->term->get_language( $value );
98
99 // Assign a default language to default term.
100 if ( ! $default_cat_lang ) {
101 $default_cat_lang = $this->model->get_default_language();
102 $this->model->term->set_language( (int) $value, $default_cat_lang );
103 }
104
105 if ( empty( $default_cat_lang ) ) {
106 return;
107 }
108
109 $taxonomy = substr( current_filter(), 22 );
110
111 foreach ( $this->model->get_languages_list() as $language ) {
112 if ( $language->slug != $default_cat_lang->slug && ! $this->model->term->get_translation( $value, $language ) ) {
113 $this->create_default_term( $language, $taxonomy );
114 }
115 }
116 }
117
118 /**
119 * Creates a default term for a language.
120 *
121 * @since 1.2
122 *
123 * @param PLL_Language|string|int $lang Language.
124 * @param string $taxonomy The current taxonomy.
125 * @return void
126 */
127 public function create_default_term( $lang, $taxonomy ) {
128 $lang = $this->model->get_language( $lang );
129
130 // Create a new term.
131 // FIXME this is translated in admin language when we would like it in $lang
132 $cat_name = __( 'Uncategorized', 'polylang' );
133 $cat_slug = sanitize_title( $cat_name . '-' . $lang->slug );
134 $cat = wp_insert_term( $cat_name, $taxonomy, array( 'slug' => $cat_slug ) );
135
136 // Check that the term was not previously created (in case the language was deleted and recreated).
137 $cat = $cat->error_data['term_exists'] ?? $cat['term_id'];
138
139 // Set language.
140 $this->model->term->set_language( (int) $cat, $lang );
141
142 // This is a translation of the default term.
143 $default = (int) get_option( 'default_' . $taxonomy );
144 $translations = $this->model->term->get_translations( $default );
145
146 $this->model->term->save_translations( (int) $cat, $translations );
147 }
148
149 /**
150 * Manages the default term when new languages are created.
151 *
152 * @since 3.1
153 *
154 * @param array $args Argument used to create the language. @see PLL_Admin_Model::add_language().
155 * @return void
156 */
157 public function handle_default_term_on_create_language( $args ) {
158 foreach ( $this->taxonomies as $taxonomy ) {
159 if ( 'category' === $taxonomy ) {
160 $default = (int) get_option( 'default_' . $taxonomy );
161
162 // Assign default language to default term.
163 if ( ! $this->model->term->get_language( $default ) ) {
164 $this->model->term->set_language( $default, $args['slug'] );
165 } elseif ( empty( $args['no_default_cat'] ) && ! $this->model->term->get( $default, $args['slug'] ) ) {
166 $this->create_default_term( $args['slug'], $taxonomy );
167 }
168 }
169 }
170 }
171
172 /**
173 * Prevents deleting all the translations of the default term.
174 *
175 * @since 2.1
176 *
177 * @param array $caps The user's actual capabilities.
178 * @param string $cap Capability name.
179 * @param int $user_id The user ID.
180 * @param array $args Adds the context to the cap. The term id.
181 * @return array
182 */
183 public function fix_delete_default_term( $caps, $cap, $user_id, $args ) {
184 if ( 'delete_term' === $cap && $this->is_default_term( reset( $args ) ) ) {
185 $caps[] = 'do_not_allow';
186 }
187
188 return $caps;
189 }
190
191 /**
192 * Checks if the term is the default term.
193 *
194 * @since 3.1
195 *
196 * @param int $term_id The term ID.
197 * @return bool True if the term is the default term, false otherwise.
198 */
199 public function is_default_term( $term_id ) {
200 $term = get_term( $term_id );
201 if ( $term instanceof WP_Term ) {
202 $default_term_id = get_option( 'default_' . $term->taxonomy );
203 return $default_term_id && in_array( $default_term_id, $this->model->term->get_translations( $term_id ) );
204 }
205 return false;
206 }
207
208 /**
209 * Updates the default term language.
210 *
211 * @since 3.1
212 *
213 * @param string $slug Language slug.
214 * @return void
215 */
216 public function update_default_term_language( $slug ) {
217 foreach ( $this->taxonomies as $taxonomy ) {
218 if ( 'category' === $taxonomy ) {
219 $default_cats = $this->model->term->get_translations( get_option( 'default_' . $taxonomy ) );
220 if ( isset( $default_cats[ $slug ] ) ) {
221 update_option( 'default_' . $taxonomy, $default_cats[ $slug ] );
222 }
223 }
224 }
225 }
226 }
227