first commit
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\TM\Menu\TranslationRoles;
|
||||
|
||||
class RoleValidator {
|
||||
/**
|
||||
* @param string $roleName
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public static function getTheHighestPossibleIfNotValid( $roleName ) {
|
||||
$wp_role = get_role( $roleName );
|
||||
$user = wp_get_current_user();
|
||||
if ( \WPML_WP_Roles::get_highest_level( $wp_role->capabilities ) > \WPML_WP_Roles::get_user_max_level( $user ) ) {
|
||||
$wp_role = current( \WPML_WP_Roles::get_roles_up_to_user_level( $user ) );
|
||||
if ( ! $wp_role ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$roleName = $wp_role->name;
|
||||
}
|
||||
|
||||
return $roleName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Translation_Roles_Section_Factory implements IWPML_TM_Admin_Section_Factory {
|
||||
|
||||
/**
|
||||
* @return \WPML_TM_Admin_Section|\WPML_TM_Translation_Roles_Section
|
||||
*/
|
||||
public function create() {
|
||||
global $wpdb;
|
||||
|
||||
$user_query_factory = new WPML_WP_User_Query_Factory();
|
||||
|
||||
$translation_manager_settings = new WPML_Translation_Manager_Settings(
|
||||
new WPML_Translation_Manager_View(),
|
||||
new WPML_Translation_Manager_Records( $wpdb, $user_query_factory, wp_roles() )
|
||||
);
|
||||
|
||||
$translator_settings = new WPML_Translator_Settings_Proxy( array( $this, 'create_translator_settings' ) );
|
||||
|
||||
return new WPML_TM_Translation_Roles_Section( $translation_manager_settings, $translator_settings );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \WPML_Translator_Settings
|
||||
*/
|
||||
public function create_translator_settings() {
|
||||
global $wpdb, $sitepress;
|
||||
|
||||
do_action( 'wpml_tm_ate_synchronize_translators' );
|
||||
|
||||
$user_query_factory = new WPML_WP_User_Query_Factory();
|
||||
|
||||
return new WPML_Translator_Settings(
|
||||
new WPML_Translator_Records( $wpdb, $user_query_factory, wp_roles() ),
|
||||
new WPML_Language_Collection( $sitepress, array_keys( $sitepress->get_active_languages() ) ),
|
||||
$sitepress->get_default_language()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Translation_Roles_Section implements IWPML_TM_Admin_Section {
|
||||
const SLUG = 'translators';
|
||||
|
||||
/**
|
||||
* The WPML_Translation_Manager_Settings instance.
|
||||
*
|
||||
* @var WPML_Translator_Settings_Interface $translator_settings
|
||||
*/
|
||||
private $translator_settings;
|
||||
|
||||
/**
|
||||
* The WPML_Translator_Settings_Interface instance.
|
||||
*
|
||||
* @var WPML_Translation_Manager_Settings $translation_manager_settings
|
||||
*/
|
||||
private $translation_manager_settings;
|
||||
|
||||
/**
|
||||
* WPML_TM_Translation_Roles_Section constructor.
|
||||
*
|
||||
* @param \WPML_Translation_Manager_Settings $translation_manager_settings The WPML_Translation_Manager_Settings instance.
|
||||
* @param \WPML_Translator_Settings_Interface $translator_settings The WPML_Translator_Settings_Interface instance.
|
||||
*/
|
||||
public function __construct(
|
||||
WPML_Translation_Manager_Settings $translation_manager_settings,
|
||||
WPML_Translator_Settings_Interface $translator_settings
|
||||
) {
|
||||
$this->translation_manager_settings = $translation_manager_settings;
|
||||
$this->translator_settings = $translator_settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a value which will be used for sorting the sections.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_order() {
|
||||
return 300;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the unique slug of the sections which is used to build the URL for opening this section.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_slug() {
|
||||
return self::SLUG;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns one or more capabilities required to display this section.
|
||||
*
|
||||
* @return string|array
|
||||
*/
|
||||
public function get_capabilities() {
|
||||
return array( WPML_Manage_Translations_Role::CAPABILITY, 'manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the caption to display in the section.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_caption() {
|
||||
return current_user_can( 'manage_options' ) ?
|
||||
__( 'Translation Roles', 'wpml-translation-management' ) :
|
||||
__( 'Translators', 'wpml-translation-management' );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the callback responsible for rendering the content of the section.
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
public function get_callback() {
|
||||
return array( $this, 'render' );
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is hooked to the `admin_enqueue_scripts` action.
|
||||
*
|
||||
* @param string $hook The current page.
|
||||
*/
|
||||
public function admin_enqueue_scripts( $hook ) {}
|
||||
|
||||
/**
|
||||
* Used to extend the logic for displaying/hiding the section.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_visible() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the content of the section.
|
||||
*/
|
||||
public function render() {
|
||||
echo $this->translator_settings->render();
|
||||
echo $this->translation_manager_settings->render();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
abstract class WPML_TM_Translators_View extends WPML_Twig_Template_Loader {
|
||||
|
||||
protected $model = array();
|
||||
|
||||
/** @var WPML_Translator_Records $user_records */
|
||||
private $user_records;
|
||||
|
||||
/** @var WPML_Language_Collection $active_languages */
|
||||
private $active_languages;
|
||||
|
||||
/** @var WPML_Language_Collection $source_languages */
|
||||
private $source_languages;
|
||||
|
||||
private $default_language;
|
||||
|
||||
public function __construct(
|
||||
WPML_Translator_Records $user_records,
|
||||
WPML_Language_Collection $active_languages,
|
||||
$default_language
|
||||
) {
|
||||
parent::__construct(
|
||||
array_merge(
|
||||
array(
|
||||
WPML_TM_PATH . '/templates/translators',
|
||||
WPML_TM_PATH . '/templates/menus/translation-managers',
|
||||
),
|
||||
$this->get_template_paths()
|
||||
)
|
||||
);
|
||||
|
||||
$this->user_records = $user_records;
|
||||
$this->active_languages = $active_languages;
|
||||
$this->default_language = $default_language;
|
||||
|
||||
$this->source_languages = apply_filters( 'wpml_tm_allowed_source_languages', clone $this->active_languages );
|
||||
}
|
||||
|
||||
public function render( $wizard_mode = false ) {
|
||||
$this->add_users();
|
||||
$this->add_strings();
|
||||
$this->add_add_translator_dialog();
|
||||
$this->add_edit_translator_languages_dialog();
|
||||
$this->add_roles();
|
||||
$this->add_languages();
|
||||
$this->add_nonce();
|
||||
$this->add_capability( $wizard_mode );
|
||||
|
||||
return $this->get_template()->show( $this->model, $this->get_twig_template() );
|
||||
}
|
||||
|
||||
public function add_strings() {
|
||||
|
||||
$this->model['strings'] = array(
|
||||
'title' => __( 'Translators', 'wpml-translation-management' ),
|
||||
'sub_title' => __( 'WPML’s Translation Editor makes it easy for your own translators to translate content in your site. You can create accounts for new translators or use existing WordPress users as your translators.', 'wpml-translation-management' ),
|
||||
'columns' => array(
|
||||
'name' => __( 'Name', 'wpml-translation-management' ),
|
||||
'email' => __( 'Email', 'wpml-translation-management' ),
|
||||
'language_pairs' => __( 'Language Pairs', 'wpml-translation-management' ),
|
||||
),
|
||||
'add_translator' => __( 'Add a Translator', 'wpml-translation-management' ),
|
||||
'go_back' => __( 'Go back', 'wpml-translation-management' ),
|
||||
'continue' => __( 'Continue', 'wpml-translation-management' ),
|
||||
'first_name' => __( 'First name:', 'wpml-translation-management' ),
|
||||
'last_name' => __( 'Last name:', 'wpml-translation-management' ),
|
||||
'email' => __( 'Email:', 'wpml-translation-management' ),
|
||||
'user_name' => __( 'User name:', 'wpml-translation-management' ),
|
||||
'wp_role' => __( 'WordPress role:', 'wpml-translation-management' ),
|
||||
'remove' => __( 'Remove', 'wpml-translation-management' ),
|
||||
'edit_languages' => __( 'Edit Languages', 'wpml-translation-management' ),
|
||||
'skip' => __( "Skip this step - I don't need to add more translators", 'wpml-translation-management' ),
|
||||
'no_capability' => __( "Only Translation Managers can add translators to the site. You can assign a different WordPress user to be the site's Translation Manager or make yourself a Translation Manager.", 'wpml-translation-management' ),
|
||||
'yes' => __( 'Yes', 'wpml-translation-management' ),
|
||||
'no' => __( 'No', 'wpml-translation-management' ),
|
||||
'only_i' => __( 'You can translate to and from all languages', 'wpml-translation-management' ),
|
||||
'remove_me' => __( 'Remove me as a translator', 'wpml-translation-management' ),
|
||||
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
private function add_roles() {
|
||||
$this->model['wp_roles'] = WPML_WP_Roles::get_roles_up_to_user_level( wp_get_current_user() );
|
||||
}
|
||||
|
||||
public function add_add_translator_dialog() {
|
||||
$this->model['add_translator_dialog'] = array(
|
||||
'id' => 'js-add-translator-dialog',
|
||||
'class' => '',
|
||||
'strings' => array(
|
||||
'title' => __( 'Add a Translator', 'wpml-translation-management' ),
|
||||
'set_languages_text' => __( 'Set language pair(s)', 'wpml-translation-management' ),
|
||||
'save_translator_text' => __( 'Save', 'wpml-translation-management' ),
|
||||
'previous_text' => __( 'Previous', 'wpml-translation-management' ),
|
||||
'cancel_text' => __( 'Cancel', 'wpml-translation-management' ),
|
||||
'existing_user' => __( 'Select an existing user and set as Translator', 'wpml-translation-management' ),
|
||||
'new_user' => __( 'Create a new user and set as Translator', 'wpml-translation-management' ),
|
||||
'set_lang' => __( 'Set language pair(s) for Translator %USERNAME%', 'wpml-translation-management' ),
|
||||
'from' => __( 'From', 'wpml-translation-management' ),
|
||||
'to' => __( 'to', 'wpml-translation-management' ),
|
||||
'choose_language' => __( '--Choose language--', 'wpml-translation-management' ),
|
||||
'add_lang_pair_text' => __( 'Add more language pairs for this translator', 'wpml-translation-management' ),
|
||||
),
|
||||
'languages' => $this->active_languages,
|
||||
'source_languages' => $this->source_languages,
|
||||
'default_language' => $this->default_language,
|
||||
'nonce' => $this->get_nonce(),
|
||||
);
|
||||
}
|
||||
|
||||
public function add_edit_translator_languages_dialog() {
|
||||
$this->model['edit_translator_languages_dialog'] = array(
|
||||
'id' => 'js-edit-translator-languages-dialog',
|
||||
'class' => '',
|
||||
'strings' => array(
|
||||
'title' => __( 'Edit language pair(s) for translator', 'wpml-translation-management' ),
|
||||
'save_text' => __( 'Save', 'wpml-translation-management' ),
|
||||
'cancel_text' => __( 'Cancel', 'wpml-translation-management' ),
|
||||
'from' => __( 'From', 'wpml-translation-management' ),
|
||||
'to' => __( 'to', 'wpml-translation-management' ),
|
||||
'choose_language' => __( '--Choose language--', 'wpml-translation-management' ),
|
||||
'add_lang_pair_text' => __( 'Add more language pairs for this translator', 'wpml-translation-management' ),
|
||||
),
|
||||
'languages' => $this->active_languages,
|
||||
'source_languages' => $this->source_languages,
|
||||
'nonce' => $this->get_nonce(),
|
||||
);
|
||||
}
|
||||
|
||||
public function add_users() {
|
||||
global $wpdb;
|
||||
$language_pair_records = new WPML_Language_Pair_Records( $wpdb, new WPML_Language_Records( $wpdb ) );
|
||||
|
||||
$this->model['users'] = array();
|
||||
|
||||
$users = $this->user_records->get_users_with_capability();
|
||||
foreach ( $users as $user ) {
|
||||
$user->language_pairs = $language_pair_records->get( $user->ID );
|
||||
if ( get_user_meta( $user->ID, WPML_TM_Wizard_Options::ONLY_I_USER_META, true ) ) {
|
||||
$user->avatar = get_avatar( $user->ID, 70 );
|
||||
$this->model['only_i_user'] = $user;
|
||||
} else {
|
||||
$this->model['users'][] = $user;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function add_languages() {
|
||||
$this->model['languages'] = $this->source_languages;
|
||||
}
|
||||
|
||||
public function add_nonce() {
|
||||
$this->model['nonce'] = $this->get_nonce();
|
||||
}
|
||||
|
||||
public function add_capability( $wizard_mode ) {
|
||||
$this->model['can_add_translators'] = $wizard_mode || current_user_can( WPML_Manage_Translations_Role::CAPABILITY );
|
||||
}
|
||||
|
||||
private function get_nonce() {
|
||||
return wp_create_nonce( WPML_Translator_Ajax::NONCE_ACTION );
|
||||
}
|
||||
|
||||
abstract public function get_twig_template();
|
||||
|
||||
abstract public function get_template_paths();
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
class WPML_Translation_Manager_Ajax extends WPML_Translation_Roles_Ajax {
|
||||
|
||||
const TRANSLATION_MANAGER_INSTRUCTIONS_TEMPLATE = 'notification/translation-manager-instructions.twig';
|
||||
|
||||
/** @var WPML_TM_Email_Notification_View $email_view */
|
||||
private $email_view;
|
||||
|
||||
public function __construct(
|
||||
IWPML_Translation_Roles_View $view,
|
||||
WPML_Translation_Roles_Records $records,
|
||||
WPML_Super_Globals_Validation $post_vars,
|
||||
WPML_WP_User_Factory $user_factory,
|
||||
WPML_TM_Email_Notification_View $email_view
|
||||
) {
|
||||
parent::__construct( $view, $records, $post_vars, $user_factory );
|
||||
$this->email_view = $email_view;
|
||||
}
|
||||
|
||||
public function get_role() {
|
||||
return 'manager';
|
||||
}
|
||||
|
||||
public function get_nonce() {
|
||||
return WPML_Translation_Manager_Settings::NONCE_ACTION;
|
||||
}
|
||||
|
||||
public function get_capability() {
|
||||
return WPML_Manage_Translations_Role::CAPABILITY;
|
||||
}
|
||||
|
||||
public function get_user_row_template() {
|
||||
return 'translation-managers-row.twig';
|
||||
}
|
||||
|
||||
public function on_user_created( WP_User $user ) {
|
||||
|
||||
}
|
||||
|
||||
public function on_remove_role( WP_User $user ) {
|
||||
|
||||
}
|
||||
|
||||
public function send_instructions_to_user( WP_User $user ) {
|
||||
$site_name = get_option( 'blogname' );
|
||||
$translation_setup_url = admin_url( 'admin.php?page=' . WPML_TM_FOLDER . '/menu/main.php' );
|
||||
$admin_user = wp_get_current_user();
|
||||
|
||||
$model = array(
|
||||
'setup_url' => esc_url( $translation_setup_url ),
|
||||
'username' => $user->display_name,
|
||||
'intro_message_1' => sprintf( __( 'You are the Translation Manager for %s. This role lets you manage everything related to translation for this site.', 'wpml-translation-management' ), $site_name ),
|
||||
'intro_message_2' => __( 'Before you can start sending content to translation, you need to complete a short setup.', 'wpml-translation-management' ),
|
||||
'setup' => __( 'Set-up the translation', 'wpml-translation-management' ),
|
||||
'reminder' => sprintf( __( '* Remember, your login name for %1$s is %2$s. If you need help with your password, use the password reset in the login page.', 'wpml-translation-management' ), $site_name, $user->user_login ),
|
||||
'at_your_service' => __( 'At your service', 'wpml-translation-management' ),
|
||||
'admin_name' => $admin_user->display_name,
|
||||
'admin_for_site' => sprintf( __( 'Administrator for %s', 'wpml-translation-management' ), $site_name ),
|
||||
);
|
||||
|
||||
$to = $user->display_name . ' <' . $user->user_email . '>';
|
||||
$message = $this->email_view->render_model( $model, self::TRANSLATION_MANAGER_INSTRUCTIONS_TEMPLATE );
|
||||
$subject = sprintf( __( 'You are now the Translation Manager for %s - action needed', 'wpml-translation-management' ), $site_name );
|
||||
|
||||
$headers = array(
|
||||
'MIME-Version: 1.0',
|
||||
'Content-type: text/html; charset=UTF-8',
|
||||
'Reply-To: ' . $admin_user->display_name . ' <' . $admin_user->user_email . '>',
|
||||
);
|
||||
|
||||
add_filter( 'wp_mail_from_name', array( $this, 'wp_mail_from_name_filter' ), 10, 1 );
|
||||
wp_mail( $to, $subject, $message, $headers );
|
||||
remove_filter( 'wp_mail_from_name', array( $this, 'wp_mail_from_name_filter' ), 10 );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function wp_mail_from_name_filter( $from_name ) {
|
||||
$admin_user = wp_get_current_user();
|
||||
|
||||
return $admin_user->display_name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
class WPML_Translation_Manager_Settings {
|
||||
|
||||
const MAIN_TEMPLATE = 'translation-managers.twig';
|
||||
const NONCE_ACTION = 'wpml_translation_manager_actions';
|
||||
|
||||
/** @var WPML_Translation_Manager_View $view */
|
||||
private $view;
|
||||
|
||||
/** @var WPML_Translation_Manager_Records $records */
|
||||
private $records;
|
||||
|
||||
public function __construct(
|
||||
WPML_Translation_Manager_View $view,
|
||||
WPML_Translation_Manager_Records $records
|
||||
) {
|
||||
$this->view = $view;
|
||||
$this->records = $records;
|
||||
}
|
||||
|
||||
public function render() {
|
||||
if ( current_user_can( 'manage_options' ) ) {
|
||||
return $this->view->show( $this->get_model(), self::MAIN_TEMPLATE );
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
public function get_model() {
|
||||
$current_user = wp_get_current_user();
|
||||
|
||||
return array(
|
||||
'translation_managers' => $this->get_translation_managers(),
|
||||
'nonce' => wp_create_nonce( self::NONCE_ACTION ),
|
||||
'user_id' => $current_user->ID,
|
||||
'wp_roles' => WPML_WP_Roles::get_editor_roles(),
|
||||
);
|
||||
}
|
||||
|
||||
private function get_translation_managers() {
|
||||
$users = $this->records->get_users_with_capability();
|
||||
|
||||
foreach ( $users as $user ) {
|
||||
$user->edit_link = esc_url(
|
||||
add_query_arg(
|
||||
'wp_http_referer',
|
||||
urlencode( esc_url( stripslashes( $_SERVER['REQUEST_URI'] ) ) ),
|
||||
"user-edit.php?user_id={$user->ID}"
|
||||
)
|
||||
);
|
||||
$user->avatar = get_avatar( $user->ID, 70 );
|
||||
}
|
||||
|
||||
return $users;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
class WPML_Translation_Manager_View extends WPML_Twig_Template_Loader implements IWPML_Translation_Roles_View {
|
||||
|
||||
const TEMPLATE_PATH = '/templates/menus/translation-managers';
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct( array( WPML_TM_PATH . self::TEMPLATE_PATH ) );
|
||||
}
|
||||
|
||||
public function show( $model, $template ) {
|
||||
$model['strings'] = self::get_strings();
|
||||
$model['dialog'] = self::add_add_translation_manager_dialog();
|
||||
|
||||
return $this->get_template()->show( $model, $template );
|
||||
}
|
||||
|
||||
public static function get_strings() {
|
||||
return array(
|
||||
'title' => __( 'Translation Managers', 'wpml-translation-management' ),
|
||||
'column_name' => __( 'Name', 'wpml-translation-management' ),
|
||||
'edit' => __( 'Edit user', 'wpml-translation-management' ),
|
||||
'remove' => __( 'Remove Translation Manager', 'wpml-translation-management' ),
|
||||
'no_users' => __( "This site doesn't yet have a Translation Manager.", 'wpml-translation-management' ),
|
||||
'placeholder' => __( 'Search for user', 'wpml-translation-management' ),
|
||||
'add_button' => __( 'Add a Translation Manager', 'wpml-translation-management' ),
|
||||
'first_name' => __( 'First name:', 'wpml-translation-management' ),
|
||||
'last_name' => __( 'Last name:', 'wpml-translation-management' ),
|
||||
'email' => __( 'Email:', 'wpml-translation-management' ),
|
||||
'user_name' => __( 'User name:', 'wpml-translation-management' ),
|
||||
'wp_role' => __( 'WordPress role:', 'wpml-translation-management' ),
|
||||
'existing_user' => __( 'Select an existing WordPress user (needs to be an editor or above)', 'wpml-translation-management' ),
|
||||
'new_user' => __( 'Create a new user to be the Translation Manager', 'wpml-translation-management' ),
|
||||
'set_user' => __( 'Set a Translation Manager', 'wpml-translation-management' ),
|
||||
'add_user' => __( 'Add a Translation Manager', 'wpml-translation-management' ),
|
||||
);
|
||||
}
|
||||
|
||||
public static function add_add_translation_manager_dialog() {
|
||||
return array(
|
||||
'id' => 'js-add-translation-manager-dialog',
|
||||
'class' => '',
|
||||
'nonce' => wp_create_nonce( WPML_Translation_Manager_Settings::NONCE_ACTION ),
|
||||
'strings' => array(
|
||||
'title' => __( 'Add a Translation Manager', 'wpml-translation-management' ),
|
||||
'add_text' => __( 'Add a Translation Manager', 'wpml-translation-management' ),
|
||||
'cancel_text' => __( 'Cancel', 'wpml-translation-management' ),
|
||||
'existing_user' => __( 'Select an existing user and set as Translation Manager', 'wpml-translation-management' ),
|
||||
'new_user' => __( 'Create a new user and set as Translation Manager', 'wpml-translation-management' ),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
abstract class WPML_Translation_Roles_Ajax extends WPML_TM_AJAX implements IWPML_Action {
|
||||
|
||||
const USER_SEARCH_LIMIT = 10;
|
||||
|
||||
/** @var IWPML_Translation_Roles_View $view */
|
||||
private $view;
|
||||
|
||||
/** @var WPML_Translation_Roles_Records $records */
|
||||
private $records;
|
||||
|
||||
/** @var WPML_Super_Globals_Validation $post_vars */
|
||||
protected $post_vars;
|
||||
|
||||
/** @var WPML_WP_User_Factory $user_factory */
|
||||
private $user_factory;
|
||||
|
||||
public function __construct(
|
||||
IWPML_Translation_Roles_View $view,
|
||||
WPML_Translation_Roles_Records $records,
|
||||
WPML_Super_Globals_Validation $post_vars,
|
||||
WPML_WP_User_Factory $user_factory
|
||||
) {
|
||||
$this->view = $view;
|
||||
$this->records = $records;
|
||||
$this->post_vars = $post_vars;
|
||||
$this->user_factory = $user_factory;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
$role = $this->get_role();
|
||||
add_action( 'wp_ajax_wpml_remove_translation_' . $role, array( $this, 'remove_translation_role' ) );
|
||||
add_action( 'wp_ajax_wpml_search_translation_' . $role, array( $this, 'search_for_translation_roles' ) );
|
||||
add_action( 'wp_ajax_wpml_add_translation_' . $role, array( $this, 'add_translation_role' ) );
|
||||
add_action( 'wp_ajax_wpml_send_instructions_to_translation_' . $role, array( $this, 'send_instructions' ) );
|
||||
}
|
||||
|
||||
public function remove_translation_role() {
|
||||
if ( $this->is_valid_request( $this->get_nonce() ) && $user = $this->get_user() ) {
|
||||
$user->remove_cap( $this->get_capability() );
|
||||
$this->on_remove_role( $user );
|
||||
do_action( 'wpml_tm_ate_synchronize_' . $this->get_role() . 's' );
|
||||
do_action( 'wpml_tm_remove_translation_role', $user, $this->get_capability() );
|
||||
wp_send_json_success();
|
||||
} else {
|
||||
wp_send_json_error( __( 'Could not find user!', 'wpml-translation-management' ) );
|
||||
}
|
||||
}
|
||||
|
||||
public function search_for_translation_roles() {
|
||||
if ( $this->is_valid_request( $this->get_nonce() ) ) {
|
||||
$results = $this->records->search_for_users_without_capability( $this->post_vars->post( 'search' ), self::USER_SEARCH_LIMIT );
|
||||
|
||||
wp_send_json_success( $results );
|
||||
}
|
||||
}
|
||||
|
||||
public function add_translation_role() {
|
||||
if ( $this->is_valid_request( $this->get_nonce() ) && $user = $this->get_user() ) {
|
||||
|
||||
if ( ! is_wp_error( $user ) ) {
|
||||
$user->add_cap( $this->get_capability() );
|
||||
|
||||
$this->on_user_created( $user );
|
||||
|
||||
$user->data->edit_link = esc_url( "user-edit.php?user_id={$user->ID}" );
|
||||
$user->data->avatar = get_avatar( $user->ID );
|
||||
|
||||
$new_row = $this->view->show(
|
||||
array(
|
||||
'user' => (array) $user->data,
|
||||
),
|
||||
$this->get_user_row_template()
|
||||
);
|
||||
|
||||
do_action( 'wpml_tm_ate_synchronize_' . $this->get_role() . 's' );
|
||||
do_action( 'wpml_tm_add_translation_role', $user, $this->get_capability() );
|
||||
|
||||
if ( $this->post_vars->post( 'sendEmail', FILTER_VALIDATE_BOOLEAN ) ) {
|
||||
$this->send_instructions_to_user( $user );
|
||||
}
|
||||
|
||||
wp_send_json_success( $new_row );
|
||||
} else {
|
||||
wp_send_json_error( $user->get_error_message() );
|
||||
}
|
||||
} else {
|
||||
wp_send_json_error( __( 'Could not find user!', 'wpml-translation-management' ) );
|
||||
}
|
||||
}
|
||||
|
||||
public function send_instructions() {
|
||||
if ( $this->is_valid_request( $this->get_nonce() ) && $user = $this->get_user() ) {
|
||||
if ( $this->send_instructions_to_user( $user ) ) {
|
||||
wp_send_json_success( sprintf( __( 'An email has been sent to %s', 'wpml-translation-management' ), $user->user_login ) );
|
||||
} else {
|
||||
wp_send_json_error( __( 'Failed to send email', 'wpml-translation-management' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function get_user() {
|
||||
$user_id = $this->post_vars->post( 'user_id', FILTER_SANITIZE_NUMBER_INT );
|
||||
if ( $user_id ) {
|
||||
return $this->user_factory->create( $user_id );
|
||||
} else {
|
||||
return $this->create_new_wp_user();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|WP_Error|WP_User
|
||||
*/
|
||||
private function create_new_wp_user() {
|
||||
$first_name = $this->post_vars->post( 'first' );
|
||||
$last_name = $this->post_vars->post( 'last' );
|
||||
$email = $this->post_vars->post( 'email', FILTER_SANITIZE_EMAIL );
|
||||
$user_name = $this->post_vars->post( 'user' );
|
||||
$role = \WPML\TM\Menu\TranslationRoles\RoleValidator::getTheHighestPossibleIfNotValid(
|
||||
$this->post_vars->post( 'role' )
|
||||
);
|
||||
|
||||
if ( $email && $user_name && $role ) {
|
||||
$user_id = wp_insert_user(
|
||||
array(
|
||||
'first_name' => $first_name,
|
||||
'last_name' => $last_name,
|
||||
'user_email' => $email,
|
||||
'user_login' => $user_name,
|
||||
'role' => $role,
|
||||
'user_pass' => wp_generate_password(),
|
||||
)
|
||||
);
|
||||
|
||||
if ( ! is_wp_error( $user_id ) ) {
|
||||
wp_send_new_user_notifications( $user_id );
|
||||
|
||||
return $this->user_factory->create( $user_id );
|
||||
} else {
|
||||
return $user_id;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
abstract public function get_role();
|
||||
abstract public function get_nonce();
|
||||
abstract public function get_capability();
|
||||
abstract public function get_user_row_template();
|
||||
abstract public function on_user_created( WP_User $user );
|
||||
abstract public function on_remove_role( WP_User $user );
|
||||
abstract public function send_instructions_to_user( WP_User $user );
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
class WPML_Translation_Roles_Ajax_Factory implements IWPML_AJAX_Action_Loader {
|
||||
|
||||
/**
|
||||
* @return \IWPML_Action|\IWPML_Action[]
|
||||
*/
|
||||
public function create() {
|
||||
global $wpdb, $sitepress;
|
||||
|
||||
$email_twig_factory = new WPML_TM_Email_Twig_Template_Factory();
|
||||
|
||||
$hooks = array(
|
||||
|
||||
new WPML_Translation_Manager_Ajax(
|
||||
new WPML_Translation_Manager_View(),
|
||||
new WPML_Translation_Manager_Records( $wpdb, new WPML_WP_User_Query_Factory(), wp_roles() ),
|
||||
new WPML_Super_Globals_Validation(),
|
||||
new WPML_WP_User_Factory(),
|
||||
new WPML_TM_Email_Notification_View( $email_twig_factory->create() )
|
||||
),
|
||||
|
||||
new WPML_Translator_Ajax(
|
||||
new WPML_Translator_View(
|
||||
apply_filters(
|
||||
'wpml_tm_allowed_source_languages',
|
||||
new WPML_Language_Collection( $sitepress, array_keys( $sitepress->get_active_languages() ) )
|
||||
)
|
||||
),
|
||||
new WPML_Translator_Records( $wpdb, new WPML_WP_User_Query_Factory(), wp_roles() ),
|
||||
new WPML_Super_Globals_Validation(),
|
||||
new WPML_WP_User_Factory(),
|
||||
new WPML_Language_Pair_Records( $wpdb, new WPML_Language_Records( $wpdb ) )
|
||||
),
|
||||
|
||||
);
|
||||
|
||||
return $hooks;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
interface IWPML_Translation_Roles_View {
|
||||
|
||||
public function show( $model, $template );
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
class WPML_Translator_Ajax extends WPML_Translation_Roles_Ajax {
|
||||
|
||||
const NONCE_ACTION = 'wpml_translator_actions';
|
||||
|
||||
/** @var WPML_Language_Pair_Records $language_pair_records */
|
||||
private $language_pair_records;
|
||||
|
||||
public function __construct(
|
||||
IWPML_Translation_Roles_View $view,
|
||||
WPML_Translation_Roles_Records $records,
|
||||
WPML_Super_Globals_Validation $post_vars,
|
||||
WPML_WP_User_Factory $user_factory,
|
||||
WPML_Language_Pair_Records $language_pair_records
|
||||
) {
|
||||
parent::__construct( $view, $records, $post_vars, $user_factory );
|
||||
$this->language_pair_records = $language_pair_records;
|
||||
}
|
||||
|
||||
public function get_role() {
|
||||
return 'translator';
|
||||
}
|
||||
|
||||
public function get_nonce() {
|
||||
return self::NONCE_ACTION;
|
||||
}
|
||||
|
||||
public function get_capability() {
|
||||
return WPML_Translator_Role::CAPABILITY;
|
||||
}
|
||||
|
||||
public function get_user_row_template() {
|
||||
return 'translators-row.twig';
|
||||
}
|
||||
|
||||
public function on_user_created( WP_User $user ) {
|
||||
$language_pairs = $this->post_vars->post( 'languagePairs' );
|
||||
$this->language_pair_records->store( $user->ID, $language_pairs );
|
||||
$user->data->language_pairs = $this->post_vars->post( 'languagePairs' );
|
||||
}
|
||||
|
||||
public function on_remove_role( WP_User $user ) {
|
||||
delete_user_meta( $user->ID, WPML_TM_Wizard_Options::ONLY_I_USER_META );
|
||||
}
|
||||
|
||||
public function send_instructions_to_user( WP_User $user ) {
|
||||
// Not needed at this stage.
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
class WPML_Translator_View extends WPML_Twig_Template_Loader implements IWPML_Translation_Roles_View {
|
||||
|
||||
const TEMPLATE_PATH = '/templates/translators';
|
||||
|
||||
/** @var WPML_Language_Collection $languages */
|
||||
private $languages;
|
||||
|
||||
public function __construct( WPML_Language_Collection $languages ) {
|
||||
$this->languages = $languages;
|
||||
parent::__construct( array( WPML_TM_PATH . self::TEMPLATE_PATH ) );
|
||||
}
|
||||
|
||||
public function show( $model, $template ) {
|
||||
$model['strings'] = self::get_strings();
|
||||
$model['languages'] = $this->languages;
|
||||
|
||||
$model['strings'] = apply_filters(
|
||||
'wpml_tm_translators_view_strings',
|
||||
$model['strings'],
|
||||
true
|
||||
);
|
||||
|
||||
return $this->get_template()->show( $model, $template );
|
||||
}
|
||||
|
||||
public static function get_strings() {
|
||||
return array(
|
||||
'edit' => __( 'Edit user', 'wpml-translation-management' ),
|
||||
'remove' => __( 'Remove', 'wpml-translation-management' ),
|
||||
'edit_languages' => __( 'Edit Languages', 'wpml-translation-management' ),
|
||||
'no' => __( 'No', 'wpml-translation-management' ),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
interface WPML_Translator_Settings_Interface {
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function render();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
class WPML_Translator_Settings_Proxy implements WPML_Translator_Settings_Interface {
|
||||
/** @var callable */
|
||||
private $create_callback;
|
||||
|
||||
/**
|
||||
* @param callable $create_callback
|
||||
*/
|
||||
public function __construct( $create_callback ) {
|
||||
$this->create_callback = $create_callback;
|
||||
}
|
||||
|
||||
public function render() {
|
||||
$translator_settings = call_user_func( $this->create_callback );
|
||||
if ( ! $translator_settings instanceof WPML_Translator_Settings_Interface ) {
|
||||
throw new RuntimeException( 'Factory method created an invalid object.' );
|
||||
}
|
||||
|
||||
return $translator_settings->render();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
class WPML_Translator_Settings extends WPML_TM_Translators_View implements WPML_Translator_Settings_Interface {
|
||||
|
||||
public function get_twig_template() {
|
||||
return 'translators.twig';
|
||||
}
|
||||
|
||||
public function get_template_paths() {
|
||||
return array();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user