Making WordPress.org

Changeset 5441


Ignore:
Timestamp:
04/29/2017 09:52:00 PM (9 years ago)
Author:
ocean90
Message:

Plugin Directory: Cache API requests to translate.wordpress.org.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-i18n.php

    r5440 r5441  
    11<?php
    22namespace WordPressdotorg\Plugin_Directory;
     3
     4use GP_Locales;
     5use WP_Http;
    36
    47/**
     
    330333
    331334                require_once GLOTPRESS_LOCALES_PATH;
    332                 $gp_locale = \GP_Locales::by_field( 'wp_locale', $wp_locale );
     335                $gp_locale = GP_Locales::by_field( 'wp_locale', $wp_locale );
    333336
    334337                if ( ! $gp_locale || 'en' === $gp_locale->slug ) {
     
    441444                $post = get_post( $post );
    442445
    443                 // This naively hits the API. It could probably be re-written to query the DB instead.
    444                 $api_url = esc_url_raw( 'https://translate.wordpress.org/api/projects/wp-plugins/' . $post->post_name . '/' . $branch, array( 'https' ) );
    445 
    446                 $locales = array();
    447                 $http    = new \WP_Http();
    448                 $result  = $http->request( $api_url );
    449 
    450                 if ( ! is_wp_error( $result ) ) {
    451                         $data = json_decode( $result['body'] );
    452 
    453                         if ( ! empty( $data->translation_sets ) ) {
    454                                 $locales = array_filter( $data->translation_sets, function( $locale ) use ( $min_percent ) {
    455                                         return $locale->percent_translated >= $min_percent;
    456                                 } );
     446                $cache_suffix = 'translation_sets';
     447
     448                $translation_sets = $this->cache_get( $post->post_name, $branch, $cache_suffix );
     449                if ( false === $translation_sets ) {
     450                        $api_url = esc_url_raw( 'https://translate.wordpress.org/api/projects/wp-plugins/' . $post->post_name . '/' . $branch, [ 'https' ] );
     451                        $response = wp_remote_get( $api_url );
     452
     453                        if ( is_wp_error( $response ) || WP_Http::OK !== wp_remote_retrieve_response_code( $response ) ) {
     454                                $translation_sets = [];
     455                        } else {
     456                                $result = json_decode( wp_remote_retrieve_body( $response ) );
     457                                $translation_sets = isset( $result->translation_sets ) ? $result->translation_sets : [];
    457458                        }
    458                 }
     459
     460                        $this->cache_set( $post->post_name, $branch, $translation_sets, $cache_suffix );
     461                }
     462
     463                $locales = array_filter( $translation_sets, function( $locale ) use ( $min_percent ) {
     464                        return $locale->percent_translated >= $min_percent;
     465                } );
    459466
    460467                return $locales;
Note: See TracChangeset for help on using the changeset viewer.