PluginProbe
Polylang / 3.8.9
Polylang v3.8.9
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / src / Model / Taxonomies.php

Taxonomies.php in Polylang 3.8.9, at src/Model/Taxonomies.php

136 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 namespace WP_Syntex\Polylang\Model;
7
8 use PLL_Translated_Term;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Model for taxonomies filtered/translated by Polylang.
14 *
15 * @since 3.7
16 */
17 class Taxonomies {
18 /**
19 * Translated term model.
20 *
21 * @var PLL_Translated_Term
22 */
23 public $translated_object;
24
25 /**
26 * Constructor.
27 *
28 * @since 3.7
29 *
30 * @param PLL_Translated_Term $translated_object Terms model.
31 */
32 public function __construct( PLL_Translated_Term $translated_object ) {
33 $this->translated_object = $translated_object;
34 }
35
36 /**
37 * Returns taxonomies that need to be translated.
38 * The taxonomies list is cached for better better performance.
39 * The method waits for 'after_setup_theme' to apply the cache
40 * to allow themes adding the filter in functions.php.
41 *
42 * @since 1.2
43 * @since 3.7 Moved from `PLL_Model::get_translated_taxonomies()` to `WP_Syntex\Polylang\Model\Taxonomies::get_translated()`.
44 *
45 * @param bool $filter True if we should return only valid registered taxonomies.
46 * @return string[] Array of registered taxonomy names for which Polylang manages languages and translations.
47 */
48 public function get_translated( $filter = true ): array {
49 return $this->translated_object->get_translated_object_types( $filter );
50 }
51
52 /**
53 * Returns true if Polylang manages languages and translations for this taxonomy.
54 *
55 * @since 1.2
56 * @since 3.7 Moved from `PLL_Model::is_translated_taxonomy()` to `WP_Syntex\Polylang\Model\Taxonomies::is_translated()`.
57 *
58 * @param string|string[] $tax Taxonomy name or array of taxonomy names.
59 * @return bool
60 */
61 public function is_translated( $tax ): bool {
62 if ( empty( array_filter( (array) $tax ) ) ) {
63 return false;
64 }
65
66 return $this->translated_object->is_translated_object_type( $tax );
67 }
68
69 /**
70 * Return taxonomies that need to be filtered (post_format like).
71 *
72 * @since 1.7
73 * @since 3.7 Moved from `PLL_Model::get_filtered_taxonomies()` to `WP_Syntex\Polylang\Model\Taxonomies::get_filtered()`.
74 *
75 * @param bool $filter True if we should return only valid registered taxonomies.
76 * @return string[] Array of registered taxonomy names.
77 */
78 public function get_filtered( $filter = true ): array {
79 if ( did_action( 'after_setup_theme' ) ) {
80 static $taxonomies = null;
81 }
82
83 if ( empty( $taxonomies ) ) {
84 $taxonomies = array( 'post_format' => 'post_format' );
85
86 /**
87 * Filters the list of taxonomies not translatable but filtered by language.
88 * Includes only the post format by default
89 * The filter must be added soon in the WordPress loading process:
90 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
91 *
92 * @since 1.7
93 *
94 * @param string[] $taxonomies List of taxonomy names.
95 * @param bool $is_settings True when displaying the list of custom taxonomies in Polylang settings.
96 */
97 $taxonomies = apply_filters( 'pll_filtered_taxonomies', $taxonomies, false );
98 }
99
100 return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies;
101 }
102
103 /**
104 * Returns true if Polylang filters this taxonomy per language.
105 *
106 * @since 1.7
107 * @since 3.7 Moved from `PLL_Model::is_filtered_taxonomy()` to `WP_Syntex\Polylang\Model\Taxonomies::is_filtered()`.
108 *
109 * @param string|string[] $tax Taxonomy name or array of taxonomy names.
110 * @return bool
111 */
112 public function is_filtered( $tax ): bool {
113 $taxonomies = $this->get_filtered( false );
114 return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) ) || in_array( $tax, $taxonomies );
115 }
116
117 /**
118 * Returns the query vars of all filtered taxonomies.
119 *
120 * @since 1.7
121 * @since 3.7 Moved from `PLL_Model::get_filtered_taxonomies_query_vars()` to `WP_Syntex\Polylang\Model\Taxonomies::get_filtered_query_vars()`.
122 *
123 * @return string[]
124 */
125 public function get_filtered_query_vars(): array {
126 $query_vars = array();
127 foreach ( $this->get_filtered() as $filtered_tax ) {
128 $tax = get_taxonomy( $filtered_tax );
129 if ( ! empty( $tax ) && is_string( $tax->query_var ) ) {
130 $query_vars[] = $tax->query_var;
131 }
132 }
133 return $query_vars;
134 }
135 }
136