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 / public / class-charitable-public.php

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

462 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable Public class.
4 *
5 * @package Charitable/Classes/Charitable_Public
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.0.0
10 * @version 1.6.60
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Public' ) ) :
19
20 /**
21 * Charitable Public class.
22 *
23 * @since 1.0.0
24 */
25 final class Charitable_Public {
26
27 /**
28 * The single instance of this class.
29 *
30 * @since 1.2.0
31 *
32 * @var Charitable_Public|null
33 */
34 private static $instance = null;
35
36 /**
37 * Returns and/or create the single instance of this class.
38 *
39 * @since 1.2.0
40 *
41 * @return Charitable_Public
42 */
43 public static function get_instance() {
44 if ( is_null( self::$instance ) ) {
45 self::$instance = new self();
46 }
47
48 return self::$instance;
49 }
50
51 /**
52 * Set up the class.
53 *
54 * @since 1.0.0
55 */
56 private function __construct() {
57 add_action( 'wp_enqueue_scripts', array( $this, 'setup_scripts' ) );
58 add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_donation_form_scripts' ), 11 );
59 add_action( 'charitable_campaign_loop_before', array( $this, 'maybe_enqueue_donation_form_scripts' ) );
60 add_filter( 'post_class', array( $this, 'campaign_post_class' ) );
61
62 do_action( 'charitable_public_start', $this );
63 }
64
65 /**
66 * Loads public facing scripts and stylesheets.
67 *
68 * @since 1.0.0
69 *
70 * @return void
71 */
72 public function setup_scripts() {
73 if ( ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ) {
74 $suffix = '';
75 $version = '';
76 } else {
77 $suffix = '.min';
78 $version = charitable()->get_version();
79 }
80
81 $assets_dir = charitable()->get_path( 'assets', false );
82
83 /* Main Charitable script. */
84 $currency = charitable_get_currency_helper();
85 $minimum = charitable_get_minimum_donation_amount( charitable_get_current_campaign_id() );
86
87 if ( $minimum > 0 ) {
88 $amount_msg = sprintf(
89 /* Translators: %s: minimum donatino amount */
90 __( 'You must donate at least %s.', 'charitable' ),
91 $currency->get_monetary_amount( $minimum, false, true )
92 );
93 } else {
94 $amount_msg = sprintf(
95 /* Translators: %s: minimum donatino amount */
96 __( 'You must donate more than %s.', 'charitable' ),
97 $currency->get_monetary_amount( $minimum, false, true )
98 );
99 }
100
101 $maximum = charitable_get_maximum_donation_amount( charitable_get_current_campaign_id() );
102 $max_exceeded_msg = $maximum
103 ? sprintf(
104 /* Translators: %s: maximum donation amount */
105 __( 'The maximum donation amount permitted is %s.', 'charitable' ),
106 $currency->get_monetary_amount( $maximum, false, true )
107 )
108 : '';
109
110 /**
111 * Return whether $0 donations are permitted.
112 *
113 * Note that this filter is also called in Charitable_Donation_Form::validate_amount().
114 *
115 * @since 1.2.0
116 *
117 * @param boolean $permitted Whether $0 donations are permitted.
118 */
119 $permit_0_donation = apply_filters( 'charitable_permit_0_donation', false );
120
121 /**
122 * Filter the Javascript vars array.
123 *
124 * @since 1.0.0
125 *
126 * @param array $vars The set of vars.
127 */
128 $vars = apply_filters(
129 'charitable_javascript_vars',
130 array(
131 'ajaxurl' => admin_url( 'admin-ajax.php' ),
132 'loading_gif' => $assets_dir . 'images/charitable-loading.gif',
133 'country' => charitable_get_option( 'country' ),
134 'currency' => charitable_get_currency(),
135 'currency_symbol' => $currency->get_currency_symbol(),
136 'currency_format_num_decimals' => esc_attr( $currency->get_decimals() ),
137 'currency_format_decimal_sep' => esc_attr( $currency->get_decimal_separator() ),
138 'currency_format_thousand_sep' => esc_attr( $currency->get_thousands_separator() ),
139 'currency_format' => esc_attr( $currency->get_accounting_js_format() ), // For accounting.js.
140 'minimum_donation' => $minimum,
141 'maximum_donation' => $maximum,
142 'permit_0_donation' => $permit_0_donation,
143 'error_invalid_amount' => $amount_msg,
144 'error_max_exceeded' => $max_exceeded_msg,
145 'error_required_fields' => __( 'Please fill out all required fields.', 'charitable' ),
146 'error_unknown' => __( 'Your donation could not be processed. Please reload the page and try again.', 'charitable' ),
147 'error_invalid_cc_number' => __( 'The credit card passed is not valid.', 'charitable' ),
148 'error_invalid_cc_expiry' => __( 'The credit card expiry date is not valid.', 'charitable' ),
149 'version' => charitable()->get_version(),
150 'test_mode' => (int) charitable_get_option( 'test_mode' ),
151 'error_notice_donation_display' => esc_attr( charitable_get_option( 'donation_form_notices_display' ) ),
152 )
153 );
154
155 /* Accounting.js */
156 wp_register_script(
157 'accounting',
158 $assets_dir . 'js/libraries/accounting' . $suffix . '.js',
159 array( 'jquery' ),
160 $version,
161 true
162 );
163
164 $deps = array( 'accounting', 'jquery' );
165
166 if ( wp_script_is( 'charitable-sessions', 'enqueued' ) ) {
167 array_unshift( $deps, 'charitable-sessions' );
168 }
169
170 wp_register_script(
171 'charitable-script',
172 $assets_dir . 'js/charitable' . $suffix . '.js',
173 $deps,
174 $version,
175 true
176 );
177
178 wp_localize_script(
179 'charitable-script',
180 'CHARITABLE_VARS',
181 $vars
182 );
183
184 /* Credit card validation */
185 wp_register_script(
186 'charitable-credit-card',
187 $assets_dir . 'js/charitable-credit-card' . $suffix . '.js',
188 array( 'charitable-script' ),
189 $version,
190 true
191 );
192
193 /* URL sanitizer. */
194 wp_register_script(
195 'charitable-url-sanitizer',
196 $assets_dir . 'js/charitable-url-sanitizer' . $suffix . '.js',
197 array( 'jquery' ),
198 $version,
199 true
200 );
201
202 /* General forms scripts. */
203 wp_register_script(
204 'charitable-forms',
205 $assets_dir . 'js/charitable-forms' . $suffix . '.js',
206 array( 'jquery' ),
207 $version,
208 true
209 );
210
211 /* Main styles */
212 wp_register_style(
213 'charitable-styles',
214 $assets_dir . 'css/charitable' . $suffix . '.css',
215 array(),
216 $version
217 );
218
219 wp_enqueue_style( 'charitable-styles' );
220
221 /* Lean Modal is registered but NOT enqueued yet. */
222 if ( 'modal' == charitable_get_option( 'donation_form_display', 'separate_page' ) ) {
223
224 wp_register_script(
225 'lean-modal',
226 $assets_dir . 'js/libraries/leanModal' . $suffix . '.js',
227 array( 'jquery' ),
228 $version
229 );
230
231 wp_register_style(
232 'lean-modal-css',
233 $assets_dir . 'css/modal' . $suffix . '.css',
234 array(),
235 $version
236 );
237
238 }
239
240 wp_register_style(
241 'charitable-datepicker',
242 $assets_dir . 'css/charitable-datepicker' . $suffix . '.css',
243 array(),
244 $version
245 );
246
247 /* pupload Fields is also registered but NOT enqueued. */
248 $upload_vars = array(
249 'remove_image' => _x( 'Remove', 'remove image button text', 'charitable' ),
250 'max_file_uploads_single' => /* translators: %d: number of files */ __( 'You can only upload %d file', 'charitable' ),
251 'max_file_uploads_plural' => __( 'You can only upload a maximum of %d files', 'charitable' ),
252 'max_file_size' => /* translators: %1$s: file name; %2$s: max upload size */ __( '%1$s exceeds the max upload size of %2$s', 'charitable' ),
253 'upload_problem' => /* translators: %s: file name */ __( '%s failed to upload. Please try again.', 'charitable' ),
254 );
255
256 wp_register_script(
257 'charitable-plup-fields',
258 $assets_dir . 'js/charitable-plupload-fields' . $suffix . '.js',
259 array( 'jquery-ui-sortable', 'wp-ajax-response', 'plupload-all' ),
260 $version,
261 true
262 );
263
264 wp_localize_script(
265 'charitable-plup-fields',
266 'CHARITABLE_UPLOAD_VARS',
267 $upload_vars
268 );
269
270 wp_register_style(
271 'charitable-plup-styles',
272 $assets_dir . 'css/charitable-plupload-fields' . $suffix . '.css',
273 array(),
274 $version
275 );
276
277
278 if ( charitable()->load_core_stripe() && class_exists( 'Charitable_Gateway_Stripe_AM' ) ) :
279
280 $gateway = new Charitable_Gateway_Stripe_AM;
281 $keys = $gateway->get_keys();
282
283 $stripe_vars = array(
284 'key' => $keys['public_key'],
285 'currency' => charitable_get_currency(),
286 'site_name' => get_option( 'blogname' ),
287 'zero_decimal' => Charitable_Gateway_Stripe_AM::is_zero_decimal_currency(),
288 );
289
290 /* Register Stripe JS script. */
291 wp_register_script(
292 'stripe-v3',
293 'https://js.stripe.com/v3/',
294 array(),
295 $version,
296 true
297 );
298
299 $dependencies = array( 'charitable-script', 'stripe-v3', 'jquery' );
300
301 if ( charitable_get_option( array( 'gateways_stripe', 'enable_stripe_checkout' ) ) ) {
302 $stripe_vars['mode'] = 'checkout';
303 } else {
304 $stripe_vars['mode'] = 'payment-intents';
305 }
306
307 wp_register_script(
308 'charitable-stripe',
309 $assets_dir . 'js/stripe/charitable-stripe' . $suffix . '.js',
310 $dependencies,
311 $version,
312 true
313 );
314
315 wp_localize_script(
316 'charitable-stripe',
317 'CHARITABLE_STRIPE_VARS',
318 $stripe_vars
319 );
320
321 if ( 'payment-intents' == $stripe_vars['mode'] ) {
322 /**
323 * Prevent the Stripe script from loading site-wide.
324 *
325 * @since 1.4.3
326 *
327 * @param boolean $load Whether to load the scripts sitewide. Defaults to true.
328 */
329 if ( apply_filters( 'charitable_stripe_load_stripe_scripts_sitewide', true ) ) {
330 wp_enqueue_script( 'stripe-v3' );
331 }
332 }
333
334 endif;
335
336
337
338
339
340
341 }
342
343 /**
344 * Conditionally load the donation form scripts if we're viewing the donation form.
345 *
346 * @since 1.4.0
347 *
348 * @return boolean True if scripts were loaded. False otherwise.
349 */
350 public function maybe_enqueue_donation_form_scripts() {
351 $load = charitable_is_page( 'campaign_donation_page' );
352
353 if ( ! $load ) {
354 $load = 'charitable_campaign_loop_before' == current_action() && 'modal' == charitable_get_option( 'donation_form_display', 'separate_page' );
355 }
356
357 if ( $load ) {
358 $this->enqueue_donation_form_scripts();
359 }
360
361 return $load;
362 }
363
364 /**
365 * Enqueues the donation form scripts.
366 *
367 * @since 1.4.6
368 *
369 * @return void
370 */
371 public function enqueue_donation_form_scripts() {
372 wp_enqueue_script( 'charitable-script' );
373
374 if ( Charitable_Gateways::get_instance()->any_gateway_supports( 'credit-card' ) ) {
375 wp_enqueue_script( 'charitable-credit-card' );
376 }
377 }
378
379 /**
380 * Adds custom post classes when viewing campaign.
381 *
382 * @since 1.0.0
383 *
384 * @param string[] $classes List of classes to be added with post_class().
385 * @return string[]
386 */
387 public function campaign_post_class( $classes ) {
388 $campaign = charitable_get_current_campaign();
389
390 if ( ! $campaign ) {
391 return $classes;
392 }
393
394 if ( $campaign->has_goal() ) {
395 $classes[] = 'campaign-has-goal';
396 $classes[] = $campaign->has_achieved_goal() ? 'campaign-has-achieved-goal' : 'campaign-has-not-achieved-goal';
397 } else {
398 $classes[] = 'campaign-has-no-goal';
399 }
400
401 if ( $campaign->is_endless() ) {
402 $classes[] = 'campaign-is-endless';
403 } else {
404 $classes[] = 'campaign-has-end-date';
405 $classes[] = $campaign->has_ended() ? 'campaign-has-ended' : 'campaign-has-not-ended';
406 }
407
408 return $classes;
409 }
410
411 /**
412 * Disable comments on application pages like the donation page.
413 *
414 * @deprecated 2.2.0
415 *
416 * @since 1.3.0
417 * @since 1.6.36 Deprecated. This is now handled in Charitable_Endpoints.
418 *
419 * @param boolean $open Whether comments are open.
420 * @return boolean
421 */
422 public function disable_comments_on_application_pages( $open ) {
423 /* If open is already false, just hit return. */
424 if ( ! $open ) {
425 return $open;
426 }
427
428 if ( charitable_is_page( 'campaign_donation_page', array( 'strict' => true ) )
429 || charitable_is_page( 'campaign_widget_page' )
430 || charitable_is_page( 'donation_receipt_page' )
431 || charitable_is_page( 'donation_processing_page' ) ) {
432 $open = false;
433 }
434
435 return $open;
436 }
437
438 /**
439 * Load the template functions after theme is loaded.
440 *
441 * This gives themes time to override the functions.
442 *
443 * @deprecated 2.0.0
444 *
445 * @since 1.2.3
446 * @since 1.6.10 Deprecated
447 *
448 * @return void
449 */
450 public function load_template_files() {
451 charitable_get_deprecated()->deprecated_function(
452 __METHOD__,
453 '1.6.10',
454 'charitable()->load_template_files()'
455 );
456
457 charitable()->load_template_files();
458 }
459 }
460
461 endif;
462