Completed
Push — master ( ed64c2...186a7a )
by
unknown
8s
created

Config::init_sidebars()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 3
eloc 13
c 2
b 1
f 1
nc 3
nop 1
dl 0
loc 19
rs 9.4285
1
<?php
2
/**
3
 * Theme Config.
4
 *
5
 * Loads theme config and registers models based on it.
6
 *
7
 * @package Classy
8
 */
9
10
namespace Classy;
11
12
/**
13
 * Class Config.
14
 */
15
class Config {
16
17
	/**
18
	 * Contains all vars from config file.
19
	 *
20
	 * @var null
21
	 */
22
	protected static $vars = null;
23
24
	/**
25
	 * Returns list of allowed variables that can be used in theme config.
26
	 *
27
	 * @return array
28
	 */
29
	private static function get_allowed_variables() {
30
		return array(
31
			'environment',
32
			'textdomain',
33
			'post_types',
34
			'taxonomies',
35
			'post_formats',
36
			'sidebars',
37
		);
38
	}
39
40
	/**
41
	 * Requires config file variables
42
	 *
43
	 * @return array
44
	 */
45
	public static function get_vars() {
46
		if ( is_null( self::$vars ) ) {
47
			// Check for a theme config.
48
			$config_file = CLASSY_THEME_FRAMEWORK_PATH . 'config.php';
49
50
			if ( ! file_exists( $config_file ) ) {
51
				wp_die( sprintf(
52
					'There is no config file in %s custom/config.php',
53
					esc_html( CLASSY_THEME )
54
				) );
55
			}
56
57
			require_once( CLASSY_THEME_FRAMEWORK_PATH . 'config.php' );
58
			$vars = self::get_allowed_variables();
59
60
			foreach ( $vars as $var ) {
61
				if ( isset( $$var ) ) {
62
					self::$vars[ $var ] = $$var;
63
64
					unset( $$var ); // We don't require it anymore.
65
				}
66
			}
67
		}
68
69
		return self::$vars;
70
	}
71
72
	/**
73
	 * Retrieves config variables and then init WordPress functionality based on them.
74
	 */
75
	public static function init() {
76
		$vars = self::get_vars();
77
78
		// Init Post Types.
79
		if ( isset( $vars['post_types'] ) ) {
80
			self::init_post_types( $vars['post_types'] );
81
		}
82
83
		// Init Taxonomies.
84
		if ( isset( $vars['taxonomies'] ) ) {
85
			self::init_taxonomies( $vars['taxonomies'] );
86
		}
87
88
		// Init Post Formats.
89
		if ( isset( $vars['post_formats'] ) ) {
90
			self::init_post_formats( $vars['post_formats'] );
91
		}
92
93
		// Init Sidebars.
94
		if ( isset( $vars['sidebars'] ) ) {
95
			self::init_sidebars( $vars['sidebars'] );
96
		}
97
	}
98
99
	/**
100
	 * Registers Post Types.
101
	 *
102
	 * @param array $post_types Custom post types to be registered.
103
	 */
104 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...
105
		if ( is_array( $post_types ) ) {
106
			foreach ( $post_types as $type => $options ) {
107
				self::add_post_type(
108
					$type,
109
					$options['config'],
110
					$options['singular'],
111
					$options['multiple']
112
				);
113
			}
114
		}
115
	}
116
117
	/**
118
	 * Wrapper for register_post_type().
119
	 *
120
	 * @param string $name     Post type key, must not exceed 20 characters.
121
	 * @param array  $config   Better look into register_post_type() function.
122
	 * @param string $singular Optional. Default singular name.
123
	 * @param string $multiple Optional. Default multiple name.
124
	 */
