PluginProbe
Polylang / 3.7.7
Polylang v3.7.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 +57 -25 2.9.13.7.7 View file →
@@ -8,49 +8,81 @@
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 + if ( 'core/edit-post' !== $context->name || ! $context->post instanceof WP_Post ) {
52 + // Do nothing if not post editor.
53 + return $preload_paths;
54 + }
46 55
47 - foreach ( $preload_paths as $k => $path ) {
48 - if ( is_string( $path ) && '/' !== $path ) {
49 - $preload_paths[ $k ] = $path . "&lang={$lang->slug}";
50 - }
51 - }
56 + if ( ! $this->model->is_translated_post_type( $context->post->post_type ) ) {
57 + return $preload_paths;
52 58 }
53 59
54 - return $preload_paths;
60 + $language = $this->model->post->get_language( $context->post->ID );
61 +
62 + if ( empty( $language ) ) {
63 + return $preload_paths;
64 + }
65 +
66 + return $this->filter_rest_routes->add_query_parameters(
67 + $preload_paths,
68 + array(
69 + 'lang' => $language->slug,
70 + )
71 + );
72 + }
73 +
74 + /**
75 + * Adds inline block editor script for filterable REST routes.
76 + *
77 + * @since 3.5
78 + *
79 + * @return void
80 + */
81 + public function add_block_editor_inline_script() {
82 + $handle = 'pll_block-editor';
83 +
84 + if ( wp_script_is( $handle, 'enqueued' ) ) {
85 + $this->filter_rest_routes->add_inline_script( $handle );
86 + }
55 87 }
56 88 }