PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.11
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.11
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / compat / class-charitable-wpml-compat.php

class-charitable-wpml-compat.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.11, at includes/compat/class-charitable-wpml-compat.php

466 lines 13.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A class to resolve compatibility issues with WPML.
4 *
5 * @package Charitable/Classes/Charitable_WPML_Compat
6 * @author David Bisset
7 * @copyright Copyright (c) 2022, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.6.44
10 * @version 1.6.44
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_WPML_Compat' ) ) :
19
20 /**
21 * Charitable_WPML_Compat
22 *
23 * @since 1.6.44
24 */
25 class Charitable_WPML_Compat {
26
27 /**
28 * The global sitepress object.
29 *
30 * @since 1.6.44
31 *
32 * @var SitePress
33 */
34 private $sitepress;
35
36 /**
37 * Create class object.
38 *
39 * @since 1.6.44
40 */
41 public function __construct() {
42 global $sitepress;
43
44 $this->sitepress = $sitepress;
45
46 /* Filter the amount donated to include any translations of the campaign. */
47 add_filter( 'charitable_campaign_donated_amount', array( $this, 'get_donated_amount' ), 5, 3 );
48
49 /* Filter the donor count to include any translations of the campaign. */
50 add_filter( 'charitable_campaign_donor_count', array( $this, 'get_donor_count' ), 5, 2 );
51
52 /* Handle WPML translation of user dashboard menu */
53 add_action( 'wp_update_nav_menu', array( $this, 'flush_menu_object_cache' ) );
54 add_action( 'wp_update_nav_menu_item', array( $this, 'flush_menu_object_cache' ) );
55 add_filter( 'transient_charitable_user_dashboard_objects', array( $this, 'get_user_dashboard_objects' ) );
56 add_filter( 'pre_set_transient_charitable_user_dashboard_objects', array( $this, 'set_user_dashboard_objects' ) );
57
58 /* Profile Page */
59 add_filter( 'charitable_permalink_profile_page', array( $this, 'get_profile_page_url' ), 10, 2 );
60 add_filter( 'charitable_is_page_profile_page', array( $this, 'is_profile_page' ), 10, 2 );
61
62 /* Login Page */
63 add_filter( 'charitable_permalink_login_page', array( $this, 'get_login_page_url' ), 10, 2 );
64 add_filter( 'charitable_is_page_login_page', array( $this, 'is_login_page' ), 10, 2 );
65
66 /* Registration Page */
67 add_filter( 'charitable_permalink_registration_page', array( $this, 'get_registration_page_url' ), 10, 2 );
68 add_filter( 'charitable_is_page_registration_page', array( $this, 'is_registration_page' ), 10, 2 );
69
70 /* Donation Receipt Page */
71 add_filter( 'charitable_permalink_donation_receipt_page', array( $this, 'get_donation_receipt_page_url' ), 10, 2 );
72 add_filter( 'charitable_is_page_donation_receipt_page', array( $this, 'is_donation_receipt_page' ), 10, 2 );
73
74 /* Terms & Conditions and Privacy Policy */
75 add_filter( 'charitable_option_terms_conditions_page', array( $this, 'get_wpml_page_id' ) );
76 add_filter( 'charitable_option_privacy_policy_page', array( $this, 'get_wpml_page_id' ) );
77
78 /* Add language code to AJAX request. */
79 add_filter( 'charitable_javascript_vars', array( $this, 'add_language_code_to_ajaxurl' ) );
80
81 /**
82 * Do something with this class instance.
83 *
84 * @since 1.6.44
85 *
86 * @param Charitable_WPML_Compat $helper WPML compatibility class instance.
87 */
88 do_action( 'charitable_wpml_compat', $this );
89 }
90
91 /**
92 * Return a list of campaign IDs that are translations of the
93 * given campaign.
94 *
95 * @since 1.6.44
96 *
97 * @param Charitable_Campaign $campaign
98 * @return array
99 */
100 public function get_campaign_translations( Charitable_Campaign $campaign ) {
101 /* Get the WPML translation ID. */
102 $trid = $this->sitepress->get_element_trid( $campaign->ID, 'post_campaign' );
103
104 return array_values(
105 wp_list_pluck(
106 $this->sitepress->get_element_translations( $trid, 'post_campaign' ),
107 'element_id'
108 )
109 );
110 }
111
112 /**
113 * Filter the amount donated to a campaign to include any translations of the same campaign.
114 *
115 * @since 1.6.44
116 *
117 * @param string $amount The default amount donated.
118 * @param Charitable_Campaign $campaign The campaign to get the donated amount for.
119 * @param boolean $sanitize Whether to sanitize the amount. False by default.
120 * @return string
121 */
122 public function get_donated_amount( $amount, Charitable_Campaign $campaign, $sanitize = false ) {
123 $campaigns = $this->get_campaign_translations( $campaign );
124
125 if ( empty( $campaigns ) ) {
126 return $amount;
127 }
128
129 $amount = charitable_get_table( 'campaign_donations' )->get_campaign_donated_amount( $campaigns );
130
131 if ( $sanitize ) {
132 $amount = charitable_sanitize_amount( $amount );
133 }
134
135 return $amount;
136 }
137
138 /**
139 * Return the number of donors, including any donations to campaign locale variations.
140 *
141 * @since 1.6.44
142 *
143 * @param int $count The default number of donors.
144 * @param Charitable_Campaign $campaign The campaign object.
145 * @return int
146 */
147 public function get_donor_count( $count, Charitable_Campaign $campaign ) {
148 $campaigns = $this->get_campaign_translations( $campaign );
149
150 if ( empty( $campaigns ) ) {
151 return $count;
152 }
153
154 return charitable_get_table( 'campaign_donations' )->count_campaign_donors( $campaigns );
155 }
156
157 /**
158 * Returns the user dashboard menus, including any WPML
159 * translation versions.
160 *
161 * @since 1.6.44
162 *
163 * @return array
164 */
165 public function get_user_dashboard_menus() {
166 /* Get all nav menu locations. */
167 $locations = get_nav_menu_locations();
168
169 if ( ! array_key_exists( 'charitable-dashboard', $locations ) ) {
170 return $dashboard_menus;
171 }
172
173 /* Get the WPML translation ID for the menu. */
174 $trid = $this->sitepress->get_element_trid( $locations['charitable-dashboard'], 'tax_nav_menu' );
175
176 foreach ( $this->sitepress->get_element_translations( $trid, 'tax_nav_menu' ) as $menu_translation ) {
177 $dashboard_menus[] = wp_get_nav_menu_object( $menu_translation->element_id );
178 }
179
180 return $dashboard_menus;
181 }
182
183 /**
184 * Flush the object cache when a menu assigned to the user dashboard
185 * location (in any language) is edited.
186 *
187 * @since 1.6.44
188 *
189 * @param int $menu_id The menu id.
190 * @return void
191 */
192 public function flush_menu_object_cache( $menu_id ) {
193 $dashboard_menus = $this->get_user_dashboard_menus();
194
195 if ( empty( $dashboard_menus ) ) {
196 delete_transient( 'charitable_user_dashboard_objects' );
197 }
198
199 if ( in_array( $menu_id, wp_list_pluck( $dashboard_menus, 'term_id' ) ) ) {
200 delete_transient( 'charitable_user_dashboard_objects' );
201 }
202 }
203
204 /**
205 * Get the user dashboard objects for the current language.
206 *
207 * @since 1.6.44
208 *
209 * @return array
210 */
211 public function get_user_dashboard_objects( $objects ) {
212 $language = $this->sitepress->get_current_language();
213
214 if ( false === $objects ) {
215 return false;
216 }
217
218 if ( ! array_key_exists( $language, $objects ) ) {
219 return false;
220 }
221
222 return $objects[ $language ];
223 }
224
225 /**
226 * When saving the user dashboard objects, include any objects in WPML
227 * locations of the user dashboard menu.
228 *
229 * @since 1.6.44
230 *
231 * @return array
232 */
233 public function set_user_dashboard_objects( $menu_items ) {
234 $language = $this->sitepress->get_current_language();
235
236 /* Temporarily switch off our filter. */
237 remove_filter( 'transient_charitable_user_dashboard_objects', array( $this, 'get_user_dashboard_objects' ) );
238
239 $all_menu_items = get_transient( 'charitable_user_dashboard_objects' );
240
241 if ( ! is_array( $all_menu_items ) ) {
242 $all_menu_items = array();
243 }
244
245 if ( ! array_key_exists( $language, $all_menu_items ) ) {
246 $all_menu_items[ $language ] = $menu_items;
247 }
248
249 /* Temporarily switch off our filter. */
250 add_filter( 'transient_charitable_user_dashboard_objects', array( $this, 'get_user_dashboard_objects' ) );
251
252 return $all_menu_items;
253 }
254
255 /**
256 * Get the current language version of the specified endpoint,
257 * or return the default.
258 *
259 * @since 1.6.44
260 *
261 * @param string $endpoint_id The endpoint ID.
262 * @param string $default The endpoint's URL.
263 * @param array $args Mixed set of arguments.
264 * @return string
265 */
266 public function get_wpml_page_url( $page_option, $default, $args ) {
267 if ( empty( $default ) ) {
268 return $default;
269 }
270
271 /* Prevent WPML override. */
272 if ( array_key_exists( 'wpml_override', $args ) && ! $args['wpml_override'] ) {
273 return $default;
274 }
275
276 $page_id = charitable_get_option( $page_option, false );
277
278 if ( in_array( $page_id, array( 'wp', 'auto' ) ) ) {
279 return $default;
280 }
281
282 return get_permalink( wpml_object_id_filter( $page_id, 'page' ) );
283 }
284
285 /**
286 * Checks whether this is the specified page.
287 *
288 * @since 1.6.44
289 *
290 * @param string $page_option The option key used to record the page ID.
291 * @param boolean $is_page Whether we are currently on the page.
292 * @param array $args Mixed arguments.
293 * @return string
294 */
295 public function is_wpml_page( $page_option, $is_page, $args ) {
296 /* We've already determined it's the current page. */
297 if ( $is_page ) {
298 return $is_page;
299 }
300
301 /* Prevent WPML override. */
302 if ( array_key_exists( 'wpml_override', $args ) && ! $args['wpml_override'] ) {
303 return $is_page;
304 }
305
306 global $post, $wp_query;
307
308 if ( is_null( $post ) || ! $wp_query->is_main_query() ) {
309 return $is_page;
310 }
311
312 $page_id = charitable_get_option( $page_option, false );
313
314 if ( in_array( $page_id, array( 'wp', 'auto' ) ) ) {
315 return $is_page;
316 }
317
318 $wpml_page = wpml_object_id_filter( $page_id );
319
320 return $wpml_page && $wpml_page === $post->ID;
321 }
322
323 /**
324 * Get the profile page URL.
325 *
326 * @since 1.6.44
327 *
328 * @param string $default The endpoint's URL.
329 * @param array $args Mixed set of arguments.
330 * @return string
331 */
332 public function get_profile_page_url( $default, $args ) {
333 return $this->get_wpml_page_url( 'profile_page', $default, $args );
334 }
335
336 /**
337 * Check whether we are currently on the profile page.
338 *
339 * @since 1.6.44
340 *
341 * @param boolean $is_page Whether we are currently on the profile page.
342 * @param array $args Mixed arguments.
343 * @return boolean
344 */
345 public function is_profile_page( $is_page, $args ) {
346 return $this->is_wpml_page( 'profile_page', $is_page, $args );
347 }
348
349 /**
350 * Get the login page URL.
351 *
352 * @since 1.6.44
353 *
354 * @param string $default The endpoint's URL.
355 * @param array $args Mixed set of arguments.
356 * @return string
357 */
358 public function get_login_page_url( $default, $args ) {
359 return $this->get_wpml_page_url( 'login_page', $default, $args );
360 }
361
362 /**
363 * Check whether we are currently on the login page.
364 *
365 * @since 1.6.44
366 *
367 * @param boolean $is_page Whether we are currently on the login page.
368 * @param array $args Mixed arguments.
369 * @return boolean
370 */
371 public function is_login_page( $is_page, $args ) {
372 return $this->is_wpml_page( 'login_page', $is_page, $args );
373 }
374
375 /**
376 * Get the registration page URL.
377 *
378 * @since 1.6.44
379 *
380 * @param string $default The endpoint's URL.
381 * @param array $args Mixed set of arguments.
382 * @return string
383 */
384 public function get_registration_page_url( $default, $args ) {
385 return $this->get_wpml_page_url( 'registration_page', $default, $args );
386 }
387
388 /**
389 * Check whether we are currently on the registration page.
390 *
391 * @since 1.6.44
392 *
393 * @param boolean $is_page Whether we are currently on the registration page.
394 * @param array $args Mixed arguments.
395 * @return boolean
396 */
397 public function is_registration_page( $is_page, $args ) {
398 return $this->is_wpml_page( 'registration_page', $is_page, $args );
399 }
400
401 /**
402 * Get the donation receipt page URL.
403 *
404 * @since 1.6.44
405 *
406 * @param string $default The endpoint's URL.
407 * @param array $args Mixed set of arguments.
408 * @return string
409 */
410 public function get_donation_receipt_page_url( $default, $args ) {
411 $wpml_url = $this->get_wpml_page_url( 'donation_receipt_page', $default, $args );
412
413 if ( $wpml_url !== $default ) {
414 $donation_id = isset( $args['donation_id'] ) ? $args['donation_id'] : get_the_ID();
415 $wpml_url = esc_url_raw( add_query_arg( array( 'donation_id' => $donation_id ), $wpml_url ) );
416 }
417
418 return $wpml_url;
419 }
420
421 /**
422 * Check whether we are currently on the donation receipt page.
423 *
424 * @since 1.6.44
425 *
426 * @param boolean $is_page Whether we are currently on the donation_receipt page.
427 * @param array $args Mixed arguments.
428 * @return boolean
429 */
430 public function is_donation_receipt_page( $is_page, $args ) {
431 return $this->is_wpml_page( 'donation_receipt_page', $is_page, $args );
432 }
433
434 /**
435 * Get the page ID of a specific page, returning the WPML
436 * translation if applicable.
437 *
438 * @since 1.6.44
439 *
440 * @param int|string $page_id The set page id.
441 * @return int|string
442 */
443 public function get_wpml_page_id( $page_id ) {
444 if ( empty( $page_id ) || 0 == $page_id ) {
445 return $page_id;
446 }
447
448 return wpml_object_id_filter( $page_id, 'page' );
449 }
450
451 /**
452 * Add the current language code to the ajax url.
453 *
454 * @since 1.6.44
455 *
456 * @param array $vars Javascript vars.
457 * @return array
458 */
459 public function add_language_code_to_ajaxurl( $vars ) {
460 $vars['ajaxurl'] = add_query_arg( 'lang', $this->sitepress->get_current_language(), $vars['ajaxurl'] );
461 return $vars;
462 }
463 }
464
465 endif;
466