| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions relating to our use of handlebars. |
| 4 |
* |
| 5 |
* @package ForceRefresh |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace JordanLeven\Plugins\ForceRefresh; |
| 9 |
|
| 10 |
use LightnCandy as ZordiusLightnCandy; |
| 11 |
|
| 12 |
// The location of the handlebars directory relative to the plugin root. |
| 13 |
define( 'FORCE_REFRESH_HANDLEBARS_DIRECTORY', '/dist/handlebars/' ); |
| 14 |
|
| 15 |
/** |
| 16 |
* Function used to add a handlebars template to the DOM (to be used by JavaScript). |
| 17 |
* |
| 18 |
* @param string $id The ID used to identify the handlebars template. |
| 19 |
* @param string $src The location of the handlebars template relative to the |
| 20 |
* predefined handlebars template directory. |
| 21 |
*/ |
| 22 |
function add_handlebars( $id, $src ) { |
| 23 |
// Get the directory of the plugin. |
| 24 |
$directory = get_force_refresh_plugin_directory(); |
| 25 |
// Get the location of the Handlebars template. |
| 26 |
$file_location = $directory . FORCE_REFRESH_HANDLEBARS_DIRECTORY . $src; |
| 27 |
if ( file_exists( $file_location ) ) { |
| 28 |
// phpcs:disable WordPress.WP.AlternativeFunctions |
| 29 |
// In the future, this method of loading handlebars templates will be deprecated as the |
| 30 |
// front-end moves towards Vue. |
| 31 |
$handlebar_contents = file_get_contents( $file_location ); |
| 32 |
// phpcs:enable WordPress.WP.AlternativeFunctions |
| 33 |
echo '<script id="' . esc_html( $id ) . '" type="text/x-handlebars-template">'; |
| 34 |
// phpcs:disable WordPress.Security.EscapeOutput |
| 35 |
// In the future, this method of loading handlebars templates will be deprecated as the |
| 36 |
// front-end moves towards Vue. |
| 37 |
echo $handlebar_contents; |
| 38 |
// phpcs:enable WordPress.Security.EscapeOutput |
| 39 |
echo '</script>'; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Function to render handlebars in PHP. Users the Handlebars framework (via Composer). |
| 45 |
* |
| 46 |
* @param string $template_name The name of the template. |
| 47 |
* @param array $replacements_array The replacements. |
| 48 |
* @param boolean $return Whether to return the HTML or print it. |
| 49 |
* |
| 50 |
* @return string The HTML |
| 51 |
*/ |
| 52 |
function render_handlebars( |
| 53 |
$template_name, |
| 54 |
$replacements_array = array(), |
| 55 |
$return = false |
| 56 |
) { |
| 57 |
// Get the directory of the plugin. |
| 58 |
$directory = get_force_refresh_plugin_directory(); |
| 59 |
// Add the template directory to the replacements. |
| 60 |
$replacements_array['template_directory_uri'] = get_template_directory_uri(); |
| 61 |
$file_location = $directory |
| 62 |
. FORCE_REFRESH_HANDLEBARS_DIRECTORY |
| 63 |
. $template_name; |
| 64 |
if ( file_exists( $file_location ) ) { |
| 65 |
// Get the file contents. |
| 66 |
// phpcs:disable WordPress.WP.AlternativeFunctions |
| 67 |
// In the future, this method of loading handlebars templates will be deprecated as the |
| 68 |
// front-end moves towards Vue. |
| 69 |
$handlebar_contents = file_get_contents( $file_location ); |
| 70 |
// phpcs:enable WordPress.WP.AlternativeFunctions |
| 71 |
$php = ZordiusLightnCandy\LightnCandy::compile( |
| 72 |
$handlebar_contents, |
| 73 |
array( |
| 74 |
'flags' => ZordiusLightnCandy\LightnCandy::FLAG_RENDER_DEBUG |
| 75 |
| ZordiusLightnCandy\LightnCandy::FLAG_HANDLEBARSJS, |
| 76 |
) |
| 77 |
); |
| 78 |
// Get the render function. |
| 79 |
$renderer = ZordiusLightnCandy\LightnCandy::prepare( $php ); |
| 80 |
$rendered_html = $renderer( $replacements_array ); |
| 81 |
// If return is true, then return the HTML. |
| 82 |
if ( $return ) { |
| 83 |
return $rendered_html; |
| 84 |
} |
| 85 |
// Otherwise, echo it. |
| 86 |
// phpcs:disable WordPress.Security.EscapeOutput |
| 87 |
// In the future, this method of loading handlebars templates will be deprecated as the |
| 88 |
// front-end moves towards Vue. |
| 89 |
echo $rendered_html; |
| 90 |
// phpcs:disable WordPress.Security.EscapeOutput |
| 91 |
} |
| 92 |
} |
| 93 |
|