PluginProbe
Polylang / 3.4
Polylang v3.4
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 / include / translatable-objects.php

translatable-objects.php in Polylang 3.4, at include/translatable-objects.php

131 lines 3.5 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 public function register( PLL_Translatable_Object $object ) {
42 if ( empty( $this->main_type ) ) {
43 $this->main_type = $object->get_type();
44 }
45
46 if ( ! isset( $this->objects[ $object->get_type() ] ) ) {
47 $this->objects[ $object->get_type() ] = $object;
48 }
49
50 return $this->objects[ $object->get_type() ];
51 }
52
53 /**
54 * Returns all registered translatable objects.
55 *
56 * @since 3.4
57 *
58 * @return ArrayIterator Iterator on $objects array property. Keys are the type of translated content (post, term, etc).
59 *
60 * @phpstan-return ArrayIterator<string, PLL_Translatable_Object>
61 */
62 #[\ReturnTypeWillChange]
63 public function getIterator() {
64 return new ArrayIterator( $this->objects );
65 }
66
67 /**
68 * Returns a translatable object, given an object type.
69 *
70 * @since 3.4
71 *
72 * @param string $object_type The object type.
73 * @return PLL_Translatable_Object|null
74 *
75 * @phpstan-return (
76 * $object_type is 'post' ? TranslatedObjectWithTypes : (
77 * $object_type is 'term' ? TranslatedObjectWithTypes : (
78 * TranslatedObjectWithTypes|TranslatableObjectWithTypes|PLL_Translated_Object|PLL_Translatable_Object|null
79 * )
80 * )
81 * )
82 */
83 public function get( $object_type ) {
84 if ( ! isset( $this->objects[ $object_type ] ) ) {
85 return null;
86 }
87
88 return $this->objects[ $object_type ];
89 }
90
91 /**
92 * Returns all translatable objects except post one.
93 *
94 * @since 3.4
95 *
96 * @return PLL_Translatable_Object[] An array of secondary translatable objects. Array keys are the type of translated content (post, term, etc).
97 *
98 * @phpstan-return array<non-empty-string, PLL_Translatable_Object>
99 */
100 public function get_secondary_translatable_objects() {
101 return array_diff_key( $this->objects, array( $this->main_type => null ) );
102 }
103
104 /**
105 * Returns taxonomy names to manage language and translations.
106 *
107 * @since 3.4
108 *
109 * @param string[] $filter An array on value to filter taxonomy names to return.
110 * @return string[] Taxonomy names.
111 *
112 * @phpstan-param array<'language'|'translations'> $filter
113 * @phpstan-return list<non-empty-string>
114 */
115 public function get_taxonomy_names( $filter = array( 'language', 'translations' ) ) {
116 $taxonomies = array();
117
118 foreach ( $this->objects as $object ) {
119 if ( in_array( 'language', $filter, true ) ) {
120 $taxonomies[] = $object->get_tax_language();
121 }
122
123 if ( in_array( 'translations', $filter, true ) && $object instanceof PLL_Translated_Object ) {
124 $taxonomies[] = $object->get_tax_translations();
125 }
126 }
127
128 return $taxonomies;
129 }
130 }
131