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