/
class-jwt-auth.php
executable file
·194 lines (177 loc) · 5.36 KB
/
class-jwt-auth.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
<?php
/**
* The file that defines the core plugin class.
*
* A class definition that includes attributes and functions used across both the
* public-facing side of the site and the admin area.
*
* @link https://enriquechavez.co
* @since 1.0.0
*/
/**
* 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
*
* @author Enrique Chavez <noone@tmeister.net>
*/
class Jwt_Auth
{
/**
* The loader that's responsible for maintaining and registering all hooks that power
* the plugin.
*
* @since 1.0.0
*
* @var Jwt_Auth_Loader Maintains and registers all hooks for the plugin.
*/
protected $loader;
/**
* The unique identifier of this plugin.
*
* @since 1.0.0
*
* @var string The string used to uniquely identify this plugin.
*/
protected $plugin_name;
/**
* The current version of the plugin.
*
* @since 1.0.0
*
* @var string The current version of the plugin.
*/
protected $version;
/**
* Define the core functionality of the plugin.
*
* Set the plugin name and the plugin version that can be used throughout the plugin.
* Load the dependencies, define the locale, and set the hooks for the admin area and
* the public-facing side of the site.
*
* @since 1.0.0
*/
public function __construct()
{
$this->plugin_name = 'jwt-auth';
$this->version = '1.0.0';
$this->load_dependencies();
$this->set_locale();
$this->define_public_hooks();
}
/**
* Load the required dependencies for this plugin.
*
* Include the following files that make up the plugin:
*
* - Jwt_Auth_Loader. Orchestrates the hooks of the plugin.
* - Jwt_Auth_i18n. Defines internationalization functionality.
* - Jwt_Auth_Admin. Defines all hooks for the admin area.
* - Jwt_Auth_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
*/
private function load_dependencies()
{
/**
* Load dependecies managed by composer.
*/
require_once plugin_dir_path(dirname(__FILE__)).'includes/vendor/autoload.php';
/**
* The class responsible for orchestrating the actions and filters of the
* core plugin.
*/
require_once plugin_dir_path(dirname(__FILE__)).'includes/class-jwt-auth-loader.php';
/**
* The class responsible for defining internationalization functionality
* of the plugin.
*/
require_once plugin_dir_path(dirname(__FILE__)).'includes/class-jwt-auth-i18n.php';
/**
* The class responsible for defining all actions that occur in the public-facing
* side of the site.
*/
require_once plugin_dir_path(dirname(__FILE__)).'public/class-jwt-auth-public.php';
$this->loader = new Jwt_Auth_Loader();
}
/**
* Define the locale for this plugin for internationalization.
*
* Uses the Jwt_Auth_i18n class in order to set the domain and to register the hook
* with WordPress.
*
* @since 1.0.0
*/
private function set_locale()
{
$plugin_i18n = new Jwt_Auth_i18n();
$plugin_i18n->set_domain($this->get_plugin_name());
$this->loader->add_action('plugins_loaded', $plugin_i18n, 'load_plugin_textdomain');
}
/**
* Register all of the hooks related to the public-facing functionality
* of the plugin.
*
* @since 1.0.0
*/
private function define_public_hooks()
{
$plugin_public = new Jwt_Auth_Public($this->get_plugin_name(), $this->get_version());
$this->loader->add_action('rest_api_init', $plugin_public, 'add_api_routes');
$this->loader->add_filter('rest_api_init', $plugin_public, 'add_cors_support');
$this->loader->add_filter('determine_current_user', $plugin_public, 'determine_current_user', 99);
$this->loader->add_filter( 'rest_pre_dispatch', $plugin_public, 'rest_pre_dispatch', 10, 2 );
}
/**
* Run the loader to execute all of the hooks with WordPress.
*
* @since 1.0.0
*/
public function run()
{
$this->loader->run();
}
/**
* The name of the plugin used to uniquely identify it within the context of
* WordPress and to define internationalization functionality.
*
* @since 1.0.0
*
* @return string The name of the plugin.
*/
public function get_plugin_name()
{
return $this->plugin_name;
}
/**
* The reference to the class that orchestrates the hooks with the plugin.
*
* @since 1.0.0
*
* @return Jwt_Auth_Loader Orchestrates the hooks of the plugin.
*/
public function get_loader()
{
return $this->loader;
}
/**
* Retrieve the version number of the plugin.
*
* @since 1.0.0
*
* @return string The version number of the plugin.
*/
public function get_version()
{
return $this->version;
}
}