PluginProbe
Polylang / 3.7.1
Polylang v3.7.1
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 +50 -28 3.03.7.1 View file →
@@ -14,53 +14,75 @@
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 + if ( 'core/edit-post' !== $context->name || ! $context->post instanceof WP_Post ) {
52 + // Do nothing if not post editor.
53 + return $preload_paths;
54 + }
56 55
57 - foreach ( $preload_paths as $k => $path ) {
58 - if ( is_string( $path ) && '/' !== $path ) {
59 - $preload_paths[ $k ] = $path . "&lang={$lang->slug}";
60 - }
61 - }
56 + if ( ! $this->model->is_translated_post_type( $context->post->post_type ) ) {
57 + return $preload_paths;
62 58 }
63 59
64 - 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 + }
65 87 }
66 88 }