Skip to:
Content

BuddyPress.org

Ticket #4857: 4857.02.patch

File 4857.02.patch, 14.8 KB (added by boonebgorges, 12 years ago)
  • new file p-core/admin/bp-core-translations.php

    diff --git bp-core/admin/bp-core-translations.php bp-core/admin/bp-core-translations.php
    new file mode 100644
    index 0000000..8ea284f
    - +  
     1<?php
     2/**
     3 * Handles automatic download of translations
     4 *
     5 * @package BuddyPress
     6 * @subpackage CoreAdministration
     7 * @since BuddyPress (1.8)
     8 */
     9
     10// Exit if accessed directly
     11if ( ! defined( 'ABSPATH' ) ) exit;
     12
     13/**
     14 * Fetch translations from http://translate.wordpress.org/ and display an update prompt on the admin dashboard.
     15 *
     16 * @since BuddyPress (1.8)
     17 */
     18class BP_Translate {
     19
     20        /**
     21         * Singleton instance of the BP_Translate class
     22         *
     23         * @since BuddyPress (1.8)
     24         * @var BP_Translate
     25         */
     26        private static $instance;
     27
     28        /**
     29         * Return the singleton instance of the BP_Translate class
     30         *
     31         * @return BP_Translate
     32         * @since BuddyPress (1.8)
     33         */
     34        static public function get_instance() {
     35                if ( ! self::$instance )
     36                        self::$instance = new BP_Translate;
     37
     38                return self::$instance;
     39        }
     40
     41        /**
     42         * Constructor
     43         *
     44         * @since BuddyPress (1.8)
     45         */
     46        public function __construct() {
     47                $this->register_actions();
     48                $this->register_cron();     // Intentionally after actions
     49                self::check_for_updated_translation();
     50        }
     51
     52        /**
     53         * Hook into actions necessary to automate the translation process and customise wp-admin
     54         *
     55         * @since BuddyPress (1.8)
     56         */
     57        protected function register_actions() {
     58                add_action( 'bp_translate_update_check',                           array( __CLASS__, 'check_for_updated_translation' ) );
     59                add_action( 'core_upgrade_preamble',                               array( __CLASS__, 'updates_screen' ) );
     60                add_action( 'update-core-custom_do-update-buddypress-translation', array( __CLASS__, 'updates_screen_iframe' ) );
     61                add_action( 'update-custom_update-buddypress-translation',         array( __CLASS__, 'update_translation' ) );
     62        }
     63
     64        /**
     65         * Register cron task to check for language updates
     66         *
     67         * @since BuddyPress (1.8)
     68         */
     69        protected function register_cron() {
     70                if ( ! wp_next_scheduled( 'bp_translate_update_check' ) )
     71                        wp_schedule_event( time(), 'daily', 'bp_translate_update_check' );
     72        }
     73
     74        /**
     75         * Find out if there's a newer translation available for this site on translate.wordpress.org
     76         *
     77         * @since BuddyPress (1.8)
     78         */
     79        static public function check_for_updated_translation() {
     80                $locale = BP_Translate::get_locale();
     81                if ( 'en_US' === $locale )
     82                        return;
     83
     84                // No point checking if we know there's an updated translation
     85                if ( bp_is_translation_update_pending() )
     86                        return;
     87
     88                $locale = BP_Translate::get_glotpress_locale();
     89                if ( ! $locale )
     90                        return;
     91
     92                $url  = 'https://translate.wordpress.org/projects/buddypress/%1$s/%2$s/default/export-translations?format=po';
     93                $args = bp_get_translation_version() ? array( 'headers' => 'If-Modified-Since: ' . gmdate( 'D, d M Y H:i:s', bp_get_translation_version() ) . ' GMT' ) : array();
     94
     95                // Check version of translation on translate.wordpress.org
     96                $response = wp_remote_head( sprintf( $url, buddypress()->glotpress_version, $locale ), $args );
     97                if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) === 304 || wp_remote_retrieve_response_code( $response ) !== 200 )
     98                        return;
     99
     100                // An updated translation is available
     101                bp_update_option( '_bp_translation_pending', true );
     102        }
     103
     104        /**
     105         * Get the current locale
     106         *
     107         * @return string
     108         * @since BuddyPress (1.8)
     109         */
     110        static public function get_locale() {
     111                return 'it_IT'; //apply_filters( 'buddypress_locale', get_locale() );
     112        }
     113
     114        /**
     115         * Get the GlotPress locale code for the current locale
     116         *
     117         * @return string|bool Returns bool if an error occured, otherwise the GlotPress locale as a string
     118         * @since BuddyPress (1.8)
     119         */
     120        static public function get_glotpress_locale() {
     121                static $glotpress_locale;
     122                if ( ! empty( $glotpress_locale ) )
     123                        return $glotpress_locale;
     124
     125                // Get the list of available translations from translate.wordpress.org
     126                $translations = wp_remote_get( sprintf( 'https://translate.wordpress.org/api/projects/buddypress/%1$s', buddypress()->glotpress_version ) );
     127                if ( is_wp_error( $translations ) || wp_remote_retrieve_response_code( $translations ) !== 200 )
     128                        return false;
     129
     130                $translations = json_decode( wp_remote_retrieve_body( $translations ) );
     131                if ( is_null( $translations ) )
     132                        return false;
     133
     134                // Does the requested $locale have an available translation?
     135                $translations = array_shift( wp_list_filter( $translations->translation_sets, array( 'wp_locale' => BP_Translate::get_locale() ) ) );
     136                if ( empty( $translations ) )
     137                        return false;
     138
     139                $glotpress_locale = $translations->locale;
     140                return $glotpress_locale;
     141        }
     142
     143        /**
     144         * If in the WordPress dashboard, maybe bump the "available updates" count if there's a pending translation.
     145         *
     146         * @param array $data Counts and UI strings for available updates
     147         * @return array
     148         * @since BuddyPress (1.8)
     149         */
     150        static public function maybe_bump_update_count( $data ) {
     151                if ( current_user_can( 'update_plugins' ) && bp_is_translation_update_pending() )
     152                        $data['counts']['total']++;
     153
     154                return $data;
     155        }
     156
     157        /**
     158         * If we have a pending translation, draw a message on the wp-admin/update-core.php screen.
     159         *
     160         * @since BuddyPress (1.8)
     161         */
     162        static public function updates_screen() {
     163
     164                if ( BP_Translate::get_locale() === 'en_US' || ! bp_is_translation_update_pending() )
     165                        return;
     166        ?>
     167                <h3><?php _e( 'BuddyPress Translation', 'buddypress' ); ?></h3>
     168                <p><?php _e( 'An updated version of the current BuddyPress translation is available. Click &#8220;Update Translation&#8221;.', 'buddypress' ); ?></p>
     169
     170                <form method="post" action="<?php echo esc_url( 'update-core.php?action=do-update-buddypress-translation' ); ?>" name="update-buddypress-translation" class="upgrade">
     171                        <?php wp_nonce_field( 'update-buddypress-translation' ); ?>
     172
     173                        <p><input class="button" type="submit" value="<?php esc_attr_e( 'Update Translation', 'buddypress' ); ?>" name="upgrade" /></p>
     174                </form>
     175        <?php
     176        }
     177
     178        /**
     179         * We're going to update the BuddyPress translation; output an iframe in which the magic will happen.
     180         *
     181         * This copies the implementation for the Plugin and Theme updates wherein the work is done in a separate
     182         * request that is iframed in. This allows WordPress to recover from any errors during the process.
     183         *
     184         * @since BuddyPress (1.8)
     185         */
     186        static public function updates_screen_iframe() {
     187
     188                if ( ! current_user_can( 'update_plugins' ) )
     189                        wp_die( __( 'You do not have sufficient permissions to update this site.', 'buddypress' ) );
     190
     191                check_admin_referer( 'update-buddypress-translation' );
     192
     193                // If no pending translation updates, redirect away.
     194                if ( ! bp_is_translation_update_pending() ) {
     195                        wp_redirect( admin_url('update-core.php') );
     196                        exit;
     197                }
     198
     199                require_once( ABSPATH . 'wp-admin/admin-header.php' );
     200                $title = __( 'Update BuddyPress Translation', 'buddypress' );
     201                $url   = wp_nonce_url( 'update.php?action=update-buddypress-translation', 'update-buddypress-translation' );
     202
     203                echo '<div class="wrap">';
     204                screen_icon( 'plugins' );
     205                echo '<h2>' . esc_html( $title ) . '</h2>';
     206                echo '<iframe src=' . esc_url( $url ) . ' style="width: 100%; height: 100%; min-height: 750px;" frameborder="0"></iframe>';
     207                echo '</div>';
     208
     209                include( ABSPATH . 'wp-admin/admin-footer.php' );
     210        }
     211
     212        /**
     213         * Download the latest version of the current locale's translation from translate.wordpress.org
     214         *
     215         * @since BuddyPress (1.8)
     216         */
     217        static public function update_translation() {
     218
     219                // Not sure this does anything
     220                if ( ! defined( 'IFRAME_REQUEST' ) )
     221                        define( 'IFRAME_REQUEST', true );
     222
     223                if ( ! current_user_can( 'update_plugins' ) || BP_Translate::get_locale() === 'en_US' )
     224                        wp_die( __( 'You do not have sufficient permissions to update this site.', 'buddypress' ) );
     225
     226                check_admin_referer( 'update-buddypress-translation' );
     227                iframe_header();
     228
     229                echo '<p>'  . __( 'The update process is starting. This process may take a while on some hosts, so please be patient.', 'buddypress' ) . '</p>';
     230                echo '<h4>' . __( 'Updating BuddyPress Translation', 'buddypress' ) . '</h4>';
     231
     232                // Download the .mo to a local temporary file
     233                $url = 'https://translate.wordpress.org/projects/buddypress/%1$s/%2$s/default/export-translations?format=po';
     234                $tmp = download_url( sprintf( $url, buddypress()->glotpress_version, BP_Translate::get_glotpress_locale() ) );
     235
     236                if ( is_wp_error( $tmp ) ) {
     237                        $css_class = 'error';
     238                        $message   = __( 'Error: failure updating translation.', 'buddypress' );
     239                        $message  .= '</p><p><strong>' . $tmp->get_error_message() . '</strong>';
     240
     241                } else {
     242                        $css_class  = 'updated';
     243                        $message    = __( 'Translation updated succesfully!', 'buddypress' );
     244                        $upload_dir = wp_upload_dir();
     245                        $new_file   = sprintf( '%s/buddypress/buddypress-%s.mo', $upload_dir['basedir'], BP_Translate::get_locale() );
     246
     247                        // Check the target folder exists
     248                        @mkdir( $upload_dir['basedir'] . '/buddypress' );
     249
     250                        // Move the file into place
     251                        @copy( $tmp, $new_file );
     252                        @unlink( $tmp );
     253
     254                        // Store the current timestamp for future checks, for the IF-MODIFIED-SINCE header
     255                        bp_update_option( '_bp_translation_version', time() );
     256
     257                        // Clear the pending translation flag
     258                        bp_delete_option( '_bp_translation_pending' );
     259                }
     260        ?>
     261
     262        <div class="<?php echo esc_attr( $css_class ); ?>">
     263                <p><?php echo $message; ?></p>
     264        </div>
     265
     266        <p><a href="<?php echo self_admin_url( 'update-core.php' ); ?>" target="_parent"><?php _e( 'Return to WordPress Updates', 'buddypress' ); ?></a></p>
     267
     268        <?php
     269                iframe_footer();
     270        }
     271}
     272
     273// Temporary function name for testing
     274function buddypress_translate() {
     275
     276        if ( ! current_user_can( 'update_plugins' ) )
     277                return;
     278
     279        if ( ! is_admin() )
     280                return;
     281
     282        buddypress()->translate = BP_Translate::get_instance();
     283}
     284add_action( 'bp_admin_init', 'buddypress_translate' );
     285
     286/**
     287 * If we're in the WordPress dashboard, and a pending translation is available, bump the update count.
     288 *
     289 * This has to be hooked before admin_init due to wp_get_update_data() being invoked in wp-admin/menu.php
     290 * before the admin_init action is called.
     291 *
     292 * @since BuddyPress (1.8)
     293 */
     294function bp_admin_maybe_bump_update_count() {
     295
     296        if ( ! current_user_can( 'update_plugins' ) )
     297                return;
     298
     299        if ( ! is_admin() )
     300                return;
     301
     302        add_filter( 'wp_get_update_data', array( 'BP_Translate', 'maybe_bump_update_count' ) );
     303}
     304add_action( 'bp_init', 'bp_admin_maybe_bump_update_count' );
  • bp-core/bp-core-admin.php

    diff --git bp-core/bp-core-admin.php bp-core/bp-core-admin.php
    index 3943c7c..427a283 100644
    class BP_Admin { 
    9393         * @access private
    9494         */
    9595        private function includes() {
    96                 require( $this->admin_dir . 'bp-core-actions.php'    );
    97                 require( $this->admin_dir . 'bp-core-settings.php'   );
    98                 require( $this->admin_dir . 'bp-core-functions.php'  );
    99                 require( $this->admin_dir . 'bp-core-components.php' );
    100                 require( $this->admin_dir . 'bp-core-slugs.php'      );
     96                require( $this->admin_dir . 'bp-core-actions.php'      );
     97                require( $this->admin_dir . 'bp-core-settings.php'     );
     98                require( $this->admin_dir . 'bp-core-functions.php'    );
     99                require( $this->admin_dir . 'bp-core-components.php'   );
     100                require( $this->admin_dir . 'bp-core-slugs.php'        );
     101                require( $this->admin_dir . 'bp-core-translations.php' );
    101102        }
    102103
    103104        /**
  • bp-core/bp-core-functions.php

    diff --git bp-core/bp-core-functions.php bp-core/bp-core-functions.php
    index 96b7564..fe5b38b 100644
    add_action( 'wp_footer', 'bp_core_print_generation_time' ); 
    748748 * @package BuddyPress Core
    749749 */
    750750function bp_core_load_buddypress_textdomain() {
    751         $locale        = apply_filters( 'buddypress_locale', get_locale() );
    752         $mofile        = sprintf( 'buddypress-%s.mo', $locale );
    753         $mofile_global = WP_LANG_DIR . '/' . $mofile;
    754         $mofile_local  = BP_PLUGIN_DIR . 'bp-languages/' . $mofile;
    755 
    756         if ( file_exists( $mofile_global ) )
    757                 return load_textdomain( 'buddypress', $mofile_global );
    758         elseif ( file_exists( $mofile_local ) )
    759                 return load_textdomain( 'buddypress', $mofile_local );
    760         else
    761                 return false;
     751        $locale      = apply_filters( 'buddypress_locale', get_locale() );
     752        $mofile      = sprintf( 'buddypress-%s.mo', $locale );
     753        $uploads_dir = wp_upload_dir();
     754
     755        $translation_paths = array(
     756                WP_LANG_DIR . "/{$mofile}",
     757                BP_PLUGIN_DIR . "bp-languages/{$mofile}",
     758                $uploads_dir['basedir'] . "/buddypress/{$mofile}",
     759        );
     760
     761        foreach ( $translation_paths as $path ) {
     762                if ( file_exists( $path ) )
     763                        return load_textdomain( 'buddypress', $path );
     764        }
    762765}
    763 add_action ( 'bp_core_loaded', 'bp_core_load_buddypress_textdomain' );
     766add_action( 'bp_core_loaded', 'bp_core_load_buddypress_textdomain' );
    764767
    765768/**
    766769 * Initializes {@link BP_Embed} after everything is loaded.
  • bp-core/bp-core-options.php

    diff --git bp-core/bp-core-options.php bp-core/bp-core-options.php
    index 550e885..36c98c7 100644
    function bp_get_default_options() { 
    6464                // The ID for the current theme package.
    6565                '_bp_theme_package_id'            => 'legacy',
    6666
     67                // Timestamp of the the current translation from translate.wordpress.org
     68                '_bp_translation_version'         => 0,
     69
     70                // Is there a more recent translation available on translate.wordpress.org?
     71                '_bp_translation_pending'         => false,
     72
    6773                /** Groups ************************************************************/
    6874
    6975                // @todo Move this into the groups component
    function bp_is_akismet_active( $default = true ) { 
    536542function bp_get_theme_package_id( $default = 'legacy' ) {
    537543        return apply_filters( 'bp_get_theme_package_id', bp_get_option( '_bp_theme_package_id', $default ) );
    538544}
     545
     546/**
     547 * Get the timestamp of the the current translation from translate.wordpress.org
     548 *
     549 * @param int $default Optional; default value 0.
     550 * @return int Unix timestamp
     551 * @since BuddyPress (1.8)
     552 */
     553function bp_get_translation_version( $default = 0 ) {
     554        return apply_filters( 'bp_get_translation_version', (int) bp_get_option( '_bp_translation_version', $default ) );
     555}
     556
     557/**
     558 * Does translate.wordpress.org have a more recent translation available on translate.wordpress.org?
     559 *
     560 * @param bool $default Optional; defaults to false.
     561 * @return bool
     562 * @see BP_Translate->check_for_updated_translation()
     563 * @since BuddyPress (1.8)
     564 */
     565function bp_is_translation_update_pending( $default = false ) {
     566        return apply_filters( 'bp_is_translation_update_pending', (bool) bp_get_option( '_bp_translation_pending', $default ) );
     567}
  • bp-loader.php

    diff --git bp-loader.php bp-loader.php
    index c0bd850..d47cfcc 100644
    class BuddyPress { 
    280280
    281281                $this->version    = '1.8-bleeding-6993';
    282282                $this->db_version = 6080;
     283                $this->glotpress_version = '1.7.x';
    283284
    284285                /** Loading ***********************************************************/
    285286