| 1 |
<?php |
| 2 |
/** |
| 3 |
* Deferred translation for Booking Modes V3 declarations. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
* @since 11.8.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Translate declaration display strings only after WordPress initializes. |
| 15 |
* |
| 16 |
* V3 declarations are validated during the early fail-closed preflight. Keeping |
| 17 |
* their source values translation-neutral prevents just-in-time text-domain |
| 18 |
* loading before `init`, while this class restores normal translated labels for |
| 19 |
* every presentation consumer later in the request. |
| 20 |
*/ |
| 21 |
final class WPBC_Booking_Modes_V3_Definition_Translator { |
| 22 |
|
| 23 |
/** |
| 24 |
* Translate every display string in one validated declaration. |
| 25 |
* |
| 26 |
* The declaration is returned unchanged before `init` and in isolated CLI |
| 27 |
* checks where the WordPress action API is unavailable. |
| 28 |
* |
| 29 |
* @param array $definition Validated translation-neutral declaration. |
| 30 |
* |
| 31 |
* @return array Declaration with translated display strings when safe. |
| 32 |
*/ |
| 33 |
public static function translate( $definition ) { |
| 34 |
if ( ! self::is_translation_ready() || ! is_array( $definition ) ) { |
| 35 |
return $definition; |
| 36 |
} |
| 37 |
|
| 38 |
foreach ( array( 'label', 'description' ) as $mode_text_key ) { |
| 39 |
if ( isset( $definition[ $mode_text_key ] ) && is_string( $definition[ $mode_text_key ] ) ) { |
| 40 |
$definition[ $mode_text_key ] = translate( $definition[ $mode_text_key ], 'booking' ); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
foreach ( $definition['pages'] as $page_id => $page_definition ) { |
| 45 |
if ( isset( $page_definition['page_options'] ) ) { |
| 46 |
$definition['pages'][ $page_id ]['page_options'] = self::translate_keys( |
| 47 |
$page_definition['page_options'], |
| 48 |
array( 'page_title', 'page_description', 'top_path_title' ) |
| 49 |
); |
| 50 |
} |
| 51 |
if ( isset( $page_definition['horizontal_menu'] ) ) { |
| 52 |
$definition['pages'][ $page_id ]['horizontal_menu'] = self::translate_keys( |
| 53 |
$page_definition['horizontal_menu'], |
| 54 |
array( 'title' ) |
| 55 |
); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
foreach ( $definition['wordpress_menu'] as $menu_slug => $menu_definition ) { |
| 60 |
$definition['wordpress_menu'][ $menu_slug ] = self::translate_keys( $menu_definition, array( 'title' ) ); |
| 61 |
} |
| 62 |
|
| 63 |
$definition['sidebar_menu'] = self::translate_sidebar_nodes( $definition['sidebar_menu'] ); |
| 64 |
|
| 65 |
return $definition; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Determine whether calling the Booking Calendar text domain is safe. |
| 70 |
* |
| 71 |
* @return bool True at or after the WordPress `init` action. |
| 72 |
*/ |
| 73 |
public static function is_translation_ready() { |
| 74 |
return function_exists( 'did_action' ) && 0 < did_action( 'init' ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Translate an allow-listed set of display keys. |
| 79 |
* |
| 80 |
* @param array $record Validated declaration record. |
| 81 |
* @param array $text_keys Display keys to translate. |
| 82 |
* |
| 83 |
* @return array Updated record. |
| 84 |
*/ |
| 85 |
private static function translate_keys( $record, $text_keys ) { |
| 86 |
foreach ( $text_keys as $text_key ) { |
| 87 |
if ( array_key_exists( $text_key, $record ) && is_string( $record[ $text_key ] ) ) { |
| 88 |
$record[ $text_key ] = translate( $record[ $text_key ], 'booking' ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return $record; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Translate recursive sidebar labels without translating identifiers/icons. |
| 97 |
* |
| 98 |
* @param array $nodes Validated sidebar node map. |
| 99 |
* |
| 100 |
* @return array Translated sidebar node map. |
| 101 |
*/ |
| 102 |
private static function translate_sidebar_nodes( $nodes ) { |
| 103 |
foreach ( $nodes as $node_key => $node ) { |
| 104 |
$node = self::translate_keys( $node, array( 'title' ) ); |
| 105 |
if ( isset( $node['items'] ) && is_array( $node['items'] ) ) { |
| 106 |
$node['items'] = self::translate_sidebar_nodes( $node['items'] ); |
| 107 |
} |
| 108 |
$nodes[ $node_key ] = $node; |
| 109 |
} |
| 110 |
|
| 111 |
return $nodes; |
| 112 |
} |
| 113 |
} |
| 114 |
|