-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_custom_functions.php
210 lines (157 loc) · 6.14 KB
/
my_custom_functions.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
<?php
/*
Plugin Name: IET Custom Functions
Plugin URI: https://gist.github.com/nfreear/9049532
Description: Search user-interface hack; admin UI simplifications for teachers/editors; print button... [JuxtaLearn]+.
Author: Nick Freear [@IET-OU]
Author URI: https://github.com/IET-OU/oer-evidence-hub-org/#Juxtalearn
Version: 1.3
*/
// http://wphidedash.org/2011/04/best-practice-for-adding-custom-functions/
// http://wpmututorials.com/basics/what-is-the-mu-plugins-folder/
if (basename($_SERVER[ 'PHP_SELF' ]) == basename( __FILE__ )) {
die( 'Sorry, but you cannot access this page directly.' );
}
define( 'IET_CUSTOM_FUNC_REGISTER_FILE', __FILE__ );
//http://wordpress.org/support/topic/stop-wordpress-from-adding-p-tags-and-removing-line-break
//remove_filter ('the_content', 'wpautop');
class IET_Custom_Functions {
const TEST_SERVER_REGEX = '@(test|approval|acct|dev|localhost)@';
protected static $host;
public function __construct() {
self::$host = self::get_option( 'iet_custom_style_hostname', filter_input( INPUT_SERVER, 'HTTP_HOST' ));
add_filter('admin_body_class', array(&$this, 'admin_body_class'));
$this->add_action( 'admin_enqueue_scripts', 'admin_enqueue_scripts' );
$this->add_action( 'wp_enqueue_scripts', 'front_enqueue_scripts' );
//add_action('wp_head', array(&$this, 'head_custom_style'));
$this->add_action( 'wp_footer', 'footer_browser_sniff' );
if (self::is_juxtalearn()) {
$this->add_action( 'admin_footer', 'admin_footer_javascript' );
}
if (self::is_test_site()) {
add_filter( 'robots_txt', array(&$this, 'robots_txt'), 10, 2 );
}
add_filter('admin_body_class', array(&$this, 'body_class'));
add_filter('body_class', array(&$this, 'body_class'));
$this->security_remove_wp_links();
}
public function body_class( $classes ) {
// Banner-logo [LACE][Bug: #10]
if (is_array( $classes )) {
$theme = wp_get_theme();
$classes[] = 'theme-' . strtolower(str_replace( ' ', '-', $theme->name ));
$classes[] = 'header-text-' . ( display_header_text() ? 'yes' : 'no' );
$custom_cls = self::get_option( 'iet_custom_fn_body_class' );
$classes[] = preg_match( '/^[\w \-]+$/', $custom_cls ) ? $custom_cls : '';
}
if (self::is_debug()) {
if (is_array( $classes )) {
$classes[] = 'debug';
} else {
$classes .= ' debug'; //'admin_body_class'
}
}
if (self::is_test_site()) {
if (is_array( $classes )) {
$classes[] = 'test-site';
} else {
$classes .= ' test-site';
}
}
return $classes;
}
// http://wordpress.stackexchange.com/questions/66834/how-to-target-with-css-admin-elem-
public function admin_body_class( $classes ) {
global $current_user;
foreach( $current_user->roles as $role )
$classes .= ' role-' . $role;
return trim( $classes );
}
public function admin_enqueue_scripts() {
wp_enqueue_style( 'iet-custom-functions-admin', plugins_url(
'css/custom-admin.css', IET_CUSTOM_FUNC_REGISTER_FILE
));
}
public function front_enqueue_scripts() {
wp_enqueue_script( 'iet-custom-functions-front', plugins_url(
'js/facetious-hack.js', IET_CUSTOM_FUNC_REGISTER_FILE
), array( 'jquery' ), false, $in_footer = TRUE );
}
protected function security_remove_wp_links() {
remove_action('wp_head', 'rsd_link'); #'EditURL' /xmlrpc.php?rsd
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'wp_generator'); # <meta name="generator" content="WordPress 3.8.1">
#? remove_action('wp_head', 'pingback_link');
}
/** http://trepmal.com/2011/04/03/change-the-virtual-robots-txt-file
*/
public function robots_txt( $output, $public ) {
$output .= 'Disallow: /' . PHP_EOL;
return $output;
}
/** Some crude browser sniffing for SVG/ fullscreen API.
*/
public function footer_browser_sniff() {
/* ?>
<!--[if gte IE 9]>
<script> document.documentElement.className += " ie ie9p "; </script>
<![endif]--> */ ?>
<!--[if !IE]>-->
<script> document.documentElement.className += " not-ie "; </script>
<!--<![endif]-->
<script>
document.documentElement.setAttribute('data-ua', navigator.userAgent);
</script>
<?php /*
<script src="//cdn.jsdelivr.net/modernizr/2.7.1/modernizr.min.js"></script>
<?php */
$this->print_button_javascript();
}
protected function print_button_javascript( $selector = NULL, $title='' ) {
if (!self::is_juxtalearn()) return;
if (!$selector) {
$selector = self::get_option( 'iet_custom_fn_print_button_selector',
'.entry-header, .wp-admin.post-php h2' );
}
$selector = json_encode( $selector );
$title = json_encode(esc_attr( $title ));
?>
<script id="iet-custom-functions-js">
jQuery(function ($) {
$(<?php echo $selector ?>).append(
'<button class="jxl-print" onclick="window.print()" title=<?php
echo $title ?>><i></i><span>Print</span></button>');
});
</script>
<?php
}
public function admin_footer_javascript() {
$this->print_button_javascript( NULL, 'Try landscape to print "Edit" pages!' );
}
// Utilities.
protected function add_action( $hook, $function, $priority = 10, $accepted_args = 1 ) {
add_action( $hook, array( &$this, $function ), $priority, $accepted_args );
}
/** Get values for a named option from the options database table.
* Uses WordPress `get_option()`. Falls back to a PHP defined() constant.
*
* @link https://codex.wordpress.org/Function_Reference/get_option
*/
public static function get_option( $key, $default = NULL ) {
$_KEY = strtoupper( $key );
$default = /*!$default &&*/ defined( $_KEY ) ? constant( $_KEY ) : $default;
return get_option( $key, $default );
}
public static function is_debug() {
return isset($_GET[ 'debug' ]);
}
public static function is_test_site() {
return preg_match( self::TEST_SERVER_REGEX, filter_input( INPUT_SERVER, 'HTTP_HOST' ));
}
public static function is_juxtalearn() {
return 'trickytopic.juxtalearn.net' == self::$host;
}
}
$iet_custom_functions = new IET_Custom_Functions();
#End.