PluginProbe
Polylang / 3.8.5
Polylang v3.8.5
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
polylang / src / modules / Blocks / Language_Switcher / Abstract_Block.php

Abstract_Block.php in Polylang 3.8.5, at src/modules/Blocks/Language_Switcher/Abstract_Block.php

237 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 namespace WP_Syntex\Polylang\Blocks\Language_Switcher;
7
8 use PLL_Language;
9 use PLL_Switcher;
10 use WP_Block_Type_Registry;
11
12 /**
13 * Abstract class for language switcher block.
14 *
15 * @since 3.2
16 * @since 3.8 Moved to Polylang Core and renamed to Language_Switcher\Abstract_Block.
17 */
18 abstract class Abstract_Block {
19 /**
20 * @var \PLL_Links
21 */
22 protected $links;
23
24 /**
25 * @var \PLL_Model
26 */
27 protected $model;
28
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 * Current language.
47 *
48 * @var PLL_Language|false|null
49 */
50 private $current_language;
51
52 /**
53 * Constructor
54 *
55 * @since 2.8
56 *
57 * @param \PLL_Base $polylang Polylang object.
58 */
59 public function __construct( &$polylang ) {
60 $this->model = &$polylang->model;
61 $this->links = &$polylang->links;
62 $this->current_language = &$polylang->curlang;
63 }
64
65 /**
66 * Adds the required hooks.
67 *
68 * @since 3.2
69 *
70 * @return self
71 */
72 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 // Register language switcher block.
77 add_action( 'init', array( $this, 'register' ) );
78
79 return $this;
80 }
81
82 /**
83 * Returns the block name with the Polylang's namespace.
84 *
85 * @since 3.2
86 *
87 * @return string The block name.
88 */
89 abstract protected function get_block_name();
90
91 /**
92 * Renders the Polylang's block on server.
93 *
94 * @since 3.2
95 * @since 3.3 Accepts two new parameters, $content and $block.
96 *
97 * @param array $attributes The block attributes.
98 * @param string $content The saved content.
99 * @param \WP_Block $block The parsed block.
100 * @return string The HTML string output to serve.
101 */
102 abstract public function render( $attributes, $content, $block );
103
104
105
106 /**
107 * Returns the path to the block JSON file directory.
108 * The directory name being used to register a block.
109 *
110 * @since 3.8
111 *
112 * @return string The path to the block.
113 */
114 abstract protected function get_path(): string;
115
116 /**
117 * Registers the Polylang's block.
118 *
119 * @since 2.8
120 * @since 3.2 Renamed and now handle any type of block registration based on a dynamic name.
121 *
122 * @return void
123 */
124 public function register() {
125 if ( WP_Block_Type_Registry::get_instance()->is_registered( $this->get_block_name() ) ) {
126 // Don't register a block more than once or WordPress send an error. See https://github.com/WordPress/wordpress-develop/blob/5.9/src/wp-includes/class-wp-block-type-registry.php#L82-L90
127 return;
128 }
129
130 if ( ! register_block_type(
131 $this->get_path(),
132 array(
133 'render_callback' => array( $this, 'render' ),
134 )
135 ) ) {
136 return;
137 }
138
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 ),
146 array(
147 'wp-block-editor',
148 'wp-blocks',
149 'wp-components',
150 'wp-element',
151 'wp-i18n',
152 'wp-server-side-render',
153 'lodash',
154 'wp-editor',
155 ),
156 POLYLANG_VERSION,
157 true
158 );
159
160 wp_localize_script( $script_handle, 'pll_block_editor_blocks_settings', PLL_Switcher::get_switcher_options( 'block', 'string' ) );
161
162 // Translated strings used in JS code
163 wp_set_script_translations( $script_handle, 'polylang' );
164
165 // Fallback to default language if current language is not set, usually happens in Site Editor.
166 $current_language = $this->current_language;
167
168 if ( ! $current_language ) {
169 $current_language = $this->model->get_default_language();
170 }
171
172 if ( ! $current_language ) {
173 // Should not happen since the module is loaded only if there are languages.
174 return;
175 }
176
177 if ( str_contains( wp_scripts()->get_inline_script_data( $script_handle, 'after' ), 'pllEditorCurrentLanguageSlug' ) ) {
178 return;
179 }
180
181 wp_add_inline_script(
182 $script_handle,
183 'let pllEditorCurrentLanguageSlug = ' . wp_json_encode( $current_language->slug ) . ';',
184 'after'
185 );
186 }
187
188 /**
189 * Returns the REST parameters for language switcher block.
190 * Used to store the request's language and context locally.
191 * Previously was in the `PLL_Block_Editor_Switcher_Block` class.
192 *
193 * @see WP_REST_Server::dispatch()
194 *
195 * @since 2.8
196 *
197 * @param mixed $result Response to replace the requested version with. Can be anything
198 * a normal endpoint can return, or null to not hijack the request.
199 * @param \WP_REST_Server $server Server instance.
200 * @param \WP_REST_Request $request Request used to generate the response.
201 * @return mixed
202 * @template T of \WP_REST_Request
203 * @phpstan-param T $request
204 */
205 public function get_rest_query_params( $result, $server, $request ) {
206 if ( pll_is_edit_rest_request( $request ) ) {
207 $this->is_edit_context = true;
208
209 $lang = $request->get_param( 'lang' );
210 if ( is_string( $lang ) && ! empty( $lang ) ) {
211 $this->admin_current_lang = $lang;
212 }
213 }
214 return $result;
215 }
216
217 /**
218 * Adds the attributes to render the block correctly.
219 * Also specifies not to echo the switcher in any case.
220 *
221 * @since 3.2
222 *
223 * @param array $attributes The attributes of the currently rendered block.
224 * @return array The modified attributes.
225 */
226 protected function set_attributes_for_block( $attributes ) {
227 $attributes['echo'] = 0;
228 if ( $this->is_edit_context ) {
229 $attributes['admin_render'] = 1;
230 $attributes['admin_current_lang'] = $this->admin_current_lang;
231 $attributes['hide_if_empty'] = 0;
232 $attributes['hide_if_no_translation'] = 0; // Force not to hide the language for the block preview even if the option is checked.
233 }
234 return $attributes;
235 }
236 }
237