← All changes
|
includes/endpoints/class-charitable-campaign-donation-endpoint.php
+103
-26
1.6.7
→
1.8.13
View file →
| @@ -2,13 +2,13 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Donate endpoint. |
| 4 | 4 | * |
| 5 | 5 | * @package Charitable/Classes/Charitable_Campaign_Donation_Endpoint |
| 6 | - * @author Eric Daams | |
| 7 | - * @copyright Copyright (c) 2018, Studio 164a | |
| 6 | + * @author David Bisset | |
| 7 | + * @copyright Copyright (c) 2023, WP Charitable LLC | |
| 8 | 8 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 | 9 | * @since 1.5.0 |
| 10 | - * @version 1.5.4 | |
| 10 | + * @version 1.6.55 | |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | 14 | exit; |
| @@ -18,17 +18,23 @@ | ||
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * Charitable_Campaign_Donation_Endpoint |
| 21 | 21 | * |
| 22 | - * @abstract | |
| 23 | - * @since 1.5.0 | |
| 22 | + * @since 1.5.0 | |
| 24 | 23 | */ |
| 25 | 24 | class Charitable_Campaign_Donation_Endpoint extends Charitable_Endpoint { |
| 26 | 25 | |
| 26 | + /** Endpoint ID. */ | |
| 27 | + const ID = 'campaign_donation'; | |
| 28 | + | |
| 27 | 29 | /** |
| 28 | - * @var string | |
| 30 | + * Whether to force HTTPS on the endpoint. | |
| 31 | + * | |
| 32 | + * @since 1.6.14 | |
| 33 | + * | |
| 34 | + * @var boolean | |
| 29 | 35 | */ |
| 30 | - const ID = 'campaign_donation'; | |
| 36 | + private $force_https; | |
| 31 | 37 | |
| 32 | 38 | /** |
| 33 | 39 | * Object instantiation. |
| 34 | 40 | * |
| @@ -35,8 +41,17 @@ | ||
| 35 | 41 | * @since 1.5.4 |
| 36 | 42 | */ |
| 37 | 43 | public function __construct() { |
| 38 | 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 ); | |
| 39 | 54 | } |
| 40 | 55 | |
| 41 | 56 | /** |
| 42 | 57 | * Return the endpoint ID. |
| @@ -59,9 +74,9 @@ | ||
| 59 | 74 | } |
| 60 | 75 | |
| 61 | 76 | /** |
| 62 | 77 | * Return the endpoint URL. |
| 63 | - * | |
| 78 | + * | |
| 64 | 79 | * @since 1.5.0 |
| 65 | 80 | * |
| 66 | 81 | * @global WP_Rewrite $wp_rewrite |
| 67 | 82 | * @param array $args Mixed args. |
| @@ -69,19 +84,33 @@ | ||
| 69 | 84 | */ |
| 70 | 85 | public function get_page_url( $args = array() ) { |
| 71 | 86 | global $wp_rewrite; |
| 72 | 87 | |
| 73 | - $campaign_id = array_key_exists( 'campaign_id', $args ) ? $args['campaign_id'] : get_the_ID(); | |
| 74 | - $campaign_url = get_permalink( $campaign_id ); | |
| 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 | + } | |
| 75 | 95 | |
| 76 | - if ( 'same_page' == charitable_get_option( 'donation_form_display', 'separate_page' ) ) { | |
| 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' ) ) { | |
| 77 | 103 | return $campaign_url; |
| 78 | 104 | } |
| 79 | 105 | |
| 80 | 106 | if ( $wp_rewrite->using_permalinks() |
| 81 | - && ! in_array( get_post_status( $campaign_id ), array( 'pending', 'draft' ) ) | |
| 82 | - && ! isset( $_GET['preview'] ) ) { | |
| 83 | - return trailingslashit( $campaign_url ) . 'donate/'; | |
| 107 | + && ! in_array( get_post_status( $args['campaign_id'] ), array( 'pending', 'draft' ) ) | |
| 108 | + && ! isset( $_GET['preview'] ) ) { // phpcs:ignore | |
| 109 | + | |
| 110 | + $url = wp_parse_url( $campaign_url ); | |
| 111 | + | |
| 112 | + return $this->sanitize_endpoint_url( $campaign_url, 'donate' ); | |
| 84 | 113 | } |
| 85 | 114 | |
| 86 | 115 | return esc_url_raw( add_query_arg( array( 'donate' => 1 ), $campaign_url ) ); |
| 87 | 116 | } |
| @@ -97,8 +126,12 @@ | ||
| 97 | 126 | */ |
| 98 | 127 | public function is_page( $args = array() ) { |
| 99 | 128 | global $wp_query; |
| 100 | 129 | |
| 130 | + if ( is_null( $wp_query->get_queried_object() ) ) { | |
| 131 | + return false; | |
| 132 | + } | |
| 133 | + | |
| 101 | 134 | if ( ! $wp_query->is_singular( Charitable::CAMPAIGN_POST_TYPE ) ) { |
| 102 | 135 | return false; |
| 103 | 136 | } |
| 104 | 137 | |
| @@ -110,20 +143,19 @@ | ||
| 110 | 143 | if ( $this->is_strict_check( $args ) ) { |
| 111 | 144 | return false; |
| 112 | 145 | } |
| 113 | 146 | |
| 114 | - return 'separate_page' != charitable_get_option( 'donation_form_display', 'separate_page' ); | |
| 147 | + return 'separate_page' !== charitable_get_option( 'donation_form_display', 'separate_page' ); | |
| 115 | 148 | } |
| 116 | 149 | |
| 117 | 150 | /** |
| 118 | - * Return the template to display for this endpoint. | |
| 151 | + * Possibly redirect elsewhere when viewing the endpoint. | |
| 119 | 152 | * |
| 120 | - * @since 1.5.0 | |
| 153 | + * @since 1.6.55 | |
| 121 | 154 | * |
| 122 | - * @param string $template The default template. | |
| 123 | - * @return string | |
| 155 | + * @return void | |
| 124 | 156 | */ |
| 125 | - public function get_template( $template ) { | |
| 157 | + public function setup_template() { | |
| 126 | 158 | $campaign_id = charitable_get_current_campaign_id(); |
| 127 | 159 | |
| 128 | 160 | /** |
| 129 | 161 | * If the campaign doesn't exist or can no longer receive donations, |
| @@ -133,21 +165,50 @@ | ||
| 133 | 165 | wp_safe_redirect( get_permalink( $campaign_id ) ); |
| 134 | 166 | exit(); |
| 135 | 167 | } |
| 136 | 168 | |
| 169 | + if ( $this->force_https && ! is_ssl() ) { | |
| 170 | + wp_safe_redirect( charitable_get_permalink( 'campaign_donation' ) ); | |
| 171 | + exit(); | |
| 172 | + } | |
| 173 | + | |
| 137 | 174 | $donation_id = get_query_var( 'donation_id', false ); |
| 138 | 175 | |
| 139 | 176 | /* If a donation ID is included, make sure it belongs to the current user. */ |
| 140 | 177 | if ( $donation_id && ! charitable_user_can_access_donation( $donation_id ) ) { |
| 141 | - wp_safe_redirect( charitable_get_permalink( 'campaign_donation', array( | |
| 142 | - 'campaign_id' => $campaign_id, | |
| 143 | - ) ) ); | |
| 178 | + wp_safe_redirect( | |
| 179 | + charitable_get_permalink( | |
| 180 | + 'campaign_donation', | |
| 181 | + array( | |
| 182 | + 'campaign_id' => $campaign_id, | |
| 183 | + ) | |
| 184 | + ) | |
| 185 | + ); | |
| 144 | 186 | exit(); |
| 145 | 187 | } |
| 146 | 188 | |
| 147 | - do_action( 'charitable_is_donate_page' ); | |
| 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 | + } | |
| 148 | 200 | |
| 149 | - return array( 'campaign-donation-page.php', 'page.php', 'index.php' ); | |
| 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' ); | |
| 150 | 211 | } |
| 151 | 212 | |
| 152 | 213 | /** |
| 153 | 214 | * Get the content to display for the endpoint. |
| @@ -153,9 +214,9 @@ | ||
| 153 | 214 | * Get the content to display for the endpoint. |
| 154 | 215 | * |
| 155 | 216 | * @since 1.5.0 |
| 156 | 217 | * |
| 157 | - * @param string $content | |
| 218 | + * @param string $content Default content. | |
| 158 | 219 | * @return string |
| 159 | 220 | */ |
| 160 | 221 | public function get_content( $content ) { |
| 161 | 222 | if ( ! charitable_is_main_loop() ) { |
| @@ -164,8 +225,24 @@ | ||
| 164 | 225 | |
| 165 | 226 | if ( 'separate_page' != charitable_get_option( 'donation_form_display', 'separate_page' ) |
| 166 | 227 | && false === get_query_var( 'donate', false ) ) { |
| 167 | 228 | return $content; |
| 229 | + } | |
| 230 | + | |
| 231 | + /* | |
| 232 | + * On block themes, Charitable_Endpoints::template_loader() returns early | |
| 233 | + * (uses_custom_template() is false), so setup_template() never runs and the | |
| 234 | + * 'charitable_is_donate_page' action never fires. Without it, URL parameters | |
| 235 | + * such as ?amount= and ?period= are never added to the session, and the form | |
| 236 | + * falls back to the campaign default instead of the requested amount. Fire it | |
| 237 | + * here for block themes so the form can pre-select the requested amount. | |
| 238 | + */ | |
| 239 | + if ( charitable_is_block_theme() ) { | |
| 240 | + $campaign_id = charitable_get_current_campaign_id(); | |
| 241 | + $donation_id = get_query_var( 'donation_id', false ); | |
| 242 | + | |
| 243 | + /** This action is documented in includes/endpoints/class-charitable-campaign-donation-endpoint.php */ | |
| 244 | + do_action( 'charitable_is_donate_page', $campaign_id, $donation_id ); | |
| 168 | 245 | } |
| 169 | 246 | |
| 170 | 247 | ob_start(); |
| 171 | 248 | |