Completed
Push — master ( ab4944...534e9c )
by Andrew
02:30
created

ClassyConfig::get_vars()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 37
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 37
rs 8.439
1
<?php 
2
3
/**
4
 * Theme Config
5
 *
6
 * Loads theme config and registers models based on it
7
 */
8
class ClassyConfig {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
10
	protected static $vars = null;
11
12
	/**
13
	 * Returns list of allowed variables that can be used in theme config
14
	 * 
15
	 * @return array
16
	 */
17
	private static function get_allowed_variables() {
18
		
19
		return array('environment', 'textdomain', 'post_types', 'taxonomies', 'post_formats', 'sidebars');;
20
21
	}
22
23
	/**
24
	 * Requires config file variables
25
	 * 
26
	 * @return void
27
	 */
28
	public static function get_vars() {
29
30
		if ( null === self::$vars ) {
31
32
			// Check for a theme config
33
			$config_file = THEME_FRAMEWORK_PATH . 'config.php';
34
35
			if (file_exists($config_file)) {
36
37
				require_once THEME_FRAMEWORK_PATH . 'config.php';
38
39
				$vars = self::get_allowed_variables();
40
41
				foreach ($vars as $var) {
42
43
					if (isset($$var)) {
44
45
						self::$vars[$var] = $$var;
46
47
						unset($$var); // We don't require it anymore
48
49
					}
50
51
				}
52
53
			} else {
54
55
				die('There is no config file in ' . THEME . ' custom/config.php');
0 ignored issues
show
Coding Style Compatibility introduced by
The method get_vars() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
56
57
			}
58
59
60
		}
61
62
		return self::$vars;
63
64
	}
65
66
67
68
	/**
69
	 * Retrieves config variables and then inits wordpress functionality based on them
70
	 */
71
	public static function init() {
72
73
		$vars = self::get_vars();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $vars is correct as self::get_vars() (which targets ClassyConfig::get_vars()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
74
75
		// Init Post Types
76
		if (isset($vars['post_types'])) self::init_post_types( $vars['post_types'] );
77
78
		// Init Taxonomies
79
		if (isset($vars['taxonomies'])) self::init_taxonomies( $vars['taxonomies'] );
80
81
		// Init Post Formats
82
		if (isset($vars['post_formats'])) self::init_post_formats( $vars['post_formats'] );
83
84
		// Init Sidebars
85
		if (isset($vars['sidebars'])) self::init_sidebars( $vars['sidebars'] );
86
		
87
	}
88
89
90
91
	/**
92
	 * Registers Post Types
93
	 * 
94
	 * @param  array $post_types
95
	 */
96 View Code Duplication
	private static function init_post_types( $post_types ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
98
		if (is_array($post_types)) {
99
100
			foreach ( $post_types as $type => $options ) {
101
			
102
				self::add_post_type( $type, $options['config'], $options['singular'], $options['multiple'] );
103
			
104
			}
105
			
106
		}
107
108
	}
109
110
111
	/**
112
	 * Register Post Type Wrapper
113
	 * 
114
	 * @param string $name
115
	 * @param array $config
116
	 * @param string $singular
117
	 * @param string $multiple
118
	 */
119
	private static function add_post_type( $name, $config, $singular = 'Entry', $multiple = 'Entries' ) {
120
121
		$domain = Classy::textdomain();
122
		
123
		if ( !isset($config['labels']) ) {
124
125
			$config['labels'] = array(
126
				'name' => __($multiple, $domain),
127
				'singular_name' => __($singular, $domain),
128
				'not_found'=> __('No ' . $multiple . ' Found', $domain),
129
				'not_found_in_trash'=> __('No ' . $multiple . ' found in Trash', $domain),
130
				'edit_item' => __('Edit ', $singular, $domain),
131
				'search_items' => __('Search ' . $multiple, $domain),
132
				'view_item' => __('View ', $singular, $domain),
133
				'new_item' => __('New ' . $singular, $domain),
134
				'add_new' => __('Add New', $domain),
135
				'add_new_item' => __('Add New ' . $singular, $domain),
136
			);
137
138
		}
139
140
		register_post_type($name, $config);
141
142
	}
143
144
	/**
145
	 * Registers taxonomies
146
	 * 
147
	 * @param  array $taxonomies
148
	 */
149 View Code Duplication
	private static function init_taxonomies( $taxonomies ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
151
		if (is_array($taxonomies)) {
152
153
			foreach ( $taxonomies as $type => $options ) {
154
			
155
				self::add_taxonomy( $type, $options['for'], $options['config'], $options['singular'], $options['multiple'] );
156
			
157
			}
158
			
159
		}
160
161
		
162
	}
163
164
	/**
165
	 * Register taxonomy wrapper
166
	 * 
167
	 * @param string $name
168
	 * @param mixed $object_type
169
	 * @param array $config
170
	 * @param string $singular
171
	 * @param string $multiple
172
	 */
173
	private static function add_taxonomy( $name, $object_type, $config, $singular = 'Entry', $multiple = 'Entries' ) {
174
175
		$domain = Classy::textdomain();
176
		
177
		if ( !isset($config['labels']) ) {
178
179
			$config['labels'] = array(
180
				'name' => __($multiple, $domain),
181
				'singular_name' => __($singular, $domain),
182
				'search_items' =>  __('Search ' . $multiple, $domain),
183
				'all_items' => __('All ' . $multiple, $domain),
184
				'parent_item' => __('Parent ' . $singular, $domain),
185
				'parent_item_colon' => __('Parent ' . $singular . ':', $domain),
186
				'edit_item' => __('Edit ' . $singular, $domain), 
187
				'update_item' => __('Update ' . $singular, $domain),
188
				'add_new_item' => __('Add New ' . $singular, $domain),
189
				'new_item_name' => __('New ' . $singular . ' Name', $domain),
190
				'menu_name' => __($singular, $domain),
191
			);
192
193
		}
194
		
195
196
		register_taxonomy($name, $object_type, $config);
197
	
198
	}
199
200
	/**
201
	 * Registers Post Formats
202
	 * 
203
	 * @param  array $post_formats
204
	 */
205
	private static function init_post_formats( $post_formats ) {
206
207
		if (is_array($post_formats)) {
208
209
			add_theme_support('post-formats', $post_formats);
210
			
211
		}
212
213
214
	}
215
216
	/**
217
	 * Registers Sidebars
218
	 * 
219
	 * @param  array $sidebars
220
	 */
221
	private static function init_sidebars( $sidebars ) {
222
223
		$domain = Classy::textdomain();
224
225
		if (is_array($sidebars)) {
226
	
227
			foreach ( $sidebars as $id => $title ) {
228
				
229
				register_sidebar(
230
					array(
231
						'id' => $id,
232
						'name' => __($title, $domain),
233
						'description' => __($title, $domain),
234
						'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="widget-inner">',
235
						'after_widget' => '</div></div>',
236
						'before_title' => '<h3>',
237
						'after_title' => '</h3>'
238
					)
239
				);
240
241
			}
242
			
243
		}
244
245
	}
246
247
}
248