PluginProbe
Polylang / 2.1
Polylang v2.1
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / mo.php

mo.php in Polylang 2.1, at include/mo.php

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