SyncTranslationDocumentStatus.php
1.31 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
<?php
namespace WPML\Core\PostTranslation;
use WPML\FP\Fns;
use WPML\FP\Obj;
use WPML\FP\Lst;
class SyncTranslationDocumentStatus implements \IWPML_Action, \IWPML_DIC_Action, \IWPML_Backend_Action, \IWPML_REST_Action {
private $sitepress;
public function __construct( \SitePress $sitepress ) {
$this->sitepress = $sitepress;
}
public function add_hooks() {
add_action( 'transition_post_status', [$this, 'onPostStatusChange'], 10, 3 );
}
/**
* @param string $newStatus
* @param string $oldStatus
* @param \WP_Post $post
*/
public function onPostStatusChange( $newStatus, $oldStatus, $post ) {
if ( $newStatus === $oldStatus || 'publish' !== $newStatus ) {
return;
}
if ( 'draft' !== $oldStatus ) {
return;
}
$settings = $this->sitepress->get_settings();
if ( ! $settings['translated_document_status_sync'] ) {
return;
}
$allPosts = \WPML\Element\API\PostTranslations::getIfOriginal( $post->ID );
$originalPost = Lst::nth( 0, Fns::filter( Obj::prop( 'original' ), $allPosts ) );
$postTranslations = Fns::reject( Obj::prop( 'original' ), $allPosts );
if ( ! $originalPost || empty( $postTranslations ) ) {
return;
}
foreach ( $postTranslations as $sourceLangCode => $data ) {
wp_update_post( ['ID' => $data->element_id, 'post_status' => $newStatus] );
}
}
}