class-translation.php
1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace WPML\PB\Gutenberg\ReusableBlocks;
class Translation {
const POST_TYPE = 'wp_block';
/** @var \SitePress $sitepress */
private $sitepress;
/**
* @param \SitePress $sitepress
*/
public function __construct( \SitePress $sitepress ) {
$this->sitepress = $sitepress;
}
/**
* @param array $block
* @param null|string $lang
*
* @return array
*/
public function convertBlock( array $block, $lang = null ) {
if ( Blocks::isReusable( $block ) ) {
$block['attrs']['ref'] = $this->convertBlockId( $block['attrs']['ref'], $lang );
}
return $block;
}
/**
* @param int $block_id
* @param string|null $lang
*
* @return int
*/
public function convertBlockId( $block_id, $lang = null ) {
return $this->sitepress->get_object_id( $block_id, self::POST_TYPE, true, $lang );
}
/**
* @param int $post_id
*
* @return string|null
*/
public function getSourceLang( $post_id ) {
$details = $this->sitepress->get_element_language_details( $post_id, 'post_' . get_post_type( $post_id ) );
return isset( $details->source_language_code ) ? $details->source_language_code : null;
}
}