1: <?php
2: namespace elasticsearch;
3:
4: /**
5: * This class provides a convenient way of accessing configuration values set through the plugin's admin interface.
6: *
7: * @license http://opensource.org/licenses/MIT
8: * @author Paris Holley <[email protected]>
9: * @version 2.0.0
10: **/
11: class Config{
12: private static $options = null;
13:
14: /**
15: * Retrieve a specific option from the wordpress database for this plugin. Note: This is cached once per request.
16: *
17: * @param string $name The name of the option
18: *
19: * @return object
20: **/
21: static function option($name){
22: if(self::$options == null){
23: self::$options = &get_option('elasticsearch');
24: }
25:
26: return self::apply_filters('config_option', isset(self::$options[$name]) ? self::$options[$name] : null, $name);
27: }
28:
29: /**
30: * The score given to a data point that determines the impact on search results. May return null if the setttings have not been saved.
31: *
32: * @param string $type The type of wordpress object that is being scored (tax|field)
33: * @param string $name The slug and/or logical name of that type
34: *
35: * @return integer
36: **/
37: static function score($type, $name){
38: return self::apply_filters('config_score', self::option("score_{$type}_{$name}"), $type, $name);
39: }
40:
41: /**
42: * The numeric ranges that have been defined for a certain field. Example of output:
43: * <code>
44: * array(
45: * '-10' => array(
46: * 'to' => 10
47: * ),
48: * '10-20' => array(
49: * 'from' => 10,
50: * 'to' => 20
51: * )
52: * )
53: * </code>
54: *
55: * @param string $field The field name to lookup
56: *
57: * @return array An associative array where the keys represent a slug and values are used for configuration.
58: **/
59: static function ranges($field){
60: $config = self::option($field . '_range');
61:
62: $val = null;
63:
64: if($config){
65: $ranges = array();
66:
67: foreach(explode(',', $config) as $range){
68: $ends = explode('-', $range);
69:
70: $tmp = array();
71:
72: if(is_numeric($ends[0])){
73: $tmp['from'] = $ends[0];
74: }
75:
76: if(is_numeric($ends[1])){
77: $tmp['to'] = $ends[1];
78: }
79:
80: $ranges[$ends[0] . '-' . $ends[1]] = $tmp;
81: }
82:
83: $val = $ranges;
84: }
85:
86: return self::apply_filters('config_ranges', $val, $field);
87: }
88:
89: /**
90: * Behaves exactly like the wordpress apply_filters method except it prefixes every filter with a convention used by this plugin (ie: 'es_').
91: **/
92: static function apply_filters(){
93: $args = func_get_args();
94: $args[0] = 'elasticsearch_' . $args[0];
95:
96: return call_user_func_array('apply_filters', $args);
97: }
98:
99: /**
100: * A list of a fields that are included when indexing data.
101: *
102: * @return string[] field names
103: **/
104: static function fields(){
105: $fieldnames = Defaults::fields();
106:
107: if($fields = self::option('fields')){
108: $fieldnames = array_keys($fields);
109: }
110:
111: // this should always exist so we have a default to sort on
112: $fieldnames[] = 'post_date';
113:
114: return self::apply_filters('config_fields', $fieldnames);
115: }
116:
117: /**
118: * A list of data points that are used for faceting.
119: *
120: * @return string[] field and/or association names
121: **/
122: static function facets(){
123: return self::apply_filters('config_facets', self::taxonomies());
124: }
125:
126: /**
127: * A list of wordpress post types that are used for indexing.
128: *
129: * @return string[] post type slugs
130: **/
131: static function types(){
132: $types = self::option('types');
133:
134: $val = Defaults::types();
135:
136: if($types){
137: $val = array_keys($types);
138: }
139:
140: return self::apply_filters('config_types', $val);
141: }
142:
143: /**
144: * A list of taxonomies that are used for indexing.
145: *
146: * @return string[] taxonomy slugs
147: **/
148: static function taxonomies(){
149: $taxes = self::option('taxonomies');
150:
151: $val = null;
152:
153: if($taxes){
154: $val = array_keys($taxes);
155: }
156:
157: if($val == null){
158: $val = Defaults::taxonomies(self::types());
159: }
160:
161: return self::apply_filters('config_taxonomies', $val);
162: }
163: }
164: ?>
165: