SetEditorMode.php
1.62 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
54
55
<?php
namespace WPML\TM\PostEditScreen\Endpoints;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Right;
use WPML_TM_Post_Edit_TM_Editor_Mode;
class SetEditorMode implements IHandler {
/** @var \SitePress $sitepress */
private $sitepress;
public function __construct( \SitePress $sitepress ) {
$this->sitepress = $sitepress;
}
public function run( Collection $data ) {
$tmSettings = $this->sitepress->get_setting( 'translation-management' );
$useNativeEditor = $data->get( 'enabledEditor' ) !== 'wpml';
$postId = $data->get( 'postId' );
$editorModeFor = $data->get( 'editorModeFor' );
switch ( $editorModeFor ) {
case 'global':
$tmSettings[ WPML_TM_Post_Edit_TM_Editor_Mode::TM_KEY_GLOBAL_USE_NATIVE ] = $useNativeEditor;
$tmSettings[ WPML_TM_Post_Edit_TM_Editor_Mode::TM_KEY_FOR_POST_TYPE_USE_NATIVE ] = [];
$this->sitepress->set_setting( 'translation-management', $tmSettings, true );
WPML_TM_Post_Edit_TM_Editor_Mode::delete_all_posts_option();
break;
case 'all_posts_of_type':
$post_type = get_post_type( $postId );
if ( $post_type ) {
$tmSettings[ WPML_TM_Post_Edit_TM_Editor_Mode::TM_KEY_FOR_POST_TYPE_USE_NATIVE ][ $post_type ] = $useNativeEditor;
$this->sitepress->set_setting( 'translation-management', $tmSettings, true );
WPML_TM_Post_Edit_TM_Editor_Mode::delete_all_posts_option( $post_type );
}
break;
case 'this_post':
update_post_meta(
$postId,
WPML_TM_Post_Edit_TM_Editor_Mode::POST_META_KEY_USE_NATIVE,
$useNativeEditor ? 'yes' : 'no'
);
break;
}
return Right::of( true );
}
}