<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[1419] sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles: Rosetta: Extend roles plugin to allow per-project permissions for validators.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/1419">1419</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/1419","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>ocean90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-03-19 14:48:45 +0000 (Thu, 19 Mar 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Rosetta: Extend roles plugin to allow per-project permissions for validators.

see <a href="http://meta.trac.wordpress.org/ticket/741">#741</a>, <a href="http://meta.trac.wordpress.org/ticket/519">#519</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkglobalwordpressorgpublic_htmlwpcontentmupluginsrolesrosettarolesphp">sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/rosetta-roles.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkglobalwordpressorgpublic_htmlwpcontentmupluginsrolesclasstranslationeditorslisttablephp">sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/class-translation-editors-list-table.php</a></li>
<li>sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/js/</li>
<li><a href="#sitestrunkglobalwordpressorgpublic_htmlwpcontentmupluginsrolesjsrosettarolesjs">sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/js/rosetta-roles.js</a></li>
<li>sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/views/</li>
<li><a href="#sitestrunkglobalwordpressorgpublic_htmlwpcontentmupluginsrolesviewsedittranslationeditorphp">sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/views/edit-translation-editor.php</a></li>
<li><a href="#sitestrunkglobalwordpressorgpublic_htmlwpcontentmupluginsrolesviewstranslationeditorsphp">sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/views/translation-editors.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkglobalwordpressorgpublic_htmlwpcontentmupluginsrolesclasstranslationeditorslisttablephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/class-translation-editors-list-table.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/class-translation-editors-list-table.php                         (rev 0)
+++ sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/class-translation-editors-list-table.php   2015-03-19 14:48:45 UTC (rev 1419)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,234 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+if ( ! class_exists( 'WP_List_Table' ) ) {
+       require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
+}
+
+class Rosetta_Translation_Editors_List_Table extends WP_List_Table {
+
+       /**
+        * Holds the role of a translation editor.
+        *
+        * @var string
+        */
+       public $user_role;
+
+       /**
+        * Holds the meta key of the project access list.
+        *
+        * @var string
+        */
+       public $project_access_meta_key;
+
+       /**
+        * Whether the current user can promote users.
+        *
+        * @var bool
+        */
+       public $user_can_promote;
+
+       /**
+        * Holds the list of all projects.
+        *
+        * @var array
+        */
+       public $projects;
+
+       /**
+        * Constructor.
+        *
+        * @param array $args An associative array of arguments.
+        */
+       public function __construct( $args = array() ) {
+               global $wpdb;
+
+               parent::__construct( array(
+                       'singular' => 'translation-editor',
+                       'plural'   => 'translation-editors',
+                       'screen'   => isset( $args['screen'] ) ? $args['screen'] : null,
+               ) );
+
+               $this->user_role = $args['user_role'];
+               $this->project_access_meta_key = $args['project_access_meta_key'];
+               $this->projects = $args['projects'];
+               $this->user_can_promote = current_user_can( 'promote_users' );
+       }
+
+       /**
+        * Prepare the list for display.
+        */
+       public function prepare_items() {
+               $search = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
+               $per_page = 10;
+               $paged = $this->get_pagenum();
+
+               $args = array(
+                       'number' => $per_page,
+                       'offset' => ( $paged - 1 ) * $per_page,
+                       'role'   => $this->user_role,
+                       'search' => $search,
+                       'fields' => 'all_with_meta'
+               );
+
+               if ( '' !== $args['search'] ) {
+                       $args['search'] = '*' . $args['search'] . '*';
+               }
+
+               if ( isset( $_REQUEST['orderby'] ) ) {
+                       $args['orderby'] = $_REQUEST['orderby'];
+               }
+
+               if ( isset( $_REQUEST['order'] ) ) {
+                       $args['order'] = $_REQUEST['order'];
+               }
+
+               $user_query = new WP_User_Query( $args );
+               $this->items = $user_query->get_results();
+
+               $this->set_pagination_args( array(
+                       'total_items' => $user_query->get_total(),
+                       'per_page'    => $per_page,
+               ) );
+       }
+
+       /**
+        * Output 'no users' message.
+        */
+       public function no_items() {
+               _e( 'No translation editors were found.', 'rosetta' );
+       }
+
+       /**
+        * Get a list of columns for the list table.
+        *
+        * @return array Array in which the key is the ID of the column,
+        *               and the value is the description.
+        */
+       public function get_columns() {
+               return array(
+                       'cb'       => '<input type="checkbox">',
+                       'username' => __( 'Username', 'rosetta' ),
+                       'name'     => __( 'Name', 'rosetta' ),
+                       'email'    => __( 'E-mail', 'rosetta' ),
+                       'projects' => __( 'Projects', 'rosetta' ),
+               );
+       }
+
+       /**
+        * Get a list of sortable columns for the list table.
+        *
+        * @return array Array of sortable columns.
+        */
+       protected function get_sortable_columns() {
+               return array(
+                       'username' => 'login',
+                       'name'     => 'name',
+                       'email'    => 'email',
+               );
+       }
+
+       /**
+        * Return a list of bulk actions available on this table.
+        *
+        * @return array Array of bulk actions.
+        */
+       protected function get_bulk_actions() {
+               return array(
+                       'remove' => _x( 'Remove', 'translation editor', 'rosetta' ),
+               );
+       }
+
+       /**
+        * Generates content for a single row of the table.
+        *
+        * @param WP_User $user The current user.
+        */
+       public function single_row( $user ) {
+               $user->filter = 'display';
+               parent::single_row( $user );
+       }
+
+       /**
+        * Prints the checkbox column.
+        *
+        * @param WP_User $user The current user.
+        */
+       public function column_cb( $user ) {
+               if ( $this->user_can_promote ) {
+                       ?>
+                       <label class="screen-reader-text" for="cb-select-<?php echo $user->ID; ?>"><?php _e( 'Select translation editor', 'rosetta' ); ?></label>
+                       <input id="cb-select-<?php echo $user->ID; ?>" type="checkbox" name="translation-editors[]" value="<?php echo $user->ID; ?>">
+                       <?php
+               }
+       }
+
+       /**
+        * Prints the username column.
+        *
+        * @param WP_User $user The current user.
+        */
+       public function column_username( $user ) {
+               $avatar = get_avatar( $user->ID, 32 );
+
+               if ( $this->user_can_promote ) {
+                       $page_url = menu_page_url( 'translation-editors', false );
+                       $edit_link = esc_url( add_query_arg( 'user_id', $user->ID, $page_url ) );
+                       $edit = "<strong><a href=\"$edit_link\">$user->user_login</a></strong>";
+
+                       $actions = array();
+                       $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit', 'rosetta' ) . '</a>';
+                       $actions['remove'] = '<a href="' . wp_nonce_url( $page_url . "&amp;action=remove-translation-editor&amp;translation-editor=$user->ID", 'remove-translation-editor' ) . '">' . __( 'Remove', 'rosetta' ) . '</a>';
+                       $edit .= $this->row_actions( $actions );
+               } else {
+                       $edit = "<strong>$user->user_login</strong>";
+               }
+
+               echo "$avatar $edit";
+       }
+
+       /**
+        * Prints the name column.
+        *
+        * @param WP_User $user The current user.
+        */
+       public function column_name( $user ) {
+               echo "$user->first_name $user->last_name";
+       }
+
+       /**
+        * Prints the email column.
+        *
+        * @param WP_User $user The current user.
+        */
+       public function column_email( $user ) {
+               echo "<a href='mailto:$user->user_email'>$user->user_email</a>";
+       }
+
+       /**
+        * Prints the projects column.
+        *
+        * @param WP_User $user The current user.
+        */
+       public function column_projects( $user ) {
+               $project_access_list = $user->get( $this->project_access_meta_key );
+
+               if ( empty( $project_access_list ) ) {
+                       _e( 'No projects', 'rosetta' );
+                       return;
+               }
+
+               if ( in_array( 'all', $project_access_list ) ) {
+                       _e( 'All projects', 'rosetta' );
+                       return;
+               }
+
+               $projects = array();
+               foreach ( $project_access_list as $project_id ) {
+                       if ( $this->projects[ $project_id ] ) {
+                               $projects[] = esc_html( $this->projects[ $project_id ]->name );
+                       }
+               }
+
+               echo implode( '<br>', $projects );
+       }
+}
</ins></span></pre></div>
<a id="sitestrunkglobalwordpressorgpublic_htmlwpcontentmupluginsrolesjsrosettarolesjs"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/js/rosetta-roles.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/js/rosetta-roles.js                              (rev 0)
+++ sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/js/rosetta-roles.js        2015-03-19 14:48:45 UTC (rev 1419)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,44 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+( function( $ ) {
+
+       $(function() {
+               var $projects = $( 'input.project' );
+
+               if ( $projects.length ) {
+                       var $allProjects = $( '#project-all' ),
+                               checked = [];
+
+                       // Deselect "All" if a project is checked.
+                       $projects.on( 'change', function() {
+                               $allProjects.prop( 'checked', false );
+                               checked = [];
+                       } );
+
+                       // (De)select projects if "All" is (de)selected.
+                       $allProjects.on( 'change', function() {
+                               if ( this.checked ) {
+                                       $projects.each( function( index, checkbox ) {
+                                               var $cb = $( checkbox );
+                                               if ( $cb.prop( 'checked' ) ) {
+                                                       checked.push( $cb.attr( 'id' ) );
+                                                       $cb.prop( 'checked', false );
+                                               }
+                                       } );
+                               } else {
+                                       for ( i = 0; i < checked.length; i++ ) {
+                                               $( '#' +  checked[ i ] ).prop( 'checked', true );
+                                       }
+                                       checked = [];
+                               }
+                       } );
+
+                       // Deselect all checkboxes.
+                       $( '#clear-all' ).on( 'click', function( event ) {
+                               event.preventDefault();
+
+                               checked = [];
+                               $allProjects.prop( 'checked', false );
+                               $projects.prop( 'checked', false );
+                       } );
+               }
+       } );
+} )( jQuery );
</ins></span></pre></div>
<a id="sitestrunkglobalwordpressorgpublic_htmlwpcontentmupluginsrolesrosettarolesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/rosetta-roles.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/rosetta-roles.php        2015-03-19 14:08:20 UTC (rev 1418)
+++ sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/rosetta-roles.php  2015-03-19 14:48:45 UTC (rev 1419)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,44 +1,538 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Plugin Name: Rosetta Roles
+ * Plugin URI: https://wordpress.org/
+ * Description: WordPress interface for managing roles.
+ * Author: ocean90
+ * Version: 1.0
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-add_filter( 'gettext_with_context', 'ros_rename_user_roles', 10, 4 );
-function ros_rename_user_roles( $translated, $text, $context, $domain ) {
-       if ( $domain !== 'default' || $context !== 'User role' ) {
-               return $translated;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+class Rosetta_Roles {
+       /**
+        * Endpoint for profiles.wordpress.org updates.
+        */
+       const PROFILES_HANDLER_URL = 'https://profiles.wordpress.org/wp-admin/admin-ajax.php';
+
+       /**
+        * Holds the role of a translation editor.
+        *
+        * @var string
+        */
+       public $translation_editor_role = 'translation_editor';
+
+       /**
+        * Holds the meta key of the project access list.
+        *
+        * @var string
+        */
+       public $project_access_meta_key = 'translation_editor_project_access_list';
+
+       /**
+        * Constructor.
+        */
+       public function __construct() {
+               add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( 'Validator' === $text ) {
-               return __( 'Validator', 'rosetta' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Attaches hooks once plugins are loaded.
+        */
+       public function plugins_loaded() {
+               add_filter( 'editable_roles', array( $this, 'editable_roles' ) );
+               add_filter( 'manage_users_columns',  array( $this, 'add_roles_column' ) );
+               add_filter( 'manage_users_custom_column',  array( $this, 'display_user_roles' ), 10, 3 );
+               add_action( 'admin_init', array( $this, 'role_modifications' ) );
+               add_action( 'set_user_role', array( $this, 'restore_translation_editor_role' ), 10, 3 );
+               add_filter( 'gettext_with_context', array( $this, 'rename_user_roles' ), 10, 4 );
+               add_action( 'user_row_actions', array( $this, 'user_row_action_role_editor' ), 10, 2 );
+               add_action( 'admin_menu', array( $this, 'register_translation_editors_page' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        return $translated;
-}
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-add_action( 'admin_menu', 'ros_remove_widgets_menu' );
-function ros_remove_widgets_menu() {
-       remove_submenu_page( 'themes.php', 'themes.php' );
-       remove_submenu_page( 'themes.php', 'widgets.php' );
-}
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Registers "Translation Editor" role and modifies editor role.
+        */
+       public function role_modifications() {
+               if ( ! get_role( $this->translation_editor_role ) ) {
+                       add_role( $this->translation_editor_role, __( 'Translation Editor', 'rosetta' ), array( 'read' => true, 'level_0' => true ) );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-add_filter( 'editable_roles', 'ros_editable_roles' );
-function ros_editable_roles( $roles ) {
-       $subscriber = $roles['subscriber'];
-       unset( $roles['subscriber'] );
-       reset( $roles );
-       $roles['subscriber'] = $subscriber;
-       if ( ! is_super_admin() && ! is_main_site() ) {
-               unset( $roles['administrator'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $editor_role = get_role( 'editor' );
+               if ( $editor_role && ! $editor_role->has_cap( 'remove_users' ) ) {
+                       $editor_role->add_cap( 'edit_theme_options' );
+                       $editor_role->add_cap( 'list_users' );
+                       $editor_role->add_cap( 'promote_users' );
+                       $editor_role->add_cap( 'remove_users' );
+               }
+
+               // Remove deprecated validator role.
+               /*$validator_role = get_role( 'validator' );
+               if ( $validator_role ) {
+                       remove_role( 'validator' );
+               }*/
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        return $roles;
-}
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-add_filter( 'admin_init', 'ros_role_modifications' );
-function ros_role_modifications() {
-       if ( ! get_role( 'validator' ) ) {
-               add_role( 'validator', __( 'Validator', 'rosetta' ), array( 'read' => true, 'level_0' => true ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Restores the "Translation Editor" role if an user is promoted.
+        *
+        * @param int    $user_id   The user ID.
+        * @param string $role      The new role.
+        * @param array  $old_roles An array of the user's previous roles.
+        */
+       public function restore_translation_editor_role( $user_id, $role, $old_roles ) {
+               if ( ! in_array( $this->translation_editor_role, $old_roles ) ) {
+                       return;
+               }
+
+               $user = new WP_User( $user_id );
+               $user->add_role( $this->translation_editor_role );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $editor_role = get_role( 'editor' );
-       if ( $editor_role && ! $editor_role->has_cap( 'remove_users' ) ) {
-               $editor_role->add_cap( 'edit_theme_options' );
-               $editor_role->add_cap( 'list_users' );
-               $editor_role->add_cap( 'promote_users' );
-               $editor_role->add_cap( 'remove_users' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Removes "Translation Editor" role and "Administrator" role from
+        * the list of editable roles.
+        *
+        * The list used in wp_dropdown_roles() on users list table.
+        *
+        * @param array $all_roles List of roles.
+        * @return array Filtered list of editable roles.
+        */
+       public function editable_roles( $roles ) {
+               unset( $roles[ $this->translation_editor_role ] );
+
+               if ( ! is_super_admin() && ! is_main_site() ) {
+                       unset( $roles['administrator'] );
+               }
+
+               return $roles;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Translates the "Translation Editor" role.
+        *
+        * @param string $translation Translated text.
+        * @param string $text        Text to translate.
+        * @param string $context     Context information for the translators.
+        * @param string $domain      Text domain.
+        * @return string Translated user role.
+        */
+       public function rename_user_roles( $translation, $text, $context, $domain ) {
+               if ( $domain !== 'default' || $context !== 'User role' ) {
+                       return $translation;
+               }
+
+               if ( 'Translation Editor' === $text ) {
+                       return __( 'Translation Editor', 'rosetta' );
+               }
+
+               return $translation;
+       }
+
+       /**
+        * Replaces the "Role" column with a "Roles" column.
+        *
+        * @param array $columns An array of column headers.
+        * @return array An array of column headers.
+        */
+       public function add_roles_column( $columns ) {
+               $posts = $columns['posts'];
+               unset( $columns['role'], $columns['posts'] );
+               reset( $columns );
+               $columns['roles'] = __( 'Roles', 'rosetta' );
+               $columns['posts'] = $posts;
+
+               return $columns;
+       }
+
+       /**
+        * Displays a comma separated list of user's roles.
+        *
+        * @param string $output      Custom column output.
+        * @param string $column_name Column name.
+        * @param int    $user_id     ID of the currently-listed user.
+        * @return string Comma separated list of user's roles.
+        */
+       public function display_user_roles( $output, $column_name, $user_id ) {
+               global $wp_roles;
+
+               if ( 'roles' == $column_name ) {
+                       $user_roles = array();
+                       $user = new WP_User( $user_id );
+                       foreach ( $user->roles as $role ) {
+                               $role_name = $wp_roles->role_names[ $role ];
+                               $role_name = translate_user_role( $role_name );
+                               $user_roles[] = $role_name;
+                       }
+
+                       return implode( ', ', $user_roles );
+               }
+
+               return $output;
+       }
+
+       /**
+        * Registers page for managing translation editors.
+        */
+       public function register_translation_editors_page() {
+               $this->translation_editors_page = add_users_page(
+                       __( 'Translation Editors', 'rosetta' ),
+                       __( 'Translation Editors', 'rosetta' ),
+                       'list_users',
+                       'translation-editors',
+                       array( $this, 'render_translation_editors_page' )
+               );
+
+               add_action( 'load-' . $this->translation_editors_page, array( $this, 'load_translation_editors_page' ) );
+               add_action( 'admin_print_scripts-' . $this->translation_editors_page, array( $this, 'enqueue_scripts' ) );
+       }
+
+       /**
+        * Enqueues scripts.
+        */
+       public function enqueue_scripts() {
+               wp_enqueue_script( 'rosetta-roles', plugins_url( '/js/rosetta-roles.js', __FILE__ ), array( 'jquery' ), '1', true );
+       }
+
+       /**
+        * Loads either the overview or the edit handler.
+        */
+       public function load_translation_editors_page() {
+               if ( ! empty( $_REQUEST['user_id'] ) ) {
+                       $this->load_edit_translation_editor( $_REQUEST['user_id'] );
+               } else {
+                       $this->load_translation_editors();
+               }
+       }
+
+       /**
+        * Renders either the overview or the edit view.
+        */
+       public function render_translation_editors_page() {
+               if ( ! empty( $_REQUEST['user_id'] ) ) {
+                       $this->render_edit_translation_editor( $_REQUEST['user_id'] );
+               } else {
+                       $this->render_translation_editors();
+               }
+       }
+
+       /**
+        * Handler for overview page.
+        */
+       private function load_translation_editors() {
+               global $wpdb;
+
+               $list_table = $this->get_translation_editors_list_table();
+               $action = $list_table->current_action();
+               $redirect = menu_page_url( 'translation-editors', false );
+
+               if ( $action ) {
+                       switch ( $action ) {
+                               case 'add-translation-editor':
+                                       check_admin_referer( 'add-translation-editor', '_nonce_add-translation-editor' );
+
+                                       if ( ! current_user_can( 'promote_users' ) ) {
+                                               wp_redirect( $redirect );
+                                               exit;
+                                       }
+
+                                       $user_details = null;
+                                       $user = wp_unslash( $_REQUEST['user'] );
+                                       if ( false !== strpos( $user_email, '@' ) ) {
+                                               $user_details = get_user_by( 'email', $user );
+                                       } else {
+                                               $user_details = get_user_by( 'login', $user );
+                                       }
+
+                                       if ( ! $user_details ) {
+                                               wp_redirect( add_query_arg( array( 'error' => 'no-user-found' ), $redirect ) );
+                                               exit;
+                                       }
+
+                                       if ( ! is_user_member_of_blog( $user_details->ID ) ) {
+                                               wp_redirect( add_query_arg( array( 'error' => 'not-a-member' ), $redirect ) );
+                                               exit;
+                                       }
+
+                                       if ( user_can( $user_details, $this->translation_editor_role ) ) {
+                                               wp_redirect( add_query_arg( array( 'error' => 'user-exists' ), $redirect ) );
+                                               exit;
+                                       }
+
+                                       $user_details->add_role( $this->translation_editor_role );
+                                       $this->notify_translation_editor_update( $user_details->ID, 'add' );
+
+                                       $projects = empty( $_REQUEST['projects'] ) ? '' : $_REQUEST['projects'];
+                                       if ( 'custom' === $projects ) {
+                                               $redirect = add_query_arg( 'user_id', $user_details->ID, $redirect );
+                                               wp_redirect( add_query_arg( array( 'update' => 'user-added-custom-projects' ), $redirect ) );
+                                               exit;
+                                       }
+
+                                       $meta_key = $wpdb->get_blog_prefix() . $this->project_access_meta_key;
+                                       update_user_meta( $user_details->ID, $meta_key, array( 'all' ) );
+
+                                       wp_redirect( add_query_arg( array( 'update' => 'user-added' ), $redirect ) );
+                                       exit;
+                               case 'remove-translation-editors':
+                                       check_admin_referer( 'bulk-translation-editors' );
+
+                                       if ( ! current_user_can( 'promote_users' ) ) {
+                                               wp_redirect( $redirect );
+                                               exit;
+                                       }
+
+                                       if ( empty( $_REQUEST['translation-editors'] ) ) {
+                                               wp_redirect( $redirect );
+                                               exit;
+                                       }
+
+                                       $count = 0;
+                                       $meta_key = $wpdb->get_blog_prefix() . $this->project_access_meta_key;
+                                       $user_ids = array_map( 'intval', (array) $_REQUEST['translation-editors'] );
+                                       foreach ( $user_ids as $user_id ) {
+                                               $user = get_user_by( 'id', $user_id );
+                                               $user->remove_role( $this->translation_editor_role );
+                                               delete_user_meta( $user_id, $meta_key );
+                                               $this->notify_translation_editor_update( $user_id, 'remove' );
+                                               $count++;
+                                       }
+
+                                       wp_redirect( add_query_arg( array( 'update' => 'user-removed', 'count' => $count ), $redirect ) );
+                                       exit;
+                               case 'remove-translation-editor':
+                                       check_admin_referer( 'remove-translation-editor' );
+
+                                       if ( ! current_user_can( 'promote_users' ) ) {
+                                               wp_redirect( $redirect );
+                                               exit;
+                                       }
+
+                                       if ( empty( $_REQUEST['translation-editor'] ) ) {
+                                               wp_redirect( $redirect );
+                                               exit;
+                                       }
+
+                                       $user_id = (int) $_REQUEST['translation-editor'];
+                                       $user = get_user_by( 'id', $user_id );
+                                       $user->remove_role( $this->translation_editor_role );
+                                       $meta_key = $wpdb->get_blog_prefix() . $this->project_access_meta_key;
+                                       delete_user_meta( $user_id, $meta_key );
+                                       $this->notify_translation_editor_update( $user_id, 'remove' );
+
+                                       wp_redirect( add_query_arg( array( 'update' => 'user-removed' ), $redirect ) );
+                                       exit;
+                       }
+               }
+       }
+
+       /**
+        * Handler for editing a translation editor.
+        *
+        * @param  int $user_id User ID of a translation editor.
+        */
+       private function load_edit_translation_editor( $user_id ) {
+               global $wpdb;
+
+               $redirect = menu_page_url( 'translation-editors', false );
+
+               if ( ! current_user_can( 'promote_users' ) ) {
+                       wp_redirect( $redirect );
+                       exit;
+               }
+
+               $user_details = get_user_by( 'id', $user_id );
+
+               if ( ! $user_details ) {
+                       wp_redirect( add_query_arg( array( 'error' => 'no-user-found' ), $redirect ) );
+                       exit;
+               }
+
+               if ( ! is_user_member_of_blog( $user_details->ID ) ) {
+                       wp_redirect( add_query_arg( array( 'error' => 'not-a-member' ), $redirect ) );
+                       exit;
+               }
+
+               if ( ! user_can( $user_details, $this->translation_editor_role ) ) {
+                       wp_redirect( add_query_arg( array( 'error' => 'user-cannot' ), $redirect ) );
+                       exit;
+               }
+
+               $action = empty( $_REQUEST['action'] ) ? '' : $_REQUEST['action'];
+               switch ( $action ) {
+                       case 'update-translation-editor':
+                               check_admin_referer( 'update-translation-editor_' . $user_details->ID );
+
+                               $redirect = add_query_arg( 'user_id', $user_details->ID, $redirect );
+
+                               $all_projects = $this->get_translate_top_level_projects();
+                               $all_projects = wp_list_pluck( $all_projects, 'id' );
+                               $all_projects = array_map( 'intval', $all_projects );
+
+                               $projects = (array) $_REQUEST['projects'];
+                               if ( in_array( 'all', $projects ) ) {
+                                       $projects = array( 'all' );
+                               } else {
+                                       $projects = array_map( 'intval', $projects );
+                                       $projects = array_values( array_intersect( $all_projects, $projects ) );
+                               }
+
+                               $meta_key = $wpdb->get_blog_prefix() . $this->project_access_meta_key;
+                               update_user_meta( $user_details->ID, $meta_key, $projects );
+
+                               wp_redirect( add_query_arg( array( 'update' => 'user-updated' ), $redirect ) );
+                               exit;
+               }
+       }
+
+       /**
+        * Renders the overview page.
+        */
+       private function render_translation_editors() {
+               $list_table = $this->get_translation_editors_list_table();
+               $list_table->prepare_items();
+
+               $feedback_message = $this->get_feedback_message();
+
+               require __DIR__ . '/views/translation-editors.php';
+       }
+
+       /**
+        * Renders the edit page.
+        */
+       private function render_edit_translation_editor( $user_id ) {
+               global $wpdb;
+
+               $projects = $this->get_translate_top_level_projects();
+
+               $meta_key = $wpdb->get_blog_prefix() . $this->project_access_meta_key;
+               $project_access_list = get_user_meta( $user_id, $meta_key, true );
+               if ( ! $project_access_list ) {
+                       $project_access_list = array();
+               }
+
+               $feedback_message = $this->get_feedback_message();
+
+               require __DIR__ . '/views/edit-translation-editor.php';
+       }
+
+       /**
+        * Returns a feedback message based on the current request.
+        *
+        * @return string HTML formatted message.
+        */
+       private function get_feedback_message() {
+               $message = '';
+
+               if ( ! empty( $_REQUEST['update'] ) && ! empty( $_REQUEST['error'] ) ) {
+                       return $message;
+               }
+
+               $count = empty( $_REQUEST['count'] ) ? 1 : (int) $_REQUEST['count'];
+
+               $messages = array(
+                       'update' => array(
+                               'user-updated' => __( 'Translation editor updated.', 'rosetta' ),
+                               'user-added'   => __( 'New translation editor added.', 'rosetta' ),
+                               'user-added-custom-projects' => __( 'New translation editor added. You can select the projects now.', 'rosetta' ),
+                               'user-removed' => sprintf( _n( '%s translation editor removed.', '%s translation editors removed.', $count, 'rosetta' ), number_format_i18n( $count ) ),
+                       ),
+
+                       'error' => array(
+                               'no-user-found' => __( 'The user couldn&#8217;t be found.', 'rosetta' ),
+                               'not-a-member'  => __( 'The user is not a member of this site.', 'rosetta' ),
+                               'user-cannot'   => __( 'The user is not a translation editor.', 'rosetta' ),
+                               'user-exists'   => __( 'The user is already a translation editor.', 'rosetta' ),
+                       ),
+               );
+
+               if ( isset( $_REQUEST['error'], $messages['error'][ $_REQUEST['error'] ] ) ) {
+                       $message = sprintf(
+                               '<div class="notice notice-error"><p>%s</p></div>',
+                               $messages['error'][ $_REQUEST['error'] ]
+                       );
+               } elseif( isset( $_REQUEST['update'], $messages['update'][ $_REQUEST['update'] ] ) ) {
+                       $message = sprintf(
+                               '<div class="notice notice-success"><p>%s</p></div>',
+                               $messages['update'][ $_REQUEST['update'] ]
+                       );
+               }
+
+               return $message;
+       }
+
+       /**
+        * Wrapper for the custom list table which lists translation editors.
+        *
+        * @return Rosetta_Translation_Editors_List_Table The list table.
+        */
+       private function get_translation_editors_list_table() {
+               global $wpdb;
+               static $list_table;
+
+               require_once __DIR__ . '/class-translation-editors-list-table.php';
+
+               if ( isset( $list_table ) ) {
+                       return $list_table;
+               }
+
+               $args = array(
+                       'user_role'               => $this->translation_editor_role,
+                       'projects'                => $this->get_translate_top_level_projects(),
+                       'project_access_meta_key' => $wpdb->get_blog_prefix() . $this->project_access_meta_key,
+               );
+               $list_table = new Rosetta_Translation_Editors_List_Table( $args );
+
+               return $list_table;
+       }
+
+       /**
+        * Notifies profiles.wordpress.org about a change.
+        *
+        * @param  int    $user_id User ID.
+        * @param  string $action  Can be 'add' or 'remove'.
+        */
+       private function notify_translation_editor_update( $user_id, $action ) {
+               $args = array(
+                       'body' => array(
+                               'action'      => 'wporg_handle_association',
+                               'source'      => 'polyglots',
+                               'command'     => $action,
+                               'user_id'     => $user_id,
+                               'association' => 'translation-editor',
+                       )
+               );
+
+               wp_remote_post( self::PROFILES_HANDLER_URL, $args );
+       }
+
+       /**
+        * Fetches all top level projects from translate.wordpress.org.
+        *
+        * @return array List of projects.
+        */
+       private function get_translate_top_level_projects() {
+               global $wpdb;
+
+               $cache = get_site_transient( 'translate-top-level-projects' );
+               if ( false !== $cache ) {
+                       return $cache;
+               }
+
+               $_projects = $wpdb->get_results( "
+                       SELECT id, name
+                       FROM translate_projects
+                       WHERE parent_project_id IS NULL
+                       ORDER BY name ASC
+               " );
+
+               $projects = array();
+               foreach ( $_projects as $project ) {
+                       $projects[ $project->id ] = $project;
+               }
+
+               set_site_transient( 'translate-top-level-projects', $projects, DAY_IN_SECONDS );
+
+               return $projects;
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+$GLOBALS['rosetta_roles'] = new Rosetta_Roles();
</ins></span></pre></div>
<a id="sitestrunkglobalwordpressorgpublic_htmlwpcontentmupluginsrolesviewsedittranslationeditorphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/views/edit-translation-editor.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/views/edit-translation-editor.php                                (rev 0)
+++ sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/views/edit-translation-editor.php  2015-03-19 14:48:45 UTC (rev 1419)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,49 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<div class="wrap">
+       <h2><?php _e( 'Edit Translation Editor', 'rosetta' ); ?></h2>
+
+       <?php echo $feedback_message; ?>
+
+       <form method="post">
+               <table class="form-table">
+                       <tbody>
+                               <tr>
+                                       <th scope="row">
+                                               <?php _e( 'Add editor access for:', 'rosetta' ); ?><br>
+                                               <small style="font-weight:normal"><a href="#clear-all" id="clear-all"><?php _ex( 'Clear All', 'Deselects all checkboxes', 'rosetta' ); ?></a></small>
+                                       </th>
+                                       <td>
+                                               <fieldset>
+                                                       <legend class="screen-reader-text"><span><?php _e( 'Add editor access for:', 'rosetta' ); ?></span></legend>
+                                                       <p>
+                                                               <label for="project-all">
+                                                                       <input name="projects[]" id="project-all" value="all" type="checkbox"<?php checked( in_array( 'all', $project_access_list ) ); ?>> <?php _e( 'All projects &ndash; If selected, translation editor will have validation permissions for all projects, including newly-added projects.', 'rosetta' ); ?>
+                                                               </label>
+                                                       </p>
+                                                       <?php
+                                                       foreach ( $projects as $project ) {
+                                                               $project_id = esc_attr( $project->id );
+                                                               printf(
+                                                                       '<p><label for="project-%d"><input name="projects[]" id="project-%d" class="project" value="%d" type="checkbox"%s> %s</label></p>',
+                                                                       $project_id,
+                                                                       $project_id,
+                                                                       $project_id,
+                                                                       checked( in_array( $project->id, $project_access_list ), true, false ),
+                                                                       esc_html( $project->name )
+                                                               );
+                                                       }
+                                                       ?>
+                                                       <p class="description"><?php _e( 'Each project includes sub projects and newly-added sub projects.', 'rosetta' ); ?></p>
+                                               </fieldset>
+                                       </td>
+                               </tr>
+                       </tbody>
+               </table>
+
+               <input type="hidden" name="action" value="update-translation-editor">
+               <input type="hidden" name="user_id" value="<?php echo esc_attr( $user_id ); ?>">
+               <?php
+               wp_nonce_field( 'update-translation-editor_' . $user_id );
+               submit_button( _x( 'Update', 'translation editor', 'rosetta' ) );
+               ?>
+       </form>
+</div>
</ins></span></pre></div>
<a id="sitestrunkglobalwordpressorgpublic_htmlwpcontentmupluginsrolesviewstranslationeditorsphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/views/translation-editors.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/views/translation-editors.php                            (rev 0)
+++ sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/views/translation-editors.php      2015-03-19 14:48:45 UTC (rev 1419)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,53 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<div class="wrap">
+       <h2>
+               <?php
+               _e( 'Translation Editors', 'rosetta' );
+
+               if ( ! empty( $_REQUEST['s'] ) ) {
+                       echo '<span class="subtitle">' . sprintf( __( 'Search results for &#8220;%s&#8221;', 'rosetta' ), esc_html( wp_unslash( $_REQUEST['s'] ) ) ) . '</span>';
+               }
+               ?>
+       </h2>
+
+       <?php echo $feedback_message; ?>
+
+       <form method="get">
+               <input type="hidden" name="page" value="translation-editors">
+               <?php $list_table->search_box( __( 'Search Translation Editors', 'rosetta' ), 'rosetta' ); ?>
+       </form>
+
+       <form method="post">
+               <?php $list_table->display(); ?>
+       </form>
+
+       <?php if ( current_user_can( 'promote_users' ) ) : ?>
+               <h3><?php _e( 'Add Translation Editor', 'rosetta' ); ?></h3>
+               <p><?php _e( 'Enter the email address or username of an existing user on this site.', 'rosetta' ); ?></p>
+               <form action="" method="post">
+                       <table class="form-table">
+                               <tr>
+                                       <th scope="row"><label for="user"><?php _e( 'E-mail or Username', 'rosetta' ); ?></label></th>
+                                       <td><input type="text" class="regular-text" name="user" id="user"></td>
+                               </tr>
+                               <tr>
+                                       <th scope="row"><label for="user"><?php _e( 'Add editor access for:', 'rosetta' ); ?></label></th>
+                                       <td>
+                                               <fieldset>
+                                                       <legend class="screen-reader-text"><?php _e( 'Add editor access for:', 'rosetta' ); ?></legend>
+                                                       <label for="all-projects" >
+                                                               <input type="radio" name="projects" value="all" id="all-projects" checked="checked"> <?php _e( 'All projects &ndash; If selected, translation editor will have validation permissions for all projects, including newly-added projects.', 'rosetta' ); ?>
+                                                       </label>
+                                                       <br>
+                                                       <label for="custom-projects">
+                                                               <input type="radio" name="projects" value="custom" id="custom-projects"> <?php _e( 'Custom &ndash; After the user is added you will be redirected to set the projects.', 'projects', 'rosetta' ); ?>
+                                                       </label>
+                                               </fieldset>
+                                       </td>
+                               </tr>
+                       </table>
+                       <input type="hidden" name="action" value="add-translation-editor">
+                       <?php wp_nonce_field( 'add-translation-editor', '_nonce_add-translation-editor' ) ?>
+                       <?php submit_button( __( 'Add Translation Editor', 'rosetta' ) ); ?>
+               </form>
+       <?php endif; ?>
+</div>
</ins></span></pre>
</div>
</div>

</body>
</html>