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