-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.php
468 lines (374 loc) · 14.6 KB
/
Main.php
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
<?php
/*
Plugin Name: MF Plus WPML
Plugin URI: http://magicfields.org
Description: This plugin provide a integration between magic fields and WPML
Versio: 1.2
Author: Magic Fields Team
Author URI: http://magicfields.org
Licence: GPL2
*/
/* Copyright 2010 Magic Fields Team (email : me@gnuget.org)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Magic Fields and WPML is already installed?
*
*/
function checking_dependencies() {
/**
* @todo If mf or wpml are not installed kill this process
*/
if(! method_exists('PanelFields','PanelFields') || ! defined('ICL_SITEPRESS_VERSION')) {
add_action('admin_notices','mf_wpml_notices');
}
}
/**
* Display a message in function if all the dependencies of the plugins
* are installed or not
*
*
* @todo improve the desing of this message
*/
function mf_wpml_notices() {
echo "<div class=\"mf_message error\">You need install first Magic Fields and WPML Multilingual CMS for use this plugin</div>";
}
add_action('init','checking_dependencies');
/**
* Fixing the links into the "manage posts screen"
*/
add_action('admin_print_scripts','mfwpml_scripts');
/**
*
*/
function mfwpml_scripts() {
global $parent_file;
$types = array('edit.php','edit-pages.php','edit.php?post_type=page');
if(in_array($parent_file,$types)) {
wp_enqueue_script('mfwpml',plugins_url('js/mfpluswpml_edit.js',__FILE__,array('jquery')));
}
}
/**
* Magic Fields Api Implementation
*/
// Filter for alter the textbox
/**
* using the same values of the original post
* for the translated one
*/
add_filter('mf_source_post_data','get_info');
function get_info($post_id = null) {
global $wpdb;
//Getting the source of the post_id
if(isset($_GET['trid']) && is_numeric($_GET['trid']) && empty($_GET['post'])) {
$post_id = $wpdb->get_var($wpdb->prepare("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid={$_GET['trid']}"));
}
return $post_id;
}
//Keeping update the custom fields between translated posts
add_action('mf_presave','mfplus_update_values',10,7);
function mfplus_update_values($field_meta_id,$name,$group_index,$field_index,$post_id,$value_field,$writepanel_id){
global $wpdb;
//looking for the translated version of the post
$trid = $wpdb->get_var($wpdb->prepare("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id = {$post_id} AND (element_type = 'post_post' OR element_type = 'post_page')"));
//Getting the ID's of the translatable fields
$ids = $wpdb->get_results("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid = {$trid} AND element_id != {$post_id}");
if(empty($ids)){
return true;
}
/** getting the translatables fields **/
if(!$translatables = get_option("mfplus_translatables")){
$translatables = array();
}
//getting the meta_id of the translated field
foreach($ids as $value) {
if(in_array($writepanel_id."_".$name,$translatables)){
add_post_meta($value->element_id,$name,$value_field);
$meta_id = $wpdb->insert_id;
// Adding the referencie in the magic fields post meta table
$wpdb->query("INSERT INTO ". MF_TABLE_POST_META .
" (id, field_name, group_count, field_count, post_id,order_id) ".
" VALUES ({$meta_id}, '{$name}',{$group_index},{$field_index},{$value->element_id},{$group_index})"
);
}else{
$exists = $wpdb->get_var("SELECT count(1) FROM ". MF_TABLE_POST_META .
" WHERE field_name = '{$name}' AND group_count = {$group_index} AND field_count = {$field_index} AND post_id = {$value->element_id} AND order_id = {$group_index}");
//if don exist we will insert this field with a black value
if(!$exists) {
add_post_meta($value->element_id,$name,"");
$meta_id = $wpdb->insert_id;
// Adding the referencie in the magic fields post meta table
$wpdb->query("INSERT INTO ". MF_TABLE_POST_META .
" (id, field_name, group_count, field_count, post_id,order_id) ".
" VALUES ({$meta_id}, '{$name}',{$group_index},{$field_index},{$value->element_id},{$group_index})"
);
}
}
}
}
/**
* Admin Menu
*/
add_action('admin_menu','mf_translatable_admin',20);
function mfplus_translatable_fields_admin() {
global $wpdb;
if(!$translatables = get_option("mfplus_translatables")) {
$translatables = array();
}
print "<h2>Custom Fields Translatables</h2>";
print "<div class='mf_plus'>";
print "<p>Select which Custom Fields will be keep sync between translated posts</p>";
print "</div>";
if(!empty($_GET['saved_translatable_fields']) && $_GET['saved_translatable_fields'] == "true"): ?>
<div id="message" class="updated">
<strong>Translatable fields saved</strong>
</div>
<?php endif;
//getting the write panels
$writepanels = RCCWP_CustomWritePanel::GetCustomWritePanels();
print "<form method=\"POST\">";
print "<div class='custom_fields'>";
foreach($writepanels as $writepanel) {
print "<h2>{$writepanel->name}</h2>";
//getting the custom fields of this write panel
$fields = $wpdb->get_results(" SELECT cf.id,cf.name,cf.description FROM ".MF_TABLE_GROUP_FIELDS." as cf LEFT JOIN ".MF_TABLE_PANEL_GROUPS." as mg ON (cf.group_id = mg.id) WHERE mg.panel_id = {$writepanel->id}");
?>
<table class="widefat" width="100%">
<thead>
<tr>
<th>Custom Field Name</th>
<th>Description</th>
<th>Keep sync</th>
</tr>
</thead>
<tbody>
<?php foreach ($fields as $field):?>
<tr>
<td><?php print $field->name;?></td>
<td><?php print $field->description;?></td>
<td>
<?php
$checked = "";
if(in_array($writepanel->id."_".$field->name,$translatables)){
$checked = "checked=checked";
}
?>
<input type="checkbox" name="translatable_field[<?php print $writepanel->id; ?>][<?php print $field->name;?>]" <?php print $checked;?>>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<?php
}
print "</div>";
print "<input type=\"submit\" name=\"save_submit\" id=\"save_sync\">";
print "</form>";
}
function mf_translatable_admin(){
add_submenu_page('MagicFieldsMenu','Translatable Fields','Translatable Fields','edit_pages','translatablefields','mfplus_translatable_fields_admin');
}
/** Saving translatable fields **/
add_action('init','save_translatable_fields');
function save_translatable_fields(){
if(empty($_POST['translatable_field'])){
return false;
}
$translatables = array();
foreach($_POST['translatable_field'] as $writepanel_id => $fields) {
foreach($fields as $field => $value){
$translatables[] = $writepanel_id."_".$field;
}
}
update_option("mfplus_translatables",$translatables);
wp_safe_redirect(
add_query_arg(
'saved_translatable_fields',
'true',
wp_get_referer()
)
);
}
add_action('save_post','mfpluswpml_delete_extras',11);
function mfpluswpml_delete_extras($postId) {
global $flag,$wpdb;
//when the translation come from the translation panel and not from the post page we no need delete extra values
if( !empty($_REQUEST['job_id'])) {
return false;
}
if($flag != 1) {
return $postId;
}
if ( $the_post = wp_is_post_revision($postId)) {
$postId = $the_post;
}
if(!empty($_REQUEST['rc-custom-write-panel-verify-key'])) {
if (!(current_user_can('edit_posts', $postId) || current_user_can('edit_published_pages', $postId))){
return $postId;
}
}
//looking for the translated version of the post
$trid = $wpdb->get_var($wpdb->prepare("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id = {$postId} AND (element_type = 'post_post' OR element_type = 'post_page')"));
if(is_null($trid)) {
return false;
}
//Getting the ID's of the translatable fields
$ids = $wpdb->get_results("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid = {$trid} AND element_id != {$postId}");
$extra_meta_ids = array();
foreach($ids as $k => $v) {
$tmp = $wpdb->get_results("select id from wp_mf_post_meta c WHERE c.post_id in (".$postId.",".$v->element_id.") AND id NOT IN (select a.id from wp_mf_post_meta a , wp_mf_post_meta b WHERE a.post_id = ".$v->element_id." AND b.post_id = ".$postId." AND a.field_name = b.field_name AND a.group_count = b.group_count AND a.field_count = b.field_count AND a.order_id = b.order_id UNION select a.id from wp_mf_post_meta a , wp_mf_post_meta b WHERE a.post_id = ".$postId." AND b.post_id = ".$v->element_id." AND a.field_name = b.field_name AND a.group_count = b.group_count AND a.field_count = b.field_count AND a.order_id = b.order_id)");
foreach($tmp as $z => $y) {
$extra_meta_ids[] = $y->id;
}
$meta_id = implode(",",$extra_meta_ids);
//deleting references for add the new data when the post be saved
$wpdb->query("DELETE FROM ".$wpdb->prefix."postmeta WHERE meta_id IN (".$meta_id.")");
$wpdb->query("DELETE FROM ".MF_TABLE_POST_META." WHERE id iN (".$meta_id.")");
}
}
add_action('save_post','mfpluswpml_save_post',9);
function mfpluswpml_save_post($postId) {
global $flag,$wpdb;
if($flag == 1) {
return $postId;
}
if ( $the_post = wp_is_post_revision($postId)) {
$postId = $the_post;
}
if(!empty($_REQUEST['rc-custom-write-panel-verify-key']) && empty($_REQUEST['icl_tm_action'])) {
//the user can edit posts?
if (!(current_user_can('edit_posts', $postId) || current_user_can('edit_published_pages', $postId))){
return $postId;
}
//looking for the translated version of the post
$trid = $wpdb->get_var($wpdb->prepare("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id = {$postId} AND (element_type = 'post_post' OR element_type = 'post_page')"));
//Getting the ID's of the translatable fields
$ids = $wpdb->get_results("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid = {$trid} AND element_id != {$postId}");
if(empty($ids)){
return true;
}
/** getting the translatables fields **/
if(!$translatables = get_option("mfplus_translatables")){
$translatables = array();
}
array_walk($translatables,'get_custom_field_name');
$translatables_text = implode("','",$translatables);
$ids_text = array();
foreach($ids as $k => $v) {
$ids_text[] = $v->element_id;
}
$ids_text = implode(",",$ids_text);
$meta_id = array();
$mids = $wpdb->get_results("SELECT id FROM ".MF_TABLE_POST_META." WHERE field_name IN ('".$translatables_text."') AND post_id IN (".$ids_text.")");
foreach($mids as $key => $value) {
$meta_id[] = $value->id;
}
$meta_id = implode(",",$meta_id);
//deleting references for add the new data when the post be saved
$wpdb->query("DELETE FROM ".$wpdb->prefix."postmeta WHERE meta_id IN (".$meta_id.")");
$wpdb->query("DELETE FROM ".MF_TABLE_POST_META." WHERE id iN (".$meta_id.")");
}
}
/**
* Return the custom field name from the translatable array
*/
function get_custom_field_name(&$item) {
$item = preg_replace("/[0-9]+\_/i","",$item);
}
/**
* Translating taxonomies
*/
add_filter('mf_extra_categories','mfwpml_add_categories');
function mfwpml_add_categories($categories) {
global $wpdb;
if( empty($_GET['lang']) || empty($_GET['source_lang']) || empty($_GET['trid']) || !is_numeric($_GET['trid'])) {
return $categories;
}
$pid = $wpdb->get_var("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid={$_GET['trid']}");
$tmp = $wpdb->get_results("SELECT term_taxonomy_id FROM {$wpdb->prefix}term_relationships WHERE object_id = {$pid}");
foreach($tmp as $key => $item ){
$cat[] = $item->term_taxonomy_id;
}
$categories = $cat;
$lang = $_GET['lang'];
$source_lang = $_GET['source_lang'];
$translated_ids = array();
foreach( $categories as $key => $item ) {
$query = $wpdb->prepare(
"SELECT
t.element_id
FROM
".$wpdb->prefix."icl_translations as f
LEFT JOIN
".$wpdb->prefix."icl_translations as t
ON
( f.trid = t.trid AND t.source_language_code = %s AND t.language_code = %s AND t.element_type = 'tax_category' )
WHERE
f.element_id = %d
", $source_lang, $lang, $item
);
$term = $wpdb->get_var($query);
if(empty($term)) {
continue;
}
$categories[$key] = $term;
}
return $categories;
}
/** Fix for the translations queue **/
add_action('mfwpml_sync_queue','mfwpml_sync_on_queue', 10, 2);
function mfwpml_sync_on_queue( $source_post, $translated_post_id ) {
global $wpdb;
//Getting the writepanel who belongs this posts
$write_panel_id = $wpdb->get_var("SELECT meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key = '_mf_write_panel_id' and post_id = ".$source_post->ID);
if( is_null($write_panel_id) ) {
//if not have write_panel_id is because this post don't belongs to a write panel
//and not have sense continue translating custom fields
return true;
}
//Getting the custom fields values of the original post
$values = $wpdb->get_results("SELECT
mfp.group_count,
mfp.field_count,
mfp.post_id,
mfp.field_name,
mfp.order_id,
wpp.meta_id,
wpp.meta_key,
wpp.meta_value
FROM
{$wpdb->prefix}mf_post_meta as mfp
LEFT JOIN
{$wpdb->prefix}postmeta as wpp ON ( mfp.id = wpp.meta_id )
WHERE
mfp.post_id = {$source_post->ID};
",ARRAY_A
);
//Linking the translated post with the right write panel
add_post_meta($translated_post_id,'_mf_write_panel_id',$write_panel_id);
//Inserting the new values
foreach( $values as $key => $value ) {
add_post_meta($translated_post_id, $value['meta_key'], $value['meta_value']);
$meta_id = $wpdb->insert_id;
$query = "INSERT INTO ".MF_TABLE_POST_META."
VALUES (
{$meta_id},
{$value['group_count']},
{$value['field_count']},
$translated_post_id,
'{$value['field_name']}',
{$value['order_id']}
)";
$wpdb->query($query);
}
}
/** / Fix for the translations queue **/