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 +54 -28 3.03.6.7 View file →
@@ -14,53 +14,79 @@
14 14 */
15 15 protected $model;
16 16
17 17 /**
18 - * Preferred language to assign to a new post.
19 - *
20 - * @var PLL_Language
18 + * @var PLL_Filter_REST_Routes
21 19 */
22 - protected $pref_lang;
20 + public $filter_rest_routes;
23 21
24 22 /**
25 - * Constructor: setups filters and actions
23 + * Constructor: setups filters and actions.
26 24 *
27 25 * @since 2.5
28 26 *
29 - * @param object $polylang
27 + * @param PLL_Admin $polylang The Polylang object.
30 28 */
31 29 public function __construct( &$polylang ) {
32 - $this->model = &$polylang->model;
33 - $this->pref_lang = &$polylang->pref_lang;
30 + $this->model = &$polylang->model;
31 + $this->filter_rest_routes = new PLL_Filter_REST_Routes( $polylang->model );
34 32
35 - 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.
36 35 }
37 36
38 37 /**
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.
38 + * Filters preload paths based on the context (block editor for posts, site editor or widget editor for instance).
42 39 *
43 - * @since 2.5
40 + * @since 3.5
44 41 *
45 - * @param (string|string[])[] $preload_paths Array of paths to preload.
46 - * @param WP_Post $post The post resource data.
47 - * @return (string|string[])[]
42 + * @param array $preload_paths Preload paths.
43 + * @param WP_Block_Editor_Context $context Editor context.
44 + * @return array Filtered preload paths.
48 45 */
49 - public function preload_paths( $preload_paths, $post ) {
50 - if ( $this->model->is_translated_post_type( $post->post_type ) ) {
51 - $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 + }
52 50
53 - if ( ! $lang ) {
54 - $lang = $this->pref_lang;
55 - }
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 + }
56 59
57 - foreach ( $preload_paths as $k => $path ) {
58 - if ( is_string( $path ) && '/' !== $path ) {
59 - $preload_paths[ $k ] = $path . "&lang={$lang->slug}";
60 - }
61 - }
60 + if ( ! $this->model->is_translated_post_type( $context->post->post_type ) ) {
61 + return $preload_paths;
62 62 }
63 63
64 - 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 + }
65 91 }
66 92 }