PluginProbe
Polylang / 1.5
Polylang v1.5
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 +40 -65 2.71.5 View file →
@@ -1,80 +1,73 @@
1 1 <?php
2 2
3 -/**
4 - * Manages strings translations storage
3 +/*
4 + * manages strings translations storage
5 5 *
6 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 7 */
9 8 class PLL_MO extends MO {
10 9
11 - /**
12 - * Registers the polylang_mo custom post type, only at first object creation
10 + /*
11 + * registers the polylang_mo custom post type, only at first object creation
13 12 *
14 13 * @since 1.2
15 14 */
16 15 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 - }
16 + if (!post_type_exists('polylang_mo'))
17 + register_post_type('polylang_mo', array('rewrite' => false, 'query_var' => false, '_pll' => true));
23 18 }
24 19
25 - /**
26 - * Writes a PLL_MO object into a custom post meta
20 + /*
21 + * writes a PLL_MO object into a custom post
27 22 *
28 23 * @since 1.2
29 24 *
30 - * @param object $lang The language in which we want to export strings
25 + * @param object $lang the language in which we want to export strings
31 26 */
32 - public function export_to_db( $lang ) {
33 - $this->add_entry( $this->make_entry( '', '' ) ); // Empty string translation, just in case
27 + public function export_to_db($lang) {
28 + $this->add_entry($this->make_entry('', '')); // empty string translation, just in case
34 29
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
30 + // would be convenient to store the whole object but it would take a huge space in DB
31 + // so let's keep only the strings in an array
37 32 $strings = array();
38 - foreach ( $this->entries as $entry ) {
39 - $strings[] = array( $entry->singular, $this->translate( $entry->singular ) );
40 - }
33 + foreach ($this->entries as $entry)
34 + $strings[] = array($entry->singular, $this->translate($entry->singular));
41 35
42 - $strings = wp_slash( $strings ); // Avoid breaking slashed strings in update_post_meta. See https://codex.wordpress.org/Function_Reference/update_post_meta#Character_Escaping
36 + $post = get_post($lang->mo_id, ARRAY_A); // wp_insert_post wants an array
43 37
44 - if ( empty( $lang->mo_id ) ) {
45 - $post = array(
46 - 'post_title' => 'polylang_mo_' . $lang->term_id,
47 - 'post_status' => 'private', // To avoid a conflict with WP Super Cache. See https://wordpress.org/support/topic/polylang_mo-and-404s-take-2
48 - 'post_type' => 'polylang_mo',
49 - );
50 - $mo_id = wp_insert_post( $post );
51 - update_post_meta( $mo_id, '_pll_strings_translations', $strings );
52 - } else {
53 - update_post_meta( $lang->mo_id, '_pll_strings_translations', $strings );
54 - }
38 + if (empty($post))
39 + $GLOBALS['polylang']->model->clean_languages_cache(); // to set mo_id
40 +
41 + $post['post_title'] = 'polylang_mo_' . $lang->term_id;
42 + // json_encode would take less space but is slower to decode
43 + // wp_insert_post expects slashed data
44 + $post['post_content'] = addslashes(serialize($strings));
45 + $post['post_status'] = 'publish';
46 + $post['post_type'] = 'polylang_mo';
47 + wp_insert_post($post);
55 48 }
56 49
57 - /**
58 - * Reads a PLL_MO object from a custom post meta
50 + /*
51 + * reads a PLL_MO object from a custom post
59 52 *
60 53 * @since 1.2
61 54 *
62 - * @param object $lang The language in which we want to get strings
55 + * @param object $lang the language in which we want to get strings
63 56 */
64 - public function import_from_db( $lang ) {
65 - if ( ! empty( $lang->mo_id ) ) {
66 - $strings = get_post_meta( $lang->mo_id, '_pll_strings_translations', true );
67 - if ( is_array( $strings ) ) {
68 - foreach ( $strings as $msg ) {
69 - $this->add_entry( $this->make_entry( $msg[0], $msg[1] ) );
70 - }
57 + public function import_from_db($lang) {
58 + if (!empty($lang->mo_id)) {
59 + $post = get_post($lang->mo_id, OBJECT);
60 + $strings = unserialize($post->post_content);
61 + if (is_array($strings)) {
62 + foreach ($strings as $msg)
63 + $this->add_entry($this->make_entry($msg[0], $msg[1]));
71 64 }
72 65 }
73 66 }
74 67
75 - /**
76 - * Returns the post id of the post storing the strings translations
68 + /*
69 + * returns the post id of the post storing the strings translations
77 70 *
78 71 * @since 1.4
79 72 *
80 73 * @param object $lang
@@ -79,27 +72,9 @@
79 72 *
80 73 * @param object $lang
81 74 * @return int
82 75 */
83 - public static function get_id( $lang ) {
76 + public static function get_id($lang) {
84 77 global $wpdb;
85 -
86 - $ids = wp_cache_get( 'polylang_mo_ids' );
87 -
88 - if ( empty( $ids ) ) {
89 - $ids = $wpdb->get_results( "SELECT post_title, ID FROM $wpdb->posts WHERE post_type='polylang_mo'", OBJECT_K );
90 - wp_cache_add( 'polylang_mo_ids', $ids );
91 - }
92 -
93 - // The mo id for a language can be transiently empty
94 - return isset( $ids[ 'polylang_mo_' . $lang->term_id ] ) ? $ids[ 'polylang_mo_' . $lang->term_id ]->ID : null;
95 - }
96 -
97 - /**
98 - * Invalidate the cache when adding a new language
99 - *
100 - * @since 2.0.5
101 - */
102 - public function clean_cache() {
103 - wp_cache_delete( 'polylang_mo_ids' );
78 + return $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", 'polylang_mo_' . $lang->term_id, 'polylang_mo'));
104 79 }
105 80 }