PluginProbe
Polylang / 3.6.7
Polylang v3.6.7
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 | admin/admin-block-editor.php +61 -25 2.93.6.7 View file →
@@ -8,49 +8,85 @@
8 8 *
9 9 * @since 2.5
10 10 */
11 11 class PLL_Admin_Block_Editor {
12 - public $model;
12 + /**
13 + * @var PLL_Model
14 + */
15 + protected $model;
13 16
14 17 /**
15 - * Constructor: setups filters and actions
18 + * @var PLL_Filter_REST_Routes
19 + */
20 + public $filter_rest_routes;
21 +
22 + /**
23 + * Constructor: setups filters and actions.
16 24 *
17 25 * @since 2.5
18 26 *
19 - * @param object $polylang
27 + * @param PLL_Admin $polylang The Polylang object.
20 28 */
21 29 public function __construct( &$polylang ) {
22 - $this->model = &$polylang->model;
23 - $this->pref_lang = &$polylang->pref_lang;
30 + $this->model = &$polylang->model;
31 + $this->filter_rest_routes = new PLL_Filter_REST_Routes( $polylang->model );
24 32
25 - add_filter( 'block_editor_preload_paths', array( $this, 'preload_paths' ), 10, 2 );
33 + add_filter( 'block_editor_rest_api_preload_paths', array( $this, 'filter_preload_paths' ), 50, 2 );
34 + add_action( 'admin_enqueue_scripts', array( $this, 'add_block_editor_inline_script' ), 15 ); // After `PLL_Admin_Base::admin_enqueue_scripts()` to ensure `pll_block-editor`script is enqueued.
26 35 }
27 36
28 37 /**
29 - * Filter the preload REST requests by the current language of the post
30 - * Necessary otherwise subsequent REST requests all filtered by the language
31 - * would not hit the preloaded requests
38 + * Filters preload paths based on the context (block editor for posts, site editor or widget editor for instance).
32 39 *
33 - * @since 2.5
40 + * @since 3.5
34 41 *
35 - * @param array $preload_paths Array of paths to preload.
36 - * @param object $post The post resource data.
37 - * @return array
42 + * @param array $preload_paths Preload paths.
43 + * @param WP_Block_Editor_Context $context Editor context.
44 + * @return array Filtered preload paths.
38 45 */
39 - public function preload_paths( $preload_paths, $post ) {
40 - if ( $this->model->is_translated_post_type( $post->post_type ) ) {
41 - $lang = $this->model->post->get_language( $post->ID );
46 + public function filter_preload_paths( $preload_paths, $context ) {
47 + if ( ! $context instanceof WP_Block_Editor_Context ) {
48 + return $preload_paths;
49 + }
42 50
43 - if ( ! $lang ) {
44 - $lang = $this->pref_lang;
45 - }
51 + // Backward compatibility with WP < 6.0 where `WP_Block_Editor_Context::$name` doesn't exist yet.
52 + if (
53 + ( property_exists( $context, 'name' ) && 'core/edit-post' !== $context->name )
54 + || ! $context->post instanceof WP_Post
55 + ) {
56 + // Do nothing if not post editor.
57 + return $preload_paths;
58 + }
46 59
47 - foreach ( $preload_paths as $k => $path ) {
48 - if ( is_string( $path ) && '/' !== $path ) {
49 - $preload_paths[ $k ] = $path . "&lang={$lang->slug}";
50 - }
51 - }
60 + if ( ! $this->model->is_translated_post_type( $context->post->post_type ) ) {
61 + return $preload_paths;
52 62 }
53 63
54 - return $preload_paths;
64 + $language = $this->model->post->get_language( $context->post->ID );
65 +
66 + if ( empty( $language ) ) {
67 + return $preload_paths;
68 + }
69 +
70 + return $this->filter_rest_routes->add_query_parameters(
71 + $preload_paths,
72 + array(
73 + 'lang' => $language->slug,
74 + )
75 + );
76 + }
77 +
78 + /**
79 + * Adds inline block editor script for filterable REST routes.
80 + *
81 + * @since 3.5
82 + *
83 + * @return void
84 + */
85 + public function add_block_editor_inline_script() {
86 + $handle = 'pll_block-editor';
87 +
88 + if ( wp_script_is( $handle, 'enqueued' ) ) {
89 + $this->filter_rest_routes->add_inline_script( $handle );
90 + }
55 91 }
56 92 }