Stencil_Bootstrap   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4
Metric Value
wmc 10
lcom 0
cbo 4
dl 0
loc 107
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 40 3
A boot() 0 16 2
A implementation() 0 21 4
A load_textdomain() 0 3 1
1
<?php
2
/**
3
 * Bootstrap class
4
 *
5
 * Add required Hooks and Filters to provide:
6
 *  - Theme required implementation
7
 *  - Implementation registration
8
 *  - Implementation ready status
9
 *  - Translations
10
 *
11
 * @package Stencil
12
 */
13
14
/**
15
 * Class Bootstrap
16
 */
17
class Stencil_Bootstrap {
18
19
	/**
20
	 * Did we bootstrap?
21
	 *
22
	 * @var bool
23
	 */
24
	private static $bootstrapped = false;
25
26
	/**
27
	 * Bootstrap constructor.
28
	 */
29
	public function __construct() {
30
31
		// Make sure we never bootstrap multiple times.
32
		if ( true === self::$bootstrapped ) {
33
			return;
34
		}
35
36
		/**
37
		 * Initialize the plugin to check for registered addons
38
		 *
39
		 * Checking if a plugin is active with the "stencil-" prefix is not enough
40
		 * The plugin could have not implemented the proper hook and could not be usable in the theme
41
		 */
42
		add_action( 'after_setup_theme', array( __CLASS__, 'boot' ) );
43
44
		/**
45
		 * Allow implementation to be filtered
46
		 */
47
		add_filter( Stencil_Environment::format_filter( 'implementation' ), array( __CLASS__, 'implementation' ) );
48
49
		/**
50
		 * Boot Stencil when an engine is ready
51
		 */
52
		add_action( Stencil_Environment::format_hook( 'engine_ready' ), array( 'Stencil', 'boot' ) );
53
54
		/**
55
		 * Load plugin textdomain
56
		 */
57
		add_action( 'plugins_loaded', array( __CLASS__, 'load_textdomain' ) );
58
59
		/**
60
		 * Boot up config if we are in the CMS.
61
		 */
62
		if ( is_admin() ) {
63
			new Stencil_Config();
64
			new Stencil_Upgrader();
65
		}
66
67
		self::$bootstrapped = true;
68
	}
69
70
	/**
71
	 * Get the classname of the required Implementation
72
	 */
73
	public static function boot() {
74
		$implementation = Stencil_Environment::filter( 'implementation', false );
75
		$required       = Stencil_Environment::filter( 'require', false );
76
77
		if ( $implementation !== $required ) {
78
			$message = __( '<em>Theme - Implementation conflict</em>. The active theme requires the Stencil Implementation: <em>%s</em> to be active.', 'stencil' );
79
			$message = sprintf( $message, $required );
80
81
			Stencil_Feedback::notification( 'error', $message );
82
83
			return null;
84
		}
85
86
		// Tell the implementation to active itself.
87
		Stencil_Environment::trigger( 'activate-' . $required );
88
	}
89
90
	/**
91
	 * Get the required implementation if found, otherwise first in list.
92
	 *
93
	 * @return bool
94
	 */
95
	public static function implementation() {
96
		$engines = Stencil_Environment::filter( 'register-engine', array() );
97
		if ( ! is_array( $engines ) || array() === $engines ) {
98
			return false;
99
		}
100
101
		/**
102
		 * Get theme required addon information
103
		 *
104
		 * If the addon that the theme needs is registered, return it
105
		 */
106
		$required = Stencil_Environment::filter( 'require', false );
107
		if ( in_array( $required, $engines, true ) ) {
108
			return $required;
109
		}
110
111
		/**
112
		 * Otherwise return first registered one
113
		 */
114
		return false;
115
	}
116
117
	/**
118
	 * Load translations for Stencil
119
	 */
120
	public static function load_textdomain() {
121
		load_plugin_textdomain( 'stencil', false, STENCIL_PATH . '/languages' );
122
	}
123
}
124