| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Public class. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Public |
| 6 |
* @author Eric Daams |
| 7 |
* @copyright Copyright (c) 2022, Studio 164a |
| 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 |
/** |
| 278 |
* Conditionally load the donation form scripts if we're viewing the donation form. |
| 279 |
* |
| 280 |
* @since 1.4.0 |
| 281 |
* |
| 282 |
* @return boolean True if scripts were loaded. False otherwise. |
| 283 |
*/ |
| 284 |
public function maybe_enqueue_donation_form_scripts() { |
| 285 |
$load = charitable_is_page( 'campaign_donation_page' ); |
| 286 |
|
| 287 |
if ( ! $load ) { |
| 288 |
$load = 'charitable_campaign_loop_before' == current_action() && 'modal' == charitable_get_option( 'donation_form_display', 'separate_page' ); |
| 289 |
} |
| 290 |
|
| 291 |
if ( $load ) { |
| 292 |
$this->enqueue_donation_form_scripts(); |
| 293 |
} |
| 294 |
|
| 295 |
return $load; |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Enqueues the donation form scripts. |
| 300 |
* |
| 301 |
* @since 1.4.6 |
| 302 |
* |
| 303 |
* @return void |
| 304 |
*/ |
| 305 |
public function enqueue_donation_form_scripts() { |
| 306 |
wp_enqueue_script( 'charitable-script' ); |
| 307 |
|
| 308 |
if ( Charitable_Gateways::get_instance()->any_gateway_supports( 'credit-card' ) ) { |
| 309 |
wp_enqueue_script( 'charitable-credit-card' ); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Adds custom post classes when viewing campaign. |
| 315 |
* |
| 316 |
* @since 1.0.0 |
| 317 |
* |
| 318 |
* @param string[] $classes List of classes to be added with post_class(). |
| 319 |
* @return string[] |
| 320 |
*/ |
| 321 |
public function campaign_post_class( $classes ) { |
| 322 |
$campaign = charitable_get_current_campaign(); |
| 323 |
|
| 324 |
if ( ! $campaign ) { |
| 325 |
return $classes; |
| 326 |
} |
| 327 |
|
| 328 |
if ( $campaign->has_goal() ) { |
| 329 |
$classes[] = 'campaign-has-goal'; |
| 330 |
$classes[] = $campaign->has_achieved_goal() ? 'campaign-has-achieved-goal' : 'campaign-has-not-achieved-goal'; |
| 331 |
} else { |
| 332 |
$classes[] = 'campaign-has-no-goal'; |
| 333 |
} |
| 334 |
|
| 335 |
if ( $campaign->is_endless() ) { |
| 336 |
$classes[] = 'campaign-is-endless'; |
| 337 |
} else { |
| 338 |
$classes[] = 'campaign-has-end-date'; |
| 339 |
$classes[] = $campaign->has_ended() ? 'campaign-has-ended' : 'campaign-has-not-ended'; |
| 340 |
} |
| 341 |
|
| 342 |
return $classes; |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Disable comments on application pages like the donation page. |
| 347 |
* |
| 348 |
* @deprecated 2.2.0 |
| 349 |
* |
| 350 |
* @since 1.3.0 |
| 351 |
* @since 1.6.36 Deprecated. This is now handled in Charitable_Endpoints. |
| 352 |
* |
| 353 |
* @param boolean $open Whether comments are open. |
| 354 |
* @return boolean |
| 355 |
*/ |
| 356 |
public function disable_comments_on_application_pages( $open ) { |
| 357 |
/* If open is already false, just hit return. */ |
| 358 |
if ( ! $open ) { |
| 359 |
return $open; |
| 360 |
} |
| 361 |
|
| 362 |
if ( charitable_is_page( 'campaign_donation_page', array( 'strict' => true ) ) |
| 363 |
|| charitable_is_page( 'campaign_widget_page' ) |
| 364 |
|| charitable_is_page( 'donation_receipt_page' ) |
| 365 |
|| charitable_is_page( 'donation_processing_page' ) ) { |
| 366 |
$open = false; |
| 367 |
} |
| 368 |
|
| 369 |
return $open; |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Load the template functions after theme is loaded. |
| 374 |
* |
| 375 |
* This gives themes time to override the functions. |
| 376 |
* |
| 377 |
* @deprecated 2.0.0 |
| 378 |
* |
| 379 |
* @since 1.2.3 |
| 380 |
* @since 1.6.10 Deprecated |
| 381 |
* |
| 382 |
* @return void |
| 383 |
*/ |
| 384 |
public function load_template_files() { |
| 385 |
charitable_get_deprecated()->deprecated_function( |
| 386 |
__METHOD__, |
| 387 |
'1.6.10', |
| 388 |
'charitable()->load_template_files()' |
| 389 |
); |
| 390 |
|
| 391 |
charitable()->load_template_files(); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
endif; |
| 396 |
|