[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/ -> advanced-permissions.php (source)

   1  <?php
   2  
   3  /**
   4   * Filter for can_user, which tries if the user
   5   * has permissions on project parents
   6   */
   7  function gp_recurse_project_permissions( $verdict, $args ) {
   8      if ( ! ( ! $verdict && 'project' == $args['object_type'] && $args['object_id'] && $args['user'] ) ) {
   9          return $verdict;
  10      }
  11      $project = GP::$project->get( $args['object_id'] );
  12      if ( $project && $project->parent_project_id ) {
  13          return GP::$permission->user_can( $args['user'], $args['action'], 'project', $project->parent_project_id );
  14      }
  15      return false;
  16  }
  17  
  18  function gp_recurse_validator_permission( $verdict, $args ) {
  19      if ( ! ( ! $verdict && GP::$validator_permission->object_type == $args['object_type'] && $args['object_id'] && $args['user'] ) ) {
  20          return $verdict;
  21      }
  22      list( $project_id, $locale_slug, $set_slug ) = GP::$validator_permission->project_id_locale_slug_set_slug( $args['object_id'] );
  23  
  24      $project = GP::$project->get( $project_id );
  25      if ( $project && $project->parent_project_id ) {
  26          return GP::$permission->user_can( $args['user'], $args['action'], $args['object_type'], $project->parent_project_id . '|' . $locale_slug . '|' . $set_slug );
  27      }
  28      return false;
  29  }
  30  
  31  
  32  function gp_route_translation_set_permissions_to_validator_permissions( $verdict, $args ) {
  33      if ( is_bool( $verdict ) ) {
  34          return $verdict;
  35      }
  36  
  37      if ( ! ( 'no-verdict' == $verdict && 'approve' == $args['action'] && 'translation-set' == $args['object_type']
  38              && $args['object_id'] && $args['user'] ) ) {
  39          return $verdict;
  40      }
  41      if ( isset( $args['extra']['set'] ) && $args['extra']['set'] && $args['extra']['set']->id == $args['object_id'] ) {
  42          $set = $args['extra']['set'];
  43      } else {
  44          $set = GP::$translation_set->get( $args['object_id'] );
  45      }
  46      return GP::$permission->user_can(
  47          $args['user'],
  48          'approve',
  49          GP::$validator_permission->object_type,
  50          GP::$validator_permission->object_id( $set->project_id, $set->locale, $set->slug )
  51      );
  52  }
  53  
  54  function gp_allow_everyone_to_translate( $verdict, $args ) {
  55      if ( is_bool( $verdict ) ) {
  56          return $verdict;
  57      }
  58  
  59      if ( 'edit' == $args['action'] && 'translation-set' == $args['object_type'] ) {
  60          return is_user_logged_in();
  61      }
  62  
  63      return $verdict;
  64  }
  65  
  66  /**
  67   * Maps the translation check to the translation-set.
  68   *
  69   * @since 2.3.0
  70   *
  71   * @param string|bool $verdict Previous decision whether the user can do this.
  72   * @param array       $args    Permission details.
  73   * @return string|bool New decision whether the user can do this.
  74   */
  75  function gp_allow_approving_translations_with_validator_permissions( $verdict, $args ) {
  76      if ( is_bool( $verdict ) ) {
  77          return $verdict;
  78      }
  79  
  80      if ( 'approve' === $args['action'] && 'translation' === $args['object_type'] ) {
  81          $args['object_type'] = 'translation-set';
  82  
  83          if ( isset( $args['extra']['translation']->translation_set_id ) ) {
  84              $args['object_id'] = $args['extra']['translation']->translation_set_id;
  85          } else {
  86              return $verdict;
  87          }
  88  
  89          return gp_route_translation_set_permissions_to_validator_permissions( $verdict, $args );
  90      }
  91  
  92      return $verdict;
  93  }
  94  
  95  add_filter( 'gp_can_user', 'gp_recurse_project_permissions', 10, 2 );
  96  add_filter( 'gp_can_user', 'gp_recurse_validator_permission', 10, 2 );
  97  add_filter( 'gp_pre_can_user', 'gp_route_translation_set_permissions_to_validator_permissions', 10, 2 );
  98  add_filter( 'gp_pre_can_user', 'gp_allow_approving_translations_with_validator_permissions', 10, 2 );
  99  add_filter( 'gp_pre_can_user', 'gp_allow_everyone_to_translate', 10, 2 );


Generated: Thu Dec 26 01:01:15 2024 Cross-referenced by PHPXref 0.7.1