| 1 |
<?php |
| 2 |
/** |
| 3 |
* A class to resolve compatibility issues with Weglot. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Weglot_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.45 |
| 10 |
* @version 1.6.47 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Weglot_Compat' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Weglot_Compat |
| 22 |
* |
| 23 |
* @since 1.6.45 |
| 24 |
*/ |
| 25 |
class Charitable_Weglot_Compat { |
| 26 |
|
| 27 |
/** |
| 28 |
* Create class object. |
| 29 |
* |
| 30 |
* @since 1.6.45 |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
/* Donation locale & email */ |
| 34 |
add_filter( 'charitable_donation_locale', 'weglot_get_current_language' ); |
| 35 |
add_action( 'charitable_before_send_email', array( $this, 'maybe_set_email_locale' ) ); |
| 36 |
|
| 37 |
/* Profile Page */ |
| 38 |
add_filter( 'charitable_permalink_profile_page', array( $this, 'get_weglot_page_url' ), 10, 2 ); |
| 39 |
add_filter( 'charitable_is_page_profile_page', array( $this, 'is_profile_page' ), 10, 2 ); |
| 40 |
|
| 41 |
/* Donation Page */ |
| 42 |
add_filter( 'charitable_permalink_campaign_donation_page', array( $this, 'get_weglot_page_url' ), 10, 2 ); |
| 43 |
add_filter( 'charitable_is_page_campaign_donation_page', array( $this, 'is_campaign_donation_page' ), 10, 2 ); |
| 44 |
|
| 45 |
/* Login Page */ |
| 46 |
add_filter( 'charitable_permalink_login_page', array( $this, 'get_weglot_page_url' ), 10, 2 ); |
| 47 |
add_filter( 'charitable_is_page_login_page', array( $this, 'is_login_page' ), 10, 2 ); |
| 48 |
|
| 49 |
/* Registration Page */ |
| 50 |
add_filter( 'charitable_permalink_registration_page', array( $this, 'get_weglot_page_url' ), 10, 2 ); |
| 51 |
add_filter( 'charitable_is_page_registration_page', array( $this, 'is_registration_page' ), 10, 2 ); |
| 52 |
|
| 53 |
/* Donation Receipt Page */ |
| 54 |
add_filter( 'charitable_permalink_donation_receipt_page', array( $this, 'get_weglot_page_url' ), 10, 2 ); |
| 55 |
add_filter( 'charitable_is_page_donation_receipt_page', array( $this, 'is_donation_receipt_page' ), 10, 2 ); |
| 56 |
|
| 57 |
/** |
| 58 |
* Do something with this class instance. |
| 59 |
* |
| 60 |
* @since 1.6.45 |
| 61 |
* |
| 62 |
* @param Charitable_Weglot_Compat $helper Weglot compatibility class instance. |
| 63 |
*/ |
| 64 |
do_action( 'charitable_weglot_compat', $this ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Maybe set a locale for the email. |
| 69 |
* |
| 70 |
* @since 1.6.45 |
| 71 |
* |
| 72 |
* @param Charitable_Email $email The email object. |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
public function maybe_set_email_locale( Charitable_Email $email ) { |
| 76 |
$donation = $email->get( 'donation' ); |
| 77 |
$campaign = $email->get( 'campaign' ); |
| 78 |
|
| 79 |
if ( is_a( $donation, 'Charitable_Donation' ) ) { |
| 80 |
$this->email_locale = $donation->get( 'locale' ); |
| 81 |
} elseif ( is_a( $campaign, 'Charitable_Campaign' ) ) { |
| 82 |
$this->email_locale = $campaign->get( 'locale' ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( ! empty( $this->email_locale ) ) { |
| 86 |
add_filter( 'weglot_translate_current_language', array( $this, 'set_email_locale' ) ); |
| 87 |
add_action( 'charitable_after_send_email', array( $this, 'turn_off_email_locale_filter' ) ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Set the locale to use for an email. |
| 93 |
* |
| 94 |
* @since 1.6.45 |
| 95 |
* |
| 96 |
* @return string |
| 97 |
*/ |
| 98 |
public function set_email_locale() { |
| 99 |
return $this->email_locale; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Turn off the email locale filter. |
| 104 |
* |
| 105 |
* @since 1.6.45 |
| 106 |
* |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public function turn_off_email_locale_filter() { |
| 110 |
remove_filter( 'weglot_translate_current_language', array( $this, 'set_email_locale' ) ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get the current language version of the specified endpoint, |
| 115 |
* or return the default. |
| 116 |
* |
| 117 |
* @since 1.6.45 |
| 118 |
* |
| 119 |
* @param string $default The endpoint's URL. |
| 120 |
* @param array $args Mixed set of arguments. |
| 121 |
* @return string |
| 122 |
*/ |
| 123 |
public function get_weglot_page_url( $default, $args ) { |
| 124 |
if ( empty( $default ) ) { |
| 125 |
return $default; |
| 126 |
} |
| 127 |
|
| 128 |
/* Prevent Weglot override. */ |
| 129 |
if ( array_key_exists( 'weglot_override', $args ) && ! $args['weglot_override'] ) { |
| 130 |
return $default; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Backwards compatibility for Weglot versions prior to 3.3.0. |
| 135 |
* |
| 136 |
* @see https://github.com/Charitable/Charitable/issues/861 |
| 137 |
*/ |
| 138 |
if ( version_compare( WEGLOT_VERSION, '3.3.0', '<' ) ) { |
| 139 |
$language = weglot_get_current_language(); |
| 140 |
} else { |
| 141 |
$language = weglot_get_service( 'Language_Service_Weglot' )->get_language_from_internal( weglot_get_original_language() ); |
| 142 |
} |
| 143 |
|
| 144 |
return weglot_create_url_object( $default )->getForLanguage( $language ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Checks whether this is the specified page. |
| 149 |
* |
| 150 |
* @since 1.6.45 |
| 151 |
* |
| 152 |
* @param string $endpoint The endpoint. |
| 153 |
* @param boolean $is_page Whether we are currently on the page. |
| 154 |
* @param array $args Mixed arguments. |
| 155 |
* @return string |
| 156 |
*/ |
| 157 |
public function is_weglot_page( $endpoint, $is_page, $args ) { |
| 158 |
/* We've already determined it's the current page. */ |
| 159 |
if ( $is_page ) { |
| 160 |
return $is_page; |
| 161 |
} |
| 162 |
|
| 163 |
/* Prevent Weglot override. */ |
| 164 |
if ( array_key_exists( 'weglot_override', $args ) && ! $args['weglot_override'] ) { |
| 165 |
return $is_page; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Backwards compatibility for Weglot versions prior to 3.3.0. |
| 170 |
* |
| 171 |
* @see https://github.com/Charitable/Charitable/issues/861 |
| 172 |
*/ |
| 173 |
if ( version_compare( WEGLOT_VERSION, '3.3.0', '<' ) ) { |
| 174 |
$language = weglot_get_original_language(); |
| 175 |
} else { |
| 176 |
$language = weglot_get_service( 'Language_Service_Weglot' )->get_language_from_internal( weglot_get_original_language() ); |
| 177 |
} |
| 178 |
|
| 179 |
return charitable_get_permalink( $endpoint, $args ) === weglot_create_url_object( weglot_get_current_full_url() )->getForLanguage( $language ); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Check whether we are currently on the campaign donation page. |
| 184 |
* |
| 185 |
* @since 1.6.45 |
| 186 |
* |
| 187 |
* @param boolean $is_page Whether we are currently on the campaign donation page. |
| 188 |
* @param array $args Mixed arguments. |
| 189 |
* @return boolean |
| 190 |
*/ |
| 191 |
public function is_campaign_donation_page( $is_page, $args ) { |
| 192 |
return $this->is_weglot_page( 'campaign_donation_page', $is_page, $args ); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Check whether we are currently on the profile page. |
| 197 |
* |
| 198 |
* @since 1.6.45 |
| 199 |
* |
| 200 |
* @param boolean $is_page Whether we are currently on the profile page. |
| 201 |
* @param array $args Mixed arguments. |
| 202 |
* @return boolean |
| 203 |
*/ |
| 204 |
public function is_profile_page( $is_page, $args ) { |
| 205 |
return $this->is_weglot_page( 'profile_page', $is_page, $args ); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Check whether we are currently on the login page. |
| 210 |
* |
| 211 |
* @since 1.6.45 |
| 212 |
* |
| 213 |
* @param boolean $is_page Whether we are currently on the login page. |
| 214 |
* @param array $args Mixed arguments. |
| 215 |
* @return boolean |
| 216 |
*/ |
| 217 |
public function is_login_page( $is_page, $args ) { |
| 218 |
return $this->is_weglot_page( 'login_page', $is_page, $args ); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Check whether we are currently on the registration page. |
| 223 |
* |
| 224 |
* @since 1.6.45 |
| 225 |
* |
| 226 |
* @param boolean $is_page Whether we are currently on the registration page. |
| 227 |
* @param array $args Mixed arguments. |
| 228 |
* @return boolean |
| 229 |
*/ |
| 230 |
public function is_registration_page( $is_page, $args ) { |
| 231 |
return $this->is_weglot_page( 'registration_page', $is_page, $args ); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Check whether we are currently on the donation receipt page. |
| 236 |
* |
| 237 |
* @since 1.6.45 |
| 238 |
* |
| 239 |
* @param boolean $is_page Whether we are currently on the donation_receipt page. |
| 240 |
* @param array $args Mixed arguments. |
| 241 |
* @return boolean |
| 242 |
*/ |
| 243 |
public function is_donation_receipt_page( $is_page, $args ) { |
| 244 |
return $this->is_weglot_page( 'donation_receipt_page', $is_page, $args ); |
| 245 |
} |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
endif; |
| 250 |
|