This repository has been archived by the owner on Nov 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-custom-field-traits.php
81 lines (58 loc) · 1.98 KB
/
wp-custom-field-traits.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/*
* Plugin Name: WP Custom Field Traits
* Description: A unique OO approach to custom WordPress fields
* Author: Coby Tamayo
* Author URI: http://tamayoweb.net
*/
define('CFT_PLUGIN_DIR', plugin_dir_path(__FILE__));
spl_autoload_register(function($className) {
$path = preg_replace('~[\\\\_]~', '/', $className);
$dir = CFT_PLUGIN_DIR . 'lib/';
if( file_exists("{$dir}/{$path}.php") ) {
require "{$dir}/{$path}.php";
}
});
$_cft_plugin_init = function() {
/*
* Set up sane, overrideable defaults for dependency injection
*/
$plugin = Cft\Plugin::getInstance();
// encapsulate the HTTP request so that we can populate it explicitly,
// with data from anywhere
if( $_POST ) {
$plugin->set('request', $_POST);
}
$plugin->set('validatorBuilder', new Cft\ValidatorBuilder($plugin));
$plugin->set('fieldBuilder', new Cft\FieldBuilder($plugin));
// where to look for view files
$plugin->set('viewDirs', [CFT_PLUGIN_DIR . 'views/']);
// where to cache rendered views
$plugin->set('viewCache', CFT_PLUGIN_DIR . 'view-cache/');
// Twig options
$plugin->set('viewOptions', [
'cache' => $plugin->get('viewCache'),
'debug' => WP_DEBUG,
]);
// how to render views
$plugin->set('view', function() use($plugin) {
$loader = new Twig_Loader_Filesystem($plugin->get('viewDirs'));
$twig = new Twig_Environment($loader, $plugin->get('viewOptions'));
// Twig filter for rendering assoc arrays as HTML attributes
$twig->addFilter( new Twig_SimpleFilter('atts', function(array $atts) {
$htmlAtts = [];
foreach( $atts as $key => $value ) {
// allow for specifying value-less attributes such as "disabled"
// by passing literal true
$htmlAtts[] = $value === true
? $key
: "{$key}=\"{$value}\"";
}
return implode( ' ', $htmlAtts );
}));
return new Cft\View\TwigView( $twig );
});
};
$_cft_plugin_init();
// Clear up the global namespace
unset($_cft_plugin_init);