-
Notifications
You must be signed in to change notification settings - Fork 2
/
ostrichcize.php
180 lines (156 loc) · 5.03 KB
/
ostrichcize.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
<?php
/*
Plugin Name: Ostrichcize
Plugin URI: https://github.com/tollmanz/ostrichcize
Description: Hides PHP errors reported by specified plugins or themes.
Author: Zack Tollman
Version: 0.1.1
Author URI: http://tollmanz.com
*/
if ( ! class_exists( 'Struthio_Camelus' ) ) :
/**
* The class that defines all functionality for the plugin.
*/
class Struthio_Camelus {
/**
* Holds the plugin directories to prepend to $this->_plugins to exclude from error reporting.
*
* @since 0.1
* @var array
*/
private $_directories = array();
/**
* Holds the plugin slugs to exclude from error reporting.
*
* @since 0.1
* @var array
*/
private $_plugins = array();
/**
* Holds the paths to exclude from error reporting.
*
* @since 0.1
* @var array
*/
private $_paths = array();
/**
* The one instance of Struthio_Camelus.
*
* @since 0.1
* @var Struthio_Camelus
*/
private static $instance;
/**
* Instantiate or return the one Struthio_Camelus instance.
*
* @uses Struthio_Camelus, Struthio_Camelus::init
*
* @since 0.1
* @return Struthio_Camelus
*/
public static function instance() {
if ( is_null( self::$instance ) )
self::$instance = new self();
return self::$instance;
}
/**
* Setup the basic plugin actions.
*
* @uses set_error_handler, apply_filters, Struthio_Camelus::_prepend_dir_to_slugs, get_stylesheet_directory
*
* @since 0.1
* @return Struthio_Camelus
*/
private function __construct() {
// Override the default error handler
set_error_handler( array( $this, 'error_handler' ) );
// Directory plans to investigate
$this->_directories = apply_filters( 'ostrichcized_directories', array( WP_PLUGIN_DIR, WPMU_PLUGIN_DIR ) );
// Plugin paths are defined by hooking into this filter
$this->_plugins = apply_filters( 'ostrichcized_plugins', array() );
// Generate excluded paths based on the defined plugin paths; check for plugins in multiple places
$this->_paths = $this->_prepend_dir_to_slugs( $this->_directories, $this->_plugins );
// Allow for a final filtering of the excluded paths
$this->_paths = apply_filters( 'ostrichcized_paths', $this->_paths );
// Allow the main theme's errors to be suppressed by filtering this value to return (bool) true
if ( true === apply_filters( 'ostrichcize_theme', false ) )
array_push( $this->_paths, get_template_directory(), get_stylesheet_directory() );
}
/**
* Takes an array of slugs and prepends it with a directory path.
*
* @uses trailingslashit
*
* @since 0.1
*
* @param array $dirs Array of directory paths to prepend.
* @param array $slugs Slugs that are prepended.
* @return array Array of slugs prepended with a specified directory path.
*/
private function _prepend_dir_to_slugs( $dirs, $slugs ) {
$paths = array();
foreach ( $slugs as $slug ) {
foreach ( $dirs as $dir ) {
$path = trailingslashit( trailingslashit( $dir ) . $slug );
$paths[] = str_replace( array( '/', '\\' ), DIRECTORY_SEPARATOR, $path );
}
}
return $paths;
}
/**
* Override the default error handler to suppress some errors from being reported.
*
* PHP allows a developer to define a custom error handler to print custom error messages. This function provides a
* custom error handler, which serves suppress errors that show in certain paths. If one of the paths defined in
* $this->_excluded_paths is present in either the $errstr or the $errfile, the error will not be reported. If this
* function returns (bool) true, no error is reported. The normal error handling (and other error handler
* extensions, such as Xdebug) will be used if this function returns (bool) false.
*
* @uses strpos
*
* @since 0.1
*
* @param string $errno The error number.
* @param string $errstr The error message.
* @param string $errfile Path to the file that caused the error.
* @param int $errline Line number of the error.
* @return bool True to success error reporting; false to use default error handler.
*/
public function error_handler( $errno, $errstr, $errfile, $errline ) {
foreach ( $this->_paths as $path ) {
if ( false !== strpos( $errstr, $path ) )
return true;
if ( false !== strpos( $errfile, $path ) )
return true;
}
// The path was not found, so report the error
return false;
}
}
/**
* Wrapper function to return the one Struthio_Camelus instance.
*
* @since 0.1
* @return Struthio_Camelus
*/
function struthrio_get_the_ostrich() {
return Struthio_Camelus::instance();
}
/**
* Load in the Ostrich, but only do it if one of the filters is used.
*
* @since 0.1.1
* @return void
*/
function struthio_load_the_ostrich() {
if ( false !== has_filter( 'ostrichcized_plugins' )
|| false !== has_filter( 'ostrichcized_directories' )
|| false !== has_filter( 'ostrichcized_paths' )
|| false !== has_filter( 'ostrichcize_theme' ) ) {
Struthio_Camelus::instance();
}
}
// Initiate the plugin functionality
add_action( 'muplugins_loaded', 'struthio_load_the_ostrich' );
add_action( 'plugins_loaded', 'struthio_load_the_ostrich' );
endif;