-
Notifications
You must be signed in to change notification settings - Fork 2
/
wpml-actions.php
354 lines (291 loc) · 10.1 KB
/
wpml-actions.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
<?php
/**
* Framework: hook into APP_View_Page::_get_id()
*/
function app_wpml_appthemes_page_id_for_template( $page_id, $template ) {
return icl_object_id( $page_id, 'page', true );
}
add_filter( 'appthemes_page_id_for_template', 'app_wpml_appthemes_page_id_for_template', 10, 2 );
/**
* Payments: Set proper language for new order
* Note: Orders probably should be excluded from localization ?!
*/
function app_wpml_create_order( $order_id, $order ) {
global $sitepress;
if ( $order->post_type != 'transaction' )
return;
$sitepress->set_element_language_details( $order_id, 'post_transaction', null, $sitepress->get_current_language() );
}
add_action( 'save_post', 'app_wpml_create_order', 20, 2 ); // after WPML's save_post_actions
/**
* Payments: Add language parameter to url
* Note: Orders probably should be excluded from localization ?!
*/
function app_wpml_appthemes_order_return_url( $url ) {
global $sitepress;
return str_replace( '&', '&', $sitepress->convert_url( $url ) );
}
add_filter( 'appthemes_order_return_url', 'app_wpml_appthemes_order_return_url' );
/**
* Payments: Removes language metabox for orders
*/
function app_wpml_orders_remove_language_metabox() {
remove_meta_box( 'icl_div', 'transaction', 'side' );
}
add_action( 'admin_head', 'app_wpml_orders_remove_language_metabox', 11 );
/**
* Payments: language selector for frontend order pages
* Orders are available only in one language, remove others from language selector
*
* @param array $languages
*
* @return array
*/
function app_wpml_orders_ls( $languages ) {
global $sitepress, $post;
$lang_code = $sitepress->get_current_language();
if ( is_singular() && get_post_type() == 'transaction' ) {
remove_filter( 'icl_ls_languages', 'app_wpml_orders_ls' );
$languages = $sitepress->get_ls_languages( array( 'skip_missing' => false ) );
$url = get_permalink( $post->ID );
foreach ( $languages as $code => $lang ) {
if ( $code == $lang_code ) {
$languages[ $code ]['url'] = $sitepress->convert_url( $url, $code );
} else {
unset( $languages[ $code ] );
}
}
add_filter( 'icl_ls_languages', 'app_wpml_orders_ls' );
}
return $languages;
}
add_filter( 'icl_ls_languages', 'app_wpml_orders_ls' );
/**
* ClassiPress: hook into cp_add_new_listing(), set proper language for new listing
*
* @param int $post_id
*
* @return void
*/
function app_wpml_cp_add_new_listing( $post_id ) {
global $sitepress;
if ( $sitepress->get_current_language() == $sitepress->get_default_language() ) {
return;
}
$post_type = get_post_type( $post_id );
$sitepress->set_element_language_details( $post_id, 'post_' . $post_type, null, $sitepress->get_current_language() );
}
add_action( 'cp_action_add_new_listing', 'app_wpml_cp_add_new_listing' );
/**
* ClassiPress: hook into cp_get_ad_details()
*
* @param object $result
* @param object $post
* @param string $location
*
* @return object
*/
function app_wpml_cp_ad_details_field( $result, $post, $location ) {
add_filter( 'cp_ad_details_' . $result->field_name, 'app_wpml_cp_ad_details_concrete_field', 10, 2 );
return $result;
}
add_filter( 'cp_ad_details_field', 'app_wpml_cp_ad_details_field', 10, 3 );
/**
* @param array $args
* @param object $result
*
* @return array
*/
function app_wpml_cp_ad_details_concrete_field( $args, $field ) {
$args['label'] = icl_translate( APP_TD, 'label_' . $field->field_name, $field->field_label );
$values = (array) $args['value'];
if ( 'checkbox' === $field->field_type ) {
if ( function_exists( 'cp_explode' ) ) {
$values = cp_explode( ',', $values );
} else {
$values = explode( ',', $values );
}
$values = array_map( 'trim', $values );
}
foreach ( $values as &$value ) {
$value = icl_translate( APP_TD, "value_{$field->field_name} {$value}", $value );
}
$args['value'] = implode( ", ", $values );
return $args;
}
/**
* ClassiPress: hook into cp_formbuilder(), cp_formbuilder_review()
*
* @param object $result
*
* @return object
*/
function app_wpml_cp_formbuilder_field( $result ) {
$result->field_label = icl_translate( APP_TD, 'label_' . $result->field_name, $result->field_label );
if ( ! empty( $result->field_tooltip ) ) {
$result->field_tooltip = icl_translate( APP_TD, 'tooltip_' . $result->field_name, $result->field_tooltip );
}
if ( ! empty( $result->field_values ) ) {
if ( function_exists( 'cp_explode' ) ) {
$options = cp_explode( ',', $result->field_values );
} else {
$options = explode( ',', $result->field_values );
}
$new_options = array();
foreach ( $options as $option ) {
$new_options[] = icl_t( APP_TD, 'value_' . $result->field_name . ' ' . trim( $option ), $option );
}
$result->field_values = implode( ',', $new_options );
}
return $result;
}
add_filter( 'cp_formbuilder_field', 'app_wpml_cp_formbuilder_field' );
add_filter( 'cp_formbuilder_review_field', 'app_wpml_cp_formbuilder_field' );
/**
* ClassiPress: hook into cp_package_field filter
*
* @param object $result
* @param string $type
*
* @return object
*/
function app_wpml_cp_package_field( $result, $type ) {
$result->pack_name = icl_translate( APP_TD, 'pack_name_' . $result->pack_id, $result->pack_name );
$result->pack_desc = icl_translate( APP_TD, 'pack_desc_' . $result->pack_id, $result->pack_desc );
return $result;
}
add_filter( 'cp_package_field', 'app_wpml_cp_package_field', 10, 2 );
/**
* ClassiPress: hook into get_pack()
*
* @param object $package
* @param int $pack_id
*
* @return object
*/
function app_wpml_cp_get_package( $package, $pack_id ) {
if ( ! empty( $package ) ) {
$package->pack_name = icl_translate( APP_TD, 'pack_name_' . $package->pack_id, $package->pack_name );
$package->pack_desc = icl_translate( APP_TD, 'pack_desc_' . $package->pack_id, $package->pack_desc );
}
return $package;
}
add_filter( 'cp_get_package', 'app_wpml_cp_get_package', 10, 2 );
/**
* ClassiPress: hook into cp_display_message()
*
* @param string $message
* @param string $tag
*
* @return string
*/
function app_wpml_cp_display_message( $message, $tag ) {
return icl_translate( APP_TD, 'message_' . $tag, $message );
}
add_filter( 'cp_display_message', 'app_wpml_cp_display_message', 10, 2 );
/**
* ClassiPress: hook into cp_custom_fields(), (un)registers strings immediately on maintaining custom fields
*
* @param string $action
* @param int $field_id
*
* @return void
*/
function app_wpml_cp_custom_fields( $action, $field_id ) {
global $wpdb;
$query = "SELECT * FROM $wpdb->cp_ad_fields WHERE field_id = %d";
$field = $wpdb->get_row( $wpdb->prepare( $query, $field_id ) );
switch ( $action ) {
case 'addfield':
case 'editfield':
icl_register_string( APP_TD, 'label_' . $field->field_name, $field->field_label );
icl_register_string( APP_TD, 'tooltip_' . $field->field_name, $field->field_tooltip );
if ( ! empty( $field->field_values ) ) {
if ( function_exists( 'cp_explode' ) ) {
$options = array_map( 'trim', cp_explode( ',', $field->field_values ) );
} else {
$options = array_map( 'trim', explode( ',', $field->field_values ) );
}
$options_escaped = array_map( 'esc_attr', $options );
$options = array_merge( $options, $options_escaped );
foreach ( $options as $option ) {
icl_register_string( APP_TD, 'value_' . $field->field_name . ' ' . $option, $option );
}
}
break;
case 'delete':
icl_unregister_string( APP_TD, 'label_' . $field->field_name, $field->field_label );
icl_unregister_string( APP_TD, 'tooltip_' . $field->field_name, $field->field_tooltip );
if ( ! empty( $field->field_values ) ) {
if ( function_exists( 'cp_explode' ) ) {
$options = array_map( 'trim', cp_explode( ',', $field->field_values ) );
} else {
$options = array_map( 'trim', explode( ',', $field->field_values ) );
}
$options_escaped = array_map( 'esc_attr', $options );
$options = array_merge( $options, $options_escaped );
foreach ( $options as $option ) {
icl_unregister_string( APP_TD, 'value_' . $field->field_name . ' ' . $option, $option );
}
}
break;
default:
break;
}
}
add_action( 'cp_custom_fields', 'app_wpml_cp_custom_fields', 10, 2 );
/**
* ClassiPress: show categories in all languages for form layouts
* This way one can define a single form for all languages, with custom
* field labels translated through string translation
*/
function app_wpml_cp_form_layouts_show_all_categories() {
if ( isset( $_GET['page'] ) && isset( $_GET['action'] ) && ! isset( $_GET['lang'] ) ) {
if ( ( $_GET['page'] == 'layouts' ) && in_array( $_GET['action'], array( 'editform', 'addform' ) ) ) {
$url = add_query_arg( 'lang', 'all' );
wp_redirect( $url );
}
}
}
add_action( 'admin_init', 'app_wpml_cp_form_layouts_show_all_categories' );
/**
* ClassiPress: language selector for frontend Edit Ad page
*
* @param array $languages
*
* @return array
*/
function app_wpml_cp_ls( $languages ) {
global $sitepress, $post;
$lang_code = $sitepress->get_current_language();
if ( is_page_template( 'tpl-edit-item.php' ) && isset( $_GET['aid'] ) ) {
$aid = $_GET['aid'];
$trid = $sitepress->get_element_trid( $aid, 'post_ad_listing' );
$translations = $sitepress->get_element_translations( $trid, 'post_ad_listing' );
foreach ( $translations as $code => $translation ) {
if ( $code != $lang_code ) {
$translated_aid = $translation->element_id;
$edit_page = $sitepress->convert_url( get_permalink( CP_Edit_Item::get_id() ), $code );
$url = add_query_arg( 'aid', $translated_aid, $edit_page );
$languages[ $code ]['url'] = $url;
} else {
$languages[ $code ]['url'] = add_query_arg( 'aid', $aid );
}
}
}
return $languages;
}
add_filter( 'icl_ls_languages', 'app_wpml_cp_ls' );
/**
* ClassiPress: Removes WPML sticky posts filtering which isn't working well with CP
*/
function app_wpml_cp_sticky_posts() {
global $sitepress, $pagenow;
if ( ( $pagenow == 'edit.php' ) && ! isset( $_GET['post_type'] ) ) {
return;
}
remove_filter( 'option_sticky_posts', array( $sitepress, 'option_sticky_posts' ) );
}
add_action( 'init', 'app_wpml_cp_sticky_posts', 11 );
add_action( 'admin_init', 'app_wpml_cp_sticky_posts', 11 );
add_action( 'admin_head', 'app_wpml_cp_sticky_posts', 11 );