PluginProbe
Polylang / 2.0.3
Polylang v2.0.3
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
← All changes | include/mo.php +24 -59 3.02.0.3 View file →
@@ -1,19 +1,15 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 - * Manages strings translations storage
4 + * manages strings translations storage
8 5 *
9 6 * @since 1.2
10 - * @since 2.1 Stores the strings in a post meta instead of post content to avoid unserialize issues (See #63)
11 7 */
12 8 class PLL_MO extends MO {
13 9
14 10 /**
15 - * Registers the polylang_mo custom post type, only at first object creation
11 + * registers the polylang_mo custom post type, only at first object creation
16 12 *
17 13 * @since 1.2
18 14 */
19 15 public function __construct() {
@@ -19,59 +15,51 @@
19 15 public function __construct() {
20 16 if ( ! post_type_exists( 'polylang_mo' ) ) {
21 17 $labels = array( 'name' => __( 'Strings translations', 'polylang' ) );
22 18 register_post_type( 'polylang_mo', array( 'labels' => $labels, 'rewrite' => false, 'query_var' => false, '_pll' => true ) );
23 -
24 - add_action( 'pll_add_language', array( $this, 'clean_cache' ) );
25 19 }
26 20 }
27 21
28 22 /**
29 - * Writes a PLL_MO object into a custom post meta.
23 + * writes a PLL_MO object into a custom post
30 24 *
31 25 * @since 1.2
32 26 *
33 - * @param PLL_Language $lang The language in which we want to export strings.
34 - * @return void
27 + * @param object $lang the language in which we want to export strings
35 28 */
36 29 public function export_to_db( $lang ) {
37 - $this->add_entry( $this->make_entry( '', '' ) ); // Empty string translation, just in case
30 + $this->add_entry( $this->make_entry( '', '' ) ); // empty string translation, just in case
38 31
39 - /*
40 - * It would be convenient to store the whole object but it would take a huge space in DB.
41 - * So let's keep only the strings in an array.
42 - * The strings are slashed to avoid breaking slashed strings in update_post_meta.
43 - * @see https://codex.wordpress.org/Function_Reference/update_post_meta#Character_Escaping.
44 - */
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
45 34 $strings = array();
46 35 foreach ( $this->entries as $entry ) {
47 - $strings[] = wp_slash( array( $entry->singular, $this->translate( $entry->singular ) ) );
36 + $strings[] = array( $entry->singular, $this->translate( $entry->singular ) );
48 37 }
49 38
50 - if ( empty( $lang->mo_id ) ) {
51 - $post = array(
52 - 'post_title' => 'polylang_mo_' . $lang->term_id,
53 - 'post_status' => 'private', // To avoid a conflict with WP Super Cache. See https://wordpress.org/support/topic/polylang_mo-and-404s-take-2
54 - 'post_type' => 'polylang_mo',
55 - );
56 - $mo_id = wp_insert_post( $post );
57 - update_post_meta( $mo_id, '_pll_strings_translations', $strings );
58 - } else {
59 - update_post_meta( $lang->mo_id, '_pll_strings_translations', $strings );
60 - }
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 );
61 49 }
62 50
63 51 /**
64 - * Reads a PLL_MO object from a custom post meta.
52 + * reads a PLL_MO object from a custom post
65 53 *
66 54 * @since 1.2
67 55 *
68 - * @param PLL_Language $lang The language in which we want to get strings.
69 - * @return void
56 + * @param object $lang the language in which we want to get strings
70 57 */
71 58 public function import_from_db( $lang ) {
72 59 if ( ! empty( $lang->mo_id ) ) {
73 - $strings = get_post_meta( $lang->mo_id, '_pll_strings_translations', true );
60 + $post = get_post( $lang->mo_id, OBJECT );
61 + $strings = unserialize( $post->post_content );
74 62 if ( is_array( $strings ) ) {
75 63 foreach ( $strings as $msg ) {
76 64 $this->add_entry( $this->make_entry( $msg[0], $msg[1] ) );
77 65 }
@@ -79,13 +67,13 @@
79 67 }
80 68 }
81 69
82 70 /**
83 - * Returns the post id of the post storing the strings translations.
71 + * returns the post id of the post storing the strings translations
84 72 *
85 73 * @since 1.4
86 74 *
87 - * @param PLL_Language $lang The language object.
75 + * @param object $lang
88 76 * @return int
89 77 */
90 78 public static function get_id( $lang ) {
91 79 global $wpdb;
@@ -98,29 +86,6 @@
98 86 }
99 87
100 88 // The mo id for a language can be transiently empty
101 89 return isset( $ids[ 'polylang_mo_' . $lang->term_id ] ) ? $ids[ 'polylang_mo_' . $lang->term_id ]->ID : null;
102 - }
103 -
104 - /**
105 - * Invalidate the cache when adding a new language
106 - *
107 - * @since 2.0.5
108 - *
109 - * @return void
110 - */
111 - public function clean_cache() {
112 - wp_cache_delete( 'polylang_mo_ids' );
113 - }
114 -
115 - /**
116 - * Deletes a string
117 - *
118 - * @since 2.9
119 - *
120 - * @param string $string The source string to remove from the translations.
121 - * @return void
122 - */
123 - public function delete_entry( $string ) {
124 - unset( $this->entries[ $string ] );
125 90 }
126 91 }