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