| 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 |
$labels = array( 'name' => __( 'Strings translations', 'polylang' ) ); |
| 18 |
register_post_type( 'polylang_mo', array( 'labels' => $labels, 'rewrite' => false, 'query_var' => false, '_pll' => true ) ); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
/* |
| 23 |
* writes a PLL_MO object into a custom post |
| 24 |
* |
| 25 |
* @since 1.2 |
| 26 |
* |
| 27 |
* @param object $lang the language in which we want to export strings |
| 28 |
*/ |
| 29 |
public function export_to_db( $lang ) { |
| 30 |
$this->add_entry( $this->make_entry( '', '' ) ); // empty string translation, just in case |
| 31 |
|
| 32 |
// would be convenient to store the whole object but it would take a huge space in DB |
| 33 |
// so let's keep only the strings in an array |
| 34 |
$strings = array(); |
| 35 |
foreach ( $this->entries as $entry ) { |
| 36 |
$strings[] = array( $entry->singular, $this->translate( $entry->singular ) ); |
| 37 |
} |
| 38 |
|
| 39 |
// 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 |
| 40 |
$post = empty( $lang->mo_id ) ? array() : get_post( $lang->mo_id, ARRAY_A ); // wp_insert_post wants an array |
| 41 |
|
| 42 |
$post['post_title'] = 'polylang_mo_' . $lang->term_id; |
| 43 |
// json_encode would take less space but is slower to decode |
| 44 |
// wp_insert_post expects slashed data |
| 45 |
$post['post_content'] = addslashes( serialize( $strings ) ); |
| 46 |
$post['post_status'] = 'private'; // to avoid a conflict with WP Super Cache. See https://wordpress.org/support/topic/polylang_mo-and-404s-take-2 |
| 47 |
$post['post_type'] = 'polylang_mo'; |
| 48 |
wp_insert_post( $post ); |
| 49 |
} |
| 50 |
|
| 51 |
/* |
| 52 |
* reads a PLL_MO object from a custom post |
| 53 |
* |
| 54 |
* @since 1.2 |
| 55 |
* |
| 56 |
* @param object $lang the language in which we want to get strings |
| 57 |
*/ |
| 58 |
public function import_from_db( $lang ) { |
| 59 |
if ( ! empty( $lang->mo_id ) ) { |
| 60 |
$post = get_post( $lang->mo_id, OBJECT ); |
| 61 |
$strings = unserialize( $post->post_content ); |
| 62 |
if ( is_array( $strings ) ) { |
| 63 |
foreach ( $strings as $msg ) { |
| 64 |
$this->add_entry( $this->make_entry( $msg[0], $msg[1] ) ); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/* |
| 71 |
* returns the post id of the post storing the strings translations |
| 72 |
* |
| 73 |
* @since 1.4 |
| 74 |
* |
| 75 |
* @param object $lang |
| 76 |
* @return int |
| 77 |
*/ |
| 78 |
public static function get_id( $lang ) { |
| 79 |
global $wpdb; |
| 80 |
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' ) ); |
| 81 |
} |
| 82 |
} |
| 83 |
|