class-wpml-tm-ams-synchronize-actions.php
3.45 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
use WPML\LIB\WP\User;
/**
* @author OnTheGo Systems
*/
class WPML_TM_AMS_Synchronize_Actions implements IWPML_Action {
const ENABLED_FOR_TRANSLATION_VIA_ATE = 'wpml_enabled_for_translation_via_ate';
/**
* @var WPML_TM_AMS_API
*/
private $ams_api;
/**
* @var WPML_TM_AMS_Users
*/
private $ams_user_records;
/**
* @var WPML_WP_User_Factory $user_factory
*/
private $user_factory;
/**
* @var WPML_TM_AMS_Translator_Activation_Records
*/
private $translator_activation_records;
/** @var int[] */
private $deletedManagerIds = [];
/** @var int[] */
private $deletedTranslatorIds = [];
public function __construct(
WPML_TM_AMS_API $ams_api,
WPML_TM_AMS_Users $ams_user_records,
WPML_WP_User_Factory $user_factory,
WPML_TM_AMS_Translator_Activation_Records $translator_activation_records
) {
$this->ams_api = $ams_api;
$this->ams_user_records = $ams_user_records;
$this->user_factory = $user_factory;
$this->translator_activation_records = $translator_activation_records;
}
public function add_hooks() {
add_action( 'wpml_tm_ate_synchronize_translators', array( $this, 'synchronize_translators' ) );
add_action( 'wpml_update_translator', array( $this, 'synchronize_translators' ) );
add_action( 'wpml_tm_ate_synchronize_managers', array( $this, 'synchronize_managers' ) );
add_action( 'wpml_tm_ate_enable_subscription', array( $this, 'enable_subscription' ) );
add_action( 'delete_user', array( $this, 'prepare_user_deleted' ), 10, 1 );
add_action( 'deleted_user', array( $this, 'user_changed' ), 10, 1 );
add_action( 'profile_update', array( $this, 'user_changed' ), 10, 1 );
}
/**
* @throws \InvalidArgumentException
*/
public function synchronize_translators() {
$result = $this->ams_api->synchronize_translators( $this->ams_user_records->get_translators() );
if ( ! is_wp_error( $result ) ) {
$this->translator_activation_records->update( isset( $result['translators'] ) ? $result['translators'] : array() );
}
}
/**
* @throws \InvalidArgumentException
*/
public function synchronize_managers() {
$this->ams_api->synchronize_managers( $this->ams_user_records->get_managers() );
}
public function enable_subscription( $user_id ) {
$user = $this->user_factory->create( $user_id );
if ( ! $user->get_meta( self::ENABLED_FOR_TRANSLATION_VIA_ATE ) ) {
$this->ams_api->enable_subscription( $user->user_email );
$user->update_meta( self::ENABLED_FOR_TRANSLATION_VIA_ATE, true );
}
}
/**
* @param int $user_id
*/
public function prepare_user_deleted( $user_id ) {
$user = User::get( $user_id );
if ( $user ) {
if ( User::hasCap( User::CAP_ADMINISTRATOR ) || User::hasCap( User::CAP_MANAGE_TRANSLATIONS, $user ) ) {
$this->deletedManagerIds[] = $user_id;
}
if ( User::hasCap( User::CAP_ADMINISTRATOR ) || User::hasCap( User::CAP_TRANSLATE, $user ) ) {
$this->deletedTranslatorIds[] = $user_id;
}
}
}
/**
* @param int $user_id
*/
public function user_changed( $user_id ) {
$user = User::get( $user_id );
if ( $user ) {
if ( in_array( $user_id, $this->deletedManagerIds ) || User::hasCap( User::CAP_ADMINISTRATOR ) || User::hasCap( User::CAP_MANAGE_TRANSLATIONS, $user ) ) {
$this->synchronize_managers();
}
if ( in_array( $user_id, $this->deletedTranslatorIds ) || User::hasCap( User::CAP_ADMINISTRATOR ) || User::hasCap( User::CAP_TRANSLATE, $user ) ) {
$this->synchronize_translators();
}
}
}
}