| 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 |
) |
| 152 |
); |
| 153 |
|
| 154 |
/* Accounting.js */ |
| 155 |
wp_register_script( |
| 156 |
'accounting', |
| 157 |
$assets_dir . 'js/libraries/accounting' . $suffix . '.js', |
| 158 |
array( 'jquery' ), |
| 159 |
$version, |
| 160 |
true |
| 161 |
); |
| 162 |
|
| 163 |
$deps = array( 'accounting', 'jquery' ); |
| 164 |
|
| 165 |
if ( wp_script_is( 'charitable-sessions', 'enqueued' ) ) { |
| 166 |
array_unshift( $deps, 'charitable-sessions' ); |
| 167 |
} |
| 168 |
|
| 169 |
wp_register_script( |
| 170 |
'charitable-script', |
| 171 |
$assets_dir . 'js/charitable' . $suffix . '.js', |
| 172 |
$deps, |
| 173 |
$version, |
| 174 |
true |
| 175 |
); |
| 176 |
|
| 177 |
wp_localize_script( |
| 178 |
'charitable-script', |
| 179 |
'CHARITABLE_VARS', |
| 180 |
$vars |
| 181 |
); |
| 182 |
|
| 183 |
/* Credit card validation */ |
| 184 |
wp_register_script( |
| 185 |
'charitable-credit-card', |
| 186 |
$assets_dir . 'js/charitable-credit-card' . $suffix . '.js', |
| 187 |
array( 'charitable-script' ), |
| 188 |
$version, |
| 189 |
true |
| 190 |
); |
| 191 |
|
| 192 |
/* URL sanitizer. */ |
| 193 |
wp_register_script( |
| 194 |
'charitable-url-sanitizer', |
| 195 |
$assets_dir . 'js/charitable-url-sanitizer' . $suffix . '.js', |
| 196 |
array( 'jquery' ), |
| 197 |
$version, |
| 198 |
true |
| 199 |
); |
| 200 |
|
| 201 |
/* General forms scripts. */ |
| 202 |
wp_register_script( |
| 203 |
'charitable-forms', |
| 204 |
$assets_dir . 'js/charitable-forms' . $suffix . '.js', |
| 205 |
array( 'jquery' ), |
| 206 |
$version, |
| 207 |
true |
| 208 |
); |
| 209 |
|
| 210 |
/* Main styles */ |
| 211 |
wp_register_style( |
| 212 |
'charitable-styles', |
| 213 |
$assets_dir . 'css/charitable' . $suffix . '.css', |
| 214 |
array(), |
| 215 |
$version |
| 216 |
); |
| 217 |
|
| 218 |
wp_enqueue_style( 'charitable-styles' ); |
| 219 |
|
| 220 |
/* Lean Modal is registered but NOT enqueued yet. */ |
| 221 |
if ( 'modal' == charitable_get_option( 'donation_form_display', 'separate_page' ) ) { |
| 222 |
|
| 223 |
wp_register_script( |
| 224 |
'lean-modal', |
| 225 |
$assets_dir . 'js/libraries/leanModal' . $suffix . '.js', |
| 226 |
array( 'jquery' ), |
| 227 |
$version |
| 228 |
); |
| 229 |
|
| 230 |
wp_register_style( |
| 231 |
'lean-modal-css', |
| 232 |
$assets_dir . 'css/modal' . $suffix . '.css', |
| 233 |
array(), |
| 234 |
$version |
| 235 |
); |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
wp_register_style( |
| 240 |
'charitable-datepicker', |
| 241 |
$assets_dir . 'css/charitable-datepicker' . $suffix . '.css', |
| 242 |
array(), |
| 243 |
$version |
| 244 |
); |
| 245 |
|
| 246 |
/* pupload Fields is also registered but NOT enqueued. */ |
| 247 |
$upload_vars = array( |
| 248 |
'remove_image' => _x( 'Remove', 'remove image button text', 'charitable' ), |
| 249 |
'max_file_uploads_single' => /* translators: %d: number of files */ __( 'You can only upload %d file', 'charitable' ), |
| 250 |
'max_file_uploads_plural' => __( 'You can only upload a maximum of %d files', 'charitable' ), |
| 251 |
'max_file_size' => /* translators: %1$s: file name; %2$s: max upload size */ __( '%1$s exceeds the max upload size of %2$s', 'charitable' ), |
| 252 |
'upload_problem' => /* translators: %s: file name */ __( '%s failed to upload. Please try again.', 'charitable' ), |
| 253 |
); |
| 254 |
|
| 255 |
wp_register_script( |
| 256 |
'charitable-plup-fields', |
| 257 |
$assets_dir . 'js/charitable-plupload-fields' . $suffix . '.js', |
| 258 |
array( 'jquery-ui-sortable', 'wp-ajax-response', 'plupload-all' ), |
| 259 |
$version, |
| 260 |
true |
| 261 |
); |
| 262 |
|
| 263 |
wp_localize_script( |
| 264 |
'charitable-plup-fields', |
| 265 |
'CHARITABLE_UPLOAD_VARS', |
| 266 |
$upload_vars |
| 267 |
); |
| 268 |
|
| 269 |
wp_register_style( |
| 270 |
'charitable-plup-styles', |
| 271 |
$assets_dir . 'css/charitable-plupload-fields' . $suffix . '.css', |
| 272 |
array(), |
| 273 |
$version |
| 274 |
); |
| 275 |
|
| 276 |
|
| 277 |
if ( charitable()->load_core_stripe() ) : |
| 278 |
|
| 279 |
$gateway = new Charitable_Gateway_Stripe_AM; |
| 280 |
$keys = $gateway->get_keys(); |
| 281 |
|
| 282 |
$stripe_vars = array( |
| 283 |
'key' => $keys['public_key'], |
| 284 |
'currency' => charitable_get_currency(), |
| 285 |
'site_name' => get_option( 'blogname' ), |
| 286 |
'zero_decimal' => Charitable_Gateway_Stripe_AM::is_zero_decimal_currency(), |
| 287 |
); |
| 288 |
|
| 289 |
/* Register Stripe JS script. */ |
| 290 |
wp_register_script( |
| 291 |
'stripe-v3', |
| 292 |
'https://js.stripe.com/v3/', |
| 293 |
array(), |
| 294 |
$version, |
| 295 |
true |
| 296 |
); |
| 297 |
|
| 298 |
$dependencies = array( 'charitable-script', 'stripe-v3', 'jquery' ); |
| 299 |
|
| 300 |
// if ( charitable_get_option( array( 'gateways_stripe', 'enable_stripe_checkout' ) ) ) { |
| 301 |
// $stripe_vars['mode'] = 'checkout'; |
| 302 |
// } else { |
| 303 |
$stripe_vars['mode'] = 'payment-intents'; |
| 304 |
// } |
| 305 |
|
| 306 |
wp_register_script( |
| 307 |
'charitable-stripe', |
| 308 |
$assets_dir . 'js/stripe/charitable-stripe' . $suffix . '.js', |
| 309 |
$dependencies, |
| 310 |
$version, |
| 311 |
true |
| 312 |
); |
| 313 |
|
| 314 |
wp_localize_script( |
| 315 |
'charitable-stripe', |
| 316 |
'CHARITABLE_STRIPE_VARS', |
| 317 |
$stripe_vars |
| 318 |
); |
| 319 |
|
| 320 |
if ( 'payment-intents' == $stripe_vars['mode'] ) { |
| 321 |
/** |
| 322 |
* Prevent the Stripe script from loading site-wide. |
| 323 |
* |
| 324 |
* @since 1.4.3 |
| 325 |
* |
| 326 |
* @param boolean $load Whether to load the scripts sitewide. Defaults to true. |
| 327 |
*/ |
| 328 |
if ( apply_filters( 'charitable_stripe_load_stripe_scripts_sitewide', true ) ) { |
| 329 |
wp_enqueue_script( 'stripe-v3' ); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
endif; |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Conditionally load the donation form scripts if we're viewing the donation form. |
| 344 |
* |
| 345 |
* @since 1.4.0 |
| 346 |
* |
| 347 |
* @return boolean True if scripts were loaded. False otherwise. |
| 348 |
*/ |
| 349 |
public function maybe_enqueue_donation_form_scripts() { |
| 350 |
$load = charitable_is_page( 'campaign_donation_page' ); |
| 351 |
|
| 352 |
if ( ! $load ) { |
| 353 |
$load = 'charitable_campaign_loop_before' == current_action() && 'modal' == charitable_get_option( 'donation_form_display', 'separate_page' ); |
| 354 |
} |
| 355 |
|
| 356 |
if ( $load ) { |
| 357 |
$this->enqueue_donation_form_scripts(); |
| 358 |
} |
| 359 |
|
| 360 |
return $load; |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Enqueues the donation form scripts. |
| 365 |
* |
| 366 |
* @since 1.4.6 |
| 367 |
* |
| 368 |
* @return void |
| 369 |
*/ |
| 370 |
public function enqueue_donation_form_scripts() { |
| 371 |
wp_enqueue_script( 'charitable-script' ); |
| 372 |
|
| 373 |
if ( Charitable_Gateways::get_instance()->any_gateway_supports( 'credit-card' ) ) { |
| 374 |
wp_enqueue_script( 'charitable-credit-card' ); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Adds custom post classes when viewing campaign. |
| 380 |
* |
| 381 |
* @since 1.0.0 |
| 382 |
* |
| 383 |
* @param string[] $classes List of classes to be added with post_class(). |
| 384 |
* @return string[] |
| 385 |
*/ |
| 386 |
public function campaign_post_class( $classes ) { |
| 387 |
$campaign = charitable_get_current_campaign(); |
| 388 |
|
| 389 |
if ( ! $campaign ) { |
| 390 |
return $classes; |
| 391 |
} |
| 392 |
|
| 393 |
if ( $campaign->has_goal() ) { |
| 394 |
$classes[] = 'campaign-has-goal'; |
| 395 |
$classes[] = $campaign->has_achieved_goal() ? 'campaign-has-achieved-goal' : 'campaign-has-not-achieved-goal'; |
| 396 |
} else { |
| 397 |
$classes[] = 'campaign-has-no-goal'; |
| 398 |
} |
| 399 |
|
| 400 |
if ( $campaign->is_endless() ) { |
| 401 |
$classes[] = 'campaign-is-endless'; |
| 402 |
} else { |
| 403 |
$classes[] = 'campaign-has-end-date'; |
| 404 |
$classes[] = $campaign->has_ended() ? 'campaign-has-ended' : 'campaign-has-not-ended'; |
| 405 |
} |
| 406 |
|
| 407 |
return $classes; |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* Disable comments on application pages like the donation page. |
| 412 |
* |
| 413 |
* @deprecated 2.2.0 |
| 414 |
* |
| 415 |
* @since 1.3.0 |
| 416 |
* @since 1.6.36 Deprecated. This is now handled in Charitable_Endpoints. |
| 417 |
* |
| 418 |
* @param boolean $open Whether comments are open. |
| 419 |
* @return boolean |
| 420 |
*/ |
| 421 |
public function disable_comments_on_application_pages( $open ) { |
| 422 |
/* If open is already false, just hit return. */ |
| 423 |
if ( ! $open ) { |
| 424 |
return $open; |
| 425 |
} |
| 426 |
|
| 427 |
if ( charitable_is_page( 'campaign_donation_page', array( 'strict' => true ) ) |
| 428 |
|| charitable_is_page( 'campaign_widget_page' ) |
| 429 |
|| charitable_is_page( 'donation_receipt_page' ) |
| 430 |
|| charitable_is_page( 'donation_processing_page' ) ) { |
| 431 |
$open = false; |
| 432 |
} |
| 433 |
|
| 434 |
return $open; |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Load the template functions after theme is loaded. |
| 439 |
* |
| 440 |
* This gives themes time to override the functions. |
| 441 |
* |
| 442 |
* @deprecated 2.0.0 |
| 443 |
* |
| 444 |
* @since 1.2.3 |
| 445 |
* @since 1.6.10 Deprecated |
| 446 |
* |
| 447 |
* @return void |
| 448 |
*/ |
| 449 |
public function load_template_files() { |
| 450 |
charitable_get_deprecated()->deprecated_function( |
| 451 |
__METHOD__, |
| 452 |
'1.6.10', |
| 453 |
'charitable()->load_template_files()' |
| 454 |
); |
| 455 |
|
| 456 |
charitable()->load_template_files(); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
endif; |
| 461 |
|