Classic.php
421 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Views\Form\Templates\Classic; |
| 4 | |
| 5 | use Give\DonationForms\DonationQuery; |
| 6 | use Give\Form\Template; |
| 7 | use Give\Form\Template\Hookable; |
| 8 | use Give\Form\Template\Scriptable; |
| 9 | use Give\Helpers\Form\Template as FormTemplateUtils; |
| 10 | use Give\Helpers\Form\Template\Utils\Frontend; |
| 11 | use Give\MultiFormGoals\ProgressBar\Model as ProgressBarModal; |
| 12 | use Give\Receipt\DonationReceipt; |
| 13 | use Give_Donate_Form; |
| 14 | use InvalidArgumentException; |
| 15 | |
| 16 | /** |
| 17 | * Classic Donation Form Template |
| 18 | * |
| 19 | * @since 2.18.0 |
| 20 | */ |
| 21 | class Classic extends Template implements Hookable, Scriptable |
| 22 | { |
| 23 | /** |
| 24 | * @var array |
| 25 | */ |
| 26 | private $options; |
| 27 | |
| 28 | /** |
| 29 | * @var bool |
| 30 | */ |
| 31 | private $scriptsLoaded = false; |
| 32 | |
| 33 | public function __construct() |
| 34 | { |
| 35 | $this->options = FormTemplateUtils::getOptions(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @inheritDoc |
| 40 | */ |
| 41 | public function getID() |
| 42 | { |
| 43 | return 'classic'; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @inheritDoc |
| 48 | */ |
| 49 | public function getName() |
| 50 | { |
| 51 | return __('Classic Form', 'give'); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @inheritDoc |
| 56 | */ |
| 57 | public function getImage() |
| 58 | { |
| 59 | return GIVE_PLUGIN_URL . 'assets/dist/images/admin/template-preview-classic.png'; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @inheritDoc |
| 64 | */ |
| 65 | public function getOptionsConfig() |
| 66 | { |
| 67 | return require 'optionConfig.php'; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @inheritDoc |
| 72 | */ |
| 73 | public function loadHooks() |
| 74 | { |
| 75 | add_action('give_pre_form', [$this, 'renderIconDefinitions']); |
| 76 | |
| 77 | // Display header |
| 78 | if ('enabled' === $this->options[ 'visual_appearance' ][ 'display_header' ]) { |
| 79 | add_action('give_pre_form', [$this, 'renderHeader'], 10, 3); |
| 80 | } |
| 81 | |
| 82 | add_action('give_before_donation_levels', [$this, 'renderDonationAmountHeading'], 20); |
| 83 | |
| 84 | $sections = [ |
| 85 | [ |
| 86 | 'hooks' => ['give_donation_form_top'], |
| 87 | 'class' => 'give-donation-amount-section', |
| 88 | ], |
| 89 | [ |
| 90 | 'hooks' => ['give_donation_form_register_login_fields'], |
| 91 | 'class' => 'give-personal-info-section', |
| 92 | ], |
| 93 | [ |
| 94 | 'hooks' => ['give_payment_mode_top', 'give_payment_mode_bottom'], |
| 95 | 'class' => 'give-payment-details-section', |
| 96 | ], |
| 97 | [ |
| 98 | 'hooks' => ['give_donation_form_before_submit', 'give_donation_form_after_submit'], |
| 99 | 'class' => 'give-donate-now-button-section', |
| 100 | ], |
| 101 | [ |
| 102 | 'hooks' => ['give_donation_summary_top', 'give_donation_summary_bottom'], |
| 103 | 'class' => 'give-donation-form-summary-section', |
| 104 | ], |
| 105 | ]; |
| 106 | |
| 107 | foreach ($sections as $section) { |
| 108 | [$start, $end] = array_pad($section['hooks'], 2, null); |
| 109 | |
| 110 | add_action($start, function () use ($section) { |
| 111 | printf('<section class="give-form-section %s">', $section[ 'class' ]); |
| 112 | }, -10000); |
| 113 | |
| 114 | add_action($end ? : $start, function () { |
| 115 | echo '</section>'; |
| 116 | }, 10000); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Remove actions |
| 121 | */ |
| 122 | // Remove goal. |
| 123 | remove_action('give_pre_form', 'give_show_goal_progress'); |
| 124 | // Remove intermediate continue button which appears when display style set to anything other than "onpage". |
| 125 | remove_action('give_after_donation_levels', 'give_display_checkout_button'); |
| 126 | // Hide title. |
| 127 | add_filter('give_form_title', '__return_empty_string'); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * @inheritDoc |
| 133 | */ |
| 134 | public function loadScripts() |
| 135 | { |
| 136 | if ($this->scriptsLoaded) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | $this->scriptsLoaded = true; |
| 141 | |
| 142 | // Font |
| 143 | if ($primaryFont = $this->getPrimaryFont()) { |
| 144 | wp_enqueue_style( |
| 145 | 'give-google-font', |
| 146 | 'https://fonts.googleapis.com/css?family=' . urlencode($primaryFont) . ':400,500,600,700&display=swap', |
| 147 | [], |
| 148 | GIVE_VERSION |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | wp_enqueue_style( |
| 153 | 'give-classic-template', |
| 154 | GIVE_PLUGIN_URL . 'assets/dist/css/give-classic-template.css', |
| 155 | [], |
| 156 | GIVE_VERSION |
| 157 | ); |
| 158 | |
| 159 | // We are replacing the Give styles with this template. Let’s not fight |
| 160 | // against ourselves. This will help us not need to write such specific |
| 161 | // styles so that users can still override ours. |
| 162 | add_action('wp_enqueue_scripts', function () { |
| 163 | wp_dequeue_style('give-styles'); |
| 164 | wp_dequeue_style('give_recurring_css'); |
| 165 | wp_dequeue_style('give-currency-switcher-style'); |
| 166 | wp_dequeue_style('give-fee-recovery'); |
| 167 | wp_dequeue_style('give-donation-summary-style-frontend'); |
| 168 | wp_dequeue_style('give-authorize-css'); |
| 169 | }, 10); |
| 170 | |
| 171 | // CSS Variables |
| 172 | wp_add_inline_style( |
| 173 | 'give-classic-template', |
| 174 | $this->loadFile('css/variables.php', [ |
| 175 | 'primaryColor' => $this->options[ 'visual_appearance' ][ 'primary_color' ], |
| 176 | 'headerBackgroundImage' => $this->options[ 'visual_appearance' ][ 'header_background_image' ], |
| 177 | 'headerBackgroundColor' => $this->options[ 'visual_appearance' ][ 'header_background_color' ], |
| 178 | 'statsProgressBarColor' => give_get_meta(Frontend::getFormId(), '_give_goal_color', true), |
| 179 | 'primaryFont' => $primaryFont ? : 'inherit' |
| 180 | ]) |
| 181 | ); |
| 182 | |
| 183 | // JS |
| 184 | wp_enqueue_script( |
| 185 | 'give-classic-template-js', |
| 186 | GIVE_PLUGIN_URL . 'assets/dist/js/give-classic-template.js', |
| 187 | ['give'], |
| 188 | GIVE_VERSION, |
| 189 | true |
| 190 | ); |
| 191 | |
| 192 | wp_localize_script( |
| 193 | 'give-classic-template-js', |
| 194 | 'classicTemplateOptions', |
| 195 | $this->options |
| 196 | ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @inheritDoc |
| 201 | * |
| 202 | * @since 2.30.0 Check if visual_appearance is set before accessing it. |
| 203 | */ |
| 204 | public function getLoadingView() |
| 205 | { |
| 206 | return $this->loadFile('views/loading.php', [ |
| 207 | 'options' => array_key_exists('visual_appearance', $this->options) |
| 208 | ? $this->options['visual_appearance'] |
| 209 | : [], |
| 210 | ]); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @inheritDoc |
| 215 | */ |
| 216 | public function renderLoadingView($formId = null) |
| 217 | { |
| 218 | echo $this->getLoadingView(); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @inheritDoc |
| 223 | */ |
| 224 | public function getReceiptView() |
| 225 | { |
| 226 | return wp_doing_ajax() |
| 227 | ? $this->getFilePath('views/receipt.php') |
| 228 | : parent::getReceiptView(); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Render donation form header |
| 233 | * |
| 234 | * @since 2.19.0 use trinary operator instead of Coalesce operator to make code php 5.6 compatible. |
| 235 | * |
| 236 | * @param int $formId |
| 237 | * @param array $args |
| 238 | * @param Give_Donate_Form $form |
| 239 | */ |
| 240 | public function renderHeader($formId, $args, $form) |
| 241 | { |
| 242 | $hasGoal = $form->has_goal(); |
| 243 | |
| 244 | echo $this->loadFile('views/header.php', [ |
| 245 | 'title' => isset($this->options['visual_appearance']['main_heading']) ? $this->options['visual_appearance']['main_heading'] : $form->post_title, |
| 246 | 'description' => $this->options[ 'visual_appearance' ][ 'description' ], |
| 247 | 'isSecureBadgeEnabled' => $this->options[ 'visual_appearance' ][ 'secure_badge' ] === 'enabled', |
| 248 | 'secureBadgeContent' => $this->options[ 'visual_appearance' ][ 'secure_badge_text' ], |
| 249 | 'hasGoal' => $hasGoal, |
| 250 | 'goalStats' => $hasGoal ? $this->getFormGoalStats($form) : [] |
| 251 | ]); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Render donation amount heading |
| 256 | */ |
| 257 | public function renderDonationAmountHeading() |
| 258 | { |
| 259 | echo $this->loadFile('views/donation-amount-heading.php', [ |
| 260 | 'content' => $this->options[ 'donation_amount' ][ 'headline' ], |
| 261 | ]); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Render the SVG icon definitions. |
| 266 | * |
| 267 | * @void |
| 268 | */ |
| 269 | public function renderIconDefinitions() |
| 270 | { |
| 271 | echo $this->loadFile('views/icon-defs.php'); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * @inheritDoc |
| 276 | */ |
| 277 | public function getReceiptDetails($donationId) |
| 278 | { |
| 279 | $receipt = new DonationReceipt($donationId); |
| 280 | |
| 281 | $receipt->heading = esc_html(give_do_email_tags($this->options[ 'donation_receipt' ][ 'headline' ], ['payment_id' => $donationId])); |
| 282 | $receipt->message = wp_kses_post(give_do_email_tags($this->options[ 'donation_receipt' ][ 'description' ], ['payment_id' => $donationId])); |
| 283 | |
| 284 | /** |
| 285 | * Fire the action for receipt object. |
| 286 | * |
| 287 | * @since 2.7.0 |
| 288 | */ |
| 289 | do_action('give_new_receipt', $receipt); |
| 290 | |
| 291 | return $receipt; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * @since 3.14.0 Use sumIntendedAmount() and getDonationCount() methods to retrieve the proper values for the raised amount and donations count |
| 296 | */ |
| 297 | public function getFormGoalStats(Give_Donate_Form $form) |
| 298 | { |
| 299 | $goalStats = give_goal_progress_stats($form->get_ID()); |
| 300 | $raisedRaw = (new DonationQuery())->form($form->ID)->sumIntendedAmount(); |
| 301 | |
| 302 | // Setup default raised value |
| 303 | $raised = give_currency_filter( |
| 304 | give_format_amount( |
| 305 | $raisedRaw, |
| 306 | [ |
| 307 | 'sanitize' => false, |
| 308 | 'decimal' => false, |
| 309 | ] |
| 310 | ) |
| 311 | ); |
| 312 | |
| 313 | // Setup default count value |
| 314 | $count = (new ProgressBarModal(['ids' => [$form->get_ID()]]))->getDonationCount(); |
| 315 | |
| 316 | // Setup default count label |
| 317 | $countLabel = _n('donation', 'donations', $count, 'give'); |
| 318 | |
| 319 | // Setup default goal value |
| 320 | $goal = give_currency_filter( |
| 321 | give_format_amount( |
| 322 | $form->get_goal(), |
| 323 | [ |
| 324 | 'sanitize' => false, |
| 325 | 'decimal' => false, |
| 326 | ] |
| 327 | ) |
| 328 | ); |
| 329 | |
| 330 | $stats = [ |
| 331 | 'raised' => $raised, |
| 332 | 'raisedRaw' => $raisedRaw, |
| 333 | 'progress' => $goalStats[ 'progress' ], |
| 334 | 'count' => $count, |
| 335 | 'countLabel' => $countLabel, |
| 336 | 'goal' => $goal, |
| 337 | 'goalRaw' => $goalStats[ 'raw_goal' ], |
| 338 | ]; |
| 339 | |
| 340 | switch ($goalStats[ 'format' ]) { |
| 341 | case 'donation': |
| 342 | return array_merge($stats, [ |
| 343 | 'count' => $goalStats[ 'actual' ], |
| 344 | 'goal' => $goalStats[ 'goal' ], |
| 345 | ]); |
| 346 | |
| 347 | case 'donors': |
| 348 | return array_merge($stats, [ |
| 349 | 'count' => $goalStats[ 'actual' ], |
| 350 | 'countLabel' => _n('donor', 'donors', $count, 'give'), |
| 351 | 'goal' => $goalStats[ 'goal' ], |
| 352 | ]); |
| 353 | |
| 354 | default: |
| 355 | return $stats; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Load file |
| 361 | * |
| 362 | * @param string $file |
| 363 | * @param array $args |
| 364 | * |
| 365 | * @return string |
| 366 | * @throws InvalidArgumentException |
| 367 | * |
| 368 | */ |
| 369 | protected function loadFile($file, $args = []) |
| 370 | { |
| 371 | $filePath = $this->getFilePath($file); |
| 372 | |
| 373 | if (! file_exists($filePath)) { |
| 374 | throw new InvalidArgumentException("File {$filePath} does not exist"); |
| 375 | } |
| 376 | |
| 377 | ob_start(); |
| 378 | extract($args); |
| 379 | include $filePath; |
| 380 | |
| 381 | return ob_get_clean(); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Get file path |
| 386 | * |
| 387 | * @param string $file |
| 388 | * |
| 389 | * @return string |
| 390 | */ |
| 391 | protected function getFilePath($file = '') |
| 392 | { |
| 393 | return GIVE_PLUGIN_DIR . "src/Views/Form/Templates/Classic/resources/{$file}"; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * @return string|null |
| 398 | */ |
| 399 | protected function getPrimaryFont() |
| 400 | { |
| 401 | $primaryFont = $this->options[ 'visual_appearance' ][ 'primary_font' ]; |
| 402 | |
| 403 | if ($primaryFont !== 'system') { |
| 404 | return $primaryFont; |
| 405 | } |
| 406 | |
| 407 | return null; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * @since 2.27.0 Return description from form settings. |
| 412 | * @param int $formId |
| 413 | * |
| 414 | * @return string |
| 415 | */ |
| 416 | public function getFormExcerpt($formId) |
| 417 | { |
| 418 | return $this->options['visual_appearance']['description']; |
| 419 | } |
| 420 | } |
| 421 |