| 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 |
public $model; |
| 13 |
|
| 14 |
/** |
| 15 |
* Constructor: setups filters and actions |
| 16 |
* |
| 17 |
* @since 2.5 |
| 18 |
* |
| 19 |
* @param object $polylang |
| 20 |
*/ |
| 21 |
public function __construct( &$polylang ) { |
| 22 |
$this->model = &$polylang->model; |
| 23 |
$this->pref_lang = &$polylang->pref_lang; |
| 24 |
|
| 25 |
add_filter( 'block_editor_preload_paths', array( $this, 'preload_paths' ), 10, 2 ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 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 |
| 32 |
* |
| 33 |
* @since 2.5 |
| 34 |
* |
| 35 |
* @param array $preload_paths Array of paths to preload. |
| 36 |
* @param object $post The post resource data. |
| 37 |
* @return array |
| 38 |
*/ |
| 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 ); |
| 42 |
|
| 43 |
if ( ! $lang ) { |
| 44 |
$lang = $this->pref_lang; |
| 45 |
} |
| 46 |
|
| 47 |
foreach ( $preload_paths as $k => $path ) { |
| 48 |
if ( is_string( $path ) && '/' !== $path ) { |
| 49 |
$preload_paths[ $k ] = $path . "&lang={$lang->slug}"; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
return $preload_paths; |
| 55 |
} |
| 56 |
} |
| 57 |
|