PLL_MO

Manages strings translations storage.

Defined (1)

The class is defined in the following location(s).

/include/mo.php  
  1. class PLL_MO extends MO { 
  2.  
  3. /** 
  4. * Registers the polylang_mo custom post type, only at first object creation 
  5. * @since 1.2 
  6. */ 
  7. public function __construct() { 
  8. if ( ! post_type_exists( 'polylang_mo' ) ) { 
  9. $labels = array( 'name' => __( 'Strings translations', 'polylang' ) ); 
  10. register_post_type( 'polylang_mo', array( 'labels' => $labels, 'rewrite' => false, 'query_var' => false, '_pll' => true ) ); 
  11.  
  12. add_action( 'pll_add_language', array( $this, 'clean_cache' ) ); 
  13.  
  14. /** 
  15. * Writes a PLL_MO object into a custom post meta 
  16. * @since 1.2 
  17. * @param object $lang The language in which we want to export strings 
  18. */ 
  19. public function export_to_db( $lang ) { 
  20. $this->add_entry( $this->make_entry( '', '' ) ); // Empty string translation, just in case 
  21.  
  22. // Would be convenient to store the whole object but it would take a huge space in DB 
  23. // So let's keep only the strings in an array 
  24. $strings = array(); 
  25. foreach ( $this->entries as $entry ) { 
  26. $strings[] = array( $entry->singular, $this->translate( $entry->singular ) ); 
  27.  
  28. $strings = wp_slash( $strings ); // Avoid breaking slashed strings in update_post_meta. See https://codex.wordpress.org/Function_Reference/update_post_meta#Character_Escaping 
  29.  
  30. if ( empty( $lang->mo_id ) ) { 
  31. $post = array( 
  32. 'post_title' => 'polylang_mo_' . $lang->term_id,  
  33. 'post_status' => 'private', // To avoid a conflict with WP Super Cache. See https://wordpress.org/support/topic/polylang_mo-and-404s-take-2 
  34. 'post_type' => 'polylang_mo',  
  35. ); 
  36. $mo_id = wp_insert_post( $post ); 
  37. update_post_meta( $mo_id, '_pll_strings_translations', $strings ); 
  38. } else { 
  39. update_post_meta( $lang->mo_id, '_pll_strings_translations', $strings ); 
  40.  
  41. /** 
  42. * Reads a PLL_MO object from a custom post meta 
  43. * @since 1.2 
  44. * @param object $lang The language in which we want to get strings 
  45. */ 
  46. public function import_from_db( $lang ) { 
  47. if ( ! empty( $lang->mo_id ) ) { 
  48. $strings = get_post_meta( $lang->mo_id, '_pll_strings_translations', true ); 
  49. if ( is_array( $strings ) ) { 
  50. foreach ( $strings as $msg ) { 
  51. $this->add_entry( $this->make_entry( $msg[0], $msg[1] ) ); 
  52.  
  53. /** 
  54. * Returns the post id of the post storing the strings translations 
  55. * @since 1.4 
  56. * @param object $lang 
  57. * @return int 
  58. */ 
  59. public static function get_id( $lang ) { 
  60. global $wpdb; 
  61.  
  62. $ids = wp_cache_get( 'polylang_mo_ids' ); 
  63.  
  64. if ( empty( $ids ) ) { 
  65. $ids = $wpdb->get_results( "SELECT post_title, ID FROM $wpdb->posts WHERE post_type='polylang_mo'", OBJECT_K ); 
  66. wp_cache_add( 'polylang_mo_ids', $ids ); 
  67.  
  68. // The mo id for a language can be transiently empty 
  69. return isset( $ids[ 'polylang_mo_' . $lang->term_id ] ) ? $ids[ 'polylang_mo_' . $lang->term_id ]->ID : null; 
  70.  
  71. /** 
  72. * Invalidate the cache when adding a new language 
  73. * @since 2.0.5 
  74. */ 
  75. public function clean_cache() { 
  76. wp_cache_delete( 'polylang_mo_ids' );