| 1 |
<?php |
| 2 |
/** |
| 3 |
* This template is used to display the donation grid with [donation_grid] |
| 4 |
*/ |
| 5 |
|
| 6 |
use Give\Helpers\Form\Template; |
| 7 |
use Give\Helpers\Form\Utils as FormUtils; |
| 8 |
|
| 9 |
// Exit if accessed directly. |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* List of changes |
| 16 |
* |
| 17 |
* @since 4.14.2 Add aria-label to the wrapper link and aria-hidden to its inner elements |
| 18 |
* @since 3.16.0 Add filters to enable the async mode and to change the values of the "amount raised" and "donations count" on the progress bar |
| 19 |
* @since 2.27.1 Use get_the_excerpt function to get short description of donation form to display in form grid. |
| 20 |
*/ |
| 21 |
|
| 22 |
$form_id = get_the_ID(); // Form ID. |
| 23 |
$give_settings = $args[0]; // Give settings. |
| 24 |
$atts = $args[1]; // Shortcode attributes. |
| 25 |
$raw_content = ''; // Raw form content. |
| 26 |
$stripped_content = ''; // Form content stripped of HTML tags and shortcodes. |
| 27 |
$excerpt = ''; // Trimmed form excerpt ready for display. |
| 28 |
|
| 29 |
$flex_direction = $atts['columns'] === '1' ? "row" : "column"; |
| 30 |
|
| 31 |
$activeTemplate = FormUtils::isLegacyForm($form_id) ? 'legacy' : Template::getActiveID($form_id); |
| 32 |
|
| 33 |
/* @var \Give\Form\Template $formTemplate */ |
| 34 |
$formTemplate = Give()->templates->getTemplate($activeTemplate); |
| 35 |
|
| 36 |
$renderTags = static function ($wrapper_class, $apply_styles = true) use ($form_id, $atts) { |
| 37 |
if (!taxonomy_exists('give_forms_tag')) { |
| 38 |
return ''; |
| 39 |
} |
| 40 |
|
| 41 |
$tags = wp_get_post_terms($form_id, 'give_forms_tag'); |
| 42 |
|
| 43 |
$tag_bg_color = !empty($atts['tag_background_color']) |
| 44 |
? $atts['tag_background_color'] |
| 45 |
: '#69b86b'; |
| 46 |
|
| 47 |
$tag_text_color = !empty($atts['tag_text_color']) |
| 48 |
? $atts['tag_text_color'] |
| 49 |
: '#ffffff'; |
| 50 |
|
| 51 |
$tag_container_color = count($tags) >= 1 |
| 52 |
? 'rgba(0, 0, 0, 0.35)' |
| 53 |
: 'none'; |
| 54 |
|
| 55 |
$tag_elements = array_map( |
| 56 |
static function ($term) use ($tag_text_color, $tag_bg_color) { |
| 57 |
$style = sprintf( |
| 58 |
'color: %s; background-color: %s;', |
| 59 |
esc_attr($tag_text_color), |
| 60 |
esc_attr($tag_bg_color) |
| 61 |
); |
| 62 |
|
| 63 |
return "<span style='$style'>$term->name</span>"; |
| 64 |
}, |
| 65 |
$tags |
| 66 |
); |
| 67 |
|
| 68 |
$tag_elements = implode('', $tag_elements); |
| 69 |
$styles = sprintf( |
| 70 |
"background-color: %s;", |
| 71 |
$apply_styles ? esc_attr($tag_container_color) : '' |
| 72 |
); |
| 73 |
|
| 74 |
return " |
| 75 |
<div class='$wrapper_class' style='$styles' > |
| 76 |
$tag_elements |
| 77 |
</div> |
| 78 |
"; |
| 79 |
}; |
| 80 |
?> |
| 81 |
|
| 82 |
<div class="give-grid__item"> |
| 83 |
<?php |
| 84 |
// Print the opening anchor tag based on display style. |
| 85 |
if ('modal_reveal' === $atts['display_style']) { |
| 86 |
printf( |
| 87 |
'<a id="give-card-%1$s" class="give-card js-give-grid-modal-launcher" data-effect="mfp-zoom-out" href="#give-modal-form-%1$s" aria-label="%2$s">', |
| 88 |
esc_attr($form_id), |
| 89 |
/* translators: %s: donation form title */ |
| 90 |
esc_attr(sprintf(__('Open donation form: %s', 'give'), $formTemplate->getFormHeading($form_id))) |
| 91 |
); |
| 92 |
} else { |
| 93 |
// Default to redirect display style. |
| 94 |
$form_grid_option = give_get_meta($form_id, '_give_form_grid_option', true); |
| 95 |
$form_grid_redirect_url = esc_url(give_get_meta($form_id, '_give_form_grid_redirect_url', true)); |
| 96 |
|
| 97 |
$url = ($form_grid_option === 'custom' && filter_var($form_grid_redirect_url, FILTER_VALIDATE_URL)) |
| 98 |
? $form_grid_redirect_url |
| 99 |
: get_the_permalink(); |
| 100 |
|
| 101 |
printf( |
| 102 |
'<a id="give-card-%1$s" onclick="return !document.body.classList.contains( \'block-editor-page\' )" class="give-card" href="%2$s" aria-label="%3$s">', |
| 103 |
esc_attr($form_id), |
| 104 |
esc_attr($url), |
| 105 |
/* translators: %s: donation form title */ |
| 106 |
esc_attr(sprintf(__('View donation form: %s', 'give'), $formTemplate->getFormHeading($form_id))) |
| 107 |
); |
| 108 |
} |
| 109 |
?> |
| 110 |
<div class="give-form-grid" style="flex-direction:<?php |
| 111 |
echo esc_attr($flex_direction) ?>" aria-hidden="true"> |
| 112 |
<?php |
| 113 |
// Maybe display the featured image. |
| 114 |
if ( |
| 115 |
give_is_setting_enabled($give_settings['form_featured_img']) |
| 116 |
&& ($imageSrc = $formTemplate->getFormFeaturedImage($form_id)) |
| 117 |
&& $atts['show_featured_image'] |
| 118 |
&& $atts['columns'] !== '1' |
| 119 |
) { |
| 120 |
/* |
| 121 |
* Filters the image size used in card layouts. |
| 122 |
* |
| 123 |
* @param string The image size. |
| 124 |
* @param array Form grid attributes. |
| 125 |
*/ |
| 126 |
$image_size = apply_filters('give_form_grid_image_size', $atts['image_size'], $atts); |
| 127 |
$image_attr = ''; |
| 128 |
|
| 129 |
if ('auto' !== $atts['image_height']) { |
| 130 |
$image_attr = [ |
| 131 |
'style' => 'height: ' . $atts['image_height'], |
| 132 |
]; |
| 133 |
} |
| 134 |
|
| 135 |
$image = wp_get_attachment_image(attachment_url_to_postid($imageSrc), $image_size, false, $image_attr); |
| 136 |
|
| 137 |
|
| 138 |
echo " |
| 139 |
<div class='give-form-grid-media'> |
| 140 |
<div class='give-card__media'> $image </div> |
| 141 |
|
| 142 |
{$renderTags('give-form-grid-media__tags')} |
| 143 |
</div> |
| 144 |
"; |
| 145 |
} elseif ( |
| 146 |
give_is_setting_enabled($give_settings['form_featured_img']) |
| 147 |
&& ($imageSrc = $formTemplate->getFormFeaturedImage($form_id)) |
| 148 |
&& $atts['show_featured_image'] |
| 149 |
&& $atts['columns'] === '1') { |
| 150 |
echo " |
| 151 |
<div id='row-media' class='give-form-grid-media'> |
| 152 |
<img class='give-form-grid-media' src='" . esc_url($imageSrc) . "' alt='' /> |
| 153 |
|
| 154 |
{$renderTags('give-form-grid-media__tags')} |
| 155 |
</div> |
| 156 |
"; |
| 157 |
} |
| 158 |
?> |
| 159 |
|
| 160 |
<div class="give-form-grid-container"> |
| 161 |
<div class="give-form-grid-content"> |
| 162 |
<?php |
| 163 |
if (!$atts['show_featured_image']) { |
| 164 |
echo " |
| 165 |
<div class='give-form-grid-media'> |
| 166 |
{$renderTags('give-form-grid-media__tags_no_image', false)} |
| 167 |
</div> |
| 168 |
"; |
| 169 |
} |
| 170 |
?> |
| 171 |
|
| 172 |
<?php |
| 173 |
|
| 174 |
// Maybe display the form title. |
| 175 |
if (true === $atts['show_title']) { |
| 176 |
printf( |
| 177 |
'<h3 class="give-form-grid-content__title">%1$s</h3>', |
| 178 |
$formTemplate->getFormHeading($form_id) |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
// Maybe display the form excerpt. |
| 183 |
if (true === $atts['show_excerpt']) { |
| 184 |
if ($raw_content = get_the_excerpt($form_id)) { |
| 185 |
$stripped_content = wp_strip_all_tags( |
| 186 |
strip_shortcodes($raw_content) |
| 187 |
); |
| 188 |
} else { |
| 189 |
// Get content from the form post's content field. |
| 190 |
$raw_content = give_get_meta($form_id, '_give_form_content', true); |
| 191 |
|
| 192 |
if (!empty($raw_content)) { |
| 193 |
$stripped_content = wp_strip_all_tags( |
| 194 |
strip_shortcodes($raw_content) |
| 195 |
); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
// Maybe truncate excerpt. |
| 200 |
if (0 < $atts['excerpt_length']) { |
| 201 |
$excerpt = wp_trim_words($stripped_content, $atts['excerpt_length']); |
| 202 |
} else { |
| 203 |
$excerpt = $stripped_content; |
| 204 |
} |
| 205 |
|
| 206 |
$excerpt = ($excerpt === '[]') ? '' : $excerpt; |
| 207 |
|
| 208 |
printf('<p class="give-form-grid-content__text">%s</p>', $excerpt); |
| 209 |
} |
| 210 |
|
| 211 |
if ($atts['show_donate_button']): |
| 212 |
$button_text = !empty($atts['donate_button_text']) |
| 213 |
? $atts['donate_button_text'] |
| 214 |
: give_get_meta($form_id, '_give_form_grid_donate_button_text', true); |
| 215 |
|
| 216 |
/** |
| 217 |
* @since 2.23.1 Updated the default text color for the donate button, see #6591. |
| 218 |
*/ |
| 219 |
$button_text_color = !empty($atts['donate_button_text_color']) |
| 220 |
? $atts['donate_button_text_color'] |
| 221 |
: '#000000'; |
| 222 |
?> |
| 223 |
<span class="give-card__button" style="text-decoration-color: <?php |
| 224 |
echo esc_attr($button_text_color); ?>; color: <?php |
| 225 |
echo esc_attr($button_text_color); ?>" aria-hidden="true"> |
| 226 |
<?php echo esc_html($button_text) ?: __('Donate', 'give'); ?> |
| 227 |
</span> |
| 228 |
<?php |
| 229 |
endif; ?> |
| 230 |
|
| 231 |
</div> |
| 232 |
<?php |
| 233 |
$form = new Give_Donate_Form($form_id); |
| 234 |
$goal_option = give_get_meta($form->ID, '_give_goal_option', true); |
| 235 |
|
| 236 |
// Sanity check - ensure form has pass all condition to show goal. |
| 237 |
$hide_goal = (isset($atts['show_goal']) && !filter_var($atts['show_goal'], FILTER_VALIDATE_BOOLEAN)) |
| 238 |
|| empty($form->ID) |
| 239 |
|| (is_singular('give_forms') && !give_is_setting_enabled($goal_option)) |
| 240 |
|| !give_is_setting_enabled($goal_option) || 0 === $form->goal; |
| 241 |
|
| 242 |
// Maybe display the goal progress bar. |
| 243 |
if (!$hide_goal) : |
| 244 |
|
| 245 |
do_action('give_form_grid_goal_progress_stats_before', $form_id); |
| 246 |
|
| 247 |
$goal_progress_stats = give_goal_progress_stats($form); |
| 248 |
$goal_format = $goal_progress_stats['format']; |
| 249 |
$color = $atts['progress_bar_color']; |
| 250 |
$show_goal = isset($atts['show_goal']) ? filter_var($atts['show_goal'], FILTER_VALIDATE_BOOLEAN) : true; |
| 251 |
|
| 252 |
/** |
| 253 |
* @since 3.16.0 Revert changes implemented on the 3.14.0 version |
| 254 |
* @since 3.14.0 Replace "$form->get_earnings()" with (new DonationQuery())->form($form->ID)->sumIntendedAmount() |
| 255 |
*/ |
| 256 |
$shortcode_stats = apply_filters( |
| 257 |
'give_goal_shortcode_stats', |
| 258 |
[ |
| 259 |
'income' => $form->get_earnings(), |
| 260 |
'goal' => $goal_progress_stats['raw_goal'], |
| 261 |
], |
| 262 |
$form_id, |
| 263 |
$goal_progress_stats, |
| 264 |
$args |
| 265 |
); |
| 266 |
|
| 267 |
$income = $shortcode_stats['income']; |
| 268 |
$goal = $shortcode_stats['goal']; |
| 269 |
|
| 270 |
switch ($goal_format) { |
| 271 |
case 'donation': |
| 272 |
$progress = $goal ? round(($form->get_sales() / $goal) * 100, 2) : 0; |
| 273 |
$progress_bar_value = $form->get_sales() >= $goal ? 100 : $progress; |
| 274 |
break; |
| 275 |
|
| 276 |
case 'donors': |
| 277 |
$progress = $goal ? round((give_get_form_donor_count($form->ID) / $goal) * 100, 2) : 0; |
| 278 |
$progress_bar_value = give_get_form_donor_count($form->ID) >= $goal ? 100 : $progress; |
| 279 |
break; |
| 280 |
|
| 281 |
case 'percentage': |
| 282 |
$progress = $goal ? round(($income / $goal) * 100, 2) : 0; |
| 283 |
$progress_bar_value = $income >= $goal ? 100 : $progress; |
| 284 |
break; |
| 285 |
|
| 286 |
default: |
| 287 |
$progress = $goal ? round(($income / $goal) * 100, 2) : 0; |
| 288 |
$progress_bar_value = $income >= $goal ? 100 : $progress; |
| 289 |
break; |
| 290 |
} |
| 291 |
|
| 292 |
?> |
| 293 |
<div class="give-form-grid-progress"> |
| 294 |
<?php |
| 295 |
$style = "width:$progress_bar_value%;"; |
| 296 |
$style .= "background: linear-gradient(180deg, {$color} 0%, {$color} 100%); background-blend-mode: multiply;"; |
| 297 |
echo '<div class="give-form-grid-progress-bar"> |
| 298 |
<div class="give-progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="' . $progress_bar_value . '" aria-label="' . esc_attr( sprintf( __( 'Form progress: %s%% toward goal', 'give' ), $progress_bar_value ) ) . '"> |
| 299 |
<span style="' . esc_attr($style) . '"></span> |
| 300 |
</div> |
| 301 |
</div>'; |
| 302 |
|
| 303 |
?> |
| 304 |
<div class="form-grid-raised"> |
| 305 |
<div class="form-grid-raised__details"> |
| 306 |
<?php |
| 307 |
if ('amount' === $goal_format) : |
| 308 |
|
| 309 |
/** |
| 310 |
* Filter the give currency. |
| 311 |
* |
| 312 |
* @since 1.8.17 |
| 313 |
*/ |
| 314 |
$form_currency = apply_filters( |
| 315 |
'give_goal_form_currency', |
| 316 |
give_get_currency($form_id), |
| 317 |
$form_id |
| 318 |
); |
| 319 |
|
| 320 |
/** |
| 321 |
* Filter the income formatting arguments. |
| 322 |
* |
| 323 |
* @since 1.8.17 |
| 324 |
*/ |
| 325 |
$income_format_args = apply_filters( |
| 326 |
'give_goal_income_format_args', |
| 327 |
[ |
| 328 |
'sanitize' => false, |
| 329 |
'currency' => $form_currency, |
| 330 |
'decimal' => false, |
| 331 |
], |
| 332 |
$form_id |
| 333 |
); |
| 334 |
|
| 335 |
/** |
| 336 |
* Filter the goal formatting arguments. |
| 337 |
* |
| 338 |
* @since 1.8.17 |
| 339 |
*/ |
| 340 |
$goal_format_args = apply_filters( |
| 341 |
'give_goal_amount_format_args', |
| 342 |
[ |
| 343 |
'sanitize' => false, |
| 344 |
'currency' => $form_currency, |
| 345 |
'decimal' => false, |
| 346 |
], |
| 347 |
$form_id |
| 348 |
); |
| 349 |
|
| 350 |
/** |
| 351 |
* This filter will be used to convert the goal amounts to different currencies. |
| 352 |
* |
| 353 |
* @since 2.5.4 |
| 354 |
* |
| 355 |
* @param array $amounts List of goal amounts. |
| 356 |
* @param int $form_id Donation Form ID. |
| 357 |
*/ |
| 358 |
$goal_amounts = apply_filters( |
| 359 |
'give_goal_amounts', |
| 360 |
[ |
| 361 |
$form_currency => $goal, |
| 362 |
], |
| 363 |
$form_id |
| 364 |
); |
| 365 |
|
| 366 |
/** |
| 367 |
* This filter will be used to convert the income amounts to different currencies. |
| 368 |
* |
| 369 |
* @since 2.5.4 |
| 370 |
* |
| 371 |
* @param array $amounts List of goal amounts. |
| 372 |
* @param int $form_id Donation Form ID. |
| 373 |
*/ |
| 374 |
$income_amounts = apply_filters( |
| 375 |
'give_goal_raised_amounts', |
| 376 |
[ |
| 377 |
$form_currency => $income, |
| 378 |
], |
| 379 |
$form_id |
| 380 |
); |
| 381 |
|
| 382 |
// Get human readable donation amount. |
| 383 |
$income = give_human_format_large_amount( |
| 384 |
give_format_amount($income, $income_format_args), ['currency' => $form_currency] |
| 385 |
); |
| 386 |
$goal = give_human_format_large_amount( |
| 387 |
give_format_amount($goal, $goal_format_args), |
| 388 |
['currency' => $form_currency] |
| 389 |
); |
| 390 |
|
| 391 |
// Format the human readable donation amount. |
| 392 |
$formatted_income = give_currency_filter( |
| 393 |
$income, |
| 394 |
[ |
| 395 |
'form_id' => $form_id, |
| 396 |
] |
| 397 |
); |
| 398 |
|
| 399 |
$formatted_goal = give_currency_filter( |
| 400 |
$goal, |
| 401 |
[ |
| 402 |
'form_id' => $form_id, |
| 403 |
] |
| 404 |
); |
| 405 |
echo sprintf( |
| 406 |
/* translators: 1: amount of income raised 2: goal target amount. */ |
| 407 |
__( |
| 408 |
'<span class="amount" data-amounts="%1$s">%2$s</span> |
| 409 |
<span class="goal" data-amounts="%3$s">of %4$s</span>', |
| 410 |
'give' |
| 411 |
), |
| 412 |
esc_attr(wp_json_encode($income_amounts, JSON_PRETTY_PRINT)), |
| 413 |
apply_filters('give_form_grid_progress_bar_amount_raised_value', esc_attr($formatted_income), $form_id), |
| 414 |
esc_attr(wp_json_encode($goal_amounts, JSON_PRETTY_PRINT)), |
| 415 |
esc_attr($formatted_goal) |
| 416 |
); |
| 417 |
|
| 418 |
elseif ('percentage' === $goal_format) : |
| 419 |
|
| 420 |
echo sprintf( /* translators: %s: percentage of the amount raised compared to the goal target */ |
| 421 |
__( |
| 422 |
' |
| 423 |
<span class="amount">%s%%</span> |
| 424 |
<span class="goal">of 100%</span>', |
| 425 |
'give' |
| 426 |
), |
| 427 |
round($progress) |
| 428 |
); |
| 429 |
|
| 430 |
elseif ('donation' === $goal_format) :?> |
| 431 |
|
| 432 |
<span class="amount"> |
| 433 |
<?php |
| 434 |
echo give_format_amount($form->get_sales(), ['decimal' => false]) ?> |
| 435 |
</span> |
| 436 |
|
| 437 |
<span class="goal"> |
| 438 |
<?php |
| 439 |
echo sprintf( |
| 440 |
_n('of %s donation', 'of %s donations', $goal, 'give'), |
| 441 |
give_format_amount($goal, ['decimal' => false]) |
| 442 |
); ?> |
| 443 |
</span> |
| 444 |
|
| 445 |
<?php |
| 446 |
elseif ('donors' === $goal_format) : ?> |
| 447 |
|
| 448 |
<span class="amount"> <?php |
| 449 |
echo give_get_form_donor_count($form->ID) ?> </span> |
| 450 |
<span class="goal"> |
| 451 |
<?php |
| 452 |
echo sprintf( |
| 453 |
_n('of %s donor', 'of %s donors', $goal, 'give'), |
| 454 |
give_format_amount($goal, ['decimal' => false]) |
| 455 |
); ?> |
| 456 |
</span> |
| 457 |
<?php |
| 458 |
endif ?> |
| 459 |
</div> |
| 460 |
|
| 461 |
<div class="form-grid-raised__details"> |
| 462 |
<span class="amount form-grid-raised__details_donations"> |
| 463 |
<?php echo apply_filters('give_form_grid_progress_bar_donations_count_value', $form->get_sales(), $form_id) ?> |
| 464 |
</span> |
| 465 |
<span class="goal"> |
| 466 |
<?php |
| 467 |
echo _n('donation', 'donations', $goal, 'give') ?> </span> |
| 468 |
</div> |
| 469 |
</div> |
| 470 |
</div> |
| 471 |
<?php |
| 472 |
endif ?> |
| 473 |
</div> |
| 474 |
</div> |
| 475 |
</a> |
| 476 |
<?php |
| 477 |
// If modal, print form in hidden container until it is time to be revealed. |
| 478 |
if ('modal_reveal' === $atts['display_style']) { |
| 479 |
if ( |
| 480 |
!isset($_GET['context']) // check if we are in block editor |
| 481 |
&& !FormUtils::isLegacyForm($form_id) |
| 482 |
) { |
| 483 |
echo give_form_shortcode( |
| 484 |
[ |
| 485 |
'id' => $form_id, |
| 486 |
'display_style' => 'button', |
| 487 |
] |
| 488 |
); |
| 489 |
} else { |
| 490 |
printf( |
| 491 |
'<div id="give-modal-form-%1$s" class="give-donation-grid-item-form give-modal--slide mfp-hide">', |
| 492 |
$form_id |
| 493 |
); |
| 494 |
|
| 495 |
echo give_form_shortcode( |
| 496 |
[ |
| 497 |
'id' => $form_id, |
| 498 |
'display_style' => 'button', |
| 499 |
] |
| 500 |
); |
| 501 |
|
| 502 |
echo '</div>'; |
| 503 |
} |
| 504 |
} |
| 505 |
?> |
| 506 |
</div> |
| 507 |
|
| 508 |
|