| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Views\Form\Templates\Sequoia; |
| 4 |
|
| 5 |
use Give\Helpers\Form\Template as FormTemplateUtils; |
| 6 |
use Give_Donate_Form; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Actions |
| 10 |
* |
| 11 |
* @package Give\Views\Form\Templates\Sequoia |
| 12 |
* @since 2.7.0 |
| 13 |
*/ |
| 14 |
class Actions |
| 15 |
{ |
| 16 |
|
| 17 |
protected $templateOptions; |
| 18 |
|
| 19 |
/** |
| 20 |
* Initialize |
| 21 |
* |
| 22 |
* @since 2.7.0 |
| 23 |
*/ |
| 24 |
public function init() |
| 25 |
{ |
| 26 |
// Get Template options |
| 27 |
$this->templateOptions = FormTemplateUtils::getOptions(); |
| 28 |
|
| 29 |
// Get decimal numbers option |
| 30 |
$decimalNumbersOption = isset($this->templateOptions['visual_appearance']['decimals_enabled']) |
| 31 |
? $this->templateOptions['visual_appearance']['decimals_enabled'] |
| 32 |
: 'disabled'; |
| 33 |
|
| 34 |
// Set zero number of decimal. |
| 35 |
if ('disabled' === $decimalNumbersOption) { |
| 36 |
add_filter('give_get_currency_formatting_settings', [ |
| 37 |
$this, |
| 38 |
'setupZeroNumberOfDecimalInCurrencyFormattingSetting', |
| 39 |
], 1); |
| 40 |
add_filter('give_get_option_number_decimals', [$this, 'setupZeroNumberOfDecimal'], 1); |
| 41 |
} |
| 42 |
|
| 43 |
// Handle common hooks. |
| 44 |
add_action('give_donation_form', [$this, 'loadCommonHooks'], 9, 2); |
| 45 |
|
| 46 |
// Setup hooks. |
| 47 |
add_action('give_pre_form_output', [$this, 'loadHooks'], 1, 3); |
| 48 |
|
| 49 |
// Setup Stripe font Styles |
| 50 |
add_filter('give_stripe_get_element_font_styles', [$this, 'setupStripeFontStyles'], 1); |
| 51 |
|
| 52 |
// Setup Stripe base styles |
| 53 |
add_filter('give_stripe_get_element_base_styles', [$this, 'setupStripeBaseStyles'], 1); |
| 54 |
} |
| 55 |
|
| 56 |
/** Set Stripe base styles consistent with Sequoia form template |
| 57 |
* |
| 58 |
* As per design requirement, we want to format Stripe elements to use Montserrat, with the same styling options as other text inputs. |
| 59 |
* |
| 60 |
* @since 2.7.0 |
| 61 |
* |
| 62 |
* @param object $styles |
| 63 |
* |
| 64 |
* @return object |
| 65 |
*/ |
| 66 |
public function setupStripeBaseStyles($styles) |
| 67 |
{ |
| 68 |
$styles = sprintf( |
| 69 |
'{ |
| 70 |
"fontFamily": "%1$s", |
| 71 |
"color": "#8d8e8e", |
| 72 |
"fontWeight": 400, |
| 73 |
"fontSize": "14px", |
| 74 |
"::placeholder": { |
| 75 |
"color": "#8d8e8e" |
| 76 |
}, |
| 77 |
":-webkit-autofill": { |
| 78 |
"color": "#e39f48" |
| 79 |
} |
| 80 |
}', |
| 81 |
$this->isGoogleFontEnabled() ? 'Montserrat' : 'system-ui' |
| 82 |
); |
| 83 |
|
| 84 |
return json_decode($styles); |
| 85 |
} |
| 86 |
|
| 87 |
/** Set Stripe Font styles consistent with Sequoia form template |
| 88 |
* |
| 89 |
* As per design requirement, we want to format Stripe elements to use Montserrat, with the same styling options as other text inputs. |
| 90 |
* |
| 91 |
* @since 2.7.0 |
| 92 |
* |
| 93 |
* @param array $fontStyles |
| 94 |
* |
| 95 |
* @return array |
| 96 |
*/ |
| 97 |
public function setupStripeFontStyles($fontStyles) |
| 98 |
{ |
| 99 |
return [ |
| 100 |
'cssSrc' => $this->isGoogleFontEnabled() ? |
| 101 |
'https://fonts.googleapis.com/css2?family=Montserrat&display=swap' : |
| 102 |
false, |
| 103 |
]; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Set zero as number of decimals in currency formatting setting. |
| 108 |
* |
| 109 |
* As per design requirement we want to format donation amount with zero decimal whether or not number of decimal admin setting set to zero. |
| 110 |
* |
| 111 |
* @since 2.7.0 |
| 112 |
* |
| 113 |
* @param array $currencyFormattingSettings |
| 114 |
* |
| 115 |
* @return array |
| 116 |
*/ |
| 117 |
public function setupZeroNumberOfDecimalInCurrencyFormattingSetting($currencyFormattingSettings) |
| 118 |
{ |
| 119 |
$currencyFormattingSettings['number_decimals'] = 0; |
| 120 |
|
| 121 |
return $currencyFormattingSettings; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Return zero as number of decimals setting value or currency formatting value. |
| 126 |
* |
| 127 |
* As per design requirement we want to format donation amount with zero decimal whether or not number of decimal admin setting set to zero. |
| 128 |
* |
| 129 |
* @since 2.7.0 |
| 130 |
* @return int |
| 131 |
*/ |
| 132 |
public function setupZeroNumberOfDecimal() |
| 133 |
{ |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Setup common hooks |
| 139 |
* |
| 140 |
* @param int $formId |
| 141 |
* @param array $args |
| 142 |
*/ |
| 143 |
public function loadCommonHooks($formId, $args) |
| 144 |
{ |
| 145 |
remove_action('give_donation_form_register_login_fields', 'give_show_register_login_fields'); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Setup hooks |
| 150 |
* |
| 151 |
* @param int $formId |
| 152 |
* @param array $args |
| 153 |
* @param Give_Donate_Form $form |
| 154 |
*/ |
| 155 |
public function loadHooks($formId, $args, $form) |
| 156 |
{ |
| 157 |
/** |
| 158 |
* Add hooks |
| 159 |
*/ |
| 160 |
add_action('give_pre_form', [$this, 'getNavigator'], 0, 3); |
| 161 |
add_action('give_post_form', [$this, 'getFooterSection'], 99998, 0); |
| 162 |
add_action('give_donation_form_top', [$this, 'getIntroductionSection'], 0, 3); |
| 163 |
add_action('give_donation_form_top', [$this, 'getStartWrapperHTMLForAmountSection'], 0); |
| 164 |
add_action('give_donation_form_top', [$this, 'getCloseWrapperHTMLForAmountSection'], 99998); |
| 165 |
add_action('give_payment_mode_top', 'give_show_register_login_fields'); |
| 166 |
add_action('give_payment_mode_top', [$this, 'getStartWrapperHTMLForPaymentSection'], 0); |
| 167 |
add_action('give_donation_form_after_submit', [$this, 'getCloseWrapperHTMLForPaymentSection'], 999); |
| 168 |
|
| 169 |
/** |
| 170 |
* Remove actions |
| 171 |
*/ |
| 172 |
// Remove goal. |
| 173 |
remove_action('give_pre_form', 'give_show_goal_progress', 10); |
| 174 |
|
| 175 |
// Remove intermediate continue button which appears when display style is set to anything other than "onpage". |
| 176 |
remove_action('give_after_donation_levels', 'give_display_checkout_button', 10); |
| 177 |
|
| 178 |
// Hide title. |
| 179 |
add_filter('give_form_title', '__return_empty_string'); |
| 180 |
|
| 181 |
// Append "Donate with " to gateway labels |
| 182 |
add_filter('give_enabled_payment_gateways', [$this, 'modifyGatewayLabels']); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Add form navigator / header |
| 187 |
* |
| 188 |
* @since 2.7.0 |
| 189 |
* |
| 190 |
* @param $formId |
| 191 |
* @param $args |
| 192 |
* @param $form |
| 193 |
*/ |
| 194 |
public function getNavigator($formId, $args, $form) |
| 195 |
{ |
| 196 |
include 'sections/form-navigator.php'; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Add introduction form section |
| 201 |
* |
| 202 |
* @since 2.7.0 |
| 203 |
* |
| 204 |
* @param $formId |
| 205 |
* @param $args |
| 206 |
* @param $form |
| 207 |
*/ |
| 208 |
public function getIntroductionSection($formId, $args, $form) |
| 209 |
{ |
| 210 |
include 'sections/introduction.php'; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Add form footer |
| 215 |
* |
| 216 |
* @since 2.7.0 |
| 217 |
*/ |
| 218 |
public function getFooterSection() |
| 219 |
{ |
| 220 |
include 'sections/footer.php'; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Add checkout button |
| 225 |
* |
| 226 |
* @since 4.16.3 Escaped the checkout button label in the Sequoia template. |
| 227 |
* @since 2.7.0 |
| 228 |
*/ |
| 229 |
public function getCheckoutButton() |
| 230 |
{ |
| 231 |
$label = isset($this->templateOptions['payment_information']['checkout_label']) ? $this->templateOptions['payment_information']['checkout_label'] : __( |
| 232 |
'Donate Now', |
| 233 |
'give' |
| 234 |
); |
| 235 |
|
| 236 |
return sprintf( |
| 237 |
'<div class="give-submit-button-wrap give-clearfix"> |
| 238 |
<input type="submit" class="give-submit give-btn" id="give-purchase-button" name="give-purchase" value="%1$s" data-before-validation-label="Donate Now"> |
| 239 |
<span class="give-loading-animation"></span> |
| 240 |
</div>', |
| 241 |
esc_attr($label) |
| 242 |
); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Add wrapper and introduction text to payment information section |
| 247 |
* |
| 248 |
* @since 4.16.4 Escaped the headline output. |
| 249 |
* @since 2.7.0 |
| 250 |
* |
| 251 |
* @param int $formId |
| 252 |
*/ |
| 253 |
public function getStartWrapperHTMLForPaymentSection($formId) |
| 254 |
{ |
| 255 |
$headline = isset($this->templateOptions['payment_information']['headline']) ? $this->templateOptions['payment_information']['headline'] : __( |
| 256 |
"Who's giving today?", |
| 257 |
'give' |
| 258 |
); |
| 259 |
$description = isset($this->templateOptions['payment_information']['description']) ? $this->templateOptions['payment_information']['description'] : __( |
| 260 |
'We’ll never share this information with anyone.', |
| 261 |
'give' |
| 262 |
); |
| 263 |
|
| 264 |
printf( |
| 265 |
'<div class="give-section payment"><div class="heading">%1$s</div><div class="subheading">%2$s</div>', |
| 266 |
esc_html($headline), |
| 267 |
$description |
| 268 |
); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Close wrapper for payment information section |
| 273 |
* |
| 274 |
* @since 2.7.0 |
| 275 |
*/ |
| 276 |
public function getCloseWrapperHTMLForPaymentSection() |
| 277 |
{ |
| 278 |
echo '</div>'; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Start choose amount section |
| 283 |
* |
| 284 |
* @since 4.16.4 Escaped the donate button label output. |
| 285 |
* @since 2.7.0 |
| 286 |
*/ |
| 287 |
public function getStartWrapperHTMLForAmountSection() |
| 288 |
{ |
| 289 |
$content = isset($this->templateOptions['payment_amount']['content']) && ! empty($this->templateOptions['payment_amount']['content']) ? $this->templateOptions['payment_amount']['content'] : sprintf( |
| 290 |
__( |
| 291 |
'How much would you like to donate? As a contributor to %s we make sure your donation goes directly to supporting our cause. Thank you for your generosity!', |
| 292 |
'give' |
| 293 |
), |
| 294 |
get_bloginfo('sitename') |
| 295 |
); |
| 296 |
$label = ! empty($this->templateOptions['introduction']['donate_label']) ? $this->templateOptions['introduction']['donate_label'] : __( |
| 297 |
'Donate Now', |
| 298 |
'give' |
| 299 |
); |
| 300 |
$arrow = is_rtl() ? 'left' : 'right'; |
| 301 |
printf( |
| 302 |
'<button class="give-btn advance-btn">%1$s<i class="fas fa-chevron-%2$s"></i></button></div>', |
| 303 |
esc_html($label), |
| 304 |
$arrow |
| 305 |
); |
| 306 |
|
| 307 |
if ( ! empty($content)) { |
| 308 |
printf( |
| 309 |
'<div class="give-section choose-amount"><p class="content">%1$s</p>', |
| 310 |
$content |
| 311 |
); |
| 312 |
} else { |
| 313 |
echo "<div class='give-section choose-amount'>"; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Close choose amount section |
| 319 |
* |
| 320 |
* @since 4.16.4 Escaped the continue button label output. |
| 321 |
* @since 2.7.0 |
| 322 |
*/ |
| 323 |
public function getCloseWrapperHTMLForAmountSection() |
| 324 |
{ |
| 325 |
$label = isset($this->templateOptions['payment_amount']['next_label']) ? $this->templateOptions['payment_amount']['next_label'] : __( |
| 326 |
'Continue', |
| 327 |
'give' |
| 328 |
); |
| 329 |
$arrow = is_rtl() ? 'left' : 'right'; |
| 330 |
|
| 331 |
printf( |
| 332 |
'<button class="give-btn advance-btn">%1$s<i class="fas fa-chevron-%2$s"></i></button></div>', |
| 333 |
esc_html($label), |
| 334 |
$arrow |
| 335 |
); |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Append gateway labels with "Donate with " |
| 340 |
* |
| 341 |
* Modify gateways array returned give_get_enabled_payment_gateways, before printing |
| 342 |
* |
| 343 |
* @param array $gateways Array of enabled gateways |
| 344 |
* |
| 345 |
* @return array $gateways Array of modified enabled gateways |
| 346 |
*/ |
| 347 |
public function modifyGatewayLabels(array $gateways) |
| 348 |
{ |
| 349 |
foreach ($gateways as $key => $value) { |
| 350 |
$gateways[$key]['checkout_label'] = sprintf( |
| 351 |
__('Donate with %1$s', 'give'), |
| 352 |
$value['checkout_label'] |
| 353 |
); |
| 354 |
} |
| 355 |
|
| 356 |
return $gateways; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* @since 2.16.0 |
| 361 |
* @return bool |
| 362 |
*/ |
| 363 |
private function isGoogleFontEnabled() |
| 364 |
{ |
| 365 |
$templateOptions = FormTemplateUtils::getOptions(); |
| 366 |
|
| 367 |
// Set defaults |
| 368 |
$templateOptions['visual_appearance']['google-fonts'] = ! empty($templateOptions['visual_appearance']['google-fonts']) ? $templateOptions['visual_appearance']['google-fonts'] : 'enabled'; |
| 369 |
|
| 370 |
return give_is_setting_enabled($templateOptions['visual_appearance']['google-fonts']); |
| 371 |
} |
| 372 |
|
| 373 |
} |
| 374 |
|