PluginProbe
Customify / 1.2.3
Customify v1.2.3
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 1.2.3, at core/core.php

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