PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.12.3
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.12.3
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.8.12.3, at includes/public/class-charitable-public.php

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