| 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 |
add_action( 'pll_add_language', array( $this, 'clean_cache' ) ); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* writes a PLL_MO object into a custom post |
| 26 |
* |
| 27 |
* @since 1.2 |
| 28 |
* |
| 29 |
* @param object $lang the language in which we want to export strings |
| 30 |
*/ |
| 31 |
public function export_to_db( $lang ) { |
| 32 |
$this->add_entry( $this->make_entry( '', '' ) ); // empty string translation, just in case |
| 33 |
|
| 34 |
// would be convenient to store the whole object but it would take a huge space in DB |
| 35 |
// so let's keep only the strings in an array |
| 36 |
$strings = array(); |
| 37 |
foreach ( $this->entries as $entry ) { |
| 38 |
$strings[] = array( $entry->singular, $this->translate( $entry->singular ) ); |
| 39 |
} |
| 40 |
|
| 41 |
// 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 |
| 42 |
$post = empty( $lang->mo_id ) ? array() : get_post( $lang->mo_id, ARRAY_A ); // wp_insert_post wants an array |
| 43 |
|
| 44 |
$post['post_title'] = 'polylang_mo_' . $lang->term_id; |
| 45 |
// json_encode would take less space but is slower to decode |
| 46 |
// wp_insert_post expects slashed data |
| 47 |
$post['post_content'] = addslashes( serialize( $strings ) ); |
| 48 |
$post['post_status'] = 'private'; // to avoid a conflict with WP Super Cache. See https://wordpress.org/support/topic/polylang_mo-and-404s-take-2 |
| 49 |
$post['post_type'] = 'polylang_mo'; |
| 50 |
wp_insert_post( $post ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* reads a PLL_MO object from a custom post |
| 55 |
* |
| 56 |
* @since 1.2 |
| 57 |
* |
| 58 |
* @param object $lang the language in which we want to get strings |
| 59 |
*/ |
| 60 |
public function import_from_db( $lang ) { |
| 61 |
if ( ! empty( $lang->mo_id ) ) { |
| 62 |
$post = get_post( $lang->mo_id, OBJECT ); |
| 63 |
$strings = unserialize( $post->post_content ); |
| 64 |
if ( is_array( $strings ) ) { |
| 65 |
foreach ( $strings as $msg ) { |
| 66 |
$this->add_entry( $this->make_entry( $msg[0], $msg[1] ) ); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* returns the post id of the post storing the strings translations |
| 74 |
* |
| 75 |
* @since 1.4 |
| 76 |
* |
| 77 |
* @param object $lang |
| 78 |
* @return int |
| 79 |
*/ |
| 80 |
public static function get_id( $lang ) { |
| 81 |
global $wpdb; |
| 82 |
|
| 83 |
$ids = wp_cache_get( 'polylang_mo_ids' ); |
| 84 |
|
| 85 |
if ( empty( $ids ) ) { |
| 86 |
$ids = $wpdb->get_results( "SELECT post_title, ID FROM $wpdb->posts WHERE post_type='polylang_mo'", OBJECT_K ); |
| 87 |
wp_cache_add( 'polylang_mo_ids', $ids ); |
| 88 |
} |
| 89 |
|
| 90 |
// The mo id for a language can be transiently empty |
| 91 |
return isset( $ids[ 'polylang_mo_' . $lang->term_id ] ) ? $ids[ 'polylang_mo_' . $lang->term_id ]->ID : null; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Invalidate the cache when adding a new language |
| 96 |
* |
| 97 |
* @since 2.0.5 |
| 98 |
*/ |
| 99 |
public function clean_cache() { |
| 100 |
wp_cache_delete( 'polylang_mo_ids' ); |
| 101 |
} |
| 102 |
} |
| 103 |
|