PluginProbe
Polylang / 2.0.12
Polylang v2.0.12
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 -48 3.02.0.12 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() {
@@ -25,53 +21,47 @@
25 21 }
26 22 }
27 23
28 24 /**
29 - * Writes a PLL_MO object into a custom post meta.
25 + * writes a PLL_MO object into a custom post
30 26 *
31 27 * @since 1.2
32 28 *
33 - * @param PLL_Language $lang The language in which we want to export strings.
34 - * @return void
29 + * @param object $lang the language in which we want to export strings
35 30 */
36 31 public function export_to_db( $lang ) {
37 - $this->add_entry( $this->make_entry( '', '' ) ); // Empty string translation, just in case
32 + $this->add_entry( $this->make_entry( '', '' ) ); // empty string translation, just in case
38 33
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 - */
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
45 36 $strings = array();
46 37 foreach ( $this->entries as $entry ) {
47 - $strings[] = wp_slash( array( $entry->singular, $this->translate( $entry->singular ) ) );
38 + $strings[] = array( $entry->singular, $this->translate( $entry->singular ) );
48 39 }
49 40
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 - }
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 );
61 51 }
62 52
63 53 /**
64 - * Reads a PLL_MO object from a custom post meta.
54 + * reads a PLL_MO object from a custom post
65 55 *
66 56 * @since 1.2
67 57 *
68 - * @param PLL_Language $lang The language in which we want to get strings.
69 - * @return void
58 + * @param object $lang the language in which we want to get strings
70 59 */
71 60 public function import_from_db( $lang ) {
72 61 if ( ! empty( $lang->mo_id ) ) {
73 - $strings = get_post_meta( $lang->mo_id, '_pll_strings_translations', true );
62 + $post = get_post( $lang->mo_id, OBJECT );
63 + $strings = unserialize( $post->post_content );
74 64 if ( is_array( $strings ) ) {
75 65 foreach ( $strings as $msg ) {
76 66 $this->add_entry( $this->make_entry( $msg[0], $msg[1] ) );
77 67 }
@@ -79,13 +69,13 @@
79 69 }
80 70 }
81 71
82 72 /**
83 - * Returns the post id of the post storing the strings translations.
73 + * returns the post id of the post storing the strings translations
84 74 *
85 75 * @since 1.4
86 76 *
87 - * @param PLL_Language $lang The language object.
77 + * @param object $lang
88 78 * @return int
89 79 */
90 80 public static function get_id( $lang ) {
91 81 global $wpdb;
@@ -104,23 +94,9 @@
104 94 /**
105 95 * Invalidate the cache when adding a new language
106 96 *
107 97 * @since 2.0.5
108 - *
109 - * @return void
110 98 */
111 99 public function clean_cache() {
112 100 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 101 }
126 102 }