PluginProbe
Customify / 2.3.1
Customify v2.3.1
2.10.9 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.7.1 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.0.1 1.6.5 1.7.0 1.7.1 All 77 releases
customify / core / core.php

core.php in Customify 2.3.1, at core/core.php

294 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php defined('ABSPATH') or die;
2
3 /**
4 * @package pixcustomify
5 * @category core
6 * @author Pixel Grade Team
7 * @copyright (c) 2013, Pixel Grade Media
8 */
9 class pixcustomify {
10
11 /** @var array core defaults */
12 protected static $defaults = null;
13
14 /**
15 * @return array
16 */
17 static function defaults() {
18 if (self::$defaults === null) {
19 self::$defaults = include self::corepath().'defaults'.EXT;
20 }
21
22 return self::$defaults;
23 }
24
25 // Simple Dependency Injection Container
26 // ------------------------------------------------------------------------
27
28 /** @var array interface -> implementation mapping */
29 protected static $mapping = array();
30
31 /**
32 * @return mixed instance of class registered for the given interface
33 */
34 static function instance() {
35 $args = func_get_args();
36 $interface = array_shift($args);
37
38 if (isset(self::$mapping[$interface])) {
39 $class = self::$mapping[$interface];
40 }
41 else { // the interface isn't mapped to a class
42 // we fallback to interface name + "Impl" suffix
43 $class = $interface.'Impl';
44 }
45
46 return call_user_func_array(array($class, 'instance'), $args);
47 }
48
49 /**
50 * Registers a class for the given interface. If no class is registered for
51 * an interface the interface name with a Impl suffix is used.
52 */
53 static function use_impl($interface, $class) {
54 self::$mapping[$interface] = $class;
55 }
56
57
58 // Syntactic Sugar
59 // ------------------------------------------------------------------------
60
61 /**
62 * @param array configuration
63 * @return PixCustomifyForm
64 */
65 static function form($config, $processor) {
66 $form = self::instance('PixCustomifyForm', $config);
67 $form->autocomplete($processor->data());
68 $form->errors($processor->errors());
69 return $form;
70 }
71
72 /**
73 * @param array configuration
74 * @return PixCustomifyProcessor
75 */
76 static function processor($config) {
77 return self::instance('PixCustomifyProcessor', $config);
78 }
79
80
81 // Paths
82 // ------------------------------------------------------------------------
83
84 /**
85 * @return string root path for core
86 */
87 static function corepath() {
88 return dirname(__FILE__).DIRECTORY_SEPARATOR;
89 }
90
91 /** @var string plugin path */
92 protected static $pluginpath = null;
93
94 /**
95 * @return string path
96 */
97 static function pluginpath() {
98 if (self::$pluginpath === null) {
99 self::$pluginpath = realpath(self::corepath().'..').DIRECTORY_SEPARATOR;
100 }
101
102 return self::$pluginpath;
103 }
104
105 /**
106 * Sets a custom plugin path; required in non-standard plugin structures.
107 */
108 static function setpluginpath($path) {
109 self::$pluginpath = $path;
110 }
111
112
113 // Helpers
114 // ------------------------------------------------------------------------
115
116 /**
117 * Hirarchical array merge. Will always return an array.
118 *
119 * @param ... arrays
120 * @return array
121 */
122 static function merge() {
123 $base = array();
124 $args = func_get_args();
125
126 foreach ($args as $arg) {
127 self::array_merge($base, $arg);
128 }
129
130 return $base;
131 }
132
133 /**
134 * Overwrites base array with overwrite array.
135 *
136 * @param array base
137 * @param array overwrite
138 */
139 protected static function array_merge(array &$base, array $overwrite) {
140 foreach ($overwrite as $key => &$value)
141 {
142 if (is_int($key))
143 {
144 // add only if it doesn't exist
145 if ( ! in_array($overwrite[$key], $base))
146 {
147 $base[] = $overwrite[$key];
148 }
149 }
150 // non-int key
151 else if (is_array($value))
152 {
153 if (isset($base[$key]) && is_array($base[$key]))
154 {
155 self::array_merge($base[$key], $value);
156 }
157 else # does not exist or it's a non-array
158 {
159 $base[$key] = $value;
160 }
161 }
162 else # not an array and not numeric key
163 {
164 $base[$key] = $value;
165 }
166 }
167 }
168
169 /**
170 * @param string callback key
171 * @return string callback function name
172 * @throws Exception
173 */
174 static function callback($key, PixCustomifyMeta $meta) {
175 $defaults = pixcustomify::defaults();
176 $default_callbacks = $defaults['callbacks'];
177 $plugin_callbacks = $meta->get('callbacks', array());
178
179 $callbacks = array_merge($default_callbacks, $plugin_callbacks);
180
181 if (isset($callbacks[$key])) {
182 return $callbacks[$key];
183 }
184 else { // missing callback
185 throw new Exception('Missing callback for ['.$key.'].');
186 }
187 }
188
189 /** @var string the translation text domain */
190 protected static $textdomain = 'customify';
191
192 /**
193 * @return string text domain
194 */
195 static function textdomain() {
196 return self::$textdomain;
197 }
198
199 /**
200 * Sets a custom text domain; if null is passed the text domain will revert
201 * to the default text domain.
202 */
203 static function settextdomain($textdomain) {
204 if ( ! empty($textdomain)) {
205 self::$textdomain = $textdomain;
206 }
207 else { // null or otherwise empty value
208 // revert to default
209 self::$textdomain = 'customify';
210 }
211 }
212
213 /**
214 * Recursively finds all files in a directory.
215 *
216 * @param string directory to search
217 * @return array found files
218 */
219 static function find_files($dir)
220 {
221 $found_files = array();
222 $files = scandir($dir);
223
224 foreach ($files as $value) {
225 // skip special dot files and directories
226 if (strpos($value,'.') === 0) {
227 continue;
228 }
229
230 // is it a file?
231 if (is_file("$dir/$value")) {
232 $found_files []= "$dir/$value";
233 continue;
234 }
235 else { // it's a directory
236 foreach (self::find_files("$dir/$value") as $value) {
237 $found_files []= $value;
238 }
239 }
240 }
241
242 return $found_files;
243 }
244
245 /**
246 * Requires all PHP files in a directory.
247 * Use case: callback directory, removes the need to manage callbacks.
248 *
249 * Should be used on a small directory chunks with no sub directories to
250 * keep code clear.
251 *
252 * @param string path
253 */
254 static function require_all($path)
255 {
256 $files = self::find_files(rtrim($path, '\\/'));
257
258 $priority_list = array();
259 foreach ($files as $file) {
260 $priority_list[$file] = self::file_priority($file);
261 }
262
263 asort($priority_list, SORT_ASC);
264
265 foreach ($priority_list as $file => $priority) {
266 if (strpos($file, EXT)) {
267 require_once $file;
268 }
269 }
270 }
271
272 /**
273 * Priority based on path length and number of directories. Files in the
274 * same directory have higher priority if their path is shorter; files in
275 * directories have +100 priority bonus for every directory.
276 *
277 * @param string file path
278 * @return int
279 */
280 protected static function file_priority($path) {
281 $path = str_replace('\\', '/', $path);
282 return strlen($path) + substr_count($path, '/') * 100;
283 }
284
285 static function option( $option, $default = null ) {
286 /** @var PixCustomifyPlugin $local_plugin */
287 $local_plugin = PixCustomifyPlugin();
288
289 return $local_plugin->get_option($option, $default = null);
290
291 }
292
293 } # class
294