| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Manages strings translations storage. |
| 8 |
* A static cache is used internally to enhance performances, |
| 9 |
* for it to work as expected, use `import_from_db` for the cache to be used |
| 10 |
* and `export_to_db` for the cache to be cleared. |
| 11 |
* |
| 12 |
* @since 1.2 |
| 13 |
* @since 2.1 Stores the strings in a post meta instead of post content to avoid unserialize issues (See #63) |
| 14 |
* @since 3.4 Stores the strings into language taxonomy term meta instead of a post meta. |
| 15 |
*/ |
| 16 |
class PLL_MO extends MO { |
| 17 |
/** |
| 18 |
* Static cache for the translations. |
| 19 |
* |
| 20 |
* @var PLL_Cache<array> |
| 21 |
*/ |
| 22 |
private static $cache; |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor, initializes the cache if not already done. |
| 26 |
* |
| 27 |
* @since 3.7 |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
if ( empty( self::$cache ) ) { |
| 31 |
self::$cache = new PLL_Cache(); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Writes the strings into a term meta. |
| 37 |
* |
| 38 |
* @since 1.2 |
| 39 |
* |
| 40 |
* @param PLL_Language $lang The language in which we want to export strings. |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function export_to_db( $lang ) { |
| 44 |
/* |
| 45 |
* It would be convenient to store the whole object, but it would take a huge space in DB. |
| 46 |
* So let's keep only the strings in an array. |
| 47 |
* The strings are slashed to avoid breaking slashed strings in update_term_meta. |
| 48 |
* @see https://codex.wordpress.org/Function_Reference/update_post_meta#Character_Escaping. |
| 49 |
*/ |
| 50 |
$strings = array(); |
| 51 |
foreach ( $this->entries as $entry ) { |
| 52 |
if ( '' !== $entry->singular ) { |
| 53 |
$strings[] = wp_slash( array( $entry->singular, $this->translate( $entry->singular ) ) ); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
update_term_meta( $lang->term_id, '_pll_strings_translations', $strings ); |
| 58 |
|
| 59 |
self::$cache->clean( $lang->slug ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Reads a PLL_MO object from the term meta. |
| 64 |
* |
| 65 |
* @since 1.2 |
| 66 |
* @since 3.4 Reads a PLL_MO from the term meta. |
| 67 |
* |
| 68 |
* @param PLL_Language $lang The language in which we want to get strings. |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public function import_from_db( $lang ) { |
| 72 |
$this->set_header( 'Language', $lang->slug ); |
| 73 |
|
| 74 |
if ( ! empty( self::$cache->get( $lang->slug ) ) ) { |
| 75 |
$this->entries = self::$cache->get( $lang->slug ); |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
$strings = get_term_meta( $lang->term_id, '_pll_strings_translations', true ); |
| 80 |
if ( empty( $strings ) || ! is_array( $strings ) ) { |
| 81 |
self::$cache->set( $lang->slug, array() ); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
foreach ( $strings as $msg ) { |
| 86 |
if ( '' === $msg[0] || '' === $msg[1] ) { |
| 87 |
continue; |
| 88 |
} |
| 89 |
|
| 90 |
$entry = $this->make_entry( $msg[0], $msg[1] ); |
| 91 |
|
| 92 |
if ( '' !== $entry->singular ) { |
| 93 |
$this->add_entry( $entry ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
self::$cache->set( $lang->slug, $this->entries ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Deletes a string |
| 102 |
* |
| 103 |
* @since 2.9 |
| 104 |
* |
| 105 |
* @param string $string The source string to remove from the translations. |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function delete_entry( $string ) { |
| 109 |
unset( $this->entries[ $string ] ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Translates a string or returns false if the translation is not found. |
| 114 |
* Contrary to `self::translate()`, this method doesn't fallback to the source string but returns empty string instead. |
| 115 |
* |
| 116 |
* @since 3.7 |
| 117 |
* |
| 118 |
* @param string $source The source string to translate. |
| 119 |
* @return string The translated string or empty string if not found. |
| 120 |
*/ |
| 121 |
public function translate_if_any( string $source ) { |
| 122 |
$entry = new Translation_Entry( array( 'singular' => $source ) ); |
| 123 |
$entry = $this->translate_entry( $entry ); |
| 124 |
|
| 125 |
if ( ! $entry instanceof Translation_Entry || empty( $entry->translations ) ) { |
| 126 |
return ''; |
| 127 |
} |
| 128 |
|
| 129 |
return $entry->translations[0]; |
| 130 |
} |
| 131 |
} |
| 132 |
|