PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.18.1
GiveWP – Donation Plugin and Fundraising Platform v2.18.1
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / src / Views / IframeView.php
give / src / Views Last commit date
Admin 4 years ago Components 4 years ago Form 4 years ago Admin.php 4 years ago IframeContentView.php 4 years ago IframeView.php 4 years ago Views.php 4 years ago
IframeView.php
366 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 * @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 = add_query_arg(
115 ['giveDonationFormInIframe' => 1],
116 $url
117 );
118
119 return $this;
120 }
121
122 /**
123 * Set whether or not show modal.
124 *
125 * @param bool $set
126 *
127 * @return $this
128 */
129 public function showInModal($set)
130 {
131 $this->modal = $set;
132
133 return $this;
134 }
135
136 /**
137 * Button title.
138 *
139 * @param string $title
140 *
141 * @return $this
142 */
143 public function setButtonTitle($title)
144 {
145 $this->buttonTitle = $title;
146
147 return $this;
148 }
149
150 /**
151 * Button color.
152 *
153 * @param string $color
154 *
155 * @return $this
156 */
157 public function setButtonColor($color)
158 {
159 $this->buttonColor = $color;
160
161 return $this;
162 }
163
164 /**
165 * Form id.
166 *
167 * @param int $id
168 *
169 * @return $this
170 */
171 public function setFormId($id)
172 {
173 $this->formId = $id;
174
175 return $this;
176 }
177
178 /**
179 * Return whether or not rest api request to render donation form block.
180 *
181 * @since 2.7.0
182 * @return bool
183 */
184 private function isDonationFormBlockRendererApiRequest()
185 {
186 return false !== strpos(
187 $_SERVER['REQUEST_URI'],
188 rest_get_url_prefix() . '/wp/v2/block-renderer/give/donation-form'
189 );
190 }
191
192 /**
193 * Extra extra query param to iframe url.
194 *
195 * @since 2.7.0
196 */
197 private function addExtraQueryParams()
198 {
199 // We can prevent live donation on in appropriate situation like: previewing donation form (with draft status)
200 if (FormTemplateUtils\Utils\Frontend::getPreviewDonationFormId(
201 ) || $this->isDonationFormBlockRendererApiRequest()) {
202 $this->url = add_query_arg(
203 ['giveDisableDonateNowButton' => 1],
204 $this->url
205 );
206 }
207 }
208
209 /**
210 * Get iframe HTML.
211 *
212 * @return string
213 */
214 private function getIframeHTML()
215 {
216 ob_start();
217
218 $this->template->renderLoadingView($this->formId);
219
220 $loader = sprintf(
221 '<div class="iframe-loader">%1$s</div>',
222 ob_get_clean()
223 );
224
225 $iframe = sprintf(
226 '<iframe
227 name="give-embed-form"
228 %1$s
229 %4$s
230 data-autoScroll="%2$s"
231 onload="if( \'undefined\' !== typeof Give ) { Give.initializeIframeResize(this) }"
232 style="border: 0;visibility: hidden;%3$s"></iframe>%5$s',
233 $this->modal ? "data-src=\"{$this->url}\"" : "src=\"{$this->url}\"",
234 $this->autoScroll,
235 $this->minHeight ? "min-height: {$this->minHeight}px;" : '',
236 $this->modal ? 'class="in-modal"' : '',
237 $loader
238 );
239
240 if ($this->modal) {
241 $iframe = sprintf(
242 '<div class="modal-inner-wrap">
243 <div class="modal-content">
244 <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>&times;</span></a>
245 %1$s
246 </div>
247 </div>',
248 $iframe,
249 esc_html__('Close', 'give'),
250 $this->uniqueId
251 );
252 }
253
254 return $iframe;
255 }
256
257 /**
258 * Get button HTML.
259 *
260 * @return string
261 */
262 private function getButtonHTML()
263 {
264 return sprintf(
265 '<div class="js-give-embed-form-modal-launcher-wrap">
266 <button
267 type="button"
268 class="js-give-embed-form-modal-opener"
269 data-form-id="%1$s"%3$s>%2$s</button>
270 </div>',
271 $this->uniqueId,
272 $this->buttonTitle,
273 $this->buttonColor ? " style=\"background-color: {$this->buttonColor}\"" : ''
274 );
275 }
276
277 /**
278 * Get iframe URL.
279 *
280 * @return string
281 */
282 private function getIframeURL()
283 {
284 $query_string = array_map('give_clean', wp_parse_args($_SERVER['QUERY_STRING']));
285 $donationHistory = give_get_purchase_session();
286 $hasAction = ! empty($query_string['giveDonationAction']);
287 $this->autoScroll = absint($hasAction);
288 $donationFormHasSession = null;
289
290 if ($donationHistory) {
291 $donationFormHasSession = $this->formId === absint($donationHistory['post_data'] ['give-form-id']);
292 }
293
294 // Do not pass donation acton by query param if does not belong to current form.
295 if (
296 $hasAction &&
297 ! empty($donationHistory) &&
298 ! $donationFormHasSession
299 ) {
300 unset($query_string['giveDonationAction']);
301 $hasAction = false;
302 $this->autoScroll = 0;
303 }
304
305 // Build iframe url.
306 $url = Give()->routeForm->getURL(get_post_field('post_name', $this->formId));
307
308 if (($hasAction && 'showReceipt' === $query_string['giveDonationAction']) || FormUtils::isViewingFormReceipt(
309 )) {
310 $url = FormUtils::getSuccessPageURL();
311 } elseif (($hasAction && 'failedDonation' === $query_string['giveDonationAction'])) {
312 $url = $this->template->getFailedPageURL($this->formId);
313 $query_string['showFailedDonationError'] = 1;
314 }
315
316 $iframe_url = add_query_arg(
317 array_merge(['giveDonationFormInIframe' => 1], $query_string),
318 $url
319 );
320
321 return GlobalUtils::removeDonationAction($iframe_url);
322 }
323
324 /**
325 * Setup Default config.
326 */
327 private function loadDefaultConfig()
328 {
329 $activeFormTemplate = FormTemplateUtils::getActiveID($this->formId);
330 $this->template = Give()->templates->getTemplate($activeFormTemplate);
331 $this->minHeight = $this->template->getFormStartingHeight($this->formId);
332
333 $this->url = $this->url ?: $this->getIframeURL();
334
335 $this->addExtraQueryParams();
336 }
337
338 /**
339 * Render view.
340 *
341 * Note: if you want to overwrite this function then do not forget to add action hook in footer and header.
342 * We use these hooks to manipulated donation form related actions.
343 *
344 * @since 2.7.0
345 */
346 public function render()
347 {
348 ob_start();
349
350 $this->loadDefaultConfig();
351
352 if ($this->modal) {
353 echo $this->getButtonHTML();
354 }
355
356 printf(
357 '<div class="give-embed-form-wrapper%1$s" id="%2$s">%3$s</div>',
358 $this->modal ? ' is-hide' : '',
359 $this->uniqueId,
360 $this->getIframeHTML()
361 );
362
363 return ob_get_clean();
364 }
365 }
366