PluginProbe
Polylang / trunk
Polylang vtrunk
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 / default-term.php

default-term.php in Polylang trunk, at src/default-term.php

248 lines 6.7 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 $current_filter = current_filter();
110 if ( ! $current_filter ) {
111 return;
112 }
113
114 $taxonomy = substr( $current_filter, 22 );
115
116 foreach ( $this->model->get_languages_list() as $language ) {
117 if ( $language->slug != $default_cat_lang->slug && ! $this->model->term->get_translation( $value, $language ) ) {
118 $this->create_default_term( $language, $taxonomy );
119 }
120 }
121 }
122
123 /**
124 * Creates a default term for a language.
125 *
126 * @since 1.2
127 *
128 * @param PLL_Language|string|int $lang Language.
129 * @param string $taxonomy The current taxonomy.
130 * @return void
131 */
132 public function create_default_term( $lang, $taxonomy ) {
133 $lang = $this->model->get_language( $lang );
134
135 // Create a new term.
136 // FIXME this is translated in admin language when we would like it in $lang
137 $cat_name = __( 'Uncategorized', 'polylang' );
138 $cat_slug = sanitize_title( $cat_name . '-' . $lang->slug );
139 $cat = wp_insert_term( $cat_name, $taxonomy, array( 'slug' => $cat_slug ) );
140
141 // Check that the term was not previously created (in case the language was deleted and recreated).
142 if ( is_array( $cat ) ) {
143 $cat = $cat['term_id'];
144 } elseif ( ! empty( $cat->get_error_data( 'term_exists' ) ) ) {
145 $cat = $cat->get_error_data( 'term_exists' );
146 } else {
147 return;
148 }
149
150 if ( ! is_numeric( $cat ) ) {
151 return;
152 }
153
154 // Set language.
155 $this->model->term->set_language( (int) $cat, $lang );
156
157 // This is a translation of the default term.
158 $default = get_option( 'default_' . $taxonomy );
159
160 if ( ! is_numeric( $default ) ) {
161 return;
162 }
163
164 $translations = $this->model->term->get_translations( (int) $default );
165
166 $this->model->term->save_translations( (int) $cat, $translations );
167 }
168
169 /**
170 * Manages the default term when new languages are created.
171 *
172 * @since 3.1
173 *
174 * @param array $args Argument used to create the language. @see `Model\Languages::add()`.
175 * @return void
176 */
177 public function handle_default_term_on_create_language( $args ) {
178 foreach ( $this->taxonomies as $taxonomy ) {
179 if ( 'category' === $taxonomy ) {
180 $default = get_option( 'default_' . $taxonomy );
181 $default = is_numeric( $default ) ? (int) $default : 0;
182
183 // Assign default language to default term.
184 if ( ! $this->model->term->get_language( $default ) ) {
185 $this->model->term->set_language( $default, $args['slug'] );
186 } elseif ( empty( $args['no_default_cat'] ) && ! $this->model->term->get( $default, $args['slug'] ) ) {
187 $this->create_default_term( $args['slug'], $taxonomy );
188 }
189 }
190 }
191 }
192
193 /**
194 * Prevents deleting all the translations of the default term.
195 *
196 * @since 2.1
197 *
198 * @param array $caps The user's actual capabilities.
199 * @param string $cap Capability name.
200 * @param int $user_id The user ID.
201 * @param array $args Adds the context to the cap. The term id.
202 * @return array
203 */
204 public function fix_delete_default_term( $caps, $cap, $user_id, $args ) {
205 if ( 'delete_term' === $cap && $this->is_default_term( reset( $args ) ) ) {
206 $caps[] = 'do_not_allow';
207 }
208
209 return $caps;
210 }
211
212 /**
213 * Checks if the term is the default term.
214 *
215 * @since 3.1
216 *
217 * @param int $term_id The term ID.
218 * @return bool True if the term is the default term, false otherwise.
219 */
220 public function is_default_term( $term_id ) {
221 $term = get_term( $term_id );
222 if ( $term instanceof WP_Term ) {
223 $default_term_id = get_option( 'default_' . $term->taxonomy );
224 return $default_term_id && in_array( $default_term_id, $this->model->term->get_translations( $term_id ) );
225 }
226 return false;
227 }
228
229 /**
230 * Updates the default term language.
231 *
232 * @since 3.1
233 *
234 * @param string $slug Language slug.
235 * @return void
236 */
237 public function update_default_term_language( $slug ) {
238 foreach ( $this->taxonomies as $taxonomy ) {
239 if ( 'category' === $taxonomy ) {
240 $default_cats = $this->model->term->get_translations( get_option( 'default_' . $taxonomy ) );
241 if ( isset( $default_cats[ $slug ] ) ) {
242 update_option( 'default_' . $taxonomy, $default_cats[ $slug ] );
243 }
244 }
245 }
246 }
247 }
248