| @@ -1,19 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | - * Manages strings translations storage | |
| 4 | + * manages strings translations storage | |
| 8 | 5 | * |
| 9 | 6 | * @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 | 7 | */ |
| 12 | 8 | class PLL_MO extends MO { |
| 13 | 9 | |
| 14 | 10 | /** |
| 15 | - * Registers the polylang_mo custom post type, only at first object creation | |
| 11 | + * registers the polylang_mo custom post type, only at first object creation | |
| 16 | 12 | * |
| 17 | 13 | * @since 1.2 |
| 18 | 14 | */ |
| 19 | 15 | public function __construct() { |
| @@ -19,55 +15,51 @@ | ||
| 19 | 15 | public function __construct() { |
| 20 | 16 | if ( ! post_type_exists( 'polylang_mo' ) ) { |
| 21 | 17 | $labels = array( 'name' => __( 'Strings translations', 'polylang' ) ); |
| 22 | 18 | 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 | 19 | } |
| 26 | 20 | } |
| 27 | 21 | |
| 28 | 22 | /** |
| 29 | - * Writes a PLL_MO object into a custom post meta | |
| 23 | + * writes a PLL_MO object into a custom post | |
| 30 | 24 | * |
| 31 | 25 | * @since 1.2 |
| 32 | 26 | * |
| 33 | - * @param object $lang The language in which we want to export strings | |
| 27 | + * @param object $lang the language in which we want to export strings | |
| 34 | 28 | */ |
| 35 | 29 | public function export_to_db( $lang ) { |
| 36 | - $this->add_entry( $this->make_entry( '', '' ) ); // Empty string translation, just in case | |
| 30 | + $this->add_entry( $this->make_entry( '', '' ) ); // empty string translation, just in case | |
| 37 | 31 | |
| 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 | |
| 32 | + // would be convenient to store the whole object but it would take a huge space in DB | |
| 33 | + // so let's keep only the strings in an array | |
| 40 | 34 | $strings = array(); |
| 41 | 35 | foreach ( $this->entries as $entry ) { |
| 42 | 36 | $strings[] = array( $entry->singular, $this->translate( $entry->singular ) ); |
| 43 | 37 | } |
| 44 | 38 | |
| 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 | |
| 39 | + // we need to make sure that $post is empty when $lang->mo_id is empty: see https://wordpress.org/support/topic/problem-when-adding-a-language | |
| 40 | + $post = empty( $lang->mo_id ) ? array() : get_post( $lang->mo_id, ARRAY_A ); // wp_insert_post wants an array | |
| 46 | 41 | |
| 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 ); | |
| 57 | - } | |
| 42 | + $post['post_title'] = 'polylang_mo_' . $lang->term_id; | |
| 43 | + // json_encode would take less space but is slower to decode | |
| 44 | + // wp_insert_post expects slashed data | |
| 45 | + $post['post_content'] = addslashes( serialize( $strings ) ); | |
| 46 | + $post['post_status'] = 'private'; // to avoid a conflict with WP Super Cache. See https://wordpress.org/support/topic/polylang_mo-and-404s-take-2 | |
| 47 | + $post['post_type'] = 'polylang_mo'; | |
| 48 | + wp_insert_post( $post ); | |
| 58 | 49 | } |
| 59 | 50 | |
| 60 | 51 | /** |
| 61 | - * Reads a PLL_MO object from a custom post meta | |
| 52 | + * reads a PLL_MO object from a custom post | |
| 62 | 53 | * |
| 63 | 54 | * @since 1.2 |
| 64 | 55 | * |
| 65 | - * @param object $lang The language in which we want to get strings | |
| 56 | + * @param object $lang the language in which we want to get strings | |
| 66 | 57 | */ |
| 67 | 58 | public function import_from_db( $lang ) { |
| 68 | 59 | if ( ! empty( $lang->mo_id ) ) { |
| 69 | - $strings = get_post_meta( $lang->mo_id, '_pll_strings_translations', true ); | |
| 60 | + $post = get_post( $lang->mo_id, OBJECT ); | |
| 61 | + $strings = unserialize( $post->post_content ); | |
| 70 | 62 | if ( is_array( $strings ) ) { |
| 71 | 63 | foreach ( $strings as $msg ) { |
| 72 | 64 | $this->add_entry( $this->make_entry( $msg[0], $msg[1] ) ); |
| 73 | 65 | } |
| @@ -75,9 +67,9 @@ | ||
| 75 | 67 | } |
| 76 | 68 | } |
| 77 | 69 | |
| 78 | 70 | /** |
| 79 | - * Returns the post id of the post storing the strings translations | |
| 71 | + * returns the post id of the post storing the strings translations | |
| 80 | 72 | * |
| 81 | 73 | * @since 1.4 |
| 82 | 74 | * |
| 83 | 75 | * @param object $lang |
| @@ -94,15 +86,6 @@ | ||
| 94 | 86 | } |
| 95 | 87 | |
| 96 | 88 | // The mo id for a language can be transiently empty |
| 97 | 89 | 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 | 90 | } |
| 108 | 91 | } |