LegacyConsumer
2 years ago
Template
3 years ago
LoadTemplate.php
4 years ago
Template.php
4 years ago
Templates.php
3 years ago
Templates.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Handle Form Templates |
| 5 | * |
| 6 | * @package Give |
| 7 | * @since 2.7.0 |
| 8 | */ |
| 9 | |
| 10 | namespace Give\Form; |
| 11 | |
| 12 | use Give\Helpers\Form\Template as FormTemplateUtils; |
| 13 | use Give\Views\Form\Templates\Classic\Classic; |
| 14 | use Give\Views\Form\Templates\Legacy\Legacy; |
| 15 | use Give\Views\Form\Templates\Sequoia\Sequoia; |
| 16 | |
| 17 | defined('ABSPATH') || exit; |
| 18 | |
| 19 | /** |
| 20 | * Class Templates |
| 21 | * |
| 22 | * @package Give\Form |
| 23 | * |
| 24 | * @since 2.7.0 |
| 25 | */ |
| 26 | class Templates |
| 27 | { |
| 28 | /** |
| 29 | * Templates |
| 30 | * |
| 31 | * @var array |
| 32 | */ |
| 33 | private $templates = []; |
| 34 | |
| 35 | /** |
| 36 | * Template Objects |
| 37 | * |
| 38 | * @var Template[] |
| 39 | */ |
| 40 | private $templateObjs = []; |
| 41 | |
| 42 | /** |
| 43 | * Load templates |
| 44 | * |
| 45 | * @since 2.7.0 |
| 46 | */ |
| 47 | public function load() |
| 48 | { |
| 49 | /** |
| 50 | * Filter list of form template |
| 51 | * |
| 52 | * @since 2.7.0 |
| 53 | * |
| 54 | * @param array $templates |
| 55 | */ |
| 56 | $this->templates = apply_filters( |
| 57 | 'give_register_form_template', |
| 58 | [ |
| 59 | 'sequoia' => Sequoia::class, |
| 60 | 'classic' => Classic::class, |
| 61 | 'legacy' => Legacy::class, |
| 62 | ] |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get Registered templates |
| 68 | * |
| 69 | * @since 2.7.0 |
| 70 | * @return Template[] |
| 71 | */ |
| 72 | public function getTemplates() |
| 73 | { |
| 74 | // Check if all templates have there object or not. |
| 75 | $remainingObjs = array_diff(array_keys($this->templates), array_keys($this->templateObjs)); |
| 76 | |
| 77 | // Get object if any remaining |
| 78 | if ($remainingObjs) { |
| 79 | foreach ($remainingObjs as $templateId) { |
| 80 | $this->templateObjs[$templateId] = $this->getTemplateObject($templateId); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return $this->templateObjs; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get Registered form template |
| 89 | * |
| 90 | * @since 2.7.0 |
| 91 | * |
| 92 | * @param string $templateId Template Id. Default to active form template. |
| 93 | * |
| 94 | * @return Template |
| 95 | */ |
| 96 | public function getTemplate($templateId = null) |
| 97 | { |
| 98 | $templateId = $templateId ?: FormTemplateUtils::getActiveID(); |
| 99 | |
| 100 | if (isset($this->templateObjs[$templateId])) { |
| 101 | return $this->templateObjs[$templateId]; |
| 102 | } |
| 103 | |
| 104 | $this->templateObjs[$templateId] = $this->getTemplateObject($templateId); |
| 105 | |
| 106 | return $this->getTemplateObject($templateId); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get class object. |
| 111 | * |
| 112 | * @since 2.7.0 |
| 113 | * |
| 114 | * @param string $templateId |
| 115 | * |
| 116 | * @return Template |
| 117 | */ |
| 118 | private function getTemplateObject($templateId) |
| 119 | { |
| 120 | return new $this->templates[$templateId](); |
| 121 | } |
| 122 | } |
| 123 |