PluginProbe
Polylang / 3.8.6
Polylang v3.8.6
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 / language-deprecated.php

language-deprecated.php in Polylang 3.8.6, at src/language-deprecated.php

256 lines 7.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 /**
7 * Holds everything related to deprecated properties of `PLL_Language`.
8 *
9 * @since 3.4
10 */
11 abstract class PLL_Language_Deprecated {
12
13 /**
14 * List of deprecated term properties and related arguments to use with `get_tax_prop()`.
15 *
16 * @private
17 *
18 * @var string[][]
19 */
20 public const DEPRECATED_TERM_PROPERTIES = array(
21 'term_taxonomy_id' => array( 'language', 'term_taxonomy_id' ),
22 'count' => array( 'language', 'count' ),
23 'tl_term_id' => array( 'term_language', 'term_id' ),
24 'tl_term_taxonomy_id' => array( 'term_language', 'term_taxonomy_id' ),
25 'tl_count' => array( 'term_language', 'count' ),
26 );
27
28 /**
29 * List of deprecated URL properties and related getter to use.
30 *
31 * @private
32 *
33 * @var string[]
34 */
35 public const DEPRECATED_URL_PROPERTIES = array(
36 'home_url' => 'get_home_url',
37 'search_url' => 'get_search_url',
38 );
39
40 /**
41 * Returns a language term property value (term ID, term taxonomy ID, or count).
42 *
43 * @since 3.4
44 *
45 * @param string $taxonomy_name Name of the taxonomy.
46 * @param string $prop_name Name of the property: 'term_taxonomy_id', 'term_id', 'count'.
47 * @return int
48 *
49 * @phpstan-param non-empty-string $taxonomy_name
50 * @phpstan-param 'term_taxonomy_id'|'term_id'|'count' $prop_name
51 * @phpstan-return int<0, max>
52 */
53 abstract public function get_tax_prop( $taxonomy_name, $prop_name );
54
55 /**
56 * Returns language's home URL. Takes care to render it dynamically if no cache is allowed.
57 *
58 * @since 3.4
59 *
60 * @return string Language home URL.
61 *
62 * @phpstan-return non-empty-string
63 */
64 abstract public function get_home_url();
65
66 /**
67 * Returns language's search URL. Takes care to render it dynamically if no cache is allowed.
68 *
69 * @since 3.4
70 *
71 * @return string Language search URL.
72 *
73 * @phpstan-return non-empty-string
74 */
75 abstract public function get_search_url();
76
77 /**
78 * Throws a depreciation notice if someone tries to get one of the following properties:
79 * `term_taxonomy_id`, `count`, `tl_term_id`, `tl_term_taxonomy_id` or `tl_count`.
80 *
81 * Backward compatibility with Polylang < 3.4.
82 *
83 * @since 3.4
84 *
85 * @param string $property Property to get.
86 * @return mixed Required property value.
87 */
88 public function __get( $property ) {
89 // Deprecated property.
90 if ( $this->is_deprecated_term_property( $property ) ) {
91 $this->deprecated_property(
92 $property,
93 sprintf(
94 "get_tax_prop( '%s', '%s' )",
95 self::DEPRECATED_TERM_PROPERTIES[ $property ][0],
96 self::DEPRECATED_TERM_PROPERTIES[ $property ][1]
97 )
98 );
99
100 return $this->get_deprecated_term_property( $property );
101 }
102
103 if ( $this->is_deprecated_url_property( $property ) ) {
104 $this->deprecated_property( $property, "get_{$property}()" );
105
106 return $this->get_deprecated_url_property( $property );
107 }
108
109 // Undefined property.
110 if ( ! property_exists( $this, $property ) ) {
111 return null;
112 }
113
114 // The property is defined.
115 $ref = new ReflectionProperty( $this, $property );
116
117 // Public property.
118 if ( $ref->isPublic() ) {
119 return $this->{$property};
120 }
121
122 // Protected or private property.
123 $visibility = $ref->isPrivate() ? 'private' : 'protected';
124 $trace = debug_backtrace(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection, WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
125 $file = $trace[0]['file'] ?? '';
126 $line = $trace[0]['line'] ?? 0;
127 trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
128 esc_html(
129 sprintf(
130 "Cannot access %s property %s::$%s in %s on line %d.\nError handler",
131 $visibility,
132 get_class( $this ),
133 $property,
134 $file,
135 $line
136 )
137 )
138 );
139 }
140
141 /**
142 * Checks for a deprecated property.
143 * Is triggered by calling `isset()` or `empty()` on inaccessible (protected or private) or non-existing properties.
144 *
145 * Backward compatibility with Polylang < 3.4.
146 *
147 * @since 3.4
148 *
149 * @param string $property A property name.
150 * @return bool
151 */
152 public function __isset( $property ) {
153 return $this->is_deprecated_term_property( $property ) || $this->is_deprecated_url_property( $property );
154 }
155
156 /**
157 * Tells if the given term property is deprecated.
158 *
159 * @since 3.4
160 * @see PLL_Language::DEPRECATED_TERM_PROPERTIES for the list of deprecated properties.
161 *
162 * @param string $property A property name.
163 * @return bool
164 *
165 * @phpstan-assert-if-true key-of<PLL_Language::DEPRECATED_TERM_PROPERTIES> $property
166 */
167 protected function is_deprecated_term_property( $property ) {
168 return array_key_exists( $property, self::DEPRECATED_TERM_PROPERTIES );
169 }
170
171 /**
172 * Returns a deprecated term property's value.
173 *
174 * @since 3.4
175 * @see PLL_Language::DEPRECATED_TERM_PROPERTIES for the list of deprecated properties.
176 *
177 * @param string $property A property name.
178 * @return int
179 *
180 * @phpstan-param key-of<PLL_Language::DEPRECATED_TERM_PROPERTIES> $property
181 * @phpstan-return int<0, max>
182 */
183 protected function get_deprecated_term_property( $property ) {
184 return $this->get_tax_prop(
185 self::DEPRECATED_TERM_PROPERTIES[ $property ][0],
186 self::DEPRECATED_TERM_PROPERTIES[ $property ][1]
187 );
188 }
189
190 /**
191 * Tells if the given URL property is deprecated.
192 *
193 * @since 3.4
194 * @see PLL_Language::DEPRECATED_URL_PROPERTIES for the list of deprecated properties.
195 *
196 * @param string $property A property name.
197 * @return bool
198 *
199 * @phpstan-assert-if-true key-of<PLL_Language::DEPRECATED_URL_PROPERTIES> $property
200 */
201 protected function is_deprecated_url_property( $property ) {
202 return array_key_exists( $property, self::DEPRECATED_URL_PROPERTIES );
203 }
204
205 /**
206 * Returns a deprecated URL property's value.
207 *
208 * @since 3.4
209 * @see PLL_Language::DEPRECATED_URL_PROPERTIES for the list of deprecated properties.
210 *
211 * @param string $property A property name.
212 * @return string
213 *
214 * @phpstan-param key-of<PLL_Language::DEPRECATED_URL_PROPERTIES> $property
215 * @phpstan-return non-empty-string
216 */
217 protected function get_deprecated_url_property( $property ) {
218 return $this->{self::DEPRECATED_URL_PROPERTIES[ $property ]}();
219 }
220
221 /**
222 * Triggers a deprecated an error for a deprecated property.
223 *
224 * @since 3.4
225 *
226 * @param string $property Deprecated property name.
227 * @param string $replacement Method or property name to use instead.
228 * @return void
229 */
230 private function deprecated_property( $property, $replacement ) {
231 /**
232 * Filters whether to trigger an error for deprecated properties.
233 *
234 * The filter name is intentionally not prefixed to use the same as WordPress
235 * in case it is added in the future.
236 *
237 * @since 3.4
238 *
239 * @param bool $trigger Whether to trigger the error for deprecated properties. Default true.
240 */
241 if ( ! WP_DEBUG || ! apply_filters( 'deprecated_property_trigger_error', true ) ) {
242 return;
243 }
244
245 trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
246 sprintf(
247 "Class property %1\$s::\$%2\$s is deprecated, use %1\$s::%3\$s instead.\nError handler",
248 esc_html( get_class( $this ) ),
249 esc_html( $property ),
250 esc_html( $replacement )
251 ),
252 E_USER_DEPRECATED
253 );
254 }
255 }
256