calderawpmetaplatecorerender

The Metaplate calderawp metaplate core render class.

Defined (1)

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

/vendor/calderawp/metaplate-core/src/render.php  
  1. class render { 
  2.  
  3. /** 
  4. * Return the content with metaplate applied. 
  5. * @uses "the_content" filter 
  6. * @param string $content Post content 
  7. * @param array|null $meta_stalk Optional. The metaplate to use to render the data. If is null, the default, one will be load, if possible, based on current global $post object. 
  8. * @param array|null $template_data Optional. Prepared field data to render metaplate with. If is null, the default, meta stalk will be retrieved, if possible, based on current global $post object. 
  9. * @param string|null $placement Optional. Where to put the template output, before, after or in place of $content. If is null, option from metaplate is used. Default is--funcitonally speaking--replace. Options: prepend|append|replace|null. 
  10. * @return string Rendered HTML with templates applied--if templates and data were provided. 
  11. */ 
  12. public function render_metaplate( $content, $meta_stack = null, $template_data = null, $placement = null ) { 
  13.  
  14. if ( is_null( $meta_stack ) ) { 
  15. $meta_stack = data::get_active_metaplates(); 
  16.  
  17.  
  18. if( empty( $meta_stack ) ) { 
  19. return $content; 
  20.  
  21.  
  22. if ( is_null( $template_data ) ) { 
  23. global $post; 
  24. $template_data = data::get_custom_field_data( $post->ID ); 
  25.  
  26. if( ! $template_data || empty( $template_data ) ) { 
  27. return $content; 
  28.  
  29.  
  30. // add filter. 
  31. $magic = new filter\magictag(); 
  32. $content = $magic->do_magic_tag( $content ); 
  33.  
  34. $style_data = null; 
  35. $script_data = null; 
  36.  
  37. $engine = new Handlebars; 
  38.  
  39. $engine = $this->helpers( $engine ); 
  40.  
  41. foreach( $meta_stack as $metaplate ) { 
  42.  
  43. // apply filter to data for this metaplate 
  44. $template_data = apply_filters( 'metaplate_data', $template_data, $metaplate ); 
  45. // check CSS 
  46. $style_data .= $engine->render( $metaplate[ 'css' ][ 'code' ], $template_data ); 
  47. // check JS 
  48. $script_data .= $engine->render( $metaplate[ 'js' ][ 'code' ] , $template_data ); 
  49.  
  50. if ( ! is_null( $placement ) ) { 
  51. $metaplate[ 'placement' ] = $placement; 
  52.  
  53. if ( ! isset( $metaplate['placement'] ) || ! in_array( $metaplate['placement'], array( 'prepend', 'append', 'replace' ) ) ) { 
  54. $metaplate['placement'] = 'replace'; 
  55.  
  56. $template = $metaplate[ 'html' ][ 'code' ]; 
  57.  
  58. switch ( $metaplate['placement'] ) { 
  59. case 'prepend': 
  60. $content = $engine->render( $template, $template_data ) . $content; 
  61. break; 
  62. case 'append': 
  63. $content .= $engine->render( $template, $template_data ); 
  64. break; 
  65. case 'replace': 
  66. $content = $engine->render( str_replace( '{{content}}', $content, $template ), $template_data ); 
  67. break; 
  68.  
  69. // insert CSS 
  70. if( !empty( $style_data ) ) { 
  71. $content = '<style>' . $style_data . '</style>' . $content; 
  72. // insert JS 
  73. if( !empty( $script_data ) ) { 
  74. $content .= '<script type="text/javascript">' . $script_data . '</script>'; 
  75.  
  76. // add magic filter. 
  77. $magic = new filter\magictag(); 
  78. $content = $magic->do_magic_tag( $content ); 
  79.  
  80. return $content; 
  81.  
  82.  
  83. /** 
  84. * Register helpers. 
  85. * Adds the default helpers, plus any set on "caldera_metaplate_handlebars_helpers" filter. 
  86. * @param obj|\Handlebars\Handlebars $handlebars Current instance of Handlebars. 
  87. * @return \Handlebars\Handlebars Current instance of Handlebars with the additional helpers added on. 
  88. */ 
  89. private function helpers( $handlebars ) { 
  90. $helpers = $this->default_helpers(); 
  91.  
  92. /** 
  93. * @param array $helpers { 
  94. * Name, class & callback for the helper. 
  95. * @type string $name Name of helper to use in Handlebars. 
  96. * @type string $class Class containing callback function. 
  97. * @type string $callback. Optional. The name of the callback function. If not set, "helper" will be used. 
  98. * } 
  99. * @param obj|\Handlebars\Handlebars $handlebars Handlebars.php class instance 
  100. */ 
  101. $helpers = apply_filters( 'caldera_metaplate_handlebars_helpers', $helpers, $handlebars ); 
  102. $handlebars = new helper_loader( $handlebars, $helpers ); 
  103.  
  104. if ( isset( $handlebars->handlebars ) ) { 
  105. $handlebars = $handlebars->handlebars; 
  106.  
  107. return $handlebars; 
  108.  
  109.  
  110. /** 
  111. * The default helpers. 
  112. * @return array 
  113. */ 
  114. private function default_helpers() { 
  115. return array( 
  116. array( 
  117. 'name' => 'is',  
  118. 'class' => 'calderawp\helpers\is' ),  
  119. array( 
  120. 'name' => '_image',  
  121. 'class' => 'calderawp\helpers\image' ),  
  122. ); 
  123.  
  124.