-
Notifications
You must be signed in to change notification settings - Fork 0
/
admincssmu.php
316 lines (255 loc) · 11.5 KB
/
admincssmu.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
<?php
/*
Plugin Name: Admin CSS MU
Plugin URI: http://millionclues.com
Description: A plugin to load a CSS file to style the admin side. Works with Multisite.
Author: Arun Basil Lal
Author URI: http://millionclues.com
Version: 2.10
Text Domain: admin-css-mu
Domain Path: /languages
License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
/**
* ::TODO::
* - Update version number ADMINCSSMU_VERSION_NUM on every update
*/
// Exit If Accessed Directly
if ( ! defined( 'ABSPATH' ) ) exit;
/*------------------------------------------*/
/* Plugin Setup Functions */
/*------------------------------------------*/
// Add Admin Sub Menu: Appearance > Admin CSS MU
function admincssmu_add_menu_links() {
if ( is_multisite() ) {
if ( get_current_blog_id() == SITE_ID_CURRENT_SITE ) {
add_theme_page ( __('Admin CSS MU','admin-css-mu'), __('Admin CSS MU','admin-css-mu'), 'update_core', 'admin-css-mu','admincssmu_admin_interface_render' );
}
}
else {
add_theme_page ( __('Admin CSS MU','admin-css-mu'), __('Admin CSS MU','admin-css-mu'), 'update_core', 'admin-css-mu','admincssmu_admin_interface_render' );
}
}
add_action( 'admin_menu', 'admincssmu_add_menu_links' );
// Print Direct Link To Admin CSS MU Options Page In Plugins List
function admincssmu_settings_link( $links ) {
return array_merge(
array(
'settings' => '<a href="' . admin_url( 'themes.php?page=admin-css-mu' ) . '">' . __( 'Add CSS', 'admin-css-mu' ) . '</a>'
),
$links
);
}
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'admincssmu_settings_link' );
// Add Links to Plugins list
function admincssmu_plugin_row_meta( $links, $file ) {
if ( strpos( $file, 'admincssmu.php' ) !== false ) {
$new_links = array(
'donate' => '<a href="http://millionclues.com/donate/" target="_blank">Donate</a>',
'hireme' => '<a href="http://millionclues.com/portfolio/" target="_blank">Hire Me For A Project</a>',
);
$links = array_merge( $links, $new_links );
}
return $links;
}
add_filter( 'plugin_row_meta', 'admincssmu_plugin_row_meta', 10, 2 );
// Load Text Domain
function admincssmu_load_plugin_textdomain() {
load_plugin_textdomain( 'admin-css-mu', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'admincssmu_load_plugin_textdomain' );
// Register Settings
function admincssmu_register_settings() {
register_setting( 'admincssmu_settings_group', 'admincssmu_custom_css', 'admincssmu_clean_css_with_csstidy' );
}
add_action( 'admin_init', 'admincssmu_register_settings' );
/**
* Add plugin version to database
*
* @since 2.4
* @constant ADMINCSSMU_VERSION_NUM the version number of the current version
* @refer https://codex.wordpress.org/Creating_Tables_with_Plugins#Adding_an_Upgrade_Function
*/
if ( !defined( 'ADMINCSSMU_VERSION_NUM' ) ) {
define( 'ADMINCSSMU_VERSION_NUM', '2.10' );
// update_option('abl_admincssmu_version', ADMINCSSMU_VERSION_NUM); // Disabled to set default values for Load Admin CSS checkbox
}
/**
* Set default values for Load CSS checkboxes
*
* @since 2.4
*/
if ( is_multisite() ) {
$installed_ver = get_blog_option( SITE_ID_CURRENT_SITE, 'abl_admincssmu_version' );
}
else {
$installed_ver = get_option( 'abl_admincssmu_version' );
}
if ($installed_ver == '' ) {
if ( is_multisite() ) {
$admincssmu_custom_css_option = get_blog_option( get_current_blog_id(), 'admincssmu_custom_css' );
}
else {
$admincssmu_custom_css_option = get_option( 'admincssmu_custom_css' );
}
// All CSS loaded by default
$admincssmu_custom_css_option['load_css'] = 1;
if ( is_multisite() ) {
update_blog_option(SITE_ID_CURRENT_SITE, 'admincssmu_custom_css', $admincssmu_custom_css_option);
update_blog_option(SITE_ID_CURRENT_SITE, 'abl_admincssmu_version', ADMINCSSMU_VERSION_NUM);
}
else {
update_option('admincssmu_custom_css', $admincssmu_custom_css_option);
update_option('abl_admincssmu_version', ADMINCSSMU_VERSION_NUM);
}
}
// Delete Options During Uninstall
function admincssmu_uninstall_plugin() {
delete_option( 'admincssmu_custom_css' );
delete_option( 'abl_admincssmu_version' );
}
register_uninstall_hook(__FILE__, 'admincssmu_uninstall_plugin' );
/**
* Admin footer text
*
* @since 2.3
*/
function admincssmu_footer_text($default) {
// Retun default on non-plugin pages
$screen = get_current_screen();
if ( $screen->id !== "appearance_page_admin-css-mu" ) {
return $default;
}
$admincssmu_footer_text = sprintf( __( 'If you like this plugin, please <a href="%s" target="_blank">make a donation</a> or leave a <a href="%s" target="_blank">★★★★★</a> rating to support continued development. Thanks a bunch!', 'admin-css-mu' ),
'http://millionclues.com/donate/',
'https://wordpress.org/support/plugin/admin-css-mu/reviews/?rate=5#new-post'
);
return $admincssmu_footer_text;
}
add_filter('admin_footer_text', 'admincssmu_footer_text');
/*--------------------------------------*/
/* Admin Options Page */
/*--------------------------------------*/
// Load Syntax Highlighter
function admincssmu_register_highlighter( $hook ) {
if ( 'appearance_page_admin-css-mu' === $hook ) {
wp_enqueue_style( 'highlighter-css', plugins_url( basename( dirname( __FILE__ ) ) . '/highlighter/codemirror.css' ) );
wp_enqueue_script( 'highlighter-js', plugins_url( basename( dirname( __FILE__ ) ) . '/highlighter/codemirror.js' ), array(), '20140329', true );
wp_enqueue_script( 'highlighter-css-js', plugins_url( basename( dirname( __FILE__ ) ) . '/highlighter/css.js' ), array(), '20140329', true );
}
}
add_action( 'admin_enqueue_scripts', 'admincssmu_register_highlighter' );
// Sanitize CSS with CSS Tidy - Uses CSS Tidy Modified By The Jetpack Team.
function admincssmu_clean_css_with_csstidy ( $input ) {
$input['admincssmu_admin_css'] = admincssmu_csstidy_helper ( $input['admincssmu_admin_css'] );
return $input;
}
// Scrub And Clean With CSS Tidy
function admincssmu_csstidy_helper ( $css, $minify=false ) {
include_once('csstidy/class.csstidy.php');
$csstidy = new csstidy();
$csstidy->set_cfg( 'remove_bslash', false );
$csstidy->set_cfg( 'compress_colors', false );
$csstidy->set_cfg( 'compress_font-weight', false );
$csstidy->set_cfg( 'optimise_shorthands', 0 );
$csstidy->set_cfg( 'remove_last_;', false );
$csstidy->set_cfg( 'case_properties', false );
$csstidy->set_cfg( 'discard_invalid_properties', true );
$csstidy->set_cfg( 'css_level', 'CSS3.0' );
$csstidy->set_cfg( 'preserve_css', true );
if ($minify === false) {
$csstidy->set_cfg( 'template', dirname( __FILE__ ) . '/csstidy/wordpress-standard.tpl' );
} else {
$csstidy->set_cfg( 'template', 'highest');
}
$css = preg_replace( '/\\\\([0-9a-fA-F]{4})/', '\\\\\\\\$1', $css );
$css = str_replace( '<=', '<=', $css );
$css = wp_kses_split( $css, array(), array() );
$css = str_replace( '>', '>', $css ); // kses replaces lone '>' with >
$css = strip_tags( $css );
$csstidy->parse( $css );
$css = $csstidy->print->plain();
return $css;
}
// Admin Interface: Appearance > Admin CSS MU
function admincssmu_admin_interface_render () {
if ( is_multisite() ) {
$admincssmu_custom_css_option = get_blog_option( get_current_blog_id(), 'admincssmu_custom_css' );
}
else {
$admincssmu_custom_css_option = get_option( 'admincssmu_custom_css' );
}
$admincssmu_admin_css_content = isset( $admincssmu_custom_css_option['admincssmu_admin_css'] ) && ! empty( $admincssmu_custom_css_option['admincssmu_admin_css'] ) ? $admincssmu_custom_css_option['admincssmu_admin_css'] : __( "/* Enter Your Custom Admin CSS Here */\r\n", 'admin-css-mu' );
?>
<div class="wrap">
<h1><?php _e('Admin CSS MU','admin-css-mu') ?></h1>
<p><?php _e('Enter your custom CSS below and it will be loaded on all Admin Pages.','admin-css-mu') ?></p>
<form method="post" action="options.php" enctype="multipart/form-data">
<?php settings_fields( 'admincssmu_settings_group' ); ?>
<textarea rows="10" class="large-text code" id="admincssmu_custom_css[admincssmu_admin_css]" name="admincssmu_custom_css[admincssmu_admin_css]"><?php echo esc_textarea( $admincssmu_admin_css_content ); ?></textarea><br>
<div style="margin-top: 5px;">
<input type="checkbox" name="admincssmu_custom_css[load_css]" id="admincssmu_custom_css[load_css]" value="1"
<?php if ( isset( $admincssmu_custom_css_option['load_css'] ) ) { checked( '1', $admincssmu_custom_css_option['load_css'] ); } ?>>
<label for="admincssmu_custom_css[load_css]" style="vertical-align: unset;"><?php _e('Load Admin CSS', 'admin-css-mu') ?></label>
</div>
<div style="margin-top: 5px;">
<input type="checkbox" name="admincssmu_custom_css[minfy_css]" id="admincssmu_custom_css[minfy_css]" value="1"
<?php if ( isset( $admincssmu_custom_css_option['minfy_css'] ) ) { checked( '1', $admincssmu_custom_css_option['minfy_css'] ); } ?>>
<label for="admincssmu_custom_css[minfy_css]" style="vertical-align: unset;"><?php _e('Minify CSS', 'admin-css-mu') ?></label>
</div>
<?php submit_button( __( 'Save CSS', 'admin-css-mu' ), 'primary', 'submit', true ); ?>
</form>
<?php // Highlighter ?>
<script language="javascript">
jQuery( document ).ready( function() {
var editor_admin_css = CodeMirror.fromTextArea( document.getElementById( "admincssmu_custom_css[admincssmu_admin_css]" ), {lineNumbers: true, lineWrapping: true} );
});
</script>
</div><?php
}
/*----------------------------------*/
/* Load CSS Styles */
/*----------------------------------*/
// Load Admin CSS Submited Via Admin
function admincssmu_load_admin_css_from_admin() {
if ( is_multisite() ) {
$admincssmu_custom_css_option = get_blog_option( SITE_ID_CURRENT_SITE, 'admincssmu_custom_css' );
}
else {
$admincssmu_custom_css_option = get_option( 'admincssmu_custom_css' );
}
// Check if Load CSS is checked
if ( !((isset($admincssmu_custom_css_option['load_css'])) && (boolval($admincssmu_custom_css_option['load_css']))) ) {
return;
}
$admincssmu_admin_css_content = isset( $admincssmu_custom_css_option['admincssmu_admin_css'] ) && ! empty( $admincssmu_custom_css_option['admincssmu_admin_css'] ) ? $admincssmu_custom_css_option['admincssmu_admin_css'] : '' ;
$admincssmu_admin_css_content = wp_kses( $admincssmu_admin_css_content, array( '\'', '\"' ) );
$admincssmu_admin_css_content = str_replace( '>', '>', $admincssmu_admin_css_content );
// Minify
if ( (isset($admincssmu_custom_css_option['minfy_css'])) && (boolval($admincssmu_custom_css_option['minfy_css'])) ) {
$admincssmu_admin_css_content = admincssmu_csstidy_helper($admincssmu_admin_css_content, true);
} ?>
<style type="text/css">
<?php echo $admincssmu_admin_css_content; ?>
</style><?php
}
add_filter( 'admin_enqueue_scripts' , 'admincssmu_load_admin_css_from_admin' );
// Load Admin CSS From /wp-content/themes/custom_admin.css For Backward Compatibility With Version 1.0
if ( file_exists ( get_theme_root() . '/custom_admin.css' )) {
function admincssmu_load_custom_admin_css() {
wp_enqueue_style('admincssmu_custom_css', get_theme_root_uri() . '/custom_admin.css');
}
add_action( 'admin_enqueue_scripts', 'admincssmu_load_custom_admin_css' );
}
/**
* Get the boolean value of a variable
*
* @param mixed The scalar value being converted to a boolean.
* @return boolean The boolean value of var.
* @refer https://millionclues.com/wordpress-tips/solved-fatal-error-call-to-undefined-function-boolval/
*/
if( !function_exists('boolval')) {
function boolval($var){
return !! $var;
}
}