125
	private static function add_post_type( $name, $config, $singular = 'Entry', $multiple = 'Entries' ) {
126
		$domain = Classy::textdomain();
127
128
		if ( ! isset( $config['labels'] ) ) {
129
			$config['labels'] = array(
130
				'name' => __( $multiple, $domain ),
131
				'singular_name' => __( $singular, $domain ),
132
				'not_found' => __( 'No ' . $multiple . ' Found', $domain ),
133
				'not_found_in_trash' => __( 'No ' . $multiple . ' found in Trash', $domain ),
134
				'edit_item' => __( 'Edit ', $singular, $domain ),
135
				'search_items' => __( 'Search ' . $multiple, $domain ),
136
				'view_item' => __( 'View ', $singular, $domain ),
137
				'new_item' => __( 'New ' . $singular, $domain ),
138
				'add_new' => __( 'Add New', $domain ),
139
				'add_new_item' => __( 'Add New ' . $singular, $domain ),
140
			);
141
		}
142
143
		register_post_type( $name, $config );
144
	}
145
146
	/**
147
	 * Registers taxonomies.
148
	 *
149
	 * @param array $taxonomies Taxonomies to be registered.
150
	 */
151 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...
152
		if ( is_array( $taxonomies ) ) {
153
			foreach ( $taxonomies as $type => $options ) {
154
				self::add_taxonomy(
155
					$type,
156
					$options['for'],
157
					$options['config'],
158
					$options['singular'],
159
					$options['multiple']
160
				);
161
			}
162
		}
163
	}
164
165
	/**
166
	 * Wrapper for register_taxonomy().
167
	 *
168
	 * @param string $name 		  Taxonomy key, must not exceed 32 characters.
169
	 * @param mixed  $object_type Name of the object type for the taxonomy object.
170
	 * @param array  $config	  Better look into register_taxonomy() function.
171
	 * @param string $singular    Optional. Default singular name.
172
	 * @param string $multiple 	  Optional. Default multiple name.
173
	 */
174
	private static function add_taxonomy( $name, $object_type, $config, $singular = 'Entry', $multiple = 'Entries' ) {
175
		$domain = Classy::textdomain();
176
177
		if ( ! isset( $config['labels'] ) ) {
178
			$config['labels'] = array(
179
				'name' => __( $multiple, $domain ),
180
				'singular_name' => __( $singular, $domain ),
181
				'search_items' => __( 'Search ' . $multiple, $domain ),
182
				'all_items' => __( 'All ' . $multiple, $domain ),
183
				'parent_item' => __( 'Parent ' . $singular, $domain ),
184
				'parent_item_colon' => __( 'Parent ' . $singular . ':', $domain ),
185
				'edit_item' => __( 'Edit ' . $singular, $domain ),
186
				'update_item' => __( 'Update ' . $singular, $domain ),
187
				'add_new_item' => __( 'Add New ' . $singular, $domain ),
188
				'new_item_name' => __( 'New ' . $singular . ' Name', $domain ),
189
				'menu_name' => __( $singular, $domain ),
190
			);
191
		}
192
193
		register_taxonomy( $name, $object_type, $config );
194
	}
195
196
	/**
197
	 * Registers Post Formats.
198
	 *
199
	 * @param array $post_formats Array with available post formats.
200
	 */
201
	private static function init_post_formats( $post_formats ) {
202
		if ( is_array( $post_formats ) ) {
203
			add_theme_support( 'post-formats', $post_formats );
204
		}
205
	}
206
207
	/**
208
	 * Wrapper for register_sidebar().
209
	 *
210
	 * @param array $sidebars Sidebars to be registered.
211
	 */
212
	private static function init_sidebars( $sidebars ) {
213
		$domain = Classy::textdomain();
214
215
		if ( is_array( $sidebars ) ) {
216
			foreach ( $sidebars as $id => $title ) {
217
				register_sidebar(
218
					array(
219
						'id' => $id,
220
						'name' => __( $title, $domain ),
221
						'description' => __( $title, $domain ),
222
						'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="widget-inner">',
223
						'after_widget' => '</div></div>',
224
						'before_title' => '<h3>',
225
						'after_title' => '</h3>',
226
					)
227
				);
228
			}
229
		}
230
	}
231
}
232