| @@ -3,31 +3,38 @@ | ||
| 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 | 40 | * @param PLL_Language $lang The language in which we want to export strings. |
| @@ -33,94 +40,92 @@ | ||
| 33 | 40 | * @param PLL_Language $lang The language in which we want to export strings. |
| 34 | 41 | * @return void |
| 35 | 42 | */ |
| 36 | 43 | public function export_to_db( $lang ) { |
| 37 | - $this->add_entry( $this->make_entry( '', '' ) ); // Empty string translation, just in case | |
| 38 | - | |
| 39 | 44 | /* |
| 40 | - * It would be convenient to store the whole object but it would take a huge space in DB. | |
| 45 | + * It would be convenient to store the whole object, but it would take a huge space in DB. | |
| 41 | 46 | * So let's keep only the strings in an array. |
| 42 | - * The strings are slashed to avoid breaking slashed strings in update_post_meta. | |
| 47 | + * The strings are slashed to avoid breaking slashed strings in update_term_meta. | |
| 43 | 48 | * @see https://codex.wordpress.org/Function_Reference/update_post_meta#Character_Escaping. |
| 44 | 49 | */ |
| 45 | 50 | $strings = array(); |
| 46 | 51 | foreach ( $this->entries as $entry ) { |
| 47 | - $strings[] = wp_slash( array( $entry->singular, $this->translate( $entry->singular ) ) ); | |
| 52 | + if ( '' !== $entry->singular ) { | |
| 53 | + $strings[] = wp_slash( array( $entry->singular, $this->translate( $entry->singular ) ) ); | |
| 54 | + } | |
| 48 | 55 | } |
| 49 | 56 | |
| 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 | - } | |
| 57 | + update_term_meta( $lang->term_id, '_pll_strings_translations', $strings ); | |
| 58 | + | |
| 59 | + self::$cache->clean( $lang->slug ); | |
| 61 | 60 | } |
| 62 | 61 | |
| 63 | 62 | /** |
| 64 | - * Reads a PLL_MO object from a custom post meta. | |
| 63 | + * Reads a PLL_MO object from the term meta. | |
| 65 | 64 | * |
| 66 | 65 | * @since 1.2 |
| 66 | + * @since 3.4 Reads a PLL_MO from the term meta. | |
| 67 | 67 | * |
| 68 | 68 | * @param PLL_Language $lang The language in which we want to get strings. |
| 69 | 69 | * @return void |
| 70 | 70 | */ |
| 71 | 71 | public function import_from_db( $lang ) { |
| 72 | - if ( ! empty( $lang->mo_id ) ) { | |
| 73 | - $strings = get_post_meta( $lang->mo_id, '_pll_strings_translations', true ); | |
| 74 | - if ( is_array( $strings ) ) { | |
| 75 | - foreach ( $strings as $msg ) { | |
| 76 | - $this->add_entry( $this->make_entry( $msg[0], $msg[1] ) ); | |
| 77 | - } | |
| 78 | - } | |
| 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; | |
| 79 | 77 | } |
| 80 | - } | |
| 81 | 78 | |
| 82 | - /** | |
| 83 | - * Returns the post id of the post storing the strings translations. | |
| 84 | - * | |
| 85 | - * @since 1.4 | |
| 86 | - * | |
| 87 | - * @param PLL_Language $lang The language object. | |
| 88 | - * @return int | |
| 89 | - */ | |
| 90 | - public static function get_id( $lang ) { | |
| 91 | - 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 | + } | |
| 92 | 84 | |
| 93 | - $ids = wp_cache_get( 'polylang_mo_ids' ); | |
| 85 | + foreach ( $strings as $msg ) { | |
| 86 | + if ( '' === $msg[0] || '' === $msg[1] ) { | |
| 87 | + continue; | |
| 88 | + } | |
| 94 | 89 | |
| 95 | - if ( empty( $ids ) ) { | |
| 96 | - $ids = $wpdb->get_results( "SELECT post_title, ID FROM $wpdb->posts WHERE post_type='polylang_mo'", OBJECT_K ); | |
| 97 | - 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 | + } | |
| 98 | 95 | } |
| 99 | 96 | |
| 100 | - // The mo id for a language can be transiently empty | |
| 101 | - return isset( $ids[ 'polylang_mo_' . $lang->term_id ] ) ? $ids[ 'polylang_mo_' . $lang->term_id ]->ID : null; | |
| 97 | + self::$cache->set( $lang->slug, $this->entries ); | |
| 102 | 98 | } |
| 103 | 99 | |
| 104 | 100 | /** |
| 105 | - * Invalidate the cache when adding a new language | |
| 101 | + * Deletes a string | |
| 106 | 102 | * |
| 107 | - * @since 2.0.5 | |
| 103 | + * @since 2.9 | |
| 108 | 104 | * |
| 105 | + * @param string $string The source string to remove from the translations. | |
| 109 | 106 | * @return void |
| 110 | 107 | */ |
| 111 | - public function clean_cache() { | |
| 112 | - wp_cache_delete( 'polylang_mo_ids' ); | |
| 108 | + public function delete_entry( $string ) { | |
| 109 | + unset( $this->entries[ $string ] ); | |
| 113 | 110 | } |
| 114 | 111 | |
| 115 | 112 | /** |
| 116 | - * 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. | |
| 117 | 115 | * |
| 118 | - * @since 2.9 | |
| 116 | + * @since 3.7 | |
| 119 | 117 | * |
| 120 | - * @param string $string The source string to remove from the translations. | |
| 121 | - * @return void | |
| 118 | + * @param string $source The source string to translate. | |
| 119 | + * @return string The translated string or empty string if not found. | |
| 122 | 120 | */ |
| 123 | - public function delete_entry( $string ) { | |
| 124 | - 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]; | |
| 125 | 130 | } |
| 126 | 131 | } |