Changeset 2864 for sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/routes/wp-plugins.php
- Timestamp:
- 03/31/2016 08:25:15 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/routes/wp-plugins.php
r2663 r2864 1 1 <?php 2 2 3 class WPorg_GP_Route_WP_Plugins extends GP_Route{3 class WPorg_GP_Route_WP_Plugins extends WPorg_GP_Route_WP_Directory { 4 4 5 5 /** … … 94 94 } ); 95 95 96 if ( function_exists( 'wporg_get_plugin_icon' ) ) { 97 $project->icon = wporg_get_plugin_icon( $project->slug, 64 ); 98 } else { 99 $project->icon = '<div class="default-icon"><span class="dashicons dashicons-admin-plugins"></span></div>'; 100 } 96 $project->icon = $this->get_plugin_icon( $project, 64 ); 101 97 102 98 $this->tmpl( 'projects-wp-plugins', get_defined_vars() ); … … 117 113 } 118 114 119 if ( function_exists( 'wporg_get_plugin_icon' ) ) { 120 $project->icon = wporg_get_plugin_icon( $project->slug, 64 ); 121 } else { 122 $project->icon = '<div class="default-icon"><span class="dashicons dashicons-admin-plugins"></span></div>'; 115 $project->icon = $this->get_plugin_icon( $project, 64 ); 116 117 $contributors_by_locale = gp_get_meta( 'wp-plugins', $project->id, 'contributors-by-locale' ); 118 if ( ! $contributors_by_locale || $contributors_by_locale['last_updated'] + HOUR_IN_SECONDS < time() ) { 119 $contributors_by_locale = $this->get_contributors( $project ); 120 $contributors_by_locale['last_updated'] = time(); 121 gp_update_meta( $project->id, 'contributors-by-locale', $contributors_by_locale, 'wp-plugins' ); 123 122 } 124 123 125 $contributors_by_locale = array(); 126 $default_value = array( 127 'count' => 0, 128 'editors' => array(), 129 'contributors' => array(), 130 ); 131 132 $translation_editors = $wpdb->get_results( $wpdb->prepare( " 133 SELECT 134 `user_id`, `locale` 135 FROM {$wpdb->wporg_translation_editors} 136 WHERE `project_id` = %d 137 ", $project->id ), OBJECT ); 138 139 foreach ( $translation_editors as $translation_editor ) { 140 if ( ! isset( $contributors_by_locale[ $translation_editor->locale ] ) ) { 141 $contributors_by_locale[ $translation_editor->locale ] = $default_value; 142 } 143 144 $user = get_user_by( 'id', $translation_editor->user_id ); 145 if ( ! $user ) { 146 continue; 147 } 148 149 $contributors_by_locale[ $translation_editor->locale ]['editors'][ $translation_editor->user_id ] = (object) array( 150 'nicename' => $user->user_nicename, 151 'display_name' => $this->_encode( $user->display_name ), 152 ); 153 154 $contributors_by_locale[ $translation_editor->locale ]['count']++; 124 $chart_data = gp_get_meta( 'wp-plugins', $project->id, 'contributors-chart-data' ); 125 if ( ! $chart_data || $chart_data['last_updated'] + DAY_IN_SECONDS < time() ) { 126 $chart_data = $this->get_contributors_chart_data( $project ); 127 $chart_data['last_updated'] = time(); 128 gp_update_meta( $project->id, 'contributors-chart-data', $chart_data, 'wp-plugins' ); 155 129 } 156 130 157 unset( $translation_editors ); 158 159 $sub_projects = $wpdb->get_col( $wpdb->prepare( " 160 SELECT id 161 FROM {$wpdb->gp_projects} 162 WHERE parent_project_id = %d 163 ", $project->id ) ); 164 165 foreach ( $sub_projects as $sub_project ) { 166 foreach( $this->get_translation_contributors_by_locale( $sub_project ) as $row ) { 167 if ( ! isset( $contributors_by_locale[ $row->locale ] ) ) { 168 $contributors_by_locale[ $row->locale ] = $default_value; 169 } 170 171 if ( isset( $contributors_by_locale[ $row->locale ]['editors'][ $row->user_id ] ) ) { 172 continue; 173 } 174 175 if ( isset( $contributors_by_locale[ $row->locale ]['contributors'][ $row->user_id ] ) ) { 176 continue; 177 } 178 179 $user = get_user_by( 'id', $row->user_id ); 180 if ( ! $user ) { 181 continue; 182 } 183 184 $contributors_by_locale[ $row->locale ]['contributors'][ $row->user_id ] = (object) array( 185 'nicename' => $user->user_nicename, 186 'display_name' => $this->_encode( $user->display_name ), 187 ); 188 189 $contributors_by_locale[ $row->locale ]['count']++; 190 } 191 } 192 193 $chart_data = $this->get_plugin_contributors_chart_data( $project->id, $sub_projects ); 131 unset( $contributors_by_locale['last_updated'], $chart_data['last_updated'] ); 194 132 195 133 $this->tmpl( 'projects-wp-plugins-contributors', get_defined_vars() ); 196 }197 198 /**199 * Generates the chart data for contributors activity.200 *201 * @param int $project_id The ID of a project. Used to store data as meta.202 * @param array $sub_projects Optional. IDs of sub-projects.203 * @return array The data to build a chart via Chartist.js.204 */205 private function get_plugin_contributors_chart_data( $project_id, $sub_projects = null ) {206 $chart_data = gp_get_meta( 'wp-plugins', $project_id, 'contributors-chart-data' );207 if ( $chart_data ) {208 if ( $chart_data['last_updated'] + DAY_IN_SECONDS > time() ) {209 return $chart_data;210 }211 }212 213 global $wpdb;214 215 if ( ! $sub_projects ) {216 $sub_projects = $wpdb->get_col( $wpdb->prepare( "217 SELECT id218 FROM {$wpdb->gp_projects}219 WHERE parent_project_id = %d220 ", $$project_id ) );221 }222 223 $translation_set_ids = $wpdb->get_col( "224 SELECT `id` FROM {$wpdb->gp_translation_sets} WHERE `project_id` IN (" . implode( ',', $sub_projects ) . ")225 " );226 227 if ( ! $translation_set_ids ) {228 return array();229 }230 231 $date_begin = new DateTime( '-6 day' );232 $date_end = new DateTime( 'NOW' );233 $date_interval = new DateInterval( 'P1D' );234 $date_range = new DatePeriod( $date_begin, $date_interval, $date_end );235 236 $days = array();237 foreach( $date_range as $date ) {238 $days[] = $date->format( 'Y-m-d' );239 }240 $days[] = $date_end->format( 'Y-m-d' );241 242 $counts = $wpdb->get_results( "243 SELECT244 DATE(date_modified) AS `day`, COUNT(*) AS `count`, `status`245 FROM {$wpdb->gp_translations}246 WHERE247 `translation_set_id` IN (" . implode( ',', $translation_set_ids ) . ")248 AND date_modified >= ( CURDATE() - INTERVAL 7 DAY )249 GROUP BY `status`, `day`250 ORDER BY `day` DESC251 " );252 253 $status = array( 'current', 'waiting', 'rejected' );254 $data = [];255 foreach ( $days as $day ) {256 $data[ $day ] = array_fill_keys( $status, 0 );257 foreach ( $counts as $count ) {258 if ( $count->day !== $day || ! in_array( $count->status, $status ) ) {259 continue;260 }261 262 $data[ $day ][ $count->status ] = (int) $count->count;263 }264 }265 266 $labels = array_keys( $data );267 array_pop( $labels );268 $labels[] = ''; // Don't show a label for today269 270 $series = array();271 $series_data = array_values( $data );272 foreach ( $status as $stati ) {273 $series[] = (object) array(274 'name' => $stati,275 'data' => wp_list_pluck( $series_data, $stati ),276 );277 }278 279 $last_updated = time();280 $chart_data = compact( 'labels', 'series', 'last_updated' );281 282 gp_update_meta( $project_id, 'contributors-chart-data', $chart_data, 'wp-plugins' );283 284 return $chart_data;285 134 } 286 135 … … 297 146 } 298 147 299 if ( function_exists( 'wporg_get_plugin_icon' ) ) { 300 $project->icon = wporg_get_plugin_icon( $project->slug, 64 ); 301 } else { 302 $project->icon = '<div class="default-icon"><span class="dashicons dashicons-admin-plugins"></span></div>'; 303 } 148 $project->icon = $this->get_plugin_icon( $project, 64 ); 304 149 305 150 $http_context = stream_context_create( array( … … 315 160 316 161 /** 317 * Retrieves t ranslators of a specific project.162 * Retrieves the icon of a plugin. 318 163 * 319 * @param int $project_id Project ID. 320 * @return object Translators of the project. 164 * @param GP_Project $project The plugin project. 165 * @param int $size Optional. The size of the icon. Default 64. 166 * @return string HTML markup for the icon. 321 167 */ 322 private function get_ translation_contributors_by_locale( $project_id) {323 global $wpdb;168 private function get_plugin_icon( $project, $size = 64 ) { 169 $default = '<div class="default-icon"><span class="dashicons dashicons-admin-plugins"></span></div>'; 324 170 325 $sql = $wpdb->prepare( " 326 SELECT ts.`locale`, ts.`slug` AS `locale_slug`, t.`user_id` 327 FROM `{$wpdb->gp_translations}` t, `{$wpdb->gp_translation_sets}` ts 328 WHERE t.`translation_set_id` = ts.`id` 329 AND t.`user_id` IS NOT NULL AND t.`user_id` != 0 330 AND t.`date_modified` > %s 331 AND ts.`project_id` = %d 332 AND t.`status` <> 'rejected' 333 GROUP BY ts.`locale`, ts.`slug`, t.`user_id` 334 ", date( 'Y-m-d', time() - YEAR_IN_SECONDS ), $project_id ); 171 if ( function_exists( 'wporg_get_plugin_icon' ) ) { 172 $icon = wporg_get_plugin_icon( $project->slug, $size ); 173 } 335 174 336 return $wpdb->get_results( $sql ); 337 } 175 if ( $icon ) { 176 return $icon; 177 } 338 178 339 private function _encode( $raw ) { 340 $raw = mb_convert_encoding( $raw, 'UTF-8', 'ASCII, JIS, UTF-8, Windows-1252, ISO-8859-1' ); 341 return ent2ncr( htmlspecialchars_decode( htmlentities( $raw, ENT_NOQUOTES, 'UTF-8' ), ENT_NOQUOTES ) ); 179 return $default; 342 180 } 343 181 }
Note: See TracChangeset
for help on using the changeset viewer.