| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 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 |
* @since 3.4 Stores the strings into language taxonomy term meta instead of a post meta. |
| 12 |
*/ |
| 13 |
class PLL_MO extends MO { |
| 14 |
|
| 15 |
/** |
| 16 |
* Writes the strings into a term meta. |
| 17 |
* |
| 18 |
* @since 1.2 |
| 19 |
* |
| 20 |
* @param PLL_Language $lang The language in which we want to export strings. |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
public function export_to_db( $lang ) { |
| 24 |
/* |
| 25 |
* It would be convenient to store the whole object, but it would take a huge space in DB. |
| 26 |
* So let's keep only the strings in an array. |
| 27 |
* The strings are slashed to avoid breaking slashed strings in update_term_meta. |
| 28 |
* @see https://codex.wordpress.org/Function_Reference/update_post_meta#Character_Escaping. |
| 29 |
*/ |
| 30 |
$strings = array(); |
| 31 |
foreach ( $this->entries as $entry ) { |
| 32 |
if ( '' !== $entry->singular ) { |
| 33 |
$strings[] = wp_slash( array( $entry->singular, $this->translate( $entry->singular ) ) ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
update_term_meta( $lang->term_id, '_pll_strings_translations', $strings ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Reads a PLL_MO object from the term meta. |
| 42 |
* |
| 43 |
* @since 1.2 |
| 44 |
* @since 3.4 Reads a PLL_MO from the term meta. |
| 45 |
* |
| 46 |
* @param PLL_Language $lang The language in which we want to get strings. |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
public function import_from_db( $lang ) { |
| 50 |
$this->set_header( 'Language', $lang->slug ); |
| 51 |
|
| 52 |
$strings = get_term_meta( $lang->term_id, '_pll_strings_translations', true ); |
| 53 |
if ( empty( $strings ) || ! is_array( $strings ) ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
foreach ( $strings as $msg ) { |
| 58 |
$entry = $this->make_entry( $msg[0], $msg[1] ); |
| 59 |
|
| 60 |
if ( '' !== $entry->singular ) { |
| 61 |
$this->add_entry( $entry ); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Deletes a string |
| 68 |
* |
| 69 |
* @since 2.9 |
| 70 |
* |
| 71 |
* @param string $string The source string to remove from the translations. |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function delete_entry( $string ) { |
| 75 |
unset( $this->entries[ $string ] ); |
| 76 |
} |
| 77 |
} |
| 78 |
|