PluginProbe
Polylang / 3.3
Polylang v3.3
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 / admin / admin-default-term.php

admin-default-term.php in Polylang 3.3, at admin/admin-default-term.php

269 lines 7.6 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.1
10 */
11 class PLL_Admin_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 $pref_lang;
26
27 /**
28 * Reference to Polylang options array
29 *
30 * @var array
31 */
32 protected $options;
33
34 /**
35 * Array of registered taxonomy names for which Polylang manages languages and translations.
36 *
37 * @var string[]
38 */
39 protected $taxonomies;
40
41 /**
42 * Constructor: setups properties.
43 *
44 * @since 3.1
45 *
46 * @param object $polylang
47 */
48 public function __construct( &$polylang ) {
49 $this->model = &$polylang->model;
50 $this->pref_lang = &$polylang->pref_lang;
51 $this->options = &$polylang->options;
52 $this->taxonomies = $this->model->get_translated_taxonomies();
53 }
54
55 /**
56 * Setups filters and actions needed.
57 *
58 * @since 3.1
59 *
60 * @return void
61 */
62 public function add_hooks() {
63 foreach ( $this->taxonomies as $taxonomy ) {
64 if ( 'category' === $taxonomy ) {
65 // Allows to get the default terms in all languages
66 add_filter( 'option_default_' . $taxonomy, array( $this, 'option_default_term' ) );
67 add_action( 'update_option_default_' . $taxonomy, array( $this, 'update_option_default_term' ), 10, 2 );
68
69 // Adds the language column in the 'Terms' table.
70 add_filter( 'manage_' . $taxonomy . '_custom_column', array( $this, 'term_column' ), 10, 3 );
71 }
72 }
73 add_action( 'pll_add_language', array( $this, 'handle_default_term_on_create_language' ) );
74
75 // The default term should be in the default language
76 add_action( 'pll_update_default_lang', array( $this, 'update_default_term_language' ) );
77
78 // Prevents deleting all the translations of the default term
79 add_filter( 'map_meta_cap', array( $this, 'fix_delete_default_term' ), 10, 4 );
80 }
81
82 /**
83 * Filters the default term in note below the term list table and in settings->writing dropdown
84 *
85 * @since 1.2
86 *
87 * @param int $taxonomy_term_id The taxonomy term id.
88 * @return int A taxonomy term id.
89 */
90 public function option_default_term( $taxonomy_term_id ) {
91 if ( isset( $this->pref_lang ) && $tr = $this->model->term->get( $taxonomy_term_id, $this->pref_lang ) ) {
92 $taxonomy_term_id = $tr;
93 }
94 return $taxonomy_term_id;
95 }
96
97 /**
98 * Checks if the new default term is translated in all languages
99 * If not, create the translations
100 *
101 * @since 1.7
102 *
103 * @param int $old_value The old option value.
104 * @param int $value The new option value.
105 * @return void
106 */
107 public function update_option_default_term( $old_value, $value ) {
108 $default_cat_lang = $this->model->term->get_language( $value );
109
110 // Assign a default language to default term
111 if ( ! $default_cat_lang ) {
112 $default_cat_lang = $this->model->get_language( $this->options['default_lang'] );
113 $this->model->term->set_language( (int) $value, $default_cat_lang );
114 }
115
116 $taxonomy = substr( current_filter(), 22 );
117
118 foreach ( $this->model->get_languages_list() as $language ) {
119 if ( $language->slug != $default_cat_lang->slug && ! $this->model->term->get_translation( $value, $language ) ) {
120 $this->create_default_term( $language, $taxonomy );
121 }
122 }
123 }
124
125 /**
126 * Create a default term for a language
127 *
128 * @since 1.2
129 *
130 * @param object|string|int $lang language
131 * @param string $taxonomy The current taxonomy
132 * @return void
133 */
134 public function create_default_term( $lang, $taxonomy ) {
135 $lang = $this->model->get_language( $lang );
136
137 // create a new term
138 // FIXME this is translated in admin language when we would like it in $lang
139 $cat_name = __( 'Uncategorized', 'polylang' );
140 $cat_slug = sanitize_title( $cat_name . '-' . $lang->slug );
141 $cat = wp_insert_term( $cat_name, $taxonomy, array( 'slug' => $cat_slug ) );
142
143 // check that the term was not previously created ( in case the language was deleted and recreated )
144 $cat = isset( $cat->error_data['term_exists'] ) ? $cat->error_data['term_exists'] : $cat['term_id'];
145
146 // set language
147 $this->model->term->set_language( (int) $cat, $lang );
148
149 // this is a translation of the default term
150 $default = (int) get_option( 'default_' . $taxonomy );
151 $translations = $this->model->term->get_translations( $default );
152
153 $this->model->term->save_translations( (int) $cat, $translations );
154 }
155
156 /**
157 * Manages the default term when new languages are created.
158 *
159 * @since 3.1
160 *
161 * @param array $args Argument used to create the language. @see PLL_Admin_Model::add_language().
162 * @return void
163 */
164 public function handle_default_term_on_create_language( $args ) {
165 foreach ( $this->taxonomies as $taxonomy ) {
166 if ( 'category' === $taxonomy ) {
167 $default = (int) get_option( 'default_' . $taxonomy );
168
169 // Assign default language to default term
170 if ( ! $this->model->term->get_language( $default ) ) {
171 $this->model->term->set_language( $default, $args['slug'] );
172 } elseif ( empty( $args['no_default_cat'] ) && ! $this->model->term->get( $default, $args['slug'] ) ) {
173 $this->create_default_term( $args['slug'], $taxonomy );
174 }
175 }
176 }
177 }
178
179 /**
180 * Identify the default term in the terms list table to disable the language dropdown in js.
181 *
182 * @since 3.1
183 *
184 * @param string $out The output.
185 * @param string $column The custom column's name.
186 * @param int $term_id The term id.
187 * @return string The HTML string.
188 */
189 public function term_column( $out, $column, $term_id ) {
190 if ( $column === $this->get_first_language_column() && $this->is_default_term( $term_id ) ) {
191 $out .= sprintf( '<div class="hidden" id="default_cat_%1$d">%1$d</div>', intval( $term_id ) );
192 }
193
194 return $out;
195 }
196
197 /**
198 * Returns the first language column in the posts, pages and media library tables
199 *
200 * @since 0.9
201 *
202 * @return string first language column name
203 */
204 protected function get_first_language_column() {
205 $columns = array();
206
207 foreach ( $this->model->get_languages_list() as $language ) {
208 $columns[] = 'language_' . $language->slug;
209 }
210
211 return empty( $columns ) ? '' : reset( $columns );
212 }
213
214 /**
215 * Prevents deleting all the translations of the default term
216 *
217 * @since 2.1
218 *
219 * @param array $caps The user's actual capabilities.
220 * @param string $cap Capability name.
221 * @param int $user_id The user ID.
222 * @param array $args Adds the context to the cap. The term id.
223 * @return array
224 */
225 public function fix_delete_default_term( $caps, $cap, $user_id, $args ) {
226 if ( 'delete_term' === $cap && $this->is_default_term( reset( $args ) ) ) {
227 $caps[] = 'do_not_allow';
228 }
229
230 return $caps;
231 }
232
233 /**
234 * Check if the term is the default term.
235 *
236 * @since 3.1
237 *
238 * @param int $term_id The term id.
239 * @return bool True if the term is the default term, false otherwise.
240 */
241 public function is_default_term( $term_id ) {
242 $term = get_term( $term_id );
243 if ( $term instanceof WP_Term ) {
244 $default_term_id = get_option( 'default_' . $term->taxonomy );
245 return $default_term_id && in_array( $default_term_id, $this->model->term->get_translations( $term_id ) );
246 }
247 return false;
248 }
249
250 /**
251 * Updates the default term language.
252 *
253 * @since 3.1
254 *
255 * @param string $slug Language slug.
256 * @return void
257 */
258 public function update_default_term_language( $slug ) {
259 foreach ( $this->taxonomies as $taxonomy ) {
260 if ( 'category' === $taxonomy ) {
261 $default_cats = $this->model->term->get_translations( get_option( 'default_' . $taxonomy ) );
262 if ( isset( $default_cats[ $slug ] ) ) {
263 update_option( 'default_' . $taxonomy, $default_cats[ $slug ] );
264 }
265 }
266 }
267 }
268 }
269