WordPoints_Importers::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Importers class.
5
 *
6
 * @package WordPoints_Importer
7
 * @since 1.3.0
8
 */
9
10
/**
11
 * Container class for the available importers.
12
 *
13
 * @since 1.0.0
14
 */
15
final class WordPoints_Importers {
16
17
	//
18
	// Private Vars.
19
	//
20
21
	/**
22
	 * The registered importers.
23
	 *
24
	 * @since 1.0.0
25
	 *
26
	 * @type array $importers
27
	 */
28
	private static $importers = array();
29
30
	/**
31
	 * Whether the class has been initialized yet.
32
	 *
33
	 * @since 1.0.0
34
	 *
35
	 * @type bool $initialized
36
	 */
37
	private static $initialized = false;
38
39
	//
40
	// Private Functions.
41
	//
42
43
	/**
44
	 * Initialize the class.
45
	 *
46
	 * @since 1.0.0
47
	 */
48
	private static function init() {
49
50
		// We do this first so we avoid infinite loops if this class is called by a
51
		// function hooked to the below action.
52
		self::$initialized = true;
53
54
		/**
55
		 * Register importers.
56
		 *
57
		 * @since 1.0.0
58
		 */
59
		do_action( 'wordpoints_register_importers' );
60
	}
61
62
	//
63
	// Public Functions.
64
	//
65
66
	/**
67
	 * Get all of the registered importers.
68
	 *
69
	 * @since 1.0.0
70
	 *
71
	 * @return array All of the registered importers.
72
	 */
73
	public static function get() {
74
75
		if ( ! self::$initialized ) {
76
			self::init();
77
		}
78
79
		return self::$importers;
80
	}
81
82
	/**
83
	 * Register an importer.
84
	 *
85
	 * If the importer is already registered, it will be overwritten.
86
	 *
87
	 * @since 1.0.0
88
	 *
89
	 * @param string $slug The unique identifier for this importer.
90
	 * @param array  $args {
91
	 *        Other importer arguments.
92
	 *
93
	 *        @type string $class The Importer class.
94
	 *        @type string $name  The name of this importer.
95
	 * }
96
	 */
97
	public static function register( $slug, array $args ) {
98
		self::$importers[ $slug ] = $args;
99
	}
100
101
	/**
102
	 * Deregister an importer.
103
	 *
104
	 * @since 1.0.0
105
	 *
106
	 * @param string $slug The slug of the importer to deregister.
107
	 */
108
	public static function deregister( $slug ) {
109
		unset( self::$importers[ $slug ] );
110
	}
111
112
	/**
113
	 * Check if an importer is registered.
114
	 *
115
	 * @since 1.0.0
116
	 *
117
	 * @param string $slug The slug of the importer.
118
	 *
119
	 * @return bool True if the importer is registered, otherwise false.
120
	 */
121
	public static function is_registered( $slug ) {
122
123
		if ( ! self::$initialized ) {
124
			self::init();
125
		}
126
127
		return isset( self::$importers[ $slug ] );
128
	}
129
130
	/**
131
	 * Get an instance of an importer.
132
	 *
133
	 * @since 1.0.0
134
	 *
135
	 * @param string $slug The slug of the importer to get an instance of.
136
	 *
137
	 * @return WordPoints_Importer|false The importer, or false if it isn't registered.
138
	 */
139
	public static function get_importer( $slug ) {
140
141
		if ( ! self::is_registered( $slug ) ) {
142
			return false;
143
		}
144
145
		$importer = self::$importers[ $slug ];
146
147
		return new $importer['class']( $importer['name'] );
148
	}
149
}
150
151
// EOF
152