PluginProbe
Polylang / 3.7.7
Polylang v3.7.7
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
← All changes | include/model.php +364 -430 2.8.33.7.7 View file →
@@ -2,436 +2,260 @@
2 2 /**
3 3 * @package Polylang
4 4 */
5 5
6 +use WP_Syntex\Polylang\Model;
7 +use WP_Syntex\Polylang\Options\Options;
8 +
6 9 /**
7 - * Setups the language and translations model based on WordPress taxonomies
10 + * Setups the language and translations model based on WordPress taxonomies.
8 11 *
9 12 * @since 1.2
13 + *
14 + * @method bool has_languages() Checks if there are languages or not. See `Model\Languages::has()`.
15 + * @method array get_languages_list(array $args = array()) Returns the list of available languages. See `Model\Languages::get_list()`.
16 + * @method bool are_languages_ready() Tells if get_languages_list() can be used. See `Model\Languages::are_ready()`.
17 + * @method void set_languages_ready() Sets the internal property `$languages_ready` to `true`, telling that get_languages_list() can be used. See `Model\Languages::set_ready()`.
18 + * @method PLL_Language|false get_language(mixed $value) Returns the language by its term_id, tl_term_id, slug or locale. See `Model\Languages::get()`.
19 + * @method true|WP_Error add_language(array $args) Adds a new language and creates a default category for this language. See `Model\Languages::add()`.
20 + * @method bool delete_language(int $lang_id) Deletes a language. See `Model\Languages::delete()`.
21 + * @method true|WP_Error update_language(array $args) Updates language properties. See `Model\Languages::update()`.
22 + * @method PLL_Language|false get_default_language() Returns the default language. See `Model\Languages::get_default()`.
23 + * @method void update_default_lang(string $slug) Updates the default language. See `Model\Languages::update_default()`.
24 + * @method void maybe_create_language_terms() Maybe adds the missing language terms for 3rd party language taxonomies. See `Model\Languages::maybe_create_terms()`.
25 + * @method string[] get_translated_post_types(bool $filter = true) Returns post types that need to be translated. See `Model\Post_Types::get_translated()`.
26 + * @method bool is_translated_post_type(string|string[] $post_type) Returns true if Polylang manages languages and translations for this post type. See `Model\Post_Types::is_translated()`.
27 + * @method string[] get_translated_taxonomies(bool $filter = true) Returns taxonomies that need to be translated. See `Model\Taxonomies::get_translated()`.
28 + * @method bool is_translated_taxonomy(string|string[] $tax) Returns true if Polylang manages languages and translations for this taxonomy. See `Model\Taxonomies::is_translated()`.
29 + * @method string[] get_filtered_taxonomies(bool $filter = true) Return taxonomies that need to be filtered (post_format like). See `Model\Taxonomies::get_filtered()`.
30 + * @method bool is_filtered_taxonomy(string|string[] $tax) Returns true if Polylang filters this taxonomy per language. See `Model\Taxonomies::is_filtered()`.
31 + * @method string[] get_filtered_taxonomies_query_vars() Returns the query vars of all filtered taxonomies. See `Model\Taxonomies::get_filtered_query_vars()`.
10 32 */
11 33 class PLL_Model {
12 - public $cache; // Our internal non persistent cache object
13 - public $options;
14 - public $post, $term; // Translated objects models
15 -
16 34 /**
17 - * Constructor
18 - * setups translated objects sub models
19 - * setups filters and actions
35 + * Internal non persistent cache object.
20 36 *
21 - * @since 1.2
22 - *
23 - * @param array $options Polylang options
37 + * @var PLL_Cache<mixed>
24 38 */
25 - public function __construct( &$options ) {
26 - $this->options = &$options;
39 + public $cache;
27 40
28 - $this->cache = new PLL_Cache();
29 - $this->post = new PLL_Translated_Post( $this ); // translated post sub model
30 - $this->term = new PLL_Translated_Term( $this ); // translated term sub model
31 -
32 - // We need to clean languages cache when editing a language and when modifying the permalink structure
33 - add_action( 'edited_term_taxonomy', array( $this, 'clean_languages_cache' ), 10, 2 );
34 - add_action( 'update_option_permalink_structure', array( $this, 'clean_languages_cache' ) );
35 - add_action( 'update_option_siteurl', array( $this, 'clean_languages_cache' ) );
36 - add_action( 'update_option_home', array( $this, 'clean_languages_cache' ) );
37 -
38 - add_filter( 'get_terms_args', array( $this, 'get_terms_args' ) );
39 -
40 - // Just in case someone would like to display the language description ;- )
41 - add_filter( 'language_description', '__return_empty_string' );
42 - }
43 -
44 41 /**
45 - * Returns the list of available languages
46 - * caches the list in a db transient ( except flags ), unless PLL_CACHE_LANGUAGES is set to false
47 - * caches the list ( with flags ) in the private property $languages
42 + * Stores the plugin options.
48 43 *
49 - * List of parameters accepted in $args:
50 - *
51 - * hide_empty => hides languages with no posts if set to true ( defaults to false )
52 - * fields => return only that field if set ( see PLL_Language for a list of fields )
53 - *
54 - * @since 0.1
55 - *
56 - * @param array $args
57 - * @return array|string|int list of PLL_Language objects or PLL_Language object properties
44 + * @var Options
58 45 */
59 - public function get_languages_list( $args = array() ) {
60 - if ( false === $languages = $this->cache->get( 'languages' ) ) {
46 + public $options;
61 47
62 - // Create the languages from taxonomies
63 - if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || false === ( $languages = get_transient( 'pll_languages_list' ) ) ) {
64 - $languages = get_terms( 'language', array( 'hide_empty' => false, 'orderby' => 'term_group' ) );
65 - $languages = empty( $languages ) || is_wp_error( $languages ) ? array() : $languages;
66 -
67 - $term_languages = get_terms( 'term_language', array( 'hide_empty' => false ) );
68 - $term_languages = empty( $term_languages ) || is_wp_error( $term_languages ) ?
69 - array() : array_combine( wp_list_pluck( $term_languages, 'slug' ), $term_languages );
70 -
71 - if ( ! empty( $languages ) && ! empty( $term_languages ) ) {
72 - // Don't use array_map + create_function to instantiate an autoloaded class as it breaks badly in old versions of PHP
73 - foreach ( $languages as $k => $v ) {
74 - $languages[ $k ] = new PLL_Language( $v, $term_languages[ 'pll_' . $v->slug ] );
75 - }
76 -
77 - // We will need the languages list to allow its access in the filter below
78 - $this->cache->set( 'languages', $languages );
79 -
80 - /**
81 - * Filter the list of languages *before* it is stored in the persistent cache
82 - * /!\ this filter is fired *before* the $polylang object is available
83 - *
84 - * @since 1.7.5
85 - *
86 - * @param array $languages the list of language objects
87 - * @param object $model PLL_Model object
88 - */
89 - $languages = apply_filters( 'pll_languages_list', $languages, $this );
90 -
91 - // Don't store directly objects as it badly break with some hosts ( GoDaddy ) due to race conditions when using object cache
92 - // Thanks to captin411 for catching this!
93 - // See https://wordpress.org/support/topic/fatal-error-pll_model_languages_list?replies=8#post-6782255;
94 - set_transient( 'pll_languages_list', array_map( 'get_object_vars', $languages ) );
95 - }
96 - else {
97 - $languages = array(); // In case something went wrong
98 - }
99 - }
100 -
101 - // Create the languages directly from arrays stored in transients
102 - else {
103 - foreach ( $languages as $k => $v ) {
104 - $languages[ $k ] = new PLL_Language( $v );
105 - }
106 - }
107 -
108 - /**
109 - * Filter the list of languages *after* it is stored in the persistent cache
110 - * /!\ this filter is fired *before* the $polylang object is available
111 - *
112 - * @since 1.8
113 - *
114 - * @param array $languages the list of language objects
115 - */
116 - $languages = apply_filters( 'pll_after_languages_cache', $languages );
117 - $this->cache->set( 'languages', $languages );
118 - }
119 -
120 - $args = wp_parse_args( $args, array( 'hide_empty' => false ) );
121 -
122 - // Remove empty languages if requested
123 - if ( $args['hide_empty'] ) {
124 - $languages = wp_list_filter( $languages, array( 'count' => 0 ), 'NOT' );
125 - }
126 -
127 - return empty( $args['fields'] ) ? $languages : wp_list_pluck( $languages, $args['fields'] );
128 - }
129 -
130 48 /**
131 - * Cleans language cache
132 - * can be called directly with no parameter
133 - * called by the 'edited_term_taxonomy' filter with 2 parameters when count needs to be updated
49 + * Translatable objects registry.
134 50 *
135 - * @since 1.2
51 + * @since 3.4
136 52 *
137 - * @param int $term not used
138 - * @param string $taxonomy taxonomy name
53 + * @var PLL_Translatable_Objects
139 54 */
140 - public function clean_languages_cache( $term = 0, $taxonomy = null ) {
141 - if ( empty( $taxonomy ) || 'language' == $taxonomy ) {
142 - delete_transient( 'pll_languages_list' );
143 - $this->cache->clean();
144 - }
145 - }
55 + public $translatable_objects;
146 56
147 57 /**
148 - * Don't query term metas when only our taxonomies are queried
58 + * Translated post model.
149 59 *
150 - * @since 2.3
151 - *
152 - * @param array $args WP_Term_Query arguments
153 - * @return array
60 + * @var PLL_Translated_Post
154 61 */
155 - public function get_terms_args( $args ) {
156 - if ( isset( $args['taxonomy'] ) && ! array_diff( (array) $args['taxonomy'], array( 'language', 'term_language', 'post_translations', 'term_translations' ) ) ) {
157 - $args['update_term_meta_cache'] = false;
158 - }
159 - return $args;
160 - }
62 + public $post;
161 63
162 64 /**
163 - * Returns the language by its term_id, tl_term_id, slug or locale
65 + * Translated term model.
164 66 *
165 - * @since 0.1
166 - *
167 - * @param int|string $value term_id, tl_term_id, slug or locale of the queried language
168 - * @return object|bool PLL_Language object, false if no language found
67 + * @var PLL_Translated_Term
169 68 */
170 - public function get_language( $value ) {
171 - if ( is_object( $value ) ) {
172 - return $value instanceof PLL_Language ? $value : $this->get_language( $value->term_id ); // will force cast to PLL_Language
173 - }
69 + public $term;
174 70
175 - if ( false === $return = $this->cache->get( 'language:' . $value ) ) {
176 - foreach ( $this->get_languages_list() as $lang ) {
177 - $this->cache->set( 'language:' . $lang->term_id, $lang );
178 - $this->cache->set( 'language:' . $lang->tl_term_id, $lang );
179 - $this->cache->set( 'language:' . $lang->slug, $lang );
180 - $this->cache->set( 'language:' . $lang->locale, $lang );
181 - $this->cache->set( 'language:' . $lang->w3c, $lang );
182 - }
183 - $return = $this->cache->get( 'language:' . $value );
184 - }
185 -
186 - return $return;
187 - }
188 -
189 71 /**
190 - * Adds terms clauses to get_terms to filter them by languages - used in both frontend and admin
72 + * Model for the languages.
191 73 *
192 - * @since 1.2
193 - *
194 - * @param array $clauses the list of sql clauses in terms query
195 - * @param object $lang PLL_Language object
196 - * @return array modified list of clauses
74 + * @var Model\Languages
197 75 */
198 - public function terms_clauses( $clauses, $lang ) {
199 - if ( ! empty( $lang ) && false === strpos( $clauses['join'], 'pll_tr' ) ) {
200 - $clauses['join'] .= $this->term->join_clause();
201 - $clauses['where'] .= $this->term->where_clause( $lang );
202 - }
203 - return $clauses;
204 - }
76 + public $languages;
205 77
206 78 /**
207 - * Returns post types that need to be translated
208 - * the post types list is cached for better better performance
209 - * wait for 'after_setup_theme' to apply the cache to allow themes adding the filter in functions.php
79 + * Model for taxonomies translated by Polylang.
210 80 *
211 - * @since 1.2
212 - *
213 - * @param bool $filter true if we should return only valid registered post types
214 - * @return array post type names for which Polylang manages languages and translations
81 + * @var Model\Post_Types
215 82 */
216 - public function get_translated_post_types( $filter = true ) {
217 - if ( false === $post_types = $this->cache->get( 'post_types' ) ) {
218 - $post_types = array( 'post' => 'post', 'page' => 'page', 'wp_block' => 'wp_block' );
83 + public $post_types;
219 84
220 - if ( ! empty( $this->options['media_support'] ) ) {
221 - $post_types['attachment'] = 'attachment';
222 - }
223 -
224 - if ( ! empty( $this->options['post_types'] ) && is_array( $this->options['post_types'] ) ) {
225 - $post_types = array_merge( $post_types, array_combine( $this->options['post_types'], $this->options['post_types'] ) );
226 - }
227 -
228 - /**
229 - * Filter the list of post types available for translation.
230 - * The default are post types which have the parameter ‘public’ set to true.
231 - * The filter must be added soon in the WordPress loading process:
232 - * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
233 - *
234 - * @since 0.8
235 - *
236 - * @param array $post_types list of post type names
237 - * @param bool $is_settings true when displaying the list of custom post types in Polylang settings
238 - */
239 - $post_types = apply_filters( 'pll_get_post_types', $post_types, false );
240 -
241 - if ( did_action( 'after_setup_theme' ) ) {
242 - $this->cache->set( 'post_types', $post_types );
243 - }
244 - }
245 -
246 - return $filter ? array_intersect( $post_types, get_post_types() ) : $post_types;
247 - }
248 -
249 85 /**
250 - * Returns true if Polylang manages languages and translations for this post type
86 + * Model for taxonomies filtered/translated by Polylang.
251 87 *
252 - * @since 1.2
253 - *
254 - * @param string|array $post_type post type name or array of post type names
255 - * @return bool
88 + * @var Model\Taxonomies
256 89 */
257 - public function is_translated_post_type( $post_type ) {
258 - $post_types = $this->get_translated_post_types( false );
259 - return ( is_array( $post_type ) && array_intersect( $post_type, $post_types ) || in_array( $post_type, $post_types ) || 'any' === $post_type && ! empty( $post_types ) );
260 - }
90 + public $taxonomies;
261 91
262 92 /**
263 - * Return taxonomies that need to be translated
93 + * Constructor.
94 + * Setups translated objects sub models.
95 + * Setups filters and actions.
264 96 *
265 97 * @since 1.2
98 + * @since 3.7 Type of parameter `$options` changed from `array` to `Options`.
266 99 *
267 - * @param bool $filter true if we should return only valid registered taxonomies
268 - * @return array array of registered taxonomy names for which Polylang manages languages and translations
100 + * @param Options $options Polylang options.
269 101 */
270 - public function get_translated_taxonomies( $filter = true ) {
271 - if ( false === $taxonomies = $this->cache->get( 'taxonomies' ) ) {
272 - $taxonomies = array( 'category' => 'category', 'post_tag' => 'post_tag' );
102 + public function __construct( Options &$options ) {
103 + $this->options = &$options;
104 + $this->cache = new PLL_Cache();
105 + $this->translatable_objects = new PLL_Translatable_Objects();
106 + $this->languages = new Model\Languages( $this->options, $this->translatable_objects, $this->cache );
273 107
274 - if ( ! empty( $this->options['taxonomies'] ) && is_array( $this->options['taxonomies'] ) ) {
275 - $taxonomies = array_merge( $taxonomies, array_combine( $this->options['taxonomies'], $this->options['taxonomies'] ) );
276 - }
108 + $this->post = $this->translatable_objects->register( new PLL_Translated_Post( $this ) ); // Translated post sub model.
109 + $this->term = $this->translatable_objects->register( new PLL_Translated_Term( $this ) ); // Translated term sub model.
277 110
278 - /**
279 - * Filter the list of taxonomies available for translation.
280 - * The default are taxonomies which have the parameter ‘public’ set to true.
281 - * The filter must be added soon in the WordPress loading process:
282 - * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
283 - *
284 - * @since 0.8
285 - *
286 - * @param array $taxonomies list of taxonomy names
287 - * @param bool $is_settings true when displaying the list of custom taxonomies in Polylang settings
288 - */
289 - $taxonomies = apply_filters( 'pll_get_taxonomies', $taxonomies, false );
290 - if ( did_action( 'after_setup_theme' ) ) {
291 - $this->cache->set( 'taxonomies', $taxonomies );
292 - }
293 - }
111 + $this->post_types = new Model\Post_Types( $this->post );
112 + $this->taxonomies = new Model\Taxonomies( $this->term );
294 113
295 - return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies;
296 - }
114 + // We need to clean languages cache when editing a language and when modifying the permalink structure.
115 + add_action( 'edited_term_taxonomy', array( $this, 'clean_languages_cache' ), 10, 2 );
116 + add_action( 'update_option_permalink_structure', array( $this, 'clean_languages_cache' ) );
117 + add_action( 'update_option_siteurl', array( $this, 'clean_languages_cache' ) );
118 + add_action( 'update_option_home', array( $this, 'clean_languages_cache' ) );
297 119
298 - /**
299 - * Returns true if Polylang manages languages and translations for this taxonomy
300 - *
301 - * @since 1.2
302 - *
303 - * @param string|array $tax taxonomy name or array of taxonomy names
304 - * @return bool
305 - */
306 - public function is_translated_taxonomy( $tax ) {
307 - $taxonomies = $this->get_translated_taxonomies( false );
308 - return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) );
120 + add_filter( 'get_terms_args', array( $this, 'get_terms_args' ) );
121 +
122 + // Just in case someone would like to display the language description ;).
123 + add_filter( 'language_description', '__return_empty_string' );
309 124 }
310 125
311 126 /**
312 - * Return taxonomies that need to be filtered ( post_format like )
127 + * Backward compatibility for methods that have been moved to sub-models.
313 128 *
314 - * @since 1.7
129 + * @since 3.7
315 130 *
316 - * @param bool $filter true if we should return only valid registered taxonomies
317 - * @return array array of registered taxonomy names
131 + * @param string $name Name of the method being called.
132 + * @param array $arguments Enumerated array containing the parameters passed to the $name'ed method.
133 + * @return mixed
318 134 */
319 - public function get_filtered_taxonomies( $filter = true ) {
320 - if ( did_action( 'after_setup_theme' ) ) {
321 - static $taxonomies = null;
322 - }
135 + public function __call( string $name, array $arguments ) {
136 + $methods = array(
137 + // Languages.
138 + 'has_languages' => array( $this->languages, 'has' ),
139 + 'get_languages_list' => array( $this->languages, 'get_list' ),
140 + 'are_languages_ready' => array( $this->languages, 'are_ready' ),
141 + 'set_languages_ready' => array( $this->languages, 'set_ready' ),
142 + 'get_language' => array( $this->languages, 'get' ),
143 + 'add_language' => array( $this->languages, 'add' ),
144 + 'delete_language' => array( $this->languages, 'delete' ),
145 + 'update_language' => array( $this->languages, 'update' ),
146 + 'get_default_language' => array( $this->languages, 'get_default' ),
147 + 'update_default_lang' => array( $this->languages, 'update_default' ),
148 + 'maybe_create_language_terms' => array( $this->languages, 'maybe_create_terms' ),
149 + // Post types.
150 + 'get_translated_post_types' => array( $this->post_types, 'get_translated' ),
151 + 'is_translated_post_type' => array( $this->post_types, 'is_translated' ),
152 + // Taxonomies.
153 + 'get_translated_taxonomies' => array( $this->taxonomies, 'get_translated' ),
154 + 'is_translated_taxonomy' => array( $this->taxonomies, 'is_translated' ),
155 + 'get_filtered_taxonomies' => array( $this->taxonomies, 'get_filtered' ),
156 + 'is_filtered_taxonomy' => array( $this->taxonomies, 'is_filtered' ),
157 + 'get_filtered_taxonomies_query_vars' => array( $this->taxonomies, 'get_filtered_query_vars' ),
158 + );
323 159
324 - if ( empty( $taxonomies ) ) {
325 - $taxonomies = array( 'post_format' => 'post_format' );
326 -
327 - /**
328 - * Filter the list of taxonomies not translatable but filtered by language.
329 - * Includes only the post format by default
330 - * The filter must be added soon in the WordPress loading process:
331 - * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
332 - *
333 - * @since 1.7
334 - *
335 - * @param array $taxonomies list of taxonomy names
336 - * @param bool $is_settings true when displaying the list of custom taxonomies in Polylang settings
337 - */
338 - $taxonomies = apply_filters( 'pll_filtered_taxonomies', $taxonomies, false );
160 + if ( isset( $methods[ $name ] ) ) {
161 + return call_user_func_array( $methods[ $name ], $arguments );
339 162 }
340 163
341 - return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies;
164 + $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
165 + trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions
166 + sprintf(
167 + 'Call to undefined function PLL()->model->%1$s() in %2$s on line %3$s' . "\nError handler",
168 + esc_html( $name ),
169 + esc_html( $debug[0]['file'] ?? '' ),
170 + absint( $debug[0]['line'] ?? 0 )
171 + ),
172 + E_USER_ERROR
173 + );
342 174 }
343 175
344 176 /**
345 - * Returns true if Polylang filters this taxonomy per language
177 + * Cleans language cache
178 + * can be called directly with no parameter
179 + * called by the 'edited_term_taxonomy' filter with 2 parameters when count needs to be updated
346 180 *
347 - * @since 1.7
181 + * @since 1.2
348 182 *
349 - * @param string|array $tax taxonomy name or array of taxonomy names
350 - * @return bool
183 + * @param int $term not used
184 + * @param string $taxonomy taxonomy name
185 + * @return void
351 186 */
352 - public function is_filtered_taxonomy( $tax ) {
353 - $taxonomies = $this->get_filtered_taxonomies( false );
354 - return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) );
187 + public function clean_languages_cache( $term = 0, $taxonomy = null ): void {
188 + if ( empty( $taxonomy ) || 'language' === $taxonomy ) {
189 + $this->languages->clean_cache();
190 + }
355 191 }
356 192
357 193 /**
358 - * Returns the query vars of all filtered taxonomies
194 + * Don't query term metas when only our taxonomies are queried
359 195 *
360 - * @since 1.7
196 + * @since 2.3
361 197 *
198 + * @param array $args WP_Term_Query arguments
362 199 * @return array
363 200 */
364 - public function get_filtered_taxonomies_query_vars() {
365 - $query_vars = array();
366 - foreach ( $this->get_filtered_taxonomies() as $filtered_tax ) {
367 - $tax = get_taxonomy( $filtered_tax );
368 - $query_vars[] = $tax->query_var;
201 + public function get_terms_args( $args ) {
202 + $taxonomies = $this->translatable_objects->get_taxonomy_names();
203 +
204 + if ( isset( $args['taxonomy'] ) && ! array_diff( (array) $args['taxonomy'], $taxonomies ) ) {
205 + $args['update_term_meta_cache'] = false;
369 206 }
370 - return $query_vars;
207 + return $args;
371 208 }
372 209
373 210 /**
374 - * Create a default category for a language
211 + * Adds terms clauses to the term query to filter them by languages.
375 212 *
376 213 * @since 1.2
377 214 *
378 - * @param object|string|int $lang language
215 + * @param string[] $clauses The list of sql clauses in terms query.
216 + * @param PLL_Language|false $lang PLL_Language object.
217 + * @return string[] Modified list of clauses.
379 218 */
380 - public function create_default_category( $lang ) {
381 - $lang = $this->get_language( $lang );
382 -
383 - // create a new category
384 - // FIXME this is translated in admin language when we would like it in $lang
385 - $cat_name = __( 'Uncategorized', 'polylang' );
386 - $cat_slug = sanitize_title( $cat_name . '-' . $lang->slug );
387 - $cat = wp_insert_term( $cat_name, 'category', array( 'slug' => $cat_slug ) );
388 -
389 - // check that the category was not previously created ( in case the language was deleted and recreated )
390 - $cat = isset( $cat->error_data['term_exists'] ) ? $cat->error_data['term_exists'] : $cat['term_id'];
391 -
392 - // set language
393 - $this->term->set_language( (int) $cat, $lang );
394 -
395 - // this is a translation of the default category
396 - $default = (int) get_option( 'default_category' );
397 - $translations = $this->term->get_translations( $default );
398 - if ( empty( $translations ) ) {
399 - if ( $lg = $this->term->get_language( $default ) ) {
400 - $translations[ $lg->slug ] = $default;
401 - }
402 - else {
403 - $translations = array();
404 - }
219 + public function terms_clauses( $clauses, $lang ) {
220 + if ( ! empty( $lang ) && false === strpos( $clauses['join'], 'pll_tr' ) ) {
221 + $clauses['join'] .= $this->term->join_clause();
222 + $clauses['where'] .= $this->term->where_clause( $lang );
405 223 }
406 -
407 - $this->term->save_translations( (int) $cat, $translations );
224 + return $clauses;
408 225 }
409 226
410 227 /**
411 228 * It is possible to have several terms with the same name in the same taxonomy ( one per language )
412 - * but the native term_exists will return true even if only one exists
413 - * so here the function adds the language parameter
229 + * but the native term_exists() will return true even if only one exists.
230 + * So here the function adds the language parameter.
414 231 *
415 232 * @since 1.4
416 233 *
417 - * @param string $term_name the term name
418 - * @param string $taxonomy taxonomy name
419 - * @param int $parent parent term id
420 - * @param string|object $language the language slug or object
421 - * @return null|int the term_id of the found term
234 + * @param string $term_name The term name.
235 + * @param string $taxonomy Taxonomy name.
236 + * @param int $parent Parent term id.
237 + * @param string|PLL_Language $language The language slug or object.
238 + * @return int The `term_id` of the found term. 0 otherwise.
239 + *
240 + * @phpstan-return int<0, max>
422 241 */
423 - public function term_exists( $term_name, $taxonomy, $parent, $language ) {
242 + public function term_exists( $term_name, $taxonomy, $parent, $language ): int {
424 243 global $wpdb;
425 244
245 + $language = $this->languages->get( $language );
246 + if ( empty( $language ) ) {
247 + return 0;
248 + }
249 +
426 250 $term_name = trim( wp_unslash( $term_name ) );
427 251 $term_name = _wp_specialchars( $term_name );
428 252
429 253 $select = "SELECT t.term_id FROM $wpdb->terms AS t";
430 - $join = " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
431 - $join .= $this->term->join_clause();
432 - $where = $wpdb->prepare( ' WHERE tt.taxonomy = %s AND t.name = %s', $taxonomy, $term_name );
433 - $where .= $this->term->where_clause( $this->get_language( $language ) );
254 + $join = " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
255 + $join .= $this->term->join_clause();
256 + $where = $wpdb->prepare( ' WHERE tt.taxonomy = %s AND t.name = %s', $taxonomy, $term_name );
257 + $where .= $this->term->where_clause( $language );
434 258
435 259 if ( $parent > 0 ) {
436 260 $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent );
437 261 }
@@ -436,31 +260,37 @@
436 260 $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent );
437 261 }
438 262
439 263 // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
440 - return $wpdb->get_var( $select . $join . $where );
264 + $term_id = $wpdb->get_var( $select . $join . $where );
265 + return max( 0, (int) $term_id );
441 266 }
442 267
443 268 /**
444 - * Checks if a term slug exists in a given language, taxonomy, hierarchy
269 + * Checks if a term slug exists in a given language, taxonomy, hierarchy.
445 270 *
446 271 * @since 1.9
447 - * @since 2.8 Moved from PLL_Share_Term_Slug::term_exists() to PLL_Model::term_exists_by_slug()
272 + * @since 2.8 Moved from PLL_Share_Term_Slug::term_exists() to PLL_Model::term_exists_by_slug().
448 273 *
449 - * @param string $slug The term slug to test.
450 - * @param string|object $language The language slug or object.
451 - * @param string $taxonomy Optional taxonomy name.
452 - * @param int $parent Optional parent term id.
453 - * @return null|int The term_id of the found term.
274 + * @param string $slug The term slug to test.
275 + * @param string|PLL_Language $language The language slug or object.
276 + * @param string $taxonomy Optional taxonomy name.
277 + * @param int $parent Optional parent term id.
278 + * @return int The `term_id` of the found term. 0 otherwise.
454 279 */
455 - public function term_exists_by_slug( $slug, $language, $taxonomy = '', $parent = 0 ) {
280 + public function term_exists_by_slug( $slug, $language, $taxonomy = '', $parent = 0 ): int {
456 281 global $wpdb;
457 282
283 + $language = $this->languages->get( $language );
284 + if ( empty( $language ) ) {
285 + return 0;
286 + }
287 +
458 288 $select = "SELECT t.term_id FROM {$wpdb->terms} AS t";
459 289 $join = " INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id";
460 290 $join .= $this->term->join_clause();
461 291 $where = $wpdb->prepare( ' WHERE t.slug = %s', $slug );
462 - $where .= $this->term->where_clause( $this->get_language( $language ) );
292 + $where .= $this->term->where_clause( $language );
463 293
464 294 if ( ! empty( $taxonomy ) ) {
465 295 $where .= $wpdb->prepare( ' AND tt.taxonomy = %s', $taxonomy );
466 296 }
@@ -469,25 +299,49 @@
469 299 $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent );
470 300 }
471 301
472 302 // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
473 - return $wpdb->get_var( $select . $join . $where );
303 + return (int) $wpdb->get_var( $select . $join . $where );
474 304 }
475 305
476 -
477 306 /**
478 - * Gets the number of posts per language in a date, author or post type archive.
307 + * Returns the number of posts per language in a date, author or post type archive.
479 308 *
480 309 * @since 1.2
481 310 *
482 - * @param object $lang PLL_Language instance.
483 - * @param array $q WP_Query arguments ( accepted: post_type, m, year, monthnum, day, author, author_name, post_format, post_status ).
311 + * @param PLL_Language $lang PLL_Language instance.
312 + * @param array $q {
313 + * WP_Query arguments:
314 + *
315 + * @type string|string[] $post_type Post type or array of post types.
316 + * @type int $m Combination YearMonth. Accepts any four-digit year and month.
317 + * @type int $year Four-digit year.
318 + * @type int $monthnum Two-digit month.
319 + * @type int $day Day of the month.
320 + * @type int $author Author id.
321 + * @type string $author_name User 'user_nicename'.
322 + * @type string $post_format Post format.
323 + * @type string $post_status Post status.
324 + * }
484 325 * @return int
326 + *
327 + * @phpstan-param array{
328 + * post_type?: non-falsy-string|array<non-falsy-string>,
329 + * post_status?: non-falsy-string,
330 + * m?: numeric-string,
331 + * year?: positive-int,
332 + * monthnum?: int<1, 12>,
333 + * day?: int<1, 31>,
334 + * author?: int<1, max>,
335 + * author_name?: non-falsy-string,
336 + * post_format?: non-falsy-string
337 + * } $q
338 + * @phpstan-return int<0, max>
485 339 */
486 - public function count_posts( $lang, $q = array() ) {
340 + public function count_posts( $lang, $q = array() ): int {
487 341 global $wpdb;
488 342
489 - $q = wp_parse_args( $q, array( 'post_type' => 'post', 'post_status' => 'publish' ) );
343 + $q = array_merge( array( 'post_type' => 'post', 'post_status' => 'publish' ), $q );
490 344
491 345 if ( ! is_array( $q['post_type'] ) ) {
492 346 $q['post_type'] = array( $q['post_type'] );
493 347 }
@@ -501,17 +355,18 @@
501 355 if ( empty( $q['post_type'] ) ) {
502 356 $q['post_type'] = array( 'post' ); // We *need* a post type.
503 357 }
504 358
505 - $cache_key = 'pll_count_posts_' . md5( maybe_serialize( $q ) );
506 - $counts = wp_cache_get( $cache_key, 'counts' );
359 + $cache_key = $this->cache->get_unique_key( 'pll_count_posts_', $q );
360 + $counts = wp_cache_get( $cache_key, 'counts' );
507 361
508 - if ( false === $counts ) {
509 - $select = "SELECT pll_tr.term_taxonomy_id, COUNT( * ) AS num_posts FROM {$wpdb->posts}";
510 - $join = $this->post->join_clause();
511 - $where = sprintf( " WHERE post_status = '%s'", esc_sql( $q['post_status'] ) );
512 - $where .= sprintf( " AND {$wpdb->posts}.post_type IN ( '%s' )", join( "', '", esc_sql( $q['post_type'] ) ) );
513 - $where .= $this->post->where_clause( $this->get_languages_list() );
362 + if ( ! is_array( $counts ) ) {
363 + $counts = array();
364 + $select = "SELECT pll_tr.term_taxonomy_id, COUNT( * ) AS num_posts FROM {$wpdb->posts}";
365 + $join = $this->post->join_clause();
366 + $where = sprintf( " WHERE post_status = '%s'", esc_sql( $q['post_status'] ) );
367 + $where .= sprintf( " AND {$wpdb->posts}.post_type IN ( '%s' )", implode( "', '", esc_sql( $q['post_type'] ) ) );
368 + $where .= $this->post->where_clause( $this->languages->get_list() );
514 369 $groupby = ' GROUP BY pll_tr.term_taxonomy_id';
515 370
516 371 if ( ! empty( $q['m'] ) ) {
517 372 $q['m'] = '' . preg_replace( '|[^0-9]|', '', $q['m'] );
@@ -547,9 +402,9 @@
547 402 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $q['author'] );
548 403 }
549 404
550 405 // Filtered taxonomies ( post_format ).
551 - foreach ( $this->get_filtered_taxonomies_query_vars() as $tax_qv ) {
406 + foreach ( $this->taxonomies->get_filtered_query_vars() as $tax_qv ) {
552 407
553 408 if ( ! empty( $q[ $tax_qv ] ) ) {
554 409 $join .= " INNER JOIN {$wpdb->term_relationships} AS tr ON tr.object_id = {$wpdb->posts}.ID";
555 410 $join .= " INNER JOIN {$wpdb->term_taxonomy} AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id";
@@ -566,29 +421,30 @@
566 421
567 422 wp_cache_set( $cache_key, $counts, 'counts' );
568 423 }
569 424
570 - return empty( $counts[ $lang->term_taxonomy_id ] ) ? 0 : $counts[ $lang->term_taxonomy_id ];
425 + $term_taxonomy_id = $lang->get_tax_prop( 'language', 'term_taxonomy_id' );
426 + return empty( $counts[ $term_taxonomy_id ] ) ? 0 : $counts[ $term_taxonomy_id ];
571 427 }
572 428
573 429 /**
574 - * Setup the links model based on options
430 + * Setup the links model based on options.
575 431 *
576 432 * @since 1.2
577 433 *
578 - * @return object implementing "links_model interface"
434 + * @return PLL_Links_Model
579 435 */
580 - public function get_links_model() {
436 + public function get_links_model(): PLL_Links_Model {
581 437 $c = array( 'Directory', 'Directory', 'Subdomain', 'Domain' );
582 438 $class = get_option( 'permalink_structure' ) ? 'PLL_Links_' . $c[ $this->options['force_lang'] ] : 'PLL_Links_Default';
583 439
584 440 /**
585 - * Filter the links model class to use
586 - * /!\ this filter is fired *before* the $polylang object is available
441 + * Filters the links model class to use.
442 + * /!\ this filter is fired *before* the $polylang object is available.
587 443 *
588 444 * @since 2.1.1
589 445 *
590 - * @param string $class A class name: PLL_Links_Default, PLL_Links_Directory, PLL_Links_Subdomain, PLL_Links_Domain
446 + * @param string $class A class name: PLL_Links_Default, PLL_Links_Directory, PLL_Links_Subdomain, PLL_Links_Domain.
591 447 */
592 448 $class = apply_filters( 'pll_links_model', $class );
593 449
594 450 return new $class( $this );
@@ -594,80 +450,158 @@
594 450 return new $class( $this );
595 451 }
596 452
597 453 /**
598 - * Some backward compatibility with Polylang < 1.8
599 - * allows for example to call $polylang->model->get_post_languages( $post_id ) instead of $polylang->model->post->get_language( $post_id )
600 - * this works but should be slower than the direct call, thus an error is triggered in debug mode
454 + * Returns a list of object IDs without language (used in settings and wizard).
601 455 *
602 - * @since 1.8
456 + * @since 0.9
457 + * @since 2.2.6 Added the `$limit` parameter.
458 + * @since 3.4 Added the `$types` parameter.
603 459 *
604 - * @param string $func Function name
605 - * @param array $args Function arguments
460 + * @param int $limit Optional. Max number of IDs to return. Defaults to -1 (no limit).
461 + * @param string[] $types Optional. Types to handle (@see PLL_Translatable_Object::get_type()). Defaults to
462 + * an empty array (all types).
463 + * @return int[][]|false {
464 + * IDs of objects without language.
465 + *
466 + * @type int[] $posts Array of post ids.
467 + * @type int[] $terms Array of term ids.
468 + * }
469 + *
470 + * @phpstan-param -1|positive-int $limit
606 471 */
607 - public function __call( $func, $args ) {
608 - $f = $func;
472 + public function get_objects_with_no_lang( $limit = -1, array $types = array() ) {
473 + /**
474 + * Filters the max number of IDs to return when searching objects with no language.
475 + * This filter can be used to decrease the memory usage in case the number of objects
476 + * without language is too big. Using a negative value is equivalent to have no limit.
477 + *
478 + * @since 2.2.6
479 + * @since 3.4 Added the `$types` parameter.
480 + *
481 + * @param int $limit Max number of IDs to retrieve from the database.
482 + * @param string[] $types Types to handle (@see PLL_Translatable_Object::get_type()). An empty array means all
483 + * types.
484 + */
485 + $limit = apply_filters( 'get_objects_with_no_lang_limit', $limit, $types );
486 + $limit = $limit < 1 ? -1 : max( (int) $limit, 1 );
487 + $objects = array();
609 488
610 - switch ( $func ) {
611 - case 'get_object_term':
612 - $o = ( false === strpos( $args[1], 'term' ) ) ? 'post' : 'term';
613 - break;
489 + foreach ( $this->translatable_objects as $type => $object ) {
490 + if ( ! empty( $types ) && ! in_array( $type, $types, true ) ) {
491 + continue;
492 + }
614 493
615 - case 'save_translations':
616 - case 'delete_translation':
617 - case 'get_translations':
618 - case 'get_translation':
619 - case 'join_clause':
620 - $o = ( 'post' == $args[0] || $this->is_translated_post_type( $args[0] ) ) ? 'post' : ( 'term' == $args[0] || $this->is_translated_taxonomy( $args[0] ) ? 'term' : false );
621 - unset( $args[0] );
622 - break;
494 + $ids = $object->get_objects_with_no_lang( $limit );
623 495
624 - case 'set_post_language':
625 - case 'get_post_language':
626 - case 'set_term_language':
627 - case 'get_term_language':
628 - case 'delete_term_language':
629 - case 'get_post':
630 - case 'get_term':
631 - $str = explode( '_', $func );
632 - $f = empty( $str[2] ) ? $str[0] : $str[0] . '_' . $str[2];
633 - $o = $str[1];
634 - break;
496 + if ( empty( $ids ) ) {
497 + continue;
498 + }
635 499
636 - case 'where_clause':
637 - case 'get_objects_in_language':
638 - $o = $args[1];
639 - unset( $args[1] );
640 - break;
500 + // The trailing 's' in the array key is for backward compatibility.
501 + $objects[ "{$type}s" ] = $ids;
641 502 }
642 503
643 - if ( ! empty( $o ) && is_object( $this->$o ) && method_exists( $this->$o, $f ) ) {
644 - if ( WP_DEBUG ) {
645 - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
646 - $i = 1 + empty( $debug[1]['line'] ); // the file and line are in $debug[2] if the function was called using call_user_func
504 + $objects = ! empty( $objects ) ? $objects : false;
647 505
648 - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions
649 - sprintf(
650 - '%1$s was called incorrectly in %4$s on line %5$s: the call to $polylang->model->%1$s() has been deprecated in Polylang 1.8, use PLL()->model->%2$s->%3$s() instead.' . "\nError handler",
651 - esc_html( $func ),
652 - esc_html( $o ),
653 - esc_html( $f ),
654 - esc_html( $debug[ $i ]['file'] ),
655 - absint( $debug[ $i ]['line'] )
656 - )
657 - );
506 + /**
507 + * Filters the list of IDs of untranslated objects.
508 + *
509 + * @since 0.9
510 + * @since 3.4 Added the `$limit` and `$types` parameters.
511 + *
512 + * @param int[][]|false $objects List of lists of object IDs, `false` if no IDs found.
513 + * @param int $limit Max number of IDs to retrieve from the database.
514 + * @param string[] $types Types to handle (@see PLL_Translatable_Object::get_type()). An empty array
515 + * means all types.
516 + */
517 + return apply_filters( 'pll_get_objects_with_no_lang', $objects, $limit, $types );
518 + }
519 +
520 + /**
521 + * Returns ids of post without language.
522 + *
523 + * @since 3.1
524 + *
525 + * @param string|string[] $post_types A translated post type or an array of translated post types.
526 + * @param int $limit Max number of objects to return. `-1` to return all of them.
527 + * @return int[]
528 + *
529 + * @phpstan-param -1|positive-int $limit
530 + * @phpstan-return list<positive-int>
531 + */
532 + public function get_posts_with_no_lang( $post_types, $limit ): array {
533 + return $this->translatable_objects->get( 'post' )->get_objects_with_no_lang( $limit, (array) $post_types );
534 + }
535 +
536 + /**
537 + * Returns ids of terms without language.
538 + *
539 + * @since 3.1
540 + *
541 + * @param string|string[] $taxonomies A translated taxonomy or an array of taxonomies post types.
542 + * @param int $limit Max number of objects to return. `-1` to return all of them.
543 + * @return int[]
544 + *
545 + * @phpstan-param -1|positive-int $limit
546 + * @phpstan-return list<positive-int>
547 + */
548 + public function get_terms_with_no_lang( $taxonomies, $limit ): array {
549 + return $this->translatable_objects->get( 'term' )->get_objects_with_no_lang( $limit, (array) $taxonomies );
550 + }
551 +
552 + /**
553 + * Assigns the default language to objects in mass.
554 + *
555 + * @since 1.2
556 + * @since 3.4 Moved from PLL_Admin_Model class.
557 + * Removed `$limit` parameter, added `$lang` and `$types` parameters.
558 + *
559 + * @param PLL_Language|null $lang Optional. The language to assign to objects. Defaults to `null` (default language).
560 + * @param string[] $types Optional. Types to handle (@see PLL_Translatable_Object::get_type()). Defaults
561 + * to an empty array (all types).
562 + * @return void
563 + */
564 + public function set_language_in_mass( $lang = null, array $types = array() ): void {
565 + if ( ! $lang instanceof PLL_Language ) {
566 + $lang = $this->languages->get_default();
567 +
568 + if ( empty( $lang ) ) {
569 + return;
658 570 }
659 - return call_user_func_array( array( $this->$o, $f ), $args );
660 571 }
661 572
662 - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
663 - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions
664 - sprintf(
665 - 'Call to undefined function PLL()->model->%1$s() in %2$s on line %3$s' . "\nError handler",
666 - esc_html( $func ),
667 - esc_html( $debug[0]['file'] ),
668 - absint( $debug[0]['line'] )
669 - ),
670 - E_USER_ERROR
671 - );
573 + // 1000 is an arbitrary value that will be filtered by `get_objects_with_no_lang_limit`.
574 + $nolang = $this->get_objects_with_no_lang( 1000, $types );
575 +
576 + if ( empty( $nolang ) ) {
577 + return;
578 + }
579 +
580 + /**
581 + * Keep track of types where we set the language:
582 + * those are types where we may have more items to process if we have more than 1000 items in total.
583 + * This will prevent unnecessary SQL queries in the next recursion: if we have 0 items in this recursion for
584 + * a type, we'll still have 0 in the next one, no need for a new query.
585 + */
586 + $types_with_objects = array();
587 +
588 + foreach ( $this->translatable_objects as $type => $object ) {
589 + if ( empty( $nolang[ "{$type}s" ] ) ) {
590 + continue;
591 + }
592 +
593 + if ( ! empty( $types ) && ! in_array( $type, $types, true ) ) {
594 + continue;
595 + }
596 +
597 + $object->set_language_in_mass( $nolang[ "{$type}s" ], $lang );
598 + $types_with_objects[] = $type;
599 + }
600 +
601 + if ( empty( $types_with_objects ) ) {
602 + return;
603 + }
604 +
605 + $this->set_language_in_mass( $lang, $types_with_objects );
672 606 }
673 607 }