-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnwcc-loader.php
More file actions
222 lines (187 loc) · 4.63 KB
/
nwcc-loader.php
File metadata and controls
222 lines (187 loc) · 4.63 KB
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
<?php
/**
* Plugin Loader.
*
* @package Network_Wide_Custom_Code
* @since x.x.x
*/
namespace NWCC;
use NWCC\Admin\Menu;
use NWCC\Core\Helper;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* NWCC_Loader
*
* @since x.x.x
*/
class NWCC_Loader {
/**
* Instance object.
*
* @var instance
*/
private static $instance;
/**
* Instance of Helper class
*
* @var object helper
* @since x.x.x
*/
private $helper;
/**
* NWCC DB Set option.
*
* @var array nwcc_option
* @since x.x.x
*/
private $nwcc_option;
/**
* Member Variable for getting current site ID
*
* @var current_blog
*/
private $current_blog;
/**
* Initiator
*
* @since x.x.x
* @return object initialized object of class.
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*
* @since x.x.x
*/
public function __construct() {
if ( false === is_multisite() ) {
return;
}
$this->current_blog = get_current_blog_id();
spl_autoload_register( [ $this, 'autoload' ] );
add_action( 'plugins_loaded', [ $this, 'load_textdomain' ] );
add_action( 'plugins_loaded', [ $this, 'setup_classes' ] );
add_action( 'init', [ $this, 'load_frontend_runner' ] );
}
/**
* Autoload classes.
*
* @param string $class class name.
*/
public function autoload( $class ) {
if ( 0 !== strpos( $class, __NAMESPACE__ ) ) {
return;
}
$class_to_load = $class;
$filename = strtolower(
preg_replace(
[ '/^' . __NAMESPACE__ . '\\\/', '/([a-z])([A-Z])/', '/_/', '/\\\/' ],
[ '', '$1-$2', '-', DIRECTORY_SEPARATOR ],
$class_to_load
)
);
$file = NWCC_DIR . $filename . '.php';
// if the file redable, include it.
if ( is_readable( $file ) ) {
require_once $file;
}
}
/**
* Load Plugin Text Domain.
* This will load the translation textdomain depending on the file priorities.
* 1. Global Languages /wp-content/languages/network-wide-custom-code/ folder
* 2. Local dorectory /wp-content/plugins/network-wide-custom-code/languages/ folder
*
* @since x.x.x
* @return void
*/
public function load_textdomain() {
$lang_dir = NWCC_ROOT . 'languages/';
/**
* Filters the languages directory path to use for plugin.
*
* @param string $lang_dir The languages directory path.
*/
$lang_dir = apply_filters( 'nwcc_languages_directory', $lang_dir );
load_plugin_textdomain( 'nwcc', false, $lang_dir );
}
/**
* Include required classes.
*
* @since x.x.x
*/
public function setup_classes() {
if ( is_admin() && is_super_admin() ) {
/* Setup Menu */
Menu::get_instance();
}
}
/**
* Load the provided CSS/JS assets on the frontend.
*
* @since x.x.x
*/
public function load_frontend_runner() {
$blogs = get_sites();
$this->helper = Helper::get_instance();
$this->nwcc_option = $this->helper->get_option( NWCC_SETTINGS );
if ( count( $blogs ) > 0 ) {
foreach ( $blogs as $blog ) {
// Add required actions for 'wp_head' script and style.
add_action( 'wp_head', [ $this, 'wp_head_style' ] );
add_action( 'wp_head', [ $this, 'wp_head_script' ] );
// Add required actions for 'wp_footer' script and style.
add_action( 'wp_footer', [ $this, 'wp_footer_style' ] );
add_action( 'wp_footer', [ $this, 'wp_footer_script' ] );
}
}
switch_to_blog( $this->current_blog );
}
/**
* Add style CSS in header.
*
* @since x.x.x
* @return void
*/
public function wp_head_style() {
echo '<style type="text/css">' . wp_unslash( $this->nwcc_option['header_css'] ) . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Add script JS in header.
*
* @since x.x.x
* @return void
*/
public function wp_head_script() {
echo '<script type="text/javascript">' . wp_unslash( $this->nwcc_option['header_js'] ) . '</script>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Add style CSS in footer.
*
* @since x.x.x
* @return void
*/
public function wp_footer_style() {
echo '<style type="text/css">' . wp_unslash( $this->nwcc_option['footer_css'] ) . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Add script in footer.
*
* @since x.x.x
* @return void
*/
public function wp_footer_script() {
echo '<script type="text/javascript">' . wp_unslash( $this->nwcc_option['footer_js'] ) . '</script>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
/**
* Kicking this off by calling 'get_instance()' method
*/
NWCC_Loader::get_instance();