PluginProbe
Customify / 2.10.9
Customify v2.10.9
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 / includes / admin-settings / core / core.php

core.php in Customify 2.10.9, at includes/admin-settings/core/core.php

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