true, self::CAP => true, 'upload_files' => true, ) ); foreach ( array( 'administrator', 'editor' ) as $role_name ) { $role = get_role( $role_name ); if ( $role ) { $role->add_cap( self::CAP ); $role->add_cap( self::CAP_REVIEW ); } } } public static function uninstall(): void { remove_role( self::ROLE ); foreach ( array( 'administrator', 'editor' ) as $role_name ) { $role = get_role( $role_name ); if ( $role ) { $role->remove_cap( self::CAP ); $role->remove_cap( self::CAP_REVIEW ); } } } /** @return string[] Languages the user may translate (all when unrestricted). */ public static function user_languages( int $user_id = 0 ): array { $user_id = $user_id ?: get_current_user_id(); $assigned = array_filter( (array) get_user_meta( $user_id, self::USER_META, true ) ); if ( ! $assigned ) { return Languages::targets(); // Not restricted. } // Restricted: only assigned languages that are still enabled. When none remain, // the translator has no languages (never a fallback to all of them). return array_values( array_intersect( $assigned, Languages::targets() ) ); } public static function can_translate( string $lang = '' ): bool { if ( current_user_can( 'manage_options' ) ) { return true; } if ( ! current_user_can( self::CAP ) ) { return false; } $langs = self::user_languages(); return '' === $lang ? (bool) $langs : in_array( $lang, $langs, true ); } /** * Whether the current user may work on translations of a post: it must be * translatable content the user can read (private posts, drafts and scheduled * posts need the matching WordPress capability; password-protected posts need * edit rights). */ public static function can_access_post( int $post_id ): bool { $source_id = Groups::source_id( 'post', $post_id ); $post = get_post( $source_id ); if ( ! $post || ! PostTypes::is_translatable( $post->post_type ) || in_array( $post->post_status, array( 'trash', 'auto-draft', 'inherit' ), true ) ) { return false; } if ( ! current_user_can( 'read_post', $source_id ) ) { return false; } // read_post does not check post passwords: protected content needs edit rights. if ( '' !== $post->post_password && ! current_user_can( 'edit_post', $source_id ) ) { return false; } return true; } /** * Whether a translation is live on the site. */ public static function is_live( int $post_id ): bool { return $post_id > 0 && in_array( get_post_status( $post_id ), array( 'publish', 'future', 'private' ), true ); } public static function can_review( string $lang = '' ): bool { if ( current_user_can( 'manage_options' ) ) { return true; } return current_user_can( self::CAP_REVIEW ) && self::can_translate( $lang ); } /** * Translators may edit unpublished translations in their languages in the * WordPress editor, without general edit_posts rights on source content. * Published translations change only through review. */ public function translator_can_edit_translations( $allcaps, $caps, $args, $user ) { if ( empty( $allcaps[ self::CAP ] ) || empty( $args[2] ) || ! in_array( $args[0] ?? '', array( 'edit_post', 'edit_page' ), true ) ) { return $allcaps; } $post_id = (int) $args[2]; if ( ! Groups::is_translation( 'post', $post_id ) ) { return $allcaps; } $lang = Groups::lang_of( 'post', $post_id ); $meta = (array) get_user_meta( $user->ID, self::USER_META, true ); if ( array_filter( $meta ) && ! in_array( $lang, $meta, true ) ) { return $allcaps; } foreach ( $caps as $cap ) { if ( in_array( $cap, array( 'edit_posts', 'edit_others_posts', 'edit_pages', 'edit_others_pages' ), true ) ) { $allcaps[ $cap ] = true; } } return $allcaps; } public function profile_fields( \WP_User $user ): void { if ( ! current_user_can( 'manage_options' ) || ! user_can( $user, self::CAP ) ) { return; } $selected = (array) get_user_meta( $user->ID, self::USER_META, true ); ?>

ID ) ) { return; } if ( ! apply_filters( 'translateplus_notify_review', true, $post ) ) { return; } $lang = Groups::lang_of( 'post', (int) $post->ID ); $users = get_users( array( 'role' => self::ROLE, 'fields' => array( 'ID', 'user_email' ), ) ); $to = array(); foreach ( $users as $u ) { if ( in_array( $lang, self::user_languages( (int) $u->ID ), true ) ) { $to[] = $u->user_email; } } if ( ! $to ) { return; } $url = admin_url( 'admin.php?page=translateplus-editor&post=' . Groups::source_id( 'post', (int) $post->ID ) . '&lang=' . rawurlencode( $lang ) ); wp_mail( $to, /* translators: 1: language, 2: post title */ sprintf( __( '[%1$s] Translation ready for review: %2$s', 'translateplus' ), Languages::name( $lang ), get_the_title( $post ) ), /* translators: %s: editor URL */ sprintf( __( "A new translation is waiting for review.\n\nOpen the editor: %s", 'translateplus' ), $url ) ); } }