Ticket #95: translation-race.php

File translation-race.php, 5.2 KB (added by Nightgunner5, 22 months ago)
Line 
1<?php
2
3class Translation_Race extends GP_Plugin {
4        var $id = 'translation-race';
5
6        var $projects = array();
7        var $sub_projects = array();
8
9        function __construct() {
10                parent::__construct();
11
12                $this->add_action( 'pre_tmpl_load', array( 'args' => 2 ) );
13                $this->add_filter( 'gp_url', array( 'args' => 3 ) );
14                $this->add_action( 'project_template_translation_set_extra', array( 'args' => 2 ) );
15                $this->add_action( 'gettext', array( 'args' => 3 ) );
16        }
17
18        function pre_tmpl_load( $template, $args ) {
19                if ( isset( $args['projects'] ) ) {
20                        $this->projects = $args['projects'];
21                        wp_enqueue_style( 'translation-race', '/../plugins/translation-race.css' );
22                }
23
24                if ( isset( $args['sub_projects'] ) ) {
25                        $this->sub_projects = $args['sub_projects'];
26                        wp_enqueue_style( 'translation-race', '/../plugins/translation-race.css' );
27                }
28
29                if ( isset( $args['translation_sets'] ) ) {
30                        ob_start( array( &$this, 'set_obhandler' ) );
31                }
32        }
33
34        function gp_url( $url, $path, $query ) {
35                if ( is_array( $path ) && $path[0] == 'projects' && strpos( '/' . implode( '/', $path ), '/-' ) === false ) {
36                        global $gpdb;
37
38                        $project = current( $this->projects );
39                        next( $this->projects );
40                        if ( !$project )
41                                return $url;
42
43                        if ( false === $original_count = wp_cache_get( $project->id, 'translation_race_originals_count' ) ) {
44                                $original_count = (int) $gpdb->get_var( $gpdb->prepare( "SELECT COUNT(*) FROM `{$gpdb->originals}` WHERE `project_id` = %d AND `status` LIKE '+%%'", $project->id ) );
45                                wp_cache_add( $project->id, $original_count, 'translation_race_originals_count', 3600 );
46                        }
47                        if ( false === $set_count = wp_cache_get( $project->id, 'translation_race_set_count' ) ) {
48                                $set_count = (int) $gpdb->get_var( $gpdb->prepare( "SELECT COUNT(*) FROM `{$gpdb->translation_sets}` WHERE `project_id` = %d", $project->id ) );
49                                wp_cache_add( $project->id, $set_count, 'translation_race_set_count', 3600 );
50                        }
51
52                        if ( false === $translated_count = wp_cache_get( $project->id, 'translation_race_translated_count' ) ) {
53                                $translated_count = (int) $gpdb->get_var( $gpdb->prepare( "SELECT COUNT(*) FROM `{$gpdb->translations}` AS `t` LEFT JOIN `{$gpdb->translation_sets}` AS `ts` ON `translation_set_id` = `ts`.`id` LEFT JOIN `{$gpdb->originals}` AS `o` ON `original_id` = `o`.`id` WHERE `ts`.`project_id` = %d AND `t`.`status` = 'current' AND `o`.`status` LIKE '+%%'", $project->id ) );
54                                wp_cache_add( $project->id, $translated_count, 'translation_race_translated_count', 3600 );
55                        }
56                        if ( false === $fuzzy_count = wp_cache_get( $project->id, 'translation_race_fuzzy_count' ) ) {
57                                $fuzzy_count = (int) $gpdb->get_var( $gpdb->prepare( "SELECT COUNT(*) FROM `{$gpdb->translations}` LEFT JOIN `{$gpdb->translation_sets}` AS `ts` ON `translation_set_id` = `ts`.`id` WHERE `project_id` = %d AND `status` = 'fuzzy'", $project->id ) );
58                                wp_cache_add( $project->id, $fuzzy_count, 'translation_race_fuzzy_count', 3600 );
59                        }
60
61                        $percent_completed = $set_count && $original_count ? $translated_count / $set_count / $original_count * 100 : 0;
62                        $percent_translated = $set_count && $original_count ? ( $translated_count + $fuzzy_count ) / $set_count / $original_count * 100 : 0;
63
64                        echo '<span class="translation-race" title="', number_format( $translated_count ), ' of ', number_format( $original_count * $set_count ), ' strings translated"><span class="translation-race-progress-bar-fuzzy" style="width: ', $percent_translated, '%;"></span><span class="translation-race-progress-bar" style="width: ', $percent_completed, '%;"></span><span class="translation-race-percentage">', round( $percent_completed, 1 ), '%</span></span> ';
65                }
66
67                return $url;
68        }
69
70        function gettext( $a, $b, $c = 'default' ) {
71                if ( $b == 'Sub-projects of %s:' && $c == 'default' ) {
72                        $this->projects = $this->sub_projects;
73                }
74
75                return $a;
76        }
77
78        function project_template_translation_set_extra( $set, $project ) {
79                global $gpdb;
80
81                if ( false === $original_count = wp_cache_get( $project->id, 'translation_race_originals_count' ) ) {
82                        $original_count = (int) $gpdb->get_var( $gpdb->prepare( "SELECT COUNT(*) FROM `{$gpdb->originals}` WHERE `project_id` = %d AND `status` LIKE '+%%'", $project->id ) );
83                        wp_cache_add( $project->id, $original_count, 'translation_race_originals_count', 3600 );
84                }
85                $translated_count = (int) $set->current_count;
86                $fuzzy_count = (int) $set->fuzzy_count;
87
88                $percent_completed = $original_count ? $translated_count / $original_count * 100 : 0;
89                $percent_translated = $original_count ? ( $translated_count + $fuzzy_count ) / $original_count * 100 : 0;
90
91                echo '<!--translation_race_wrongside--><span class="translation-race" title="', number_format( $translated_count ), ' of ', number_format( $original_count ), ' strings translated"><span class="translation-race-progress-bar-fuzzy" style="width: ', $percent_translated, '%;"></span><span class="translation-race-progress-bar" style="width: ', $percent_completed, '%;"></span><span class="translation-race-percentage">', round( $percent_completed, 1 ), '%</span></span><!--translation_race_endwrongside-->';
92        }
93
94        function set_obhandler( $buffer ) {
95                return preg_replace( '/<li>([\s\S]*?)<!--translation_race_wrongside-->([\s\S]*?)<!--translation_race_endwrongside-->([\s\S]*?)<\/li>/', '<li>$2 $1 $3</li>', $buffer );
96        }
97}
98
99GP::$plugins->translation_race = new Translation_Race();