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 future |
27
|
|
|
*/ |
28
|
|
|
public $plugin; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \GV\Request The global request. |
32
|
|
|
* |
33
|
|
|
* @api |
34
|
|
|
* @since future |
35
|
|
|
*/ |
36
|
|
|
public $request; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \GV\Logger; |
40
|
|
|
* |
41
|
|
|
* @api |
42
|
|
|
* @since future |
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
|
2 |
|
public static function get() { |
52
|
2 |
|
if ( ! self::$__instance instanceof self ) { |
53
|
|
|
self::$__instance = new self; |
54
|
|
|
} |
55
|
2 |
|
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 |
|
|
|
|
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
|
|
|
/** |
89
|
|
|
* Stop all further functionality from loading if the WordPress |
90
|
|
|
* plugin is incompatible with the current environment. |
91
|
|
|
*/ |
92
|
|
|
if ( ! $this->plugin->is_compatible() ) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** Enable logging. */ |
97
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-logger.php' ); |
98
|
|
|
$this->log = new WP_Action_Logger(); |
99
|
|
|
|
100
|
|
|
/** Templating. */ |
101
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-template.php' ); |
102
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-template-view.php' ); |
103
|
|
|
|
104
|
|
|
/** Register the gravityview post type upon WordPress core init. */ |
105
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-view.php' ); |
106
|
|
|
add_action( 'init', array( '\GV\View', 'register_post_type' ) ); |
107
|
|
|
|
108
|
|
|
/** The Contexts. */ |
109
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-context.php' ); |
110
|
|
|
|
111
|
|
|
/** The Settings. */ |
112
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-settings.php' ); |
113
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-settings-view.php' ); |
114
|
|
|
|
115
|
|
|
/** Add rewrite endpoint for single-entry URLs. */ |
116
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-entry.php' ); |
117
|
|
|
add_action( 'init', array( '\GV\Entry', 'add_rewrite_endpoint' ) ); |
118
|
|
|
|
119
|
|
|
/** Shortcodes */ |
120
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-shortcode.php' ); |
121
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-shortcode-gravityview.php' ); |
122
|
|
|
// add_action( 'init', array( '\GV\Shortcodes\gravityview', 'add' ) ); // @todo uncomment when original is stubbed |
|
|
|
|
123
|
|
|
|
124
|
|
|
/** Our Source generic and beloved source and form backend implementations. */ |
125
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-source.php' ); |
126
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-source-internal.php' ); |
127
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-form.php' ); |
128
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-form-gravityforms.php' ); |
129
|
|
|
|
130
|
|
|
/** Our Entry generic and beloved entry backend implementations. */ |
131
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-entry.php' ); |
132
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-entry-gravityforms.php' ); |
133
|
|
|
|
134
|
|
|
/** Our Field generic. */ |
135
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-field.php' ); |
136
|
|
|
|
137
|
|
|
/** Get the collections ready. */ |
138
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-collection.php' ); |
139
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-collection-form.php' ); |
140
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-collection-field.php' ); |
141
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-collection-entry.php' ); |
142
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-collection-view.php' ); |
143
|
|
|
|
144
|
|
|
/** The sorting, filtering and paging classes. */ |
145
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-collection-entry-filter.php' ); |
146
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-collection-entry-sort.php' ); |
147
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-collection-entry-offset.php' ); |
148
|
|
|
|
149
|
|
|
/** Initialize the current request. For now we assume a default WordPress frontent context. */ |
150
|
|
|
require_once $this->plugin->dir( 'future/includes/class-gv-request.php' ); |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Use this for global state tracking in the old code. |
154
|
|
|
* |
155
|
|
|
* We're in a tricky situation now, where we're putting our |
156
|
|
|
* Frontend_Request to work. But the old code is relying on |
157
|
|
|
* it to keep track of views state and whatnot. Ugh. |
158
|
|
|
* |
159
|
|
|
* More importantly GravityView_View_Data is resetting it every |
160
|
|
|
* time the class instantiates! This conflicts with adding filters, |
161
|
|
|
* actions, and other global initialization for the real request. |
162
|
|
|
* |
163
|
|
|
* Let's give them a Dummy_Request to work with. They're using it |
164
|
|
|
* as a container for views either way. And for the is_admin() |
165
|
|
|
* function, which will be available once GravityView_View_Data |
166
|
|
|
* is removed. |
167
|
|
|
*/ |
168
|
|
|
$this->request = new Dummy_Request(); |
169
|
|
|
|
170
|
|
|
if ( ! $this->request->is_admin() ) { |
171
|
|
|
/** The main frontend request. */ |
172
|
|
|
new Frontend_Request(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
define( 'GRAVITYVIEW_FUTURE_CORE_LOADED', true ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
private function __clone() { } |
179
|
|
|
|
180
|
|
|
private function __wakeup() { } |
181
|
|
|
|
182
|
2 |
|
public function __get( $key ) { |
183
|
|
|
switch ( $key ) { |
184
|
2 |
|
case 'views': |
185
|
2 |
|
return $this->request->views; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function __set( $key, $value ) { |
190
|
|
|
switch ( $key ) { |
191
|
|
|
case 'views': |
192
|
|
|
throw new \RuntimeException( __CLASS__ . '::$views is an immutable reference to ' . __CLASS__ . '::$request::$views.' ); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.