Stencil_Smarty_3   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 96
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 50 5
A set() 0 9 2
A get() 0 3 1
A fetch() 0 3 1
1
<?php
2
/**
3
 * Engine code
4
 *
5
 * @package Stencil
6
 * @subpackage Smarty3
7
 */
8
9
/**
10
 * Double check to make sure Stencil is loaded
11
 */
12
if ( class_exists( 'Stencil_Implementation' ) ) :
13
14
	/**
15
	 * Create new instance on init
16
	 */
17
	add_action( 'init', create_function( '', 'new Stencil_Smarty_3();' ) );
0 ignored issues
show
Security Best Practice introduced by
The use of create_function is highly discouraged, better use a closure.

create_function can pose a great security vulnerability as it is similar to eval, and could be used for arbitrary code execution. We highly recommend to use a closure instead.

// Instead of
$function = create_function('$a, $b', 'return $a + $b');

// Better use
$function = function($a, $b) { return $a + $b; }
Loading history...
18
19
	/**
20
	 * Class stencil_smarty3
21
	 *
22
	 * Implementation of the "Smarty 3.x" templating engine
23
	 */
24
	class Stencil_Smarty_3 extends Stencil_Implementation {
25
26
		/**
27
		 * Initialize Smarty3 and set defaults
28
		 */
29
		public function __construct() {
30
			parent::__construct();
31
32
			require_once( 'lib/Smarty/Smarty.class.php' );
33
34
			$this->engine = new Smarty();
35
			$this->engine->setCacheDir( $this->cache_path );
36
			$this->engine->setCompileDir( $this->compile_path );
37
			$this->engine->setTemplateDir( $this->template_path );
38
39
			/**
40
			 * For config see:
41
			 * http://www.smarty.net/docs/en/config.files.tpl
42
			 */
43
44
			/*
45
			 * $this->engine->setConfigDir( $template_dir . 'configs/');
46
			 */
47
48
			// Add custom plugins to smarty (per template).
49
			$plugin_dir = apply_filters( 'smarty3-template-plugin-dir', 'smarty-plugins' );
50
51
			/**
52
			 * Add theme plugins & child-theme plugins
53
			 */
54
			if ( ! empty( $plugin_dir ) ) {
55
				$template_root = get_template_directory();
56
				$plugin_bases  = array( $template_root );
57
58
				$child_root = get_stylesheet_directory();
59
				if ( $child_root !== $template_root ) {
60
					$plugin_bases[] = $child_root;
61
				}
62
63
				foreach ( $plugin_bases as $plugin_base ) {
64
					$plugin_dir = implode( DIRECTORY_SEPARATOR, array( $plugin_base, $plugin_dir, '' ) );
65
					if ( is_dir( $plugin_dir ) ) {
66
						$this->engine->addPluginsDir( $plugin_dir );
67
					}
68
				}
69
			}
70
71
			/**
72
			 * Caching - when and how?
73
			 * http://www.smarty.net/docsv2/en/caching.tpl
74
			 */
75
			$this->engine->caching = 0;
76
77
			$this->ready();
78
		}
79
80
		/**
81
		 * Sets the variable to value
82
		 *
83
		 * @param string $variable Variable to set.
84
		 * @param mixed  $value Value to apply.
85
		 *
86
		 * @return mixed|void
87
		 */
88
		public function set( $variable, $value ) {
89
			if ( is_object( $value ) ) {
90
				$this->engine->assignByRef( $variable, $value );
91
			} else {
92
				$this->engine->assign( $variable, $value );
93
			}
94
95
			return $this->get( $variable );
96
		}
97
98
		/**
99
		 * Gets the value of variable
100
		 *
101
		 * @param string $variable Variable to read.
102
		 *
103
		 * @return mixed|string
104
		 */
105
		public function get( $variable ) {
106
			return $this->engine->getTemplateVars( $variable );
107
		}
108
109
		/**
110
		 * Fetches the Smarty compiled template
111
		 *
112
		 * @param string $template Template file to get.
113
		 *
114
		 * @return string
115
		 */
116
		public function fetch( $template ) {
117
			return $this->engine->fetch( $template );
118
		}
119
	}
120
121
endif;
122