Completed
Push — master ( b01591...3ee7a1 )
by Zack
70:00 queued 53:54
created

Core::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * The core GravityView API.
11
 *
12
 * Returned by the wrapper gravityview() global function, exposes
13
 * all the required public functionality and classes, sets up global
14
 * state depending on current request context, etc.
15
 */
16
final class Core {
17
	/**
18
	 * @var \GV\Core The \GV\Core static instance.
19
	 */
20
	private static $__instance = null;
21
22
	/**
23
	 * @var \GV\Plugin The WordPress plugin context.
24
	 *
25
	 * @api
26
	 * @since 2.0
27
	 */
28
	public $plugin;
29
30
	/**
31
	 * @var \GV\Request The global request.
32
	 *
33
	 * @api
34
	 * @since 2.0
35
	 */
36
	public $request;
37
38
	/**
39
	 * @var \GV\Logger
40
	 *
41
	 * @api
42
	 * @since 2.0
43
	 */
44
	public $log;
45
46
	/**
47
	 * Get the global instance of \GV\Core.
48
	 *
49
	 * @return \GV\Core The global instance of GravityView Core.
50
	 */
51 189
	public static function get() {
52 189
		if ( ! self::$__instance instanceof self ) {
53
			self::$__instance = new self;
54
		}
55 189
		return self::$__instance;
56
	}
57
58
	/**
59
	 * Very early initialization.
60
	 *
61
	 * Activation handlers, rewrites, post type registration.
62
	 */
63
	public static function bootstrap() {
64
		require_once dirname( __FILE__ ) . '/class-gv-plugin.php';
65
		Plugin::get()->register_activation_hooks();
66
	}
67
68
	/**
69
	 * Bootstrap.
70
	 *
71
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
72
	 */
73
	private function __construct() {
74
		self::$__instance = $this;
75
		$this->init();
76
	}
77
78
	/**
79
	 * Early initialization.
80
	 *
81
	 * Loads dependencies, sets up the object, adds hooks, etc.
82
	 *
83
	 * @return void
84
	 */
85
	private function init() {
86
		$this->plugin = Plugin::get();
87
88
		/** Enable logging. */
89
		require_once $this->plugin->dir( 'future/includes/class-gv-logger.php' );
90
		/**
91
		 * @filter `gravityview/logger` Filter the logger instance being used for logging.
92
		 * @param \GV\Logger $logger The logger instance.
93
		 */
94
		$this->log = apply_filters( 'gravityview/logger', new WP_Action_Logger() );
95
96
		/**
97
		 * Stop all further functionality from loading if the WordPress
98
		 * plugin is incompatible with the current environment.
99
		 *
100
		 * Saves some time and memory.
101
		 */
102
		if ( ! $this->plugin->is_compatible() ) {
103
			$this->log->error( 'GravityView 2.0 is not compatible with this environment. Stopped loading.' );
104
			return;
105
		}
106
107
		/**
108
		 * Utilities.
109
		 */
110
		require_once $this->plugin->dir( 'future/includes/class-gv-utils.php' );
111
112
		/** The Settings. */
113
		require_once $this->plugin->dir( 'future/includes/class-gv-settings.php' );
114
		require_once $this->plugin->dir( 'future/includes/class-gv-settings-view.php' );
115
116
		/** Request. */
117
		require_once $this->plugin->dir( 'future/includes/class-gv-request.php' );
118
119
		if ( Request::is_admin() ) {
120
			$this->request = new Admin_Request();
121
		} else {
122
			$this->request = new Frontend_Request();
123
		}
124
125
		/** Require critical legacy core files. @todo Deprecate */
126
		require_once $this->plugin->dir( 'includes/helper-functions.php' );
127
		require_once $this->plugin->dir( 'includes/class-common.php');
128
		require_once $this->plugin->dir( 'includes/connector-functions.php');
129
		require_once $this->plugin->dir( 'includes/class-gravityview-compatibility.php' );
130
		require_once $this->plugin->dir( 'includes/class-gravityview-roles-capabilities.php' );
131
		require_once $this->plugin->dir( 'includes/class-gravityview-admin-notices.php' );
132
		require_once $this->plugin->dir( 'includes/class-admin.php' );
133
		require_once $this->plugin->dir( 'includes/class-post-types.php');
134
		require_once $this->plugin->dir( 'includes/class-cache.php');
135
136
		/**
137
		 * GravityView extensions and widgets.
138
		 */
139
		require_once $this->plugin->dir( 'future/includes/class-gv-extension.php' );
140
		require_once $this->plugin->dir( 'future/includes/class-gv-widget.php' );
141
142
		/** More legacy core. @todo Deprecate */
143
		$this->plugin->include_legacy_core();
144
145
		/** Register the gravityview post type upon WordPress core init. */
146
		require_once $this->plugin->dir( 'future/includes/class-gv-view.php' );
147
		add_action( 'init', array( '\GV\View', 'register_post_type' ) );
148
		add_action( 'init', array( '\GV\View', 'add_rewrite_endpoint' ) );
149
		add_filter( 'map_meta_cap', array( '\GV\View', 'restrict' ), 11, 4 );
150
		add_action( 'template_redirect', array( '\GV\View', 'template_redirect' ) );
151
		add_action( 'the_content', array( '\GV\View', 'content' ) );
152
153
		/** Add rewrite endpoint for single-entry URLs. */
154
		require_once $this->plugin->dir( 'future/includes/class-gv-entry.php' );
155
		add_action( 'init', array( '\GV\Entry', 'add_rewrite_endpoint' ) );
156
157
		/** REST API */
158
		require_once $this->plugin->dir( 'future/includes/rest/class-gv-rest-core.php' );
159
		add_action( 'rest_api_init', array( '\GV\REST\Core', 'init' ) );
160
161
		/** Generate custom slugs on entry save. @todo Deprecate. */
162
		add_action( 'gform_entry_created', array( '\GravityView_API', 'entry_create_custom_slug' ), 10, 2 );
163
164
		/** Shortcodes */
165
		require_once $this->plugin->dir( 'future/includes/class-gv-shortcode.php' );
166
		require_once $this->plugin->dir( 'future/includes/class-gv-shortcode-gravityview.php' );
167
		require_once $this->plugin->dir( 'future/includes/class-gv-shortcode-gventry.php' );
168
		require_once $this->plugin->dir( 'future/includes/class-gv-shortcode-gvfield.php' );
169
		add_action( 'init', array( '\GV\Shortcodes\gravityview', 'add' ) );
170
		add_action( 'init', array( '\GV\Shortcodes\gventry', 'add' ) );
171
		add_action( 'init', array( '\GV\Shortcodes\gvfield', 'add' ) );
172
		
173
		/** oEmbed */
174
		require_once $this->plugin->dir( 'future/includes/class-gv-oembed.php' );
175
		add_action( 'init', array( '\GV\oEmbed', 'init' ), 11 );
176
177
		/** Our Source generic and beloved source and form backend implementations. */
178
		require_once $this->plugin->dir( 'future/includes/class-gv-source.php' );
179
		require_once $this->plugin->dir( 'future/includes/class-gv-source-internal.php' );
180
		require_once $this->plugin->dir( 'future/includes/class-gv-form.php' );
181
		require_once $this->plugin->dir( 'future/includes/class-gv-form-gravityforms.php' );
182
183
		/** Joins */
184
		require_once $this->plugin->dir( 'future/includes/class-gv-form-join.php' );
185
186
		/** Our Entry generic and beloved entry backend implementations. */
187
		require_once $this->plugin->dir( 'future/includes/class-gv-entry-gravityforms.php' );
188
		require_once $this->plugin->dir( 'future/includes/class-gv-entry-multi.php' );
189
190
		/** Context is everything. */
191
		require_once $this->plugin->dir( 'future/includes/class-gv-context.php' );
192
		require_once $this->plugin->dir( 'future/includes/class-gv-context-template.php' );
193
194
		/** Our Field generic and implementations. */
195
		require_once $this->plugin->dir( 'future/includes/class-gv-field.php' );
196
		require_once $this->plugin->dir( 'future/includes/class-gv-field-gravityforms.php' );
197
		require_once $this->plugin->dir( 'future/includes/class-gv-field-internal.php' );
198
199
		/** Get the collections ready. */
200
		require_once $this->plugin->dir( 'future/includes/class-gv-collection.php' );
201
		require_once $this->plugin->dir( 'future/includes/class-gv-collection-form.php' );
202
		require_once $this->plugin->dir( 'future/includes/class-gv-collection-field.php' );
203
		require_once $this->plugin->dir( 'future/includes/class-gv-collection-entry.php' );
204
		require_once $this->plugin->dir( 'future/includes/class-gv-collection-widget.php' );
205
		require_once $this->plugin->dir( 'future/includes/class-gv-collection-view.php' );
206
207
		/** The sorting, filtering and paging classes. */
208
		require_once $this->plugin->dir( 'future/includes/class-gv-collection-entry-filter.php' );
209
		require_once $this->plugin->dir( 'future/includes/class-gv-collection-entry-sort.php' );
210
		require_once $this->plugin->dir( 'future/includes/class-gv-collection-entry-offset.php' );
211
212
		/** The Renderers. */
213
		require_once $this->plugin->dir( 'future/includes/class-gv-renderer.php' );
214
		require_once $this->plugin->dir( 'future/includes/class-gv-renderer-view.php' );
215
		require_once $this->plugin->dir( 'future/includes/class-gv-renderer-entry.php' );
216
		require_once $this->plugin->dir( 'future/includes/class-gv-renderer-entry-edit.php' );
217
		require_once $this->plugin->dir( 'future/includes/class-gv-renderer-field.php' );
218
219
		/** Templating. */
220
		require_once $this->plugin->dir( 'future/includes/class-gv-template.php' );
221
		require_once $this->plugin->dir( 'future/includes/class-gv-template-view.php' );
222
		require_once $this->plugin->dir( 'future/includes/class-gv-template-entry.php' );
223
		require_once $this->plugin->dir( 'future/includes/class-gv-template-field.php' );
224
		require_once $this->plugin->dir( 'future/includes/class-gv-template-legacy-override.php' );
225
226
		/** Magic. */
227
		require_once $this->plugin->dir( 'future/includes/class-gv-wrappers.php' );
228
229
		/** Cache busting. */
230
		add_action( 'clean_post_cache', '\GV\View::_flush_cache' );
231
232
		/**
233
		 * @action `gravityview/loaded` The core has been loaded.
234
		 *
235
		 * Note: this is a very early load hook, not all of WordPress core has been loaded here.
236
		 *  `init` hasn't been called yet.
237
		 */
238
		do_action( 'gravityview/loaded' );
239
	}
240
241
	private function __clone() { }
242
243
	private function __wakeup() { }
244
245
	/**
246
	 * Wrapper magic.
247
	 *
248
	 * Making developers happy, since 2017.
249
	 */
250 15
	public function __get( $key ) {
251 15
		static $views;
252
253
		switch ( $key ) {
254 15
			case 'views':
255 15
				if ( is_null( $views ) ) {
256 1
					$views = new \GV\Wrappers\views();
257
				}
258 15
				return $views;
259
		}
260
	}
261
262
	public function __set( $key, $value ) {
263
	}
264
}
265