wpml-package.class.php
13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?php
class WPML_Package {
const CACHE_GROUP = 'WPML_Package';
public $ID;
public $view_link;
public $edit_link;
public $is_translation;
public $string_data;
public $title;
public $new_title;
public $kind_slug;
public $kind;
public $trid;
public $name;
public $translation_element_type;
public $post_id;
private $element_type_prefix;
/**
* This gives a context to determine what's really
* required to load. When set to `true`, we skip
* some useless DB queries in the constructor.
*
* @var bool
*/
private $translate_only = false;
/**
* @param stdClass|WPML_Package|array|int|WP_Post $data_item
*/
function __construct( $data_item ) {
$this->element_type_prefix = 'package';
$this->view_link = '';
$this->edit_link = '';
$this->post_id = null;
if ( $data_item ) {
if ( is_object( $data_item ) ) {
$data_item = get_object_vars( $data_item );
}
if ( is_numeric( $data_item ) ) {
$data_item = $this->init_from_id( $data_item, ARRAY_A );
}
if ( isset( $data_item['title'] ) ) {
$this->new_title = $data_item['title'];
}
if ( $data_item && is_array( $data_item ) ) {
$this->init_from_array( $data_item );
}
$this->new_title = $this->new_title != $this->title ? $this->new_title : null;
}
}
private function init_from_id( $id, $output = OBJECT ) {
global $wpdb;
$packages_query = "SELECT * FROM {$wpdb->prefix}icl_string_packages WHERE id=%s";
$packages_prepared = $wpdb->prepare( $packages_query, $id );
$package = $wpdb->get_row( $packages_prepared, $output );
return $package;
}
public function __get( $property ) {
if ( $property == 'id' ) {
_deprecated_argument( 'id', '0.0.2', "Property 'id' is deprecated. Please use 'ID'." );
return $this->ID;
}
if ( $property == 'post_id' ) {
return $this->ID;
}
if ( $property == 'post_title' ) {
return $this->title;
}
if ( $property == 'post_type' ) {
return $this->kind_slug;
}
return null;
}
public function __set( $property, $value ) {
if ( $property == 'id' ) {
_deprecated_argument( 'id', '0.0.2', "Property 'id' is deprecated. Please use 'ID'." );
$this->ID = $value;
} else {
$this->$property = $value;
}
}
public function __isset( $property ) {
if ( $property == 'id' ) {
return isset( $this->ID );
}
return false;
}
public function __unset( $property ) {
if ( $property == 'id' ) {
unset( $this->$property );
}
}
public function get_translation_element_type() {
return $this->translation_element_type;
}
public function get_package_post_id() {
return $this->get_element_type_prefix() . '_' . $this->kind_slug . '_' . $this->ID;
}
public function get_element_type_prefix() {
return $this->element_type_prefix;
}
public function set_package_post_data() {
$this->translation_element_type = $this->element_type_prefix . '_' . $this->kind_slug;
$this->update_strings_data();
}
public function update_strings_data() {
$strings = [];
$package_id = $this->ID;
if ( $package_id ) {
$results = $this->get_package_strings();
foreach ( $results as $result ) {
$string_name = $this->get_package_string_name_from_st_name( $result );
$strings[ $string_name ] = $result->value;
}
$this->string_data = $strings;
}
}
/**
* @param bool $refresh
*
* @return mixed
*/
public function get_package_strings( $refresh = false ) {
global $wpdb;
$package_id = $this->ID;
$results = false;
if ( $package_id ) {
$cache = new WPML_WP_Cache( self::CACHE_GROUP );
$cache_key = 'strings:' . $package_id;
$found = false;
$results = $cache->get( $cache_key, $found );
if ( ! $found || $refresh ) {
$results_query = "
SELECT id, name, value, wrap_tag, type, title
FROM {$wpdb->prefix}icl_strings
WHERE string_package_id=%d
ORDER BY location, id ASC";
$results_prepare = $wpdb->prepare( $results_query, $package_id );
$results = $wpdb->get_results( $results_prepare );
$cache->set( $cache_key, $results );
}
}
return $results;
}
public function set_strings_language( $language_code ) {
global $wpdb;
$package_id = $this->ID;
if ( $package_id ) {
$update_query = "UPDATE {$wpdb->prefix}icl_strings SET language=%s WHERE string_package_id=%d";
$update_prepare = $wpdb->prepare( $update_query, $language_code, $package_id );
$wpdb->query( $update_prepare );
// Action called after string is updated.
do_action( 'wpml_st_string_updated' );
}
}
/**
* @param \stdClass $result
*
* @return string
*/
private function get_package_string_name_from_st_name( $result ) {
// package string name is the same as the string name.
return $result->name;
}
private function sanitize_attributes() {
if ( isset( $this->name ) ) {
$this->name = $this->sanitize_string_name( $this->name );
}
if ( ( ! isset( $this->title ) || $this->title === '' ) && isset( $this->name ) ) {
$this->title = $this->name;
}
if ( ! isset( $this->edit_link ) ) {
$this->edit_link = '';
}
$this->sanitize_kind();
}
public function create_new_package_record() {
$this->sanitize_attributes();
$package_id = $this->package_exists();
if ( ! $package_id ) {
global $wpdb;
$data = array(
'kind_slug' => $this->kind_slug,
'kind' => $this->kind,
'name' => $this->name,
'title' => $this->title,
'edit_link' => $this->edit_link,
'view_link' => $this->view_link,
'post_id' => $this->post_id,
);
$wpdb->insert( $wpdb->prefix . 'icl_string_packages', $data );
$package_id = $wpdb->insert_id;
$this->ID = $package_id;
}
return $package_id;
}
public function update_package_record() {
$result = false;
if ( $this->ID ) {
global $wpdb;
$update_data = array(
'kind_slug' => $this->kind_slug,
'kind' => $this->kind,
'name' => $this->name,
'title' => $this->title,
'edit_link' => $this->edit_link,
'view_link' => $this->view_link,
);
$update_where = array(
'ID' => $this->ID,
);
$result = $wpdb->update( $wpdb->prefix . 'icl_string_packages', $update_data, $update_where );
}
return $result;
}
public function get_package_id() {
return $this->ID;
}
public function sanitize_string_name( $string_name ) {
$string_name = preg_replace( '/[ \[\]]+/', '-', $string_name );
return $string_name;
}
/**
* @param string $string_value
* @param string $sanitized_string_name
*
* @return string|mixed
*/
function translate_string( $string_value, $sanitized_string_name ) {
if ( $this->translate_only || $this->get_package_id() ) {
$sanitized_string_name = $this->sanitize_string_name( $sanitized_string_name );
$string_context = $this->get_string_context_from_package();
$string_name = $sanitized_string_name;
return icl_translate( $string_context, $string_name, $string_value );
} else {
return $string_value;
}
}
function get_string_context_from_package() {
return $this->kind_slug . '-' . $this->name;
}
public function get_string_id_from_package( $string_name, $string_value ) {
$package_id = $this->get_package_id();
$string_context = $this->get_string_context_from_package();
$string_data = array(
'context' => $string_context,
'name' => $string_name,
);
/**
* @param int|null $default
* @param array $string_data {
*
* @type string $context
* @type string $name Optional
* }
*/
$string_id = apply_filters( 'wpml_string_id', null, $string_data );
if ( ! $string_id ) {
$string_id = icl_register_string( $string_context, $string_name, $string_value, false, $this->get_package_language() );
}
return $string_id;
}
function get_translated_strings( $strings ) {
$package_id = $this->get_package_id();
if ( $package_id ) {
$results = $this->get_package_strings();
foreach ( $results as $result ) {
$translations = icl_get_string_translations_by_id( $result->id );
if ( ! empty( $translations ) ) {
$string_name = $this->get_package_string_name_from_st_name( $result );
$strings[ $string_name ] = $translations;
}
}
}
return $strings;
}
function set_translated_strings( $translations ) {
global $wpdb;
$this->sanitize_attributes();
$package_id = $this->get_package_id();
if ( $package_id ) {
foreach ( $translations as $string_name => $languages ) {
$string_id_query = "SELECT id FROM {$wpdb->prefix}icl_strings WHERE name='%s'";
$string_id_prepare = $wpdb->prepare( $string_id_query, $string_name );
$string_id = $wpdb->get_var( $string_id_prepare );
foreach ( $languages as $language_code => $language_data ) {
icl_add_string_translation( $string_id, $language_code, $language_data['value'], $language_data['status'] );
}
}
}
}
private function init_from_array( $args ) {
foreach ( $args as $key => $value ) {
if ( 'id' == $key ) {
$key = 'ID';
}
$this->$key = $value;
}
$this->sanitize_attributes();
if ( $this->translate_only ) {
return;
}
if ( $this->package_id_exists() || $this->package_name_and_kind_exists() ) {
$this->set_package_from_db();
}
$this->set_package_post_data();
}
public function has_kind_and_name() {
return ( isset( $this->kind ) && isset( $this->name ) && $this->kind && $this->name );
}
private function set_package_from_db() {
$package = false;
if ( $this->package_id_exists() ) {
$package = $this->get_package_from_id();
} elseif ( $this->package_name_and_kind_exists() ) {
$package = $this->get_package_from_name_and_kind();
}
if ( $package ) {
$this->object_to_package( $package );
}
$this->sanitize_kind();
}
private function get_package_from_id() {
$result = false;
if ( $this->has_id() ) {
global $wpdb;
$package_query = "SELECT * FROM {$wpdb->prefix}icl_string_packages WHERE ID=%d";
$package_prepared = $wpdb->prepare( $package_query, array( $this->ID ) );
$result = $wpdb->get_row( $package_prepared );
}
return $result;
}
private function get_package_from_name_and_kind() {
global $wpdb;
$cache = new WPML_WP_Cache( self::CACHE_GROUP );
$cache_key = 'name-kind:' . $this->kind_slug . $this->name;
$found = false;
$result = $cache->get( $cache_key, $found );
if ( ! $found ) {
$package_query = "SELECT * FROM {$wpdb->prefix}icl_string_packages WHERE kind_slug=%s AND name=%s";
$package_prepared = $wpdb->prepare( $package_query, array( $this->kind_slug, $this->name ) );
$result = $wpdb->get_row( $package_prepared );
if ( $result ) {
$cache->set( $cache_key, $result );
}
}
return $result;
}
private function package_name_and_kind_exists() {
$result = false;
if ( $this->has_kind_and_name() ) {
$result = (bool) $this->get_package_from_name_and_kind();
}
return $result;
}
private function package_id_exists() {
$result = false;
if ( $this->has_id() ) {
global $wpdb;
$package_query = "SELECT ID FROM {$wpdb->prefix}icl_string_packages WHERE ID=%d";
$package_prepared = $wpdb->prepare( $package_query, array( $this->ID ) );
$result = $wpdb->get_var( $package_prepared );
}
return $result;
}
/**
* @return bool|mixed
*/
protected function package_exists() {
$existing_package = false;
if ( $this->has_id() ) {
$existing_package = $this->package_id_exists();
} elseif ( $this->has_kind_and_name() ) {
$existing_package = $this->package_name_and_kind_exists();
}
return $existing_package;
}
/**
* @return bool
*/
private function has_id() {
return isset( $this->ID ) && $this->ID;
}
/**
* @param \stdClass $package
*/
private function object_to_package( $package ) {
$this->ID = $package->ID;
$this->kind_slug = $package->kind_slug;
$this->kind = $package->kind;
$this->name = $package->name;
$this->title = $package->title;
$this->edit_link = $package->edit_link;
$this->view_link = $package->view_link;
}
private function get_kind_from_slug() {
global $wpdb;
if ( $this->kind_slug ) {
$kinds_query = "SELECT kind FROM {$wpdb->prefix}icl_string_packages WHERE kind_slug=%s GROUP BY kind";
$kinds_prepared = $wpdb->prepare( $kinds_query, $this->kind_slug );
$kinds = $wpdb->get_col( $kinds_prepared );
if ( count( $kinds ) > 1 ) {
throw new WPML_Package_Exception( 'error', 'Package contains multiple kinds' );
}
if ( $kinds ) {
return $kinds[0];
}
}
return null;
}
private function sanitize_kind() {
if ( isset( $this->kind ) && ( ! isset( $this->kind_slug ) || trim( $this->kind_slug ) === '' ) ) {
$this->kind_slug = sanitize_title_with_dashes( $this->kind );
}
if ( $this->kind == $this->kind_slug ) {
$this->kind = $this->get_kind_from_slug();
}
}
public function get_package_element_type() {
return 'package_' . $this->kind_slug;
}
/**
* @return string|null
*/
public function get_package_language() {
global $sitepress;
if ( $this->post_id ) {
$details = null;
$post = get_post( $this->post_id );
if ( $post ) {
$details = $sitepress->get_element_language_details( $this->post_id, 'post_' . $post->post_type );
}
} else {
$element_type = $this->get_package_element_type();
$details = $sitepress->get_element_language_details( $this->ID, $element_type );
}
if ( $details ) {
return $details->language_code;
} else {
return null;
}
}
public function are_all_strings_included( $strings ) {
// check to see if all the strings in this package are present in $strings
$package_strings = $this->get_package_strings();
if ( is_array( $package_strings ) ) {
foreach ( $package_strings as $string ) {
if ( ! in_array( $string->id, $strings ) ) {
return false;
}
}
}
return true;
}
public function flush_cache() {
$cache = new WPML_WP_Cache( self::CACHE_GROUP );
$cache->flush_group_cache();
}
}