Template.php
66 lines
| 1 | <?php |
| 2 | namespace Give\Helpers\Form; |
| 3 | |
| 4 | use Give\Form\Template\LegacyFormSettingCompatibility; |
| 5 | use Give\Helpers\Form\Template\Utils\Frontend; |
| 6 | |
| 7 | class Template { |
| 8 | /** |
| 9 | * This function will return selected form template for a specific form. |
| 10 | * |
| 11 | * @param int $formId Form id. Default value: check explanation in ./Utils.php:103 |
| 12 | * |
| 13 | * @return string |
| 14 | * @since 2.7.0 |
| 15 | */ |
| 16 | public static function getActiveID( $formId = null ) { |
| 17 | return Give()->form_meta->get_meta( $formId ?: Frontend::getFormId(), '_give_form_template', true ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Return saved form template settings |
| 22 | * |
| 23 | * @param int $formId |
| 24 | * @param string $templateId |
| 25 | * |
| 26 | * @return array |
| 27 | * @since 2.7.0 |
| 28 | */ |
| 29 | public static function getOptions( $formId = null, $templateId = '' ) { |
| 30 | $formId = $formId ?: Frontend::getFormId(); |
| 31 | $template = $templateId ?: Give()->form_meta->get_meta( $formId, '_give_form_template', true ); |
| 32 | $settings = Give()->form_meta->get_meta( $formId, "_give_{$template}_form_template_settings", true ); |
| 33 | |
| 34 | return $settings ?: []; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Save settings |
| 39 | * |
| 40 | * @sinxe 2.7.0 |
| 41 | * @param $formId |
| 42 | * @param $settings |
| 43 | * |
| 44 | * @return mixed |
| 45 | */ |
| 46 | public static function saveOptions( $formId, $settings ) { |
| 47 | $templateId = Give()->form_meta->get_meta( $formId, '_give_form_template', true ); |
| 48 | |
| 49 | /* @var \Give\Form\Template $template */ |
| 50 | $template = Give()->templates->getTemplate( $templateId ); |
| 51 | |
| 52 | $isUpdated = Give()->form_meta->update_meta( $formId, "_give_{$templateId}_form_template_settings", $settings ); |
| 53 | |
| 54 | /* |
| 55 | * Below code save legacy setting which connected/mapped to form template setting. |
| 56 | * Existing form render on basis of these settings if missed then required output will not generate from give_form_shortcode -> give_get_donation_form function. |
| 57 | * |
| 58 | * Note: We can remove legacy setting compatibility by returning anything except LegacyFormSettingCompatibility class object. |
| 59 | */ |
| 60 | $legacySettingHandler = new LegacyFormSettingCompatibility( $template ); |
| 61 | $legacySettingHandler->save( $formId, $settings ); |
| 62 | |
| 63 | return $isUpdated; |
| 64 | } |
| 65 | } |
| 66 |