PluginProbe
Booking Calendar / 11.9
Booking Calendar v11.9
11.9 11.8.4 11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 All 205 releases
booking / includes / booking_modes_v3 / class-wpbc-booking-modes-v3-definition-translator.php

class-wpbc-booking-modes-v3-definition-translator.php in Booking Calendar 11.9, at includes/booking_modes_v3/class-wpbc-booking-modes-v3-definition-translator.php

168 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ] = self::translate_display_string( $definition[ $mode_text_key ] );
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 a declaration display string and expose declaration-only text to gettext.
79 *
80 * Mode declarations must remain translation-neutral because preflight loads them
81 * before WordPress `init`. Most declaration labels also occur in ordinary page
82 * code, but the strings below exist only in the raw declarations and therefore
83 * need an active, post-`init` gettext boundary so translation tools can extract
84 * them and runtime catalogs can resolve them.
85 *
86 * @param string $source_string Validated English declaration string.
87 *
88 * @return string Translated display string.
89 */
90 private static function translate_display_string( $source_string ) {
91 switch ( $source_string ) {
92 case '+ Add Appointment':
93 return __( '+ Add Appointment', 'booking' );
94 case '+ Add Booking':
95 return __( '+ Add Booking', 'booking' );
96 case 'Add Booking':
97 return __( 'Add Booking', 'booking' );
98 case 'Appointment administration.':
99 return __( 'Appointment administration.', 'booking' );
100 case 'Booking Forms':
101 return __( 'Booking Forms', 'booking' );
102 case 'Classic':
103 return __( 'Classic', 'booking' );
104 case 'General booking administration.':
105 return __( 'General booking administration.', 'booking' );
106 case 'MultiUser Settings':
107 return __( 'MultiUser Settings', 'booking' );
108 case 'Prices & Extras':
109 return __( 'Prices & Extras', 'booking' );
110 case 'Pricing & Extras':
111 return __( 'Pricing & Extras', 'booking' );
112 case 'Pricing & Payments':
113 return __( 'Pricing & Payments', 'booking' );
114 case 'Pricing Rules':
115 return __( 'Pricing Rules', 'booking' );
116 case 'Properties':
117 return __( 'Properties', 'booking' );
118 case 'Property rental administration.':
119 return __( 'Property rental administration.', 'booking' );
120 case 'Rentals':
121 return __( 'Rentals', 'booking' );
122 case 'Searchable Properties':
123 return __( 'Searchable Properties', 'booking' );
124 case 'Searchable Providers':
125 return __( 'Searchable Providers', 'booking' );
126 default:
127 return translate( $source_string, 'booking' );
128 }
129 }
130
131 /**
132 * Translate an allow-listed set of display keys.
133 *
134 * @param array $record Validated declaration record.
135 * @param array $text_keys Display keys to translate.
136 *
137 * @return array Updated record.
138 */
139 private static function translate_keys( $record, $text_keys ) {
140 foreach ( $text_keys as $text_key ) {
141 if ( array_key_exists( $text_key, $record ) && is_string( $record[ $text_key ] ) ) {
142 $record[ $text_key ] = self::translate_display_string( $record[ $text_key ] );
143 }
144 }
145
146 return $record;
147 }
148
149 /**
150 * Translate recursive sidebar labels without translating identifiers/icons.
151 *
152 * @param array $nodes Validated sidebar node map.
153 *
154 * @return array Translated sidebar node map.
155 */
156 private static function translate_sidebar_nodes( $nodes ) {
157 foreach ( $nodes as $node_key => $node ) {
158 $node = self::translate_keys( $node, array( 'title' ) );
159 if ( isset( $node['items'] ) && is_array( $node['items'] ) ) {
160 $node['items'] = self::translate_sidebar_nodes( $node['items'] );
161 }
162 $nodes[ $node_key ] = $node;
163 }
164
165 return $nodes;
166 }
167 }
168