-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmycred-module-network.php
440 lines (331 loc) · 12.7 KB
/
mycred-module-network.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
if ( ! defined( 'myCRED_VERSION' ) ) exit;
/**
* myCRED_Network class
* This module handles all Multisite related features along with adding in the Network settings
* page in the wp-admin area. Only used if myCRED is enabled network wide!
* @since 0.1
* @version 1.3
*/
if ( ! class_exists( 'myCRED_Network_Module' ) ) :
class myCRED_Network_Module {
public $core;
public $plug;
public $blog_id = 0;
public $settings = array();
/**
* Construct
*/
public function __construct() {
global $mycred_network;
$this->core = mycred();
$this->blog_id = get_current_blog_id();
$this->settings = mycred_get_settings_network();
}
/**
* Load
* @since 0.1
* @version 1.1
*/
public function load() {
add_action( 'mycred_init', array( $this, 'module_init' ) );
add_action( 'mycred_admin_init', array( $this, 'module_admin_init' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_before' ) );
add_action( 'network_admin_menu', array( $this, 'add_menu' ) );
}
/**
* Init
* @since 0.1
* @version 1.0.1
*/
public function module_init() {
if ( MYCRED_ENABLE_LOGGING && ! mycred_is_main_site() ) {
/**
* In situations where we are enforcing our main sites settings on all blogs and
* we are not centralising the log, we need to check if the local database table
* should be installed.
*/
if ( $this->settings['master'] && ! $this->settings['central'] ) {
$local_install = get_blog_option( $this->blog_id, 'mycred_version_db', false );
if ( $local_install === false ) {
mycred_install_log( NULL, true );
// Add local marker to prevent this from running again
add_blog_option( $this->blog_id, 'mycred_version_db', time() );
}
}
}
$this->network_enabled = is_plugin_active_for_network( 'mycred/mycred.php' );
if ( $this->network_enabled ) {
add_filter( 'wpmu_blogs_columns', array( $this, 'site_column_headers' ) );
add_action( 'manage_sites_custom_column', array( $this, 'site_column_content' ), 10, 2 );
}
}
/**
* Admin Init
* @since 0.1
* @version 1.0
*/
public function module_admin_init() {
register_setting( 'mycred_network', 'mycred_network', array( $this, 'save_network_prefs' ) );
}
/**
* Enqueue Admin Before
* Adjust the myCRED column on the sites screen.
* @since 1.7.6
* @version 1.0
*/
public function enqueue_admin_before() {
$screen = get_current_screen();
if ( $screen->id == 'sites-network' ) {
echo '<style type="text/css">th#' . esc_attr( MYCRED_SLUG ) . ' { width: 15%; }</style>';
}
}
/**
* Site Column Headers
* @since 1.7.6
* @version 1.0
*/
public function site_column_headers( $columns ) {
if ( ! array_key_exists( MYCRED_SLUG, $columns ) )
$columns[ MYCRED_SLUG ] = mycred_label();
return $columns;
}
/**
* Site Column Content
* @since 1.7.6
* @version 1.0
*/
public function site_column_content( $column_name, $blog_id ) {
if ( $column_name == MYCRED_SLUG ) {
if ( mycred_is_site_blocked( $blog_id ) ) {
echo '<span class="dashicons dashicons-warning"></span><div class="row-actions"><span class="info" style="color: #666">' . esc_html__( 'Blocked', 'mycred' ) . '</span></div>';
}
else {
if ( ! $this->settings['master'] ) {
if ( get_blog_option( $blog_id, 'mycred_setup_completed', false ) !== false )
echo '<span class="dashicons dashicons-yes" style="color: green;"></span><div class="row-actions"><span class="info" style="color: #666">' . esc_html__( 'Installed', 'mycred' ) . '</span></div>';
else
echo '<span class="dashicons dashicons-minus"></span><div class="row-actions"><span class="info" style="color: #666">' . esc_html__( 'Not Installed', 'mycred' ) . '</span></div>';
}
else {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<span class="dashicons dashicons-yes"' . ( $blog_id == 1 ? ' style="color: green;"' : '' ) . '></span><div class="row-actions"><span class="info" style="color: #666">' . ( $blog_id == 1 ? esc_html__( 'Master Template', 'mycred' ) : esc_html__( 'Enabled', 'mycred' ) ) . '</span></div>';
}
}
}
}
/**
* Add Network Menu Items
* @since 0.1
* @version 1.2
*/
public function add_menu() {
$pages = array();
$name = mycred_label( true );
$pages[] = add_menu_page(
$name,
$name,
'manage_network_options',
MYCRED_SLUG . '-network',
'',
'dashicons-star-filled'
);
$pages[] = add_submenu_page(
MYCRED_SLUG . '-network',
__( 'Network Settings', 'mycred' ),
__( 'Network Settings', 'mycred' ),
'manage_network_options',
MYCRED_SLUG . '-network',
array( $this, 'admin_page_settings' )
);
foreach ( $pages as $page )
add_action( 'admin_print_styles-' . $page, array( $this, 'admin_page_header' ) );
}
/**
* Add Admin Menu Styling
* @since 0.1
* @version 1.1
*/
public function admin_page_header() {
wp_enqueue_style( 'mycred-bootstrap-grid' );
wp_localize_script( 'mycred-accordion', 'myCRED', array( 'active' => 0 ) );
wp_enqueue_script( 'mycred-accordion' );
?>
<!-- myCRED Accordion Styling -->
<style type="text/css">
h4:before { float:right; padding-right: 12px; font-size: 14px; font-weight: normal; color: silver; }
h4.ui-accordion-header.ui-state-active:before { content: "<?php esc_attr_e( 'click to close', 'mycred' ); ?>"; }
h4.ui-accordion-header:before { content: "<?php esc_attr_e( 'click to open', 'mycred' ); ?>"; }
</style>
<?php
}
/**
* Network Settings Page
* @since 0.1
* @version 1.1
*/
public function admin_page_settings() {
// Security
if ( ! current_user_can( 'manage_network_options' ) ) wp_die( 'Access Denied' );
global $mycred_network;
$name = mycred_label();
?>
<div class="wrap mycred-metabox" id="myCRED-wrap">
<h1><?php printf( esc_html__( '%s Network', 'mycred' ), esc_html( $name ) ); ?><?php if ( MYCRED_DEFAULT_LABEL === 'myCRED' ) : ?> <a href="http://codex.mycred.me/chapter-i/multisites/" class="mycred-ui-info-btn" target="_blank"></a><?php endif; ?></h1>
<?php
if ( wp_is_large_network() ) {
?>
<p><?php esc_html_e( 'I am sorry but your network is too big to use these features.', 'mycred' ); ?></p>
<?php
}
else {
// Inform user that myCRED has not yet been setup
$setup = get_blog_option( 1, 'mycred_setup_completed', false );
if ( $setup === false )
echo '<div class="error"><p>' . sprintf( esc_html__( 'Note! %s has not yet been setup.', 'mycred' ), esc_html( $name ) ) . '</p></div>';
// Settings Updated
if ( isset( $_GET['settings-updated'] ) )
echo '<div class="updated"><p>' . esc_html__( 'Settings Updated', 'mycred' ) . '</p></div>';
?>
<form method="post" action="<?php echo esc_url( admin_url( 'options.php' ) ); ?>" class="form" name="mycred-core-settings-form" novalidate>
<?php settings_fields( 'mycred_network' ); ?>
<div class="list-items expandable-li" id="accordion">
<div class="mycred-ui-accordion">
<div class="mycred-ui-accordion-header">
<h4 class="mycred-ui-accordion-header-title">
<span class="dashicons dashicons-admin-settings static mycred-ui-accordion-header-icon"></span>
<label><?php esc_html_e( 'Settings', 'mycred' ); ?></label>
</h4>
<div class="mycred-ui-accordion-header-actions hide-if-no-js">
<button type="button" aria-expanded="true">
<span class="mycred-ui-toggle-indicator" aria-hidden="true"></span>
</button>
</div>
</div>
<div class="body mycred-ui-accordion-body" style="display:none;">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<h3><?php esc_html_e( 'Master Template', 'mycred' ); ?></h3>
<p><a href="http://codex.mycred.me/chapter-i/multisites/master-template/" class="mycred-ui-color-purple" target="_blank"><?php esc_html_e( 'Documentation', 'mycred' ); ?></a></p>
<div class="row">
<div class="col-xs-6">
<div class="form-group">
<label for="mycred-network-overwrite-enabled"><input type="radio" name="mycred_network[master]" id="mycred-network-overwrite-enabled" <?php checked( (int) $this->settings['master'], 1 ); ?> value="1" /> <?php esc_html_e( 'Enabled', 'mycred' ); ?></label>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label for="mycred-network-overwrite-disabled"><input type="radio" name="mycred_network[master]" id="mycred-network-overwrite-disabled" <?php checked( (int) $this->settings['master'], 0 ); ?> value="0" /> <?php esc_html_e( 'Disabled', 'mycred' ); ?></label>
</div>
</div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<h3><?php esc_html_e( 'Central Logging', 'mycred' ); ?></h3>
<p><a href="http://codex.mycred.me/chapter-i/multisites/central-logging/" class="mycred-ui-color-purple" target="_blank"><?php esc_html_e( 'Documentation', 'mycred' ); ?></a></p>
<div class="row">
<div class="col-xs-6">
<div class="form-group">
<label for="mycred-network-overwrite-log-enabled"><input type="radio" name="mycred_network[central]" id="mycred-network-overwrite-log-enabled" <?php checked( (int) $this->settings['central'], 1 ); ?> value="1" /> <?php esc_html_e( 'Enabled', 'mycred' ); ?></label>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label for="mycred-network-overwrite-log-disabled"><input type="radio" name="mycred_network[central]" id="mycred-network-overwrite-log-disabled" <?php checked( (int) $this->settings['central'], 0 ); ?> value="0" /> <?php esc_html_e( 'Disabled', 'mycred' ); ?></label>
</div>
</div>
</div>
</div>
</div>
<h3><?php esc_html_e( 'Site Block', 'mycred' ); ?></h3>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="form-group">
<label for="mycred-network-block"><?php esc_html_e( 'Blog IDs', 'mycred' ); ?></label>
<input type="text" name="mycred_network[block]" id="mycred-network-block" value="<?php echo esc_attr( $this->settings['block'] ); ?>" class="form-control" />
<p><span class="description"><?php printf( esc_html__( 'Comma separated list of blog ids where %s is to be disabled.', 'mycred' ), esc_html( $name ) ); ?></span></p>
</div>
</div>
</div>
<?php do_action( 'mycred_network_prefs', $this ); ?>
</div>
</div>
<?php do_action( 'mycred_after_network_prefs', $this ); ?>
</div>
<?php submit_button( __( 'Save Network Settings', 'mycred' ), 'mycred-ui-mt20 mycred-ui-btn-purple', 'submit', false ); ?>
</form>
<?php
}
do_action( 'mycred_bottom_network_page', $this );
?>
</div>
<?php
}
/**
* Save Network Settings
* @since 0.1
* @version 1.1
*/
public function save_network_prefs( $settings ) {
$new_settings = array();
$new_settings['master'] = ( isset( $settings['master'] ) ) ? absint( $settings['master'] ) : 0;
$new_settings['central'] = ( isset( $settings['central'] ) ) ? absint( $settings['central'] ) : 0;
$new_settings['block'] = sanitize_text_field( $settings['block'] );
// Master template feature change
if ( (bool) $new_settings['master'] !== $this->settings['master'] ) {
// Enabled
if ( (bool) $new_settings['master'] === true ) {
$this->enable_master_template();
}
// Disabled
else {
$this->disable_master_template();
}
}
// Central logging feature change
if ( (bool) $new_settings['central'] !== $this->settings['central'] ) {
// Enabled
if ( (bool) $new_settings['central'] === true ) {
$this->enable_central_logging();
}
// Disabled
else {
$this->disable_central_logging();
}
}
return apply_filters( 'mycred_save_network_prefs', $new_settings, $settings, $this->core );
}
/**
* Enable Master Template
* @since 1.7.6
* @version 1.0
*/
protected function enable_master_template() {
do_action( 'mycred_master_template_enabled' );
}
/**
* Disable Master Template
* @since 1.7.6
* @version 1.0
*/
protected function disable_master_template() {
do_action( 'mycred_master_template_disabled' );
}
/**
* Enable Central Logging
* @since 1.7.6
* @version 1.0
*/
protected function enable_central_logging() {
do_action( 'mycred_central_logging_enabled' );
}
/**
* Disable Central Logging
* @since 1.7.6
* @version 1.0
*/
protected function disable_central_logging() {
do_action( 'mycred_central_logging_disabled' );
}
}
endif;