| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* manages strings translations storage |
| 5 |
* |
| 6 |
* @since 1.2 |
| 7 |
*/ |
| 8 |
class PLL_MO extends MO { |
| 9 |
|
| 10 |
/* |
| 11 |
* registers the polylang_mo custom post type |
| 12 |
* |
| 13 |
* @since 1.2 |
| 14 |
*/ |
| 15 |
public function __construct() { |
| 16 |
register_post_type('polylang_mo', array('rewrite' => false, 'query_var' => false, '_pll' => true)); |
| 17 |
} |
| 18 |
|
| 19 |
/* |
| 20 |
* writes a PLL_MO object into a custom post |
| 21 |
* |
| 22 |
* @since 1.2 |
| 23 |
* |
| 24 |
* @param object $lang the language in which we want to export strings |
| 25 |
*/ |
| 26 |
public function export_to_db($lang) { |
| 27 |
$this->add_entry($this->make_entry('', '')); // empty string translation, just in case |
| 28 |
|
| 29 |
// would be convenient to store the whole object but it would take a huge space in DB |
| 30 |
// so let's keep only the strings in an array |
| 31 |
$strings = array(); |
| 32 |
foreach ($this->entries as $entry) |
| 33 |
$strings[] = array($entry->singular, $this->translate($entry->singular)); |
| 34 |
|
| 35 |
$lang_id = is_object($lang) ? $lang->term_id : $lang; |
| 36 |
$post = get_page_by_title('polylang_mo_' . $lang_id, ARRAY_A, 'polylang_mo'); // wp_insert_post wants an array |
| 37 |
$post['post_title'] = 'polylang_mo_' . $lang_id; |
| 38 |
// json_encode would take less space but is slower to decode |
| 39 |
// wp_insert_post expects slashed data |
| 40 |
$post['post_content'] = addslashes(serialize($strings)); |
| 41 |
$post['post_status'] = 'publish'; |
| 42 |
$post['post_type'] = 'polylang_mo'; |
| 43 |
wp_insert_post($post); |
| 44 |
} |
| 45 |
|
| 46 |
/* |
| 47 |
* reads a PLL_MO object from a custom post |
| 48 |
* |
| 49 |
* @since 1.2 |
| 50 |
* |
| 51 |
* @param object $lang the language in which we want to get strings |
| 52 |
*/ |
| 53 |
public function import_from_db($lang) { |
| 54 |
$lang_id = is_object($lang) ? $lang->term_id : $lang; |
| 55 |
$post = get_page_by_title('polylang_mo_' . $lang_id, OBJECT, 'polylang_mo'); |
| 56 |
if (!empty($post)) { |
| 57 |
foreach (unserialize($post->post_content) as $msg) |
| 58 |
$this->add_entry($this->make_entry($msg[0], $msg[1])); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|