Making WordPress.org


Ignore:
Timestamp:
12/11/2016 08:34:14 PM (8 years ago)
Author:
ocean90
Message:

Translate, Rosetta Roles: Add support for the new translation object type.

See https://github.com/GlotPress/GlotPress-WP/pull/538.
See #2000.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-rosetta-roles/inc/class-plugin.php

    r4395 r4510  
    100100
    101101        // No permissions for unknown object types.
    102         if ( ! in_array( $args['object_type'], array( 'project|locale|set-slug', 'translation-set' ) ) ) {
    103             return false;
     102        if ( ! in_array( $args['object_type'], array( 'project|locale|set-slug', 'translation-set', 'translation' ), true ) ) {
     103            return false;
     104        }
     105
     106        // Allow logged in users to submit translations.
     107        if ( 'edit' == $args['action'] && 'translation-set' === $args['object_type'] ) {
     108            return is_user_logged_in();
    104109        }
    105110
    106111        // Get locale and current project ID.
    107         $locale_and_project_id = (object) $this->get_locale_and_project_id( $args['object_type'], $args['object_id'] );
     112        $locale_and_project_id = (object) $this->get_locale_and_project_id( $args['object_type'], $args['object_id'], $args );
    108113        if ( ! $locale_and_project_id ) {
    109114            return false;
     
    482487     * @param string $object_type Current object type.
    483488     * @param string $object_id   Current object ID.
     489     * @param array  $args        Optional. Array of additional arguments.
    484490     * @return array|false Locale and project ID, false on failure.
    485491     */
    486     public function get_locale_and_project_id( $object_type, $object_id ) {
     492    public function get_locale_and_project_id( $object_type, $object_id, $args = array() ) {
     493        static $set_cache = array();
     494
    487495        switch ( $object_type ) {
     496            case 'translation' :
     497                if ( empty( $args['extra']['translation']->translation_set_id ) ) {
     498                    break;
     499                }
     500
     501                $translation_set_id = $args['extra']['translation']->translation_set_id;
     502                if ( isset( $set_cache[ $translation_set_id ] ) ) {
     503                    $set = $set_cache[ $translation_set_id ];
     504                } else {
     505                    $set = GP::$translation_set->get( $args['extra']['translation']->translation_set_id );
     506                    $set_cache[ $translation_set_id ] = $set;
     507                }
     508
     509                return array( 'locale' => $set->locale, 'project_id' => (int) $set->project_id );
     510
    488511            case 'translation-set' :
    489                 $set = GP::$translation_set->get( $object_id );
     512                if ( isset( $set_cache[ $object_id ] ) ) {
     513                    $set = $set_cache[ $object_id ];
     514                } else {
     515                    $set = GP::$translation_set->get( $object_id );
     516                    $set_cache[ $object_id ] = $set;
     517                }
     518
    490519                return array( 'locale' => $set->locale, 'project_id' => (int) $set->project_id );
    491520
     
    494523                return array( 'locale' => $locale, 'project_id' => (int) $project_id );
    495524        }
     525
    496526        return false;
    497527    }
Note: See TracChangeset for help on using the changeset viewer.