PluginProbe
Polylang / 3.2.7
Polylang v3.2.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
polylang / include / mo.php

mo.php in Polylang 3.2.7, at include/mo.php

125 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Manages strings translations storage
8 *
9 * @since 1.2
10 * @since 2.1 Stores the strings in a post meta instead of post content to avoid unserialize issues (See #63)
11 */
12 class PLL_MO extends MO {
13
14 /**
15 * Registers the polylang_mo custom post type, only at first object creation
16 *
17 * @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 *
31 * @since 1.2
32 *
33 * @param PLL_Language $lang The language in which we want to export strings.
34 * @return void
35 */
36 public function export_to_db( $lang ) {
37 /*
38 * It 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.
40 * The strings are slashed to avoid breaking slashed strings in update_post_meta.
41 * @see https://codex.wordpress.org/Function_Reference/update_post_meta#Character_Escaping.
42 */
43 $strings = array();
44 foreach ( $this->entries as $entry ) {
45 $strings[] = wp_slash( array( $entry->singular, $this->translate( $entry->singular ) ) );
46 }
47
48 if ( empty( $lang->mo_id ) ) {
49 $post = array(
50 'post_title' => 'polylang_mo_' . $lang->term_id,
51 'post_status' => 'private', // To avoid a conflict with WP Super Cache. See https://wordpress.org/support/topic/polylang_mo-and-404s-take-2
52 'post_type' => 'polylang_mo',
53 );
54 $mo_id = wp_insert_post( $post );
55 update_post_meta( $mo_id, '_pll_strings_translations', $strings );
56 } else {
57 update_post_meta( $lang->mo_id, '_pll_strings_translations', $strings );
58 }
59 }
60
61 /**
62 * Reads a PLL_MO object from a custom post meta.
63 *
64 * @since 1.2
65 *
66 * @param PLL_Language $lang The language in which we want to get strings.
67 * @return void
68 */
69 public function import_from_db( $lang ) {
70 if ( ! empty( $lang->mo_id ) ) {
71 $strings = get_post_meta( $lang->mo_id, '_pll_strings_translations', true );
72 if ( is_array( $strings ) ) {
73 foreach ( $strings as $msg ) {
74 $this->add_entry( $this->make_entry( $msg[0], $msg[1] ) );
75 }
76 }
77 }
78 }
79
80 /**
81 * Returns the post id of the post storing the strings translations.
82 *
83 * @since 1.4
84 *
85 * @param PLL_Language $lang The language object.
86 * @return int
87 */
88 public static function get_id( $lang ) {
89 global $wpdb;
90
91 $ids = wp_cache_get( 'polylang_mo_ids' );
92
93 if ( empty( $ids ) ) {
94 $ids = $wpdb->get_results( "SELECT post_title, ID FROM $wpdb->posts WHERE post_type='polylang_mo'", OBJECT_K );
95 wp_cache_add( 'polylang_mo_ids', $ids );
96 }
97
98 // The mo id for a language can be transiently empty
99 return isset( $ids[ 'polylang_mo_' . $lang->term_id ] ) ? $ids[ 'polylang_mo_' . $lang->term_id ]->ID : null;
100 }
101
102 /**
103 * Invalidate the cache when adding a new language
104 *
105 * @since 2.0.5
106 *
107 * @return void
108 */
109 public function clean_cache() {
110 wp_cache_delete( 'polylang_mo_ids' );
111 }
112
113 /**
114 * Deletes a string
115 *
116 * @since 2.9
117 *
118 * @param string $string The source string to remove from the translations.
119 * @return void
120 */
121 public function delete_entry( $string ) {
122 unset( $this->entries[ $string ] );
123 }
124 }
125