ams_api = $ams_api; $this->ams_user_records = $ams_user_records; $this->user_factory = $user_factory; $this->translator_activation_records = $translator_activation_records; $this->tm_records = $tm_records; $this->translator_records = $translator_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 ) { if ( $this->tm_records->does_user_have_capability( $user_id ) ) { $this->deletedManagerIds[] = $user_id; } if ( $this->translator_records->does_user_have_capability( $user_id ) ) { $this->deletedTranslatorIds[] = $user_id; } } /** * @param int $user_id */ public function user_changed( $user_id ) { if ( in_array( $user_id, $this->deletedManagerIds ) || $this->tm_records->does_user_have_capability( $user_id ) ) { $this->synchronize_managers(); } if ( in_array( $user_id, $this->deletedTranslatorIds ) || $this->translator_records->does_user_have_capability( $user_id ) ) { $this->synchronize_translators(); } } }