LegacyConsumer
1 year ago
Template
3 years ago
LoadTemplate.php
4 years ago
Template.php
1 year ago
Templates.php
3 years ago
Template.php
349 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Handle basic setup of form template |
| 5 | * |
| 6 | * @package Give |
| 7 | * @since 2.7.0 |
| 8 | */ |
| 9 | |
| 10 | namespace Give\Form; |
| 11 | |
| 12 | use Give\DonationForms\Models\DonationForm; |
| 13 | use Give\Form\Template\Options; |
| 14 | use Give\Helpers\Form\Utils; |
| 15 | use Give\Helpers\Form\Utils as FormUtils; |
| 16 | use Give\Receipt\DonationReceipt; |
| 17 | |
| 18 | defined('ABSPATH') || exit; |
| 19 | |
| 20 | /** |
| 21 | * Template class. |
| 22 | * |
| 23 | * @since 2.7.0 |
| 24 | */ |
| 25 | abstract class Template |
| 26 | { |
| 27 | /** |
| 28 | * Flag to check whether or not open success page in iframe. |
| 29 | * |
| 30 | * @var bool $openSuccessPageInIframe If set to false then success page will open in window instead of iframe. |
| 31 | */ |
| 32 | public $openSuccessPageInIframe = true; |
| 33 | |
| 34 | /** |
| 35 | * Flag to check whether or not open failed page in iframe. |
| 36 | * |
| 37 | * @var bool $openFailedPageInIframe If set to false then failed page will open in window instead of iframe. |
| 38 | */ |
| 39 | public $openFailedPageInIframe = true; |
| 40 | |
| 41 | /** |
| 42 | * Determines how the form is rendered to the page. |
| 43 | * |
| 44 | * Acceptable values: |
| 45 | * - button (show a button that displays the form when clicked) |
| 46 | * - onpage (render the form right on the page) |
| 47 | * |
| 48 | * @var string |
| 49 | */ |
| 50 | public $donationFormStyle = 'button'; |
| 51 | |
| 52 | /** |
| 53 | * Determines how the form amount choices are rendered to the page. |
| 54 | * |
| 55 | * Acceptable values: |
| 56 | * - buttons (render donation amount choices as button ) |
| 57 | * - radio (render donation amount choices as radio ) |
| 58 | * - dropdown (render donation amount choices in dropdown (select) ) |
| 59 | * |
| 60 | * @var string |
| 61 | */ |
| 62 | public $donationFormLevelsStyle = 'buttons'; |
| 63 | |
| 64 | /** |
| 65 | * Determines how the form field labels render on the page. |
| 66 | * |
| 67 | * Acceptable values: |
| 68 | * - true (render with floating label style) |
| 69 | * - false (render as is) |
| 70 | * |
| 71 | * @var bool |
| 72 | */ |
| 73 | public $floatingLabelsStyle = false; |
| 74 | |
| 75 | /** |
| 76 | * Determines whether or not render form content on page. |
| 77 | * |
| 78 | * Acceptable values: |
| 79 | * - true (render form content on page ) |
| 80 | * - false (do not render form content on page) |
| 81 | * |
| 82 | * @var bool |
| 83 | */ |
| 84 | public $showDonationIntroductionContent = false; |
| 85 | |
| 86 | /** |
| 87 | * Get starting form height |
| 88 | * |
| 89 | * Returns starting height for iframe (in pixels), this is used to predict iframe height before the iframe loads |
| 90 | * Implemented in includes/shortcodes.php: |
| 91 | * |
| 92 | * @param int $formId Form ID |
| 93 | * |
| 94 | * @return int |
| 95 | **/ |
| 96 | public function getFormStartingHeight($formId) |
| 97 | { |
| 98 | return 600; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get form receipt height |
| 103 | * |
| 104 | * Returns receipt height for iframe (in pixels), this is used to predict iframe height before the iframe loads |
| 105 | * Implemented in includes/shortcodes.php: |
| 106 | * Implemented in form donation processing view |
| 107 | * |
| 108 | * @return int |
| 109 | **/ |
| 110 | public function getFormReceiptHeight() |
| 111 | { |
| 112 | return 977; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get loading view filepath |
| 117 | * |
| 118 | * @since 2.7.0 |
| 119 | * |
| 120 | * @return string |
| 121 | */ |
| 122 | public function getLoadingView() |
| 123 | { |
| 124 | return GIVE_PLUGIN_DIR . 'src/Views/Form/defaultLoadingView.php'; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Renders the loading view |
| 129 | * |
| 130 | * @since 2.9.2 |
| 131 | * |
| 132 | * @param int $formId |
| 133 | */ |
| 134 | public function renderLoadingView($formId = null) |
| 135 | { |
| 136 | include $this->getLoadingView(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get form view filepath |
| 141 | * |
| 142 | * @since 2.7.0 |
| 143 | * |
| 144 | * @return string |
| 145 | */ |
| 146 | public function getFormView() |
| 147 | { |
| 148 | return GIVE_PLUGIN_DIR . 'src/Views/Form/defaultFormTemplate.php'; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get receipt view filepath |
| 153 | * |
| 154 | * @since 2.7.0 |
| 155 | * |
| 156 | * @return string |
| 157 | */ |
| 158 | public function getReceiptView() |
| 159 | { |
| 160 | return GIVE_PLUGIN_DIR . 'src/Views/Form/defaultFormReceiptTemplate.php'; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * return form template ID. |
| 165 | * |
| 166 | * @since 2.7.0 |
| 167 | * @return string |
| 168 | */ |
| 169 | abstract public function getID(); |
| 170 | |
| 171 | /** |
| 172 | * Get form template name. |
| 173 | * |
| 174 | * @since 2.7.0 |
| 175 | * @return string |
| 176 | */ |
| 177 | abstract public function getName(); |
| 178 | |
| 179 | /** |
| 180 | * Get form template image. |
| 181 | * |
| 182 | * @since 2.7.0 |
| 183 | * @return string |
| 184 | */ |
| 185 | abstract public function getImage(); |
| 186 | |
| 187 | /** |
| 188 | * Get options config |
| 189 | * |
| 190 | * @since 2.7.0 |
| 191 | * @return array |
| 192 | */ |
| 193 | abstract public function getOptionsConfig(); |
| 194 | |
| 195 | /** |
| 196 | * Get form template options |
| 197 | * |
| 198 | * @return Options |
| 199 | */ |
| 200 | public function getOptions() |
| 201 | { |
| 202 | return Options::fromArray($this->getOptionsConfig()); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Get failed/cancelled donation message. |
| 207 | * |
| 208 | * @since 2.7.0 |
| 209 | * @return string |
| 210 | */ |
| 211 | public function getFailedDonationMessage() |
| 212 | { |
| 213 | return esc_html__( |
| 214 | 'We\'re sorry, your donation failed to process. Please try again or contact site support.', |
| 215 | 'give' |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Get failed donation page URL. |
| 221 | * |
| 222 | * @since 2.7.0 |
| 223 | * |
| 224 | * @param int $formId |
| 225 | * |
| 226 | * @return mixed |
| 227 | */ |
| 228 | public function getFailedPageURL($formId) |
| 229 | { |
| 230 | return FormUtils::createFailedPageURL(Give()->routeForm->getURL(get_post_field('post_name', $formId))); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Get donate now button label text. |
| 235 | * |
| 236 | * @since 2.7.0 |
| 237 | * @return string |
| 238 | */ |
| 239 | public function getDonateNowButtonLabel() |
| 240 | { |
| 241 | return __('Donate Now', 'give'); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Get continue to donation form button label text. |
| 246 | * |
| 247 | * @since 2.7.0 |
| 248 | * @return string |
| 249 | */ |
| 250 | public function getContinueToDonationFormLabel() |
| 251 | { |
| 252 | return __('Donate Now', 'give'); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Get donation introduction text. |
| 257 | * |
| 258 | * @since 2.7.0 |
| 259 | * @return string|null |
| 260 | */ |
| 261 | public function getDonationIntroductionContent() |
| 262 | { |
| 263 | return null; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Return content position on donation form. |
| 268 | * |
| 269 | * Note: Even you are free to add introduction content at any place on donation form |
| 270 | * But still this depends upon form template style and configuration on which places you are allowed to show display introduction content. |
| 271 | * |
| 272 | * @since 2.7.0 |
| 273 | * @return string|null |
| 274 | */ |
| 275 | public function getDonationIntroductionContentPosition() |
| 276 | { |
| 277 | return null; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Get receipt details. |
| 282 | * |
| 283 | * @since 2.7.0 |
| 284 | * |
| 285 | * @param int $donationId |
| 286 | * |
| 287 | * @return DonationReceipt |
| 288 | */ |
| 289 | public function getReceiptDetails($donationId) |
| 290 | { |
| 291 | $receipt = new DonationReceipt($donationId); |
| 292 | |
| 293 | /** |
| 294 | * Fire the action for receipt object. |
| 295 | * |
| 296 | * @since 2.7.0 |
| 297 | */ |
| 298 | do_action('give_new_receipt', $receipt); |
| 299 | |
| 300 | return $receipt; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Get form heading |
| 305 | * |
| 306 | * @since 2.7.0 |
| 307 | * |
| 308 | * @param int $formId |
| 309 | * |
| 310 | * @return string |
| 311 | */ |
| 312 | public function getFormHeading($formId) |
| 313 | { |
| 314 | return get_the_title($formId); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Get form image |
| 319 | * |
| 320 | * @since 2.7.0 |
| 321 | * |
| 322 | * @param int $formId |
| 323 | * |
| 324 | * @return string |
| 325 | */ |
| 326 | public function getFormFeaturedImage($formId) |
| 327 | { |
| 328 | if (Utils::isV3Form($formId)) { |
| 329 | return DonationForm::find($formId)->settings->designSettingsImageUrl; |
| 330 | } |
| 331 | |
| 332 | return get_the_post_thumbnail_url($formId, 'full'); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Get form excerpt |
| 337 | * |
| 338 | * @since 2.7.0 |
| 339 | * |
| 340 | * @param int|null $formId |
| 341 | * |
| 342 | * @return string |
| 343 | */ |
| 344 | public function getFormExcerpt($formId) |
| 345 | { |
| 346 | return get_the_excerpt($formId); |
| 347 | } |
| 348 | } |
| 349 |