PluginProbe
Polylang / trunk
Polylang vtrunk
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
← All changes | src/modules/Blocks/Language_Switcher/Abstract_Block.php +51 -73 3.8.8 → trunk View file →
@@ -5,10 +5,10 @@
5 5
6 6 namespace WP_Syntex\Polylang\Blocks\Language_Switcher;
7 7
8 8 use PLL_Language;
9 -use PLL_Switcher;
10 9 use WP_Block_Type_Registry;
10 +use WP_HTML_Tag_Processor;
11 11
12 12 /**
13 13 * Abstract class for language switcher block.
14 14 *
@@ -26,24 +26,8 @@
26 26 */
27 27 protected $model;
28 28
29 29 /**
30 - * Current lang to render the language switcher block in an admin context.
31 - *
32 - * @since 2.8
33 - *
34 - * @var string|null
35 - */
36 - protected $admin_current_lang;
37 -
38 - /**
39 - * Is it the edit context?
40 - *
41 - * @var bool
42 - */
43 - protected $is_edit_context = false;
44 -
45 - /**
46 30 * Current language.
47 31 *
48 32 * @var PLL_Language|false|null
49 33 */
@@ -69,11 +53,8 @@
69 53 *
70 54 * @return self
71 55 */
72 56 public function init() {
73 - // Use rest_pre_dispatch_filter to get additional parameters for language switcher block.
74 - add_filter( 'rest_pre_dispatch', array( $this, 'get_rest_query_params' ), 10, 3 );
75 -
76 57 // Register language switcher block.
77 58 add_action( 'init', array( $this, 'register' ) );
78 59
79 60 return $this;
@@ -100,10 +81,8 @@
100 81 * @return string The HTML string output to serve.
101 82 */
102 83 abstract public function render( $attributes, $content, $block );
103 84
104 -
105 -
106 85 /**
107 86 * Returns the path to the block JSON file directory.
108 87 * The directory name being used to register a block.
109 88 *
@@ -135,15 +114,11 @@
135 114 ) ) {
136 115 return;
137 116 }
138 117
139 - $script_handle = 'pll_blocks';
140 - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
141 - $script_filename = 'js/build/blocks' . $suffix . '.js';
142 -
143 - wp_register_script(
144 - $script_handle,
145 - plugins_url( $script_filename, POLYLANG_ROOT_FILE ),
118 + $script_handle = 'pll_blocks';
119 + pll_register_script(
120 + 'blocks',
146 121 array(
147 122 'wp-block-editor',
148 123 'wp-blocks',
149 124 'wp-components',
@@ -148,17 +123,15 @@
148 123 'wp-blocks',
149 124 'wp-components',
150 125 'wp-element',
151 126 'wp-i18n',
152 - 'wp-server-side-render',
153 - 'lodash',
154 127 ),
155 - POLYLANG_VERSION,
156 - true
128 + array(
129 + 'in_footer' => true,
130 + ),
131 + 'pll_'
157 132 );
158 133
159 - wp_localize_script( $script_handle, 'pll_block_editor_blocks_settings', PLL_Switcher::get_switcher_options( 'block', 'string' ) );
160 -
161 134 // Translated strings used in JS code
162 135 wp_set_script_translations( $script_handle, 'polylang' );
163 136
164 137 // Fallback to default language if current language is not set, usually happens in Site Editor.
@@ -184,52 +157,57 @@
184 157 );
185 158 }
186 159
187 160 /**
188 - * Returns the REST parameters for language switcher block.
189 - * Used to store the request's language and context locally.
190 - * Previously was in the `PLL_Block_Editor_Switcher_Block` class.
161 + * Applies flag block attributes to switcher markup.
191 162 *
192 - * @see WP_REST_Server::dispatch()
163 + * Uses WP_HTML_Tag_Processor so the dropdown SVG toggle can stay in the markup
164 + * (WP_HTML_Processor aborts on foreign content).
193 165 *
194 - * @since 2.8
166 + * @since 3.9
195 167 *
196 - * @param mixed $result Response to replace the requested version with. Can be anything
197 - * a normal endpoint can return, or null to not hijack the request.
198 - * @param \WP_REST_Server $server Server instance.
199 - * @param \WP_REST_Request $request Request used to generate the response.
200 - * @return mixed
201 - * @template T of \WP_REST_Request
202 - * @phpstan-param T $request
168 + * @param string $html Switcher HTML.
169 + * @param array $attributes Block attributes.
170 + * @return string
203 171 */
204 - public function get_rest_query_params( $result, $server, $request ) {
205 - if ( pll_is_edit_rest_request( $request ) ) {
206 - $this->is_edit_context = true;
172 + protected function apply_flag_styles_to_markup( string $html, array $attributes ): string {
173 + if ( empty( $attributes['show_flags'] ) ) {
174 + return $html;
175 + }
207 176
208 - $lang = $request->get_param( 'lang' );
209 - if ( is_string( $lang ) && ! empty( $lang ) ) {
210 - $this->admin_current_lang = $lang;
177 + $border_radius = max( 0, min( 100, (int) ( $attributes['flag_border_radius'] ?? 0 ) ) );
178 + $flag_width = $attributes['flag_width'] ?? '18px';
179 + $flag_style = safecss_filter_attr(
180 + sprintf(
181 + '--pll-flag-border-radius:%1$d;--pll-flag-width:%2$s',
182 + $border_radius,
183 + $flag_width
184 + )
185 + );
186 +
187 + $label_style = '';
188 + if ( ! empty( $attributes['show_labels'] ) ) {
189 + $label_style = safecss_filter_attr(
190 + sprintf(
191 + '--pll-flag-label-spacing:%1$s',
192 + $attributes['flag_label_spacing'] ?? '0.3em'
193 + )
194 + );
195 + }
196 +
197 + $processor = new WP_HTML_Tag_Processor( $html );
198 +
199 + while ( $processor->next_tag( array( 'tag_name' => 'SPAN', 'class_name' => 'pll-switcher-flag' ) ) ) {
200 + $style = $processor->get_attribute( 'style' ) ?? '';
201 + /** @var string $style `WP_HTML_Tag_Processor::get_attribute()` returns string|null for non-boolean attributes. */
202 + $processor->set_attribute( 'style', '' === $style ? $flag_style : rtrim( $style, ';' ) . ';' . $flag_style );
203 +
204 + if ( '' !== $label_style && $processor->next_tag( array( 'tag_name' => 'SPAN', 'class_name' => 'pll-switcher-label' ) ) ) {
205 + $style = $processor->get_attribute( 'style' ) ?? '';
206 + /** @var string $style `WP_HTML_Tag_Processor::get_attribute()` returns string|null for non-boolean attributes. */
207 + $processor->set_attribute( 'style', '' === $style ? $label_style : rtrim( $style, ';' ) . ';' . $label_style );
211 208 }
212 209 }
213 - return $result;
214 - }
215 210
216 - /**
217 - * Adds the attributes to render the block correctly.
218 - * Also specifies not to echo the switcher in any case.
219 - *
220 - * @since 3.2
221 - *
222 - * @param array $attributes The attributes of the currently rendered block.
223 - * @return array The modified attributes.
224 - */
225 - protected function set_attributes_for_block( $attributes ) {
226 - $attributes['echo'] = 0;
227 - if ( $this->is_edit_context ) {
228 - $attributes['admin_render'] = 1;
229 - $attributes['admin_current_lang'] = $this->admin_current_lang;
230 - $attributes['hide_if_empty'] = 0;
231 - $attributes['hide_if_no_translation'] = 0; // Force not to hide the language for the block preview even if the option is checked.
232 - }
233 - return $attributes;
211 + return $processor->get_updated_html();
234 212 }
235 213 }