| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Registry for all translatable objects. |
| 8 |
* |
| 9 |
* @since 3.4 |
| 10 |
* |
| 11 |
* @phpstan-implements IteratorAggregate<non-empty-string, PLL_Translatable_Object> |
| 12 |
* @phpstan-type TranslatedObjectWithTypes PLL_Translated_Object&PLL_Translatable_Object_With_Types_Interface |
| 13 |
* @phpstan-type TranslatableObjectWithTypes PLL_Translatable_Object&PLL_Translatable_Object_With_Types_Interface |
| 14 |
*/ |
| 15 |
class PLL_Translatable_Objects implements IteratorAggregate { |
| 16 |
|
| 17 |
/** |
| 18 |
* Type of the main translatable object. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
private $main_type = ''; |
| 23 |
|
| 24 |
/** |
| 25 |
* List of registered objects. |
| 26 |
* |
| 27 |
* @var PLL_Translatable_Object[] Array keys are the type of translated content (post, term, etc). |
| 28 |
* |
| 29 |
* @phpstan-var array<non-empty-string, PLL_Translatable_Object> |
| 30 |
*/ |
| 31 |
private $objects = array(); |
| 32 |
|
| 33 |
/** |
| 34 |
* Registers a translatable object. |
| 35 |
* |
| 36 |
* @since 3.4 |
| 37 |
* |
| 38 |
* @param PLL_Translatable_Object $object The translatable object to register. |
| 39 |
* @return PLL_Translatable_Object |
| 40 |
* |
| 41 |
* @phpstan-return ( |
| 42 |
* $object is PLL_Translated_Post ? PLL_Translated_Post : ( |
| 43 |
* $object is PLL_Translated_Term ? PLL_Translated_Term : ( |
| 44 |
* PLL_Translatable_Object |
| 45 |
* ) |
| 46 |
* ) |
| 47 |
* ) |
| 48 |
*/ |
| 49 |
public function register( PLL_Translatable_Object $object ) { |
| 50 |
if ( empty( $this->main_type ) ) { |
| 51 |
$this->main_type = $object->get_type(); |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! isset( $this->objects[ $object->get_type() ] ) ) { |
| 55 |
$this->objects[ $object->get_type() ] = $object; |
| 56 |
} |
| 57 |
|
| 58 |
return $this->objects[ $object->get_type() ]; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns all registered translatable objects. |
| 63 |
* |
| 64 |
* @since 3.4 |
| 65 |
* |
| 66 |
* @return ArrayIterator Iterator on $objects array property. Keys are the type of translated content (post, term, etc). |
| 67 |
* |
| 68 |
* @phpstan-return ArrayIterator<string, PLL_Translatable_Object> |
| 69 |
*/ |
| 70 |
#[\ReturnTypeWillChange] |
| 71 |
public function getIterator() { |
| 72 |
return new ArrayIterator( $this->objects ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns a translatable object, given an object type. |
| 77 |
* |
| 78 |
* @since 3.4 |
| 79 |
* |
| 80 |
* @param string $object_type The object type. |
| 81 |
* @return PLL_Translatable_Object|null |
| 82 |
* |
| 83 |
* @phpstan-return ( |
| 84 |
* $object_type is 'post' ? TranslatedObjectWithTypes : ( |
| 85 |
* $object_type is 'term' ? TranslatedObjectWithTypes : ( |
| 86 |
* TranslatedObjectWithTypes|TranslatableObjectWithTypes|PLL_Translated_Object|PLL_Translatable_Object|null |
| 87 |
* ) |
| 88 |
* ) |
| 89 |
* ) |
| 90 |
*/ |
| 91 |
public function get( $object_type ) { |
| 92 |
if ( ! isset( $this->objects[ $object_type ] ) ) { |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
return $this->objects[ $object_type ]; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns all translatable objects except post one. |
| 101 |
* |
| 102 |
* @since 3.4 |
| 103 |
* |
| 104 |
* @return PLL_Translatable_Object[] An array of secondary translatable objects. Array keys are the type of translated content (post, term, etc). |
| 105 |
* |
| 106 |
* @phpstan-return array<non-empty-string, PLL_Translatable_Object> |
| 107 |
*/ |
| 108 |
public function get_secondary_translatable_objects() { |
| 109 |
return array_diff_key( $this->objects, array( $this->main_type => null ) ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Returns taxonomy names to manage language and translations. |
| 114 |
* |
| 115 |
* @since 3.4 |
| 116 |
* |
| 117 |
* @param string[] $filter An array on value to filter taxonomy names to return. |
| 118 |
* @return string[] Taxonomy names. |
| 119 |
* |
| 120 |
* @phpstan-param array<'language'|'translations'> $filter |
| 121 |
* @phpstan-return list<non-empty-string> |
| 122 |
*/ |
| 123 |
public function get_taxonomy_names( $filter = array( 'language', 'translations' ) ) { |
| 124 |
$taxonomies = array(); |
| 125 |
|
| 126 |
foreach ( $this->objects as $object ) { |
| 127 |
if ( in_array( 'language', $filter, true ) ) { |
| 128 |
$taxonomies[] = $object->get_tax_language(); |
| 129 |
} |
| 130 |
|
| 131 |
if ( in_array( 'translations', $filter, true ) && $object instanceof PLL_Translated_Object ) { |
| 132 |
$taxonomies[] = $object->get_tax_translations(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return $taxonomies; |
| 137 |
} |
| 138 |
} |
| 139 |
|