| @@ -8,29 +8,77 @@ | ||
| 8 | 8 | * |
| 9 | 9 | * @since 1.2 |
| 10 | 10 | */ |
| 11 | 11 | class PLL_Model { |
| 12 | - public $cache; // Our internal non persistent cache object | |
| 12 | + /** | |
| 13 | + * Internal non persistent cache object. | |
| 14 | + * | |
| 15 | + * @var PLL_Cache<mixed> | |
| 16 | + */ | |
| 17 | + public $cache; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Stores the plugin options. | |
| 21 | + * | |
| 22 | + * @var array | |
| 23 | + */ | |
| 13 | 24 | public $options; |
| 14 | - public $post, $term; // Translated objects models | |
| 15 | 25 | |
| 16 | 26 | /** |
| 17 | - * Constructor | |
| 18 | - * setups translated objects sub models | |
| 19 | - * setups filters and actions | |
| 27 | + * Translatable objects registry. | |
| 20 | 28 | * |
| 29 | + * @since 3.4 | |
| 30 | + * | |
| 31 | + * @var PLL_Translatable_Objects | |
| 32 | + */ | |
| 33 | + public $translatable_objects; | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Translated post model. | |
| 37 | + * | |
| 38 | + * @var PLL_Translated_Post | |
| 39 | + */ | |
| 40 | + public $post; | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Translated term model. | |
| 44 | + * | |
| 45 | + * @var PLL_Translated_Term | |
| 46 | + */ | |
| 47 | + public $term; | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * Flag set to true during the language objects creation. | |
| 51 | + * | |
| 52 | + * @var bool | |
| 53 | + */ | |
| 54 | + private $is_creating_language_objects = false; | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * Tells if {@see PLL_Model::get_languages_list()} can be used. | |
| 58 | + * | |
| 59 | + * @var bool | |
| 60 | + */ | |
| 61 | + private $languages_ready = false; | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Constructor. | |
| 65 | + * Setups translated objects sub models. | |
| 66 | + * Setups filters and actions. | |
| 67 | + * | |
| 21 | 68 | * @since 1.2 |
| 22 | 69 | * |
| 23 | - * @param array $options Polylang options | |
| 70 | + * @param array $options Polylang options. | |
| 24 | 71 | */ |
| 25 | 72 | public function __construct( &$options ) { |
| 26 | 73 | $this->options = &$options; |
| 27 | 74 | |
| 28 | 75 | $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 | |
| 76 | + $this->translatable_objects = new PLL_Translatable_Objects(); | |
| 77 | + $this->post = $this->translatable_objects->register( new PLL_Translated_Post( $this ) ); // Translated post sub model. | |
| 78 | + $this->term = $this->translatable_objects->register( new PLL_Translated_Term( $this ) ); // Translated term sub model. | |
| 31 | 79 | |
| 32 | - // We need to clean languages cache when editing a language and when modifying the permalink structure | |
| 80 | + // We need to clean languages cache when editing a language and when modifying the permalink structure. | |
| 33 | 81 | add_action( 'edited_term_taxonomy', array( $this, 'clean_languages_cache' ), 10, 2 ); |
| 34 | 82 | add_action( 'update_option_permalink_structure', array( $this, 'clean_languages_cache' ) ); |
| 35 | 83 | add_action( 'update_option_siteurl', array( $this, 'clean_languages_cache' ) ); |
| 36 | 84 | add_action( 'update_option_home', array( $this, 'clean_languages_cache' ) ); |
| @@ -36,99 +84,144 @@ | ||
| 36 | 84 | add_action( 'update_option_home', array( $this, 'clean_languages_cache' ) ); |
| 37 | 85 | |
| 38 | 86 | add_filter( 'get_terms_args', array( $this, 'get_terms_args' ) ); |
| 39 | 87 | |
| 40 | - // Just in case someone would like to display the language description ;- ) | |
| 88 | + // Just in case someone would like to display the language description ;). | |
| 41 | 89 | add_filter( 'language_description', '__return_empty_string' ); |
| 42 | 90 | } |
| 43 | 91 | |
| 44 | 92 | /** |
| 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 | |
| 93 | + * Checks if there are languages or not. | |
| 48 | 94 | * |
| 49 | - * List of parameters accepted in $args: | |
| 95 | + * @since 3.3 | |
| 50 | 96 | * |
| 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 ) | |
| 97 | + * @return bool True if there are, false otherwise. | |
| 98 | + */ | |
| 99 | + public function has_languages() { | |
| 100 | + if ( ! empty( $this->cache->get( 'languages' ) ) ) { | |
| 101 | + return true; | |
| 102 | + } | |
| 103 | + | |
| 104 | + if ( ! empty( get_transient( 'pll_languages_list' ) ) ) { | |
| 105 | + return true; | |
| 106 | + } | |
| 107 | + | |
| 108 | + return ! empty( $this->get_language_terms() ); | |
| 109 | + } | |
| 110 | + | |
| 111 | + /** | |
| 112 | + * Returns the list of available languages. | |
| 113 | + * - Stores the list in a db transient (except flags), unless `PLL_CACHE_LANGUAGES` is set to false. | |
| 114 | + * - Caches the list (with flags) in a `PLL_Cache` object. | |
| 53 | 115 | * |
| 54 | 116 | * @since 0.1 |
| 55 | 117 | * |
| 56 | - * @param array $args | |
| 57 | - * @return array|string|int list of PLL_Language objects or PLL_Language object properties | |
| 118 | + * @param array $args { | |
| 119 | + * @type bool $hide_empty Hides languages with no posts if set to `true` (defaults to `false`). | |
| 120 | + * @type bool $hide_default Hides default language from the list (default to `false`). | |
| 121 | + * @type string $fields Returns only that field if set; {@see PLL_Language} for a list of fields. | |
| 122 | + * } | |
| 123 | + * @return array List of PLL_Language objects or PLL_Language object properties. | |
| 58 | 124 | */ |
| 59 | 125 | public function get_languages_list( $args = array() ) { |
| 60 | - if ( false === $languages = $this->cache->get( 'languages' ) ) { | |
| 126 | + if ( ! $this->are_languages_ready() ) { | |
| 127 | + _doing_it_wrong( | |
| 128 | + __METHOD__ . '()', | |
| 129 | + "It must not be called before the hook 'pll_pre_init'.", | |
| 130 | + '3.4' | |
| 131 | + ); | |
| 132 | + } | |
| 61 | 133 | |
| 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; | |
| 134 | + $languages = $this->cache->get( 'languages' ); | |
| 66 | 135 | |
| 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 ); | |
| 136 | + if ( ! is_array( $languages ) ) { | |
| 137 | + // Bail out early if languages are currently created to avoid an infinite loop. | |
| 138 | + if ( $this->is_creating_language_objects ) { | |
| 139 | + return array(); | |
| 140 | + } | |
| 70 | 141 | |
| 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 | - } | |
| 142 | + $this->is_creating_language_objects = true; | |
| 76 | 143 | |
| 77 | - // We will need the languages list to allow its access in the filter below | |
| 78 | - $this->cache->set( 'languages', $languages ); | |
| 144 | + if ( ! pll_get_constant( 'PLL_CACHE_LANGUAGES', true ) ) { | |
| 145 | + // Create the languages from taxonomies. | |
| 146 | + $languages = $this->get_languages_from_taxonomies(); | |
| 147 | + } else { | |
| 148 | + $languages = get_transient( 'pll_languages_list' ); | |
| 79 | 149 | |
| 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 ); | |
| 150 | + if ( empty( $languages ) || ! is_array( $languages ) || empty( reset( $languages )['term_props'] ) ) { // Test `term_props` in case we got a transient older than 3.4. | |
| 151 | + // Create the languages from taxonomies. | |
| 152 | + $languages = $this->get_languages_from_taxonomies(); | |
| 153 | + } else { | |
| 154 | + // Create the languages directly from arrays stored in the transient. | |
| 155 | + $languages = array_map( | |
| 156 | + array( new PLL_Language_Factory( $this->options ), 'get' ), | |
| 157 | + $languages | |
| 158 | + ); | |
| 90 | 159 | |
| 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 | - } | |
| 160 | + // Remove potential empty language. | |
| 161 | + $languages = array_filter( $languages ); | |
| 100 | 162 | |
| 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 ); | |
| 163 | + // Re-index. | |
| 164 | + $languages = array_values( $languages ); | |
| 105 | 165 | } |
| 106 | 166 | } |
| 107 | 167 | |
| 108 | 168 | /** |
| 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 | |
| 169 | + * Filters the list of languages *after* it is stored in the persistent cache. | |
| 170 | + * /!\ This filter is fired *before* the $polylang object is available. | |
| 111 | 171 | * |
| 112 | 172 | * @since 1.8 |
| 173 | + * @since 3.4 Deprecated. If you used this hook to filter URLs, you may hook `'site_url'` instead. | |
| 174 | + * @deprecated | |
| 113 | 175 | * |
| 114 | - * @param array $languages the list of language objects | |
| 176 | + * @param PLL_Language[] $languages The list of language objects. | |
| 115 | 177 | */ |
| 116 | - $languages = apply_filters( 'pll_after_languages_cache', $languages ); | |
| 117 | - $this->cache->set( 'languages', $languages ); | |
| 178 | + $languages = apply_filters_deprecated( 'pll_after_languages_cache', array( $languages ), '3.4' ); | |
| 179 | + | |
| 180 | + if ( $this->are_languages_ready() ) { | |
| 181 | + $this->cache->set( 'languages', $languages ); | |
| 182 | + } | |
| 183 | + | |
| 184 | + $this->is_creating_language_objects = false; | |
| 118 | 185 | } |
| 119 | 186 | |
| 120 | - $args = wp_parse_args( $args, array( 'hide_empty' => false ) ); | |
| 187 | + $languages = array_filter( | |
| 188 | + $languages, | |
| 189 | + function ( $lang ) use ( $args ) { | |
| 190 | + $keep_empty = empty( $args['hide_empty'] ) || $lang->get_tax_prop( 'language', 'count' ); | |
| 191 | + $keep_default = empty( $args['hide_default'] ) || ! $lang->is_default; | |
| 192 | + return $keep_empty && $keep_default; | |
| 193 | + } | |
| 194 | + ); | |
| 121 | 195 | |
| 122 | - // Remove empty languages if requested | |
| 123 | - if ( $args['hide_empty'] ) { | |
| 124 | - $languages = wp_list_filter( $languages, array( 'count' => 0 ), 'NOT' ); | |
| 125 | - } | |
| 196 | + $languages = array_values( $languages ); // Re-index. | |
| 126 | 197 | |
| 127 | 198 | return empty( $args['fields'] ) ? $languages : wp_list_pluck( $languages, $args['fields'] ); |
| 128 | 199 | } |
| 129 | 200 | |
| 130 | 201 | /** |
| 202 | + * Tells if {@see PLL_Model::get_languages_list()} can be used. | |
| 203 | + * | |
| 204 | + * @since 3.4 | |
| 205 | + * | |
| 206 | + * @return bool | |
| 207 | + */ | |
| 208 | + public function are_languages_ready() { | |
| 209 | + return $this->languages_ready; | |
| 210 | + } | |
| 211 | + | |
| 212 | + /** | |
| 213 | + * Sets the internal property `$languages_ready` to `true`, telling that {@see PLL_Model::get_languages_list()} can be used. | |
| 214 | + * | |
| 215 | + * @since 3.4 | |
| 216 | + * | |
| 217 | + * @return void | |
| 218 | + */ | |
| 219 | + public function set_languages_ready() { | |
| 220 | + $this->languages_ready = true; | |
| 221 | + } | |
| 222 | + | |
| 223 | + /** | |
| 131 | 224 | * Cleans language cache |
| 132 | 225 | * can be called directly with no parameter |
| 133 | 226 | * called by the 'edited_term_taxonomy' filter with 2 parameters when count needs to be updated |
| 134 | 227 | * |
| @@ -135,11 +228,12 @@ | ||
| 135 | 228 | * @since 1.2 |
| 136 | 229 | * |
| 137 | 230 | * @param int $term not used |
| 138 | 231 | * @param string $taxonomy taxonomy name |
| 232 | + * @return void | |
| 139 | 233 | */ |
| 140 | 234 | public function clean_languages_cache( $term = 0, $taxonomy = null ) { |
| 141 | - if ( empty( $taxonomy ) || 'language' == $taxonomy ) { | |
| 235 | + if ( empty( $taxonomy ) || 'language' === $taxonomy ) { | |
| 142 | 236 | delete_transient( 'pll_languages_list' ); |
| 143 | 237 | $this->cache->clean(); |
| 144 | 238 | } |
| 145 | 239 | } |
| @@ -152,9 +246,11 @@ | ||
| 152 | 246 | * @param array $args WP_Term_Query arguments |
| 153 | 247 | * @return array |
| 154 | 248 | */ |
| 155 | 249 | public function get_terms_args( $args ) { |
| 156 | - if ( isset( $args['taxonomy'] ) && ! array_diff( (array) $args['taxonomy'], array( 'language', 'term_language', 'post_translations', 'term_translations' ) ) ) { | |
| 250 | + $taxonomies = $this->translatable_objects->get_taxonomy_names(); | |
| 251 | + | |
| 252 | + if ( isset( $args['taxonomy'] ) && ! array_diff( (array) $args['taxonomy'], $taxonomies ) ) { | |
| 157 | 253 | $args['update_term_meta_cache'] = false; |
| 158 | 254 | } |
| 159 | 255 | return $args; |
| 160 | 256 | } |
| @@ -159,42 +255,67 @@ | ||
| 159 | 255 | return $args; |
| 160 | 256 | } |
| 161 | 257 | |
| 162 | 258 | /** |
| 163 | - * Returns the language by its term_id, tl_term_id, slug or locale | |
| 259 | + * Returns the language by its term_id, tl_term_id, slug or locale. | |
| 164 | 260 | * |
| 165 | 261 | * @since 0.1 |
| 262 | + * @since 3.4 Allow to get a language by `term_taxonomy_id`. | |
| 166 | 263 | * |
| 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 | |
| 264 | + * @param mixed $value `term_id`, `term_taxonomy_id`, `slug`, `locale`, or `w3c` of the queried language. | |
| 265 | + * `term_id` and `term_taxonomy_id` can be fetched for any language taxonomy. | |
| 266 | + * /!\ For the `term_taxonomy_id`, prefix the ID by `tt:` (ex: `"tt:{$tt_id}"`), | |
| 267 | + * this is to prevent confusion between `term_id` and `term_taxonomy_id`. | |
| 268 | + * @return PLL_Language|false Language object, false if no language found. | |
| 169 | 269 | */ |
| 170 | 270 | public function get_language( $value ) { |
| 171 | 271 | if ( is_object( $value ) ) { |
| 172 | - return $value instanceof PLL_Language ? $value : $this->get_language( $value->term_id ); // will force cast to PLL_Language | |
| 272 | + return $value instanceof PLL_Language ? $value : $this->get_language( $value->term_id ); // Will force cast to PLL_Language. | |
| 173 | 273 | } |
| 174 | 274 | |
| 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 ); | |
| 275 | + $return = $this->cache->get( 'language:' . $value ); | |
| 276 | + | |
| 277 | + if ( $return instanceof PLL_Language ) { | |
| 278 | + return $return; | |
| 279 | + } | |
| 280 | + | |
| 281 | + foreach ( $this->get_languages_list() as $lang ) { | |
| 282 | + foreach ( $lang->get_tax_props() as $props ) { | |
| 283 | + $this->cache->set( 'language:' . $props['term_id'], $lang ); | |
| 284 | + $this->cache->set( 'language:tt:' . $props['term_taxonomy_id'], $lang ); | |
| 182 | 285 | } |
| 183 | - $return = $this->cache->get( 'language:' . $value ); | |
| 286 | + $this->cache->set( 'language:' . $lang->slug, $lang ); | |
| 287 | + $this->cache->set( 'language:' . $lang->locale, $lang ); | |
| 288 | + $this->cache->set( 'language:' . $lang->w3c, $lang ); | |
| 184 | 289 | } |
| 185 | 290 | |
| 186 | - return $return; | |
| 291 | + /** @var PLL_Language|false */ | |
| 292 | + return $this->cache->get( 'language:' . $value ); | |
| 187 | 293 | } |
| 188 | 294 | |
| 189 | 295 | /** |
| 190 | - * Adds terms clauses to get_terms to filter them by languages - used in both frontend and admin | |
| 296 | + * Returns the default language. | |
| 191 | 297 | * |
| 298 | + * @since 3.4 | |
| 299 | + * | |
| 300 | + * @return PLL_Language|false Default language object, `false` if no language found. | |
| 301 | + */ | |
| 302 | + public function get_default_language() { | |
| 303 | + if ( empty( $this->options['default_lang'] ) ) { | |
| 304 | + return false; | |
| 305 | + } | |
| 306 | + | |
| 307 | + return $this->get_language( $this->options['default_lang'] ); | |
| 308 | + } | |
| 309 | + | |
| 310 | + /** | |
| 311 | + * Adds terms clauses to the term query to filter them by languages. | |
| 312 | + * | |
| 192 | 313 | * @since 1.2 |
| 193 | 314 | * |
| 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 | |
| 315 | + * @param string[] $clauses The list of sql clauses in terms query. | |
| 316 | + * @param PLL_Language|false $lang PLL_Language object. | |
| 317 | + * @return string[] Modified list of clauses. | |
| 197 | 318 | */ |
| 198 | 319 | public function terms_clauses( $clauses, $lang ) { |
| 199 | 320 | if ( ! empty( $lang ) && false === strpos( $clauses['join'], 'pll_tr' ) ) { |
| 200 | 321 | $clauses['join'] .= $this->term->join_clause(); |
| @@ -203,119 +324,78 @@ | ||
| 203 | 324 | return $clauses; |
| 204 | 325 | } |
| 205 | 326 | |
| 206 | 327 | /** |
| 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 | |
| 328 | + * Returns post types that need to be translated. | |
| 329 | + * The post types list is cached for better better performance. | |
| 330 | + * The method waits for 'after_setup_theme' to apply the cache | |
| 331 | + * to allow themes adding the filter in functions.php. | |
| 210 | 332 | * |
| 211 | 333 | * @since 1.2 |
| 212 | 334 | * |
| 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 | |
| 335 | + * @param bool $filter True if we should return only valid registered post types. | |
| 336 | + * @return string[] Post type names for which Polylang manages languages and translations. | |
| 215 | 337 | */ |
| 216 | 338 | 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' ); | |
| 219 | - | |
| 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; | |
| 339 | + return $this->translatable_objects->get( 'post' )->get_translated_object_types( $filter ); | |
| 247 | 340 | } |
| 248 | 341 | |
| 249 | 342 | /** |
| 250 | - * Returns true if Polylang manages languages and translations for this post type | |
| 343 | + * Returns true if Polylang manages languages and translations for this post type. | |
| 251 | 344 | * |
| 252 | 345 | * @since 1.2 |
| 253 | 346 | * |
| 254 | - * @param string|array $post_type post type name or array of post type names | |
| 347 | + * @param string|string[] $post_type Post type name or array of post type names. | |
| 255 | 348 | * @return bool |
| 256 | 349 | */ |
| 257 | 350 | 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 ) ); | |
| 351 | + if ( empty( array_filter( (array) $post_type ) ) ) { | |
| 352 | + return false; | |
| 353 | + } | |
| 354 | + | |
| 355 | + /** @var non-empty-array<non-empty-string>|non-empty-string $post_type */ | |
| 356 | + return $this->translatable_objects->get( 'post' )->is_translated_object_type( $post_type ); | |
| 260 | 357 | } |
| 261 | 358 | |
| 262 | 359 | /** |
| 263 | - * Return taxonomies that need to be translated | |
| 360 | + * Returns taxonomies that need to be translated. | |
| 361 | + * The taxonomies list is cached for better better performance. | |
| 362 | + * The method waits for 'after_setup_theme' to apply the cache | |
| 363 | + * to allow themes adding the filter in functions.php. | |
| 264 | 364 | * |
| 265 | 365 | * @since 1.2 |
| 266 | 366 | * |
| 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 | |
| 367 | + * @param bool $filter True if we should return only valid registered taxonomies. | |
| 368 | + * @return string[] Array of registered taxonomy names for which Polylang manages languages and translations. | |
| 269 | 369 | */ |
| 270 | 370 | public function get_translated_taxonomies( $filter = true ) { |
| 271 | - if ( false === $taxonomies = $this->cache->get( 'taxonomies' ) ) { | |
| 272 | - $taxonomies = array( 'category' => 'category', 'post_tag' => 'post_tag' ); | |
| 273 | - | |
| 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 | - } | |
| 277 | - | |
| 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 | - } | |
| 294 | - | |
| 295 | - return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies; | |
| 371 | + return $this->translatable_objects->get( 'term' )->get_translated_object_types( $filter ); | |
| 296 | 372 | } |
| 297 | 373 | |
| 298 | 374 | /** |
| 299 | - * Returns true if Polylang manages languages and translations for this taxonomy | |
| 375 | + * Returns true if Polylang manages languages and translations for this taxonomy. | |
| 300 | 376 | * |
| 301 | 377 | * @since 1.2 |
| 302 | 378 | * |
| 303 | - * @param string|array $tax taxonomy name or array of taxonomy names | |
| 379 | + * @param string|string[] $tax Taxonomy name or array of taxonomy names. | |
| 304 | 380 | * @return bool |
| 305 | 381 | */ |
| 306 | 382 | 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 ) ); | |
| 383 | + if ( empty( array_filter( (array) $tax ) ) ) { | |
| 384 | + return false; | |
| 385 | + } | |
| 386 | + | |
| 387 | + /** @var non-empty-array<non-empty-string>|non-empty-string $tax */ | |
| 388 | + return $this->translatable_objects->get( 'term' )->is_translated_object_type( $tax ); | |
| 309 | 389 | } |
| 310 | 390 | |
| 311 | 391 | /** |
| 312 | - * Return taxonomies that need to be filtered ( post_format like ) | |
| 392 | + * Return taxonomies that need to be filtered (post_format like). | |
| 313 | 393 | * |
| 314 | 394 | * @since 1.7 |
| 315 | 395 | * |
| 316 | - * @param bool $filter true if we should return only valid registered taxonomies | |
| 317 | - * @return array array of registered taxonomy names | |
| 396 | + * @param bool $filter True if we should return only valid registered taxonomies. | |
| 397 | + * @return string[] Array of registered taxonomy names. | |
| 318 | 398 | */ |
| 319 | 399 | public function get_filtered_taxonomies( $filter = true ) { |
| 320 | 400 | if ( did_action( 'after_setup_theme' ) ) { |
| 321 | 401 | static $taxonomies = null; |
| @@ -324,9 +404,9 @@ | ||
| 324 | 404 | if ( empty( $taxonomies ) ) { |
| 325 | 405 | $taxonomies = array( 'post_format' => 'post_format' ); |
| 326 | 406 | |
| 327 | 407 | /** |
| 328 | - * Filter the list of taxonomies not translatable but filtered by language. | |
| 408 | + * Filters the list of taxonomies not translatable but filtered by language. | |
| 329 | 409 | * Includes only the post format by default |
| 330 | 410 | * The filter must be added soon in the WordPress loading process: |
| 331 | 411 | * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes. |
| 332 | 412 | * |
| @@ -331,10 +411,10 @@ | ||
| 331 | 411 | * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes. |
| 332 | 412 | * |
| 333 | 413 | * @since 1.7 |
| 334 | 414 | * |
| 335 | - * @param array $taxonomies list of taxonomy names | |
| 336 | - * @param bool $is_settings true when displaying the list of custom taxonomies in Polylang settings | |
| 415 | + * @param string[] $taxonomies List of taxonomy names. | |
| 416 | + * @param bool $is_settings True when displaying the list of custom taxonomies in Polylang settings. | |
| 337 | 417 | */ |
| 338 | 418 | $taxonomies = apply_filters( 'pll_filtered_taxonomies', $taxonomies, false ); |
| 339 | 419 | } |
| 340 | 420 | |
| @@ -341,13 +421,13 @@ | ||
| 341 | 421 | return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies; |
| 342 | 422 | } |
| 343 | 423 | |
| 344 | 424 | /** |
| 345 | - * Returns true if Polylang filters this taxonomy per language | |
| 425 | + * Returns true if Polylang filters this taxonomy per language. | |
| 346 | 426 | * |
| 347 | 427 | * @since 1.7 |
| 348 | 428 | * |
| 349 | - * @param string|array $tax taxonomy name or array of taxonomy names | |
| 429 | + * @param string|string[] $tax Taxonomy name or array of taxonomy names. | |
| 350 | 430 | * @return bool |
| 351 | 431 | */ |
| 352 | 432 | public function is_filtered_taxonomy( $tax ) { |
| 353 | 433 | $taxonomies = $this->get_filtered_taxonomies( false ); |
| @@ -354,76 +434,48 @@ | ||
| 354 | 434 | return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) ); |
| 355 | 435 | } |
| 356 | 436 | |
| 357 | 437 | /** |
| 358 | - * Returns the query vars of all filtered taxonomies | |
| 438 | + * Returns the query vars of all filtered taxonomies. | |
| 359 | 439 | * |
| 360 | 440 | * @since 1.7 |
| 361 | 441 | * |
| 362 | - * @return array | |
| 442 | + * @return string[] | |
| 363 | 443 | */ |
| 364 | 444 | public function get_filtered_taxonomies_query_vars() { |
| 365 | 445 | $query_vars = array(); |
| 366 | 446 | foreach ( $this->get_filtered_taxonomies() as $filtered_tax ) { |
| 367 | 447 | $tax = get_taxonomy( $filtered_tax ); |
| 368 | - $query_vars[] = $tax->query_var; | |
| 448 | + if ( ! empty( $tax ) && is_string( $tax->query_var ) ) { | |
| 449 | + $query_vars[] = $tax->query_var; | |
| 450 | + } | |
| 369 | 451 | } |
| 370 | 452 | return $query_vars; |
| 371 | 453 | } |
| 372 | 454 | |
| 373 | 455 | /** |
| 374 | - * Create a default category for a language | |
| 375 | - * | |
| 376 | - * @since 1.2 | |
| 377 | - * | |
| 378 | - * @param object|string|int $lang language | |
| 379 | - */ | |
| 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 | - } | |
| 405 | - } | |
| 406 | - | |
| 407 | - $this->term->save_translations( (int) $cat, $translations ); | |
| 408 | - } | |
| 409 | - | |
| 410 | - /** | |
| 411 | 456 | * 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 | |
| 457 | + * but the native term_exists() will return true even if only one exists. | |
| 458 | + * So here the function adds the language parameter. | |
| 414 | 459 | * |
| 415 | 460 | * @since 1.4 |
| 416 | 461 | * |
| 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 | |
| 462 | + * @param string $term_name The term name. | |
| 463 | + * @param string $taxonomy Taxonomy name. | |
| 464 | + * @param int $parent Parent term id. | |
| 465 | + * @param string|PLL_Language $language The language slug or object. | |
| 466 | + * @return int The `term_id` of the found term. 0 otherwise. | |
| 467 | + * | |
| 468 | + * @phpstan-return int<0, max> | |
| 422 | 469 | */ |
| 423 | 470 | public function term_exists( $term_name, $taxonomy, $parent, $language ) { |
| 424 | 471 | global $wpdb; |
| 425 | 472 | |
| 473 | + $language = $this->get_language( $language ); | |
| 474 | + if ( empty( $language ) ) { | |
| 475 | + return 0; | |
| 476 | + } | |
| 477 | + | |
| 426 | 478 | $term_name = trim( wp_unslash( $term_name ) ); |
| 427 | 479 | $term_name = _wp_specialchars( $term_name ); |
| 428 | 480 | |
| 429 | 481 | $select = "SELECT t.term_id FROM $wpdb->terms AS t"; |
| @@ -429,9 +481,9 @@ | ||
| 429 | 481 | $select = "SELECT t.term_id FROM $wpdb->terms AS t"; |
| 430 | 482 | $join = " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id"; |
| 431 | 483 | $join .= $this->term->join_clause(); |
| 432 | 484 | $where = $wpdb->prepare( ' WHERE tt.taxonomy = %s AND t.name = %s', $taxonomy, $term_name ); |
| 433 | - $where .= $this->term->where_clause( $this->get_language( $language ) ); | |
| 485 | + $where .= $this->term->where_clause( $language ); | |
| 434 | 486 | |
| 435 | 487 | if ( $parent > 0 ) { |
| 436 | 488 | $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent ); |
| 437 | 489 | } |
| @@ -436,31 +488,37 @@ | ||
| 436 | 488 | $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent ); |
| 437 | 489 | } |
| 438 | 490 | |
| 439 | 491 | // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 440 | - return $wpdb->get_var( $select . $join . $where ); | |
| 492 | + $term_id = $wpdb->get_var( $select . $join . $where ); | |
| 493 | + return max( 0, (int) $term_id ); | |
| 441 | 494 | } |
| 442 | 495 | |
| 443 | 496 | /** |
| 444 | - * Checks if a term slug exists in a given language, taxonomy, hierarchy | |
| 497 | + * Checks if a term slug exists in a given language, taxonomy, hierarchy. | |
| 445 | 498 | * |
| 446 | 499 | * @since 1.9 |
| 447 | - * @since 2.8 Moved from PLL_Share_Term_Slug::term_exists() to PLL_Model::term_exists_by_slug() | |
| 500 | + * @since 2.8 Moved from PLL_Share_Term_Slug::term_exists() to PLL_Model::term_exists_by_slug(). | |
| 448 | 501 | * |
| 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. | |
| 502 | + * @param string $slug The term slug to test. | |
| 503 | + * @param string|PLL_Language $language The language slug or object. | |
| 504 | + * @param string $taxonomy Optional taxonomy name. | |
| 505 | + * @param int $parent Optional parent term id. | |
| 506 | + * @return int The `term_id` of the found term. 0 otherwise. | |
| 454 | 507 | */ |
| 455 | 508 | public function term_exists_by_slug( $slug, $language, $taxonomy = '', $parent = 0 ) { |
| 456 | 509 | global $wpdb; |
| 457 | 510 | |
| 511 | + $language = $this->get_language( $language ); | |
| 512 | + if ( empty( $language ) ) { | |
| 513 | + return 0; | |
| 514 | + } | |
| 515 | + | |
| 458 | 516 | $select = "SELECT t.term_id FROM {$wpdb->terms} AS t"; |
| 459 | 517 | $join = " INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id"; |
| 460 | 518 | $join .= $this->term->join_clause(); |
| 461 | 519 | $where = $wpdb->prepare( ' WHERE t.slug = %s', $slug ); |
| 462 | - $where .= $this->term->where_clause( $this->get_language( $language ) ); | |
| 520 | + $where .= $this->term->where_clause( $language ); | |
| 463 | 521 | |
| 464 | 522 | if ( ! empty( $taxonomy ) ) { |
| 465 | 523 | $where .= $wpdb->prepare( ' AND tt.taxonomy = %s', $taxonomy ); |
| 466 | 524 | } |
| @@ -474,20 +532,45 @@ | ||
| 474 | 532 | } |
| 475 | 533 | |
| 476 | 534 | |
| 477 | 535 | /** |
| 478 | - * Gets the number of posts per language in a date, author or post type archive. | |
| 536 | + * Returns the number of posts per language in a date, author or post type archive. | |
| 479 | 537 | * |
| 480 | 538 | * @since 1.2 |
| 481 | 539 | * |
| 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 ). | |
| 540 | + * @param PLL_Language $lang PLL_Language instance. | |
| 541 | + * @param array $q { | |
| 542 | + * WP_Query arguments: | |
| 543 | + * | |
| 544 | + * @type string|string[] $post_type Post type or array of post types. | |
| 545 | + * @type int $m Combination YearMonth. Accepts any four-digit year and month. | |
| 546 | + * @type int $year Four-digit year. | |
| 547 | + * @type int $monthnum Two-digit month. | |
| 548 | + * @type int $day Day of the month. | |
| 549 | + * @type int $author Author id. | |
| 550 | + * @type string $author_name User 'user_nicename'. | |
| 551 | + * @type string $post_format Post format. | |
| 552 | + * @type string $post_status Post status. | |
| 553 | + * } | |
| 484 | 554 | * @return int |
| 555 | + * | |
| 556 | + * @phpstan-param array{ | |
| 557 | + * post_type?: non-falsy-string|array<non-falsy-string>, | |
| 558 | + * post_status?: non-falsy-string, | |
| 559 | + * m?: numeric-string, | |
| 560 | + * year?: positive-int, | |
| 561 | + * monthnum?: int<1, 12>, | |
| 562 | + * day?: int<1, 31>, | |
| 563 | + * author?: int<1, max>, | |
| 564 | + * author_name?: non-falsy-string, | |
| 565 | + * post_format?: non-falsy-string | |
| 566 | + * } $q | |
| 567 | + * @phpstan-return int<0, max> | |
| 485 | 568 | */ |
| 486 | 569 | public function count_posts( $lang, $q = array() ) { |
| 487 | 570 | global $wpdb; |
| 488 | 571 | |
| 489 | - $q = wp_parse_args( $q, array( 'post_type' => 'post', 'post_status' => 'publish' ) ); | |
| 572 | + $q = array_merge( array( 'post_type' => 'post', 'post_status' => 'publish' ), $q ); | |
| 490 | 573 | |
| 491 | 574 | if ( ! is_array( $q['post_type'] ) ) { |
| 492 | 575 | $q['post_type'] = array( $q['post_type'] ); |
| 493 | 576 | } |
| @@ -501,17 +584,18 @@ | ||
| 501 | 584 | if ( empty( $q['post_type'] ) ) { |
| 502 | 585 | $q['post_type'] = array( 'post' ); // We *need* a post type. |
| 503 | 586 | } |
| 504 | 587 | |
| 505 | - $cache_key = 'pll_count_posts_' . md5( maybe_serialize( $q ) ); | |
| 506 | - $counts = wp_cache_get( $cache_key, 'counts' ); | |
| 588 | + $cache_key = $this->cache->get_unique_key( 'pll_count_posts_', $q ); | |
| 589 | + $counts = wp_cache_get( $cache_key, 'counts' ); | |
| 507 | 590 | |
| 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() ); | |
| 591 | + if ( ! is_array( $counts ) ) { | |
| 592 | + $counts = array(); | |
| 593 | + $select = "SELECT pll_tr.term_taxonomy_id, COUNT( * ) AS num_posts FROM {$wpdb->posts}"; | |
| 594 | + $join = $this->post->join_clause(); | |
| 595 | + $where = sprintf( " WHERE post_status = '%s'", esc_sql( $q['post_status'] ) ); | |
| 596 | + $where .= sprintf( " AND {$wpdb->posts}.post_type IN ( '%s' )", implode( "', '", esc_sql( $q['post_type'] ) ) ); | |
| 597 | + $where .= $this->post->where_clause( $this->get_languages_list() ); | |
| 514 | 598 | $groupby = ' GROUP BY pll_tr.term_taxonomy_id'; |
| 515 | 599 | |
| 516 | 600 | if ( ! empty( $q['m'] ) ) { |
| 517 | 601 | $q['m'] = '' . preg_replace( '|[^0-9]|', '', $q['m'] ); |
| @@ -566,17 +650,18 @@ | ||
| 566 | 650 | |
| 567 | 651 | wp_cache_set( $cache_key, $counts, 'counts' ); |
| 568 | 652 | } |
| 569 | 653 | |
| 570 | - return empty( $counts[ $lang->term_taxonomy_id ] ) ? 0 : $counts[ $lang->term_taxonomy_id ]; | |
| 654 | + $term_taxonomy_id = $lang->get_tax_prop( 'language', 'term_taxonomy_id' ); | |
| 655 | + return empty( $counts[ $term_taxonomy_id ] ) ? 0 : $counts[ $term_taxonomy_id ]; | |
| 571 | 656 | } |
| 572 | 657 | |
| 573 | 658 | /** |
| 574 | - * Setup the links model based on options | |
| 659 | + * Setup the links model based on options. | |
| 575 | 660 | * |
| 576 | 661 | * @since 1.2 |
| 577 | 662 | * |
| 578 | - * @return object implementing "links_model interface" | |
| 663 | + * @return PLL_Links_Model | |
| 579 | 664 | */ |
| 580 | 665 | public function get_links_model() { |
| 581 | 666 | $c = array( 'Directory', 'Directory', 'Subdomain', 'Domain' ); |
| 582 | 667 | $class = get_option( 'permalink_structure' ) ? 'PLL_Links_' . $c[ $this->options['force_lang'] ] : 'PLL_Links_Default'; |
| @@ -581,14 +666,14 @@ | ||
| 581 | 666 | $c = array( 'Directory', 'Directory', 'Subdomain', 'Domain' ); |
| 582 | 667 | $class = get_option( 'permalink_structure' ) ? 'PLL_Links_' . $c[ $this->options['force_lang'] ] : 'PLL_Links_Default'; |
| 583 | 668 | |
| 584 | 669 | /** |
| 585 | - * Filter the links model class to use | |
| 586 | - * /!\ this filter is fired *before* the $polylang object is available | |
| 670 | + * Filters the links model class to use. | |
| 671 | + * /!\ this filter is fired *before* the $polylang object is available. | |
| 587 | 672 | * |
| 588 | 673 | * @since 2.1.1 |
| 589 | 674 | * |
| 590 | - * @param string $class A class name: PLL_Links_Default, PLL_Links_Directory, PLL_Links_Subdomain, PLL_Links_Domain | |
| 675 | + * @param string $class A class name: PLL_Links_Default, PLL_Links_Directory, PLL_Links_Subdomain, PLL_Links_Domain. | |
| 591 | 676 | */ |
| 592 | 677 | $class = apply_filters( 'pll_links_model', $class ); |
| 593 | 678 | |
| 594 | 679 | return new $class( $this ); |
| @@ -594,80 +679,372 @@ | ||
| 594 | 679 | return new $class( $this ); |
| 595 | 680 | } |
| 596 | 681 | |
| 597 | 682 | /** |
| 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 | |
| 683 | + * Returns a list of object IDs without language (used in settings and wizard). | |
| 601 | 684 | * |
| 602 | - * @since 1.8 | |
| 685 | + * @since 0.9 | |
| 686 | + * @since 2.2.6 Added the `$limit` parameter. | |
| 687 | + * @since 3.4 Added the `$types` parameter. | |
| 603 | 688 | * |
| 604 | - * @param string $func Function name | |
| 605 | - * @param array $args Function arguments | |
| 689 | + * @param int $limit Optional. Max number of IDs to return. Defaults to -1 (no limit). | |
| 690 | + * @param string[] $types Optional. Types to handle (@see PLL_Translatable_Object::get_type()). Defaults to | |
| 691 | + * an empty array (all types). | |
| 692 | + * @return int[][]|false { | |
| 693 | + * IDs of objects without language. | |
| 694 | + * | |
| 695 | + * @type int[] $posts Array of post ids. | |
| 696 | + * @type int[] $terms Array of term ids. | |
| 697 | + * } | |
| 698 | + * | |
| 699 | + * @phpstan-param -1|positive-int $limit | |
| 606 | 700 | */ |
| 607 | - public function __call( $func, $args ) { | |
| 608 | - $f = $func; | |
| 701 | + public function get_objects_with_no_lang( $limit = -1, array $types = array() ) { | |
| 702 | + /** | |
| 703 | + * Filters the max number of IDs to return when searching objects with no language. | |
| 704 | + * This filter can be used to decrease the memory usage in case the number of objects | |
| 705 | + * without language is too big. Using a negative value is equivalent to have no limit. | |
| 706 | + * | |
| 707 | + * @since 2.2.6 | |
| 708 | + * @since 3.4 Added the `$types` parameter. | |
| 709 | + * | |
| 710 | + * @param int $limit Max number of IDs to retrieve from the database. | |
| 711 | + * @param string[] $types Types to handle (@see PLL_Translatable_Object::get_type()). An empty array means all | |
| 712 | + * types. | |
| 713 | + */ | |
| 714 | + $limit = apply_filters( 'get_objects_with_no_lang_limit', $limit, $types ); | |
| 715 | + $limit = $limit < 1 ? -1 : max( (int) $limit, 1 ); | |
| 716 | + $objects = array(); | |
| 609 | 717 | |
| 610 | - switch ( $func ) { | |
| 611 | - case 'get_object_term': | |
| 612 | - $o = ( false === strpos( $args[1], 'term' ) ) ? 'post' : 'term'; | |
| 613 | - break; | |
| 718 | + foreach ( $this->translatable_objects as $type => $object ) { | |
| 719 | + if ( ! empty( $types ) && ! in_array( $type, $types, true ) ) { | |
| 720 | + continue; | |
| 721 | + } | |
| 614 | 722 | |
| 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; | |
| 723 | + $ids = $object->get_objects_with_no_lang( $limit ); | |
| 623 | 724 | |
| 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; | |
| 725 | + if ( empty( $ids ) ) { | |
| 726 | + continue; | |
| 727 | + } | |
| 635 | 728 | |
| 636 | - case 'where_clause': | |
| 637 | - case 'get_objects_in_language': | |
| 638 | - $o = $args[1]; | |
| 639 | - unset( $args[1] ); | |
| 640 | - break; | |
| 729 | + // The trailing 's' in the array key is for backward compatibility. | |
| 730 | + $objects[ "{$type}s" ] = $ids; | |
| 641 | 731 | } |
| 642 | 732 | |
| 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 | |
| 733 | + $objects = ! empty( $objects ) ? $objects : false; | |
| 647 | 734 | |
| 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 | - ); | |
| 735 | + /** | |
| 736 | + * Filters the list of IDs of untranslated objects. | |
| 737 | + * | |
| 738 | + * @since 0.9 | |
| 739 | + * @since 3.4 Added the `$limit` and `$types` parameters. | |
| 740 | + * | |
| 741 | + * @param int[][]|false $objects List of lists of object IDs, `false` if no IDs found. | |
| 742 | + * @param int $limit Max number of IDs to retrieve from the database. | |
| 743 | + * @param string[] $types Types to handle (@see PLL_Translatable_Object::get_type()). An empty array | |
| 744 | + * means all types. | |
| 745 | + */ | |
| 746 | + return apply_filters( 'pll_get_objects_with_no_lang', $objects, $limit, $types ); | |
| 747 | + } | |
| 748 | + | |
| 749 | + /** | |
| 750 | + * Returns ids of post without language. | |
| 751 | + * | |
| 752 | + * @since 3.1 | |
| 753 | + * | |
| 754 | + * @param string|string[] $post_types A translated post type or an array of translated post types. | |
| 755 | + * @param int $limit Max number of objects to return. `-1` to return all of them. | |
| 756 | + * @return int[] | |
| 757 | + * | |
| 758 | + * @phpstan-param -1|positive-int $limit | |
| 759 | + * @phpstan-return list<positive-int> | |
| 760 | + */ | |
| 761 | + public function get_posts_with_no_lang( $post_types, $limit ) { | |
| 762 | + return $this->translatable_objects->get( 'post' )->get_objects_with_no_lang( $limit, (array) $post_types ); | |
| 763 | + } | |
| 764 | + | |
| 765 | + /** | |
| 766 | + * Returns ids of terms without language. | |
| 767 | + * | |
| 768 | + * @since 3.1 | |
| 769 | + * | |
| 770 | + * @param string|string[] $taxonomies A translated taxonomy or an array of taxonomies post types. | |
| 771 | + * @param int $limit Max number of objects to return. `-1` to return all of them. | |
| 772 | + * @return int[] | |
| 773 | + * | |
| 774 | + * @phpstan-param -1|positive-int $limit | |
| 775 | + * @phpstan-return list<positive-int> | |
| 776 | + */ | |
| 777 | + public function get_terms_with_no_lang( $taxonomies, $limit ) { | |
| 778 | + return $this->translatable_objects->get( 'term' )->get_objects_with_no_lang( $limit, (array) $taxonomies ); | |
| 779 | + } | |
| 780 | + | |
| 781 | + /** | |
| 782 | + * Assigns the default language to objects in mass. | |
| 783 | + * | |
| 784 | + * @since 1.2 | |
| 785 | + * @since 3.4 Moved from PLL_Admin_Model class. | |
| 786 | + * Removed `$limit` parameter, added `$lang` and `$types` parameters. | |
| 787 | + * | |
| 788 | + * @param PLL_Language|null $lang Optional. The language to assign to objects. Defaults to `null` (default language). | |
| 789 | + * @param string[] $types Optional. Types to handle (@see PLL_Translatable_Object::get_type()). Defaults | |
| 790 | + * to an empty array (all types). | |
| 791 | + * @return void | |
| 792 | + */ | |
| 793 | + public function set_language_in_mass( $lang = null, array $types = array() ) { | |
| 794 | + if ( ! $lang instanceof PLL_Language ) { | |
| 795 | + $lang = $this->get_default_language(); | |
| 796 | + | |
| 797 | + if ( empty( $lang ) ) { | |
| 798 | + return; | |
| 658 | 799 | } |
| 659 | - return call_user_func_array( array( $this->$o, $f ), $args ); | |
| 660 | 800 | } |
| 661 | 801 | |
| 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 | |
| 802 | + // 1000 is an arbitrary value that will be filtered by `get_objects_with_no_lang_limit`. | |
| 803 | + $nolang = $this->get_objects_with_no_lang( 1000, $types ); | |
| 804 | + | |
| 805 | + if ( empty( $nolang ) ) { | |
| 806 | + return; | |
| 807 | + } | |
| 808 | + | |
| 809 | + /** | |
| 810 | + * Keep track of types where we set the language: | |
| 811 | + * those are types where we may have more items to process if we have more than 1000 items in total. | |
| 812 | + * This will prevent unnecessary SQL queries in the next recursion: if we have 0 items in this recursion for | |
| 813 | + * a type, we'll still have 0 in the next one, no need for a new query. | |
| 814 | + */ | |
| 815 | + $types_with_objects = array(); | |
| 816 | + | |
| 817 | + foreach ( $this->translatable_objects as $type => $object ) { | |
| 818 | + if ( empty( $nolang[ "{$type}s" ] ) ) { | |
| 819 | + continue; | |
| 820 | + } | |
| 821 | + | |
| 822 | + if ( ! empty( $types ) && ! in_array( $type, $types, true ) ) { | |
| 823 | + continue; | |
| 824 | + } | |
| 825 | + | |
| 826 | + $object->set_language_in_mass( $nolang[ "{$type}s" ], $lang ); | |
| 827 | + $types_with_objects[] = $type; | |
| 828 | + } | |
| 829 | + | |
| 830 | + if ( empty( $types_with_objects ) ) { | |
| 831 | + return; | |
| 832 | + } | |
| 833 | + | |
| 834 | + $this->set_language_in_mass( $lang, $types_with_objects ); | |
| 835 | + } | |
| 836 | + | |
| 837 | + /** | |
| 838 | + * Filters the ORDERBY clause of the languages query. | |
| 839 | + * | |
| 840 | + * This allows to order languages terms by `taxonomy` first then by `term_group` and `term_id`. | |
| 841 | + * Ordering terms by taxonomy allows not to mix terms between all language taxomonomies. | |
| 842 | + * Having the "language' taxonomy first is important for {@see PLL_Admin_Model:delete_language()}. | |
| 843 | + * | |
| 844 | + * @since 3.2.3 | |
| 845 | + * | |
| 846 | + * @param string $orderby `ORDERBY` clause of the terms query. | |
| 847 | + * @param array $args An array of term query arguments. | |
| 848 | + * @param string[] $taxonomies An array of taxonomy names. | |
| 849 | + * @return string | |
| 850 | + */ | |
| 851 | + public function filter_language_terms_orderby( $orderby, $args, $taxonomies ) { | |
| 852 | + $allowed_taxonomies = $this->translatable_objects->get_taxonomy_names( array( 'language' ) ); | |
| 853 | + | |
| 854 | + if ( ! is_array( $taxonomies ) || ! empty( array_diff( $taxonomies, $allowed_taxonomies ) ) ) { | |
| 855 | + return $orderby; | |
| 856 | + } | |
| 857 | + | |
| 858 | + if ( empty( $orderby ) || ! is_string( $orderby ) ) { | |
| 859 | + return $orderby; | |
| 860 | + } | |
| 861 | + | |
| 862 | + if ( ! preg_match( '@^(?<alias>[^.]+)\.term_group$@', $orderby, $matches ) ) { | |
| 863 | + return $orderby; | |
| 864 | + } | |
| 865 | + | |
| 866 | + return sprintf( 'tt.taxonomy = \'language\' DESC, %1$s.term_group, %1$s.term_id', $matches['alias'] ); | |
| 867 | + } | |
| 868 | + | |
| 869 | + /** | |
| 870 | + * Maybe adds the missing language terms for 3rd party language taxonomies. | |
| 871 | + * | |
| 872 | + * @since 3.4 | |
| 873 | + * | |
| 874 | + * @return void | |
| 875 | + */ | |
| 876 | + public function maybe_create_language_terms() { | |
| 877 | + $registered_taxonomies = array_diff( | |
| 878 | + $this->translatable_objects->get_taxonomy_names( array( 'language' ) ), | |
| 879 | + // Exclude the post and term language taxonomies from the list. | |
| 880 | + array( $this->post->get_tax_language(), $this->term->get_tax_language() ) | |
| 671 | 881 | ); |
| 882 | + | |
| 883 | + if ( empty( $registered_taxonomies ) ) { | |
| 884 | + // No 3rd party language taxonomies. | |
| 885 | + return; | |
| 886 | + } | |
| 887 | + | |
| 888 | + // We have at least one 3rd party language taxonomy. | |
| 889 | + $known_taxonomies = ! empty( $this->options['language_taxonomies'] ) && is_array( $this->options['language_taxonomies'] ) ? $this->options['language_taxonomies'] : array(); | |
| 890 | + $new_taxonomies = array_diff( $registered_taxonomies, $known_taxonomies ); | |
| 891 | + | |
| 892 | + if ( empty( $new_taxonomies ) ) { | |
| 893 | + // No new 3rd party language taxonomies. | |
| 894 | + return; | |
| 895 | + } | |
| 896 | + | |
| 897 | + // We have at least one unknown 3rd party language taxonomy. | |
| 898 | + foreach ( $this->get_languages_list() as $language ) { | |
| 899 | + $this->update_secondary_language_terms( $language->slug, $language->name, $language, $new_taxonomies ); | |
| 900 | + } | |
| 901 | + | |
| 902 | + // Clear the cache, so the new `term_id` and `term_taxonomy_id` appear in the languages list. | |
| 903 | + $this->clean_languages_cache(); | |
| 904 | + | |
| 905 | + // Keep the previous values, so this is triggered only once per taxonomy. | |
| 906 | + $this->options['language_taxonomies'] = array_merge( $known_taxonomies, $new_taxonomies ); | |
| 907 | + update_option( 'polylang', $this->options ); | |
| 908 | + } | |
| 909 | + | |
| 910 | + /** | |
| 911 | + * Updates or adds new terms for a secondary language taxonomy (aka not 'language'). | |
| 912 | + * | |
| 913 | + * @since 3.4 | |
| 914 | + * | |
| 915 | + * @param string $slug Language term slug (with or without the `pll_` prefix). | |
| 916 | + * @param string $name Language name (label). | |
| 917 | + * @param PLL_Language|null $language Optional. A language object. Required to update the existing terms. | |
| 918 | + * @param string[] $taxonomies Optional. List of language taxonomies to deal with. An empty value means | |
| 919 | + * all of them. Defaults to all taxonomies. | |
| 920 | + * @return void | |
| 921 | + * | |
| 922 | + * @phpstan-param non-empty-string $slug | |
| 923 | + * @phpstan-param non-empty-string $name | |
| 924 | + * @phpstan-param array<non-empty-string> $taxonomies | |
| 925 | + */ | |
| 926 | + protected function update_secondary_language_terms( $slug, $name, PLL_Language $language = null, array $taxonomies = array() ) { | |
| 927 | + $slug = 0 === strpos( $slug, 'pll_' ) ? $slug : "pll_$slug"; | |
| 928 | + | |
| 929 | + foreach ( $this->translatable_objects->get_secondary_translatable_objects() as $object ) { | |
| 930 | + if ( ! empty( $taxonomies ) && ! in_array( $object->get_tax_language(), $taxonomies, true ) ) { | |
| 931 | + // Not in the list. | |
| 932 | + continue; | |
| 933 | + } | |
| 934 | + | |
| 935 | + if ( ! empty( $language ) ) { | |
| 936 | + $term_id = $language->get_tax_prop( $object->get_tax_language(), 'term_id' ); | |
| 937 | + } else { | |
| 938 | + $term_id = 0; | |
| 939 | + } | |
| 940 | + | |
| 941 | + if ( empty( $term_id ) ) { | |
| 942 | + // Attempt to repair the language if a term has been deleted by a database cleaning tool. | |
| 943 | + wp_insert_term( $name, $object->get_tax_language(), array( 'slug' => $slug ) ); | |
| 944 | + continue; | |
| 945 | + } | |
| 946 | + | |
| 947 | + /** @var PLL_Language $language */ | |
| 948 | + if ( "pll_{$language->slug}" !== $slug || $language->name !== $name ) { | |
| 949 | + // Something has changed. | |
| 950 | + wp_update_term( $term_id, $object->get_tax_language(), array( 'slug' => $slug, 'name' => $name ) ); | |
| 951 | + } | |
| 952 | + } | |
| 953 | + } | |
| 954 | + | |
| 955 | + /** | |
| 956 | + * Returns the list of available languages, based on the language taxonomy terms. | |
| 957 | + * Stores the list in a db transient and in a `PLL_Cache` object. | |
| 958 | + * | |
| 959 | + * @since 3.4 | |
| 960 | + * | |
| 961 | + * @return PLL_Language[] An array of `PLL_Language` objects, array keys are the type. | |
| 962 | + * | |
| 963 | + * @phpstan-return list<PLL_Language> | |
| 964 | + */ | |
| 965 | + protected function get_languages_from_taxonomies() { | |
| 966 | + $terms_by_slug = array(); | |
| 967 | + | |
| 968 | + foreach ( $this->get_language_terms() as $term ) { | |
| 969 | + // Except for language taxonomy term slugs, remove 'pll_' prefix from the other language taxonomy term slugs. | |
| 970 | + $key = 'language' === $term->taxonomy ? $term->slug : substr( $term->slug, 4 ); | |
| 971 | + $terms_by_slug[ $key ][ $term->taxonomy ] = $term; | |
| 972 | + } | |
| 973 | + | |
| 974 | + /** | |
| 975 | + * @var ( | |
| 976 | + * array{ | |
| 977 | + * string: array{ | |
| 978 | + * language: WP_Term, | |
| 979 | + * }&array<non-empty-string, WP_Term> | |
| 980 | + * } | |
| 981 | + * ) $terms_by_slug | |
| 982 | + */ | |
| 983 | + $languages = array_filter( | |
| 984 | + array_map( | |
| 985 | + array( new PLL_Language_Factory( $this->options ), 'get_from_terms' ), | |
| 986 | + array_values( $terms_by_slug ) | |
| 987 | + ) | |
| 988 | + ); | |
| 989 | + | |
| 990 | + /** | |
| 991 | + * Filters the list of languages *before* it is stored in the persistent cache. | |
| 992 | + * /!\ This filter is fired *before* the $polylang object is available. | |
| 993 | + * | |
| 994 | + * @since 1.7.5 | |
| 995 | + * @since 3.4 Deprecated. | |
| 996 | + * @deprecated | |
| 997 | + * | |
| 998 | + * @param PLL_Language[] $languages The list of language objects. | |
| 999 | + * @param PLL_Model $model PLL_Model object. | |
| 1000 | + */ | |
| 1001 | + $languages = apply_filters_deprecated( 'pll_languages_list', array( $languages, $this ), '3.4', 'pll_additional_language_data' ); | |
| 1002 | + | |
| 1003 | + if ( ! $this->are_languages_ready() ) { | |
| 1004 | + // Do not cache an incomplete list. | |
| 1005 | + /** @var list<PLL_Language> $languages */ | |
| 1006 | + return $languages; | |
| 1007 | + } | |
| 1008 | + | |
| 1009 | + /** | |
| 1010 | + * Don't store directly objects as it badly break with some hosts ( GoDaddy ) due to race conditions when using object cache. | |
| 1011 | + * Thanks to captin411 for catching this! | |
| 1012 | + * | |
| 1013 | + * @see https://wordpress.org/support/topic/fatal-error-pll_model_languages_list?replies=8#post-6782255 | |
| 1014 | + */ | |
| 1015 | + $languages_data = array_map( | |
| 1016 | + function ( $language ) { | |
| 1017 | + return $language->to_array( 'db' ); | |
| 1018 | + }, | |
| 1019 | + $languages | |
| 1020 | + ); | |
| 1021 | + | |
| 1022 | + set_transient( 'pll_languages_list', $languages_data ); | |
| 1023 | + | |
| 1024 | + /** @var list<PLL_Language> $languages */ | |
| 1025 | + return $languages; | |
| 1026 | + } | |
| 1027 | + | |
| 1028 | + /** | |
| 1029 | + * Returns the list of existing language terms. | |
| 1030 | + * - Returns all terms, that are or not assigned to posts. | |
| 1031 | + * - Terms are ordered by `term_group` and `term_id` (see `PLL_Model->filter_language_terms_orderby()`). | |
| 1032 | + * | |
| 1033 | + * @since 3.2.3 | |
| 1034 | + * | |
| 1035 | + * @return WP_Term[] | |
| 1036 | + */ | |
| 1037 | + protected function get_language_terms() { | |
| 1038 | + add_filter( 'get_terms_orderby', array( $this, 'filter_language_terms_orderby' ), 10, 3 ); | |
| 1039 | + $terms = get_terms( | |
| 1040 | + array( | |
| 1041 | + 'taxonomy' => $this->translatable_objects->get_taxonomy_names( array( 'language' ) ), | |
| 1042 | + 'orderby' => 'term_group', | |
| 1043 | + 'hide_empty' => false, | |
| 1044 | + ) | |
| 1045 | + ); | |
| 1046 | + remove_filter( 'get_terms_orderby', array( $this, 'filter_language_terms_orderby' ) ); | |
| 1047 | + | |
| 1048 | + return empty( $terms ) || is_wp_error( $terms ) ? array() : $terms; | |
| 672 | 1049 | } |
| 673 | 1050 | } |