]> _ Git - physioassist-wordpress.git/blob
e8eb070c38c5a8c1851cb7d315caa6a44d7bb763
[physioassist-wordpress.git] /
1 <?php
2
3 abstract class WPML_Slug_Translation_Records {
4
5         const CONTEXT_DEFAULT   = 'default';
6         const CONTEXT_WORDPRESS = 'WordPress';
7
8         /** @var wpdb $wpdb */
9         private $wpdb;
10
11         /** @var WPML_WP_Cache_Factory $cache_factory*/
12         private $cache_factory;
13
14         public function __construct( wpdb $wpdb, WPML_WP_Cache_Factory $cache_factory ) {
15                 $this->wpdb          = $wpdb;
16                 $this->cache_factory = $cache_factory;
17         }
18
19         /**
20          * @param string $type
21          *
22          * @return WPML_ST_Slug
23          */
24         public function get_slug( $type ) {
25                 $cache_item = $this->cache_factory->create_cache_item( $this->get_cache_group(), $type );
26
27                 if ( ! $cache_item->exists() ) {
28                         $slug = new WPML_ST_Slug();
29
30                         /** @var \stdClass $original */
31                         $original = $this->wpdb->get_row(
32                                 $this->wpdb->prepare(
33                                         "SELECT id, value, language, context, name
34                                          FROM {$this->wpdb->prefix}icl_strings
35                                          WHERE name = %s
36                                             AND (context = %s OR context = %s)",
37                                         $this->get_string_name( $type ),
38                                         self::CONTEXT_DEFAULT,
39                                         self::CONTEXT_WORDPRESS
40                                 )
41                         );
42
43                         if ( $original ) {
44                                 $slug->set_lang_data( $original );
45
46                                 /** @var array<\stdClass> $translations */
47                                 $translations = $this->wpdb->get_results(
48                                         $this->wpdb->prepare(
49                                                 "SELECT value, language, status
50                                          FROM {$this->wpdb->prefix}icl_string_translations
51                                          WHERE string_id = %d
52                                                 AND value <> ''",
53                                                 $original->id
54                                         )
55                                 );
56
57                                 if ( $translations ) {
58                                         foreach ( $translations as $translation ) {
59                                                 $slug->set_lang_data( $translation );
60                                         }
61                                 }
62                         }
63
64                         $cache_item->set( $slug );
65
66                 }
67
68                 return $cache_item->get();
69         }
70
71         /** @return string */
72         private function get_cache_group() {
73                 return __CLASS__ . '::' . $this->get_element_type();
74         }
75
76         private function flush_cache() {
77                 $cache_group = $this->cache_factory->create_cache_group( $this->get_cache_group() );
78                 $cache_group->flush_group_cache();
79         }
80
81         /**
82          * @deprecated use `get_slug` instead.
83          *
84          * @param string $type
85          * @param string $lang
86          *
87          * @return null|string
88          */
89         public function get_translation( $type, $lang ) {
90                 $slug = $this->get_slug( $type );
91
92                 if ( $slug->is_translation_complete( $lang ) ) {
93                         return $slug->get_value( $lang );
94                 }
95
96                 return null;
97         }
98
99         /**
100          * @deprecated use `get_slug` instead.
101          *
102          * @param string $type
103          * @param string $lang
104          *
105          * @return null|string
106          */
107         public function get_original( $type, $lang = '' ) {
108                 $slug = $this->get_slug( $type );
109
110                 if ( ! $lang || $slug->get_original_lang() === $lang ) {
111                         return $slug->get_original_value();
112                 }
113
114                 return null;
115         }
116
117         /**
118          * @deprecated use `get_slug` instead.
119          *
120          * @param string $type
121          *
122          * @return int|null
123          */
124         public function get_slug_id( $type ) {
125                 $slug = $this->get_slug( $type );
126
127                 if ( $slug->get_original_id() ) {
128                         return $slug->get_original_id();
129                 }
130
131                 return null;
132         }
133
134         /**
135          * @param string $type
136          * @param string $slug
137          *
138          * @return int|null
139          */
140         public function register_slug( $type, $slug ) {
141                 $string_id = icl_register_string(
142                         self::CONTEXT_WORDPRESS,
143                         $this->get_string_name( $type ),
144                         $slug
145                 );
146
147                 $this->flush_cache();
148
149                 return $string_id;
150         }
151
152         /**
153          * @param string $type
154          * @param string $slug
155          */
156         public function update_original_slug( $type, $slug ) {
157                 $this->wpdb->update(
158                         $this->wpdb->prefix . 'icl_strings',
159                         array( 'value' => $slug ),
160                         array( 'name' => $this->get_string_name( $type ) )
161                 );
162
163                 $this->flush_cache();
164         }
165
166         /**
167          * @deprecated use `get_slug` instead.
168          *
169          * @param string $type
170          *
171          * @return null|stdClass
172          */
173         public function get_original_slug_and_lang( $type ) {
174                 $original_slug_and_lang = null;
175
176                 $slug = $this->get_slug( $type );
177
178                 if ( $slug->get_original_id() ) {
179                         $original_slug_and_lang = (object) array(
180                                 'value'    => $slug->get_original_value(),
181                                 'language' => $slug->get_original_lang(),
182                         );
183                 }
184
185                 return $original_slug_and_lang;
186         }
187
188         /**
189          * @deprecated use `get_slug` instead.
190          *
191          * @param string $type
192          * @param bool   $only_status_complete
193          *
194          * @return array
195          */
196         public function get_element_slug_translations( $type, $only_status_complete = true ) {
197                 $slug = $this->get_slug( $type );
198
199                 $rows = array();
200
201                 foreach ( $slug->get_language_codes() as $lang ) {
202                         if ( $slug->get_original_lang() === $lang
203                                 || ( $only_status_complete && ! $slug->is_translation_complete( $lang ) )
204                         ) {
205                                 continue;
206                         }
207
208                         $rows[] = (object) array(
209                                 'value'    => $slug->get_value( $lang ),
210                                 'language' => $lang,
211                                 'status'   => $slug->get_status( $lang ),
212                         );
213                 }
214
215                 return $rows;
216         }
217
218         /**
219          * @deprecated use `get_slug` instead.
220          *
221          * @param array $types
222          *
223          * @return array
224          */
225         public function get_all_slug_translations( $types ) {
226                 $rows = array();
227
228                 foreach ( $types as $type ) {
229                         $slug = $this->get_slug( $type );
230
231                         foreach ( $slug->get_language_codes() as $lang ) {
232                                 if ( $slug->get_original_lang() !== $lang ) {
233                                         $rows[] = (object) array(
234                                                 'value' => $slug->get_value( $lang ),
235                                                 'name'  => $slug->get_name(),
236                                         );
237                                 }
238                         }
239                 }
240
241                 return $rows;
242         }
243
244         /**
245          * @deprecated use `get_slug` instead.
246          *
247          * @param string $type
248          *
249          * @return array
250          */
251         public function get_slug_translation_languages( $type ) {
252                 $languages = array();
253                 $slug      = $this->get_slug( $type );
254
255                 foreach ( $slug->get_language_codes() as $lang ) {
256                         if ( $slug->is_translation_complete( $lang ) ) {
257                                 $languages[] = $lang;
258                         }
259                 }
260
261                 return $languages;
262         }
263
264         /**
265          * Use `WPML_ST_String` only for updating the values in the DB
266          * because it does not have any caching feature.
267          *
268          * @param string $type
269          *
270          * @return null|WPML_ST_String
271          */
272         public function get_slug_string( $type ) {
273                 $string_id = $this->get_slug_id( $type );
274
275                 if ( $string_id ) {
276                         return new WPML_ST_String( $string_id, $this->wpdb );
277                 }
278
279                 return null;
280         }
281
282         /**
283          * @param string $slug
284          *
285          * @return string
286          */
287         abstract protected function get_string_name( $slug );
288
289         /** @return string */
290         abstract protected function get_element_type();
291 }