| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Public class. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Public |
| 6 |
* @version 1.0.0 |
| 7 |
* @author Eric Daams |
| 8 |
* @copyright Copyright (c) 2019, Studio 164a |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 14 |
|
| 15 |
if ( ! class_exists( 'Charitable_Public' ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* Charitable Public class. |
| 19 |
* |
| 20 |
* @final |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
final class Charitable_Public { |
| 24 |
|
| 25 |
/** |
| 26 |
* The single instance of this class. |
| 27 |
* |
| 28 |
* @since 1.2.0 |
| 29 |
* |
| 30 |
* @var Charitable_Public|null |
| 31 |
*/ |
| 32 |
private static $instance = null; |
| 33 |
|
| 34 |
/** |
| 35 |
* Returns and/or create the single instance of this class. |
| 36 |
* |
| 37 |
* @since 1.2.0 |
| 38 |
* |
| 39 |
* @return Charitable_Public |
| 40 |
*/ |
| 41 |
public static function get_instance() { |
| 42 |
if ( is_null( self::$instance ) ) { |
| 43 |
self::$instance = new self(); |
| 44 |
} |
| 45 |
|
| 46 |
return self::$instance; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Set up the class. |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
*/ |
| 54 |
private function __construct() { |
| 55 |
add_action( 'after_setup_theme', array( $this, 'load_template_files' ) ); |
| 56 |
add_action( 'wp_enqueue_scripts', array( $this, 'setup_scripts' ) ); |
| 57 |
add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_donation_form_scripts' ), 11 ); |
| 58 |
add_action( 'charitable_campaign_loop_before', array( $this, 'maybe_enqueue_donation_form_scripts' ) ); |
| 59 |
add_filter( 'post_class', array( $this, 'campaign_post_class' ) ); |
| 60 |
add_filter( 'comments_open', array( $this, 'disable_comments_on_application_pages' ) ); |
| 61 |
|
| 62 |
do_action( 'charitable_public_start', $this ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Load the template functions after theme is loaded. |
| 67 |
* |
| 68 |
* This gives themes time to override the functions. |
| 69 |
* |
| 70 |
* @since 1.2.3 |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function load_template_files() { |
| 75 |
require_once( 'charitable-template-functions.php' ); |
| 76 |
require_once( 'charitable-template-hooks.php' ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Loads public facing scripts and stylesheets. |
| 81 |
* |
| 82 |
* @since 1.0.0 |
| 83 |
* |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
public function setup_scripts() { |
| 87 |
if ( ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ) { |
| 88 |
$suffix = ''; |
| 89 |
$version = ''; |
| 90 |
} else { |
| 91 |
$suffix = '.min'; |
| 92 |
$version = charitable()->get_version(); |
| 93 |
} |
| 94 |
|
| 95 |
$assets_dir = charitable()->get_path( 'assets', false ); |
| 96 |
|
| 97 |
/* Main Charitable script. */ |
| 98 |
$minimum = charitable_get_minimum_donation_amount(); |
| 99 |
$amount_msg = $minimum > 0 |
| 100 |
? sprintf( |
| 101 |
/* Translators: %s: minimum donatino amount */ |
| 102 |
__( 'You must donate at least %s.', 'charitable' ), charitable_format_money( $minimum ) |
| 103 |
) |
| 104 |
: sprintf( |
| 105 |
/* Translators: %s: minimum donatino amount */ |
| 106 |
__( 'You must donate more than %s.', 'charitable' ), charitable_format_money( $minimum ) |
| 107 |
); |
| 108 |
|
| 109 |
$currency = charitable_get_currency_helper(); |
| 110 |
|
| 111 |
/** |
| 112 |
* Filter the Javascript vars array. |
| 113 |
* |
| 114 |
* @since 1.0.0 |
| 115 |
* |
| 116 |
* @param array $vars The set of vars. |
| 117 |
*/ |
| 118 |
$vars = apply_filters( 'charitable_javascript_vars', array( |
| 119 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 120 |
'loading_gif' => $assets_dir . '/images/charitable-loading.gif', |
| 121 |
'currency_symbol' => $currency->get_currency_symbol(), |
| 122 |
'currency_format_num_decimals' => esc_attr( $currency->get_decimals() ), |
| 123 |
'currency_format_decimal_sep' => esc_attr( $currency->get_decimal_separator() ), |
| 124 |
'currency_format_thousand_sep' => esc_attr( $currency->get_thousands_separator() ), |
| 125 |
'currency_format' => esc_attr( $currency->get_accounting_js_format() ), // For accounting.js. |
| 126 |
'minimum_donation' => $minimum, |
| 127 |
'error_invalid_amount' => $amount_msg, |
| 128 |
'error_required_fields' => __( 'Please fill out all required fields.', 'charitable' ), |
| 129 |
'error_unknown' => __( 'Your donation could not be processed. Please reload the page and try again.', 'charitable' ), |
| 130 |
'error_invalid_cc_number' => __( 'The credit card passed is not valid.', 'charitable' ), |
| 131 |
'error_invalid_cc_expiry' => __( 'The credit card expiry date is not valid.', 'charitable' ), |
| 132 |
) ); |
| 133 |
|
| 134 |
/* Accounting.js */ |
| 135 |
wp_register_script( |
| 136 |
'accounting', |
| 137 |
$assets_dir . 'js/libraries/accounting' . $suffix . '.js', |
| 138 |
array( 'jquery-core' ), |
| 139 |
$version, |
| 140 |
true |
| 141 |
); |
| 142 |
|
| 143 |
$deps = array( 'accounting', 'jquery-core' ); |
| 144 |
|
| 145 |
if ( wp_script_is( 'charitable-sessions', 'enqueued' ) ) { |
| 146 |
array_unshift( $deps, 'charitable-sessions' ); |
| 147 |
} |
| 148 |
|
| 149 |
wp_register_script( |
| 150 |
'charitable-script', |
| 151 |
$assets_dir . 'js/charitable' . $suffix . '.js', |
| 152 |
$deps, |
| 153 |
$version, |
| 154 |
true |
| 155 |
); |
| 156 |
|
| 157 |
wp_localize_script( |
| 158 |
'charitable-script', |
| 159 |
'CHARITABLE_VARS', |
| 160 |
$vars |
| 161 |
); |
| 162 |
|
| 163 |
/* Credit card validation */ |
| 164 |
wp_register_script( |
| 165 |
'charitable-credit-card', |
| 166 |
$assets_dir . 'js/charitable-credit-card' . $suffix . '.js', |
| 167 |
array( 'charitable-script' ), |
| 168 |
$version, |
| 169 |
true |
| 170 |
); |
| 171 |
|
| 172 |
/* Main styles */ |
| 173 |
wp_register_style( |
| 174 |
'charitable-styles', |
| 175 |
$assets_dir . 'css/charitable' . $suffix . '.css', |
| 176 |
array(), |
| 177 |
$version |
| 178 |
); |
| 179 |
|
| 180 |
wp_enqueue_style( 'charitable-styles' ); |
| 181 |
|
| 182 |
/* Lean Modal is registered but NOT enqueued yet. */ |
| 183 |
if ( 'modal' == charitable_get_option( 'donation_form_display', 'separate_page' ) ) { |
| 184 |
|
| 185 |
wp_register_script( |
| 186 |
'lean-modal', |
| 187 |
$assets_dir . 'js/libraries/leanModal' . $suffix . '.js', |
| 188 |
array( 'jquery-core' ), |
| 189 |
$version |
| 190 |
); |
| 191 |
|
| 192 |
wp_register_style( |
| 193 |
'lean-modal-css', |
| 194 |
$assets_dir . 'css/modal' . $suffix .'.css', |
| 195 |
array(), |
| 196 |
$version |
| 197 |
); |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
wp_register_style( |
| 202 |
'charitable-datepicker', |
| 203 |
$assets_dir . 'css/charitable-datepicker' . $suffix .'.css', |
| 204 |
array(), |
| 205 |
$version |
| 206 |
); |
| 207 |
|
| 208 |
/* pupload Fields is also registered but NOT enqueued. */ |
| 209 |
$upload_vars = array( |
| 210 |
'remove_image' => _x( 'Remove', 'remove image button text', 'charitable' ), |
| 211 |
'max_file_uploads_single' => __( 'You can only upload %d file', 'charitable' ), |
| 212 |
'max_file_uploads_plural' => __( 'You can only upload a maximum of %d files', 'charitable' ), |
| 213 |
'max_file_size' => __( '%1$s exceeds the max upload size of %2$s', 'charitable' ), |
| 214 |
'upload_problem' => __( '%s failed to upload. Please try again.', 'charitable' ), |
| 215 |
); |
| 216 |
|
| 217 |
wp_register_script( |
| 218 |
'charitable-plup-fields', |
| 219 |
$assets_dir . 'js/charitable-plupload-fields' . $suffix . '.js', |
| 220 |
array( 'jquery-ui-sortable', 'wp-ajax-response', 'plupload-all' ), |
| 221 |
$version, |
| 222 |
true |
| 223 |
); |
| 224 |
|
| 225 |
wp_localize_script( |
| 226 |
'charitable-plup-fields', |
| 227 |
'CHARITABLE_UPLOAD_VARS', |
| 228 |
$upload_vars |
| 229 |
); |
| 230 |
|
| 231 |
wp_register_style( |
| 232 |
'charitable-plup-styles', |
| 233 |
$assets_dir . 'css/charitable-plupload-fields' . $suffix . '.css', |
| 234 |
array(), |
| 235 |
$version |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Conditionally load the donation form scripts if we're viewing the donation form. |
| 241 |
* |
| 242 |
* @since 1.4.0 |
| 243 |
* |
| 244 |
* @return boolean True if scripts were loaded. False otherwise. |
| 245 |
*/ |
| 246 |
public function maybe_enqueue_donation_form_scripts() { |
| 247 |
$load = charitable_is_page( 'campaign_donation_page' ); |
| 248 |
|
| 249 |
if ( ! $load ) { |
| 250 |
$load = 'charitable_campaign_loop_before' == current_action() && 'modal' == charitable_get_option( 'donation_form_display', 'separate_page' ); |
| 251 |
} |
| 252 |
|
| 253 |
if ( $load ) { |
| 254 |
$this->enqueue_donation_form_scripts(); |
| 255 |
} |
| 256 |
|
| 257 |
return $load; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Enqueues the donation form scripts. |
| 262 |
* |
| 263 |
* @since 1.4.6 |
| 264 |
* |
| 265 |
* @return void |
| 266 |
*/ |
| 267 |
public function enqueue_donation_form_scripts() { |
| 268 |
wp_enqueue_script( 'charitable-script' ); |
| 269 |
|
| 270 |
if ( Charitable_Gateways::get_instance()->any_gateway_supports( 'credit-card' ) ) { |
| 271 |
wp_enqueue_script( 'charitable-credit-card' ); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Adds custom post classes when viewing campaign. |
| 277 |
* |
| 278 |
* @since 1.0.0 |
| 279 |
* |
| 280 |
* @param string[] $classes List of classes to be added with post_class(). |
| 281 |
* @return string[] |
| 282 |
*/ |
| 283 |
public function campaign_post_class( $classes ) { |
| 284 |
$campaign = charitable_get_current_campaign(); |
| 285 |
|
| 286 |
if ( ! $campaign ) { |
| 287 |
return $classes; |
| 288 |
} |
| 289 |
|
| 290 |
$classes[] = $campaign->has_goal() ? 'campaign-has-goal' : 'campaign-has-no-goal'; |
| 291 |
$classes[] = $campaign->is_endless() ? 'campaign-is-endless' : 'campaign-has-end-date'; |
| 292 |
return $classes; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Disable comments on application pages like the donation page. |
| 297 |
* |
| 298 |
* @since 1.3.0 |
| 299 |
* |
| 300 |
* @param boolean $open Whether comments are open. |
| 301 |
* @return boolean |
| 302 |
*/ |
| 303 |
public function disable_comments_on_application_pages( $open ) { |
| 304 |
/* If open is already false, just hit return. */ |
| 305 |
if ( ! $open ) { |
| 306 |
return $open; |
| 307 |
} |
| 308 |
|
| 309 |
if ( charitable_is_page( 'campaign_donation_page', array( 'strict' => true ) ) |
| 310 |
|| charitable_is_page( 'campaign_widget_page' ) |
| 311 |
|| charitable_is_page( 'donation_receipt_page' ) |
| 312 |
|| charitable_is_page( 'donation_processing_page' ) ) { |
| 313 |
$open = false; |
| 314 |
} |
| 315 |
|
| 316 |
return $open; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
endif; |
| 321 |
|