| @@ -1,5 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | /** |
| 4 | 7 | * Manages filters and actions related to the block editor |
| 5 | 8 | * |
| @@ -5,49 +8,85 @@ | ||
| 5 | 8 | * |
| 6 | 9 | * @since 2.5 |
| 7 | 10 | */ |
| 8 | 11 | class PLL_Admin_Block_Editor { |
| 9 | - public $model; | |
| 12 | + /** | |
| 13 | + * @var PLL_Model | |
| 14 | + */ | |
| 15 | + protected $model; | |
| 10 | 16 | |
| 11 | 17 | /** |
| 12 | - * 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. | |
| 13 | 24 | * |
| 14 | 25 | * @since 2.5 |
| 15 | 26 | * |
| 16 | - * @param object $polylang | |
| 27 | + * @param PLL_Admin $polylang The Polylang object. | |
| 17 | 28 | */ |
| 18 | 29 | public function __construct( &$polylang ) { |
| 19 | - $this->model = &$polylang->model; | |
| 20 | - $this->pref_lang = &$polylang->pref_lang; | |
| 30 | + $this->model = &$polylang->model; | |
| 31 | + $this->filter_rest_routes = new PLL_Filter_REST_Routes( $polylang->model ); | |
| 21 | 32 | |
| 22 | - 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. | |
| 23 | 35 | } |
| 24 | 36 | |
| 25 | 37 | /** |
| 26 | - * Filter the preload REST requests by the current language of the post | |
| 27 | - * Necessary otherwise subsequent REST requests all filtered by the language | |
| 28 | - * 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). | |
| 29 | 39 | * |
| 30 | - * @since 2.5 | |
| 40 | + * @since 3.5 | |
| 31 | 41 | * |
| 32 | - * @param array $preload_paths Array of paths to preload. | |
| 33 | - * @param object $post The post resource data. | |
| 34 | - * @return array | |
| 42 | + * @param array $preload_paths Preload paths. | |
| 43 | + * @param WP_Block_Editor_Context $context Editor context. | |
| 44 | + * @return array Filtered preload paths. | |
| 35 | 45 | */ |
| 36 | - public function preload_paths( $preload_paths, $post ) { | |
| 37 | - if ( $this->model->is_translated_post_type( $post->post_type ) ) { | |
| 38 | - $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 | + } | |
| 39 | 50 | |
| 40 | - if ( ! $lang ) { | |
| 41 | - $lang = $this->pref_lang; | |
| 42 | - } | |
| 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 | + } | |
| 43 | 59 | |
| 44 | - foreach ( $preload_paths as $k => $path ) { | |
| 45 | - if ( is_string( $path ) && '/' !== $path ) { | |
| 46 | - $preload_paths[ $k ] = $path . "&lang={$lang->slug}"; | |
| 47 | - } | |
| 48 | - } | |
| 60 | + if ( ! $this->model->is_translated_post_type( $context->post->post_type ) ) { | |
| 61 | + return $preload_paths; | |
| 49 | 62 | } |
| 50 | 63 | |
| 51 | - 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 | + } | |
| 52 | 91 | } |
| 53 | 92 | } |