-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
class-plugin.php
262 lines (203 loc) · 6.52 KB
/
class-plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
* The core plugin class.
*
* This is used to define internationalization, admin-specific hooks, and
* public-facing site hooks.
*
* Also maintains the unique identifier of this plugin as well as the current
* version of the plugin.
*
* @since 1.0.0
* @package Plugin
* @subpackage Plugin/includes
* @author Plugin_Author <email@example.com>
*/
if( ! class_exists( 'Plugin_Main' ) ) {
class Plugin_Main {
/**
* The loader that's responsible for maintaining and registering all hooks that power
* the plugin.
*
* @since 1.0.0
* @access protected
* @var Plugin_Loader $loader Maintains and registers all hooks for the plugin.
*/
protected $loader;
/**
* The variables container object for the plugin.
*
* @since 1.0.0
* @access private
* @var object $plugin The plugin variables.
*/
private $plugin;
/**
* Define the core functionality of the plugin.
*
* @since 1.0.0
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
$this->load_dependencies();
$this->set_locale();
$this->define_admin_hooks();
$this->define_public_hooks();
}
/**
* Load the required dependencies for this plugin.
*
* Include the following files that make up the plugin:
*
* - Plugin_Loader. Orchestrates the hooks of the plugin.
* - Plugin_i18n. Defines internationalization functionality.
* - Plugin_Admin. Defines all hooks for the admin area.
* - Plugin_Public. Defines all hooks for the public side of the site.
*
* Create an instance of the loader which will be used to register the hooks
* with WordPress.
*
* @since 1.0.0
* @access private
*/
private function load_dependencies() {
/**
* The class responsible for orchestrating the actions and filters of the
* core plugin.
*/
require_once $this->plugin['path'] . 'includes/class-plugin-loader.php';
/**
* The class responsible for defining internationalization functionality
* of the plugin.
*/
require_once $this->plugin['path'] . 'includes/class-plugin-i18n.php';
/**
* The class responsible for defining all actions that occur in the admin area.
*/
require_once $this->plugin['path'] . 'admin/class-plugin-admin.php';
/**
* The class responsible for settings in the admin area.
*/
require_once $this->plugin['path'] . 'admin/class-plugin-settings.php';
/**
* The class responsible for custom post types.
*/
require_once $this->plugin['path'] . 'admin/class-plugin-types.php';
/**
* The class responsible for custom metadata.
*/
require_once $this->plugin['path'] . 'admin/class-plugin-meta-boxes.php';
/**
* The class responsible for defining all actions that occur in the public-facing
* side of the site.
*/
require_once $this->plugin['path'] . 'public/class-plugin-public.php';
/**
* The class responsible for public views.
*/
require_once $this->plugin['path'] . 'public/class-plugin-views.php';
/**
* The class responsible for public AJAX functionality.
*/
require_once $this->plugin['path'] . 'public/class-plugin-ajax.php';
/**
* Initialize the plugin loader class.
*/
$this->loader = new Plugin_Loader();
}
/**
* Define the locale for this plugin for internationalization.
*
* Uses the Plugin_i18n class in order to set the domain and to register the hook
* with WordPress.
*
* @since 1.0.0
* @access private
*/
private function set_locale() {
$plugin_i18n = new Plugin_i18n();
$this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' );
}
/**
* Register all of the hooks related to the admin area functionality
* of the plugin.
*
* @since 1.0.0
* @access private
*/
private function define_admin_hooks() {
/**
* Admin
*/
$plugin_admin = new Plugin_Admin( $this->plugin );
// Enqueue the stylesheets.
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
// Enqueue the scripts.
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
// Add the additional action links.
$this->loader->add_filter( 'plugin_action_links_' . $this->plugin['basename'], $plugin_admin, 'action_links' );
/**
* Settings
*/
$plugin_settings = new Plugin_Settings( $this->plugin );
// Register the settings page.
$this->loader->add_action( 'admin_menu', $plugin_settings, 'plugin_menu' );
/**
* Post Types
*/
$plugin_types = new Plugin_Post_Types( $this->plugin );
// Register the "item" post type.
$this->loader->add_action( 'init', $plugin_types, 'register_type_item' );
// Register the "item_category" taxonomy.
$this->loader->add_action( 'init', $plugin_types, 'register_taxonomy_item_category' );
/**
* Meta Boxes
*/
$plugin_meta = new Plugin_Meta_Boxes();
// Register the meta boxes.
$this->loader->add_action( 'add_meta_boxes', $plugin_meta, 'register_meta_boxes' );
// Save the meta boxes.
$this->loader->add_action( 'save_post', $plugin_meta, 'save_meta_boxes' );
}
/**
* Register all of the hooks related to the public-facing functionality
* of the plugin.
*
* @since 1.0.0
* @access private
*/
private function define_public_hooks() {
/**
* Public
*/
$plugin_public = new Plugin_Public( $this->plugin );
// Enqueue the stylesheets.
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
// Enqueue the scripts.
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
/**
* Views
*/
$plugin_views = new Plugin_Views( $this->plugin );
// Setup the single template for the 'item' custom post type.
$this->loader->add_filter( 'single_template', $plugin_views, 'template_item_single' );
// Setup the archive template for the 'item' custom post type.
$this->loader->add_filter( 'archive_template', $plugin_views, 'template_item_archive' );
/**
* AJAX
*/
$plugin_ajax = new Plugin_Public_Ajax( $this->plugin );
// Setup the public AJAX actions.
$this->loader->add_action( 'wp_ajax_nopriv_callback', $plugin_ajax, 'callback' );
$this->loader->add_action( 'wp_ajax_callback', $plugin_ajax, 'callback' );
}
/**
* Run the loader to execute all of the hooks with WordPress.
*
* @since 1.0.0
*/
public function run() {
$this->loader->run();
}
}
}