PluginProbe
Polylang / trunk
Polylang vtrunk
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 / translatable-objects.php

translatable-objects.php in Polylang trunk, at src/translatable-objects.php

138 lines 3.7 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 /**
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 public function getIterator(): ArrayIterator {
71 return new ArrayIterator( $this->objects );
72 }
73
74 /**
75 * Returns a translatable object, given an object type.
76 *
77 * @since 3.4
78 *
79 * @param string $object_type The object type.
80 * @return PLL_Translatable_Object|null
81 *
82 * @phpstan-return (
83 * $object_type is 'post' ? TranslatedObjectWithTypes : (
84 * $object_type is 'term' ? TranslatedObjectWithTypes : (
85 * TranslatedObjectWithTypes|TranslatableObjectWithTypes|PLL_Translated_Object|PLL_Translatable_Object|null
86 * )
87 * )
88 * )
89 */
90 public function get( $object_type ) {
91 if ( ! isset( $this->objects[ $object_type ] ) ) {
92 return null;
93 }
94
95 return $this->objects[ $object_type ];
96 }
97
98 /**
99 * Returns all translatable objects except post one.
100 *
101 * @since 3.4
102 *
103 * @return PLL_Translatable_Object[] An array of secondary translatable objects. Array keys are the type of translated content (post, term, etc).
104 *
105 * @phpstan-return array<non-empty-string, PLL_Translatable_Object>
106 */
107 public function get_secondary_translatable_objects() {
108 return array_diff_key( $this->objects, array( $this->main_type => null ) );
109 }
110
111 /**
112 * Returns taxonomy names to manage language and translations.
113 *
114 * @since 3.4
115 *
116 * @param string[] $filter An array on value to filter taxonomy names to return.
117 * @return string[] Taxonomy names.
118 *
119 * @phpstan-param array<'language'|'translations'> $filter
120 * @phpstan-return list<non-empty-string>
121 */
122 public function get_taxonomy_names( $filter = array( 'language', 'translations' ) ) {
123 $taxonomies = array();
124
125 foreach ( $this->objects as $object ) {
126 if ( in_array( 'language', $filter, true ) ) {
127 $taxonomies[] = $object->get_tax_language();
128 }
129
130 if ( in_array( 'translations', $filter, true ) && $object instanceof PLL_Translated_Object ) {
131 $taxonomies[] = $object->get_tax_translations();
132 }
133 }
134
135 return $taxonomies;
136 }
137 }
138