PluginProbe
Polylang / 3.4
Polylang v3.4
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 / admin / admin-block-editor.php

admin-block-editor.php in Polylang 3.4, at admin/admin-block-editor.php

71 lines 1.6 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 /**
7 * Manages filters and actions related to the block editor
8 *
9 * @since 2.5
10 */
11 class PLL_Admin_Block_Editor {
12 /**
13 * @var PLL_Model
14 */
15 protected $model;
16
17 /**
18 * Preferred language to assign to a new post.
19 *
20 * @var PLL_Language|null
21 */
22 protected $pref_lang;
23
24 /**
25 * Constructor: setups filters and actions
26 *
27 * @since 2.5
28 *
29 * @param object $polylang
30 */
31 public function __construct( &$polylang ) {
32 $this->model = &$polylang->model;
33 $this->pref_lang = &$polylang->pref_lang;
34
35 add_filter( 'block_editor_rest_api_preload_paths', array( $this, 'preload_paths' ), 10, 2 );
36 }
37
38 /**
39 * Filters the preload REST requests by the current language of the post.
40 * Necessary otherwise subsequent REST requests, all filtered by the language,
41 * would not hit the preloaded requests.
42 *
43 * @since 2.5
44 *
45 * @param (string|string[])[] $preload_paths Array of paths to preload.
46 * @param WP_Block_Editor_Context $context Block editor context.
47 * @return (string|string[])[]
48 */
49 public function preload_paths( $preload_paths, $context ) {
50 if (
51 $context instanceof WP_Block_Editor_Context
52 && $context->post instanceof WP_Post
53 && $this->model->is_translated_post_type( $context->post->post_type )
54 ) {
55 $lang = $this->model->post->get_language( $context->post->ID );
56
57 if ( ! $lang ) {
58 $lang = $this->pref_lang;
59 }
60
61 foreach ( $preload_paths as $k => $path ) {
62 if ( is_string( $path ) && '/' !== $path ) {
63 $preload_paths[ $k ] = $path . "&lang={$lang->slug}";
64 }
65 }
66 }
67
68 return $preload_paths;
69 }
70 }
71