PluginProbe
Polylang / 3.7.1
Polylang v3.7.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
← All changes | include/mo.php +81 -55 2.7.23.7.1 View file →
@@ -1,105 +1,131 @@
1 1 <?php
2 +/**
3 + * @package Polylang
4 + */
2 5
3 6 /**
4 - * Manages strings translations storage
7 + * Manages strings translations storage.
8 + * A static cache is used internally to enhance performances,
9 + * for it to work as expected, use `import_from_db` for the cache to be used
10 + * and `export_to_db` for the cache to be cleared.
5 11 *
6 12 * @since 1.2
7 13 * @since 2.1 Stores the strings in a post meta instead of post content to avoid unserialize issues (See #63)
14 + * @since 3.4 Stores the strings into language taxonomy term meta instead of a post meta.
8 15 */
9 16 class PLL_MO extends MO {
17 + /**
18 + * Static cache for the translations.
19 + *
20 + * @var PLL_Cache<array>
21 + */
22 + private static $cache;
10 23
11 24 /**
12 - * Registers the polylang_mo custom post type, only at first object creation
25 + * Constructor, initializes the cache if not already done.
13 26 *
14 - * @since 1.2
27 + * @since 3.7
15 28 */
16 29 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' ) );
30 + if ( empty( self::$cache ) ) {
31 + self::$cache = new PLL_Cache();
22 32 }
23 33 }
24 34
25 35 /**
26 - * Writes a PLL_MO object into a custom post meta
36 + * Writes the strings into a term meta.
27 37 *
28 38 * @since 1.2
29 39 *
30 - * @param object $lang The language in which we want to export strings
40 + * @param PLL_Language $lang The language in which we want to export strings.
41 + * @return void
31 42 */
32 43 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
44 + /*
45 + * It would be convenient to store the whole object, but it would take a huge space in DB.
46 + * So let's keep only the strings in an array.
47 + * The strings are slashed to avoid breaking slashed strings in update_term_meta.
48 + * @see https://codex.wordpress.org/Function_Reference/update_post_meta#Character_Escaping.
49 + */
37 50 $strings = array();
38 51 foreach ( $this->entries as $entry ) {
39 - $strings[] = array( $entry->singular, $this->translate( $entry->singular ) );
52 + if ( '' !== $entry->singular ) {
53 + $strings[] = wp_slash( array( $entry->singular, $this->translate( $entry->singular ) ) );
54 + }
40 55 }
41 56
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
57 + update_term_meta( $lang->term_id, '_pll_strings_translations', $strings );
43 58
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 - }
59 + self::$cache->clean( $lang->slug );
55 60 }
56 61
57 62 /**
58 - * Reads a PLL_MO object from a custom post meta
63 + * Reads a PLL_MO object from the term meta.
59 64 *
60 65 * @since 1.2
66 + * @since 3.4 Reads a PLL_MO from the term meta.
61 67 *
62 - * @param object $lang The language in which we want to get strings
68 + * @param PLL_Language $lang The language in which we want to get strings.
69 + * @return void
63 70 */
64 71 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 - }
72 + $this->set_header( 'Language', $lang->slug );
73 +
74 + if ( ! empty( self::$cache->get( $lang->slug ) ) ) {
75 + $this->entries = self::$cache->get( $lang->slug );
76 + return;
77 + }
78 +
79 + $strings = get_term_meta( $lang->term_id, '_pll_strings_translations', true );
80 + if ( empty( $strings ) || ! is_array( $strings ) ) {
81 + self::$cache->set( $lang->slug, array() );
82 + return;
83 + }
84 +
85 + foreach ( $strings as $msg ) {
86 + if ( '' === $msg[0] || '' === $msg[1] ) {
87 + continue;
71 88 }
89 +
90 + $entry = $this->make_entry( $msg[0], $msg[1] );
91 +
92 + if ( '' !== $entry->singular ) {
93 + $this->add_entry( $entry );
94 + }
72 95 }
96 +
97 + self::$cache->set( $lang->slug, $this->entries );
73 98 }
74 99
75 100 /**
76 - * Returns the post id of the post storing the strings translations
101 + * Deletes a string
77 102 *
78 - * @since 1.4
103 + * @since 2.9
79 104 *
80 - * @param object $lang
81 - * @return int
105 + * @param string $string The source string to remove from the translations.
106 + * @return void
82 107 */
83 - public static function get_id( $lang ) {
84 - 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;
108 + public function delete_entry( $string ) {
109 + unset( $this->entries[ $string ] );
95 110 }
96 111
97 112 /**
98 - * Invalidate the cache when adding a new language
113 + * Translates a string or returns false if the translation is not found.
114 + * Contrary to `self::translate()`, this method doesn't fallback to the source string but returns empty string instead.
99 115 *
100 - * @since 2.0.5
116 + * @since 3.7
117 + *
118 + * @param string $source The source string to translate.
119 + * @return string The translated string or empty string if not found.
101 120 */
102 - public function clean_cache() {
103 - wp_cache_delete( 'polylang_mo_ids' );
121 + public function translate_if_any( string $source ) {
122 + $entry = new Translation_Entry( array( 'singular' => $source ) );
123 + $entry = $this->translate_entry( $entry );
124 +
125 + if ( ! $entry instanceof Translation_Entry || empty( $entry->translations ) ) {
126 + return '';
127 + }
128 +
129 + return $entry->translations[0];
104 130 }
105 131 }