PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.1
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.1
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 / locale / class-charitable-locale.php

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

199 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Add support for switching between locales.
4 *
5 * @package Charitable/Classes/Charitable_Locale
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.43
10 * @version 1.6.57
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Locale' ) ) :
19
20 /**
21 * Charitable_Locale
22 *
23 * @since 1.6.43
24 */
25 class Charitable_Locale {
26
27 /**
28 * The current locale.
29 *
30 * @since 1.6.43
31 *
32 * @var string
33 */
34 private $locale;
35
36 /**
37 * The locale to use for the email being sent.
38 *
39 * @since 1.6.43
40 *
41 * @var string
42 */
43 private $email_locale;
44
45 /**
46 * Create class object.
47 *
48 * @since 1.6.43
49 */
50 public function __construct() {
51 /**
52 * Boolean flag to decide whether to enable the locale functions.
53 *
54 * @since 1.6.43
55 *
56 * @param boolean $locale_enabled Whether to enable locale functions.
57 */
58 if ( ! apply_filters( 'charitable_enable_locale_functions', false ) ) {
59 return;
60 }
61
62 $this->locale = get_locale();
63
64 /**
65 * Add locale as donation and campaign fields.
66 */
67 add_filter( 'charitable_default_donation_fields', array( $this, 'add_locale_donation_field' ) );
68 add_filter( 'charitable_default_campaign_fields', array( $this, 'add_locale_campaign_field' ) );
69
70 /**
71 * Store current locale when a donation is made.
72 */
73 add_filter( 'charitable_donation_meta', array( $this, 'save_donation_locale' ) );
74
75 /**
76 * Before sending an email, start filtering the locale based
77 * on the stored locale for the campaign or donation.
78 */
79 add_action( 'charitable_before_send_email', array( $this, 'maybe_set_email_locale' ) );
80 }
81
82 /**
83 * Add locale donation field.
84 *
85 * @since 1.6.43
86 *
87 * @param array $fields The default registered fields.
88 * @return array
89 */
90 public function add_locale_donation_field( $fields ) {
91 $fields['locale'] = array(
92 'label' => __( 'Donor Locale', 'charitable' ),
93 'data_type' => 'meta',
94 'donation_form' => false,
95 'admin_form' => false,
96 'show_in_meta' => true,
97 'show_in_export' => true,
98 'email_tag' => false,
99 );
100
101 return $fields;
102 }
103
104 /**
105 * Add locale campaign field.
106 *
107 * @since 1.6.43
108 *
109 * @param array $fields The default registered fields.
110 * @return array
111 */
112 public function add_locale_campaign_field( $fields ) {
113 $fields['locale'] = array(
114 'label' => __( 'Campaign Creator Locale', 'charitable' ),
115 'data_type' => 'meta',
116 'campaign_form' => false,
117 'admin_form' => false,
118 'show_in_export' => true,
119 'email_tag' => false,
120 );
121
122 return $fields;
123 }
124
125 /**
126 * Save the locale that a donor was using when they donated.
127 *
128 * @since 1.6.43
129 *
130 * @param array $meta_fields The meta fields to be saved.
131 * @return array
132 */
133 public function save_donation_locale( $meta_fields ) {
134 /**
135 * Filter the locale. By default this will use the result of get_locale().
136 *
137 * @see get_locale
138 *
139 * @since 1.6.45
140 *
141 * @param string $locale The locale.
142 */
143 $meta_fields['locale'] = apply_filters( 'charitable_donation_locale', get_locale() );
144
145 return $meta_fields;
146 }
147
148 /**
149 * Maybe set a locale for the email.
150 *
151 * @since 1.6.43
152 *
153 * @param Charitable_Email $email The email object.
154 * @return void
155 */
156 public function maybe_set_email_locale( Charitable_Email $email ) {
157 $donation = $email->get( 'donation' );
158 $campaign = $email->get( 'campaign' );
159
160 if ( is_a( $donation, 'Charitable_Donation' ) ) {
161 $this->email_locale = $donation->get( 'locale' );
162 } elseif ( is_a( $campaign, 'Charitable_Campaign' ) ) {
163 $this->email_locale = $campaign->get( 'locale' );
164 }
165
166 if ( ! empty( $this->email_locale ) ) {
167 switch_to_locale( $this->email_locale );
168
169 add_filter( 'locale', array( $this, 'set_email_locale' ) );
170 add_action( 'charitable_after_send_email', array( $this, 'turn_off_email_locale_filter' ) );
171 }
172 }
173
174 /**
175 * Set the locale to use for an email.
176 *
177 * @since 1.6.43
178 *
179 * @return string
180 */
181 public function set_email_locale() {
182 return $this->email_locale;
183 }
184
185 /**
186 * Turn off the email locale filter.
187 *
188 * @since 1.6.43
189 *
190 * @return void
191 */
192 public function turn_off_email_locale_filter() {
193 remove_filter( 'locale', array( $this, 'set_email_locale' ) );
194 restore_previous_locale();
195 }
196 }
197
198 endif;
199