-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-edd-multilingual.php
231 lines (193 loc) · 7.99 KB
/
class-edd-multilingual.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
<?php
/**
* Main plugin class.
*/
class EDD_Multilingual {
/**
* EDD_Multilingual constructor.
*/
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'init' ), 20 );
}
/**
* Initialize plugin.
*/
public function init() {
// Sanity check.
if ( ! defined( 'ICL_SITEPRESS_VERSION' ) || ! defined( 'EDD_VERSION' ) ) {
add_action( 'admin_notices', array( $this, 'error_no_plugins' ) );
return;
} elseif ( version_compare( ICL_SITEPRESS_VERSION, '4.0', '<' ) ) {
add_action( 'admin_notices', array( $this, 'error_wpml_update' ) );
return;
}
// WPML setup has to be finished.
if ( ! apply_filters( 'wpml_setting', false, 'setup_complete' ) ) {
add_action( 'admin_notices', array( $this, 'error_wpml_setup' ) );
return;
}
$this->init_hooks();
$this->switch_payment_language();
$this->translate_page_ids();
}
/**
* Error message if requirements not met.
*/
public function error_no_plugins() {
$message = __( '%s plugin is enabled but not effective. It requires %s and %s plugins in order to work.', 'edd_multilingual' );
echo '<div class="error"><p>' .
sprintf( $message, '<strong>EDD multilingual</strong>',
'<a href="http://wpml.org/">WPML</a>',
'<a href="https://wordpress.org/plugins/easy-digital-downloads/">Easy Digital Downloads</a>' ) .
'</p></div>';
}
/**
* Error message if WPML is not recent enough.
*/
public function error_wpml_update() {
$message = __( '%s plugin is enabled but not effective. It requires at least WPML 4.0.', 'edd_multilingual' );
echo '<div class="error"><p>' . sprintf( $message, '<strong>EDD multilingual</strong>' ) . '</p></div>';
}
/**
* Error message if WPML setup is not finished.
*/
public function error_wpml_setup() {
$message = __( '%s plugin is enabled but not effective. You have to finish WPML setup.', 'edd_multilingual' );
echo '<div class="error"><p>' . sprintf( $message, '<strong>EDD multilingual</strong>' ) . '</p></div>';
}
/**
* Load plugin hooks.
*/
public function init_hooks() {
global $sitepress;
// Save order language.
add_action( 'edd_insert_payment', array( $this, 'save_payment_language' ), 10 );
// Add language column to the payments history table.
add_filter( 'edd_payments_table_columns', array( $this, 'payments_table_language_column' ) );
add_filter( 'edd_payments_table_column', array( $this, 'render_payments_table_column' ), 10, 3 );
// Add back the flags to downloads manager. NOTE: Not working when EDD FES is used.
add_filter( 'edd_download_columns', array(
new WPML_Custom_Columns( $sitepress ),
'add_posts_management_column'
) );
// Remove WPML header for some admin pages.
if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array(
'edd-payment-history',
'edd-discounts',
'edd-settings'
) )
) {
add_action( 'admin_enqueue_scripts', array( $this, 'remove_wpml_language_filter' ), 5 );
}
// Support for 'Checkout Fields Manager' extension.
add_filter( 'option_cfm-checkout-form', array( $this, 'translate_checkout_fields_id' ) );
// Adds support for REST requests.
add_filter( 'edd_api_v2_products_query_args', array( $this, 'rest_returns_current_language' ) );
}
/**
* Save the language the payment was made in.
*
* @param $payment
*/
public function save_payment_language( $payment ) {
update_post_meta( $payment, 'wpml_language', apply_filters( 'wpml_current_language', null ) );
}
/**
* Send email notifications in the correct language.
*/
public function switch_payment_language() {
if ( is_admin() && isset( $_GET['edd-action'] ) && $_GET['edd-action'] == 'email_links' ) {
$language_code = self::get_payment_language( $_GET['purchase_id'] );
if ( ! empty( $language_code ) ) {
do_action( 'wpml_switch_language', $language_code );
}
}
}
/**
* Remove WPML post count per language in admin page header.
*/
public function remove_wpml_language_filter() {
global $sitepress;
remove_action( 'admin_enqueue_scripts', array( $sitepress, 'language_filter' ) );
}
/**
* Translate EDD page IDs.
*/
public function translate_page_ids() {
global $edd_options;
// Re-read settings because EDD reads them before WPML has hooked onto the filters.
$edd_options = edd_get_settings();
// Translate post_id for pages in options.
isset( $edd_options['purchase_page'] ) ? $edd_options['purchase_page'] = apply_filters( 'wpml_object_id', $edd_options['purchase_page'], 'page', true ) : '';
isset( $edd_options['success_page'] ) ? $edd_options['success_page'] = apply_filters( 'wpml_object_id', $edd_options['success_page'], 'page', true ) : '';
isset( $edd_options['failure_page'] ) ? $edd_options['failure_page'] = apply_filters( 'wpml_object_id', $edd_options['failure_page'], 'page', true ) : '';
isset( $edd_options['purchase_history_page'] ) ? $edd_options['purchase_history_page'] = apply_filters( 'wpml_object_id', $edd_options['purchase_history_page'], 'page', true ) : '';
isset( $edd_options['login_redirect_page'] ) ? $edd_options['login_redirect_page'] = apply_filters( 'wpml_object_id', $edd_options['login_redirect_page'], 'page', true ) : '';
// Translate post_id for edd-fes add-on.
isset( $edd_options['fes-vendor-dashboard-page'] ) ? $edd_options['fes-vendor-dashboard-page'] = apply_filters( 'wpml_object_id', $edd_options['fes-vendor-dashboard-page'], 'page', true ) : '';
isset( $edd_options['fes-vendor-page'] ) ? $edd_options['fes-vendor-page'] = apply_filters( 'wpml_object_id', $edd_options['fes-vendor-page'], 'page', true ) : '';
isset( $edd_options['fes-submission-form'] ) ? $edd_options['fes-submission-form'] = apply_filters( 'wpml_object_id', $edd_options['fes-submission-form'], 'page', true ) : '';
isset( $edd_options['fes-profile-form'] ) ? $edd_options['fes-profile-form'] = apply_filters( 'wpml_object_id', $edd_options['fes-profile-form'], 'page', true ) : '';
isset( $edd_options['fes-login-form'] ) ? $edd_options['fes-login-form'] = apply_filters( 'wpml_object_id', $edd_options['fes-login-form'], 'page', true ) : '';
isset( $edd_options['fes-registration-form'] ) ? $edd_options['fes-registration-form'] = apply_filters( 'wpml_object_id', $edd_options['fes-registration-form'], 'page', true ) : '';
isset( $edd_options['fes-vendor-contact-form'] ) ? $edd_options['fes-vendor-contact-form'] = apply_filters( 'wpml_object_id', $edd_options['fes-vendor-contact-form'], 'page', true ) : '';
}
/**
* Translate the post_id for checkout fields.
*/
function translate_checkout_fields_id( $id ) {
return apply_filters( 'wpml_object_id', $id, 'edd-checkout-fields', true );
}
/**
* Add "Language" column to the payments table.
*
* @param $columns
*
* @return mixed
*/
public function payments_table_language_column( $columns ) {
$columns['language'] = __( 'Language', 'easy-digital-downloads' );
return $columns;
}
/**
* Fill payments table "Language" column with payment languages presented as flags.
*
* @param $value
* @param $payment_id
* @param $column_name
*
* @return string
*/
public function render_payments_table_column( $value, $payment_id, $column_name ) {
if ( $column_name === 'language' ) {
$language_code = self::get_payment_language( $payment_id );
$languages = apply_filters( 'wpml_active_languages', null, 'orderby=id&order=desc' );
if ( array_key_exists( $language_code, $languages ) ) {
$language_data = $languages[ $language_code ];
$value = '<img src="' . $language_data['country_flag_url'] . '" height="12" width="18" />';
return $value;
}
$value = __( 'N/A', 'edd_multilingual' );
}
return $value;
}
/**
* Retrieving payment language from post_meta table.
*
* @param $payment_id
*
* @return String
*/
public static function get_payment_language( $payment_id ) {
return get_post_meta( intval( $payment_id ), 'wpml_language', true );
}
/**
* @param array $args
*
* @return array
*/
public function rest_returns_current_language( $args ) {
$args['suppress_filters'] = false;
return $args;
}
}