-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTemplate_Helpers.php
69 lines (62 loc) · 2.05 KB
/
Template_Helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace Your_Namespace\Core\Traits;
use Your_Namespace\Core\Config;
trait Template_Helpers {
use Config_Helpers;
/**
* Remove evil tags: script, style, link, iframe
*
* @param string $html
* @return string The sanitized html
*/
public static function safe_html ( $html ) {
// remove all script and style tags with code
$html = \preg_replace( '/<(script|style)[^>]*?>.*?<\/\\1>/si', '', $html );
// remove any script, style, link and iframe tags
$html = \preg_replace( '/<(script|style|iframe|link)[^>]*?>/si', '', $html );
return $html;
}
/**
* Returns a template rendered with the arguments (2nd parameter).
*
* @param string $template_path
* @param array $args
* @return string - The rendered template
* @throws \Exception
*/
public static function get_template ( $template_path, $args = [] ) {
$args = \apply_filters( self::prefix( 'get_template_args' ), $args, $template_path );
$full_path = self::get_template_path( $template_path );
$html = '';
try {
\extract( $args );
\ob_start();
require $full_path;
$html = \ob_get_clean();
} catch ( \Throwable $e ) {
self::log_critical( $e );
if ( self::get_defined( 'WP_DEBUG' ) && current_user_can( 'administrator' ) ) {
echo '<pre>' . esc_html( wp_slash( "Error while rendering template '$template_path': " . $e->getMessage() ) ) . '</pre>';
} else {
throw new \Exception( $e );
}
}
return $html;
}
/**
* @param string $template_path
* @return string The template absolute path
*/
public static function get_template_path ( $template_path ) {
$template_path .= '.php' === substr( $template_path, -4 ) ? '' : '.php';
$full_path = self::get_templates_dir() . ltrim( $template_path, '/' );
return apply_filters( self::prefix( 'get_template_full_path' ), $full_path, $template_path );
}
/**
* @return string The directory that contains the plugin templates.
*/
public static function get_templates_dir () {
$templates = \trim( Config::get( 'TEMPLATES_DIR', 'templates' ), '/' );
return Config::get( 'DIR' ) . "/{$templates}/";
}
}