| 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 |
/** @phpstan-var non-empty-array<non-empty-string>|non-empty-string $tax */ |
| 67 |
return $this->translated_object->is_translated_object_type( $tax ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Return taxonomies that need to be filtered (post_format like). |
| 72 |
* |
| 73 |
* @since 1.7 |
| 74 |
* @since 3.7 Moved from `PLL_Model::get_filtered_taxonomies()` to `WP_Syntex\Polylang\Model\Taxonomies::get_filtered()`. |
| 75 |
* |
| 76 |
* @param bool $filter True if we should return only valid registered taxonomies. |
| 77 |
* @return string[] Array of registered taxonomy names. |
| 78 |
*/ |
| 79 |
public function get_filtered( $filter = true ): array { |
| 80 |
if ( did_action( 'after_setup_theme' ) ) { |
| 81 |
static $taxonomies = null; |
| 82 |
} |
| 83 |
|
| 84 |
if ( empty( $taxonomies ) ) { |
| 85 |
$taxonomies = array( 'post_format' => 'post_format' ); |
| 86 |
|
| 87 |
/** |
| 88 |
* Filters the list of taxonomies not translatable but filtered by language. |
| 89 |
* Includes only the post format by default |
| 90 |
* The filter must be added soon in the WordPress loading process: |
| 91 |
* in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes. |
| 92 |
* |
| 93 |
* @since 1.7 |
| 94 |
* |
| 95 |
* @param string[] $taxonomies List of taxonomy names. |
| 96 |
* @param bool $is_settings True when displaying the list of custom taxonomies in Polylang settings. |
| 97 |
*/ |
| 98 |
$taxonomies = apply_filters( 'pll_filtered_taxonomies', $taxonomies, false ); |
| 99 |
} |
| 100 |
|
| 101 |
return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns true if Polylang filters this taxonomy per language. |
| 106 |
* |
| 107 |
* @since 1.7 |
| 108 |
* @since 3.7 Moved from `PLL_Model::is_filtered_taxonomy()` to `WP_Syntex\Polylang\Model\Taxonomies::is_filtered()`. |
| 109 |
* |
| 110 |
* @param string|string[] $tax Taxonomy name or array of taxonomy names. |
| 111 |
* @return bool |
| 112 |
*/ |
| 113 |
public function is_filtered( $tax ): bool { |
| 114 |
$taxonomies = $this->get_filtered( false ); |
| 115 |
return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) ) || in_array( $tax, $taxonomies ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns the query vars of all filtered taxonomies. |
| 120 |
* |
| 121 |
* @since 1.7 |
| 122 |
* @since 3.7 Moved from `PLL_Model::get_filtered_taxonomies_query_vars()` to `WP_Syntex\Polylang\Model\Taxonomies::get_filtered_query_vars()`. |
| 123 |
* |
| 124 |
* @return string[] |
| 125 |
*/ |
| 126 |
public function get_filtered_query_vars(): array { |
| 127 |
$query_vars = array(); |
| 128 |
foreach ( $this->get_filtered() as $filtered_tax ) { |
| 129 |
$tax = get_taxonomy( $filtered_tax ); |
| 130 |
if ( ! empty( $tax ) && is_string( $tax->query_var ) ) { |
| 131 |
$query_vars[] = $tax->query_var; |
| 132 |
} |
| 133 |
} |
| 134 |
return $query_vars; |
| 135 |
} |
| 136 |
} |
| 137 |
|