| 1 |
<?php |
| 2 |
/** |
| 3 |
* Offline Helper - Static helpers for offline donation settings |
| 4 |
* |
| 5 |
* @package SureDonation |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace SureDonation\Inc\Payments\Offline; |
| 9 |
|
| 10 |
use SureDonation\Inc\Helper; |
| 11 |
use SureDonation\Inc\Payments\Payment_Helper; |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Offline_Helper class |
| 20 |
* Provides offline donation utilities |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
class Offline_Helper { |
| 25 |
/** |
| 26 |
* Check if offline donations are enabled. |
| 27 |
* |
| 28 |
* @return bool True if offline donations are enabled. |
| 29 |
* @since 1.0.0 |
| 30 |
*/ |
| 31 |
public static function is_offline_enabled() { |
| 32 |
$settings = self::get_all_offline_settings(); |
| 33 |
return ! empty( $settings['enabled'] ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get offline donation instructions. |
| 38 |
* |
| 39 |
* @param string $campaign_name Optional campaign name for smart tag replacement. |
| 40 |
* @return string Sanitized HTML instructions. |
| 41 |
* @since 1.0.0 |
| 42 |
*/ |
| 43 |
public static function get_offline_instructions( $campaign_name = '' ) { |
| 44 |
$settings = self::get_all_offline_settings(); |
| 45 |
$instructions = $settings['instructions'] ?? ''; |
| 46 |
$instructions = is_string( $instructions ) ? $instructions : ''; |
| 47 |
|
| 48 |
// Replace smart tags with escaped values. |
| 49 |
$tags = [ |
| 50 |
'{campaign_name}' => esc_html( $campaign_name ), |
| 51 |
'{site_title}' => esc_html( get_bloginfo( 'name' ) ), |
| 52 |
'{site_url}' => esc_url( home_url() ), |
| 53 |
'{admin_email}' => esc_html( Helper::get_string_value( get_option( 'admin_email', '' ) ) ), |
| 54 |
]; |
| 55 |
|
| 56 |
// Apply wp_kses_post first to sanitize the HTML template, then replace smart tags. |
| 57 |
// This avoids double-encoding of entities in smart tag values (e.g. "Tom & Jerry"). |
| 58 |
$instructions = wp_kses_post( $instructions ); |
| 59 |
return str_replace( array_keys( $tags ), array_values( $tags ), $instructions ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get all offline donation settings with defaults. |
| 64 |
* |
| 65 |
* @return array<string, mixed> Offline settings. |
| 66 |
* @since 1.0.0 |
| 67 |
*/ |
| 68 |
public static function get_all_offline_settings() { |
| 69 |
$settings = Payment_Helper::get_gateway_settings( 'offline' ); |
| 70 |
|
| 71 |
$defaults = [ |
| 72 |
'enabled' => false, |
| 73 |
'instructions' => self::get_default_instructions(), |
| 74 |
]; |
| 75 |
|
| 76 |
return wp_parse_args( $settings, $defaults ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get default offline donation instructions. |
| 81 |
* |
| 82 |
* @return string Default instructions HTML. |
| 83 |
* @since 1.0.0 |
| 84 |
*/ |
| 85 |
public static function get_default_instructions() { |
| 86 |
$instructions = '<p>' . __( 'To make an offline donation toward this cause, follow these steps:', 'suredonation' ) . '</p>'; |
| 87 |
$instructions .= '<ol>'; |
| 88 |
$instructions .= '<li>' . __( 'Write a check payable to "{site_title}"', 'suredonation' ) . '</li>'; |
| 89 |
$instructions .= '<li>' . __( 'On the memo line of the check, indicate that the donation is for "{site_title}"', 'suredonation' ) . '</li>'; |
| 90 |
$instructions .= '<li>' . __( 'Mail your check to the address below:', 'suredonation' ) . '</li>'; |
| 91 |
$instructions .= '</ol>'; |
| 92 |
$instructions .= '<p><em>' . __( 'Your mailing address here', 'suredonation' ) . '</em></p>'; |
| 93 |
$instructions .= '<p>' . __( 'Your tax-deductible donation is greatly appreciated!', 'suredonation' ) . '</p>'; |
| 94 |
|
| 95 |
return $instructions; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Determine whether an instructions value is effectively blank. |
| 100 |
* |
| 101 |
* A cleared WYSIWYG editor serializes as empty markup (e.g. <p> </p> or |
| 102 |
* <p><br></p>) rather than an empty string, so a plain trim() would treat it |
| 103 |
* as non-blank. Strip tags and non-breaking spaces before checking. |
| 104 |
* |
| 105 |
* @param mixed $instructions Raw instructions value. |
| 106 |
* @return bool True when the value has no meaningful content. |
| 107 |
* @since 1.1.2 |
| 108 |
*/ |
| 109 |
public static function is_blank_instructions( $instructions ) { |
| 110 |
if ( ! is_string( $instructions ) ) { |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
$plain = str_replace( [ ' ', "\xc2\xa0" ], ' ', $instructions ); |
| 115 |
return '' === trim( wp_strip_all_tags( $plain ) ); |
| 116 |
} |
| 117 |
} |
| 118 |
|