Skip to content

Instantly share code, notes, and snippets.

@nacin
Created February 7, 2013 23:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nacin/4735308 to your computer and use it in GitHub Desktop.
Save nacin/4735308 to your computer and use it in GitHub Desktop.
<?php
class GP_WPorg_Rosetta_Roles extends GP_Plugin {
var $id = 'wporg-rosetta-roles';
function __construct() {
parent::__construct();
$this->add_filter( 'pre_can_user', array( 'args' => 2, 'priority' => 9 ) );
}
function pre_can_user( $verdict, $args ) {
if ( ! class_exists( 'BP_Roles' ) )
require_once( BACKPRESS_PATH . 'class.bp-roles.php' );
if ( ! class_exists( 'BP_User' ) )
require_once( BACKPRESS_PATH . 'class.bp-user.php' );
$user = new BP_User( $args['user_id'] );
// 78 = global.wordpress.org. Administrators on this site are considered global admins in GlotPress.
if ( ! empty( $user->ros_78_capabilities ) && is_array( $user->ros_78_capabilities ) && ! empty( $user->ros_78_capabilities['administrator'] ) )
return true;
if ( $args['action'] !== 'approve' || ! in_array( $args['object_type'], array( 'project|locale|set-slug', 'translation-set' ) ) )
return false;
if ( ! $locale_slug = $this->get_locale_slug( $args['object_type'], $args['object_id'] ) )
return false;
if ( ! $maybe_cap_key = $this->get_cap_key( $locale_slug ) )
return false;
$user->cap_key = $maybe_cap_key;
$user->caps = &$user->{$user->cap_key};
if ( ! is_array( $user->caps ) )
$user->caps = array();
$user->get_role_caps();
foreach ( array( 'administrator', 'editor', 'author', 'contributor', 'validator' ) as $role ) {
if ( $user->has_cap( $role ) )
return true;
}
return false;
}
function get_locale_slug( $object_type, $object_id ) {
switch ( $object_type ) {
case 'translation-set' :
return GP::$translation_set->get( $object_id )->locale;
break;
case 'project|locale|set-slug' :
list( , $locale ) = explode( '|', $object_id );
return $locale;
break;
}
return false;
}
function get_cap_key( $locale_slug ) {
global $gpdb;
static $ros_blogs, $ros_locale_assoc;
$gp_locale = GP_Locales::by_slug( $locale_slug );
if ( ! $gp_locale || ! isset( $gp_locale->wp_locale ) )
return false;
$wp_locale = $gp_locale->wp_locale;
if ( ! isset( $ros_blogs ) ) {
$ros_locale_assoc = $gpdb->get_results( "SELECT locale, subdomain FROM locales", OBJECT_K );
$ros_blogs = $gpdb->get_results( "SELECT domain, blog_id FROM ros_blogs", OBJECT_K );
}
if ( isset( $ros_locale_assoc[ $wp_locale ] ) )
$subdomain = $ros_locale_assoc[ $wp_locale ]->subdomain;
else
return false;
if ( isset( $ros_blogs[ "$subdomain.wordpress.org" ] ) )
return 'ros_' . $ros_blogs[ "$subdomain.wordpress.org" ]->blog_id . '_capabilities';
return false;
}
}
GP::$plugins->wporg_rosetta_roles = new GP_WPorg_Rosetta_Roles;
CREATE TABLE IF NOT EXISTS `languages` (
`lang_id` int(11) NOT NULL AUTO_INCREMENT,
`slug` varchar(7) NOT NULL DEFAULT '',
`name` varchar(250) NOT NULL DEFAULT '',
PRIMARY KEY (`lang_id`),
KEY `slug` (`slug`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `locales` (
`locale` varchar(20) NOT NULL DEFAULT '',
`subdomain` varchar(20) NOT NULL DEFAULT '',
`complete` enum('yes','no') DEFAULT 'no',
`latest_release` varchar(16) DEFAULT NULL,
PRIMARY KEY (`subdomain`),
KEY `locale` (`locale`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
<?php
/*
* Plugin Name: Rosetta Role Modifications
* Author: Andrew Nacin
*/
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;
if ( 'Validator' === $text )
return __( 'Validator', 'rosetta' );
return $translated;
}
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' );
}
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() )
unset( $roles['administrator'] );
return $roles;
}
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 ) );
$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' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment