We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 112 |
Total Lines | 1037 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
Complex classes like Settings often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Settings, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class Settings implements \ArrayAccess, \JsonSerializable { |
||
42 | |||
43 | /** |
||
44 | * The current no-break narrow space character. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $no_break_narrow_space; |
||
49 | |||
50 | /** |
||
51 | * Primary quote style. |
||
52 | * |
||
53 | * @var Quotes |
||
54 | */ |
||
55 | protected $primary_quote_style; |
||
56 | |||
57 | /** |
||
58 | * Secondary quote style. |
||
59 | * |
||
60 | * @var Quotes |
||
61 | */ |
||
62 | protected $secondary_quote_style; |
||
63 | |||
64 | /** |
||
65 | * A regex pattern for custom units (or the empty string). |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $custom_units = ''; |
||
70 | |||
71 | /** |
||
72 | * A hashmap of settings for the various typographic options. |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $data = []; |
||
77 | |||
78 | /** |
||
79 | * The current dash style. |
||
80 | * |
||
81 | * @var Settings\Dashes |
||
82 | */ |
||
83 | protected $dash_style; |
||
84 | |||
85 | /** |
||
86 | * Sets up a new Settings object. |
||
87 | * |
||
88 | * @since 6.0.0 If $set_defaults is `false`, the settings object is not fully |
||
89 | * initialized unless `set_smart_quotes_primary`, |
||
90 | * `set_smart_quotes_secondary`, `set_smart_dashes_style` and |
||
91 | * `set_true_no_break_narrow_space` are called explicitly. |
||
92 | * |
||
93 | * @param bool $set_defaults If true, set default values for various properties. Defaults to true. |
||
94 | */ |
||
95 | 1 | public function __construct( $set_defaults = true ) { |
|
96 | 1 | if ( $set_defaults ) { |
|
97 | 1 | $this->set_defaults(); |
|
98 | } |
||
99 | 1 | } |
|
100 | |||
101 | /** |
||
102 | * Provides access to named settings (object syntax). |
||
103 | * |
||
104 | * @param string $key The settings key. |
||
105 | * |
||
106 | * @return mixed |
||
107 | */ |
||
108 | 1 | public function &__get( $key ) { |
|
109 | 1 | return $this->data[ $key ]; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * Changes a named setting (object syntax). |
||
114 | * |
||
115 | * @param string $key The settings key. |
||
116 | * @param mixed $value The settings value. |
||
117 | */ |
||
118 | 1 | public function __set( $key, $value ) { |
|
119 | 1 | $this->data[ $key ] = $value; |
|
120 | 1 | } |
|
121 | |||
122 | /** |
||
123 | * Checks if a named setting exists (object syntax). |
||
124 | * |
||
125 | * @param string $key The settings key. |
||
126 | */ |
||
127 | 1 | public function __isset( $key ) { |
|
128 | 1 | return isset( $this->data[ $key ] ); |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Unsets a named setting. |
||
133 | * |
||
134 | * @param string $key The settings key. |
||
135 | */ |
||
136 | 1 | public function __unset( $key ) { |
|
137 | 1 | unset( $this->data[ $key ] ); |
|
138 | 1 | } |
|
139 | |||
140 | /** |
||
141 | * Changes a named setting (array syntax). |
||
142 | * |
||
143 | * @param string $offset The settings key. |
||
144 | * @param mixed $value The settings value. |
||
145 | */ |
||
146 | 1 | public function offsetSet( $offset, $value ) { |
|
147 | 1 | if ( ! empty( $offset ) ) { |
|
148 | 1 | $this->data[ $offset ] = $value; |
|
149 | } |
||
150 | 1 | } |
|
151 | |||
152 | /** |
||
153 | * Checks if a named setting exists (array syntax). |
||
154 | * |
||
155 | * @param string $offset The settings key. |
||
156 | */ |
||
157 | 1 | public function offsetExists( $offset ) { |
|
158 | 1 | return isset( $this->data[ $offset ] ); |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Unsets a named setting (array syntax). |
||
163 | * |
||
164 | * @param string $offset The settings key. |
||
165 | */ |
||
166 | 1 | public function offsetUnset( $offset ) { |
|
167 | 1 | unset( $this->data[ $offset ] ); |
|
168 | 1 | } |
|
169 | |||
170 | /** |
||
171 | * Provides access to named settings (array syntax). |
||
172 | * |
||
173 | * @param string $offset The settings key. |
||
174 | * |
||
175 | * @return mixed |
||
176 | */ |
||
177 | 1 | public function offsetGet( $offset ) { |
|
178 | 1 | return isset( $this->data[ $offset ] ) ? $this->data[ $offset ] : null; |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * Provides a JSON serialization of the settings. |
||
183 | * |
||
184 | * @return mixed |
||
185 | */ |
||
186 | 1 | public function jsonSerialize() { |
|
187 | 1 | return \array_merge( |
|
188 | 1 | $this->data, |
|
189 | [ |
||
190 | 1 | 'no_break_narrow_space' => $this->no_break_narrow_space, |
|
191 | 1 | 'primary_quotes' => "{$this->primary_quote_style->open()}|{$this->primary_quote_style->close()}", |
|
192 | 1 | 'secondary_quotes' => "{$this->secondary_quote_style->open()}|{$this->secondary_quote_style->close()}", |
|
193 | 1 | 'dash_style' => "{$this->dash_style->interval_dash()}|{$this->dash_style->interval_space()}|{$this->dash_style->parenthetical_dash()}|{$this->dash_style->parenthetical_space()}", |
|
194 | 1 | 'custom_units' => $this->custom_units, |
|
195 | ] |
||
196 | ); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Retrieves the current non-breaking narrow space character (either the |
||
201 | * regular non-breaking space or the the true non-breaking narrow space  ). |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | 1 | public function no_break_narrow_space() { |
|
206 | 1 | return $this->no_break_narrow_space; |
|
207 | } |
||
208 | |||
209 | /** |
||
210 | * Retrieves the primary (double) quote style. |
||
211 | * |
||
212 | * @return Quotes |
||
213 | */ |
||
214 | 1 | public function primary_quote_style() { |
|
215 | 1 | return $this->primary_quote_style; |
|
216 | } |
||
217 | |||
218 | /** |
||
219 | * Retrieves the secondary (single) quote style. |
||
220 | * |
||
221 | * @return Quotes |
||
222 | */ |
||
223 | 1 | public function secondary_quote_style() { |
|
224 | 1 | return $this->secondary_quote_style; |
|
225 | } |
||
226 | |||
227 | /** |
||
228 | * Retrieves the dash style. |
||
229 | * |
||
230 | * @return Settings\Dashes |
||
231 | */ |
||
232 | 1 | public function dash_style() { |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * Retrieves the custom units pattern. |
||
238 | * |
||
239 | * @return string The pattern is suitable for inclusion into a regular expression. |
||
240 | */ |
||
241 | 1 | public function custom_units() { |
|
242 | 1 | return $this->custom_units; |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * (Re)set various options to their default values. |
||
247 | */ |
||
248 | 1 | public function set_defaults() { |
|
249 | // General attributes. |
||
250 | 1 | $this->set_tags_to_ignore(); |
|
251 | 1 | $this->set_classes_to_ignore(); |
|
252 | 1 | $this->set_ids_to_ignore(); |
|
253 | |||
254 | // Smart characters. |
||
255 | 1 | $this->set_smart_quotes(); |
|
256 | 1 | $this->set_smart_quotes_primary(); |
|
257 | 1 | $this->set_smart_quotes_secondary(); |
|
258 | 1 | $this->set_smart_quotes_exceptions(); |
|
259 | 1 | $this->set_smart_dashes(); |
|
260 | 1 | $this->set_smart_dashes_style(); |
|
261 | 1 | $this->set_smart_ellipses(); |
|
262 | 1 | $this->set_smart_diacritics(); |
|
263 | 1 | $this->set_diacritic_language(); |
|
264 | 1 | $this->set_diacritic_custom_replacements(); |
|
265 | 1 | $this->set_smart_marks(); |
|
266 | 1 | $this->set_smart_ordinal_suffix(); |
|
267 | 1 | $this->set_smart_math(); |
|
268 | 1 | $this->set_smart_fractions(); |
|
269 | 1 | $this->set_smart_exponents(); |
|
270 | |||
271 | // Smart spacing. |
||
272 | 1 | $this->set_single_character_word_spacing(); |
|
273 | 1 | $this->set_fraction_spacing(); |
|
274 | 1 | $this->set_unit_spacing(); |
|
275 | 1 | $this->set_french_punctuation_spacing(); |
|
276 | 1 | $this->set_units(); |
|
277 | 1 | $this->set_dash_spacing(); |
|
278 | 1 | $this->set_dewidow(); |
|
279 | 1 | $this->set_max_dewidow_length(); |
|
280 | 1 | $this->set_max_dewidow_pull(); |
|
281 | 1 | $this->set_dewidow_word_number(); |
|
282 | 1 | $this->set_wrap_hard_hyphens(); |
|
283 | 1 | $this->set_url_wrap(); |
|
284 | 1 | $this->set_email_wrap(); |
|
285 | 1 | $this->set_min_after_url_wrap(); |
|
286 | 1 | $this->set_space_collapse(); |
|
287 | 1 | $this->set_true_no_break_narrow_space(); |
|
288 | |||
289 | // Character styling. |
||
290 | 1 | $this->set_style_ampersands(); |
|
291 | 1 | $this->set_style_caps(); |
|
292 | 1 | $this->set_style_initial_quotes(); |
|
293 | 1 | $this->set_style_numbers(); |
|
294 | 1 | $this->set_style_hanging_punctuation(); |
|
295 | 1 | $this->set_initial_quote_tags(); |
|
296 | |||
297 | // Hyphenation. |
||
298 | 1 | $this->set_hyphenation(); |
|
299 | 1 | $this->set_hyphenation_language(); |
|
300 | 1 | $this->set_min_length_hyphenation(); |
|
301 | 1 | $this->set_min_before_hyphenation(); |
|
302 | 1 | $this->set_min_after_hyphenation(); |
|
303 | 1 | $this->set_hyphenate_headings(); |
|
304 | 1 | $this->set_hyphenate_all_caps(); |
|
305 | 1 | $this->set_hyphenate_title_case(); |
|
306 | 1 | $this->set_hyphenate_compounds(); |
|
307 | 1 | $this->set_hyphenation_exceptions(); |
|
308 | |||
309 | // Parser error handling. |
||
310 | 1 | $this->set_ignore_parser_errors(); |
|
311 | 1 | } |
|
312 | |||
313 | /** |
||
314 | * Enable lenient parser error handling (HTML is "best guess" if enabled). |
||
315 | * |
||
316 | * @param bool $on Optional. Default false. |
||
317 | */ |
||
318 | 1 | public function set_ignore_parser_errors( $on = false ) { |
|
319 | 1 | $this->data['parserErrorsIgnore'] = $on; |
|
320 | 1 | } |
|
321 | |||
322 | /** |
||
323 | * Sets an optional handler for parser errors. Invalid callbacks will be silently ignored. |
||
324 | * |
||
325 | * @since 6.0.0. callable type is enforced via typehinting. |
||
326 | * |
||
327 | * @param callable|null $handler Optional. A callable that takes an array of error strings as its parameter. Default null. |
||
328 | */ |
||
329 | 2 | public function set_parser_errors_handler( callable $handler = null ) { |
|
330 | 2 | $this->data['parserErrorsHandler'] = $handler; |
|
331 | 2 | } |
|
332 | |||
333 | /** |
||
334 | * Enable usage of true "no-break narrow space" ( ) instead of the normal no-break space ( ). |
||
335 | * |
||
336 | * @param bool $on Optional. Default false. |
||
337 | */ |
||
338 | 1 | public function set_true_no_break_narrow_space( $on = false ) { |
|
339 | |||
340 | 1 | if ( $on ) { |
|
341 | 1 | $this->no_break_narrow_space = U::NO_BREAK_NARROW_SPACE; |
|
342 | } else { |
||
343 | 1 | $this->no_break_narrow_space = U::NO_BREAK_SPACE; |
|
344 | } |
||
345 | 1 | } |
|
346 | |||
347 | /** |
||
348 | * Sets tags for which the typography of their children will be left untouched. |
||
349 | * |
||
350 | * @param string|array $tags A comma separated list or an array of tag names. |
||
351 | */ |
||
352 | 1 | public function set_tags_to_ignore( $tags = [ 'code', 'head', 'kbd', 'object', 'option', 'pre', 'samp', 'script', 'noscript', 'noembed', 'select', 'style', 'textarea', 'title', 'var', 'math' ] ) { |
|
353 | // Ensure that we pass only lower-case tag names to XPath. |
||
354 | 1 | $tags = array_filter( array_map( 'strtolower', Strings::maybe_split_parameters( $tags ) ), 'ctype_alnum' ); |
|
355 | |||
356 | 1 | $this->data['ignoreTags'] = array_unique( array_merge( $tags, array_flip( DOM::inappropriate_tags() ) ) ); |
|
357 | 1 | } |
|
358 | |||
359 | /** |
||
360 | * Sets classes for which the typography of their children will be left untouched. |
||
361 | * |
||
362 | * @param string|array $classes A comma separated list or an array of class names. |
||
363 | */ |
||
364 | 1 | public function set_classes_to_ignore( $classes = [ 'vcard', 'noTypo' ] ) { |
|
365 | 1 | $this->data['ignoreClasses'] = Strings::maybe_split_parameters( $classes ); |
|
366 | 1 | } |
|
367 | |||
368 | /** |
||
369 | * Sets IDs for which the typography of their children will be left untouched. |
||
370 | * |
||
371 | * @param string|array $ids A comma separated list or an array of tag names. |
||
372 | */ |
||
373 | 1 | public function set_ids_to_ignore( $ids = [] ) { |
|
374 | 1 | $this->data['ignoreIDs'] = Strings::maybe_split_parameters( $ids ); |
|
375 | 1 | } |
|
376 | |||
377 | /** |
||
378 | * Enables/disables typographic quotes. |
||
379 | * |
||
380 | * @param bool $on Optional. Default true. |
||
381 | */ |
||
382 | 1 | public function set_smart_quotes( $on = true ) { |
|
383 | 1 | $this->data['smartQuotes'] = $on; |
|
384 | 1 | } |
|
385 | |||
386 | /** |
||
387 | * Sets the style for primary ('double') quotemarks. |
||
388 | * |
||
389 | * Allowed values for $style: |
||
390 | * "doubleCurled" => "“foo”", |
||
391 | * "doubleCurledReversed" => "”foo”", |
||
392 | * "doubleLow9" => "„foo”", |
||
393 | * "doubleLow9Reversed" => "„foo“", |
||
394 | * "singleCurled" => "‘foo’", |
||
395 | * "singleCurledReversed" => "’foo’", |
||
396 | * "singleLow9" => "‚foo’", |
||
397 | * "singleLow9Reversed" => "‚foo‘", |
||
398 | * "doubleGuillemetsFrench" => "« foo »", |
||
399 | * "doubleGuillemets" => "«foo»", |
||
400 | * "doubleGuillemetsReversed" => "»foo«", |
||
401 | * "singleGuillemets" => "‹foo›", |
||
402 | * "singleGuillemetsReversed" => "›foo‹", |
||
403 | * "cornerBrackets" => "「foo」", |
||
404 | * "whiteCornerBracket" => "『foo』" |
||
405 | * |
||
406 | * @param Quotes|string $style Optional. A Quotes instance or a quote style constant. Defaults to 'doubleCurled'. |
||
407 | * |
||
408 | * @throws \DomainException Thrown if $style constant is invalid. |
||
409 | */ |
||
410 | 3 | public function set_smart_quotes_primary( $style = Quote_Style::DOUBLE_CURLED ) { |
|
411 | 3 | $this->primary_quote_style = $this->get_quote_style( $style ); |
|
412 | 2 | } |
|
413 | |||
414 | /** |
||
415 | * Sets the style for secondary ('single') quotemarks. |
||
416 | * |
||
417 | * Allowed values for $style: |
||
418 | * "doubleCurled" => "“foo”", |
||
419 | * "doubleCurledReversed" => "”foo”", |
||
420 | * "doubleLow9" => "„foo”", |
||
421 | * "doubleLow9Reversed" => "„foo“", |
||
422 | * "singleCurled" => "‘foo’", |
||
423 | * "singleCurledReversed" => "’foo’", |
||
424 | * "singleLow9" => "‚foo’", |
||
425 | * "singleLow9Reversed" => "‚foo‘", |
||
426 | * "doubleGuillemetsFrench" => "« foo »", |
||
427 | * "doubleGuillemets" => "«foo»", |
||
428 | * "doubleGuillemetsReversed" => "»foo«", |
||
429 | * "singleGuillemets" => "‹foo›", |
||
430 | * "singleGuillemetsReversed" => "›foo‹", |
||
431 | * "cornerBrackets" => "「foo」", |
||
432 | * "whiteCornerBracket" => "『foo』" |
||
433 | * |
||
434 | * @param Quotes|string $style Optional. A Quotes instance or a quote style constant. Defaults to 'singleCurled'. |
||
435 | * |
||
436 | * @throws \DomainException Thrown if $style constant is invalid. |
||
437 | */ |
||
438 | 3 | public function set_smart_quotes_secondary( $style = Quote_Style::SINGLE_CURLED ) { |
|
439 | 3 | $this->secondary_quote_style = $this->get_quote_style( $style ); |
|
440 | 2 | } |
|
441 | |||
442 | /** |
||
443 | * Retrieves a Quotes instance from a given style. |
||
444 | * |
||
445 | * @param Quotes|string $style A Quotes instance or a quote style constant. |
||
446 | * |
||
447 | * @throws \DomainException Thrown if $style constant is invalid. |
||
448 | * |
||
449 | * @return Quotes |
||
450 | */ |
||
451 | 6 | protected function get_quote_style( $style ) { |
|
452 | 6 | return $this->get_style( $style, Quotes::class, [ Quote_Style::class, 'get_styled_quotes' ], 'quote' ); |
|
453 | } |
||
454 | |||
455 | /** |
||
456 | * Sets the list of exceptional words for smart quotes replacement. Mainly, |
||
457 | * this is used for contractions beginning with an apostrophe. |
||
458 | * |
||
459 | * @param string[] $exceptions Optional. An array of replacements indexed by the ”non-smart" form. |
||
460 | * Default a list of English words beginning with an apostrophy. |
||
461 | */ |
||
462 | 1 | public function set_smart_quotes_exceptions( $exceptions = [ |
|
463 | "'tain't" => U::APOSTROPHE . 'tain' . U::APOSTROPHE . 't', |
||
464 | "'twere" => U::APOSTROPHE . 'twere', |
||
465 | "'twas" => U::APOSTROPHE . 'twas', |
||
466 | "'tis" => U::APOSTROPHE . 'tis', |
||
467 | "'til" => U::APOSTROPHE . 'til', |
||
468 | "'bout" => U::APOSTROPHE . 'bout', |
||
469 | "'nuff" => U::APOSTROPHE . 'nuff', |
||
470 | "'round" => U::APOSTROPHE . 'round', |
||
471 | "'cause" => U::APOSTROPHE . 'cause', |
||
472 | "'splainin" => U::APOSTROPHE . 'splainin', |
||
473 | "'em'" => U::APOSTROPHE . 'em', |
||
474 | ] ) { |
||
475 | 1 | $this->data['smartQuotesExceptions'] = [ |
|
476 | 1 | 'patterns' => \array_keys( $exceptions ), |
|
477 | 1 | 'replacements' => \array_values( $exceptions ), |
|
478 | ]; |
||
479 | 1 | } |
|
480 | |||
481 | /** |
||
482 | * Retrieves an object from a given style. |
||
483 | * |
||
484 | * @param object|string $style A style object instance or a style constant. |
||
485 | * @param string $expected_class A class name. |
||
486 | * @param callable $get_style A function that returns a style object from a given style constant. |
||
487 | * @param string $description Style description for the exception message. |
||
488 | * |
||
489 | * @throws \DomainException Thrown if $style constant is invalid. |
||
490 | * |
||
491 | * @return object An instance of $expected_class. |
||
492 | */ |
||
493 | 9 | protected function get_style( $style, $expected_class, callable $get_style, $description ) { |
|
494 | 9 | if ( $style instanceof $expected_class ) { |
|
495 | 3 | $object = $style; |
|
496 | } else { |
||
497 | 6 | $object = $get_style( $style, $this ); |
|
498 | } |
||
499 | |||
500 | 9 | if ( ! \is_object( $object ) || ! $object instanceof $expected_class ) { |
|
501 | 3 | throw new \DomainException( "Invalid $description style $style." ); |
|
502 | } |
||
503 | |||
504 | 6 | return $object; |
|
505 | } |
||
506 | |||
507 | /** |
||
508 | * Enables/disables replacement of "a--a" with En Dash " -- " and "---" with Em Dash. |
||
509 | * |
||
510 | * @param bool $on Optional. Default true. |
||
511 | */ |
||
512 | 1 | public function set_smart_dashes( $on = true ) { |
|
513 | 1 | $this->data['smartDashes'] = $on; |
|
514 | 1 | } |
|
515 | |||
516 | /** |
||
517 | * Sets the typographical conventions used by smart_dashes. |
||
518 | * |
||
519 | * Allowed values for $style: |
||
520 | * - "traditionalUS" |
||
521 | * - "international" |
||
522 | * |
||
523 | * @param string|Settings\Dashes $style Optional. Default Dash_Style::TRADITIONAL_US. |
||
524 | * |
||
525 | * @throws \DomainException Thrown if $style constant is invalid. |
||
526 | */ |
||
527 | 3 | public function set_smart_dashes_style( $style = Dash_Style::TRADITIONAL_US ) { |
|
528 | 3 | $this->dash_style = $this->get_style( $style, Settings\Dashes::class, [ Dash_Style::class, 'get_styled_dashes' ], 'dash' ); |
|
529 | 2 | } |
|
530 | |||
531 | /** |
||
532 | * Enables/disables replacement of "..." with "…". |
||
533 | * |
||
534 | * @param bool $on Optional. Default true. |
||
535 | */ |
||
536 | 1 | public function set_smart_ellipses( $on = true ) { |
|
537 | 1 | $this->data['smartEllipses'] = $on; |
|
538 | 1 | } |
|
539 | |||
540 | /** |
||
541 | * Enables/disables replacement "creme brulee" with "crème brûlée". |
||
542 | * |
||
543 | * @param bool $on Optional. Default true. |
||
544 | */ |
||
545 | 1 | public function set_smart_diacritics( $on = true ) { |
|
546 | 1 | $this->data['smartDiacritics'] = $on; |
|
547 | 1 | } |
|
548 | |||
549 | /** |
||
550 | * Sets the language used for diacritics replacements. |
||
551 | * |
||
552 | * @param string $lang Has to correspond to a filename in 'diacritics'. Optional. Default 'en-US'. |
||
553 | */ |
||
554 | 1 | public function set_diacritic_language( $lang = 'en-US' ) { |
|
555 | 1 | if ( isset( $this->data['diacriticLanguage'] ) && $this->data['diacriticLanguage'] === $lang ) { |
|
556 | 1 | return; |
|
557 | } |
||
558 | |||
559 | 1 | $this->data['diacriticLanguage'] = $lang; |
|
560 | 1 | $language_file_name = \dirname( __FILE__ ) . '/diacritics/' . $lang . '.json'; |
|
561 | |||
562 | 1 | if ( \file_exists( $language_file_name ) ) { |
|
563 | 1 | $diacritics_file = \json_decode( \file_get_contents( $language_file_name ), true ); |
|
564 | 1 | $this->data['diacriticWords'] = $diacritics_file['diacritic_words']; |
|
565 | } else { |
||
566 | 1 | unset( $this->data['diacriticWords'] ); |
|
567 | } |
||
568 | |||
569 | 1 | $this->update_diacritics_replacement_arrays(); |
|
570 | 1 | } |
|
571 | |||
572 | /** |
||
573 | * Sets up custom diacritics replacements. |
||
574 | * |
||
575 | * @param string|array $custom_replacements An array formatted [needle=>replacement, needle=>replacement...], |
||
576 | * or a string formatted `"needle"=>"replacement","needle"=>"replacement",... |
||
577 | */ |
||
578 | 5 | public function set_diacritic_custom_replacements( $custom_replacements = [] ) { |
|
579 | 5 | if ( ! \is_array( $custom_replacements ) ) { |
|
580 | 2 | $custom_replacements = $this->parse_diacritics_replacement_string( $custom_replacements ); |
|
581 | } |
||
582 | |||
583 | $this->data['diacriticCustomReplacements'] = self::array_map_assoc( function( $key, $replacement ) { |
||
584 | 5 | $key = \strip_tags( \trim( $key ) ); |
|
585 | 5 | $replacement = \strip_tags( \trim( $replacement ) ); |
|
586 | |||
587 | 5 | if ( ! empty( $key ) && ! empty( $replacement ) ) { |
|
588 | 3 | return [ $key => $replacement ]; |
|
589 | } else { |
||
590 | 2 | return []; |
|
591 | } |
||
592 | 5 | }, $custom_replacements ); |
|
593 | |||
594 | 5 | $this->update_diacritics_replacement_arrays(); |
|
595 | 5 | } |
|
596 | |||
597 | /** |
||
598 | * Parses a custom diacritics replacement string into an array. |
||
599 | * |
||
600 | * @param string $custom_replacements A string formatted `"needle"=>"replacement","needle"=>"replacement",... |
||
601 | * |
||
602 | * @return array |
||
603 | */ |
||
604 | 2 | private function parse_diacritics_replacement_string( $custom_replacements ) { |
|
605 | return self::array_map_assoc( function( $key, $replacement ) { |
||
606 | // Account for single and double quotes in keys ... |
||
607 | 2 | if ( \preg_match( '/("|\')((?:(?!\1).)+)(?:\1\s*=>)/', $replacement, $match ) ) { |
|
608 | 2 | $key = $match[2]; |
|
609 | } |
||
610 | |||
611 | // ... and values. |
||
612 | 2 | if ( \preg_match( '/(?:=>\s*("|\'))((?:(?!\1).)+)(?:\1)/', $replacement, $match ) ) { |
|
613 | 2 | $replacement = $match[2]; |
|
614 | } |
||
615 | |||
616 | 2 | return [ $key => $replacement ]; |
|
617 | 2 | }, /** RE correct. @scrutinizer ignore-type */ \preg_split( '/,/', $custom_replacements, -1, PREG_SPLIT_NO_EMPTY ) ); |
|
618 | } |
||
619 | |||
620 | /** |
||
621 | * Provides an array_map implementation with control over resulting array's keys. |
||
622 | * |
||
623 | * Based on https://gist.github.com/jasand-pereza/84ecec7907f003564584. |
||
624 | * |
||
625 | * @since 6.0.0 |
||
626 | * |
||
627 | * @param callable $callback A callback function that needs to return [ $key => $value ] pairs. |
||
628 | * @param array $array The array. |
||
629 | * |
||
630 | * @return array |
||
631 | */ |
||
632 | 2 | protected static function array_map_assoc( callable $callback, array $array ) { |
|
633 | 2 | $new = []; |
|
634 | |||
635 | 2 | foreach ( $array as $k => $v ) { |
|
636 | 2 | $u = $callback( $k, $v ); |
|
637 | |||
638 | 2 | if ( ! empty( $u ) ) { |
|
639 | 2 | $new[ \key( $u ) ] = \current( $u ); |
|
640 | } |
||
641 | } |
||
642 | |||
643 | 2 | return $new; |
|
644 | } |
||
645 | |||
646 | /** |
||
647 | * Update the pattern and replacement arrays in $settings['diacriticReplacement']. |
||
648 | * |
||
649 | * Should be called whenever a new diacritics replacement language is selected or |
||
650 | * when the custom replacements are updated. |
||
651 | */ |
||
652 | 6 | private function update_diacritics_replacement_arrays() { |
|
653 | 6 | $patterns = []; |
|
654 | 6 | $replacements = []; |
|
655 | |||
656 | 6 | if ( ! empty( $this->data['diacriticCustomReplacements'] ) ) { |
|
657 | 3 | $this->parse_diacritics_rules( $this->data['diacriticCustomReplacements'], $patterns, $replacements ); |
|
658 | } |
||
659 | 6 | if ( ! empty( $this->data['diacriticWords'] ) ) { |
|
660 | 1 | $this->parse_diacritics_rules( $this->data['diacriticWords'], $patterns, $replacements ); |
|
661 | } |
||
662 | |||
663 | 6 | $this->data['diacriticReplacement'] = [ |
|
664 | 6 | 'patterns' => $patterns, |
|
665 | 6 | 'replacements' => $replacements, |
|
666 | ]; |
||
667 | 6 | } |
|
668 | |||
669 | /** |
||
670 | * Parse an array of diacritics rules. |
||
671 | * |
||
672 | * @param array $diacritics_rules The rules ( $word => $replacement ). |
||
673 | * @param array $patterns Resulting patterns. Passed by reference. |
||
674 | * @param array $replacements Resulting replacements. Passed by reference. |
||
675 | */ |
||
676 | 4 | private function parse_diacritics_rules( array $diacritics_rules, array &$patterns, array &$replacements ) { |
|
677 | |||
678 | 4 | foreach ( $diacritics_rules as $needle => $replacement ) { |
|
679 | 4 | $patterns[] = '/\b(?<!\w[' . U::NO_BREAK_SPACE . U::SOFT_HYPHEN . '])' . $needle . '\b(?![' . U::NO_BREAK_SPACE . U::SOFT_HYPHEN . ']\w)/u'; |
|
680 | 4 | $replacements[ $needle ] = $replacement; |
|
681 | } |
||
682 | 4 | } |
|
683 | |||
684 | /** |
||
685 | * Enables/disables replacement of (r) (c) (tm) (sm) (p) (R) (C) (TM) (SM) (P) with ® © ™ ℠ ℗. |
||
686 | * |
||
687 | * @param bool $on Optional. Default true. |
||
688 | */ |
||
689 | 1 | public function set_smart_marks( $on = true ) { |
|
690 | 1 | $this->data['smartMarks'] = $on; |
|
691 | 1 | } |
|
692 | |||
693 | /** |
||
694 | * Enables/disables proper mathematical symbols. |
||
695 | * |
||
696 | * @param bool $on Optional. Default true. |
||
697 | */ |
||
698 | 1 | public function set_smart_math( $on = true ) { |
|
699 | 1 | $this->data['smartMath'] = $on; |
|
700 | 1 | } |
|
701 | |||
702 | /** |
||
703 | * Enables/disables replacement of 2^2 with 2<sup>2</sup> |
||
704 | * |
||
705 | * @param bool $on Optional. Default true. |
||
706 | */ |
||
707 | 1 | public function set_smart_exponents( $on = true ) { |
|
708 | 1 | $this->data['smartExponents'] = $on; |
|
709 | 1 | } |
|
710 | |||
711 | /** |
||
712 | * Enables/disables replacement of 1/4 with <sup>1</sup>⁄<sub>4</sub>. |
||
713 | * |
||
714 | * @param bool $on Optional. Default true. |
||
715 | */ |
||
716 | 1 | public function set_smart_fractions( $on = true ) { |
|
718 | 1 | } |
|
719 | |||
720 | /** |
||
721 | * Enables/disables replacement of 1st with 1<sup>st</sup>. |
||
722 | * |
||
723 | * @param bool $on Optional. Default true. |
||
724 | */ |
||
725 | 1 | public function set_smart_ordinal_suffix( $on = true ) { |
|
726 | 1 | $this->data['smartOrdinalSuffix'] = $on; |
|
727 | 1 | } |
|
728 | |||
729 | /** |
||
730 | * Enables/disables forcing single character words to next line with the insertion of . |
||
731 | * |
||
732 | * @param bool $on Optional. Default true. |
||
733 | */ |
||
734 | 1 | public function set_single_character_word_spacing( $on = true ) { |
|
735 | 1 | $this->data['singleCharacterWordSpacing'] = $on; |
|
736 | 1 | } |
|
737 | |||
738 | /** |
||
739 | * Enables/disables fraction spacing. |
||
740 | * |
||
741 | * @param bool $on Optional. Default true. |
||
742 | */ |
||
743 | 1 | public function set_fraction_spacing( $on = true ) { |
|
745 | 1 | } |
|
746 | |||
747 | /** |
||
748 | * Enables/disables keeping units and values together with the insertion of . |
||
749 | * |
||
750 | * @param bool $on Optional. Default true. |
||
751 | */ |
||
752 | 1 | public function set_unit_spacing( $on = true ) { |
|
753 | 1 | $this->data['unitSpacing'] = $on; |
|
754 | 1 | } |
|
755 | |||
756 | /** |
||
757 | * Enables/disables numbered abbreviations like "ISO 9000" together with the insertion of . |
||
758 | * |
||
759 | * @param bool $on Optional. Default true. |
||
760 | */ |
||
761 | 1 | public function set_numbered_abbreviation_spacing( $on = true ) { |
|
762 | 1 | $this->data['numberedAbbreviationSpacing'] = $on; |
|
763 | 1 | } |
|
764 | |||
765 | /** |
||
766 | * Enables/disables extra whitespace before certain punction marks, as is the French custom. |
||
767 | * |
||
768 | * @since 6.0.0 The default value is now `false`.` |
||
769 | * |
||
770 | * @param bool $on Optional. Default false. |
||
771 | */ |
||
772 | 1 | public function set_french_punctuation_spacing( $on = false ) { |
|
773 | 1 | $this->data['frenchPunctuationSpacing'] = $on; |
|
774 | 1 | } |
|
775 | |||
776 | /** |
||
777 | * Sets the list of units to keep together with their values. |
||
778 | * |
||
779 | * @param string|array $units A comma separated list or an array of units. |
||
780 | */ |
||
781 | 1 | public function set_units( $units = [] ) { |
|
782 | 1 | $this->data['units'] = Strings::maybe_split_parameters( $units ); |
|
783 | 1 | $this->update_unit_pattern( $this->data['units'] ); |
|
784 | 1 | } |
|
785 | |||
786 | /** |
||
787 | * Update components and pattern for matching both standard and custom units. |
||
788 | * |
||
789 | * @param array $units An array of unit names. |
||
790 | */ |
||
791 | 1 | private function update_unit_pattern( array $units ) { |
|
792 | // Update components & regex pattern. |
||
793 | 1 | foreach ( $units as $index => $unit ) { |
|
794 | // Escape special chars. |
||
795 | 1 | $units[ $index ] = \preg_replace( '#([\[\\\^\$\.\|\?\*\+\(\)\{\}])#', '\\\\$1', $unit ); |
|
796 | } |
||
797 | 1 | $this->custom_units = \implode( '|', $units ); |
|
798 | 1 | $this->custom_units .= ( $this->custom_units ) ? '|' : ''; |
|
799 | 1 | } |
|
800 | |||
801 | /** |
||
802 | * Enables/disables wrapping of Em and En dashes are in thin spaces. |
||
803 | * |
||
804 | * @param bool $on Optional. Default true. |
||
805 | */ |
||
806 | 1 | public function set_dash_spacing( $on = true ) { |
|
807 | 1 | $this->data['dashSpacing'] = $on; |
|
808 | 1 | } |
|
809 | |||
810 | /** |
||
811 | * Enables/disables removal of extra whitespace characters. |
||
812 | * |
||
813 | * @param bool $on Optional. Default true. |
||
814 | */ |
||
815 | 1 | public function set_space_collapse( $on = true ) { |
|
817 | 1 | } |
|
818 | |||
819 | /** |
||
820 | * Enables/disables widow handling. |
||
821 | * |
||
822 | * @param bool $on Optional. Default true. |
||
823 | */ |
||
824 | 1 | public function set_dewidow( $on = true ) { |
|
825 | 1 | $this->data['dewidow'] = $on; |
|
826 | 1 | } |
|
827 | |||
828 | /** |
||
829 | * Sets the maximum length of widows that will be protected. |
||
830 | * |
||
831 | * @param int $length Defaults to 5. Trying to set the value to less than 2 resets the length to the default. |
||
832 | */ |
||
833 | 1 | public function set_max_dewidow_length( $length = 5 ) { |
|
834 | 1 | $length = ( $length > 1 ) ? $length : 5; |
|
835 | |||
836 | 1 | $this->data['dewidowMaxLength'] = $length; |
|
837 | 1 | } |
|
838 | |||
839 | /** |
||
840 | * Sets the maximum number of words considered for dewidowing. |
||
841 | * |
||
842 | * @param int $number Defaults to 1. Only 1, 2 and 3 are valid. |
||
843 | */ |
||
844 | 1 | public function set_dewidow_word_number( $number = 1 ) { |
|
845 | 1 | $number = ( $number > 3 || $number < 1 ) ? 1 : $number; |
|
846 | |||
847 | 1 | $this->data['dewidowWordNumber'] = $number; |
|
848 | 1 | } |
|
849 | |||
850 | /** |
||
851 | * Sets the maximum length of pulled text to keep widows company. |
||
852 | * |
||
853 | * @param int $length Defaults to 5. Trying to set the value to less than 2 resets the length to the default. |
||
854 | */ |
||
855 | 1 | public function set_max_dewidow_pull( $length = 5 ) { |
|
856 | 1 | $length = ( $length > 1 ) ? $length : 5; |
|
857 | |||
858 | 1 | $this->data['dewidowMaxPull'] = $length; |
|
859 | 1 | } |
|
860 | |||
861 | /** |
||
862 | * Enables/disables wrapping at internal hard hyphens with the insertion of a zero-width-space. |
||
863 | * |
||
864 | * @param bool $on Optional. Default true. |
||
865 | */ |
||
866 | 1 | public function set_wrap_hard_hyphens( $on = true ) { |
|
867 | 1 | $this->data['hyphenHardWrap'] = $on; |
|
868 | 1 | } |
|
869 | |||
870 | /** |
||
871 | * Enables/disables wrapping of urls. |
||
872 | * |
||
873 | * @param bool $on Optional. Default true. |
||
874 | */ |
||
875 | 1 | public function set_url_wrap( $on = true ) { |
|
876 | 1 | $this->data['urlWrap'] = $on; |
|
877 | 1 | } |
|
878 | |||
879 | /** |
||
880 | * Enables/disables wrapping of email addresses. |
||
881 | * |
||
882 | * @param bool $on Optional. Default true. |
||
883 | */ |
||
884 | 1 | public function set_email_wrap( $on = true ) { |
|
885 | 1 | $this->data['emailWrap'] = $on; |
|
886 | 1 | } |
|
887 | |||
888 | /** |
||
889 | * Sets the minimum character requirement after an URL wrapping point. |
||
890 | * |
||
891 | * @param int $length Defaults to 5. Trying to set the value to less than 1 resets the length to the default. |
||
892 | */ |
||
893 | 1 | public function set_min_after_url_wrap( $length = 5 ) { |
|
894 | 1 | $length = ( $length > 0 ) ? $length : 5; |
|
895 | |||
896 | 1 | $this->data['urlMinAfterWrap'] = $length; |
|
897 | 1 | } |
|
898 | |||
899 | /** |
||
900 | * Enables/disables wrapping of ampersands in <span class="amp">. |
||
901 | * |
||
902 | * @param bool $on Optional. Default true. |
||
903 | */ |
||
904 | 1 | public function set_style_ampersands( $on = true ) { |
|
905 | 1 | $this->data['styleAmpersands'] = $on; |
|
906 | 1 | } |
|
907 | |||
908 | /** |
||
909 | * Enables/disables wrapping caps in <span class="caps">. |
||
910 | * |
||
911 | * @param bool $on Optional. Default true. |
||
912 | */ |
||
913 | 1 | public function set_style_caps( $on = true ) { |
|
914 | 1 | $this->data['styleCaps'] = $on; |
|
915 | 1 | } |
|
916 | |||
917 | /** |
||
918 | * Enables/disables wrapping of initial quotes in <span class="quo"> or <span class="dquo">. |
||
919 | * |
||
920 | * @param bool $on Optional. Default true. |
||
921 | */ |
||
922 | 1 | public function set_style_initial_quotes( $on = true ) { |
|
923 | 1 | $this->data['styleInitialQuotes'] = $on; |
|
924 | 1 | } |
|
925 | |||
926 | /** |
||
927 | * Enables/disables wrapping of numbers in <span class="numbers">. |
||
928 | * |
||
929 | * @param bool $on Optional. Default true. |
||
930 | */ |
||
931 | 1 | public function set_style_numbers( $on = true ) { |
|
932 | 1 | $this->data['styleNumbers'] = $on; |
|
933 | 1 | } |
|
934 | |||
935 | /** |
||
936 | * Enables/disables wrapping of punctiation and wide characters in <span class="pull-*">. |
||
937 | * |
||
938 | * @param bool $on Optional. Default true. |
||
939 | */ |
||
940 | 1 | public function set_style_hanging_punctuation( $on = true ) { |
|
941 | 1 | $this->data['styleHangingPunctuation'] = $on; |
|
942 | 1 | } |
|
943 | |||
944 | /** |
||
945 | * Sets the list of tags where initial quotes and guillemets should be styled. |
||
946 | * |
||
947 | * @param string|array $tags A comma separated list or an array of tag names. |
||
948 | */ |
||
949 | 1 | public function set_initial_quote_tags( $tags = [ 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'li', 'dd', 'dt' ] ) { |
|
950 | // Make array if handed a list of tags as a string. |
||
951 | 1 | if ( ! \is_array( $tags ) ) { |
|
952 | 1 | $tags = \preg_split( '/[^a-z0-9]+/', $tags, -1, PREG_SPLIT_NO_EMPTY ); |
|
953 | } |
||
954 | |||
955 | // Store the tag array inverted (with the tagName as its index for faster lookup). |
||
956 | 1 | $this->data['initialQuoteTags'] = \array_change_key_case( \array_flip( /** Array. @scrutinizer ignore-type */ $tags ), CASE_LOWER ); |
|
957 | 1 | } |
|
958 | |||
959 | /** |
||
960 | * Enables/disables hyphenation. |
||
961 | * |
||
962 | * @param bool $on Optional. Default true. |
||
963 | */ |
||
964 | 1 | public function set_hyphenation( $on = true ) { |
|
965 | 1 | $this->data['hyphenation'] = $on; |
|
966 | 1 | } |
|
967 | |||
968 | /** |
||
969 | * Sets the hyphenation pattern language. |
||
970 | * |
||
971 | * @param string $lang Has to correspond to a filename in 'lang'. Optional. Default 'en-US'. |
||
972 | */ |
||
973 | 8 | public function set_hyphenation_language( $lang = 'en-US' ) { |
|
974 | 8 | if ( isset( $this->data['hyphenLanguage'] ) && $this->data['hyphenLanguage'] === $lang ) { |
|
975 | 3 | return; // Bail out, no need to do anything. |
|
976 | } |
||
977 | |||
978 | 8 | $this->data['hyphenLanguage'] = $lang; |
|
979 | 8 | } |
|
980 | |||
981 | /** |
||
982 | * Sets the minimum length of a word that may be hyphenated. |
||
983 | * |
||
984 | * @param int $length Defaults to 5. Trying to set the value to less than 2 resets the length to the default. |
||
985 | */ |
||
986 | 1 | public function set_min_length_hyphenation( $length = 5 ) { |
|
987 | 1 | $length = ( $length > 1 ) ? $length : 5; |
|
988 | |||
989 | 1 | $this->data['hyphenMinLength'] = $length; |
|
990 | 1 | } |
|
991 | |||
992 | /** |
||
993 | * Sets the minimum character requirement before a hyphenation point. |
||
994 | * |
||
995 | * @param int $length Defaults to 3. Trying to set the value to less than 1 resets the length to the default. |
||
996 | */ |
||
997 | 1 | public function set_min_before_hyphenation( $length = 3 ) { |
|
1001 | 1 | } |
|
1002 | |||
1003 | /** |
||
1004 | * Sets the minimum character requirement after a hyphenation point. |
||
1005 | * |
||
1006 | * @param int $length Defaults to 2. Trying to set the value to less than 1 resets the length to the default. |
||
1007 | */ |
||
1008 | 1 | public function set_min_after_hyphenation( $length = 2 ) { |
|
1009 | 1 | $length = ( $length > 0 ) ? $length : 2; |
|
1010 | |||
1011 | 1 | $this->data['hyphenMinAfter'] = $length; |
|
1012 | 1 | } |
|
1013 | |||
1014 | /** |
||
1015 | * Enables/disables hyphenation of titles and headings. |
||
1016 | * |
||
1017 | * @param bool $on Optional. Default true. |
||
1018 | */ |
||
1019 | 1 | public function set_hyphenate_headings( $on = true ) { |
|
1021 | 1 | } |
|
1022 | |||
1023 | /** |
||
1024 | * Enables/disables hyphenation of words set completely in capital letters. |
||
1025 | * |
||
1026 | * @param bool $on Optional. Default true. |
||
1027 | */ |
||
1028 | 1 | public function set_hyphenate_all_caps( $on = true ) { |
|
1029 | 1 | $this->data['hyphenateAllCaps'] = $on; |
|
1030 | 1 | } |
|
1031 | |||
1032 | /** |
||
1033 | * Enables/disables hyphenation of words starting with a capital letter. |
||
1034 | * |
||
1035 | * @param bool $on Optional. Default true. |
||
1036 | */ |
||
1037 | 1 | public function set_hyphenate_title_case( $on = true ) { |
|
1038 | 1 | $this->data['hyphenateTitleCase'] = $on; |
|
1039 | 1 | } |
|
1040 | |||
1041 | /** |
||
1042 | * Enables/disables hyphenation of compound words (e.g. "editor-in-chief"). |
||
1043 | * |
||
1044 | * @param bool $on Optional. Default true. |
||
1045 | */ |
||
1046 | 1 | public function set_hyphenate_compounds( $on = true ) { |
|
1047 | 1 | $this->data['hyphenateCompounds'] = $on; |
|
1048 | 1 | } |
|
1049 | |||
1050 | /** |
||
1051 | * Sets custom word hyphenations. |
||
1052 | * |
||
1053 | * @param string|array $exceptions An array of words with all hyphenation points marked with a hard hyphen (or a string list of such words). |
||
1054 | * In the latter case, only alphanumeric characters and hyphens are recognized. The default is empty. |
||
1055 | */ |
||
1056 | 2 | public function set_hyphenation_exceptions( $exceptions = [] ) { |
|
1057 | 2 | $this->data['hyphenationCustomExceptions'] = Strings::maybe_split_parameters( $exceptions ); |
|
1058 | 2 | } |
|
1059 | |||
1060 | /** |
||
1061 | * Retrieves a unique hash value for the current settings. |
||
1062 | * |
||
1063 | * @since 5.2.0 The new parameter $raw_output has been added. |
||
1064 | * |
||
1065 | * @param int $max_length Optional. The maximum number of bytes returned (0 for unlimited). Default 16. |
||
1066 | * @param bool $raw_output Optional. Wether to return raw binary data for the hash. Default true. |
||
1067 | * |
||
1068 | * @return string A binary hash value for the current settings limited to $max_length. |
||
1069 | */ |
||
1070 | 1 | public function get_hash( $max_length = 16, $raw_output = true ) { |
|
1078 | } |
||
1079 | } |
||
1080 |