| 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, only at first object creation |
| 12 |
* |
| 13 |
* @since 1.2 |
| 14 |
*/ |
| 15 |
public function __construct() { |
| 16 |
if (!post_type_exists('polylang_mo')) |
| 17 |
register_post_type('polylang_mo', array('rewrite' => false, 'query_var' => false, '_pll' => true)); |
| 18 |
} |
| 19 |
|
| 20 |
/* |
| 21 |
* writes a PLL_MO object into a custom post |
| 22 |
* |
| 23 |
* @since 1.2 |
| 24 |
* |
| 25 |
* @param object $lang the language in which we want to export strings |
| 26 |
*/ |
| 27 |
public function export_to_db($lang) { |
| 28 |
$this->add_entry($this->make_entry('', '')); // empty string translation, just in case |
| 29 |
|
| 30 |
// would be convenient to store the whole object but it would take a huge space in DB |
| 31 |
// so let's keep only the strings in an array |
| 32 |
$strings = array(); |
| 33 |
foreach ($this->entries as $entry) |
| 34 |
$strings[] = array($entry->singular, $this->translate($entry->singular)); |
| 35 |
|
| 36 |
$post = get_post($lang->mo_id, ARRAY_A); // wp_insert_post wants an array |
| 37 |
|
| 38 |
if (empty($post)) |
| 39 |
$GLOBALS['polylang']->model->clean_languages_cache(); // to set mo_id |
| 40 |
|
| 41 |
$post['post_title'] = 'polylang_mo_' . $lang->term_id; |
| 42 |
// json_encode would take less space but is slower to decode |
| 43 |
// wp_insert_post expects slashed data |
| 44 |
$post['post_content'] = addslashes(serialize($strings)); |
| 45 |
$post['post_status'] = 'publish'; |
| 46 |
$post['post_type'] = 'polylang_mo'; |
| 47 |
wp_insert_post($post); |
| 48 |
} |
| 49 |
|
| 50 |
/* |
| 51 |
* reads a PLL_MO object from a custom post |
| 52 |
* |
| 53 |
* @since 1.2 |
| 54 |
* |
| 55 |
* @param object $lang the language in which we want to get strings |
| 56 |
*/ |
| 57 |
public function import_from_db($lang) { |
| 58 |
if (!empty($lang->mo_id)) { |
| 59 |
$post = get_post($lang->mo_id, OBJECT); |
| 60 |
$strings = unserialize($post->post_content); |
| 61 |
if (is_array($strings)) { |
| 62 |
foreach ($strings as $msg) |
| 63 |
$this->add_entry($this->make_entry($msg[0], $msg[1])); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* returns the post id of the post storing the strings translations |
| 70 |
* |
| 71 |
* @since 1.4 |
| 72 |
* |
| 73 |
* @param object $lang |
| 74 |
* @return int |
| 75 |
*/ |
| 76 |
public static function get_id($lang) { |
| 77 |
global $wpdb; |
| 78 |
return $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", 'polylang_mo_' . $lang->term_id, 'polylang_mo')); |
| 79 |
} |
| 80 |
} |
| 81 |
|