Newer
Older
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
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*
*/
// This file must not accessed directly.
if (!defined('ABSPATH')) {
exit;
}
/**
* WPML plugin compatatibility class.
*
* @since 5.66
*/
class CiviCRM_For_WordPress_Compat_WPML {
/**
* @var object
* Plugin object reference.
* @since 5.66
* @access public
*/
public $civi;
/**
* @var array
* Collected rewrites.
* @since 5.75
* @access private
*/
private $rewrites = [];
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
/**
* Instance constructor.
*
* @since 5.66
*/
public function __construct() {
// Store reference to CiviCRM plugin object.
$this->civi = civi_wp();
// Register plugin compatibility hooks.
$this->register_hooks();
}
/**
* Register hooks.
*
* This is called via the constructor during the "plugins_loaded" action which
* is much earlier that CiviCRM's own internal hooks. The reason for this is
* that compability may need callbacks for events that fire well before "init"
* which is when CiviCRM begins to load.
*
* @since 5.66
*/
public function register_hooks() {
// Bail if CiviCRM not installed yet.
if (!CIVICRM_INSTALLED) {
return;
}
// Bail if WPML is not present.
if (!defined('ICL_SITEPRESS_VERSION')) {
return;
}
// Register WPML compatibility callbacks.
add_action('civicrm_after_rewrite_rules', [$this, 'rewrite_rules'], 10, 2);
add_filter('icl_ls_languages', [$this, 'basepage_urls_rebuild']);
// Register specific CiviCRM callbacks.
add_filter('civicrm/basepage/match', [$this, 'basepage_match'], 10, 2);
add_filter('civicrm/core/url/base', [$this, 'base_url_filter'], 10, 2);
add_filter('civicrm/core/locale', [$this, 'locale_filter'], 10, 2);
}
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
/**
* Setup the rewrite rules for WPML.
*
* @since 5.75
*
* @param bool $flush_rewrite_rules True if rules flushed, false otherwise.
* @param WP_Post $basepage The Base Page post object.
*/
public function rewrite_rules($flush_rewrite_rules, $basepage) {
global $sitepress, $wpml_url_filters;
/*
* Collect all rewrite rules into an array.
*
* Because the array of specific Post IDs is added *after* the array of
* paths for the Base Page ID, those specific rewrite rules will "win" over
* the more general Base Page rules.
*/
$collected_rewrites = [];
// Grab information about configuration.
$wpml_active = apply_filters('wpml_active_languages', NULL);
// Support prefixes for a single Base Page.
$wpml_url_filters->remove_global_hooks();
remove_filter('page_link', [$wpml_url_filters, 'page_link_filter'], 1);
$basepage_url = get_permalink($basepage->ID);
add_filter('page_link', [$wpml_url_filters, 'page_link_filter'], 1, 2);
$wpml_url_filters->add_global_hooks();
foreach ($wpml_active as $slug => $data) {
$language_url = $sitepress->convert_url($basepage_url, $slug);
$parsed_url = wp_parse_url($language_url, PHP_URL_PATH);
$regex_path = substr($parsed_url, 1);
$collected_rewrites[$basepage->ID][] = $regex_path;
$post_id = apply_filters('wpml_object_id', $basepage->ID, 'page', FALSE, $slug);
if (!empty($post_id)) {
$collected_rewrites[$post_id][] = $regex_path;
}
}
// Support prefixes for Base Pages in multiple languages.
foreach ($wpml_active as $slug => $data) {
// Determine if there is a translation.
$post_id = apply_filters('wpml_object_id', $basepage->ID, 'page', FALSE, $slug);
if (empty($post_id)) {
continue;
}
// Get the regex path for the unfiltered permalink.
$wpml_url_filters->remove_global_hooks();
remove_filter('page_link', [$wpml_url_filters, 'page_link_filter'], 1);
$url = get_permalink($post_id);
add_filter('page_link', [$wpml_url_filters, 'page_link_filter'], 1, 2);
$wpml_url_filters->add_global_hooks();
$parsed_url = wp_parse_url($url, PHP_URL_PATH);
$regex_path = substr($parsed_url, 1);
// Get the regex path for the converted permalink.
$converted_url = $sitepress->convert_url($url, $slug);
$parsed_url = wp_parse_url($converted_url, PHP_URL_PATH);
$regex_path_converted = substr($parsed_url, 1);
$collected_rewrites[$basepage->ID][] = $regex_path;
$collected_rewrites[$basepage->ID][] = $regex_path_converted;
$collected_rewrites[$post_id][] = $regex_path;
$collected_rewrites[$post_id][] = $regex_path_converted;
}
// Make collection unique and add remaining rewrite rules.
$this->rewrites = array_map('array_unique', $collected_rewrites);
if (!empty($this->rewrites)) {
foreach ($this->rewrites as $post_id => $rewrite) {
foreach ($rewrite as $path) {
add_rewrite_rule(
'^' . $path . '([^?]*)?',
'index.php?page_id=' . $post_id . '&civiwp=CiviCRM&q=civicrm%2F$matches[1]',
'top'
);
}
}
}
// Maybe force flush.
if ($flush_rewrite_rules) {
flush_rewrite_rules();
}
}
/**
* Fixes the CiviCRM Base Page URLs in the WPML language switcher.
*
* @since 5.75
*
* @param array $languages The array of language data for the current query.
* @return array $languages The modified array of language data for the current query.
*/
public function basepage_urls_rebuild($languages) {
global $post, $sitepress;
// We need the current Post object.
if (empty($post) || !($post instanceof WP_Post)) {
return $languages;
}
// Skip unless on Base Page.
if (!civi_wp()->basepage->is_match($post->ID)) {
return $languages;
}
// Get the canonical Base Page URL in the current language.
$permalink = get_permalink($post->ID);
$canonical = htmlspecialchars_decode(civi_wp()->basepage->basepage_canonical_url($permalink));
// Let's deal with this first.
foreach ($languages as &$language) {
if ($language['active']) {
$language['url'] = $canonical;
}
}
// We do not want to filter Base Page URL.
remove_filter('civicrm/core/url/base', [$this, 'base_url_filter'], 10);
// Now handle remaining languages.
foreach ($languages as &$language) {
if ($language['active']) {
continue;
}
// WPML will return the "naked" URL to the translated Base Page.
$language_url = $sitepress->convert_url($canonical, $language['language_code']);
/**
* A custom callback that returns the translated Base Page URL.
*
* @since 5.75
*
* @param str $url The URL as built by CiviCRM.
* @param bool $admin_request True if building an admin URL, false otherwise.
* @return str $url The URL as modified by WPML.
*/
$closure = function($url, $admin_request) use (&$language) {
return $language['url'];
};
// Use the unmodified canonical URL.
add_filter('civicrm/core/url/base', $closure, 10, 2);
$canonical = htmlspecialchars_decode(civi_wp()->basepage->basepage_canonical_url($language_url));
remove_filter('civicrm/core/url/base', $closure, 10);
$language['url'] = $canonical;
}
// Reinstate Base Page URL filter.
add_filter('civicrm/core/url/base', [$this, 'base_url_filter'], 10, 2);
return $languages;
}
/**
* Checks WPML for CiviCRM Base Page matches.
*
* @since 5.75
*
* @param bool $is_basepage TRUE if the Post ID matches the Base Page ID, FALSE otherwise.
* @param int $post_id The WordPress Post ID to check.
* @return bool $is_basepage TRUE if the Post ID matches the Base Page ID, FALSE otherwise.
*/
public function basepage_match($is_basepage, $post_id) {
// Bail if this is already the Base Page.
if ($is_basepage) {
return $is_basepage;
}
// Bail if there are no rewrites.
if (empty($this->rewrites)) {
return $is_basepage;
}
// Check rewrites to see if we have a match with this Post ID.
if (isset($this->rewrites[$post_id]) && !empty($this->rewrites[$post_id])) {
$is_basepage = TRUE;
}
return $is_basepage;
}
/**
* Filters the CiviCRM Base URL for the current language reported by WPML.
*
* Only filters URLs that point to the front-end, since WordPress admin URLs are not
* rewritten by WPML.
*
* @since 5.75
*
* @param str $url The URL as built by CiviCRM.
* @param bool $admin_request True if building an admin URL, false otherwise.
* @return str $url The URL as modified by WPML.
*/
public function base_url_filter($url, $admin_request) {
global $sitepress, $wpml_url_filters;
// Skip when not defined.
if (empty($url) || $admin_request) {
return $url;
}
// Determine the language code.
$code = apply_filters('wpml_current_language', NULL);
if (empty($code)) {
return $url;
}
if ($code === 'all') {
$code = apply_filters('wpml_default_language', NULL);
}
// Get the converted URL.
$language_url = $sitepress->convert_url($url, $code);
// Let's use that if there are no rewrites.
if (empty($this->rewrites)) {
return $language_url;
}
// Let's find the Base Page in the appropriate language.
$parsed_url = wp_parse_url($language_url, PHP_URL_PATH);
$regex_path = trailingslashit(substr($parsed_url, 1));
foreach (array_reverse($this->rewrites, TRUE) as $page_id => $rewrite) {
if (in_array($regex_path, $rewrite)) {
$post_id = $page_id;
break;
}
}
// Bail if we don't find the post.
if (empty($post_id)) {
return $language_url;
}
// Get the Base Page permalink in the right language.
$language_url = get_permalink($post_id);
return $language_url;
}
/**
* Filters the CiviCRM locale for the current language as set by WPML.
*
* @since 5.66
*
* @param str $locale The locale as reported by WordPress.
* @return str $locale The locale as modified by Polylang.
*/
public function locale_filter($locale) {
$languages = apply_filters('wpml_active_languages', NULL);
foreach ($languages as $language) {
if ($language['active']) {
$locale = $language['default_locale'];
break;
}
}
return $locale;
}
}