| 1 |
<?php |
| 2 |
/** |
| 3 |
* Donate endpoint. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Campaign_Donation_Endpoint |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2022, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.5.0 |
| 10 |
* @version 1.6.55 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Campaign_Donation_Endpoint' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Campaign_Donation_Endpoint |
| 21 |
* |
| 22 |
* @since 1.5.0 |
| 23 |
*/ |
| 24 |
class Charitable_Campaign_Donation_Endpoint extends Charitable_Endpoint { |
| 25 |
|
| 26 |
/** Endpoint ID. */ |
| 27 |
const ID = 'campaign_donation'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Whether to force HTTPS on the endpoint. |
| 31 |
* |
| 32 |
* @since 1.6.14 |
| 33 |
* |
| 34 |
* @var boolean |
| 35 |
*/ |
| 36 |
private $force_https; |
| 37 |
|
| 38 |
/** |
| 39 |
* Object instantiation. |
| 40 |
* |
| 41 |
* @since 1.5.4 |
| 42 |
*/ |
| 43 |
public function __construct() { |
| 44 |
$this->cacheable = false; |
| 45 |
|
| 46 |
/** |
| 47 |
* Whether to force HTTPS on the donation endpoint. |
| 48 |
* |
| 49 |
* @since 1.6.14 |
| 50 |
* |
| 51 |
* @param boolean $force_https Whether HTTPS is forced for the donation endpoint. |
| 52 |
*/ |
| 53 |
$this->force_https = apply_filters( 'charitable_campaign_donation_endpoint_force_https', false ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Return the endpoint ID. |
| 58 |
* |
| 59 |
* @since 1.5.0 |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
public static function get_endpoint_id() { |
| 64 |
return self::ID; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Add rewrite rules for the endpoint. |
| 69 |
* |
| 70 |
* @since 1.5.0 |
| 71 |
*/ |
| 72 |
public function setup_rewrite_rules() { |
| 73 |
add_rewrite_endpoint( 'donate', EP_PERMALINK ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Return the endpoint URL. |
| 78 |
* |
| 79 |
* @since 1.5.0 |
| 80 |
* |
| 81 |
* @global WP_Rewrite $wp_rewrite |
| 82 |
* @param array $args Mixed args. |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
public function get_page_url( $args = array() ) { |
| 86 |
global $wp_rewrite; |
| 87 |
|
| 88 |
/** |
| 89 |
* A campaign ID must be passed for us to get a |
| 90 |
* campaign donation endpoint URL. |
| 91 |
*/ |
| 92 |
if ( ! isset( $args['campaign_id'] ) ) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
$campaign_url = get_permalink( $args['campaign_id'] ); |
| 97 |
|
| 98 |
if ( $this->force_https ) { |
| 99 |
$campaign_url = str_replace( 'http://', 'https://', $campaign_url ); |
| 100 |
} |
| 101 |
|
| 102 |
if ( 'same_page' === charitable_get_option( 'donation_form_display', 'separate_page' ) ) { |
| 103 |
return $campaign_url; |
| 104 |
} |
| 105 |
|
| 106 |
if ( $wp_rewrite->using_permalinks() |
| 107 |
&& ! in_array( get_post_status( $args['campaign_id'] ), array( 'pending', 'draft' ) ) |
| 108 |
&& ! isset( $_GET['preview'] ) ) { |
| 109 |
|
| 110 |
$url = parse_url( $campaign_url ); |
| 111 |
|
| 112 |
return $this->sanitize_endpoint_url( $campaign_url, 'donate' ); |
| 113 |
} |
| 114 |
|
| 115 |
return esc_url_raw( add_query_arg( array( 'donate' => 1 ), $campaign_url ) ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Return whether we are currently viewing the endpoint. |
| 120 |
* |
| 121 |
* @since 1.5.0 |
| 122 |
* |
| 123 |
* @global WP_Query $wp_query |
| 124 |
* @param array $args Mixed args. |
| 125 |
* @return boolean |
| 126 |
*/ |
| 127 |
public function is_page( $args = array() ) { |
| 128 |
global $wp_query; |
| 129 |
|
| 130 |
if ( is_null( $wp_query->get_queried_object() ) ) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
if ( ! $wp_query->is_singular( Charitable::CAMPAIGN_POST_TYPE ) ) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
if ( array_key_exists( 'donate', $wp_query->query_vars ) ) { |
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
/* If 'strict' is set to `true`, this will only return true if this has the /donate/ endpoint. */ |
| 143 |
if ( $this->is_strict_check( $args ) ) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
return 'separate_page' !== charitable_get_option( 'donation_form_display', 'separate_page' ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Possibly redirect elsewhere when viewing the endpoint. |
| 152 |
* |
| 153 |
* @since 1.6.55 |
| 154 |
* |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
public function setup_template() { |
| 158 |
$campaign_id = charitable_get_current_campaign_id(); |
| 159 |
|
| 160 |
/** |
| 161 |
* If the campaign doesn't exist or can no longer receive donations, |
| 162 |
* redirect the user to the campaign page. |
| 163 |
*/ |
| 164 |
if ( ! charitable_campaign_can_receive_donations( $campaign_id ) ) { |
| 165 |
wp_safe_redirect( get_permalink( $campaign_id ) ); |
| 166 |
exit(); |
| 167 |
} |
| 168 |
|
| 169 |
if ( $this->force_https && ! is_ssl() ) { |
| 170 |
wp_safe_redirect( charitable_get_permalink( 'campaign_donation' ) ); |
| 171 |
exit(); |
| 172 |
} |
| 173 |
|
| 174 |
$donation_id = get_query_var( 'donation_id', false ); |
| 175 |
|
| 176 |
/* If a donation ID is included, make sure it belongs to the current user. */ |
| 177 |
if ( $donation_id && ! charitable_user_can_access_donation( $donation_id ) ) { |
| 178 |
wp_safe_redirect( |
| 179 |
charitable_get_permalink( |
| 180 |
'campaign_donation', |
| 181 |
array( |
| 182 |
'campaign_id' => $campaign_id, |
| 183 |
) |
| 184 |
) |
| 185 |
); |
| 186 |
exit(); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Do something when the donate page is loaded. |
| 191 |
* |
| 192 |
* @since 1.0.0 |
| 193 |
* @since 1.6.25 Added $campaign_id and $donation_id parameters. |
| 194 |
* |
| 195 |
* @param int $campaign_id The campaign receiving the donation. |
| 196 |
* @param int $donation_id The donation id, if this is an update to an existing donation. |
| 197 |
*/ |
| 198 |
do_action( 'charitable_is_donate_page', $campaign_id, $donation_id ); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Return the template to display for this endpoint. |
| 203 |
* |
| 204 |
* @since 1.5.0 |
| 205 |
* |
| 206 |
* @param string $template The default template. |
| 207 |
* @return array |
| 208 |
*/ |
| 209 |
public function get_template( $template ) { |
| 210 |
return array( 'campaign-donation-page.php', 'page.php', 'singular.php', 'index.php' ); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Get the content to display for the endpoint. |
| 215 |
* |
| 216 |
* @since 1.5.0 |
| 217 |
* |
| 218 |
* @param string $content Default content. |
| 219 |
* @return string |
| 220 |
*/ |
| 221 |
public function get_content( $content ) { |
| 222 |
if ( ! charitable_is_main_loop() ) { |
| 223 |
return $content; |
| 224 |
} |
| 225 |
|
| 226 |
if ( 'separate_page' != charitable_get_option( 'donation_form_display', 'separate_page' ) |
| 227 |
&& false === get_query_var( 'donate', false ) ) { |
| 228 |
return $content; |
| 229 |
} |
| 230 |
|
| 231 |
ob_start(); |
| 232 |
|
| 233 |
charitable_template( 'content-donation-form.php', array() ); |
| 234 |
|
| 235 |
return ob_get_clean(); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Returns whether the current request is for a campaign that can receive donations. |
| 240 |
* |
| 241 |
* @since 1.5.4 |
| 242 |
* |
| 243 |
* @return boolean |
| 244 |
*/ |
| 245 |
protected function is_donation_ready_campaign() { |
| 246 |
return charitable_campaign_can_receive_donations( charitable_get_current_campaign_id() ); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Returns whether the passed donation is valid for the current user. |
| 251 |
* |
| 252 |
* @since 1.5.4 |
| 253 |
* |
| 254 |
* @param int $donation_id The donation ID in query vars. |
| 255 |
* @return boolean |
| 256 |
*/ |
| 257 |
protected function is_invalid_donation( $donation_id ) { |
| 258 |
return ! charitable_user_can_access_donation( $donation_id ); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Returns whether this is a strict check. |
| 263 |
* |
| 264 |
* @since 1.5.4 |
| 265 |
* |
| 266 |
* @param array $args Mixed args. |
| 267 |
* @return boolean |
| 268 |
*/ |
| 269 |
protected function is_strict_check( $args ) { |
| 270 |
return array_key_exists( 'strict', $args ) && $args['strict']; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
endif; |
| 275 |
|