[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/things/ -> translation.php (source)

   1  <?php
   2  /**
   3   * Things: GP_Translation class
   4   *
   5   * @package GlotPress
   6   * @subpackage Things
   7   * @since 1.0.0
   8   */
   9  
  10  /**
  11   * Core class used to implement the translations.
  12   *
  13   * @since 1.0.0
  14   */
  15  class GP_Translation extends GP_Thing {
  16  
  17      /**
  18       * Number of translations per page.
  19       *
  20       * @var int $per_page
  21       */
  22      public $per_page = 15;
  23  
  24      /**
  25       * Name of the database table.
  26       *
  27       * @var string $table_basename
  28       */
  29      public $table_basename = 'gp_translations';
  30  
  31      /**
  32       * List of field names for a translation.
  33       *
  34       * @var array $field_names
  35       */
  36      public $field_names = array(
  37          'id',
  38          'original_id',
  39          'translation_set_id',
  40          'translation_0',
  41          'translation_1',
  42          'translation_2',
  43          'translation_3',
  44          'translation_4',
  45          'translation_5',
  46          'user_id',
  47          'user_id_last_modified',
  48          'status',
  49          'date_added',
  50          'date_modified',
  51          'warnings',
  52      );
  53  
  54      /**
  55       * List of field names which have an integer value.
  56       *
  57       * @var array $int_fields
  58       */
  59      public $int_fields = array(
  60          'id',
  61          'original_id',
  62          'translation_set_id',
  63          'user_id',
  64          'user_id_last_modified',
  65      );
  66  
  67      /**
  68       * List of field names which cannot be updated.
  69       *
  70       * @var array $non_updatable_attributes
  71       */
  72      public $non_updatable_attributes = array( 'id' );
  73  
  74      /**
  75       * ID of the translation.
  76       *
  77       * @var int $id
  78       */
  79      public $id;
  80  
  81      /**
  82       * ID of the original.
  83       *
  84       * @var int $original_id
  85       */
  86      public $original_id;
  87  
  88      /**
  89       * ID of the translation set.
  90       *
  91       * @var int $translation_set_id
  92       */
  93      public $translation_set_id;
  94  
  95      /**
  96       * Translation for a singular form.
  97       *
  98       * @var string $translation_1
  99       */
 100      public $translation_0;
 101  
 102      /**
 103       * Translation for a plural form.
 104       *
 105       * @var string $translation_1
 106       */
 107      public $translation_1;
 108  
 109      /**
 110       * Translation for a second plural form.
 111       *
 112       * @var string $translation_2
 113       */
 114      public $translation_2;
 115  
 116      /**
 117       * Translation for a third plural form.
 118       *
 119       * @var string $translation_3
 120       */
 121      public $translation_3;
 122  
 123      /**
 124       * Translation for a fourth plural form.
 125       *
 126       * @var string $translation_4
 127       */
 128      public $translation_4;
 129  
 130      /**
 131       * Translation for a fifth plural form.
 132       *
 133       * @var string $translation_5
 134       */
 135      public $translation_5;
 136  
 137      /**
 138       * ID of a user who submitted the translation.
 139       *
 140       * @var int $user_id
 141       */
 142      public $user_id;
 143  
 144      /**
 145       * ID of a user (validator) who last changed the status of the translation.
 146       *
 147       * @since 2.1.0
 148       *
 149       * @var int $user_id_last_modified
 150       */
 151      public $user_id_last_modified;
 152  
 153      /**
 154       * Status of the translation.
 155       *
 156       * @var string $status
 157       */
 158      public $status;
 159  
 160      /**
 161       * Date when the translation was added.
 162       *
 163       * @var string $date_added
 164       */
 165      public $date_added;
 166  
 167      /**
 168       * Date when the translation was modified.
 169       *
 170       * @var string $date_added
 171       */
 172      public $date_modified;
 173  
 174      /**
 175       * List of warnings when translation isn't correct.
 176       *
 177       * @var array $warnings
 178       */
 179      public $warnings;
 180  
 181      /**
 182       * Number of found results.
 183       *
 184       * @var int $found_rows
 185       */
 186      public $found_rows;
 187  
 188      /**
 189       * List of valid statuses.
 190       *
 191       * @var array $statuses
 192       * @static
 193       */
 194      public static $statuses = array( 'current', 'waiting', 'rejected', 'fuzzy', 'old' );
 195  
 196      /**
 197       * Number of supported translations per original.
 198       *
 199       * @var int $number_of_plural_translations
 200       * @static
 201       */
 202      public static $number_of_plural_translations = 6;
 203  
 204  	public function create( $args ) {
 205          $inserted = parent::create( $args );
 206  
 207          if ( $inserted && is_array( $args ) && isset( $args['translation_set_id'] ) ) {
 208              gp_clean_translation_set_cache( $args['translation_set_id'] );
 209          }
 210  
 211          return $inserted;
 212      }
 213  
 214      /**
 215       * Normalizes an array with key-value pairs representing
 216       * a GP_Translation object.
 217       *
 218       * @since 1.0.0
 219       *
 220       * @param array $args Arguments for a GP_Translation object.
 221       * @return array Normalized arguments for a GP_Translation object.
 222       */
 223  	public function normalize_fields( $args ) {
 224          $args = (array) $args;
 225  
 226          if ( isset( $args['translations'] ) && is_array( $args['translations'] ) ) {
 227              // Reduce range by one since we're starting at 0, see GH#516.
 228              foreach ( range( 0, $this->get_static( 'number_of_plural_translations' ) - 1 ) as $i ) {
 229                  if ( isset( $args['translations'][ $i ] ) ) {
 230                      $args["translation_$i"] = $args['translations'][ $i ];
 231                  }
 232              }
 233              unset( $args['translations'] );
 234          }
 235  
 236          // Reduce range by one since we're starting at 0, see GH#516.
 237          foreach ( range( 0, $this->get_static( 'number_of_plural_translations' ) - 1 ) as $i ) {
 238              if ( isset( $args[ "translation_$i" ] ) ) {
 239                  $args[ "translation_$i" ] = $args[ "translation_$i" ];
 240              }
 241          }
 242  
 243          if ( gp_array_get( $args, 'warnings' ) == array() ) {
 244              $args['warnings'] = null;
 245          }
 246  
 247          return $args;
 248      }
 249  
 250  	public function prepare_fields_for_save( $args ) {
 251          $args = parent::prepare_fields_for_save( $args );
 252  
 253          /**
 254           * Filters the translation fields before they are saved.
 255           *
 256           * @since 3.0.0
 257           *
 258           * @param array          $args Translation arguments.
 259           * @param GP_Translation $this Translation class.
 260           */
 261          $args = apply_filters( 'gp_translation_prepare_for_save', $args, $this );
 262  
 263          if ( is_array( gp_array_get( $args, 'warnings' ) ) ) {
 264              $args['warnings'] = serialize( $args['warnings'] );
 265          }
 266  
 267          return $args;
 268      }
 269  
 270      /**
 271       * Sets restriction rules for fields.
 272       *
 273       * @since 1.0.0
 274       *
 275       * @param GP_Validation_Rules $rules The validation rules instance.
 276       */
 277  	public function restrict_fields( $rules ) {
 278          $rules->translation_0_should_not_be( 'empty_string' );
 279          $rules->translation_1_should_not_be( 'empty_string' );
 280          $rules->translation_2_should_not_be( 'empty_string' );
 281          $rules->translation_3_should_not_be( 'empty_string' );
 282          $rules->translation_4_should_not_be( 'empty_string' );
 283          $rules->translation_5_should_not_be( 'empty_string' );
 284          $rules->status_should_not_be( 'empty' );
 285          $rules->original_id_should_be( 'positive_int' );
 286          $rules->translation_set_id_should_be( 'positive_int' );
 287          $rules->user_id_should_be( 'positive_int' );
 288          $rules->user_id_last_modified_should_not_be( 'empty_string' );
 289      }
 290  
 291  
 292  	public function set_fields( $db_object ) {
 293          parent::set_fields( $db_object );
 294          if ( $this->warnings ) {
 295              $this->warnings = maybe_unserialize( $this->warnings );
 296          }
 297      }
 298  
 299  	public function for_export( $project, $translation_set, $filters =  null ) {
 300          return GP::$translation->for_translation( $project, $translation_set, 'no-limit', $filters? $filters : array( 'status' => 'current_or_untranslated' ) );
 301      }
 302  
 303  	public function for_translation( $project, $translation_set, $page, $filters = array(), $sort = array() ) {
 304          global $wpdb;
 305  
 306          $locale               = GP_Locales::by_slug( $translation_set->locale );
 307          $root_locale          = null;
 308          $root_translation_set = null;
 309          $has_root             = false;
 310  
 311          if ( null !== $locale->variant_root ) {
 312              $root_for_translation = array();
 313  
 314              $root_locale          = GP_Locales::by_slug( $locale->variant_root );
 315              $root_translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set->slug, $locale->variant_root );
 316  
 317              // Only set the root translation flag if we have a valid root translation set, otherwise there's no point in querying it later.
 318              if ( null !== $root_translation_set && false !== $root_translation_set ) {
 319                  $has_root = true;
 320              }
 321          }
 322  
 323          $join_type = 'INNER';
 324  
 325          $sort_bys = wp_list_pluck( gp_get_sort_by_fields(), 'sql_sort_by' );
 326  
 327          $default_sort = get_user_option( 'gp_default_sort' );
 328          if ( ! is_array( $default_sort ) ) {
 329              $default_sort = array(
 330                  'by'  => 'priority',
 331                  'how' => 'desc'
 332              );
 333          }
 334  
 335          $sort_by = gp_array_get( $sort_bys, gp_array_get( $sort, 'by' ),  gp_array_get( $sort_bys, $default_sort['by'] ) );
 336          $sort_hows = array('asc' => 'ASC', 'desc' => 'DESC', );
 337          $sort_how = gp_array_get( $sort_hows, gp_array_get( $sort, 'how' ), gp_array_get( $sort_hows, $default_sort['how'] ) );
 338          $collation = 'yes' === gp_array_get( $filters, 'case_sensitive' ) ? 'BINARY' : '';
 339  
 340          $where = array();
 341          if ( gp_array_get( $filters, 'term' ) ) {
 342              $like = "LIKE $collation '%" . ( esc_sql( $wpdb->esc_like( gp_array_get( $filters, 'term' ) ) ) ) . "%'";
 343  
 344              $term_scope = gp_array_get( $filters, 'term_scope', 'scope_any' );
 345  
 346              switch ( $term_scope ) {
 347                  case 'scope_originals':
 348                      $scope_array = array( 'o.singular', 'o.plural' );
 349                      break;
 350                  case 'scope_translations':
 351                      $scope_array = array( 't.translation_0', 't.translation_1' );
 352                      break;
 353                  case 'scope_context':
 354                      $scope_array = array( 'o.context' );
 355                      break;
 356                  case 'scope_references':
 357                      $scope_array = array( 'o.references' );
 358                      break;
 359                  case 'scope_both':
 360                      $scope_array = array( 'o.singular', 't.translation_0', 'o.plural', 't.translation_1' );
 361                      break;
 362                  default:
 363                      $scope_array = array( 'o.singular', 't.translation_0', 'o.plural', 't.translation_1', 'o.context', 'o.references' );
 364                      break;
 365              }
 366  
 367              $mapped_scope_array = array_map(
 368                  function( $x ) use ( $like ) {
 369                      return "($x $like)";
 370                  },
 371                  $scope_array
 372              );
 373  
 374              $where[] = '(' . implode( ' OR ', $mapped_scope_array ) . ')';
 375          }
 376          if ( gp_array_get( $filters, 'before_date_added' ) ) {
 377              $where[] = $wpdb->prepare( 't.date_added > %s', gp_array_get( $filters, 'before_date_added' ) );
 378          }
 379          if ( gp_array_get( $filters, 'translation_id' ) ) {
 380              $where[] = $wpdb->prepare( 't.id = %d', gp_array_get( $filters, 'translation_id' ) );
 381          }
 382          if ( gp_array_get( $filters, 'original_id' ) ) {
 383              $where[] = $wpdb->prepare( 'o.id = %d', gp_array_get( $filters, 'original_id' ) );
 384          }
 385          if ( 'yes' == gp_array_get( $filters, 'warnings' ) ) {
 386              $where[] = 't.warnings IS NOT NULL';
 387              $where[] = 't.warnings != ""';
 388          } elseif ( 'no' == gp_array_get( $filters, 'warnings' ) ) {
 389              $where[] = 't.warnings IS NULL';
 390          }
 391          if ( 'yes' == gp_array_get( $filters, 'with_context' ) ) {
 392              $where[] = 'o.context IS NOT NULL';
 393          }
 394          if ( 'yes' == gp_array_get( $filters, 'with_comment' ) ) {
 395              $where[] = 'o.comment IS NOT NULL AND o.comment <> ""';
 396          }
 397  
 398          if ( gp_array_get( $filters, 'user_login' ) ) {
 399              $user = get_user_by( 'login', $filters['user_login'] );
 400              // do not return any entries if the user doesn't exist
 401              $where[] = $wpdb->prepare( 't.user_id = %d', ($user && $user->ID) ? $user->ID : -1 );
 402          }
 403  
 404          if ( ! GP::$permission->current_user_can( 'write', 'project', $project->id ) ) {
 405              $where[] = 'o.priority > -2';
 406          }
 407  
 408          $priorities = gp_array_get( $filters, 'priority' );
 409          if ( $priorities ) {
 410              $valid_priorities = array_keys( GP::$original->get_static( 'priorities' ) );
 411              $priorities = array_filter( gp_array_get( $filters, 'priority' ), function( $p ) use ( $valid_priorities ) {
 412                  return in_array( intval( $p ), $valid_priorities, true );
 413              } );
 414  
 415              $priorities_where = array();
 416              foreach ( $priorities as $single_priority ) {
 417                  $priorities_where[] = $wpdb->prepare( 'o.priority = %s', $single_priority );
 418              }
 419  
 420              if ( ! empty( $priorities_where ) ) {
 421                  $priorities_where = '(' . implode( ' OR ', $priorities_where ) . ')';
 422                  $where[] = $priorities_where;
 423              }
 424          };
 425  
 426          $join_on = array();
 427          $status = gp_array_get( $filters, 'status', 'current_or_waiting_or_fuzzy_or_untranslated' );
 428          $statuses = explode( '_or_', $status );
 429          if ( in_array( 'untranslated', $statuses ) ) {
 430              if ( $statuses == array( 'untranslated' ) ) {
 431                  $where[] = 't.translation_0 IS NULL';
 432              }
 433              $join_type = 'LEFT';
 434              $join_on[] = 'status != "rejected"';
 435              $join_on[] = 'status != "old"';
 436              $statuses = array_filter( $statuses, function( $x ) {
 437                  return 'untranslated' !== $x ;
 438              } );
 439          }
 440  
 441          $all_statuses = $this->get_static( 'statuses' );
 442          $statuses = array_filter( $statuses, function( $s ) use ( $all_statuses ) {
 443              return in_array( $s, $all_statuses );
 444          } );
 445  
 446          if ( ! empty( $statuses ) ) {
 447              $statuses_where = array();
 448              foreach( $statuses as $single_status ) {
 449                  $statuses_where[] = $wpdb->prepare( 'status = %s', $single_status );
 450              }
 451              $statuses_where = '(' . implode( ' OR ', $statuses_where ) . ')';
 452              $join_on[] = $statuses_where;
 453          }
 454  
 455          /**
 456           * Filter the SQL WHERE clause to get available translations.
 457           *
 458           * @since 1.0.0
 459           *
 460           * @param array              $where           An array of where conditions.
 461           * @param GP_Translation_Set $translation_set Current translation set.
 462           */
 463          $where = apply_filters( 'gp_for_translation_where', $where, $translation_set );
 464  
 465          $where = implode( ' AND ', $where );
 466          if ( $where ) {
 467              $where = 'AND ' . $where;
 468          }
 469  
 470          $where = 'o.project_id = ' . (int) $project->id . ' AND o.status = "+active" ' . $where;
 471  
 472          $join_on = implode( ' AND ', $join_on );
 473          if ( $join_on ) {
 474              $join_on = 'AND ' . $join_on;
 475          }
 476  
 477          $fields = array(
 478              't.*',
 479              'o.*',
 480              't.id as id',
 481              'o.id as original_id',
 482              't.status as translation_status',
 483              'o.status as original_status',
 484              't.date_added as translation_added',
 485              'o.date_added as original_added',
 486          );
 487  
 488          if ( $has_root ) {
 489              $fields[] = 'r.id as root_id';
 490              $fields[] = 'r.original_id as root_original_id';
 491              $fields[] = 'r.translation_set_id as root_translation_set_id';
 492              $fields[] = 'r.translation_0 as root_translation_0';
 493              $fields[] = 'r.translation_1 as root_translation_1';
 494              $fields[] = 'r.translation_2 as root_translation_2';
 495              $fields[] = 'r.translation_3 as root_translation_3';
 496              $fields[] = 'r.translation_4 as root_translation_4';
 497              $fields[] = 'r.translation_5 as root_translation_5';
 498              $fields[] = 'r.user_id as root_user_id';
 499              $fields[] = 'r.status as root_status';
 500              $fields[] = 'r.date_added as root_date_added';
 501              $fields[] = 'r.date_modified as root_date_modified';
 502              $fields[] = 'r.warnings as root_warnings';
 503              $fields[] = 'r.user_id_last_modified as root_user_id_last_modified';
 504          }
 505  
 506          $join      = "$join_type JOIN ( SELECT * FROM {$wpdb->gp_translations} WHERE translation_set_id = " . (int) $translation_set->id . " $join_on ) AS t ON o.id = t.original_id";
 507          $root_join = '';
 508  
 509          if ( $has_root ) {
 510              $root_join = "$join_type JOIN ( SELECT * FROM {$wpdb->gp_translations} WHERE translation_set_id = " . (int) $root_translation_set->id . " $join_on ) AS r ON o.id = r.original_id";
 511          }
 512  
 513          $orderby = sprintf( $sort_by, $sort_how );
 514  
 515          $limit = $this->sql_limit_for_paging( $page, $this->per_page );
 516  
 517          /**
 518           * Filters the 'for_translation' query SQL clauses.
 519           *
 520           * @since 2.3.0
 521           * @since 2.4.0 Removed $join_on and added $root_join clause.  Also added $root_translation_set.
 522           *
 523           * @param array              $pieces          {
 524           *     Translation query SQL clauses.
 525           *
 526           *     @type array  $fields    Fields to select in the query.
 527           *     @type string $join      JOIN clause of the query.
 528           *     @type string $root_join JOIN clause for the root translation set if it exists, otherwise an empty string.
 529           *     @type string $where     WHERE clause of the query.
 530           *     @type string $orderby   Fields for ORDER BY clause.
 531           *     @type string $limit     LIMIT clause of the query.
 532           * }
 533           * @param GP_Translation_Set      $translation_set      The translation set object being queried.
 534           * @param array                   $filters              An array of search filters.
 535           * @param array                   $sort                 An array of sort settings.
 536           * @param GP_Translation_Set|null $root_translation_set The root translation set object if one exists, otherwise null.
 537           */
 538          $clauses = apply_filters( 'gp_for_translation_clauses', compact( 'fields', 'join', 'root_join', 'where', 'orderby', 'limit' ), $translation_set, $filters, $sort, $root_translation_set );
 539  
 540          $fields = isset( $clauses['fields'] ) ? implode( ', ', $clauses['fields'] ) : '*';
 541          $join = isset( $clauses['join'] ) ? $clauses['join'] : '';
 542          $root_join = isset( $clauses['root_join'] ) ? $clauses['root_join'] : '';
 543          $where = isset( $clauses['where'] ) ? $clauses['where'] : '';
 544          $orderby = isset( $clauses['orderby'] ) ? 'ORDER BY ' . $clauses['orderby'] : '';
 545          $limit = isset( $clauses['limit'] ) ? $clauses['limit'] : '';
 546  
 547          $sql_for_translations = "
 548              SELECT SQL_CALC_FOUND_ROWS $fields
 549              FROM {$wpdb->gp_originals} as o
 550              $join
 551              $root_join
 552              WHERE $where $orderby $limit";
 553  
 554          $rows = $this->many_no_map( $sql_for_translations );
 555  
 556          /**
 557           * Filters the rows returned from the for_translation query.
 558           *
 559           * @since 2.3.0
 560           *
 561           * @param array              $rows            Array of arrays returned by the query.
 562           * @param GP_Translation_Set $translation_set The translation set object being queried.
 563           */
 564          $rows = apply_filters( 'gp_for_translation_rows', $rows, $translation_set );
 565  
 566          $this->found_rows = $this->found_rows();
 567          $translations = array();
 568  
 569          foreach( (array)$rows as $row ) {
 570              if ( null === $row->id && $has_root ) {
 571                  $row->id                    = $row->root_id;
 572                  $row->original_id           = $row->root_original_id;
 573                  $row->translation_set_id    = $row->root_translation_set_id;
 574                  $row->translation_0         = $row->root_translation_0;
 575                  $row->translation_1         = $row->root_translation_1;
 576                  $row->translation_2         = $row->root_translation_2;
 577                  $row->translation_3         = $row->root_translation_3;
 578                  $row->translation_4         = $row->root_translation_4;
 579                  $row->translation_5         = $row->root_translation_5;
 580                  $row->user_id               = $row->root_user_id;
 581                  $row->status                = $row->root_status;
 582                  $row->date_added            = $row->root_date_added;
 583                  $row->date_modified         = $row->root_date_modified;
 584                  $row->warnings              = $row->root_warnings;
 585                  $row->user_id_last_modified = $row->root_user_id_last_modified;
 586                  $row->translation_status    = $row->root_status;
 587                  $row->translation_added     = $row->root_date_added;
 588              }
 589  
 590              $row->user = $row->user_last_modified = null;
 591  
 592              if ( $row->user_id && 'no-limit' !== $this->per_page ) {
 593                  $user = get_userdata( $row->user_id );
 594                  if ( $user ) {
 595                      $row->user = (object) array(
 596                          'ID'            => $user->ID,
 597                          'user_login'    => $user->user_login,
 598                          'display_name'  => $user->display_name,
 599                          'user_nicename' => $user->user_nicename,
 600                      );
 601                  }
 602              }
 603  
 604              if ( $row->user_id_last_modified && 'no-limit' !== $this->per_page ) {
 605                  $user = get_userdata( $row->user_id_last_modified );
 606                  if ( $user ) {
 607                      $row->user_last_modified = (object) array(
 608                          'ID'            => $user->ID,
 609                          'user_login'    => $user->user_login,
 610                          'display_name'  => $user->display_name,
 611                          'user_nicename' => $user->user_nicename,
 612                      );
 613                  }
 614              }
 615  
 616              $row->translations = array();
 617              for( $i = 0; $i < $locale->nplurals; $i++ ) {
 618                  $row->translations[] = $row->{"translation_".$i};
 619              }
 620              $row->references = preg_split('/\s+/', $row->references, -1, PREG_SPLIT_NO_EMPTY);
 621              $row->extracted_comments = $row->comment;
 622              $row->warnings = $row->warnings? maybe_unserialize( $row->warnings ) : null;
 623              unset($row->comment);
 624  
 625              // Reduce range by one since we're starting at 0, see GH#516.
 626              foreach ( range( 0, $this->get_static( 'number_of_plural_translations' ) - 1 ) as $i ) {
 627                  $member = "translation_$i";
 628                  unset($row->$member);
 629              }
 630              $row->row_id = $row->original_id . ( $row->id? "-$row->id" : '' );
 631              $translations[] = new Translation_Entry( (array)$row );
 632          }
 633          unset( $rows );
 634          return $translations;
 635      }
 636  
 637  	public function set_as_current() {
 638          $result = $this->update( array( 'status' => 'old' ),
 639          array( 'original_id' => $this->original_id, 'translation_set_id' => $this->translation_set_id, 'status' => 'current' ) )
 640          &&     $this->update( array( 'status' => 'old' ),
 641          array( 'original_id' => $this->original_id, 'translation_set_id' => $this->translation_set_id, 'status' => 'waiting' ) )
 642          && $this->update( array( 'status' => 'old' ),
 643          array( 'original_id' => $this->original_id, 'translation_set_id' => $this->translation_set_id, 'status' => 'fuzzy' ) )
 644          && $this->save( array( 'status' => 'current', 'user_id_last_modified' => get_current_user_id() ) );
 645  
 646          return $result;
 647      }
 648  
 649  	public function reject() {
 650          $this->set_status( 'rejected' );
 651      }
 652  
 653      /**
 654       * Decides whether the status of a translation can be changed to a desired status.
 655       *
 656       * @since 2.3.0
 657       *
 658       * @param string $desired_status The desired status.
 659       * @return bool Whether the status can be set.
 660       */
 661  	public function can_set_status( $desired_status ) {
 662          /**
 663           * Filters the decision whether a translation can be set to a status.
 664           *
 665           * @since 2.3.0
 666           *
 667           * @param string|bool    $can_set_status Whether the user can set the desired status.
 668           * @param GP_Translation $translation    The translation to decide this for.
 669           * @param string         $desired_status The desired status.
 670           */
 671          $can_set_status = apply_filters( 'gp_pre_can_set_translation_status', 'no-verdict', $this, $desired_status );
 672          if ( is_bool( $can_set_status ) ) {
 673              return $can_set_status;
 674          }
 675  
 676          if ( ! is_bool( $can_set_status ) && 'rejected' === $desired_status && get_current_user_id() === (int) $this->user_id ) {
 677              $can_set_status = true;
 678          }
 679  
 680          if ( ! is_bool( $can_set_status ) && ( 'current' === $desired_status || 'rejected' === $desired_status ) ) {
 681              if ( ! GP::$permission->current_user_can( 'approve', 'translation', $this->id, array( 'translation' => $this ) ) ) {
 682                  $can_set_status = false;
 683              }
 684          }
 685  
 686          if ( ! is_bool( $can_set_status ) ) {
 687              $can_set_status = true;
 688          }
 689  
 690          /**
 691           * Filters the decision whether a translation can be set to a status.
 692           *
 693           * @since 2.3.0
 694           *
 695           * @param bool           $can_set_status Whether the user can set the desired status.
 696           * @param GP_Translation $translation    The translation to decide this for.
 697           * @param string         $desired_status The desired status.
 698           */
 699          $can_set_status = apply_filters( 'gp_can_set_translation_status', $can_set_status, $this, $desired_status );
 700  
 701          return $can_set_status;
 702      }
 703  
 704      /**
 705       * Changes the status of a translation if possible.
 706       *
 707       * @since 2.3.0
 708       *
 709       * @param string $status The status to be set.
 710       * @return bool Whether the setting of status was successful.
 711       */
 712  	public function set_status( $status ) {
 713          if ( ! $this->can_set_status( $status ) ) {
 714              return false;
 715          }
 716  
 717          if ( 'current' === $status ) {
 718              $updated = $this->set_as_current();
 719          } else {
 720              $updated = $this->save( array( 'user_id_last_modified' => get_current_user_id(), 'status' => $status ) );
 721          }
 722  
 723          if ( $updated ) {
 724              gp_clean_translation_set_cache( $this->translation_set_id );
 725          }
 726  
 727          return $updated;
 728      }
 729  
 730  	public function translations() {
 731          $translations = array();
 732  
 733          // Reduce range by one since we're starting at 0, see GH#516.
 734          foreach ( range( 0, $this->get_static( 'number_of_plural_translations' ) - 1 ) as $i ) {
 735              $translations[ $i ] = isset( $this->{"translation_$i"} ) ? $this->{"translation_$i"} : null;
 736          }
 737          return $translations;
 738      }
 739  
 740  	public function last_modified( $translation_set ) {
 741          global $wpdb;
 742  
 743          $last_modified = wp_cache_get( $translation_set->id, 'translation_set_last_modified' );
 744          // Cached as "" if no translations.
 745          if ( "" === $last_modified ) {
 746              return false;
 747          } elseif ( false !== $last_modified ) {
 748              return $last_modified;
 749          }
 750  
 751          $last_modified = $wpdb->get_var( $wpdb->prepare( "SELECT date_modified FROM {$this->table} WHERE translation_set_id = %d AND status = %s ORDER BY date_modified DESC LIMIT 1", $translation_set->id, 'current' ) );
 752          wp_cache_set( $translation_set->id, (string) $last_modified, 'translation_set_last_modified' );
 753          return $last_modified;
 754      }
 755  
 756      // Triggers
 757  
 758      /**
 759       * Executes after creating a translation.
 760       *
 761       * @since 1.0.0
 762       *
 763       * @return bool
 764       */
 765  	public function after_create() {
 766          /**
 767           * Fires after a translation was created.
 768           *
 769           * @since 1.0.0
 770           *
 771           * @param GP_Translation $translation Translation that was created.
 772           */
 773          do_action( 'gp_translation_created', $this );
 774  
 775          return true;
 776      }
 777  
 778      /**
 779       * Executes after saving a translation.
 780       *
 781       * @since 1.0.0
 782       * @since 2.4.0 Added the `$original_before` parameter.
 783       *
 784       * @param GP_Translation $translation_before Translation before the update.
 785       * @return bool
 786       */
 787  	public function after_save( $translation_before ) {
 788          /**
 789           * Fires after a translation was saved.
 790           *
 791           * @since 1.0.0
 792           * @since 2.4.0 Added the `$original_before` parameter.
 793           *
 794           * @param GP_Translation $translation        Translation following the update.
 795           * @param GP_Translation $translation_before Translation before the update.
 796           */
 797          do_action( 'gp_translation_saved', $this, $translation_before );
 798  
 799          return true;
 800      }
 801  
 802      /**
 803       * Executes after deleting a translation.
 804       *
 805       * @since 2.0.0
 806       *
 807       * @return bool
 808       */
 809  	public function after_delete() {
 810          /**
 811           * Fires after a translation was deleted.
 812           *
 813           * @since 2.0.0
 814           *
 815           * @param GP_Translation $translation Translation that was deleted.
 816           */
 817          do_action( 'gp_translation_deleted', $this );
 818  
 819          return true;
 820      }
 821  }
 822  
 823  GP::$translation = new GP_Translation();


Generated: Sat Aug 17 01:01:56 2019 Cross-referenced by PHPXref 0.7.1