| 1 | <?php |
|---|
| 2 | // markoheijnen has some props for providing glotpress API examples |
|---|
| 3 | |
|---|
| 4 | /** |
|---|
| 5 | * Communicates with http://translate.wordpress.org/ and downloads a translation for the current locale |
|---|
| 6 | * |
|---|
| 7 | * @since BuddyPress (1.8) |
|---|
| 8 | */ |
|---|
| 9 | class BP_Translate { |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * Singleton instance of the BP_Translate class |
|---|
| 13 | * |
|---|
| 14 | * @since BuddyPress (1.8) |
|---|
| 15 | * @var BP_Translate |
|---|
| 16 | */ |
|---|
| 17 | private static $instance; |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * Return the singleton instance of the BP_Translate class |
|---|
| 21 | * |
|---|
| 22 | * @return BP_Translate |
|---|
| 23 | * @since BuddyPress (1.8) |
|---|
| 24 | */ |
|---|
| 25 | static public function get_instance() { |
|---|
| 26 | if ( ! self::$instance ) |
|---|
| 27 | self::$instance = new BP_Translate; |
|---|
| 28 | |
|---|
| 29 | return self::$instance; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * Constructor |
|---|
| 34 | * |
|---|
| 35 | * @since BuddyPress (1.8) |
|---|
| 36 | */ |
|---|
| 37 | public function __construct() { |
|---|
| 38 | $this->register_filters(); |
|---|
| 39 | $this->register_actions(); |
|---|
| 40 | |
|---|
| 41 | $this->register_cron(); // Intentionally after filters + actions |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Hook into filters necessary to automate the translation process |
|---|
| 46 | * |
|---|
| 47 | * @since BuddyPress (1.8) |
|---|
| 48 | */ |
|---|
| 49 | protected function register_filters() { |
|---|
| 50 | add_filter( 'cron_schedules', array( __CLASS__, 'add_weekly_frequency_to_cron' ) ) ; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Hook into actions necessary to automate the translation process |
|---|
| 55 | * |
|---|
| 56 | * @since BuddyPress (1.8) |
|---|
| 57 | */ |
|---|
| 58 | protected function register_actions() { |
|---|
| 59 | add_action( 'bp_translate_update_check', array( __CLASS__, 'maybe_update_translation' ) ); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Register cron task to check for language updates |
|---|
| 64 | * |
|---|
| 65 | * @since BuddyPress (1.8) |
|---|
| 66 | */ |
|---|
| 67 | protected function register_cron() { |
|---|
| 68 | if ( ! wp_next_scheduled( 'bp_translate_update_check' ) ) |
|---|
| 69 | wp_schedule_event( time(), 'bp_weekly', 'bp_translate_update_check' ); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * Adds a "weekly" frequency type to the cron system |
|---|
| 74 | * |
|---|
| 75 | * @param array $schedules |
|---|
| 76 | * @since BuddyPress (1.8) |
|---|
| 77 | */ |
|---|
| 78 | static public function add_weekly_frequency_to_cron( $schedules ) { |
|---|
| 79 | $schedules['bp_weekly'] = array( |
|---|
| 80 | 'display' => __( 'Once Weekly', 'buddypress' ), |
|---|
| 81 | 'interval' => WEEK_IN_SECONDS, |
|---|
| 82 | ); |
|---|
| 83 | |
|---|
| 84 | return $schedules; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * Converts WordPress' locale code to the format used by translate.wordpress.org |
|---|
| 89 | * |
|---|
| 90 | * @return string Locale code, e.g. "fr_FR". |
|---|
| 91 | * @since BuddyPress (1.8) |
|---|
| 92 | * @todo Try to get endpoint added to translate.wordpress.org to convert from get_locale() to glotpress' version. |
|---|
| 93 | */ |
|---|
| 94 | static public function get_glotpress_locale() { |
|---|
| 95 | $locale = apply_filters( 'buddypress_locale', get_locale() ); |
|---|
| 96 | $languages = array( |
|---|
| 97 | 'da_DK' => 'da', |
|---|
| 98 | 'es_ES' => 'es', |
|---|
| 99 | 'fr_FR' => 'fr', |
|---|
| 100 | 'hu_HU' => 'hu', |
|---|
| 101 | 'it_IT' => 'it', |
|---|
| 102 | 'ko_KR' => 'ko', |
|---|
| 103 | 'nb_NO' => 'nb', |
|---|
| 104 | 'nl_NL' => 'nl', |
|---|
| 105 | 'pt_PT' => 'pt', |
|---|
| 106 | 'pt_BR' => 'pt-br', |
|---|
| 107 | 'ru_RU' => 'ru', |
|---|
| 108 | 'sk_SK' => 'sk', |
|---|
| 109 | 'th' => 'th', |
|---|
| 110 | 'zh_TW' => 'zh-tw', |
|---|
| 111 | ); |
|---|
| 112 | |
|---|
| 113 | if ( isset( $languages[$locale] ) ) |
|---|
| 114 | return $languages[$locale]; |
|---|
| 115 | |
|---|
| 116 | return $locale; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * Find out if there's a newer translation available for this site on translate.wordpress.org |
|---|
| 121 | * |
|---|
| 122 | * @since BuddyPress (1.8) |
|---|
| 123 | */ |
|---|
| 124 | static public function maybe_update_translation() { |
|---|
| 125 | $locale = apply_filters( 'buddypress_locale', get_locale() ); |
|---|
| 126 | |
|---|
| 127 | // BuddyPress is written in American English, so bail out early. |
|---|
| 128 | if ( 'en_US' == $locale ) |
|---|
| 129 | return; |
|---|
| 130 | |
|---|
| 131 | // Try to get the .mo |
|---|
| 132 | $url = sprintf( 'https://translate.wordpress.org/projects/buddypress/%1$s/%2$s/default/export-translations?format=mo', '1.6.x', self::get_glotpress_locale() ); |
|---|
| 133 | $tmp_file = download_url( esc_url_raw( $url ) ); |
|---|
| 134 | if ( is_wp_error( $tmp_file ) ) |
|---|
| 135 | return; |
|---|
| 136 | |
|---|
| 137 | // Check the language folder exists |
|---|
| 138 | $dir = WP_LANG_DIR . '/plugins/'; |
|---|
| 139 | if ( ! wp_mkdir_p( $dir ) ) |
|---|
| 140 | return; |
|---|
| 141 | |
|---|
| 142 | // Move the .mo into place |
|---|
| 143 | @copy( $tmp_file, sprintf( "{$dir}buddypress-%1\$s.mo", $locale ) ); |
|---|
| 144 | @unlink( $tmp_file ); |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | // Temporary function name for testing |
|---|
| 149 | function buddypress_translate() { |
|---|
| 150 | |
|---|
| 151 | // Bail out if we're not in wp-admin, or a site admin |
|---|
| 152 | if ( ! is_admin() || ! is_super_admin() || ! apply_filters( 'bp_fetch_translations', true ) ) |
|---|
| 153 | return; |
|---|
| 154 | |
|---|
| 155 | BP_Translate::get_instance(); |
|---|
| 156 | |
|---|
| 157 | /** |
|---|
| 158 | * @todo This line is just for easy testing. This is meant to be triggered by the cron. |
|---|
| 159 | */ |
|---|
| 160 | BP_Translate::maybe_update_translation(); |
|---|
| 161 | } |
|---|
| 162 | add_action( 'bp_admin_init', 'buddypress_translate' ); |
|---|