| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Manages filters and actions related to the block editor |
| 8 |
* |
| 9 |
* @since 2.5 |
| 10 |
*/ |
| 11 |
class PLL_Admin_Block_Editor { |
| 12 |
/** |
| 13 |
* @var PLL_Model |
| 14 |
*/ |
| 15 |
protected $model; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var PLL_Filter_REST_Routes |
| 19 |
*/ |
| 20 |
public $filter_rest_routes; |
| 21 |
|
| 22 |
/** |
| 23 |
* Constructor: setups filters and actions. |
| 24 |
* |
| 25 |
* @since 2.5 |
| 26 |
* |
| 27 |
* @param PLL_Admin $polylang The Polylang object. |
| 28 |
*/ |
| 29 |
public function __construct( &$polylang ) { |
| 30 |
$this->model = &$polylang->model; |
| 31 |
$this->filter_rest_routes = new PLL_Filter_REST_Routes( $polylang->model ); |
| 32 |
|
| 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. |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Filters preload paths based on the context (block editor for posts, site editor or widget editor for instance). |
| 39 |
* |
| 40 |
* @since 3.5 |
| 41 |
* |
| 42 |
* @param array $preload_paths Preload paths. |
| 43 |
* @param WP_Block_Editor_Context $context Editor context. |
| 44 |
* @return array Filtered preload paths. |
| 45 |
*/ |
| 46 |
public function filter_preload_paths( $preload_paths, $context ) { |
| 47 |
if ( ! $context instanceof WP_Block_Editor_Context ) { |
| 48 |
return $preload_paths; |
| 49 |
} |
| 50 |
|
| 51 |
if ( 'core/edit-post' !== $context->name || ! $context->post instanceof WP_Post ) { |
| 52 |
// Do nothing if not post editor. |
| 53 |
return $preload_paths; |
| 54 |
} |
| 55 |
|
| 56 |
if ( ! $this->model->is_translated_post_type( $context->post->post_type ) ) { |
| 57 |
return $preload_paths; |
| 58 |
} |
| 59 |
|
| 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 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|