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 +74 -62 2.9.23.7.1 View file →
@@ -3,117 +3,129 @@
3 3 * @package Polylang
4 4 */
5 5
6 6 /**
7 - * 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.
8 11 *
9 12 * @since 1.2
10 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.
11 15 */
12 16 class PLL_MO extends MO {
17 + /**
18 + * Static cache for the translations.
19 + *
20 + * @var PLL_Cache<array>
21 + */
22 + private static $cache;
13 23
14 24 /**
15 - * Registers the polylang_mo custom post type, only at first object creation
25 + * Constructor, initializes the cache if not already done.
16 26 *
17 - * @since 1.2
27 + * @since 3.7
18 28 */
19 29 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' ) );
30 + if ( empty( self::$cache ) ) {
31 + self::$cache = new PLL_Cache();
25 32 }
26 33 }
27 34
28 35 /**
29 - * Writes a PLL_MO object into a custom post meta
36 + * Writes the strings into a term meta.
30 37 *
31 38 * @since 1.2
32 39 *
33 - * @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
34 42 */
35 43 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
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 + */
40 50 $strings = array();
41 51 foreach ( $this->entries as $entry ) {
42 - $strings[] = array( $entry->singular, $this->translate( $entry->singular ) );
52 + if ( '' !== $entry->singular ) {
53 + $strings[] = wp_slash( array( $entry->singular, $this->translate( $entry->singular ) ) );
54 + }
43 55 }
44 56
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
57 + update_term_meta( $lang->term_id, '_pll_strings_translations', $strings );
46 58
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 - }
59 + self::$cache->clean( $lang->slug );
58 60 }
59 61
60 62 /**
61 - * Reads a PLL_MO object from a custom post meta
63 + * Reads a PLL_MO object from the term meta.
62 64 *
63 65 * @since 1.2
66 + * @since 3.4 Reads a PLL_MO from the term meta.
64 67 *
65 - * @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
66 70 */
67 71 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 - }
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;
75 77 }
76 - }
77 78
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;
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 + }
88 84
89 - $ids = wp_cache_get( 'polylang_mo_ids' );
85 + foreach ( $strings as $msg ) {
86 + if ( '' === $msg[0] || '' === $msg[1] ) {
87 + continue;
88 + }
90 89
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 );
90 + $entry = $this->make_entry( $msg[0], $msg[1] );
91 +
92 + if ( '' !== $entry->singular ) {
93 + $this->add_entry( $entry );
94 + }
94 95 }
95 96
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;
97 + self::$cache->set( $lang->slug, $this->entries );
98 98 }
99 99
100 100 /**
101 - * Invalidate the cache when adding a new language
101 + * Deletes a string
102 102 *
103 - * @since 2.0.5
103 + * @since 2.9
104 + *
105 + * @param string $string The source string to remove from the translations.
106 + * @return void
104 107 */
105 - public function clean_cache() {
106 - wp_cache_delete( 'polylang_mo_ids' );
108 + public function delete_entry( $string ) {
109 + unset( $this->entries[ $string ] );
107 110 }
108 111
109 112 /**
110 - * Deletes a string
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.
111 115 *
112 - * @since 2.9
116 + * @since 3.7
113 117 *
114 - * @param string $string The source string to remove from the translations.
118 + * @param string $source The source string to translate.
119 + * @return string The translated string or empty string if not found.
115 120 */
116 - public function delete_entry( $string ) {
117 - unset( $this->entries[ $string ] );
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];
118 130 }
119 131 }