| @@ -1,119 +1,56 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 2 | +// Backward compatibility for WP versions older than 3.3, in which the method MO::export does not exist | |
| 3 | +// FIXME to be removed once WP 3.2.1 and older versions is no more supported | |
| 5 | 4 | |
| 6 | -/** | |
| 7 | - * Manages strings translations storage | |
| 8 | - * | |
| 9 | - * @since 1.2 | |
| 10 | - * @since 2.1 Stores the strings in a post meta instead of post content to avoid unserialize issues (See #63) | |
| 11 | - */ | |
| 12 | -class PLL_MO extends MO { | |
| 13 | - | |
| 14 | - /** | |
| 15 | - * Registers the polylang_mo custom post type, only at first object creation | |
| 16 | - * | |
| 17 | - * @since 1.2 | |
| 18 | - */ | |
| 19 | - public function __construct() { | |
| 20 | - if ( ! post_type_exists( 'polylang_mo' ) ) { | |
| 21 | - $labels = array( 'name' => __( 'Strings translations', 'polylang' ) ); | |
| 22 | - register_post_type( 'polylang_mo', array( 'labels' => $labels, 'rewrite' => false, 'query_var' => false, '_pll' => true ) ); | |
| 23 | - | |
| 24 | - add_action( 'pll_add_language', array( $this, 'clean_cache' ) ); | |
| 25 | - } | |
| 5 | + function pll_mo_export($mo) { | |
| 6 | + $tmp_fh = fopen("php://temp", 'r+'); | |
| 7 | + if ( !$tmp_fh ) return false; | |
| 8 | + pll_mo_export_to_file_handle( $mo, $tmp_fh ); | |
| 9 | + rewind( $tmp_fh ); | |
| 10 | + return stream_get_contents( $tmp_fh ); | |
| 26 | 11 | } |
| 12 | + | |
| 13 | + function pll_mo_export_to_file_handle($mo, $fh) { | |
| 14 | + $entries = array_filter($mo->entries, create_function('$e', 'return !empty($e->translations);')); | |
| 15 | + ksort($entries); | |
| 16 | + $magic = 0x950412de; | |
| 17 | + $revision = 0; | |
| 18 | + $total = count($entries) + 1; // all the headers are one entry | |
| 19 | + $originals_lenghts_addr = 28; | |
| 20 | + $translations_lenghts_addr = $originals_lenghts_addr + 8 * $total; | |
| 21 | + $size_of_hash = 0; | |
| 22 | + $hash_addr = $translations_lenghts_addr + 8 * $total; | |
| 23 | + $current_addr = $hash_addr; | |
| 24 | + fwrite($fh, pack('V*', $magic, $revision, $total, $originals_lenghts_addr, | |
| 25 | + $translations_lenghts_addr, $size_of_hash, $hash_addr)); | |
| 26 | + fseek($fh, $originals_lenghts_addr); | |
| 27 | + | |
| 28 | + // headers' msgid is an empty string | |
| 29 | + fwrite($fh, pack('VV', 0, $current_addr)); | |
| 30 | + $current_addr++; | |
| 31 | + $originals_table = chr(0); | |
| 27 | 32 | |
| 28 | - /** | |
| 29 | - * Writes a PLL_MO object into a custom post meta | |
| 30 | - * | |
| 31 | - * @since 1.2 | |
| 32 | - * | |
| 33 | - * @param object $lang The language in which we want to export strings | |
| 34 | - */ | |
| 35 | - public function export_to_db( $lang ) { | |
| 36 | - $this->add_entry( $this->make_entry( '', '' ) ); // Empty string translation, just in case | |
| 37 | - | |
| 38 | - // Would be convenient to store the whole object but it would take a huge space in DB | |
| 39 | - // So let's keep only the strings in an array | |
| 40 | - $strings = array(); | |
| 41 | - foreach ( $this->entries as $entry ) { | |
| 42 | - $strings[] = array( $entry->singular, $this->translate( $entry->singular ) ); | |
| 33 | + foreach($entries as $entry) { | |
| 34 | + $originals_table .= $mo->export_original($entry) . chr(0); | |
| 35 | + $length = strlen($mo->export_original($entry)); | |
| 36 | + fwrite($fh, pack('VV', $length, $current_addr)); | |
| 37 | + $current_addr += $length + 1; // account for the NULL byte after | |
| 43 | 38 | } |
| 44 | - | |
| 45 | - $strings = wp_slash( $strings ); // Avoid breaking slashed strings in update_post_meta. See https://codex.wordpress.org/Function_Reference/update_post_meta#Character_Escaping | |
| 46 | - | |
| 47 | - if ( empty( $lang->mo_id ) ) { | |
| 48 | - $post = array( | |
| 49 | - 'post_title' => 'polylang_mo_' . $lang->term_id, | |
| 50 | - 'post_status' => 'private', // To avoid a conflict with WP Super Cache. See https://wordpress.org/support/topic/polylang_mo-and-404s-take-2 | |
| 51 | - 'post_type' => 'polylang_mo', | |
| 52 | - ); | |
| 53 | - $mo_id = wp_insert_post( $post ); | |
| 54 | - update_post_meta( $mo_id, '_pll_strings_translations', $strings ); | |
| 55 | - } else { | |
| 56 | - update_post_meta( $lang->mo_id, '_pll_strings_translations', $strings ); | |
| 39 | + | |
| 40 | + $exported_headers = $mo->export_headers(); | |
| 41 | + fwrite($fh, pack('VV', strlen($exported_headers), $current_addr)); | |
| 42 | + $current_addr += strlen($exported_headers) + 1; | |
| 43 | + $translations_table = $exported_headers . chr(0); | |
| 44 | + | |
| 45 | + foreach($entries as $entry) { | |
| 46 | + $translations_table .= $mo->export_translations($entry) . chr(0); | |
| 47 | + $length = strlen($mo->export_translations($entry)); | |
| 48 | + fwrite($fh, pack('VV', $length, $current_addr)); | |
| 49 | + $current_addr += $length + 1; | |
| 57 | 50 | } |
| 51 | + | |
| 52 | + fwrite($fh, $originals_table); | |
| 53 | + fwrite($fh, $translations_table); | |
| 54 | + return true; | |
| 58 | 55 | } |
| 59 | - | |
| 60 | - /** | |
| 61 | - * Reads a PLL_MO object from a custom post meta | |
| 62 | - * | |
| 63 | - * @since 1.2 | |
| 64 | - * | |
| 65 | - * @param object $lang The language in which we want to get strings | |
| 66 | - */ | |
| 67 | - public function import_from_db( $lang ) { | |
| 68 | - if ( ! empty( $lang->mo_id ) ) { | |
| 69 | - $strings = get_post_meta( $lang->mo_id, '_pll_strings_translations', true ); | |
| 70 | - if ( is_array( $strings ) ) { | |
| 71 | - foreach ( $strings as $msg ) { | |
| 72 | - $this->add_entry( $this->make_entry( $msg[0], $msg[1] ) ); | |
| 73 | - } | |
| 74 | - } | |
| 75 | - } | |
| 76 | - } | |
| 77 | - | |
| 78 | - /** | |
| 79 | - * Returns the post id of the post storing the strings translations | |
| 80 | - * | |
| 81 | - * @since 1.4 | |
| 82 | - * | |
| 83 | - * @param object $lang | |
| 84 | - * @return int | |
| 85 | - */ | |
| 86 | - public static function get_id( $lang ) { | |
| 87 | - global $wpdb; | |
| 88 | - | |
| 89 | - $ids = wp_cache_get( 'polylang_mo_ids' ); | |
| 90 | - | |
| 91 | - if ( empty( $ids ) ) { | |
| 92 | - $ids = $wpdb->get_results( "SELECT post_title, ID FROM $wpdb->posts WHERE post_type='polylang_mo'", OBJECT_K ); | |
| 93 | - wp_cache_add( 'polylang_mo_ids', $ids ); | |
| 94 | - } | |
| 95 | - | |
| 96 | - // The mo id for a language can be transiently empty | |
| 97 | - return isset( $ids[ 'polylang_mo_' . $lang->term_id ] ) ? $ids[ 'polylang_mo_' . $lang->term_id ]->ID : null; | |
| 98 | - } | |
| 99 | - | |
| 100 | - /** | |
| 101 | - * Invalidate the cache when adding a new language | |
| 102 | - * | |
| 103 | - * @since 2.0.5 | |
| 104 | - */ | |
| 105 | - public function clean_cache() { | |
| 106 | - wp_cache_delete( 'polylang_mo_ids' ); | |
| 107 | - } | |
| 108 | - | |
| 109 | - /** | |
| 110 | - * Deletes a string | |
| 111 | - * | |
| 112 | - * @since 2.9 | |
| 113 | - * | |
| 114 | - * @param string $string The source string to remove from the translations. | |
| 115 | - */ | |
| 116 | - public function delete_entry( $string ) { | |
| 117 | - unset( $this->entries[ $string ] ); | |
| 118 | - } | |
| 119 | -} | |
| 56 | +?> | |