Making WordPress.org

Changeset 1739


Ignore:
Timestamp:
07/14/2015 05:42:00 AM (11 years ago)
Author:
dd32
Message:

Translate: Add paging to the project portals. Currently paging is set to 20 per page.
See #1091

Location:
sites/trunk/translate.wordpress.org
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-routes/routes/locale.php

    r1734 r1739  
    1515         */
    1616        public function get_locale_projects( $locale_slug, $set_slug = 'default', $project_path = 'wp' ) {
     17                global $gpdb;
     18
     19                $per_page = 20;
     20                $page = (int) gp_get( 'page', 1 );
     21                $search = gp_get( 's', '' );
     22
    1723                $locale = GP_Locales::by_slug( $locale_slug );
    1824                if ( ! $locale ) {
     
    2531                }
    2632
    27                 $sub_projects = $this->get_active_sub_projects( $project );
    28                 if ( ! $sub_projects ) {
    29                         return $this->die_with_404();
    30                 }
    31 
    32                 usort( $sub_projects, array( $this, '_sort_name_callback' ) );
     33                $paged_sub_projects = $this->get_paged_active_sub_projects(
     34                        $project,
     35                        array(
     36                                'page' => $page,
     37                                'per_page' => $per_page,
     38                                'orderby' => 'name',
     39                                'search' => $search,
     40                                'set_slug' => $set_slug,
     41                                'locale' => $locale_slug,
     42                        )
     43                );
     44
     45                if ( ! $paged_sub_projects ) {
     46                        return $this->die_with_404();
     47                }
     48
     49                $sub_projects   = $paged_sub_projects['projects'];
     50                $pages          = $paged_sub_projects['pages'];
     51                unset( $paged_sub_projects );
    3352
    3453                $project_status = $project_icons = array();
    3554                foreach ( $sub_projects as $key => $sub_project ) {
    36                         $status = $this->get_project_status( $sub_project, $locale_slug, $set_slug );
    37                         if ( ! $status->all_count ) {
    38                                 unset( $sub_projects[ $key ] );
    39                         }
    40 
    41                         $project_status[ $sub_project->id ] = $status;
     55                        $project_status[ $sub_project->id ] = $this->get_project_status( $sub_project, $locale_slug, $set_slug );
    4256                        $project_icons[ $sub_project->id ] = $this->get_project_icon( $project, $sub_project );
    4357                }
     58
     59                $project_ids = array_keys( $project_status );
     60                $project_ids[] = $project->id;
     61                $project_ids = array_merge(
     62                        $project_ids,
     63                        $gpdb->get_col( "SELECT id FROM {$gpdb->projects} WHERE parent_project_id IN(" . implode(', ', $project_ids  ) . ")" )
     64                );
    4465
    4566                $contributors_count = wp_cache_get( 'contributors-count', 'wporg-translate' );
     
    5172                usort( $top_level_projects, array( $this, '_sort_reverse_name_callback' ) );
    5273
    53                 $variants = $this->get_locale_variants( $locale_slug, array_keys( $project_status ) );
     74                $variants = $this->get_locale_variants( $locale_slug, $project_ids );
     75                // If there were no results for the current variant in the current project branch, it should still show it.
     76                if ( ! in_array( $set_slug, $variants, true ) ) {
     77                        $variants[] = $set_slug;
     78                }
    5479
    5580                $this->tmpl( 'locale-projects', get_defined_vars() );
     
    119144                        case 'wp':
    120145                                return '<div class="wordpress-icon"><span class="dashicons dashicons-wordpress-alt"></span></div>';
     146                        case 'wp-themes':
     147                                $screenshot = gp_get_meta( 'wp-themes', $sub_project->id, 'screenshot' );
     148                                if ( $screenshot ) {
     149                                        return '<div class="theme icon"><img src="https://i0.wp.com/' . $screenshot . '?w=' . $size . '&amp;strip=all"></div>';
     150                                } else {
     151                                        return '<div class="default-icon"><span class="dashicons dashicons-admin-appearance"></span></div>';
     152                                }
    121153                        case 'bbpress':
    122154                        case 'buddypress':
     
    255287        }
    256288
     289        /**
     290         * Retrieves active sub projects with paging.
     291         *
     292         * This method is horribly inefficient when there exists many sub-projects, as it can't use SQL.
     293         *
     294         * @param GP_Project $project           The parent project
     295         * @param array $args {
     296         *      @type int    $per_page Number of items per page. Default 20
     297         *      @type int    $page     The page of results to view. Default 1.
     298         *      @type string $orderby  The field to order by, id or name. Default id.
     299         *      @type string $order    The sorting order, ASC or DESC. Default ASC.
     300         *      @type string $search   The search string
     301         *      @type string $set_slug The translation set to view.
     302         *      @type string $locale   The locale of the translation set to view.
     303         * }
     304         * @return array List of sub projects.
     305         */
     306        private function get_paged_active_sub_projects( $project, $args = array() ) {
     307                global $gpdb;
     308
     309                $defaults = array(
     310                        'per_page' => 20,
     311                        'page'     => 1,
     312                        'orderby'  => 'id',
     313                        'order'    => 'ASC',
     314                        'search'   => false,
     315                        'set_slug' => '',
     316                        'locale'   => '',
     317                );
     318                $r = wp_parse_args( $args, $defaults );
     319                extract( $r, EXTR_SKIP );
     320
     321                $order = ( 'ASC' === $order ) ? 'ASC' : 'DESC';
     322                $orderby = ( in_array( $orderby, array( 'id', 'name' ) ) ? $orderby : 'id' );
     323
     324                $limit_sql = '';
     325                if ( $per_page ) {
     326                        $limit_sql = $gpdb->prepare( 'LIMIT %d, %d', ( $page - 1 ) * $per_page, $per_page );
     327                }
     328                $search_sql = '';
     329                if ( $search ) {
     330                        $esc_search = '%%' . like_escape( $search ) . '%%';
     331                        $search_sql = $gpdb->prepare( 'AND ( tp.name LIKE %s OR tp.slug LIKE %s )', $esc_search, $esc_search );
     332                }
     333
     334                /*
     335                 * Find all child projects with translation sets that match the current locale/slug.
     336                 *
     337                 * 1. We need to fetch all sub-projects of the current project (so, if we're at wp-plugins, we want akismet, debug bar, importers, etc)
     338                 * 2. Next, we fetch the sub-projects of those sub-projects, that gets us things like Development, Readme, etc.
     339                 * 3. Next, we fetch the translation sets of both the sub-projects(1), and any sub-sub-projects(2).
     340                 * Once we have the sets in 3, we can then check to see if there exists any translation sets for the current (locale, slug) (ie. en-au/default)
     341                 * If not, we can simply filter them out, so that paging only has items returned that actually exist.
     342                 */
     343                $translation_sets_table = GP::$translation_set->table;
     344                $_projects = $project->many( "
     345                        SELECT SQL_CALC_FOUND_ROWS tp.*
     346                                FROM {$project->table} tp
     347                                LEFT JOIN {$project->table} tp_sub ON tp.id = tp_sub.parent_project_id AND tp_sub.active = 1
     348                                LEFT JOIN {$translation_sets_table} sets ON sets.project_id = tp.id AND sets.locale = %s AND sets.slug = %s
     349                                LEFT JOIN {$translation_sets_table} sets_sub ON sets_sub.project_id = tp_sub.id AND sets_sub.locale = %s AND sets_sub.slug = %s
     350                        WHERE
     351                                tp.parent_project_id = %d
     352                                AND tp.active = 1
     353                                AND ( sets.id IS NOT NULL OR sets_sub.id IS NOT NULL )
     354                                $search_sql
     355                        GROUP BY tp.id
     356                        ORDER BY tp.$orderby $order
     357                        $limit_sql
     358                ", $locale, $set_slug, $locale, $set_slug, $project->id  );
     359
     360                $results = (int) $project->found_rows();
     361                $pages = (int)ceil( $results / $per_page );
     362
     363                $projects = array();
     364                foreach ( $_projects as $project ) {
     365                        $projects[ $project->id ] = $project;
     366                }
     367
     368                return array(
     369                        'pages' => compact( 'pages', 'page', 'per_page', 'results' ),
     370                        'projects' => $projects,
     371                );
     372        }
     373
    257374        private function _sort_reverse_name_callback( $a, $b ) {
    258375                return - strcasecmp( $a->name, $b->name );
  • sites/trunk/translate.wordpress.org/public_html/gp-templates/locale-projects.php

    r1734 r1739  
    6868        </ul>
    6969        <div class="search-form">
    70                 <label class="screen-reader-text" for="projects-filter"><?php esc_attr_e( 'Search projects...' ); ?></label>
    71                 <input placeholder="<?php esc_attr_e( 'Search projects...' ); ?>" type="search" id="projects-filter" class="filter-search">
     70                <form>
     71                        <label class="screen-reader-text" for="projects-filter"><?php esc_attr_e( 'Search projects...' ); ?></label>
     72                        <input placeholder="<?php esc_attr_e( 'Search projects...' ); ?>" type="search" id="projects-filter" name="s" value="<?php if ( !empty( $search ) ) { echo esc_attr( $search ); } ?>" class="filter-search">
     73                        <input type="submit" value="<?php esc_attr_e( 'Search' ); ?>" class="screen-reader-text" />
     74                </form>
    7275        </div>
    7376</div>
     
    140143        ?>
    141144</div>
     145<?php
     146        if ( isset( $pages ) && $pages['pages'] > 1 ) {
     147                echo gp_pagination( $pages['page'], $pages['per_page'], $pages['results'] );
     148        }
     149?>
    142150
    143151<script>
    144152        jQuery( document ).ready( function( $ ) {
     153                // Don't filter if there's an existing search term, or if we're paginated
     154                // Fall back to a full page reload for those cases.
     155                var live_filtering_enabled = ( ! $( '#projects-filter' ).val() && ! $( '.paging' ).length );
    145156                $rows = $( '#projects' ).find( '.project' );
    146157                $( '#projects-filter' ).on( 'input keyup', function() {
     158                        if ( ! live_filtering_enabled ) {
     159                                return;
     160                        }
     161
    147162                        var words = this.value.toLowerCase().split( ' ' );
    148163
Note: See TracChangeset for help on using the changeset viewer.