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