Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 19, 2025

This PR adds a new WPML_User_Helper class that provides convenient methods for managing WPML translation capabilities without requiring users to have the editor role. This addresses the need for more granular permission management in multilingual WordPress setups.

New Helper Class: WPML_User_Helper

The helper provides four main methods for translation capability management:

Core Methods (as requested in issue)

Single User Management:

// Grant translation capability to a specific user
$result = WPML_User_Helper::add_translation_manager_capability($user_id);

Bulk Role Management:

// Grant capability to all users in a role
$results = WPML_User_Helper::add_translation_manager_capability_to_role('author');
// Returns: ['success' => 5, 'failed' => 0, 'total' => 5]

Additional Utility Methods

// Check if user has capability
$has_cap = WPML_User_Helper::has_translation_manager_capability($user);

// Remove capability (with ATE sync)
$removed = WPML_User_Helper::remove_translation_manager_capability($user);

Key Features

  • Flexible Input: Accepts both user IDs and WP_User objects
  • WPML Integration: Automatically calls do_action('wpml_tm_ate_synchronize_managers', $user_id) for proper ATE synchronization
  • Error Handling: Graceful handling of invalid users, non-existent roles, and edge cases
  • Memory Efficient: Bulk operations use fields => 'ID' for optimal performance
  • Capability Checking: Avoids redundant operations by checking existing capabilities

Documentation

Comprehensive documentation added at docs/Helpers/wpml-user-helper.md including:

  • Detailed method documentation with examples
  • Integration patterns for admin interfaces, REST APIs, and WP-CLI
  • Migration guide from manual capability management
  • Performance considerations and best practices

Use Cases

This helper enables several important use cases:

  1. Granular Permissions: Grant translation access to content creators without full editor privileges
  2. Role-Based Setup: Bulk configure translation capabilities for specific user roles
  3. Custom Workflows: Integrate translation capability management into custom admin interfaces
  4. Team Management: Easily manage translation teams across different WordPress roles

Example Usage

// Old way (manual, error-prone)
$user = new WP_User($user_id);
if ($user && !$user->has_cap('manage_translations')) {
    $user->add_cap('manage_translations');
    do_action('wpml_tm_ate_synchronize_managers', $user_id);
}

// New way (simple, reliable)
WPML_User_Helper::add_translation_manager_capability($user_id);

The helper follows the same patterns as existing helpers (WPML_Post_Helper, WPML_Language_Helper, WPML_Term_Helper) ensuring consistency across the codebase.

Fixes #15.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/5b5e3821314f947dd040c70f7992a64eac89025c
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/1PMJm2 /usr/bin/composer install (http block)
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/Nn1jHb /usr/bin/composer install --dev --no-interaction (http block)
  • https://api.github.com/repos/PHPCSStandards/composer-installer/zipball/18a95476797ed480b3f2598984bc6f7e1eecc9a8
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/Nn1jHb /usr/bin/composer install --dev --no-interaction (http block)
  • https://api.github.com/repos/composer/installers/zipball/12fb2dfe5e16183de69e784a7b84046c43d97e8e
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/1PMJm2 /usr/bin/composer install (http block)
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/Nn1jHb /usr/bin/composer install --dev --no-interaction (http block)
  • https://api.github.com/repos/doctrine/instantiator/zipball/0a0fa9780f5d4e507415a065172d26a98d02047b
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/1PMJm2 /usr/bin/composer install (http block)
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/Nn1jHb /usr/bin/composer install --dev --no-interaction (http block)
  • https://api.github.com/repos/myclabs/DeepCopy/zipball/1720ddd719e16cf0db4eb1c6eca108031636d46c
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/1PMJm2 /usr/bin/composer install (http block)
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/Nn1jHb /usr/bin/composer install --dev --no-interaction (http block)
  • https://api.github.com/repos/phar-io/version/zipball/4f7fd7836c6f332bb2933569e566a0d6c4cbed74
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/1PMJm2 /usr/bin/composer install (http block)
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/Nn1jHb /usr/bin/composer install --dev --no-interaction (http block)
  • https://api.github.com/repos/phpstan/extension-installer/zipball/85e90b3942d06b2326fba0403ec24fe912372936
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/Nn1jHb /usr/bin/composer install --dev --no-interaction (http block)
  • https://api.github.com/repos/phpstan/phpstan/zipball/89b5ef665716fa2a52ecd2633f21007a6a349053
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/Nn1jHb /usr/bin/composer install --dev --no-interaction (http block)

If you need me to access, download, or install something from one of these locations, you can either:


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Add capabilities helper for WPML Add WPML_User_Helper for managing translation capabilities without editor role requirement Aug 19, 2025
Copilot AI requested a review from JUVOJustin August 19, 2025 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add capabilities helper for WPML

2 participants