← All changes
|
src/modules/Blocks/Language_Switcher/Abstract_Block.php
+51
-79
3.8.3
→
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,26 +123,18 @@ | ||
| 148 | 123 | 'wp-blocks', |
| 149 | 124 | 'wp-components', |
| 150 | 125 | 'wp-element', |
| 151 | 126 | 'wp-i18n', |
| 152 | - 'wp-server-side-render', | |
| 153 | - 'lodash', | |
| 154 | - 'wp-editor', | |
| 155 | 127 | ), |
| 156 | - POLYLANG_VERSION, | |
| 157 | - true | |
| 128 | + array( | |
| 129 | + 'in_footer' => true, | |
| 130 | + ), | |
| 131 | + 'pll_' | |
| 158 | 132 | ); |
| 159 | 133 | |
| 160 | - wp_localize_script( $script_handle, 'pll_block_editor_blocks_settings', PLL_Switcher::get_switcher_options( 'block', 'string' ) ); | |
| 161 | - | |
| 162 | 134 | // Translated strings used in JS code |
| 163 | 135 | wp_set_script_translations( $script_handle, 'polylang' ); |
| 164 | 136 | |
| 165 | - // No need to render the JS variable if Polylang Pro is installed, the editor will use `lang` REST field instead. | |
| 166 | - if ( class_exists( 'WP_Syntex\Polylang_Pro\REST\Translated\Post' ) ) { | |
| 167 | - return; | |
| 168 | - } | |
| 169 | - | |
| 170 | 137 | // Fallback to default language if current language is not set, usually happens in Site Editor. |
| 171 | 138 | $current_language = $this->current_language; |
| 172 | 139 | |
| 173 | 140 | if ( ! $current_language ) { |
| @@ -190,52 +157,57 @@ | ||
| 190 | 157 | ); |
| 191 | 158 | } |
| 192 | 159 | |
| 193 | 160 | /** |
| 194 | - * Returns the REST parameters for language switcher block. | |
| 195 | - * Used to store the request's language and context locally. | |
| 196 | - * Previously was in the `PLL_Block_Editor_Switcher_Block` class. | |
| 161 | + * Applies flag block attributes to switcher markup. | |
| 197 | 162 | * |
| 198 | - * @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). | |
| 199 | 165 | * |
| 200 | - * @since 2.8 | |
| 166 | + * @since 3.9 | |
| 201 | 167 | * |
| 202 | - * @param mixed $result Response to replace the requested version with. Can be anything | |
| 203 | - * a normal endpoint can return, or null to not hijack the request. | |
| 204 | - * @param \WP_REST_Server $server Server instance. | |
| 205 | - * @param \WP_REST_Request $request Request used to generate the response. | |
| 206 | - * @return mixed | |
| 207 | - * @template T of \WP_REST_Request | |
| 208 | - * @phpstan-param T $request | |
| 168 | + * @param string $html Switcher HTML. | |
| 169 | + * @param array $attributes Block attributes. | |
| 170 | + * @return string | |
| 209 | 171 | */ |
| 210 | - public function get_rest_query_params( $result, $server, $request ) { | |
| 211 | - if ( pll_is_edit_rest_request( $request ) ) { | |
| 212 | - $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 | + } | |
| 213 | 176 | |
| 214 | - $lang = $request->get_param( 'lang' ); | |
| 215 | - if ( is_string( $lang ) && ! empty( $lang ) ) { | |
| 216 | - $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 ); | |
| 217 | 208 | } |
| 218 | 209 | } |
| 219 | - return $result; | |
| 220 | - } | |
| 221 | 210 | |
| 222 | - /** | |
| 223 | - * Adds the attributes to render the block correctly. | |
| 224 | - * Also specifies not to echo the switcher in any case. | |
| 225 | - * | |
| 226 | - * @since 3.2 | |
| 227 | - * | |
| 228 | - * @param array $attributes The attributes of the currently rendered block. | |
| 229 | - * @return array The modified attributes. | |
| 230 | - */ | |
| 231 | - protected function set_attributes_for_block( $attributes ) { | |
| 232 | - $attributes['echo'] = 0; | |
| 233 | - if ( $this->is_edit_context ) { | |
| 234 | - $attributes['admin_render'] = 1; | |
| 235 | - $attributes['admin_current_lang'] = $this->admin_current_lang; | |
| 236 | - $attributes['hide_if_empty'] = 0; | |
| 237 | - $attributes['hide_if_no_translation'] = 0; // Force not to hide the language for the block preview even if the option is checked. | |
| 238 | - } | |
| 239 | - return $attributes; | |
| 211 | + return $processor->get_updated_html(); | |
| 240 | 212 | } |
| 241 | 213 | } |