| @@ -8,29 +8,53 @@ | ||
| 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 | |
| 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 | + * Translated post model. | |
| 20 | 28 | * |
| 29 | + * @var PLL_Translated_Post | |
| 30 | + */ | |
| 31 | + public $post; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Translated term model. | |
| 35 | + * | |
| 36 | + * @var PLL_Translated_Term | |
| 37 | + */ | |
| 38 | + public $term; | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * Constructor. | |
| 42 | + * Setups translated objects sub models. | |
| 43 | + * Setups filters and actions. | |
| 44 | + * | |
| 21 | 45 | * @since 1.2 |
| 22 | 46 | * |
| 23 | - * @param array $options Polylang options | |
| 47 | + * @param array $options Polylang options. | |
| 24 | 48 | */ |
| 25 | 49 | public function __construct( &$options ) { |
| 26 | 50 | $this->options = &$options; |
| 27 | 51 | |
| 28 | 52 | $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 | |
| 53 | + $this->post = new PLL_Translated_Post( $this ); // Translated post sub model. | |
| 54 | + $this->term = new PLL_Translated_Term( $this ); // Translated term sub model. | |
| 31 | 55 | |
| 32 | - // We need to clean languages cache when editing a language and when modifying the permalink structure | |
| 56 | + // We need to clean languages cache when editing a language and when modifying the permalink structure. | |
| 33 | 57 | add_action( 'edited_term_taxonomy', array( $this, 'clean_languages_cache' ), 10, 2 ); |
| 34 | 58 | add_action( 'update_option_permalink_structure', array( $this, 'clean_languages_cache' ) ); |
| 35 | 59 | add_action( 'update_option_siteurl', array( $this, 'clean_languages_cache' ) ); |
| 36 | 60 | add_action( 'update_option_home', array( $this, 'clean_languages_cache' ) ); |
| @@ -36,31 +60,29 @@ | ||
| 36 | 60 | add_action( 'update_option_home', array( $this, 'clean_languages_cache' ) ); |
| 37 | 61 | |
| 38 | 62 | add_filter( 'get_terms_args', array( $this, 'get_terms_args' ) ); |
| 39 | 63 | |
| 40 | - // Just in case someone would like to display the language description ;- ) | |
| 64 | + // Just in case someone would like to display the language description ;). | |
| 41 | 65 | add_filter( 'language_description', '__return_empty_string' ); |
| 42 | 66 | } |
| 43 | 67 | |
| 44 | 68 | /** |
| 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 | |
| 69 | + * Returns the list of available languages. | |
| 70 | + * - Stores the list in a db transient ( except flags ), unless PLL_CACHE_LANGUAGES is set to false. | |
| 71 | + * - Caches the list ( with flags ) in a PLL_Cache object. | |
| 48 | 72 | * |
| 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 | 73 | * @since 0.1 |
| 55 | 74 | * |
| 56 | - * @param array $args | |
| 57 | - * @return array|string|int list of PLL_Language objects or PLL_Language object properties | |
| 75 | + * @param array $args { | |
| 76 | + * @type bool $hide_empty Hides languages with no posts if set to true ( defaults to false ). | |
| 77 | + * @type string $fields Returns only that field if set; {@see PLL_Language} for a list of fields. | |
| 78 | + * } | |
| 79 | + * @return array List of PLL_Language objects or PLL_Language object properties. | |
| 58 | 80 | */ |
| 59 | 81 | public function get_languages_list( $args = array() ) { |
| 60 | 82 | if ( false === $languages = $this->cache->get( 'languages' ) ) { |
| 61 | 83 | |
| 62 | - // Create the languages from taxonomies | |
| 84 | + // Create the languages from taxonomies. | |
| 63 | 85 | if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || false === ( $languages = get_transient( 'pll_languages_list' ) ) ) { |
| 64 | 86 | $languages = get_terms( 'language', array( 'hide_empty' => false, 'orderby' => 'term_group' ) ); |
| 65 | 87 | $languages = empty( $languages ) || is_wp_error( $languages ) ? array() : $languages; |
| 66 | 88 | |
| @@ -68,38 +90,39 @@ | ||
| 68 | 90 | $term_languages = empty( $term_languages ) || is_wp_error( $term_languages ) ? |
| 69 | 91 | array() : array_combine( wp_list_pluck( $term_languages, 'slug' ), $term_languages ); |
| 70 | 92 | |
| 71 | 93 | 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 | 94 | foreach ( $languages as $k => $v ) { |
| 74 | 95 | $languages[ $k ] = new PLL_Language( $v, $term_languages[ 'pll_' . $v->slug ] ); |
| 75 | 96 | } |
| 76 | 97 | |
| 77 | - // We will need the languages list to allow its access in the filter below | |
| 98 | + // We will need the languages list to allow its access in the filter below. | |
| 78 | 99 | $this->cache->set( 'languages', $languages ); |
| 79 | 100 | |
| 80 | 101 | /** |
| 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 | |
| 102 | + * Filters the list of languages *before* it is stored in the persistent cache. | |
| 103 | + * /!\ This filter is fired *before* the $polylang object is available. | |
| 83 | 104 | * |
| 84 | 105 | * @since 1.7.5 |
| 85 | 106 | * |
| 86 | - * @param array $languages the list of language objects | |
| 87 | - * @param object $model PLL_Model object | |
| 107 | + * @param PLL_Language[] $languages The list of language objects. | |
| 108 | + * @param PLL_Model $model PLL_Model object. | |
| 88 | 109 | */ |
| 89 | 110 | $languages = apply_filters( 'pll_languages_list', $languages, $this ); |
| 90 | 111 | |
| 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; | |
| 112 | + /* | |
| 113 | + * Don't store directly objects as it badly break with some hosts ( GoDaddy ) due to race conditions when using object cache. | |
| 114 | + * Thanks to captin411 for catching this! | |
| 115 | + * @see https://wordpress.org/support/topic/fatal-error-pll_model_languages_list?replies=8#post-6782255 | |
| 116 | + */ | |
| 94 | 117 | set_transient( 'pll_languages_list', array_map( 'get_object_vars', $languages ) ); |
| 95 | 118 | } |
| 96 | 119 | else { |
| 97 | - $languages = array(); // In case something went wrong | |
| 120 | + $languages = array(); // In case something went wrong. | |
| 98 | 121 | } |
| 99 | 122 | } |
| 100 | 123 | |
| 101 | - // Create the languages directly from arrays stored in transients | |
| 124 | + // Create the languages directly from arrays stored in transients. | |
| 102 | 125 | else { |
| 103 | 126 | foreach ( $languages as $k => $v ) { |
| 104 | 127 | $languages[ $k ] = new PLL_Language( $v ); |
| 105 | 128 | } |
| @@ -105,14 +128,14 @@ | ||
| 105 | 128 | } |
| 106 | 129 | } |
| 107 | 130 | |
| 108 | 131 | /** |
| 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 | |
| 132 | + * Filters the list of languages *after* it is stored in the persistent cache. | |
| 133 | + * /!\ This filter is fired *before* the $polylang object is available. | |
| 111 | 134 | * |
| 112 | 135 | * @since 1.8 |
| 113 | 136 | * |
| 114 | - * @param array $languages the list of language objects | |
| 137 | + * @param PLL_Language[] $languages The list of language objects. | |
| 115 | 138 | */ |
| 116 | 139 | $languages = apply_filters( 'pll_after_languages_cache', $languages ); |
| 117 | 140 | $this->cache->set( 'languages', $languages ); |
| 118 | 141 | } |
| @@ -118,9 +141,9 @@ | ||
| 118 | 141 | } |
| 119 | 142 | |
| 120 | 143 | $args = wp_parse_args( $args, array( 'hide_empty' => false ) ); |
| 121 | 144 | |
| 122 | - // Remove empty languages if requested | |
| 145 | + // Remove empty languages if requested. | |
| 123 | 146 | if ( $args['hide_empty'] ) { |
| 124 | 147 | $languages = wp_list_filter( $languages, array( 'count' => 0 ), 'NOT' ); |
| 125 | 148 | } |
| 126 | 149 | |
| @@ -135,8 +158,9 @@ | ||
| 135 | 158 | * @since 1.2 |
| 136 | 159 | * |
| 137 | 160 | * @param int $term not used |
| 138 | 161 | * @param string $taxonomy taxonomy name |
| 162 | + * @return void | |
| 139 | 163 | */ |
| 140 | 164 | public function clean_languages_cache( $term = 0, $taxonomy = null ) { |
| 141 | 165 | if ( empty( $taxonomy ) || 'language' == $taxonomy ) { |
| 142 | 166 | delete_transient( 'pll_languages_list' ); |
| @@ -159,14 +183,14 @@ | ||
| 159 | 183 | return $args; |
| 160 | 184 | } |
| 161 | 185 | |
| 162 | 186 | /** |
| 163 | - * Returns the language by its term_id, tl_term_id, slug or locale | |
| 187 | + * Returns the language by its term_id, tl_term_id, slug or locale. | |
| 164 | 188 | * |
| 165 | 189 | * @since 0.1 |
| 166 | 190 | * |
| 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 | |
| 191 | + * @param mixed $value term_id, tl_term_id, slug or locale of the queried language. | |
| 192 | + * @return PLL_Language|false Language object, false if no language found. | |
| 169 | 193 | */ |
| 170 | 194 | public function get_language( $value ) { |
| 171 | 195 | if ( is_object( $value ) ) { |
| 172 | 196 | return $value instanceof PLL_Language ? $value : $this->get_language( $value->term_id ); // will force cast to PLL_Language |
| @@ -186,15 +210,15 @@ | ||
| 186 | 210 | return $return; |
| 187 | 211 | } |
| 188 | 212 | |
| 189 | 213 | /** |
| 190 | - * Adds terms clauses to get_terms to filter them by languages - used in both frontend and admin | |
| 214 | + * Adds terms clauses to the term query to filter them by languages. | |
| 191 | 215 | * |
| 192 | 216 | * @since 1.2 |
| 193 | 217 | * |
| 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 | |
| 218 | + * @param string[] $clauses The list of sql clauses in terms query. | |
| 219 | + * @param PLL_Language $lang PLL_Language object. | |
| 220 | + * @return string[] Modified list of clauses. | |
| 197 | 221 | */ |
| 198 | 222 | public function terms_clauses( $clauses, $lang ) { |
| 199 | 223 | if ( ! empty( $lang ) && false === strpos( $clauses['join'], 'pll_tr' ) ) { |
| 200 | 224 | $clauses['join'] .= $this->term->join_clause(); |
| @@ -203,16 +227,17 @@ | ||
| 203 | 227 | return $clauses; |
| 204 | 228 | } |
| 205 | 229 | |
| 206 | 230 | /** |
| 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 | |
| 231 | + * Returns post types that need to be translated. | |
| 232 | + * The post types list is cached for better better performance. | |
| 233 | + * The method waits for 'after_setup_theme' to apply the cache | |
| 234 | + * to allow themes adding the filter in functions.php. | |
| 210 | 235 | * |
| 211 | 236 | * @since 1.2 |
| 212 | 237 | * |
| 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 | |
| 238 | + * @param bool $filter True if we should return only valid registered post types. | |
| 239 | + * @return string[] Post type names for which Polylang manages languages and translations. | |
| 215 | 240 | */ |
| 216 | 241 | public function get_translated_post_types( $filter = true ) { |
| 217 | 242 | if ( false === $post_types = $this->cache->get( 'post_types' ) ) { |
| 218 | 243 | $post_types = array( 'post' => 'post', 'page' => 'page', 'wp_block' => 'wp_block' ); |
| @@ -225,9 +250,9 @@ | ||
| 225 | 250 | $post_types = array_merge( $post_types, array_combine( $this->options['post_types'], $this->options['post_types'] ) ); |
| 226 | 251 | } |
| 227 | 252 | |
| 228 | 253 | /** |
| 229 | - * Filter the list of post types available for translation. | |
| 254 | + * Filters the list of post types available for translation. | |
| 230 | 255 | * The default are post types which have the parameter ‘public’ set to true. |
| 231 | 256 | * The filter must be added soon in the WordPress loading process: |
| 232 | 257 | * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes. |
| 233 | 258 | * |
| @@ -232,10 +257,10 @@ | ||
| 232 | 257 | * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes. |
| 233 | 258 | * |
| 234 | 259 | * @since 0.8 |
| 235 | 260 | * |
| 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 | |
| 261 | + * @param string[] $post_types List of post type names. | |
| 262 | + * @param bool $is_settings True when displaying the list of custom post types in Polylang settings. | |
| 238 | 263 | */ |
| 239 | 264 | $post_types = apply_filters( 'pll_get_post_types', $post_types, false ); |
| 240 | 265 | |
| 241 | 266 | if ( did_action( 'after_setup_theme' ) ) { |
| @@ -246,13 +271,13 @@ | ||
| 246 | 271 | return $filter ? array_intersect( $post_types, get_post_types() ) : $post_types; |
| 247 | 272 | } |
| 248 | 273 | |
| 249 | 274 | /** |
| 250 | - * Returns true if Polylang manages languages and translations for this post type | |
| 275 | + * Returns true if Polylang manages languages and translations for this post type. | |
| 251 | 276 | * |
| 252 | 277 | * @since 1.2 |
| 253 | 278 | * |
| 254 | - * @param string|array $post_type post type name or array of post type names | |
| 279 | + * @param string|string[] $post_type Post type name or array of post type names. | |
| 255 | 280 | * @return bool |
| 256 | 281 | */ |
| 257 | 282 | public function is_translated_post_type( $post_type ) { |
| 258 | 283 | $post_types = $this->get_translated_post_types( false ); |
| @@ -259,14 +284,14 @@ | ||
| 259 | 284 | return ( is_array( $post_type ) && array_intersect( $post_type, $post_types ) || in_array( $post_type, $post_types ) || 'any' === $post_type && ! empty( $post_types ) ); |
| 260 | 285 | } |
| 261 | 286 | |
| 262 | 287 | /** |
| 263 | - * Return taxonomies that need to be translated | |
| 288 | + * Returns taxonomies that need to be translated. | |
| 264 | 289 | * |
| 265 | 290 | * @since 1.2 |
| 266 | 291 | * |
| 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 | |
| 292 | + * @param bool $filter True if we should return only valid registered taxonomies. | |
| 293 | + * @return string[] Array of registered taxonomy names for which Polylang manages languages and translations. | |
| 269 | 294 | */ |
| 270 | 295 | public function get_translated_taxonomies( $filter = true ) { |
| 271 | 296 | if ( false === $taxonomies = $this->cache->get( 'taxonomies' ) ) { |
| 272 | 297 | $taxonomies = array( 'category' => 'category', 'post_tag' => 'post_tag' ); |
| @@ -275,9 +300,9 @@ | ||
| 275 | 300 | $taxonomies = array_merge( $taxonomies, array_combine( $this->options['taxonomies'], $this->options['taxonomies'] ) ); |
| 276 | 301 | } |
| 277 | 302 | |
| 278 | 303 | /** |
| 279 | - * Filter the list of taxonomies available for translation. | |
| 304 | + * Filters the list of taxonomies available for translation. | |
| 280 | 305 | * The default are taxonomies which have the parameter ‘public’ set to true. |
| 281 | 306 | * The filter must be added soon in the WordPress loading process: |
| 282 | 307 | * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes. |
| 283 | 308 | * |
| @@ -282,10 +307,10 @@ | ||
| 282 | 307 | * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes. |
| 283 | 308 | * |
| 284 | 309 | * @since 0.8 |
| 285 | 310 | * |
| 286 | - * @param array $taxonomies list of taxonomy names | |
| 287 | - * @param bool $is_settings true when displaying the list of custom taxonomies in Polylang settings | |
| 311 | + * @param string[] $taxonomies List of taxonomy names. | |
| 312 | + * @param bool $is_settings True when displaying the list of custom taxonomies in Polylang settings. | |
| 288 | 313 | */ |
| 289 | 314 | $taxonomies = apply_filters( 'pll_get_taxonomies', $taxonomies, false ); |
| 290 | 315 | if ( did_action( 'after_setup_theme' ) ) { |
| 291 | 316 | $this->cache->set( 'taxonomies', $taxonomies ); |
| @@ -295,13 +320,13 @@ | ||
| 295 | 320 | return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies; |
| 296 | 321 | } |
| 297 | 322 | |
| 298 | 323 | /** |
| 299 | - * Returns true if Polylang manages languages and translations for this taxonomy | |
| 324 | + * Returns true if Polylang manages languages and translations for this taxonomy. | |
| 300 | 325 | * |
| 301 | 326 | * @since 1.2 |
| 302 | 327 | * |
| 303 | - * @param string|array $tax taxonomy name or array of taxonomy names | |
| 328 | + * @param string|string[] $tax Taxonomy name or array of taxonomy names. | |
| 304 | 329 | * @return bool |
| 305 | 330 | */ |
| 306 | 331 | public function is_translated_taxonomy( $tax ) { |
| 307 | 332 | $taxonomies = $this->get_translated_taxonomies( false ); |
| @@ -308,14 +333,14 @@ | ||
| 308 | 333 | return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) ); |
| 309 | 334 | } |
| 310 | 335 | |
| 311 | 336 | /** |
| 312 | - * Return taxonomies that need to be filtered ( post_format like ) | |
| 337 | + * Return staxonomies that need to be filtered ( post_format like ). | |
| 313 | 338 | * |
| 314 | 339 | * @since 1.7 |
| 315 | 340 | * |
| 316 | - * @param bool $filter true if we should return only valid registered taxonomies | |
| 317 | - * @return array array of registered taxonomy names | |
| 341 | + * @param bool $filter True if we should return only valid registered taxonomies. | |
| 342 | + * @return string[] Array of registered taxonomy names. | |
| 318 | 343 | */ |
| 319 | 344 | public function get_filtered_taxonomies( $filter = true ) { |
| 320 | 345 | if ( did_action( 'after_setup_theme' ) ) { |
| 321 | 346 | static $taxonomies = null; |
| @@ -324,9 +349,9 @@ | ||
| 324 | 349 | if ( empty( $taxonomies ) ) { |
| 325 | 350 | $taxonomies = array( 'post_format' => 'post_format' ); |
| 326 | 351 | |
| 327 | 352 | /** |
| 328 | - * Filter the list of taxonomies not translatable but filtered by language. | |
| 353 | + * Filters the list of taxonomies not translatable but filtered by language. | |
| 329 | 354 | * Includes only the post format by default |
| 330 | 355 | * The filter must be added soon in the WordPress loading process: |
| 331 | 356 | * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes. |
| 332 | 357 | * |
| @@ -331,10 +356,10 @@ | ||
| 331 | 356 | * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes. |
| 332 | 357 | * |
| 333 | 358 | * @since 1.7 |
| 334 | 359 | * |
| 335 | - * @param array $taxonomies list of taxonomy names | |
| 336 | - * @param bool $is_settings true when displaying the list of custom taxonomies in Polylang settings | |
| 360 | + * @param string[] $taxonomies List of taxonomy names. | |
| 361 | + * @param bool $is_settings True when displaying the list of custom taxonomies in Polylang settings. | |
| 337 | 362 | */ |
| 338 | 363 | $taxonomies = apply_filters( 'pll_filtered_taxonomies', $taxonomies, false ); |
| 339 | 364 | } |
| 340 | 365 | |
| @@ -341,13 +366,13 @@ | ||
| 341 | 366 | return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies; |
| 342 | 367 | } |
| 343 | 368 | |
| 344 | 369 | /** |
| 345 | - * Returns true if Polylang filters this taxonomy per language | |
| 370 | + * Returns true if Polylang filters this taxonomy per language. | |
| 346 | 371 | * |
| 347 | 372 | * @since 1.7 |
| 348 | 373 | * |
| 349 | - * @param string|array $tax taxonomy name or array of taxonomy names | |
| 374 | + * @param string|string[] $tax Taxonomy name or array of taxonomy names. | |
| 350 | 375 | * @return bool |
| 351 | 376 | */ |
| 352 | 377 | public function is_filtered_taxonomy( $tax ) { |
| 353 | 378 | $taxonomies = $this->get_filtered_taxonomies( false ); |
| @@ -354,9 +379,9 @@ | ||
| 354 | 379 | return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) ); |
| 355 | 380 | } |
| 356 | 381 | |
| 357 | 382 | /** |
| 358 | - * Returns the query vars of all filtered taxonomies | |
| 383 | + * Returns the query vars of all filtered taxonomies. | |
| 359 | 384 | * |
| 360 | 385 | * @since 1.7 |
| 361 | 386 | * |
| 362 | 387 | * @return array |
| @@ -364,62 +389,27 @@ | ||
| 364 | 389 | public function get_filtered_taxonomies_query_vars() { |
| 365 | 390 | $query_vars = array(); |
| 366 | 391 | foreach ( $this->get_filtered_taxonomies() as $filtered_tax ) { |
| 367 | 392 | $tax = get_taxonomy( $filtered_tax ); |
| 368 | - $query_vars[] = $tax->query_var; | |
| 393 | + if ( ! empty( $tax ) ) { | |
| 394 | + $query_vars[] = $tax->query_var; | |
| 395 | + } | |
| 369 | 396 | } |
| 370 | 397 | return $query_vars; |
| 371 | 398 | } |
| 372 | 399 | |
| 373 | 400 | /** |
| 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 | 401 | * 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 | |
| 402 | + * but the native term_exists() will return true even if only one exists. | |
| 403 | + * So here the function adds the language parameter. | |
| 414 | 404 | * |
| 415 | 405 | * @since 1.4 |
| 416 | 406 | * |
| 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 | |
| 407 | + * @param string $term_name The term name. | |
| 408 | + * @param string $taxonomy Taxonomy name. | |
| 409 | + * @param int $parent Parent term id. | |
| 410 | + * @param string|PLL_Language $language The language slug or object. | |
| 411 | + * @return null|int The term_id of the found term. | |
| 422 | 412 | */ |
| 423 | 413 | public function term_exists( $term_name, $taxonomy, $parent, $language ) { |
| 424 | 414 | global $wpdb; |
| 425 | 415 | |
| @@ -440,17 +430,17 @@ | ||
| 440 | 430 | return $wpdb->get_var( $select . $join . $where ); |
| 441 | 431 | } |
| 442 | 432 | |
| 443 | 433 | /** |
| 444 | - * Checks if a term slug exists in a given language, taxonomy, hierarchy | |
| 434 | + * Checks if a term slug exists in a given language, taxonomy, hierarchy. | |
| 445 | 435 | * |
| 446 | 436 | * @since 1.9 |
| 447 | - * @since 2.8 Moved from PLL_Share_Term_Slug::term_exists() to PLL_Model::term_exists_by_slug() | |
| 437 | + * @since 2.8 Moved from PLL_Share_Term_Slug::term_exists() to PLL_Model::term_exists_by_slug(). | |
| 448 | 438 | * |
| 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. | |
| 439 | + * @param string $slug The term slug to test. | |
| 440 | + * @param string|PLL_Language $language The language slug or object. | |
| 441 | + * @param string $taxonomy Optional taxonomy name. | |
| 442 | + * @param int $parent Optional parent term id. | |
| 453 | 443 | * @return null|int The term_id of the found term. |
| 454 | 444 | */ |
| 455 | 445 | public function term_exists_by_slug( $slug, $language, $taxonomy = '', $parent = 0 ) { |
| 456 | 446 | global $wpdb; |
| @@ -478,10 +468,22 @@ | ||
| 478 | 468 | * Gets the number of posts per language in a date, author or post type archive. |
| 479 | 469 | * |
| 480 | 470 | * @since 1.2 |
| 481 | 471 | * |
| 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 ). | |
| 472 | + * @param PLL_Language $lang PLL_Language instance. | |
| 473 | + * @param array $q { | |
| 474 | + * WP_Query arguments: | |
| 475 | + * | |
| 476 | + * @type string|string[] $post_type Post type or array of post types. | |
| 477 | + * @type int $m Combination YearMonth. Accepts any four-digit year and month. | |
| 478 | + * @type int $year Four-digit year. | |
| 479 | + * @type int $monthnum Two-digit month. | |
| 480 | + * @type int $day Day of the month. | |
| 481 | + * @type int $author Author id. | |
| 482 | + * @type string $author_name User 'user_nicename'. | |
| 483 | + * @type string $post_format Post format. | |
| 484 | + * @type string $post_status Post status. | |
| 485 | + * } | |
| 484 | 486 | * @return int |
| 485 | 487 | */ |
| 486 | 488 | public function count_posts( $lang, $q = array() ) { |
| 487 | 489 | global $wpdb; |
| @@ -508,9 +510,9 @@ | ||
| 508 | 510 | if ( false === $counts ) { |
| 509 | 511 | $select = "SELECT pll_tr.term_taxonomy_id, COUNT( * ) AS num_posts FROM {$wpdb->posts}"; |
| 510 | 512 | $join = $this->post->join_clause(); |
| 511 | 513 | $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'] ) ) ); | |
| 514 | + $where .= sprintf( " AND {$wpdb->posts}.post_type IN ( '%s' )", implode( "', '", esc_sql( $q['post_type'] ) ) ); | |
| 513 | 515 | $where .= $this->post->where_clause( $this->get_languages_list() ); |
| 514 | 516 | $groupby = ' GROUP BY pll_tr.term_taxonomy_id'; |
| 515 | 517 | |
| 516 | 518 | if ( ! empty( $q['m'] ) ) { |
| @@ -570,13 +572,13 @@ | ||
| 570 | 572 | return empty( $counts[ $lang->term_taxonomy_id ] ) ? 0 : $counts[ $lang->term_taxonomy_id ]; |
| 571 | 573 | } |
| 572 | 574 | |
| 573 | 575 | /** |
| 574 | - * Setup the links model based on options | |
| 576 | + * Setup the links model based on options. | |
| 575 | 577 | * |
| 576 | 578 | * @since 1.2 |
| 577 | 579 | * |
| 578 | - * @return object implementing "links_model interface" | |
| 580 | + * @return PLL_Links_Model | |
| 579 | 581 | */ |
| 580 | 582 | public function get_links_model() { |
| 581 | 583 | $c = array( 'Directory', 'Directory', 'Subdomain', 'Domain' ); |
| 582 | 584 | $class = get_option( 'permalink_structure' ) ? 'PLL_Links_' . $c[ $this->options['force_lang'] ] : 'PLL_Links_Default'; |
| @@ -581,14 +583,14 @@ | ||
| 581 | 583 | $c = array( 'Directory', 'Directory', 'Subdomain', 'Domain' ); |
| 582 | 584 | $class = get_option( 'permalink_structure' ) ? 'PLL_Links_' . $c[ $this->options['force_lang'] ] : 'PLL_Links_Default'; |
| 583 | 585 | |
| 584 | 586 | /** |
| 585 | - * Filter the links model class to use | |
| 586 | - * /!\ this filter is fired *before* the $polylang object is available | |
| 587 | + * Filters the links model class to use. | |
| 588 | + * /!\ this filter is fired *before* the $polylang object is available. | |
| 587 | 589 | * |
| 588 | 590 | * @since 2.1.1 |
| 589 | 591 | * |
| 590 | - * @param string $class A class name: PLL_Links_Default, PLL_Links_Directory, PLL_Links_Subdomain, PLL_Links_Domain | |
| 592 | + * @param string $class A class name: PLL_Links_Default, PLL_Links_Directory, PLL_Links_Subdomain, PLL_Links_Domain. | |
| 591 | 593 | */ |
| 592 | 594 | $class = apply_filters( 'pll_links_model', $class ); |
| 593 | 595 | |
| 594 | 596 | return new $class( $this ); |
| @@ -594,80 +596,110 @@ | ||
| 594 | 596 | return new $class( $this ); |
| 595 | 597 | } |
| 596 | 598 | |
| 597 | 599 | /** |
| 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 | |
| 600 | + * Returns posts and terms ids without language ( used in settings ). | |
| 601 | 601 | * |
| 602 | - * @since 1.8 | |
| 602 | + * @since 0.9 | |
| 603 | + * @since 2.2.6 Add the $limit argument. | |
| 603 | 604 | * |
| 604 | - * @param string $func Function name | |
| 605 | - * @param array $args Function arguments | |
| 605 | + * @param int $limit Max number of posts or terms to return. Defaults to -1 (no limit). | |
| 606 | + * @return array { | |
| 607 | + * Objects without language. | |
| 608 | + * | |
| 609 | + * @type int[] $posts Array of post ids. | |
| 610 | + * @type int[] $terms Array of term ids. | |
| 611 | + * } | |
| 606 | 612 | */ |
| 607 | - public function __call( $func, $args ) { | |
| 608 | - $f = $func; | |
| 613 | + public function get_objects_with_no_lang( $limit = -1 ) { | |
| 614 | + /** | |
| 615 | + * Filters the max number of posts or terms to return when searching objects with no language. | |
| 616 | + * This filter can be used to decrease the memory usage in case the number of objects | |
| 617 | + * without language is too big. Using a negative value is equivalent to have no limit. | |
| 618 | + * | |
| 619 | + * @since 2.2.6 | |
| 620 | + * | |
| 621 | + * @param int $limit Max number of posts or terms to retrieve from the database. | |
| 622 | + */ | |
| 623 | + $limit = (int) apply_filters( 'get_objects_with_no_lang_limit', $limit ); | |
| 609 | 624 | |
| 610 | - switch ( $func ) { | |
| 611 | - case 'get_object_term': | |
| 612 | - $o = ( false === strpos( $args[1], 'term' ) ) ? 'post' : 'term'; | |
| 613 | - break; | |
| 625 | + $posts = $this->get_posts_with_no_lang( $this->get_translated_post_types(), $limit ); | |
| 626 | + $terms = $this->get_terms_with_no_lang( $this->get_translated_taxonomies(), $limit ); | |
| 614 | 627 | |
| 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; | |
| 628 | + /** | |
| 629 | + * Filters the list of untranslated posts ids and terms ids | |
| 630 | + * | |
| 631 | + * @since 0.9 | |
| 632 | + * | |
| 633 | + * @param array|false $objects false if no ids found, list of post and/or term ids otherwise. | |
| 634 | + */ | |
| 635 | + return apply_filters( 'pll_get_objects_with_no_lang', empty( $posts ) && empty( $terms ) ? false : array( 'posts' => $posts, 'terms' => $terms ) ); | |
| 636 | + } | |
| 623 | 637 | |
| 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; | |
| 638 | + /** | |
| 639 | + * Returns ids of post without language. | |
| 640 | + * | |
| 641 | + * @since 3.1 | |
| 642 | + * | |
| 643 | + * @param string|string[] $post_types A translated post type or an array of translated post types. | |
| 644 | + * @param int $limit Max number of posts to return. | |
| 645 | + * @return int[] | |
| 646 | + */ | |
| 647 | + public function get_posts_with_no_lang( $post_types, $limit ) { | |
| 648 | + return get_posts( | |
| 649 | + array( | |
| 650 | + 'numberposts' => $limit, | |
| 651 | + 'nopaging' => $limit <= 0, | |
| 652 | + 'post_type' => $post_types, | |
| 653 | + 'post_status' => 'any', | |
| 654 | + 'fields' => 'ids', | |
| 655 | + 'tax_query' => array( | |
| 656 | + array( | |
| 657 | + 'taxonomy' => 'language', | |
| 658 | + 'terms' => $this->get_languages_list( array( 'fields' => 'term_id' ) ), | |
| 659 | + 'operator' => 'NOT IN', | |
| 660 | + ), | |
| 661 | + ), | |
| 662 | + ) | |
| 663 | + ); | |
| 664 | + } | |
| 635 | 665 | |
| 636 | - case 'where_clause': | |
| 637 | - case 'get_objects_in_language': | |
| 638 | - $o = $args[1]; | |
| 639 | - unset( $args[1] ); | |
| 640 | - break; | |
| 641 | - } | |
| 666 | + /** | |
| 667 | + * Returns ids of terms without language. | |
| 668 | + * | |
| 669 | + * @since 3.1 | |
| 670 | + * | |
| 671 | + * @param string|string[] $taxonomies A translated taxonomy or an array of taxonomies post types. | |
| 672 | + * @param int $limit Max number of terms to return. | |
| 673 | + * @return int[] | |
| 674 | + */ | |
| 675 | + public function get_terms_with_no_lang( $taxonomies, $limit ) { | |
| 676 | + global $wpdb; | |
| 642 | 677 | |
| 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 | |
| 678 | + $taxonomies = (array) $taxonomies; | |
| 647 | 679 | |
| 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 | - ); | |
| 658 | - } | |
| 659 | - return call_user_func_array( array( $this->$o, $f ), $args ); | |
| 680 | + $sql = sprintf( | |
| 681 | + "SELECT {$wpdb->term_taxonomy}.term_id FROM {$wpdb->term_taxonomy} | |
| 682 | + WHERE taxonomy IN ('%s') | |
| 683 | + AND {$wpdb->term_taxonomy}.term_id NOT IN ( | |
| 684 | + SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN (%s) | |
| 685 | + ) | |
| 686 | + %s", | |
| 687 | + implode( "','", array_map( 'esc_sql', $taxonomies ) ), | |
| 688 | + implode( ',', array_map( 'intval', $this->get_languages_list( array( 'fields' => 'tl_term_taxonomy_id' ) ) ) ), | |
| 689 | + $limit > 0 ? sprintf( 'LIMIT %d', intval( $limit ) ) : '' | |
| 690 | + ); | |
| 691 | + | |
| 692 | + $key = md5( $sql ); | |
| 693 | + $last_changed = wp_cache_get_last_changed( 'terms' ); | |
| 694 | + $cache_key = "terms_no_lang:{$key}:{$last_changed}"; | |
| 695 | + | |
| 696 | + $term_ids = wp_cache_get( $cache_key, 'terms' ); | |
| 697 | + | |
| 698 | + if ( false === $term_ids ) { | |
| 699 | + $term_ids = $wpdb->get_col( $sql ); // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 700 | + wp_cache_set( $cache_key, $term_ids, 'terms' ); | |
| 660 | 701 | } |
| 661 | 702 | |
| 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 | - ); | |
| 703 | + return $term_ids; | |
| 672 | 704 | } |
| 673 | 705 | } |