Source: templates/class-template.php

  1. <?php
  2. /**
  3. * Abstract template.
  4. *
  5. * @package HivePress\Templates
  6. */
  7. namespace HivePress\Templates;
  8. use HivePress\Helpers as hp;
  9. use HivePress\Traits;
  10. // Exit if accessed directly.
  11. defined( 'ABSPATH' ) || exit;
  12. /**
  13. * Abstract template class.
  14. */
  15. abstract class Template {
  16. use Traits\Mutator;
  17. use Traits\Meta;
  18. use Traits\Context;
  19. /**
  20. * Template blocks.
  21. *
  22. * @var array
  23. */
  24. protected $blocks = [];
  25. /**
  26. * Template attributes.
  27. *
  28. * @var array
  29. */
  30. protected $attributes = [];
  31. /**
  32. * Class initializer.
  33. *
  34. * @param array $meta Class meta values.
  35. */
  36. public static function init( $meta = [] ) {
  37. $meta = hp\merge_arrays(
  38. [
  39. 'name' => hp\get_class_name( static::class ),
  40. ],
  41. $meta
  42. );
  43. // Filter meta.
  44. foreach ( hp\get_class_parents( static::class ) as $class ) {
  45. /**
  46. * Filters the template class meta. The class meta stores properties related to the template type rather than a specific template instance. The dynamic part of the hook refers to the template name (e.g. `listing_view_page`). You can check the available templates in the `includes/templates` directory of HivePress.
  47. *
  48. * @hook hivepress/v1/templates/{template_name}/meta
  49. * @param {array} $meta Class meta values.
  50. * @return {array} Class meta values.
  51. */
  52. $meta = apply_filters( 'hivepress/v1/templates/' . hp\get_class_name( $class ) . '/meta', $meta );
  53. }
  54. // Set meta.
  55. static::set_meta( $meta );
  56. }
  57. /**
  58. * Class constructor.
  59. *
  60. * @param array $args Template arguments.
  61. */
  62. public function __construct( $args = [] ) {
  63. // Filter properties.
  64. foreach ( hp\get_class_parents( static::class ) as $class ) {
  65. /**
  66. * Filters the template properties. The dynamic part of the hook refers to the template name (e.g. `listing_view_page`). You can check the available templates in the `includes/templates` directory of HivePress.
  67. *
  68. * @hook hivepress/v1/templates/{template_name}
  69. * @param {array} $props Template properties.
  70. * @param {object} $template Template object.
  71. * @return {array} Template properties.
  72. */
  73. $args = apply_filters( 'hivepress/v1/templates/' . hp\get_class_name( $class ), $args, $this );
  74. }
  75. // Set properties.
  76. foreach ( $args as $name => $value ) {
  77. $this->set_property( $name, $value );
  78. }
  79. // Bootstrap properties.
  80. $this->boot();
  81. }
  82. /**
  83. * Bootstraps template properties.
  84. */
  85. protected function boot() {
  86. // Filter blocks.
  87. foreach ( hp\get_class_parents( static::class ) as $class ) {
  88. /**
  89. * Filters template blocks. At the time of this hook the template context is already available. The dynamic part of the hook refers to the template name (e.g. `listing_view_page`). You can check the available templates in the `includes/templates` directory of HivePress.
  90. *
  91. * @hook hivepress/v1/templates/{template_name}/blocks
  92. * @param {array} $blocks Template blocks.
  93. * @param {object} $template Template object.
  94. * @return {array} Template blocks.
  95. */
  96. $this->blocks = apply_filters( 'hivepress/v1/templates/' . hp\get_class_name( $class ) . '/blocks', $this->blocks, $this );
  97. }
  98. }
  99. /**
  100. * Gets template blocks.
  101. *
  102. * @return array
  103. */
  104. final public function get_blocks() {
  105. return $this->blocks;
  106. }
  107. /**
  108. * Gets template attributes.
  109. *
  110. * @return array
  111. */
  112. final public function get_attributes() {
  113. return $this->attributes;
  114. }
  115. /**
  116. * Sets object context value.
  117. *
  118. * @param string $name Context name.
  119. * @param mixed $value Context value.
  120. */
  121. final public function set_context( $name, $value = null ) {
  122. if ( is_array( $name ) ) {
  123. $this->context = $name;
  124. } else {
  125. $this->context[ $name ] = $value;
  126. }
  127. }
  128. }