| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle iframe skin. |
| 4 |
* |
| 5 |
* @package Give |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Give\Views; |
| 9 |
|
| 10 |
use Give\Form\Template; |
| 11 |
use Give\Helpers\Form\Template as FormTemplateUtils; |
| 12 |
use Give\Helpers\Form\Utils as FormUtils; |
| 13 |
use Give\Helpers\Utils as GlobalUtils; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class IframeView |
| 17 |
* |
| 18 |
* Note: only for internal use. |
| 19 |
* |
| 20 |
* @package Give |
| 21 |
* @since 2.7.0 |
| 22 |
*/ |
| 23 |
class IframeView |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Iframe URL. |
| 27 |
* |
| 28 |
* This will be use to setup src attribute. |
| 29 |
* |
| 30 |
* @since 2.7.0 |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected $url; |
| 34 |
|
| 35 |
/** |
| 36 |
* Flag to check whether show modal or iframe on page. |
| 37 |
* |
| 38 |
* @since 2.7.0 |
| 39 |
* @var bool |
| 40 |
*/ |
| 41 |
protected $modal = false; |
| 42 |
|
| 43 |
/** |
| 44 |
* Flag to check whether on not auto scroll page to iframe. |
| 45 |
* |
| 46 |
* @since 2.7.0 |
| 47 |
* @var int |
| 48 |
*/ |
| 49 |
protected $autoScroll = 0; |
| 50 |
|
| 51 |
/** |
| 52 |
* Iframe minimum height. |
| 53 |
* |
| 54 |
* @since 2.7.0 |
| 55 |
* @var bool |
| 56 |
*/ |
| 57 |
protected $minHeight = null; |
| 58 |
|
| 59 |
/** |
| 60 |
* Unique identifier. |
| 61 |
* |
| 62 |
* @var string|null |
| 63 |
*/ |
| 64 |
protected $uniqueId = null; |
| 65 |
|
| 66 |
/** |
| 67 |
* Button title. |
| 68 |
* |
| 69 |
* @var string|null |
| 70 |
*/ |
| 71 |
protected $buttonTitle = null; |
| 72 |
|
| 73 |
/** |
| 74 |
* Button color. |
| 75 |
* |
| 76 |
* @var string|null |
| 77 |
*/ |
| 78 |
protected $buttonColor = null; |
| 79 |
|
| 80 |
/** |
| 81 |
* Form template. |
| 82 |
* |
| 83 |
* @var Template |
| 84 |
*/ |
| 85 |
protected $template = null; |
| 86 |
|
| 87 |
/** |
| 88 |
* Form id. |
| 89 |
* |
| 90 |
* @var int |
| 91 |
*/ |
| 92 |
protected $formId = 0; |
| 93 |
|
| 94 |
/** |
| 95 |
* IframeView Constructor |
| 96 |
* |
| 97 |
* @param Template $template |
| 98 |
*/ |
| 99 |
public function __construct($template = null) |
| 100 |
{ |
| 101 |
$this->uniqueId = uniqid('give-'); |
| 102 |
$this->buttonTitle = esc_html__('Click to donate', 'give'); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Set iframe URL. |
| 107 |
* |
| 108 |
* @param string $url |
| 109 |
* |
| 110 |
* @return $this |
| 111 |
*/ |
| 112 |
public function setURL($url = null) |
| 113 |
{ |
| 114 |
$this->url = esc_url( |
| 115 |
add_query_arg( |
| 116 |
['giveDonationFormInIframe' => 1], |
| 117 |
$url |
| 118 |
) |
| 119 |
); |
| 120 |
|
| 121 |
return $this; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Set whether or not show modal. |
| 126 |
* |
| 127 |
* @param bool $set |
| 128 |
* |
| 129 |
* @return $this |
| 130 |
*/ |
| 131 |
public function showInModal($set) |
| 132 |
{ |
| 133 |
$this->modal = $set; |
| 134 |
|
| 135 |
return $this; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Button title. |
| 140 |
* |
| 141 |
* @param string $title |
| 142 |
* |
| 143 |
* @return $this |
| 144 |
*/ |
| 145 |
public function setButtonTitle($title) |
| 146 |
{ |
| 147 |
$this->buttonTitle = $title; |
| 148 |
|
| 149 |
return $this; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Button color. |
| 154 |
* |
| 155 |
* @param string $color |
| 156 |
* |
| 157 |
* @return $this |
| 158 |
*/ |
| 159 |
public function setButtonColor($color) |
| 160 |
{ |
| 161 |
$this->buttonColor = $color; |
| 162 |
|
| 163 |
return $this; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Form id. |
| 168 |
* |
| 169 |
* @param int $id |
| 170 |
* |
| 171 |
* @return $this |
| 172 |
*/ |
| 173 |
public function setFormId($id) |
| 174 |
{ |
| 175 |
$this->formId = $id; |
| 176 |
|
| 177 |
return $this; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Return whether or not rest api request to render donation form block. |
| 182 |
* |
| 183 |
* @since 2.7.0 |
| 184 |
* @return bool |
| 185 |
*/ |
| 186 |
private function isDonationFormBlockRendererApiRequest() |
| 187 |
{ |
| 188 |
return false !== strpos( |
| 189 |
$_SERVER['REQUEST_URI'], |
| 190 |
rest_get_url_prefix() . '/wp/v2/block-renderer/give/donation-form' |
| 191 |
); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Extra extra query param to iframe url. |
| 196 |
* |
| 197 |
* @since 2.7.0 |
| 198 |
*/ |
| 199 |
private function addExtraQueryParams() |
| 200 |
{ |
| 201 |
// We can prevent live donation on in appropriate situation like: previewing donation form (with draft status) |
| 202 |
if (FormTemplateUtils\Utils\Frontend::getPreviewDonationFormId( |
| 203 |
) || $this->isDonationFormBlockRendererApiRequest()) { |
| 204 |
$this->url = esc_url( |
| 205 |
add_query_arg( |
| 206 |
['giveDisableDonateNowButton' => 1], |
| 207 |
$this->url |
| 208 |
) |
| 209 |
); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Get iframe HTML. |
| 215 |
* |
| 216 |
* @return string |
| 217 |
*/ |
| 218 |
private function getIframeHTML() |
| 219 |
{ |
| 220 |
ob_start(); |
| 221 |
|
| 222 |
$this->template->renderLoadingView($this->formId); |
| 223 |
|
| 224 |
$loader = sprintf( |
| 225 |
'<div class="iframe-loader">%1$s</div>', |
| 226 |
ob_get_clean() |
| 227 |
); |
| 228 |
|
| 229 |
$iframe = sprintf( |
| 230 |
'<iframe |
| 231 |
name="give-embed-form" |
| 232 |
%1$s |
| 233 |
%4$s |
| 234 |
data-autoScroll="%2$s" |
| 235 |
onload="if( \'undefined\' !== typeof Give ) { Give.initializeIframeResize(this) }" |
| 236 |
style="border: 0;visibility: hidden;%3$s"></iframe>%5$s', |
| 237 |
$this->modal ? "data-src=\"{$this->url}\"" : "src=\"{$this->url}\"", |
| 238 |
$this->autoScroll, |
| 239 |
$this->minHeight ? "min-height: {$this->minHeight}px;" : '', |
| 240 |
$this->modal ? 'class="in-modal"' : '', |
| 241 |
$loader |
| 242 |
); |
| 243 |
|
| 244 |
/** |
| 245 |
* @since 4.3.0 remove modal-inner-wrap element to achieve style parity with v3 modals. |
| 246 |
*/ |
| 247 |
if ($this->modal) { |
| 248 |
$iframe = sprintf( |
| 249 |
' |
| 250 |
<div class="modal-content"> |
| 251 |
<a href="#" class="close-btn js-give-embed-form-modal-closer" aria-label="%3$s" data-form-id="%3$s" rel="nofollow">%2$s |
| 252 |
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false" class="givewp-donation-form-modal__close__icon"><path stroke="black" stroke-width="2" d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.6 6.5 6.6 1-1z"></path></svg> |
| 253 |
</a> |
| 254 |
%1$s |
| 255 |
</div> |
| 256 |
', |
| 257 |
$iframe, |
| 258 |
'', |
| 259 |
$this->uniqueId |
| 260 |
); |
| 261 |
} |
| 262 |
|
| 263 |
return $iframe; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Get button HTML. |
| 268 |
* |
| 269 |
* @since 4.3.0 add v3 button classname to match v3 modals. |
| 270 |
* @since 3.7.0 Escape attributes |
| 271 |
* |
| 272 |
* @return string |
| 273 |
*/ |
| 274 |
private function getButtonHTML() |
| 275 |
{ |
| 276 |
return sprintf( |
| 277 |
'<div class="js-give-embed-form-modal-launcher-wrap"> |
| 278 |
<button |
| 279 |
type="button" |
| 280 |
class="js-give-embed-form-modal-opener givewp-donation-form-modal__open" |
| 281 |
data-form-id="%1$s"%3$s |
| 282 |
>%2$s</button> |
| 283 |
</div>', |
| 284 |
esc_attr($this->uniqueId), |
| 285 |
esc_html($this->buttonTitle), |
| 286 |
$this->buttonColor ? ' style="background-color: ' . esc_attr($this->buttonColor) . '"' : '' |
| 287 |
); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Get iframe URL. |
| 292 |
* |
| 293 |
* @return string |
| 294 |
*/ |
| 295 |
private function getIframeURL() |
| 296 |
{ |
| 297 |
$query_string = array_map('give_clean', wp_parse_args($_SERVER['QUERY_STRING'])); |
| 298 |
$donationHistory = give_get_purchase_session(); |
| 299 |
$hasAction = !empty($query_string['giveDonationAction']); |
| 300 |
$this->autoScroll = absint($hasAction); |
| 301 |
$donationFormHasSession = null; |
| 302 |
|
| 303 |
if ($donationHistory) { |
| 304 |
$donationFormHasSession = $this->formId === absint($donationHistory['post_data'] ['give-form-id']); |
| 305 |
} |
| 306 |
|
| 307 |
// Do not pass donation acton by query param if does not belong to current form. |
| 308 |
if ( |
| 309 |
$hasAction && |
| 310 |
!empty($donationHistory) && |
| 311 |
!$donationFormHasSession |
| 312 |
) { |
| 313 |
unset($query_string['giveDonationAction']); |
| 314 |
$hasAction = false; |
| 315 |
$this->autoScroll = 0; |
| 316 |
} |
| 317 |
|
| 318 |
// Build iframe url. |
| 319 |
$url = Give()->routeForm->getURL(get_post_field('post_name', $this->formId)); |
| 320 |
|
| 321 |
if (($hasAction && 'showReceipt' === $query_string['giveDonationAction']) || FormUtils::isViewingFormReceipt( |
| 322 |
)) { |
| 323 |
$url = FormUtils::getSuccessPageURL(); |
| 324 |
} elseif (($hasAction && 'failedDonation' === $query_string['giveDonationAction'])) { |
| 325 |
$url = $this->template->getFailedPageURL($this->formId); |
| 326 |
$query_string['showFailedDonationError'] = 1; |
| 327 |
} |
| 328 |
|
| 329 |
$iframe_url = add_query_arg( |
| 330 |
array_merge(['giveDonationFormInIframe' => 1], $query_string), |
| 331 |
$url |
| 332 |
); |
| 333 |
|
| 334 |
return GlobalUtils::removeDonationAction($iframe_url); |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Setup Default config. |
| 339 |
*/ |
| 340 |
private function loadDefaultConfig() |
| 341 |
{ |
| 342 |
$activeFormTemplate = FormTemplateUtils::getActiveID($this->formId); |
| 343 |
$this->template = Give()->templates->getTemplate($activeFormTemplate); |
| 344 |
$this->minHeight = $this->template->getFormStartingHeight($this->formId); |
| 345 |
|
| 346 |
$this->url = $this->url ?: $this->getIframeURL(); |
| 347 |
|
| 348 |
$this->addExtraQueryParams(); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Render view. |
| 353 |
* |
| 354 |
* Note: if you want to overwrite this function then do not forget to add action hook in footer and header. |
| 355 |
* We use these hooks to manipulated donation form related actions. |
| 356 |
* |
| 357 |
* @since 2.7.0 |
| 358 |
*/ |
| 359 |
public function render() |
| 360 |
{ |
| 361 |
ob_start(); |
| 362 |
|
| 363 |
$this->loadDefaultConfig(); |
| 364 |
|
| 365 |
if ($this->modal) { |
| 366 |
echo $this->getButtonHTML(); |
| 367 |
} |
| 368 |
|
| 369 |
printf( |
| 370 |
'<div class="give-embed-form-wrapper%1$s" id="%2$s">%3$s</div>', |
| 371 |
$this->modal ? ' is-hide' : '', |
| 372 |
$this->uniqueId, |
| 373 |
$this->getIframeHTML() |
| 374 |
); |
| 375 |
|
| 376 |
return ob_get_clean(); |
| 377 |
} |
| 378 |
} |
| 379 |
|