WPB::define_public_hooks()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * The file that defines the core plugin class.
4
 *
5
 * A class definition that includes attributes and functions used across both the
6
 * public-facing side of the site and the admin area.
7
 *
8
 * @link       https://github.com/maab16
9
 * @since      1.0.0
10
 */
11
12
/**
13
 * The core plugin class.
14
 *
15
 * This is used to define internationalization, admin-specific hooks, and
16
 * public-facing site hooks.
17
 *
18
 * Also maintains the unique identifier of this plugin as well as the current
19
 * version of the plugin.
20
 *
21
 * @since      1.0.0
22
 *
23
 * @author     Md Abu Ahsan basir <[email protected]>
24
 */
25
class WPB
26
{
27
    /**
28
     * The loader that's responsible for maintaining and registering all hooks that power
29
     * the plugin.
30
     *
31
     * @since    1.0.0
32
     *
33
     * @var WPB_Loader Maintains and registers all hooks for the plugin.
34
     */
35
    protected $loader;
36
37
    /**
38
     * The unique identifier of this plugin.
39
     *
40
     * @since    1.0.0
41
     *
42
     * @var string The string used to uniquely identify this plugin.
43
     */
44
    protected $plugin_name;
45
46
    /**
47
     * The current version of the plugin.
48
     *
49
     * @since    1.0.0
50
     *
51
     * @var string The current version of the plugin.
52
     */
53
    protected $version;
54
55
    /**
56
     * Define the core functionality of the plugin.
57
     *
58
     * Set the plugin name and the plugin version that can be used throughout the plugin.
59
     * Load the dependencies, define the locale, and set the hooks for the admin area and
60
     * the public-facing side of the site.
61
     *
62
     * @since    1.0.0
63
     */
64
    public function __construct()
65
    {
66
        $this->define_constants();
67
68
        if (defined('WPB_VERSION')) {
69
            $this->version = WPB_VERSION;
70
        } else {
71
            $this->version = '1.0.0';
72
        }
73
        $this->plugin_name = 'wpb';
74
        $this->load_dependencies();
75
        $this->set_locale();
76
        $this->define_admin_hooks();
77
        $this->define_public_hooks();
78
        $this->register_assets();
79
    }
80
81
    /**
82
     * Load the required dependencies for this plugin.
83
     *
84
     * Include the following files that make up the plugin:
85
     *
86
     * - WPB_Loader. Orchestrates the hooks of the plugin.
87
     * - WPB_i18n. Defines internationalization functionality.
88
     * - WPB_Admin. Defines all hooks for the admin area.
89
     * - WPB_Public. Defines all hooks for the public side of the site.
90
     *
91
     * Create an instance of the loader which will be used to register the hooks
92
     * with WordPress.
93
     *
94
     * @since    1.0.0
95
     */
96
    private function load_dependencies()
97
    {
98
99
        /**
100
         * The class responsible for orchestrating the actions and filters of the
101
         * core plugin.
102
         */
103
        require_once plugin_dir_path(dirname(__FILE__)).'includes/class-wpb-loader.php';
0 ignored issues
show
Bug introduced by
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        require_once /** @scrutinizer ignore-call */ plugin_dir_path(dirname(__FILE__)).'includes/class-wpb-loader.php';
Loading history...
104
105
        /**
106
         * The class responsible for defining internationalization functionality
107
         * of the plugin.
108
         */
109
        require_once plugin_dir_path(dirname(__FILE__)).'includes/class-wpb-i18n.php';
110
111
        /**
112
         * The class responsible for defining all actions that occur in the admin area.
113
         */
114
        require_once plugin_dir_path(dirname(__FILE__)).'admin/class-wpb-admin.php';
115
116
        /**
117
         * The class responsible for defining all menu actions that occur in the admin area.
118
         */
119
        require_once plugin_dir_path(dirname(__FILE__)).'admin/class-wpb-admin-menu.php';
120
121
        /**
122
         * The class responsible for defining all submenu actions that occur in the admin area.
123
         */
124
        require_once plugin_dir_path(dirname(__FILE__)).'admin/class-wpb-admin-submenu.php';
125
126
        /**
127
         * The class responsible for defining all actions that occur in the public-facing
128
         * side of the site.
129
         */
130
        require_once plugin_dir_path(dirname(__FILE__)).'public/class-wpb-public.php';
131
132
        $this->loader = new WPB_Loader();
133
    }
134
135
    /**
136
     * Define the locale for this plugin for internationalization.
137
     *
138
     * Uses the WPB_i18n class in order to set the domain and to register the hook
139
     * with WordPress.
140
     *
141
     * @since    1.0.0
142
     */
143
    private function set_locale()
144
    {
145
        $plugin_i18n = new WPB_I18n();
146
147
        $this->loader->add_action('plugins_loaded', $plugin_i18n, 'load_plugin_textdomain');
148
    }
149
150
    /**
151
     * Register all of the hooks related to the admin area functionality
152
     * of the plugin.
153
     *
154
     * @since    1.0.0
155
     */
156
    private function define_admin_hooks()
157
    {
158
        $plugin_admin = new WPB_Admin($this->get_plugin_name(), $this->get_version());
159
160
        $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles');
161
        $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts');
162
    }
163
164
    /**
165
     * Register all of the hooks related to the public-facing functionality
166
     * of the plugin.
167
     *
168
     * @since    1.0.0
169
     */
170
    private function define_public_hooks()
171
    {
172
        $plugin_public = new WPB_Public($this->get_plugin_name(), $this->get_version());
173
174
        $this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_styles');
175
        $this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_scripts');
176
    }
177
178
    /**
179
     * Run the loader to execute all of the hooks with WordPress.
180
     *
181
     * @since    1.0.0
182
     */
183
    public function run()
184
    {
185
        $this->loader->run();
186
    }
187
188
    /**
189
     * The name of the plugin used to uniquely identify it within the context of
190
     * WordPress and to define internationalization functionality.
191
     *
192
     * @since     1.0.0
193
     *
194
     * @return string The name of the plugin.
195
     */
196
    public function get_plugin_name()
197
    {
198
        return $this->plugin_name;
199
    }
200
201
    /**
202
     * The reference to the class that orchestrates the hooks with the plugin.
203
     *
204
     * @since     1.0.0
205
     *
206
     * @return WPB_Loader Orchestrates the hooks of the plugin.
207
     */
208
    public function get_loader()
209
    {
210
        return $this->loader;
211
    }
212
213
    /**
214
     * Retrieve the version number of the plugin.
215
     *
216
     * @since     1.0.0
217
     *
218
     * @return string The version number of the plugin.
219
     */
220
    public function get_version()
221
    {
222
        return $this->version;
223
    }
224
225
    /**
226
     * Define the constants.
227
     *
228
     * @return void
229
     */
230
    public function define_constants()
231
    {
232
        define('WPB_VERSION', $this->version);
233
    }
234
235
    /**
236
     * Register our app scripts and styles.
237
     *
238
     * @return void
239
     */
240
    public function register_assets()
241
    {
242
        $this->register_scripts($this->get_scripts());
243
        $this->register_styles($this->get_styles());
244
    }
245
246
    /**
247
     * Register scripts.
248
     *
249
     * @param array $scripts All Scripts as an array.
250
     *
251
     * @return void
252
     */
253
    private function register_scripts($scripts)
254
    {
255
        foreach ($scripts as $handle => $script) {
256
            $deps = isset($script['deps']) ? $script['deps'] : false;
257
            $in_footer = isset($script['in_footer']) ? $script['in_footer'] : false;
258
            $version = isset($script['version']) ? $script['version'] : WPB_VERSION;
259
260
            wp_register_script($handle, $script['src'], $deps, $version, $in_footer);
0 ignored issues
show
Bug introduced by
The function wp_register_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

260
            /** @scrutinizer ignore-call */ 
261
            wp_register_script($handle, $script['src'], $deps, $version, $in_footer);
Loading history...
261
        }
262
    }
263
264
    /**
265
     * Register styles.
266
     *
267
     * @param array $styles All styles as an array.
268
     *
269
     * @return void
270
     */
271
    public function register_styles($styles)
272
    {
273
        foreach ($styles as $handle => $style) {
274
            $deps = isset($style['deps']) ? $style['deps'] : false;
275
276
            wp_register_style($handle, $style['src'], $deps, WPB_VERSION);
0 ignored issues
show
Bug introduced by
The function wp_register_style was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

276
            /** @scrutinizer ignore-call */ 
277
            wp_register_style($handle, $style['src'], $deps, WPB_VERSION);
Loading history...
277
        }
278
    }
279
280
    /**
281
     * Get all registered scripts.
282
     *
283
     * @return array
284
     */
285
    public function get_scripts()
286
    {
287
        $prefix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '.min' : '';
0 ignored issues
show
Bug introduced by
The constant SCRIPT_DEBUG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Unused Code introduced by
The assignment to $prefix is dead and can be removed.
Loading history...
288
289
        $scripts = [
290
            'wpb-runtime'  => [
291
                'src'       => WPB_ASSETS.'/js/runtime.js',
292
                'version'   => filemtime(WPB_PATH.'/public/js/runtime.js'),
293
                'in_footer' => true,
294
            ],
295
            'wpb-vendor'   => [
296
                'src'       => WPB_ASSETS.'/js/vendors.js',
297
                'version'   => filemtime(WPB_PATH.'/public/js/vendors.js'),
298
                'in_footer' => true,
299
            ],
300
            'wpb-frontend' => [
301
                'src'       => WPB_ASSETS.'/js/frontend.js',
302
                'deps'      => ['jquery', 'wpb-vendor', 'wpb-runtime'],
303
                'version'   => filemtime(WPB_PATH.'/public/js/frontend.js'),
304
                'in_footer' => true,
305
            ],
306
            'wpb-admin'    => [
307
                'src'       => WPB_ASSETS.'/js/admin.js',
308
                'deps'      => ['jquery', 'wpb-vendor', 'wpb-runtime'],
309
                'version'   => filemtime(WPB_PATH.'/public/js/admin.js'),
310
                'in_footer' => true,
311
            ],
312
            'wpb-spa'      => [
313
                'src'       => WPB_ASSETS.'/js/spa.js',
314
                'deps'      => ['jquery', 'wpb-vendor', 'wpb-runtime'],
315
                'version'   => filemtime(WPB_PATH.'/public/js/spa.js'),
316
                'in_footer' => true,
317
            ],
318
        ];
319
320
        return $scripts;
321
    }
322
323
    /**
324
     * Get registered styles.
325
     *
326
     * @return array
327
     */
328
    public function get_styles()
329
    {
330
        $styles = [
331
            'wpb-style'    => [
332
                'src' => WPB_ASSETS.'/css/style.css',
333
            ],
334
            'wpb-frontend' => [
335
                'src' => WPB_ASSETS.'/css/frontend.css',
336
            ],
337
            'wpb-admin'    => [
338
                'src' => WPB_ASSETS.'/css/admin.css',
339
            ],
340
            'wpb-spa'      => [
341
                'src' => WPB_ASSETS.'/css/spa.css',
342
            ],
343
            'wpb-vendors'  => [
344
                'src' => WPB_ASSETS.'/css/vendors.css',
345
            ],
346
        ];
347
348
        return $styles;
349
    }
350
}
351