| 1 |
<?php |
| 2 |
/** |
| 3 |
* Donation receipt endpoint. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Donation_Receipt_Endpoint |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.5.0 |
| 10 |
* @version 1.8.9.1 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Donation_Receipt_Endpoint' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Donation_Receipt_Endpoint |
| 21 |
* |
| 22 |
* @since 1.5.0 |
| 23 |
*/ |
| 24 |
class Charitable_Donation_Receipt_Endpoint extends Charitable_Endpoint { |
| 25 |
|
| 26 |
/** Endpoint ID. */ |
| 27 |
const ID = 'donation_receipt'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Return the endpoint ID. |
| 31 |
* |
| 32 |
* @since 1.5.0 |
| 33 |
* |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
public static function get_endpoint_id() { |
| 37 |
return self::ID; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Add rewrite rules for the endpoint. |
| 42 |
* |
| 43 |
* @since 1.5.0 |
| 44 |
*/ |
| 45 |
public function setup_rewrite_rules() { |
| 46 |
add_rewrite_endpoint( 'donation_receipt', EP_ROOT ); |
| 47 |
add_rewrite_rule( 'donation-receipt/([0-9]+)/?$', 'index.php?donation_id=$matches[1]&donation_receipt=1', 'top' ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Return the endpoint URL. |
| 52 |
* |
| 53 |
* @since 1.5.0 |
| 54 |
* |
| 55 |
* @global WP_Rewrite $wp_rewrite |
| 56 |
* @param array $args Mixed page arguments. |
| 57 |
* @return string |
| 58 |
*/ |
| 59 |
public function get_page_url( $args = array() ) { |
| 60 |
global $wp_rewrite; |
| 61 |
|
| 62 |
$receipt_page = charitable_get_option( 'donation_receipt_page', 'auto' ); |
| 63 |
|
| 64 |
$donation_id = isset( $args['donation_id'] ) ? $args['donation_id'] : get_the_ID(); |
| 65 |
|
| 66 |
if ( 'auto' !== $receipt_page ) { |
| 67 |
return esc_url_raw( add_query_arg( array( 'donation_id' => $donation_id ), get_permalink( $receipt_page ) ) ); |
| 68 |
} |
| 69 |
|
| 70 |
if ( $wp_rewrite->using_permalinks() ) { |
| 71 |
$url = $this->sanitize_endpoint_url( |
| 72 |
home_url(), |
| 73 |
'donation-receipt/' . $donation_id |
| 74 |
); |
| 75 |
} else { |
| 76 |
$url = esc_url_raw( |
| 77 |
add_query_arg( |
| 78 |
array( |
| 79 |
'donation_receipt' => 1, |
| 80 |
'donation_id' => $donation_id, |
| 81 |
), |
| 82 |
home_url() |
| 83 |
) |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
return $url; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Return whether we are currently viewing the endpoint. |
| 92 |
* |
| 93 |
* @since 1.5.0 |
| 94 |
* |
| 95 |
* @global WP_Query $wp_query |
| 96 |
* @param array $args Mixed arguments. |
| 97 |
* @return boolean |
| 98 |
*/ |
| 99 |
public function is_page( $args = array() ) { |
| 100 |
global $wp_query; |
| 101 |
|
| 102 |
$receipt_page = charitable_get_option( 'donation_receipt_page', 'auto' ); |
| 103 |
|
| 104 |
if ( 'auto' !== $receipt_page ) { |
| 105 |
return is_page( $receipt_page ); |
| 106 |
} |
| 107 |
|
| 108 |
return is_main_query() |
| 109 |
&& isset( $wp_query->query_vars['donation_receipt'] ) |
| 110 |
&& isset( $wp_query->query_vars['donation_id'] ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Whether the endpoint uses a custom template. |
| 115 |
* If this returns true, the donation receipt page will use default template if option is set to auto. |
| 116 |
* |
| 117 |
* @since 1.6.55 |
| 118 |
* @since 1.8.1 Removed check for block theme. |
| 119 |
* @version 1.8.9.1 |
| 120 |
* |
| 121 |
* @return boolean |
| 122 |
*/ |
| 123 |
public function uses_custom_template() { |
| 124 |
if ( array_key_exists( 'print', $_GET ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
return 'auto' === charitable_get_option( 'donation_receipt_page', 'auto' ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Prepare the template for the endpoint. |
| 133 |
* |
| 134 |
* @since 1.6.55 |
| 135 |
* |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
public function setup_template() { |
| 139 |
/** |
| 140 |
* Filter the title of the donation receipt page. |
| 141 |
* |
| 142 |
* @since 1.4.0 |
| 143 |
* |
| 144 |
* @param string $title The page title. |
| 145 |
*/ |
| 146 |
$donation_receipt_page_title = apply_filters( 'charitable_donation_receipt_page_title', __( 'Your Receipt', 'charitable' ) ); |
| 147 |
|
| 148 |
new Charitable_Ghost_Page( |
| 149 |
'donation-receipt-page', |
| 150 |
array( |
| 151 |
'title' => $donation_receipt_page_title, |
| 152 |
'content' => '<!-- ' . __( 'Silence is golden', 'charitable' ) . ' -->', |
| 153 |
) |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Return the default template for this endpoint. |
| 159 |
* |
| 160 |
* @since 1.5.0 |
| 161 |
* |
| 162 |
* @param string $template The default template. |
| 163 |
* @return array |
| 164 |
*/ |
| 165 |
public function get_template( $template ) { |
| 166 |
return array( 'donation-receipt-page.php', 'page.php', 'singular.php', 'index.php' ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Get the content to display for the endpoint. |
| 171 |
* |
| 172 |
* @since 1.5.0 |
| 173 |
* |
| 174 |
* @param string $content The default content to display. |
| 175 |
* @return string |
| 176 |
*/ |
| 177 |
public function get_content( $content ) { |
| 178 |
if ( ! in_the_loop() ) { |
| 179 |
return $content; |
| 180 |
} |
| 181 |
|
| 182 |
/* If we are NOT using the automatic option, this is a static page with the shortcode, so don't filter again. */ |
| 183 |
if ( 'auto' !== charitable_get_option( 'donation_receipt_page', 'auto' ) ) { |
| 184 |
return $content; |
| 185 |
} |
| 186 |
|
| 187 |
return charitable_template_donation_receipt_output( $content ); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Return the body class to add for the endpoint. |
| 192 |
* |
| 193 |
* @since 1.5.0 |
| 194 |
* |
| 195 |
* @return string |
| 196 |
*/ |
| 197 |
public function get_body_class() { |
| 198 |
return 'campaign-donation-receipt'; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
endif; |
| 203 |
|