| @@ -1,16 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * Manages strings translations storage | |
| 4 | + * manages strings translations storage | |
| 5 | 5 | * |
| 6 | 6 | * @since 1.2 |
| 7 | - * @since 2.1 Stores the strings in a post meta instead of post content to avoid unserialize issues (See #63) | |
| 8 | 7 | */ |
| 9 | 8 | class PLL_MO extends MO { |
| 10 | 9 | |
| 11 | 10 | /** |
| 12 | - * 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 | |
| 13 | 12 | * |
| 14 | 13 | * @since 1.2 |
| 15 | 14 | */ |
| 16 | 15 | public function __construct() { |
| @@ -22,49 +21,47 @@ | ||
| 22 | 21 | } |
| 23 | 22 | } |
| 24 | 23 | |
| 25 | 24 | /** |
| 26 | - * Writes a PLL_MO object into a custom post meta | |
| 25 | + * writes a PLL_MO object into a custom post | |
| 27 | 26 | * |
| 28 | 27 | * @since 1.2 |
| 29 | 28 | * |
| 30 | - * @param object $lang The language in which we want to export strings | |
| 29 | + * @param object $lang the language in which we want to export strings | |
| 31 | 30 | */ |
| 32 | 31 | public function export_to_db( $lang ) { |
| 33 | - $this->add_entry( $this->make_entry( '', '' ) ); // Empty string translation, just in case | |
| 32 | + $this->add_entry( $this->make_entry( '', '' ) ); // empty string translation, just in case | |
| 34 | 33 | |
| 35 | - // Would be convenient to store the whole object but it would take a huge space in DB | |
| 36 | - // So let's keep only the strings in an array | |
| 34 | + // would be convenient to store the whole object but it would take a huge space in DB | |
| 35 | + // so let's keep only the strings in an array | |
| 37 | 36 | $strings = array(); |
| 38 | 37 | foreach ( $this->entries as $entry ) { |
| 39 | 38 | $strings[] = array( $entry->singular, $this->translate( $entry->singular ) ); |
| 40 | 39 | } |
| 41 | 40 | |
| 42 | - $strings = wp_slash( $strings ); // Avoid breaking slashed strings in update_post_meta. See https://codex.wordpress.org/Function_Reference/update_post_meta#Character_Escaping | |
| 41 | + // 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 | |
| 42 | + $post = empty( $lang->mo_id ) ? array() : get_post( $lang->mo_id, ARRAY_A ); // wp_insert_post wants an array | |
| 43 | 43 | |
| 44 | - if ( empty( $lang->mo_id ) ) { | |
| 45 | - $post = array( | |
| 46 | - 'post_title' => 'polylang_mo_' . $lang->term_id, | |
| 47 | - 'post_status' => 'private', // To avoid a conflict with WP Super Cache. See https://wordpress.org/support/topic/polylang_mo-and-404s-take-2 | |
| 48 | - 'post_type' => 'polylang_mo', | |
| 49 | - ); | |
| 50 | - $mo_id = wp_insert_post( $post ); | |
| 51 | - update_post_meta( $mo_id, '_pll_strings_translations', $strings ); | |
| 52 | - } else { | |
| 53 | - update_post_meta( $lang->mo_id, '_pll_strings_translations', $strings ); | |
| 54 | - } | |
| 44 | + $post['post_title'] = 'polylang_mo_' . $lang->term_id; | |
| 45 | + // json_encode would take less space but is slower to decode | |
| 46 | + // wp_insert_post expects slashed data | |
| 47 | + $post['post_content'] = addslashes( serialize( $strings ) ); | |
| 48 | + $post['post_status'] = 'private'; // to avoid a conflict with WP Super Cache. See https://wordpress.org/support/topic/polylang_mo-and-404s-take-2 | |
| 49 | + $post['post_type'] = 'polylang_mo'; | |
| 50 | + wp_insert_post( $post ); | |
| 55 | 51 | } |
| 56 | 52 | |
| 57 | 53 | /** |
| 58 | - * Reads a PLL_MO object from a custom post meta | |
| 54 | + * reads a PLL_MO object from a custom post | |
| 59 | 55 | * |
| 60 | 56 | * @since 1.2 |
| 61 | 57 | * |
| 62 | - * @param object $lang The language in which we want to get strings | |
| 58 | + * @param object $lang the language in which we want to get strings | |
| 63 | 59 | */ |
| 64 | 60 | public function import_from_db( $lang ) { |
| 65 | 61 | if ( ! empty( $lang->mo_id ) ) { |
| 66 | - $strings = get_post_meta( $lang->mo_id, '_pll_strings_translations', true ); | |
| 62 | + $post = get_post( $lang->mo_id, OBJECT ); | |
| 63 | + $strings = unserialize( $post->post_content ); | |
| 67 | 64 | if ( is_array( $strings ) ) { |
| 68 | 65 | foreach ( $strings as $msg ) { |
| 69 | 66 | $this->add_entry( $this->make_entry( $msg[0], $msg[1] ) ); |
| 70 | 67 | } |
| @@ -72,9 +69,9 @@ | ||
| 72 | 69 | } |
| 73 | 70 | } |
| 74 | 71 | |
| 75 | 72 | /** |
| 76 | - * Returns the post id of the post storing the strings translations | |
| 73 | + * returns the post id of the post storing the strings translations | |
| 77 | 74 | * |
| 78 | 75 | * @since 1.4 |
| 79 | 76 | * |
| 80 | 77 | * @param object $lang |