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