[ 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 && $args['object_type'] == 'project' && $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 && $args['object_type'] == GP::$validator_permission->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      $project = GP::$project->get( $project_id );
  24      if ( $project && $project->parent_project_id ) {
  25          return GP::$permission->user_can( $args['user'], $args['action'], $args['object_type'], $project->parent_project_id.'|'.$locale_slug.'|'.$set_slug );
  26      }
  27      return false;
  28  }
  29  
  30  
  31  function gp_route_translation_set_permissions_to_validator_permissions( $verdict, $args ) {
  32      if ( is_bool( $verdict ) ) {
  33          return $verdict;
  34      }
  35  
  36      if ( !( $verdict == 'no-verdict' && $args['action'] == 'approve' && $args['object_type'] == 'translation-set'
  37              && $args['object_id'] && $args['user'] ) ) {
  38          return $verdict;
  39      }
  40      if ( isset( $args['extra']['set'] ) && $args['extra']['set'] && $args['extra']['set']->id == $args['object_id'] )
  41          $set = $args['extra']['set'];
  42      else
  43          $set = GP::$translation_set->get( $args['object_id'] );
  44      return GP::$permission->user_can( $args['user'], 'approve', GP::$validator_permission->object_type,
  45          GP::$validator_permission->object_id( $set->project_id, $set->locale, $set->slug ) );
  46  }
  47  
  48  function gp_allow_everyone_to_translate( $verdict, $args ) {
  49      if ( is_bool( $verdict ) ) {
  50          return $verdict;
  51      }
  52  
  53      if ( 'edit' == $args['action'] && 'translation-set' == $args['object_type'] ) {
  54          return is_user_logged_in();
  55      }
  56  
  57      return $verdict;
  58  }
  59  
  60  /**
  61   * Maps the translation check to the translation-set.
  62   *
  63   * @since 2.3.0
  64   *
  65   * @param string|bool $verdict Previous decision whether the user can do this.
  66   * @param array       $args    Permission details.
  67   * @return string|bool New decision whether the user can do this.
  68   */
  69  function gp_allow_approving_translations_with_validator_permissions( $verdict, $args ) {
  70      if ( is_bool( $verdict ) ) {
  71          return $verdict;
  72      }
  73  
  74      if ( 'approve' === $args['action'] && 'translation' === $args['object_type'] ) {
  75          $args['object_type'] = 'translation-set';
  76  
  77          if ( isset( $args['extra']['translation']->translation_set_id ) ) {
  78              $args['object_id'] = $args['extra']['translation']->translation_set_id;
  79          } else {
  80              return $verdict;
  81          }
  82  
  83          return gp_route_translation_set_permissions_to_validator_permissions( $verdict, $args );
  84      }
  85  
  86      return $verdict;
  87  }
  88  
  89  add_filter( 'gp_can_user', 'gp_recurse_project_permissions', 10, 2 );
  90  add_filter( 'gp_can_user', 'gp_recurse_validator_permission', 10, 2 );
  91  add_filter( 'gp_pre_can_user', 'gp_route_translation_set_permissions_to_validator_permissions', 10, 2 );
  92  add_filter( 'gp_pre_can_user', 'gp_allow_approving_translations_with_validator_permissions', 10, 2 );
  93  add_filter( 'gp_pre_can_user', 'gp_allow_everyone_to_translate', 10, 2 );


Generated: Tue Mar 19 01:01:50 2019 Cross-referenced by PHPXref 0.7.1