calderawpmetaplatecorehelper_loader

Class helper_loader.

Defined (1)

The class is defined in the following location(s).

/vendor/calderawp/metaplate-core/src/helper_loader.php  
  1. class helper_loader { 
  2. /** 
  3. * Handlebars.php class instance 
  4. * @var obj|\Handlebars\Handlebars Handlebars.php class instance 
  5. */ 
  6. public $handlebars; 
  7.  
  8. /** 
  9. * Constructor for class. 
  10. * @param obj|\Handlebars\Handlebars $handlebars Handlebars.php class instance 
  11. * @param array $helpers { 
  12. * Name, class & callback for the helper. 
  13. * @type string $name Name of helper to use in Handlebars. 
  14. * @type string $class Class containing callback function. 
  15. * @type string $callback. Optional. The name of the callback function. If not set, "helper" will be used. 
  16. * } 
  17. * @return obj|\Handlebars\Handlebars $handlebars Handlebars.php class instance 
  18. */ 
  19. function __construct( $handlebars, $helpers ) { 
  20. if ( is_a( $handlebars, 'Handlebars\Handlebars' ) ) { 
  21. $this->handlebars = $handlebars; 
  22. $this->add_helpers( $helpers ); 
  23.  
  24.  
  25. return $this->handlebars; 
  26.  
  27.  
  28. /** 
  29. * Add the helpers to the Handlebars instance, if validation passes. 
  30. * @param array $helpers Array of helpers to add 
  31. */ 
  32. private function add_helpers( $helpers ) { 
  33. foreach( $helpers as $helper ) { 
  34. $helper = $this->validate_helper_input( $helper ); 
  35. if ( is_array( $helper ) ) { 
  36. $this->add_helper( $helper ); 
  37.  
  38.  
  39.  
  40. /** 
  41. * Add a single helper to the Handlebars instance. 
  42. * @param array $helper Array of information for this helper. 
  43. */ 
  44. private function add_helper( $helper ) { 
  45. $this->handlebars->addHelper( 
  46. $helper[ 'name' ],  
  47. array( 
  48. $helper[ 'class' ],  
  49. $helper[ 'callback' ] 
  50. ); 
  51.  
  52. /** 
  53. * Make sure each helper passed in is valid. 
  54. * @todo ensure helper class/callback are callable. Preferably, without creating object of the class. 
  55. * @param array $helper Array to add helper with. 
  56. * @return bool|array Returns the array if valid, false if not. 
  57. */ 
  58. private function validate_helper_input( $helper ) { 
  59. if ( ! is_array( $helper ) || empty( $helper ) ) { 
  60. return false; 
  61.  
  62. if ( ! isset( $helper[ 'name' ] ) || ! isset( $helper[ 'class' ] ) ) { 
  63. return false; 
  64.  
  65. //if not set, set callback name to "helper" 
  66. if ( ! isset( $helper[ 'callback' ] ) ) { 
  67. $helper[ 'callback' ] = 'helper'; 
  68.  
  69. return $helper; 
  70.  
  71.  
  72.