From 334edeb32d58a20c9cc132ef261ff2e6a52f48ed Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Fri, 20 Sep 2013 19:12:45 +0000 Subject: [PATCH] Send installed language data to the plugin and theme update-check endpoints. Introduces wp_get_installed_language_data() and wp_get_pomo_file_data(). see #18200. git-svn-id: https://develop.svn.wordpress.org/trunk@25520 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/l10n.php | 60 ++++++++++++++++++++++++++++++++++++++ src/wp-includes/update.php | 8 +++-- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 7df80bd3d0..dfffb296ca 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -625,3 +625,63 @@ function get_available_languages( $dir = null ) { return $languages; } + +/** + * Get installed language data. + * + * Looks in the wp-content/languages directory for translations of + * plugins or themes. + * + * @since 3.7.0 + * + * @param string $type What to search for. Accepts 'plugins', 'themes'. + * @return array Array of language data. + */ +function wp_get_installed_language_data( $type ) { + if ( $type !== 'themes' && $type !== 'plugins' ) + return array(); + + if ( ! is_dir( WP_LANG_DIR ) || ! is_dir( WP_LANG_DIR . "/$type" ) ) + return array(); + + $files = scandir( WP_LANG_DIR . "/$type" ); + if ( ! $files ) + return array(); + + $language_data = array(); + + foreach ( $files as $file ) { + if ( '.' === $file[0] || is_dir( $file ) ) + continue; + if ( substr( $file, -3 ) !== '.po' ) + continue; + if ( ! preg_match( '/(.*)-([A-Za-z_]{2,6}).po/', $file, $match ) ) + continue; + + list( , $textdomain, $language ) = $match; + $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "/$type/$file" ); + } + return $language_data; +} + +/** + * Extract headers from a PO file. + * + * @since 3.7.0 + * + * @param string $po_file Path to PO file. + * @return array PO file headers. + */ +function wp_get_pomo_file_data( $po_file ) { + $headers = get_file_data( $po_file, array( + 'POT-Creation-Date' => '"POT-Creation-Date', + 'PO-Revision-Date' => '"PO-Revision-Date', + 'Project-Id-Version' => '"Project-Id-Version', + 'X-Generator' => '"X-Generator', + ) ); + foreach ( $headers as &$header ) { + // Remove possible contextual '\n' and closing double quote. + $header = preg_replace( '~(\\\n)?"$~', '', $header ); + } + return $headers; +} diff --git a/src/wp-includes/update.php b/src/wp-includes/update.php index b0d9612c75..d71542a241 100644 --- a/src/wp-includes/update.php +++ b/src/wp-includes/update.php @@ -146,6 +146,8 @@ function wp_update_plugins() { require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); $plugins = get_plugins(); + $languages = wp_get_installed_language_data( 'plugins' ); + $active = get_option( 'active_plugins', array() ); $current = get_site_transient( 'update_plugins' ); if ( ! is_object($current) ) @@ -200,7 +202,7 @@ function wp_update_plugins() { $options = array( 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3), - 'body' => array( 'plugins' => json_encode( $to_send ) ), + 'body' => array( 'plugins' => json_encode( $to_send ), 'languages' => json_encode( $languages ) ), 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) ); @@ -243,6 +245,8 @@ function wp_update_themes() { return false; $installed_themes = wp_get_themes(); + $languages = wp_get_installed_language_data( 'themes' ); + $last_update = get_site_transient( 'update_themes' ); if ( ! is_object($last_update) ) $last_update = new stdClass; @@ -310,7 +314,7 @@ function wp_update_themes() { $options = array( 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3), - 'body' => array( 'themes' => json_encode( $request ) ), + 'body' => array( 'themes' => json_encode( $request ), 'languages' => json_encode( $languages ) ), 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) );