PluginProbe
Social Share Buttons / 0.9
Social Share Buttons v0.9
trunk 0.9 1.0 1.1 1.1.1 1.1.2 1.10 1.11 1.12 1.15 1.16 1.17 1.18 1.19 1.2 1.20 1.3 1.30 1.4 1.5 1.6 1.7 1.8 1.9
share-button / libraries / simple_template.php

simple_template.php in Social Share Buttons 0.9, at libraries/simple_template.php

141 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace MBSocial;
3
4 defined('ABSPATH') or die('No direct access permitted');
5
6 class simpleTemplate
7 {
8 private $version = '1.1.2';
9
10 static $object = null;
11 static $template = null;
12
13 public static function parse($template_file, $object)
14 {
15 self::$object = $object;
16
17 if ($template_file == '')
18 {
19 MI()->errors()->add( new \Exception ('Template file is empty') );
20 return false;
21 }
22 $template = file_get_contents($template_file);
23
24 $template = static::checkif($template, $object);
25 $template = static::checkfor($template, $object);
26 $template = static::checkvar($template, $object);
27
28 self::$template = $template;
29 return $template;
30 }
31
32 public static function get_last_object()
33 {
34 return self::$object;
35 }
36
37 public static function checkvar($template, $object)
38 {
39 preg_match_all('/%%(.*?)%%/im', $template, $matches);
40
41 if(isset($matches[1]) && count($matches[1]) > 0)
42 {
43 for($i = 0; $i < count($matches[1]); $i++)
44 {
45 $match = $matches[1][$i];
46 $replace = $matches[0][$i];
47
48 if (! isset($object->$match))
49 continue;
50
51 $template = str_replace($replace, $object->$match, $template);
52 }
53 }
54 return $template;
55 }
56
57 public static function checkif($template, $object)
58 {
59 $count = preg_match_all('/\{if:(.*)\}(.*)\{\/if:(.*)\}/i', $template, $matches);
60
61 if (! isset($matches[0]) || $count == 0)
62 return $template; // no statements;
63
64 /* matches[0] = full statement
65 matches[1] = name of field + possible value
66 matches[2] = inner content
67 */
68
69 for($i = 0; $i < $count; $i++)
70 {
71
72 $full = $matches[0][$i];
73 $field = $matches[1][$i];
74 $value = null;
75
76 if (strstr($field, '=')) // check for value construct
77 {
78 list($field, $value) = explode('=', $field);
79 }
80
81 $content = $matches[2][$i];
82
83 if (isset($object->$field) && ($value == null || $object->$field == $value) )
84 {
85 $template = str_replace($full, $content, $template);
86 }
87 else
88 {
89 $template = str_replace($full, '', $template);
90 }
91 }
92 return $template;
93 }
94
95 public static function checkfor($template, $object)
96 {
97 $count = preg_match_all('/\{for:(.*)\}(.*)\{\/for:(.*)\}/is', $template, $matches);
98
99 if (! isset($matches[0]) || $count == 0)
100 return $template; // no statements;
101
102 for ($i = 0; $i < $count; $i++)
103 {
104 $content = '';
105 $field = $matches[1][$i];
106 $repeatline = $matches[2][$i];
107
108 if (isset($object->$field))
109 {
110 foreach($object->$field as $key => $item)
111 {
112 if (is_array($item))
113 {
114 $line = $repeatline;
115
116 foreach($item as $subkey => $subitem)
117 {
118 $line = str_replace('%%' . $subkey . '%%',$subitem, $line);
119
120 }
121 $line = str_replace('%%key%%', $key, $line);
122 $content .= $line;
123 }
124 else
125 {
126 $line = str_replace('%%key%%',$key, $repeatline);
127 $content .= str_replace('%%item%%', $item, $line);
128 }
129 }
130
131 }
132 $template = str_replace($matches[0][$i], $content, $template);
133
134 }
135
136 return $template;
137 }
138
139
140 }
141