Making WordPress.org


Ignore:
Timestamp:
07/26/2015 04:42:45 PM (10 years ago)
Author:
ocean90
Message:

Rosetta: Update roles plugin to support sub projects.

props folletto for the design idea.
see #1101.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/rosetta-roles.php

    r1741 r1786  
    211211        add_action( 'load-' . $this->translation_editors_page, array( $this, 'load_translation_editors_page' ) );
    212212        add_action( 'admin_print_scripts-' . $this->translation_editors_page, array( $this, 'enqueue_scripts' ) );
     213        add_action( 'admin_footer-' . $this->translation_editors_page, array( $this, 'print_js_templates' ) );
     214        add_action( 'admin_print_styles-' . $this->translation_editors_page, array( $this, 'enqueue_styles' ) );
    213215    }
    214216
     
    217219     */
    218220    public function enqueue_scripts() {
    219         wp_enqueue_script( 'rosetta-roles', plugins_url( '/js/rosetta-roles.js', __FILE__ ), array( 'jquery' ), '1', true );
     221        wp_enqueue_script( 'rosetta-roles', plugins_url( '/js/rosetta-roles.js', __FILE__ ), array( 'jquery', 'wp-backbone' ), '2', true );
     222    }
     223
     224    /**
     225     * Enqueues styles.
     226     */
     227    public function enqueue_styles() {
     228        wp_enqueue_style( 'rosetta-roles', plugins_url( '/css/rosetta-roles.css', __FILE__ ), array(), '2' );
     229    }
     230
     231    /**
     232     * Prints JavaScript templates.
     233     */
     234    public function print_js_templates() {
     235        ?>
     236        <script id="tmpl-project-checkbox" type="text/html">
     237            <# if ( ! data.checkedSubProjects ) {
     238                #>
     239                <label>
     240                    <input type="checkbox" class="input-checkbox" name="projects[]" value="{{data.id}}"
     241                    <#
     242                    if ( data.checked ) {
     243                        #> checked="checked"<#
     244                    }
     245                    #>
     246                    />
     247                    {{data.name}}
     248                </label>
     249            <# } else { #>
     250                <label>
     251                    <input type="radio" class="input-radio" checked="checked" /> {{data.name}}
     252                </label>
     253            <# } #>
     254        </script>
     255        <?php
    220256    }
    221257
     
    290326                    $this->notify_translation_editor_update( $user_details->ID, 'add' );
    291327
     328                    $meta_key = $wpdb->get_blog_prefix() . $this->project_access_meta_key;
     329
    292330                    $projects = empty( $_REQUEST['projects'] ) ? '' : $_REQUEST['projects'];
    293331                    if ( 'custom' === $projects ) {
     332                        update_user_meta( $user_details->ID, $meta_key, array() );
    294333                        $redirect = add_query_arg( 'user_id', $user_details->ID, $redirect );
    295334                        wp_redirect( add_query_arg( array( 'update' => 'user-added-custom-projects' ), $redirect ) );
     
    297336                    }
    298337
    299                     $meta_key = $wpdb->get_blog_prefix() . $this->project_access_meta_key;
    300338                    update_user_meta( $user_details->ID, $meta_key, array( 'all' ) );
    301339
     
    393431                $redirect = add_query_arg( 'user_id', $user_details->ID, $redirect );
    394432
    395                 $all_projects = $this->get_translate_top_level_projects();
     433                $all_projects = $this->get_translate_projects();
    396434                $all_projects = wp_list_pluck( $all_projects, 'id' );
    397435                $all_projects = array_map( 'intval', $all_projects );
     
    431469        global $wpdb;
    432470
    433         $projects = $this->get_translate_top_level_projects();
     471        $projects = $this->get_translate_projects();
     472        $project_tree = $this->get_project_tree( $projects, 0, 1 );
     473
     474        // Sort the tree and remove array keys.
     475        usort( $project_tree, array( $this, '_sort_name_callback' ) );
     476        foreach ( $project_tree as $key => $project ) {
     477            if ( $project->sub_projects ) {
     478                usort( $project->sub_projects, array( $this, '_sort_name_callback' ) );
     479            }
     480            $project->sub_projects = array_values( $project->sub_projects );
     481        }
     482        $project_tree = array_values( $project_tree );
    434483
    435484        $meta_key = $wpdb->get_blog_prefix() . $this->project_access_meta_key;
     
    438487            $project_access_list = array();
    439488        }
     489
     490        wp_localize_script( 'rosetta-roles', '_rosettaProjectsSettings', array(
     491            'l10n' => array(
     492                'searchPlaceholder' => esc_attr__( 'Search...', 'rosetta' )
     493            ),
     494            'data' => $project_tree,
     495            'accessList' => $project_access_list
     496        ) );
    440497
    441498        $feedback_message = $this->get_feedback_message();
     
    504561        }
    505562
     563        $projects = $this->get_translate_projects();
     564        $project_tree = $this->get_project_tree( $projects, 0, 1 );
     565
    506566        $args = array(
    507567            'user_role'               => $this->translation_editor_role,
    508             'projects'                => $this->get_translate_top_level_projects(),
     568            'projects'                => $projects,
     569            'project_tree'            => $project_tree,
    509570            'project_access_meta_key' => $wpdb->get_blog_prefix() . $this->project_access_meta_key,
    510571        );
     
    535596
    536597    /**
    537      * Fetches all top level projects from translate.wordpress.org.
     598     * Fetches all projects from translate.wordpress.org.
    538599     *
    539600     * @return array List of projects.
    540601     */
    541     private function get_translate_top_level_projects() {
     602    private function get_translate_projects() {
    542603        global $wpdb;
    543 
    544         $cache = get_site_transient( 'translate-top-level-projects' );
    545         if ( false !== $cache ) {
    546             return $cache;
     604        static $projects = null;
     605
     606        if ( null !== $projects ) {
     607            return $projects;
    547608        }
    548609
    549610        $_projects = $wpdb->get_results( "
    550             SELECT id, name
     611            SELECT id, name, parent_project_id
    551612            FROM translate_projects
    552             WHERE parent_project_id IS NULL
    553             ORDER BY name ASC
     613            ORDER BY id ASC
    554614        " );
    555615
     
    559619        }
    560620
    561         set_site_transient( 'translate-top-level-projects', $projects, DAY_IN_SECONDS );
    562 
    563621        return $projects;
    564622    }
     623
     624    /**
     625     * Transforms a flat array to a hierarchy tree.
     626     *
     627     * @param array $projects  The projects.
     628     * @param int   $parent_id Optional. Parent ID. Default 0.
     629     * @param int   $max_depth Optional. Max depth to avoid endless recursion. Default 5.
     630     * @return array The project tree.
     631     */
     632    public function get_project_tree( $projects, $parent_id = 0, $max_depth = 5 ) {
     633        if ( $max_depth < 0 ) { // Avoid an endless recursion.
     634            return;
     635        }
     636
     637        $tree = array();
     638        foreach ( $projects as $project ) {
     639            if ( $project->parent_project_id == $parent_id ) {
     640                $sub_projects = $this->get_project_tree( $projects, $project->id, $max_depth - 1 );
     641                if ( $sub_projects ) {
     642                    $project->sub_projects = $sub_projects;
     643                }
     644
     645                $tree[ $project->id ] = $project;
     646            }
     647        }
     648        return $tree;
     649    }
     650
     651    private function _sort_name_callback( $a, $b ) {
     652        return strnatcasecmp( $a->name, $b->name );
     653    }
    565654}
Note: See TracChangeset for help on using the changeset viewer.