author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 15:52:01 +0100 | |
changeset 17 | 34716fd837a4 |
parent 16 | a86126ab1dd4 |
child 18 | be944660c56a |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Plugin Administration API |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
10 |
* Parses the plugin contents to retrieve plugin's metadata. |
0 | 11 |
* |
16 | 12 |
* All plugin headers must be on their own line. Plugin description must not have |
13 |
* any newlines, otherwise only parts of the description will be displayed. |
|
14 |
* The below is formatted for printing. |
|
0 | 15 |
* |
5 | 16 |
* /* |
16 | 17 |
* Plugin Name: Name of the plugin. |
18 |
* Plugin URI: The home page of the plugin. |
|
19 |
* Description: Plugin description. |
|
20 |
* Author: Plugin author's name. |
|
21 |
* Author URI: Link to the author's website. |
|
22 |
* Version: Plugin version. |
|
5 | 23 |
* Text Domain: Optional. Unique identifier, should be same as the one used in |
16 | 24 |
* load_plugin_textdomain(). |
5 | 25 |
* Domain Path: Optional. Only useful if the translations are located in a |
9 | 26 |
* folder above the plugin's base path. For example, if .mo files are |
27 |
* located in the locale folder then Domain Path will be "/locale/" and |
|
28 |
* must have the first slash. Defaults to the base folder the plugin is |
|
29 |
* located in. |
|
5 | 30 |
* Network: Optional. Specify "Network: true" to require that a plugin is activated |
9 | 31 |
* across all sites in an installation. This will prevent a plugin from being |
32 |
* activated on a single site when Multisite is enabled. |
|
16 | 33 |
* Requires at least: Optional. Specify the minimum required WordPress version. |
34 |
* Requires PHP: Optional. Specify the minimum required PHP version. |
|
35 |
* * / # Remove the space to close comment. |
|
0 | 36 |
* |
16 | 37 |
* The first 8 KB of the file will be pulled in and if the plugin data is not |
38 |
* within that first 8 KB, then the plugin author should correct their plugin |
|
0 | 39 |
* and move the plugin data headers to the top. |
40 |
* |
|
41 |
* The plugin file is assumed to have permissions to allow for scripts to read |
|
42 |
* the file. This is not checked however and the file is only opened for |
|
43 |
* reading. |
|
44 |
* |
|
45 |
* @since 1.5.0 |
|
16 | 46 |
* @since 5.3.0 Added support for `Requires at least` and `Requires PHP` headers. |
0 | 47 |
* |
9 | 48 |
* @param string $plugin_file Absolute path to the main plugin file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
* @param bool $markup Optional. If the returned data should have HTML markup applied. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
* Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
51 |
* @param bool $translate Optional. If the returned data should be translated. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
* @return array { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
* Plugin data. Values will be empty if not supplied by the plugin. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
* @type string $Name Name of the plugin. Should be unique. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
* @type string $Title Title of the plugin and link to the plugin's site (if set). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
* @type string $Description Plugin description. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
* @type string $Author Author's name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
* @type string $AuthorURI Author's website address (if set). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
* @type string $Version Plugin version. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
* @type string $TextDomain Plugin textdomain. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
* @type string $DomainPath Plugins relative directory path to .mo files. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
* @type bool $Network Whether the plugin can only be activated network-wide. |
16 | 64 |
* @type string $RequiresWP Minimum required version of WordPress. |
65 |
* @type string $RequiresPHP Minimum required version of PHP. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
* } |
0 | 67 |
*/ |
68 |
function get_plugin_data( $plugin_file, $markup = true, $translate = true ) { |
|
69 |
||
70 |
$default_headers = array( |
|
9 | 71 |
'Name' => 'Plugin Name', |
72 |
'PluginURI' => 'Plugin URI', |
|
73 |
'Version' => 'Version', |
|
0 | 74 |
'Description' => 'Description', |
9 | 75 |
'Author' => 'Author', |
76 |
'AuthorURI' => 'Author URI', |
|
77 |
'TextDomain' => 'Text Domain', |
|
78 |
'DomainPath' => 'Domain Path', |
|
79 |
'Network' => 'Network', |
|
16 | 80 |
'RequiresWP' => 'Requires at least', |
81 |
'RequiresPHP' => 'Requires PHP', |
|
0 | 82 |
// Site Wide Only is deprecated in favor of Network. |
9 | 83 |
'_sitewide' => 'Site Wide Only', |
0 | 84 |
); |
85 |
||
86 |
$plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' ); |
|
87 |
||
16 | 88 |
// Site Wide Only is the old header for Network. |
0 | 89 |
if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
/* translators: 1: Site Wide Only: true, 2: Network: true */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
_deprecated_argument( __FUNCTION__, '3.0.0', sprintf( __( 'The %1$s plugin header is deprecated. Use %2$s instead.' ), '<code>Site Wide Only: true</code>', '<code>Network: true</code>' ) ); |
0 | 92 |
$plugin_data['Network'] = $plugin_data['_sitewide']; |
93 |
} |
|
16 | 94 |
$plugin_data['Network'] = ( 'true' === strtolower( $plugin_data['Network'] ) ); |
0 | 95 |
unset( $plugin_data['_sitewide'] ); |
96 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
97 |
// If no text domain is defined fall back to the plugin slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
98 |
if ( ! $plugin_data['TextDomain'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
99 |
$plugin_slug = dirname( plugin_basename( $plugin_file ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
100 |
if ( '.' !== $plugin_slug && false === strpos( $plugin_slug, '/' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
101 |
$plugin_data['TextDomain'] = $plugin_slug; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
102 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
103 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
104 |
|
0 | 105 |
if ( $markup || $translate ) { |
106 |
$plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate ); |
|
107 |
} else { |
|
108 |
$plugin_data['Title'] = $plugin_data['Name']; |
|
109 |
$plugin_data['AuthorName'] = $plugin_data['Author']; |
|
110 |
} |
|
111 |
||
112 |
return $plugin_data; |
|
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* Sanitizes plugin data, optionally adds markup, optionally translates. |
|
117 |
* |
|
118 |
* @since 2.7.0 |
|
9 | 119 |
* |
120 |
* @see get_plugin_data() |
|
121 |
* |
|
0 | 122 |
* @access private |
9 | 123 |
* |
124 |
* @param string $plugin_file Path to the main plugin file. |
|
125 |
* @param array $plugin_data An array of plugin data. See `get_plugin_data()`. |
|
126 |
* @param bool $markup Optional. If the returned data should have HTML markup applied. |
|
127 |
* Default true. |
|
128 |
* @param bool $translate Optional. If the returned data should be translated. Default true. |
|
129 |
* @return array { |
|
130 |
* Plugin data. Values will be empty if not supplied by the plugin. |
|
131 |
* |
|
132 |
* @type string $Name Name of the plugin. Should be unique. |
|
133 |
* @type string $Title Title of the plugin and link to the plugin's site (if set). |
|
134 |
* @type string $Description Plugin description. |
|
135 |
* @type string $Author Author's name. |
|
136 |
* @type string $AuthorURI Author's website address (if set). |
|
137 |
* @type string $Version Plugin version. |
|
138 |
* @type string $TextDomain Plugin textdomain. |
|
139 |
* @type string $DomainPath Plugins relative directory path to .mo files. |
|
140 |
* @type bool $Network Whether the plugin can only be activated network-wide. |
|
141 |
* } |
|
0 | 142 |
*/ |
143 |
function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) { |
|
144 |
||
16 | 145 |
// Sanitize the plugin filename to a WP_PLUGIN_DIR relative path. |
0 | 146 |
$plugin_file = plugin_basename( $plugin_file ); |
147 |
||
16 | 148 |
// Translate fields. |
0 | 149 |
if ( $translate ) { |
16 | 150 |
$textdomain = $plugin_data['TextDomain']; |
151 |
if ( $textdomain ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
if ( ! is_textdomain_loaded( $textdomain ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
if ( $plugin_data['DomainPath'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
154 |
load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
155 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
} |
16 | 159 |
} elseif ( 'hello.php' === basename( $plugin_file ) ) { |
0 | 160 |
$textdomain = 'default'; |
161 |
} |
|
162 |
if ( $textdomain ) { |
|
9 | 163 |
foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field ) { |
164 |
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain |
|
0 | 165 |
$plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain ); |
9 | 166 |
} |
0 | 167 |
} |
168 |
} |
|
169 |
||
16 | 170 |
// Sanitize fields. |
9 | 171 |
$allowed_tags_in_links = array( |
0 | 172 |
'abbr' => array( 'title' => true ), |
173 |
'acronym' => array( 'title' => true ), |
|
174 |
'code' => true, |
|
175 |
'em' => true, |
|
176 |
'strong' => true, |
|
177 |
); |
|
9 | 178 |
|
179 |
$allowed_tags = $allowed_tags_in_links; |
|
180 |
$allowed_tags['a'] = array( |
|
181 |
'href' => true, |
|
182 |
'title' => true, |
|
183 |
); |
|
0 | 184 |
|
185 |
// Name is marked up inside <a> tags. Don't allow these. |
|
186 |
// Author is too, but some plugins have used <a> here (omitting Author URI). |
|
9 | 187 |
$plugin_data['Name'] = wp_kses( $plugin_data['Name'], $allowed_tags_in_links ); |
188 |
$plugin_data['Author'] = wp_kses( $plugin_data['Author'], $allowed_tags ); |
|
0 | 189 |
|
190 |
$plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags ); |
|
9 | 191 |
$plugin_data['Version'] = wp_kses( $plugin_data['Version'], $allowed_tags ); |
0 | 192 |
|
9 | 193 |
$plugin_data['PluginURI'] = esc_url( $plugin_data['PluginURI'] ); |
194 |
$plugin_data['AuthorURI'] = esc_url( $plugin_data['AuthorURI'] ); |
|
0 | 195 |
|
196 |
$plugin_data['Title'] = $plugin_data['Name']; |
|
197 |
$plugin_data['AuthorName'] = $plugin_data['Author']; |
|
198 |
||
16 | 199 |
// Apply markup. |
0 | 200 |
if ( $markup ) { |
9 | 201 |
if ( $plugin_data['PluginURI'] && $plugin_data['Name'] ) { |
5 | 202 |
$plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '">' . $plugin_data['Name'] . '</a>'; |
9 | 203 |
} |
0 | 204 |
|
9 | 205 |
if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] ) { |
5 | 206 |
$plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '">' . $plugin_data['Author'] . '</a>'; |
9 | 207 |
} |
0 | 208 |
|
209 |
$plugin_data['Description'] = wptexturize( $plugin_data['Description'] ); |
|
210 |
||
9 | 211 |
if ( $plugin_data['Author'] ) { |
16 | 212 |
$plugin_data['Description'] .= sprintf( |
213 |
/* translators: %s: Plugin author. */ |
|
214 |
' <cite>' . __( 'By %s.' ) . '</cite>', |
|
215 |
$plugin_data['Author'] |
|
216 |
); |
|
9 | 217 |
} |
0 | 218 |
} |
219 |
||
220 |
return $plugin_data; |
|
221 |
} |
|
222 |
||
223 |
/** |
|
224 |
* Get a list of a plugin's files. |
|
225 |
* |
|
226 |
* @since 2.8.0 |
|
227 |
* |
|
9 | 228 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
16 | 229 |
* @return string[] Array of file names relative to the plugin root. |
0 | 230 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
231 |
function get_plugin_files( $plugin ) { |
0 | 232 |
$plugin_file = WP_PLUGIN_DIR . '/' . $plugin; |
9 | 233 |
$dir = dirname( $plugin_file ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
235 |
$plugin_files = array( plugin_basename( $plugin_file ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
if ( is_dir( $dir ) && WP_PLUGIN_DIR !== $dir ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
* Filters the array of excluded directories and files while scanning the folder. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
* |
9 | 244 |
* @param string[] $exclusions Array of excluded directories and files. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
245 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
$exclusions = (array) apply_filters( 'plugin_files_exclusions', array( 'CVS', 'node_modules', 'vendor', 'bower_components' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
247 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
$list_files = list_files( $dir, 100, $exclusions ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
249 |
$list_files = array_map( 'plugin_basename', $list_files ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
250 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
$plugin_files = array_merge( $plugin_files, $list_files ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
$plugin_files = array_values( array_unique( $plugin_files ) ); |
0 | 253 |
} |
254 |
||
255 |
return $plugin_files; |
|
256 |
} |
|
257 |
||
258 |
/** |
|
259 |
* Check the plugins directory and retrieve all plugin files with plugin data. |
|
260 |
* |
|
261 |
* WordPress only supports plugin files in the base plugins directory |
|
262 |
* (wp-content/plugins) and in one directory above the plugins directory |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
* (wp-content/plugins/my-plugin). The file it looks for has the plugin data |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
* and must be found in those two locations. It is recommended to keep your |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
265 |
* plugin files in their own directories. |
0 | 266 |
* |
267 |
* The file with the plugin data is the file that will be included and therefore |
|
268 |
* needs to have the main execution for the plugin. This does not mean |
|
269 |
* everything must be contained in the file and it is recommended that the file |
|
270 |
* be split for maintainability. Keep everything in one file for extreme |
|
271 |
* optimization purposes. |
|
272 |
* |
|
273 |
* @since 1.5.0 |
|
274 |
* |
|
275 |
* @param string $plugin_folder Optional. Relative path to single plugin folder. |
|
16 | 276 |
* @return array[] Array of arrays of plugin data, keyed by plugin file name. See `get_plugin_data()`. |
0 | 277 |
*/ |
9 | 278 |
function get_plugins( $plugin_folder = '' ) { |
0 | 279 |
|
9 | 280 |
$cache_plugins = wp_cache_get( 'plugins', 'plugins' ); |
281 |
if ( ! $cache_plugins ) { |
|
0 | 282 |
$cache_plugins = array(); |
9 | 283 |
} |
0 | 284 |
|
9 | 285 |
if ( isset( $cache_plugins[ $plugin_folder ] ) ) { |
0 | 286 |
return $cache_plugins[ $plugin_folder ]; |
9 | 287 |
} |
0 | 288 |
|
9 | 289 |
$wp_plugins = array(); |
0 | 290 |
$plugin_root = WP_PLUGIN_DIR; |
9 | 291 |
if ( ! empty( $plugin_folder ) ) { |
0 | 292 |
$plugin_root .= $plugin_folder; |
9 | 293 |
} |
0 | 294 |
|
16 | 295 |
// Files in wp-content/plugins directory. |
9 | 296 |
$plugins_dir = @ opendir( $plugin_root ); |
0 | 297 |
$plugin_files = array(); |
16 | 298 |
|
0 | 299 |
if ( $plugins_dir ) { |
9 | 300 |
while ( ( $file = readdir( $plugins_dir ) ) !== false ) { |
16 | 301 |
if ( '.' === substr( $file, 0, 1 ) ) { |
0 | 302 |
continue; |
9 | 303 |
} |
16 | 304 |
|
9 | 305 |
if ( is_dir( $plugin_root . '/' . $file ) ) { |
306 |
$plugins_subdir = @ opendir( $plugin_root . '/' . $file ); |
|
16 | 307 |
|
0 | 308 |
if ( $plugins_subdir ) { |
9 | 309 |
while ( ( $subfile = readdir( $plugins_subdir ) ) !== false ) { |
16 | 310 |
if ( '.' === substr( $subfile, 0, 1 ) ) { |
0 | 311 |
continue; |
9 | 312 |
} |
16 | 313 |
|
314 |
if ( '.php' === substr( $subfile, -4 ) ) { |
|
0 | 315 |
$plugin_files[] = "$file/$subfile"; |
9 | 316 |
} |
0 | 317 |
} |
16 | 318 |
|
0 | 319 |
closedir( $plugins_subdir ); |
320 |
} |
|
321 |
} else { |
|
16 | 322 |
if ( '.php' === substr( $file, -4 ) ) { |
0 | 323 |
$plugin_files[] = $file; |
9 | 324 |
} |
0 | 325 |
} |
326 |
} |
|
16 | 327 |
|
0 | 328 |
closedir( $plugins_dir ); |
329 |
} |
|
330 |
||
9 | 331 |
if ( empty( $plugin_files ) ) { |
0 | 332 |
return $wp_plugins; |
9 | 333 |
} |
0 | 334 |
|
335 |
foreach ( $plugin_files as $plugin_file ) { |
|
9 | 336 |
if ( ! is_readable( "$plugin_root/$plugin_file" ) ) { |
0 | 337 |
continue; |
9 | 338 |
} |
0 | 339 |
|
16 | 340 |
// Do not apply markup/translate as it will be cached. |
341 |
$plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); |
|
0 | 342 |
|
9 | 343 |
if ( empty( $plugin_data['Name'] ) ) { |
0 | 344 |
continue; |
9 | 345 |
} |
0 | 346 |
|
9 | 347 |
$wp_plugins[ plugin_basename( $plugin_file ) ] = $plugin_data; |
0 | 348 |
} |
349 |
||
350 |
uasort( $wp_plugins, '_sort_uname_callback' ); |
|
351 |
||
352 |
$cache_plugins[ $plugin_folder ] = $wp_plugins; |
|
9 | 353 |
wp_cache_set( 'plugins', $cache_plugins, 'plugins' ); |
0 | 354 |
|
355 |
return $wp_plugins; |
|
356 |
} |
|
357 |
||
358 |
/** |
|
359 |
* Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data. |
|
360 |
* |
|
361 |
* WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins). |
|
362 |
* |
|
363 |
* @since 3.0.0 |
|
16 | 364 |
* @return array[] Array of arrays of mu-plugin data, keyed by plugin file name. See `get_plugin_data()`. |
0 | 365 |
*/ |
366 |
function get_mu_plugins() { |
|
16 | 367 |
$wp_plugins = array(); |
0 | 368 |
$plugin_files = array(); |
369 |
||
9 | 370 |
if ( ! is_dir( WPMU_PLUGIN_DIR ) ) { |
0 | 371 |
return $wp_plugins; |
9 | 372 |
} |
16 | 373 |
|
374 |
// Files in wp-content/mu-plugins directory. |
|
375 |
$plugins_dir = @opendir( WPMU_PLUGIN_DIR ); |
|
376 |
if ( $plugins_dir ) { |
|
0 | 377 |
while ( ( $file = readdir( $plugins_dir ) ) !== false ) { |
16 | 378 |
if ( '.php' === substr( $file, -4 ) ) { |
0 | 379 |
$plugin_files[] = $file; |
9 | 380 |
} |
0 | 381 |
} |
382 |
} else { |
|
383 |
return $wp_plugins; |
|
384 |
} |
|
385 |
||
16 | 386 |
closedir( $plugins_dir ); |
0 | 387 |
|
9 | 388 |
if ( empty( $plugin_files ) ) { |
0 | 389 |
return $wp_plugins; |
9 | 390 |
} |
0 | 391 |
|
392 |
foreach ( $plugin_files as $plugin_file ) { |
|
9 | 393 |
if ( ! is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) ) { |
0 | 394 |
continue; |
9 | 395 |
} |
0 | 396 |
|
16 | 397 |
// Do not apply markup/translate as it will be cached. |
398 |
$plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); |
|
0 | 399 |
|
9 | 400 |
if ( empty( $plugin_data['Name'] ) ) { |
0 | 401 |
$plugin_data['Name'] = $plugin_file; |
9 | 402 |
} |
0 | 403 |
|
404 |
$wp_plugins[ $plugin_file ] = $plugin_data; |
|
405 |
} |
|
406 |
||
16 | 407 |
if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php' ) <= 30 ) { |
408 |
// Silence is golden. |
|
0 | 409 |
unset( $wp_plugins['index.php'] ); |
9 | 410 |
} |
0 | 411 |
|
412 |
uasort( $wp_plugins, '_sort_uname_callback' ); |
|
413 |
||
414 |
return $wp_plugins; |
|
415 |
} |
|
416 |
||
417 |
/** |
|
418 |
* Callback to sort array by a 'Name' key. |
|
419 |
* |
|
420 |
* @since 3.1.0 |
|
9 | 421 |
* |
0 | 422 |
* @access private |
9 | 423 |
* |
424 |
* @param array $a array with 'Name' key. |
|
425 |
* @param array $b array with 'Name' key. |
|
426 |
* @return int Return 0 or 1 based on two string comparison. |
|
0 | 427 |
*/ |
428 |
function _sort_uname_callback( $a, $b ) { |
|
429 |
return strnatcasecmp( $a['Name'], $b['Name'] ); |
|
430 |
} |
|
431 |
||
432 |
/** |
|
433 |
* Check the wp-content directory and retrieve all drop-ins with any plugin data. |
|
434 |
* |
|
435 |
* @since 3.0.0 |
|
16 | 436 |
* @return array[] Array of arrays of dropin plugin data, keyed by plugin file name. See `get_plugin_data()`. |
0 | 437 |
*/ |
438 |
function get_dropins() { |
|
9 | 439 |
$dropins = array(); |
0 | 440 |
$plugin_files = array(); |
441 |
||
442 |
$_dropins = _get_dropins(); |
|
443 |
||
16 | 444 |
// Files in wp-content directory. |
445 |
$plugins_dir = @opendir( WP_CONTENT_DIR ); |
|
446 |
if ( $plugins_dir ) { |
|
0 | 447 |
while ( ( $file = readdir( $plugins_dir ) ) !== false ) { |
9 | 448 |
if ( isset( $_dropins[ $file ] ) ) { |
0 | 449 |
$plugin_files[] = $file; |
9 | 450 |
} |
0 | 451 |
} |
452 |
} else { |
|
453 |
return $dropins; |
|
454 |
} |
|
455 |
||
16 | 456 |
closedir( $plugins_dir ); |
0 | 457 |
|
9 | 458 |
if ( empty( $plugin_files ) ) { |
0 | 459 |
return $dropins; |
9 | 460 |
} |
0 | 461 |
|
462 |
foreach ( $plugin_files as $plugin_file ) { |
|
9 | 463 |
if ( ! is_readable( WP_CONTENT_DIR . "/$plugin_file" ) ) { |
0 | 464 |
continue; |
9 | 465 |
} |
16 | 466 |
|
467 |
// Do not apply markup/translate as it will be cached. |
|
468 |
$plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); |
|
469 |
||
9 | 470 |
if ( empty( $plugin_data['Name'] ) ) { |
0 | 471 |
$plugin_data['Name'] = $plugin_file; |
9 | 472 |
} |
16 | 473 |
|
0 | 474 |
$dropins[ $plugin_file ] = $plugin_data; |
475 |
} |
|
476 |
||
477 |
uksort( $dropins, 'strnatcasecmp' ); |
|
478 |
||
479 |
return $dropins; |
|
480 |
} |
|
481 |
||
482 |
/** |
|
483 |
* Returns drop-ins that WordPress uses. |
|
484 |
* |
|
485 |
* Includes Multisite drop-ins only when is_multisite() |
|
486 |
* |
|
487 |
* @since 3.0.0 |
|
16 | 488 |
* @return array[] Key is file name. The value is an array, with the first value the |
9 | 489 |
* purpose of the drop-in and the second value the name of the constant that must be |
490 |
* true for the drop-in to be used, or true if no constant is required. |
|
0 | 491 |
*/ |
492 |
function _get_dropins() { |
|
493 |
$dropins = array( |
|
16 | 494 |
'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE |
495 |
'db.php' => array( __( 'Custom database class.' ), true ), // Auto on load. |
|
496 |
'db-error.php' => array( __( 'Custom database error message.' ), true ), // Auto on error. |
|
497 |
'install.php' => array( __( 'Custom installation script.' ), true ), // Auto on installation. |
|
498 |
'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // Auto on maintenance. |
|
499 |
'object-cache.php' => array( __( 'External object cache.' ), true ), // Auto on load. |
|
500 |
'php-error.php' => array( __( 'Custom PHP error message.' ), true ), // Auto on error. |
|
501 |
'fatal-error-handler.php' => array( __( 'Custom PHP fatal error handler.' ), true ), // Auto on error. |
|
0 | 502 |
); |
503 |
||
504 |
if ( is_multisite() ) { |
|
9 | 505 |
$dropins['sunrise.php'] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE |
16 | 506 |
$dropins['blog-deleted.php'] = array( __( 'Custom site deleted message.' ), true ); // Auto on deleted blog. |
507 |
$dropins['blog-inactive.php'] = array( __( 'Custom site inactive message.' ), true ); // Auto on inactive blog. |
|
508 |
$dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // Auto on archived or spammed blog. |
|
0 | 509 |
} |
510 |
||
511 |
return $dropins; |
|
512 |
} |
|
513 |
||
514 |
/** |
|
9 | 515 |
* Determines whether a plugin is active. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
516 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
517 |
* Only plugins installed in the plugins/ folder can be active. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
518 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
519 |
* Plugins in the mu-plugins/ folder can't be "activated," so this function will |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
520 |
* return false for those plugins. |
0 | 521 |
* |
9 | 522 |
* For more information on this and similar theme functions, check out |
523 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
524 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
525 |
* |
|
0 | 526 |
* @since 2.5.0 |
527 |
* |
|
9 | 528 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
0 | 529 |
* @return bool True, if in the active plugins list. False, not in the list. |
530 |
*/ |
|
531 |
function is_plugin_active( $plugin ) { |
|
16 | 532 |
return in_array( $plugin, (array) get_option( 'active_plugins', array() ), true ) || is_plugin_active_for_network( $plugin ); |
0 | 533 |
} |
534 |
||
535 |
/** |
|
9 | 536 |
* Determines whether the plugin is inactive. |
0 | 537 |
* |
538 |
* Reverse of is_plugin_active(). Used as a callback. |
|
539 |
* |
|
9 | 540 |
* For more information on this and similar theme functions, check out |
541 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
542 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
543 |
* |
|
0 | 544 |
* @since 3.1.0 |
16 | 545 |
* |
0 | 546 |
* @see is_plugin_active() |
547 |
* |
|
9 | 548 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
0 | 549 |
* @return bool True if inactive. False if active. |
550 |
*/ |
|
551 |
function is_plugin_inactive( $plugin ) { |
|
552 |
return ! is_plugin_active( $plugin ); |
|
553 |
} |
|
554 |
||
555 |
/** |
|
9 | 556 |
* Determines whether the plugin is active for the entire network. |
0 | 557 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
* Only plugins installed in the plugins/ folder can be active. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
559 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
560 |
* Plugins in the mu-plugins/ folder can't be "activated," so this function will |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
561 |
* return false for those plugins. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
562 |
* |
9 | 563 |
* For more information on this and similar theme functions, check out |
564 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
565 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
566 |
* |
|
0 | 567 |
* @since 3.0.0 |
568 |
* |
|
9 | 569 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
570 |
* @return bool True if active for the network, otherwise false. |
|
0 | 571 |
*/ |
572 |
function is_plugin_active_for_network( $plugin ) { |
|
9 | 573 |
if ( ! is_multisite() ) { |
0 | 574 |
return false; |
9 | 575 |
} |
0 | 576 |
|
9 | 577 |
$plugins = get_site_option( 'active_sitewide_plugins' ); |
578 |
if ( isset( $plugins[ $plugin ] ) ) { |
|
0 | 579 |
return true; |
9 | 580 |
} |
0 | 581 |
|
582 |
return false; |
|
583 |
} |
|
584 |
||
585 |
/** |
|
586 |
* Checks for "Network: true" in the plugin header to see if this should |
|
587 |
* be activated only as a network wide plugin. The plugin would also work |
|
588 |
* when Multisite is not enabled. |
|
589 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
590 |
* Checks for "Site Wide Only: true" for backward compatibility. |
0 | 591 |
* |
592 |
* @since 3.0.0 |
|
593 |
* |
|
9 | 594 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
0 | 595 |
* @return bool True if plugin is network only, false otherwise. |
596 |
*/ |
|
597 |
function is_network_only_plugin( $plugin ) { |
|
598 |
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); |
|
9 | 599 |
if ( $plugin_data ) { |
0 | 600 |
return $plugin_data['Network']; |
9 | 601 |
} |
0 | 602 |
return false; |
603 |
} |
|
604 |
||
605 |
/** |
|
606 |
* Attempts activation of plugin in a "sandbox" and redirects on success. |
|
607 |
* |
|
608 |
* A plugin that is already activated will not attempt to be activated again. |
|
609 |
* |
|
610 |
* The way it works is by setting the redirection to the error before trying to |
|
611 |
* include the plugin file. If the plugin fails, then the redirection will not |
|
612 |
* be overwritten with the success message. Also, the options will not be |
|
613 |
* updated and the activation hook will not be called on plugin error. |
|
614 |
* |
|
615 |
* It should be noted that in no way the below code will actually prevent errors |
|
616 |
* within the file. The code should not be used elsewhere to replicate the |
|
617 |
* "sandbox", which uses redirection to work. |
|
618 |
* {@source 13 1} |
|
619 |
* |
|
620 |
* If any errors are found or text is outputted, then it will be captured to |
|
621 |
* ensure that the success redirection will update the error redirection. |
|
622 |
* |
|
623 |
* @since 2.5.0 |
|
9 | 624 |
* @since 5.2.0 Test for WordPress version and PHP version compatibility. |
0 | 625 |
* |
9 | 626 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
627 |
* @param string $redirect Optional. URL to redirect to. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
628 |
* @param bool $network_wide Optional. Whether to enable the plugin for all sites in the network |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
629 |
* or just the current site. Multisite only. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
630 |
* @param bool $silent Optional. Whether to prevent calling activation hooks. Default false. |
16 | 631 |
* @return null|WP_Error Null on success, WP_Error on invalid file. |
0 | 632 |
*/ |
633 |
function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) { |
|
634 |
$plugin = plugin_basename( trim( $plugin ) ); |
|
635 |
||
9 | 636 |
if ( is_multisite() && ( $network_wide || is_network_only_plugin( $plugin ) ) ) { |
637 |
$network_wide = true; |
|
638 |
$current = get_site_option( 'active_sitewide_plugins', array() ); |
|
0 | 639 |
$_GET['networkwide'] = 1; // Back compat for plugins looking for this value. |
640 |
} else { |
|
641 |
$current = get_option( 'active_plugins', array() ); |
|
642 |
} |
|
643 |
||
9 | 644 |
$valid = validate_plugin( $plugin ); |
645 |
if ( is_wp_error( $valid ) ) { |
|
0 | 646 |
return $valid; |
9 | 647 |
} |
648 |
||
649 |
$requirements = validate_plugin_requirements( $plugin ); |
|
650 |
if ( is_wp_error( $requirements ) ) { |
|
651 |
return $requirements; |
|
652 |
} |
|
0 | 653 |
|
16 | 654 |
if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current, true ) ) ) { |
9 | 655 |
if ( ! empty( $redirect ) ) { |
16 | 656 |
// We'll override this later if the plugin can be included without fatal error. |
657 |
wp_redirect( add_query_arg( '_error_nonce', wp_create_nonce( 'plugin-activation-error_' . $plugin ), $redirect ) ); |
|
9 | 658 |
} |
659 |
||
0 | 660 |
ob_start(); |
16 | 661 |
|
9 | 662 |
if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) { |
663 |
define( 'WP_SANDBOX_SCRAPING', true ); |
|
664 |
} |
|
16 | 665 |
|
666 |
wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin ); |
|
667 |
$_wp_plugin_file = $plugin; |
|
668 |
include_once WP_PLUGIN_DIR . '/' . $plugin; |
|
5 | 669 |
$plugin = $_wp_plugin_file; // Avoid stomping of the $plugin variable in a plugin. |
0 | 670 |
|
671 |
if ( ! $silent ) { |
|
672 |
/** |
|
5 | 673 |
* Fires before a plugin is activated. |
674 |
* |
|
675 |
* If a plugin is silently activated (such as during an update), |
|
676 |
* this hook does not fire. |
|
0 | 677 |
* |
678 |
* @since 2.9.0 |
|
679 |
* |
|
9 | 680 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
0 | 681 |
* @param bool $network_wide Whether to enable the plugin for all sites in the network |
16 | 682 |
* or just the current site. Multisite only. Default false. |
0 | 683 |
*/ |
684 |
do_action( 'activate_plugin', $plugin, $network_wide ); |
|
685 |
||
686 |
/** |
|
5 | 687 |
* Fires as a specific plugin is being activated. |
0 | 688 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
689 |
* This hook is the "activation" hook used internally by register_activation_hook(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
690 |
* The dynamic portion of the hook name, `$plugin`, refers to the plugin basename. |
5 | 691 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
692 |
* If a plugin is silently activated (such as during an update), this hook does not fire. |
0 | 693 |
* |
694 |
* @since 2.0.0 |
|
695 |
* |
|
696 |
* @param bool $network_wide Whether to enable the plugin for all sites in the network |
|
16 | 697 |
* or just the current site. Multisite only. Default false. |
0 | 698 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
699 |
do_action( "activate_{$plugin}", $network_wide ); |
0 | 700 |
} |
701 |
||
702 |
if ( $network_wide ) { |
|
9 | 703 |
$current = get_site_option( 'active_sitewide_plugins', array() ); |
704 |
$current[ $plugin ] = time(); |
|
0 | 705 |
update_site_option( 'active_sitewide_plugins', $current ); |
706 |
} else { |
|
9 | 707 |
$current = get_option( 'active_plugins', array() ); |
0 | 708 |
$current[] = $plugin; |
9 | 709 |
sort( $current ); |
710 |
update_option( 'active_plugins', $current ); |
|
0 | 711 |
} |
712 |
||
713 |
if ( ! $silent ) { |
|
714 |
/** |
|
5 | 715 |
* Fires after a plugin has been activated. |
716 |
* |
|
717 |
* If a plugin is silently activated (such as during an update), |
|
718 |
* this hook does not fire. |
|
0 | 719 |
* |
720 |
* @since 2.9.0 |
|
721 |
* |
|
9 | 722 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
0 | 723 |
* @param bool $network_wide Whether to enable the plugin for all sites in the network |
16 | 724 |
* or just the current site. Multisite only. Default false. |
0 | 725 |
*/ |
726 |
do_action( 'activated_plugin', $plugin, $network_wide ); |
|
727 |
} |
|
728 |
||
729 |
if ( ob_get_length() > 0 ) { |
|
730 |
$output = ob_get_clean(); |
|
9 | 731 |
return new WP_Error( 'unexpected_output', __( 'The plugin generated unexpected output.' ), $output ); |
0 | 732 |
} |
733 |
ob_end_clean(); |
|
734 |
} |
|
735 |
||
736 |
return null; |
|
737 |
} |
|
738 |
||
739 |
/** |
|
740 |
* Deactivate a single plugin or multiple plugins. |
|
741 |
* |
|
742 |
* The deactivation hook is disabled by the plugin upgrader by using the $silent |
|
743 |
* parameter. |
|
744 |
* |
|
745 |
* @since 2.5.0 |
|
746 |
* |
|
16 | 747 |
* @param string|string[] $plugins Single plugin or list of plugins to deactivate. |
748 |
* @param bool $silent Prevent calling deactivation hooks. Default false. |
|
749 |
* @param bool|null $network_wide Whether to deactivate the plugin for all sites in the network. |
|
750 |
* A value of null will deactivate plugins for both the network |
|
751 |
* and the current site. Multisite only. Default null. |
|
0 | 752 |
*/ |
753 |
function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) { |
|
9 | 754 |
if ( is_multisite() ) { |
0 | 755 |
$network_current = get_site_option( 'active_sitewide_plugins', array() ); |
9 | 756 |
} |
16 | 757 |
$current = get_option( 'active_plugins', array() ); |
758 |
$do_blog = false; |
|
759 |
$do_network = false; |
|
0 | 760 |
|
761 |
foreach ( (array) $plugins as $plugin ) { |
|
762 |
$plugin = plugin_basename( trim( $plugin ) ); |
|
9 | 763 |
if ( ! is_plugin_active( $plugin ) ) { |
0 | 764 |
continue; |
9 | 765 |
} |
0 | 766 |
|
767 |
$network_deactivating = false !== $network_wide && is_plugin_active_for_network( $plugin ); |
|
768 |
||
5 | 769 |
if ( ! $silent ) { |
0 | 770 |
/** |
5 | 771 |
* Fires before a plugin is deactivated. |
772 |
* |
|
773 |
* If a plugin is silently deactivated (such as during an update), |
|
774 |
* this hook does not fire. |
|
0 | 775 |
* |
776 |
* @since 2.9.0 |
|
777 |
* |
|
9 | 778 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
0 | 779 |
* @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network |
16 | 780 |
* or just the current site. Multisite only. Default false. |
0 | 781 |
*/ |
782 |
do_action( 'deactivate_plugin', $plugin, $network_deactivating ); |
|
5 | 783 |
} |
0 | 784 |
|
785 |
if ( false !== $network_wide ) { |
|
786 |
if ( is_plugin_active_for_network( $plugin ) ) { |
|
787 |
$do_network = true; |
|
788 |
unset( $network_current[ $plugin ] ); |
|
789 |
} elseif ( $network_wide ) { |
|
790 |
continue; |
|
791 |
} |
|
792 |
} |
|
793 |
||
794 |
if ( true !== $network_wide ) { |
|
16 | 795 |
$key = array_search( $plugin, $current, true ); |
0 | 796 |
if ( false !== $key ) { |
797 |
$do_blog = true; |
|
798 |
unset( $current[ $key ] ); |
|
799 |
} |
|
800 |
} |
|
801 |
||
9 | 802 |
if ( $do_blog && wp_is_recovery_mode() ) { |
803 |
list( $extension ) = explode( '/', $plugin ); |
|
804 |
wp_paused_plugins()->delete( $extension ); |
|
805 |
} |
|
806 |
||
0 | 807 |
if ( ! $silent ) { |
808 |
/** |
|
5 | 809 |
* Fires as a specific plugin is being deactivated. |
0 | 810 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
811 |
* This hook is the "deactivation" hook used internally by register_deactivation_hook(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
812 |
* The dynamic portion of the hook name, `$plugin`, refers to the plugin basename. |
5 | 813 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
814 |
* If a plugin is silently deactivated (such as during an update), this hook does not fire. |
0 | 815 |
* |
816 |
* @since 2.0.0 |
|
817 |
* |
|
818 |
* @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network |
|
16 | 819 |
* or just the current site. Multisite only. Default false. |
0 | 820 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
821 |
do_action( "deactivate_{$plugin}", $network_deactivating ); |
0 | 822 |
|
823 |
/** |
|
5 | 824 |
* Fires after a plugin is deactivated. |
825 |
* |
|
826 |
* If a plugin is silently deactivated (such as during an update), |
|
827 |
* this hook does not fire. |
|
0 | 828 |
* |
829 |
* @since 2.9.0 |
|
830 |
* |
|
9 | 831 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
16 | 832 |
* @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network |
5 | 833 |
* or just the current site. Multisite only. Default false. |
0 | 834 |
*/ |
835 |
do_action( 'deactivated_plugin', $plugin, $network_deactivating ); |
|
836 |
} |
|
837 |
} |
|
838 |
||
9 | 839 |
if ( $do_blog ) { |
840 |
update_option( 'active_plugins', $current ); |
|
841 |
} |
|
842 |
if ( $do_network ) { |
|
0 | 843 |
update_site_option( 'active_sitewide_plugins', $network_current ); |
9 | 844 |
} |
0 | 845 |
} |
846 |
||
847 |
/** |
|
848 |
* Activate multiple plugins. |
|
849 |
* |
|
850 |
* When WP_Error is returned, it does not mean that one of the plugins had |
|
16 | 851 |
* errors. It means that one or more of the plugin file paths were invalid. |
0 | 852 |
* |
853 |
* The execution will be halted as soon as one of the plugins has an error. |
|
854 |
* |
|
855 |
* @since 2.6.0 |
|
856 |
* |
|
16 | 857 |
* @param string|string[] $plugins Single plugin or list of plugins to activate. |
858 |
* @param string $redirect Redirect to page after successful activation. |
|
859 |
* @param bool $network_wide Whether to enable the plugin for all sites in the network. |
|
860 |
* Default false. |
|
861 |
* @param bool $silent Prevent calling activation hooks. Default false. |
|
0 | 862 |
* @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation. |
863 |
*/ |
|
864 |
function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) { |
|
9 | 865 |
if ( ! is_array( $plugins ) ) { |
866 |
$plugins = array( $plugins ); |
|
867 |
} |
|
0 | 868 |
|
869 |
$errors = array(); |
|
870 |
foreach ( $plugins as $plugin ) { |
|
9 | 871 |
if ( ! empty( $redirect ) ) { |
872 |
$redirect = add_query_arg( 'plugin', $plugin, $redirect ); |
|
873 |
} |
|
874 |
$result = activate_plugin( $plugin, $redirect, $network_wide, $silent ); |
|
875 |
if ( is_wp_error( $result ) ) { |
|
876 |
$errors[ $plugin ] = $result; |
|
877 |
} |
|
0 | 878 |
} |
879 |
||
9 | 880 |
if ( ! empty( $errors ) ) { |
881 |
return new WP_Error( 'plugins_invalid', __( 'One of the plugins is invalid.' ), $errors ); |
|
882 |
} |
|
0 | 883 |
|
884 |
return true; |
|
885 |
} |
|
886 |
||
887 |
/** |
|
5 | 888 |
* Remove directory and files of a plugin for a list of plugins. |
0 | 889 |
* |
890 |
* @since 2.6.0 |
|
891 |
* |
|
9 | 892 |
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
893 |
* |
9 | 894 |
* @param string[] $plugins List of plugin paths to delete, relative to the plugins directory. |
895 |
* @param string $deprecated Not used. |
|
896 |
* @return bool|null|WP_Error True on success, false if `$plugins` is empty, `WP_Error` on failure. |
|
897 |
* `null` if filesystem credentials are required to proceed. |
|
0 | 898 |
*/ |
5 | 899 |
function delete_plugins( $plugins, $deprecated = '' ) { |
0 | 900 |
global $wp_filesystem; |
901 |
||
9 | 902 |
if ( empty( $plugins ) ) { |
0 | 903 |
return false; |
9 | 904 |
} |
0 | 905 |
|
906 |
$checked = array(); |
|
9 | 907 |
foreach ( $plugins as $plugin ) { |
0 | 908 |
$checked[] = 'checked[]=' . $plugin; |
9 | 909 |
} |
0 | 910 |
|
9 | 911 |
$url = wp_nonce_url( 'plugins.php?action=delete-selected&verify-delete=1&' . implode( '&', $checked ), 'bulk-plugins' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
912 |
|
0 | 913 |
ob_start(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
914 |
$credentials = request_filesystem_credentials( $url ); |
9 | 915 |
$data = ob_get_clean(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
916 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
917 |
if ( false === $credentials ) { |
9 | 918 |
if ( ! empty( $data ) ) { |
16 | 919 |
require_once ABSPATH . 'wp-admin/admin-header.php'; |
0 | 920 |
echo $data; |
16 | 921 |
require_once ABSPATH . 'wp-admin/admin-footer.php'; |
0 | 922 |
exit; |
923 |
} |
|
924 |
return; |
|
925 |
} |
|
926 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
927 |
if ( ! WP_Filesystem( $credentials ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
928 |
ob_start(); |
16 | 929 |
// Failed to connect. Error and request again. |
930 |
request_filesystem_credentials( $url, '', true ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
931 |
$data = ob_get_clean(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
932 |
|
9 | 933 |
if ( ! empty( $data ) ) { |
16 | 934 |
require_once ABSPATH . 'wp-admin/admin-header.php'; |
0 | 935 |
echo $data; |
16 | 936 |
require_once ABSPATH . 'wp-admin/admin-footer.php'; |
0 | 937 |
exit; |
938 |
} |
|
939 |
return; |
|
940 |
} |
|
941 |
||
9 | 942 |
if ( ! is_object( $wp_filesystem ) ) { |
943 |
return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) ); |
|
944 |
} |
|
0 | 945 |
|
9 | 946 |
if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { |
947 |
return new WP_Error( 'fs_error', __( 'Filesystem error.' ), $wp_filesystem->errors ); |
|
948 |
} |
|
0 | 949 |
|
5 | 950 |
// Get the base plugin folder. |
0 | 951 |
$plugins_dir = $wp_filesystem->wp_plugins_dir(); |
5 | 952 |
if ( empty( $plugins_dir ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
953 |
return new WP_Error( 'fs_no_plugins_dir', __( 'Unable to locate WordPress plugin directory.' ) ); |
5 | 954 |
} |
0 | 955 |
|
956 |
$plugins_dir = trailingslashit( $plugins_dir ); |
|
957 |
||
5 | 958 |
$plugin_translations = wp_get_installed_translations( 'plugins' ); |
959 |
||
0 | 960 |
$errors = array(); |
961 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
962 |
foreach ( $plugins as $plugin_file ) { |
5 | 963 |
// Run Uninstall hook. |
964 |
if ( is_uninstallable_plugin( $plugin_file ) ) { |
|
9 | 965 |
uninstall_plugin( $plugin_file ); |
5 | 966 |
} |
0 | 967 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
968 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
969 |
* Fires immediately before a plugin deletion attempt. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
970 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
971 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
* |
9 | 973 |
* @param string $plugin_file Path to the plugin file relative to the plugins directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
974 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
975 |
do_action( 'delete_plugin', $plugin_file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
976 |
|
5 | 977 |
$this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin_file ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
978 |
|
0 | 979 |
// If plugin is in its own directory, recursively delete the directory. |
16 | 980 |
// Base check on if plugin includes directory separator AND that it's not the root plugin folder. |
981 |
if ( strpos( $plugin_file, '/' ) && $this_plugin_dir != $plugins_dir ) { |
|
5 | 982 |
$deleted = $wp_filesystem->delete( $this_plugin_dir, true ); |
983 |
} else { |
|
984 |
$deleted = $wp_filesystem->delete( $plugins_dir . $plugin_file ); |
|
985 |
} |
|
986 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
987 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
988 |
* Fires immediately after a plugin deletion attempt. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
989 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
990 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
991 |
* |
9 | 992 |
* @param string $plugin_file Path to the plugin file relative to the plugins directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
993 |
* @param bool $deleted Whether the plugin deletion was successful. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
994 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
995 |
do_action( 'deleted_plugin', $plugin_file, $deleted ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
996 |
|
5 | 997 |
if ( ! $deleted ) { |
998 |
$errors[] = $plugin_file; |
|
999 |
continue; |
|
1000 |
} |
|
1001 |
||
1002 |
// Remove language files, silently. |
|
1003 |
$plugin_slug = dirname( $plugin_file ); |
|
1004 |
if ( '.' !== $plugin_slug && ! empty( $plugin_translations[ $plugin_slug ] ) ) { |
|
1005 |
$translations = $plugin_translations[ $plugin_slug ]; |
|
0 | 1006 |
|
5 | 1007 |
foreach ( $translations as $translation => $data ) { |
1008 |
$wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.po' ); |
|
1009 |
$wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.mo' ); |
|
9 | 1010 |
|
1011 |
$json_translation_files = glob( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '-*.json' ); |
|
1012 |
if ( $json_translation_files ) { |
|
1013 |
array_map( array( $wp_filesystem, 'delete' ), $json_translation_files ); |
|
1014 |
} |
|
5 | 1015 |
} |
1016 |
} |
|
1017 |
} |
|
1018 |
||
1019 |
// Remove deleted plugins from the plugin updates list. |
|
16 | 1020 |
$current = get_site_transient( 'update_plugins' ); |
1021 |
if ( $current ) { |
|
5 | 1022 |
// Don't remove the plugins that weren't deleted. |
1023 |
$deleted = array_diff( $plugins, $errors ); |
|
1024 |
||
1025 |
foreach ( $deleted as $plugin_file ) { |
|
1026 |
unset( $current->response[ $plugin_file ] ); |
|
1027 |
} |
|
1028 |
||
1029 |
set_site_transient( 'update_plugins', $current ); |
|
0 | 1030 |
} |
1031 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1032 |
if ( ! empty( $errors ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1033 |
if ( 1 === count( $errors ) ) { |
16 | 1034 |
/* translators: %s: Plugin filename. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1035 |
$message = __( 'Could not fully remove the plugin %s.' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1036 |
} else { |
16 | 1037 |
/* translators: %s: Comma-separated list of plugin filenames. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1038 |
$message = __( 'Could not fully remove the plugins %s.' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1039 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1040 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1041 |
return new WP_Error( 'could_not_remove_plugin', sprintf( $message, implode( ', ', $errors ) ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1042 |
} |
0 | 1043 |
|
1044 |
return true; |
|
1045 |
} |
|
1046 |
||
1047 |
/** |
|
1048 |
* Validate active plugins |
|
1049 |
* |
|
1050 |
* Validate all active plugins, deactivates invalid and |
|
1051 |
* returns an array of deactivated ones. |
|
1052 |
* |
|
1053 |
* @since 2.5.0 |
|
16 | 1054 |
* @return WP_Error[] Array of plugin errors keyed by plugin file name. |
0 | 1055 |
*/ |
1056 |
function validate_active_plugins() { |
|
1057 |
$plugins = get_option( 'active_plugins', array() ); |
|
5 | 1058 |
// Validate vartype: array. |
0 | 1059 |
if ( ! is_array( $plugins ) ) { |
1060 |
update_option( 'active_plugins', array() ); |
|
1061 |
$plugins = array(); |
|
1062 |
} |
|
1063 |
||
5 | 1064 |
if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) { |
0 | 1065 |
$network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() ); |
9 | 1066 |
$plugins = array_merge( $plugins, array_keys( $network_plugins ) ); |
0 | 1067 |
} |
1068 |
||
9 | 1069 |
if ( empty( $plugins ) ) { |
5 | 1070 |
return array(); |
9 | 1071 |
} |
0 | 1072 |
|
1073 |
$invalid = array(); |
|
1074 |
||
5 | 1075 |
// Invalid plugins get deactivated. |
0 | 1076 |
foreach ( $plugins as $plugin ) { |
1077 |
$result = validate_plugin( $plugin ); |
|
1078 |
if ( is_wp_error( $result ) ) { |
|
9 | 1079 |
$invalid[ $plugin ] = $result; |
0 | 1080 |
deactivate_plugins( $plugin, true ); |
1081 |
} |
|
1082 |
} |
|
1083 |
return $invalid; |
|
1084 |
} |
|
1085 |
||
1086 |
/** |
|
1087 |
* Validate the plugin path. |
|
1088 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1089 |
* Checks that the main plugin file exists and is a valid plugin. See validate_file(). |
0 | 1090 |
* |
1091 |
* @since 2.5.0 |
|
1092 |
* |
|
9 | 1093 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
16 | 1094 |
* @return int|WP_Error 0 on success, WP_Error on failure. |
0 | 1095 |
*/ |
9 | 1096 |
function validate_plugin( $plugin ) { |
1097 |
if ( validate_file( $plugin ) ) { |
|
1098 |
return new WP_Error( 'plugin_invalid', __( 'Invalid plugin path.' ) ); |
|
1099 |
} |
|
1100 |
if ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) { |
|
1101 |
return new WP_Error( 'plugin_not_found', __( 'Plugin file does not exist.' ) ); |
|
1102 |
} |
|
0 | 1103 |
|
1104 |
$installed_plugins = get_plugins(); |
|
9 | 1105 |
if ( ! isset( $installed_plugins[ $plugin ] ) ) { |
1106 |
return new WP_Error( 'no_plugin_header', __( 'The plugin does not have a valid header.' ) ); |
|
1107 |
} |
|
0 | 1108 |
return 0; |
1109 |
} |
|
1110 |
||
1111 |
/** |
|
16 | 1112 |
* Validates the plugin requirements for WordPress version and PHP version. |
1113 |
* |
|
1114 |
* Uses the information from `Requires at least` and `Requires PHP` headers |
|
1115 |
* defined in the plugin's main PHP file. |
|
1116 |
* |
|
1117 |
* If the headers are not present in the plugin's main PHP file, |
|
1118 |
* `readme.txt` is also checked as a fallback. |
|
9 | 1119 |
* |
1120 |
* @since 5.2.0 |
|
16 | 1121 |
* @since 5.3.0 Added support for reading the headers from the plugin's |
1122 |
* main PHP file, with `readme.txt` as a fallback. |
|
9 | 1123 |
* |
1124 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
|
1125 |
* @return true|WP_Error True if requirements are met, WP_Error on failure. |
|
1126 |
*/ |
|
1127 |
function validate_plugin_requirements( $plugin ) { |
|
16 | 1128 |
$plugin_headers = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); |
1129 |
||
1130 |
$requirements = array( |
|
1131 |
'requires' => ! empty( $plugin_headers['RequiresWP'] ) ? $plugin_headers['RequiresWP'] : '', |
|
1132 |
'requires_php' => ! empty( $plugin_headers['RequiresPHP'] ) ? $plugin_headers['RequiresPHP'] : '', |
|
1133 |
); |
|
1134 |
||
9 | 1135 |
$readme_file = WP_PLUGIN_DIR . '/' . dirname( $plugin ) . '/readme.txt'; |
1136 |
||
1137 |
if ( file_exists( $readme_file ) ) { |
|
16 | 1138 |
$readme_headers = get_file_data( |
9 | 1139 |
$readme_file, |
1140 |
array( |
|
1141 |
'requires' => 'Requires at least', |
|
1142 |
'requires_php' => 'Requires PHP', |
|
1143 |
), |
|
1144 |
'plugin' |
|
1145 |
); |
|
16 | 1146 |
|
1147 |
$requirements = array_merge( $readme_headers, $requirements ); |
|
9 | 1148 |
} |
1149 |
||
16 | 1150 |
$compatible_wp = is_wp_version_compatible( $requirements['requires'] ); |
1151 |
$compatible_php = is_php_version_compatible( $requirements['requires_php'] ); |
|
9 | 1152 |
|
16 | 1153 |
/* translators: %s: URL to Update PHP page. */ |
1154 |
$php_update_message = '</p><p>' . sprintf( |
|
1155 |
__( '<a href="%s">Learn more about updating PHP</a>.' ), |
|
1156 |
esc_url( wp_get_update_php_url() ) |
|
1157 |
); |
|
9 | 1158 |
|
16 | 1159 |
$annotation = wp_get_update_php_annotation(); |
1160 |
||
1161 |
if ( $annotation ) { |
|
1162 |
$php_update_message .= '</p><p><em>' . $annotation . '</em>'; |
|
1163 |
} |
|
1164 |
||
1165 |
if ( ! $compatible_wp && ! $compatible_php ) { |
|
9 | 1166 |
return new WP_Error( |
1167 |
'plugin_wp_php_incompatible', |
|
16 | 1168 |
'<p>' . sprintf( |
1169 |
/* translators: 1: Current WordPress version, 2: Current PHP version, 3: Plugin name, 4: Required WordPress version, 5: Required PHP version. */ |
|
1170 |
_x( '<strong>Error:</strong> Current versions of WordPress (%1$s) and PHP (%2$s) do not meet minimum requirements for %3$s. The plugin requires WordPress %4$s and PHP %5$s.', 'plugin' ), |
|
1171 |
get_bloginfo( 'version' ), |
|
1172 |
phpversion(), |
|
1173 |
$plugin_headers['Name'], |
|
1174 |
$requirements['requires'], |
|
1175 |
$requirements['requires_php'] |
|
1176 |
) . $php_update_message . '</p>' |
|
9 | 1177 |
); |
16 | 1178 |
} elseif ( ! $compatible_php ) { |
9 | 1179 |
return new WP_Error( |
1180 |
'plugin_php_incompatible', |
|
16 | 1181 |
'<p>' . sprintf( |
1182 |
/* translators: 1: Current PHP version, 2: Plugin name, 3: Required PHP version. */ |
|
1183 |
_x( '<strong>Error:</strong> Current PHP version (%1$s) does not meet minimum requirements for %2$s. The plugin requires PHP %3$s.', 'plugin' ), |
|
1184 |
phpversion(), |
|
1185 |
$plugin_headers['Name'], |
|
1186 |
$requirements['requires_php'] |
|
1187 |
) . $php_update_message . '</p>' |
|
9 | 1188 |
); |
16 | 1189 |
} elseif ( ! $compatible_wp ) { |
9 | 1190 |
return new WP_Error( |
1191 |
'plugin_wp_incompatible', |
|
16 | 1192 |
'<p>' . sprintf( |
1193 |
/* translators: 1: Current WordPress version, 2: Plugin name, 3: Required WordPress version. */ |
|
1194 |
_x( '<strong>Error:</strong> Current WordPress version (%1$s) does not meet minimum requirements for %2$s. The plugin requires WordPress %3$s.', 'plugin' ), |
|
1195 |
get_bloginfo( 'version' ), |
|
1196 |
$plugin_headers['Name'], |
|
1197 |
$requirements['requires'] |
|
1198 |
) . '</p>' |
|
9 | 1199 |
); |
1200 |
} |
|
1201 |
||
1202 |
return true; |
|
1203 |
} |
|
1204 |
||
1205 |
/** |
|
0 | 1206 |
* Whether the plugin can be uninstalled. |
1207 |
* |
|
1208 |
* @since 2.7.0 |
|
1209 |
* |
|
9 | 1210 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
0 | 1211 |
* @return bool Whether plugin can be uninstalled. |
1212 |
*/ |
|
9 | 1213 |
function is_uninstallable_plugin( $plugin ) { |
1214 |
$file = plugin_basename( $plugin ); |
|
0 | 1215 |
|
9 | 1216 |
$uninstallable_plugins = (array) get_option( 'uninstall_plugins' ); |
1217 |
if ( isset( $uninstallable_plugins[ $file ] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname( $file ) . '/uninstall.php' ) ) { |
|
0 | 1218 |
return true; |
9 | 1219 |
} |
0 | 1220 |
|
1221 |
return false; |
|
1222 |
} |
|
1223 |
||
1224 |
/** |
|
1225 |
* Uninstall a single plugin. |
|
1226 |
* |
|
1227 |
* Calls the uninstall hook, if it is available. |
|
1228 |
* |
|
1229 |
* @since 2.7.0 |
|
1230 |
* |
|
9 | 1231 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1232 |
* @return true True if a plugin's uninstall.php file has been found and included. |
0 | 1233 |
*/ |
9 | 1234 |
function uninstall_plugin( $plugin ) { |
1235 |
$file = plugin_basename( $plugin ); |
|
0 | 1236 |
|
9 | 1237 |
$uninstallable_plugins = (array) get_option( 'uninstall_plugins' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1238 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1239 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1240 |
* Fires in uninstall_plugin() immediately before the plugin is uninstalled. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1241 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1242 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1243 |
* |
9 | 1244 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1245 |
* @param array $uninstallable_plugins Uninstallable plugins. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1246 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1247 |
do_action( 'pre_uninstall_plugin', $plugin, $uninstallable_plugins ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1248 |
|
9 | 1249 |
if ( file_exists( WP_PLUGIN_DIR . '/' . dirname( $file ) . '/uninstall.php' ) ) { |
1250 |
if ( isset( $uninstallable_plugins[ $file ] ) ) { |
|
1251 |
unset( $uninstallable_plugins[ $file ] ); |
|
1252 |
update_option( 'uninstall_plugins', $uninstallable_plugins ); |
|
0 | 1253 |
} |
9 | 1254 |
unset( $uninstallable_plugins ); |
0 | 1255 |
|
9 | 1256 |
define( 'WP_UNINSTALL_PLUGIN', $file ); |
16 | 1257 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1258 |
wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file ); |
16 | 1259 |
include_once WP_PLUGIN_DIR . '/' . dirname( $file ) . '/uninstall.php'; |
0 | 1260 |
|
1261 |
return true; |
|
1262 |
} |
|
1263 |
||
9 | 1264 |
if ( isset( $uninstallable_plugins[ $file ] ) ) { |
1265 |
$callable = $uninstallable_plugins[ $file ]; |
|
1266 |
unset( $uninstallable_plugins[ $file ] ); |
|
1267 |
update_option( 'uninstall_plugins', $uninstallable_plugins ); |
|
1268 |
unset( $uninstallable_plugins ); |
|
0 | 1269 |
|
5 | 1270 |
wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file ); |
16 | 1271 |
include_once WP_PLUGIN_DIR . '/' . $file; |
0 | 1272 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1273 |
add_action( "uninstall_{$file}", $callable ); |
0 | 1274 |
|
1275 |
/** |
|
1276 |
* Fires in uninstall_plugin() once the plugin has been uninstalled. |
|
1277 |
* |
|
1278 |
* The action concatenates the 'uninstall_' prefix with the basename of the |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1279 |
* plugin passed to uninstall_plugin() to create a dynamically-named action. |
0 | 1280 |
* |
1281 |
* @since 2.7.0 |
|
1282 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1283 |
do_action( "uninstall_{$file}" ); |
0 | 1284 |
} |
1285 |
} |
|
1286 |
||
1287 |
// |
|
16 | 1288 |
// Menu. |
0 | 1289 |
// |
1290 |
||
1291 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1292 |
* Add a top-level menu page. |
0 | 1293 |
* |
1294 |
* This function takes a capability which will be used to determine whether |
|
1295 |
* or not a page is included in the menu. |
|
1296 |
* |
|
1297 |
* The function which is hooked in to handle the output of the page must check |
|
1298 |
* that the user has the required capability as well. |
|
1299 |
* |
|
9 | 1300 |
* @since 1.5.0 |
1301 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1302 |
* @global array $menu |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1303 |
* @global array $admin_page_hooks |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1304 |
* @global array $_registered_pages |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1305 |
* @global array $_parent_pages |
0 | 1306 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1307 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1308 |
* @param string $menu_title The text to be used for the menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1309 |
* @param string $capability The capability required for this menu to be displayed to the user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1310 |
* @param string $menu_slug The slug name to refer to this menu by. Should be unique for this menu page and only |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1311 |
* include lowercase alphanumeric, dashes, and underscores characters to be compatible |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1312 |
* with sanitize_key(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1313 |
* @param callable $function The function to be called to output the content for this page. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1314 |
* @param string $icon_url The URL to the icon to be used for this menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1315 |
* * Pass a base64-encoded SVG using a data URI, which will be colored to match |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1316 |
* the color scheme. This should begin with 'data:image/svg+xml;base64,'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1317 |
* * Pass the name of a Dashicons helper class to use a font icon, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1318 |
* e.g. 'dashicons-chart-pie'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1319 |
* * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS. |
16 | 1320 |
* @param int $position The position in the menu order this item should appear. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1321 |
* @return string The resulting page's hook_suffix. |
0 | 1322 |
*/ |
1323 |
function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) { |
|
1324 |
global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages; |
|
1325 |
||
1326 |
$menu_slug = plugin_basename( $menu_slug ); |
|
1327 |
||
9 | 1328 |
$admin_page_hooks[ $menu_slug ] = sanitize_title( $menu_title ); |
0 | 1329 |
|
1330 |
$hookname = get_plugin_page_hookname( $menu_slug, '' ); |
|
1331 |
||
9 | 1332 |
if ( ! empty( $function ) && ! empty( $hookname ) && current_user_can( $capability ) ) { |
0 | 1333 |
add_action( $hookname, $function ); |
9 | 1334 |
} |
0 | 1335 |
|
9 | 1336 |
if ( empty( $icon_url ) ) { |
1337 |
$icon_url = 'dashicons-admin-generic'; |
|
0 | 1338 |
$icon_class = 'menu-icon-generic '; |
1339 |
} else { |
|
9 | 1340 |
$icon_url = set_url_scheme( $icon_url ); |
0 | 1341 |
$icon_class = ''; |
1342 |
} |
|
1343 |
||
1344 |
$new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url ); |
|
1345 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1346 |
if ( null === $position ) { |
0 | 1347 |
$menu[] = $new_menu; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1348 |
} elseif ( isset( $menu[ "$position" ] ) ) { |
9 | 1349 |
$position = $position + substr( base_convert( md5( $menu_slug . $menu_title ), 16, 10 ), -5 ) * 0.00001; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1350 |
$menu[ "$position" ] = $new_menu; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1351 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1352 |
$menu[ $position ] = $new_menu; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1353 |
} |
0 | 1354 |
|
9 | 1355 |
$_registered_pages[ $hookname ] = true; |
0 | 1356 |
|
16 | 1357 |
// No parent as top level. |
9 | 1358 |
$_parent_pages[ $menu_slug ] = false; |
0 | 1359 |
|
1360 |
return $hookname; |
|
1361 |
} |
|
1362 |
||
1363 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1364 |
* Add a submenu page. |
0 | 1365 |
* |
1366 |
* This function takes a capability which will be used to determine whether |
|
1367 |
* or not a page is included in the menu. |
|
1368 |
* |
|
1369 |
* The function which is hooked in to handle the output of the page must check |
|
1370 |
* that the user has the required capability as well. |
|
1371 |
* |
|
9 | 1372 |
* @since 1.5.0 |
16 | 1373 |
* @since 5.3.0 Added the `$position` parameter. |
9 | 1374 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1375 |
* @global array $submenu |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1376 |
* @global array $menu |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1377 |
* @global array $_wp_real_parent_file |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1378 |
* @global bool $_wp_submenu_nopriv |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1379 |
* @global array $_registered_pages |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1380 |
* @global array $_parent_pages |
0 | 1381 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1382 |
* @param string $parent_slug The slug name for the parent menu (or the file name of a standard |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1383 |
* WordPress admin page). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1384 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1385 |
* is selected. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1386 |
* @param string $menu_title The text to be used for the menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1387 |
* @param string $capability The capability required for this menu to be displayed to the user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1388 |
* @param string $menu_slug The slug name to refer to this menu by. Should be unique for this menu |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1389 |
* and only include lowercase alphanumeric, dashes, and underscores characters |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1390 |
* to be compatible with sanitize_key(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1391 |
* @param callable $function The function to be called to output the content for this page. |
16 | 1392 |
* @param int $position The position in the menu order this item should appear. |
1393 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
0 | 1394 |
*/ |
16 | 1395 |
function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1397 |
$_registered_pages, $_parent_pages; |
0 | 1398 |
|
9 | 1399 |
$menu_slug = plugin_basename( $menu_slug ); |
1400 |
$parent_slug = plugin_basename( $parent_slug ); |
|
0 | 1401 |
|
9 | 1402 |
if ( isset( $_wp_real_parent_file[ $parent_slug ] ) ) { |
1403 |
$parent_slug = $_wp_real_parent_file[ $parent_slug ]; |
|
1404 |
} |
|
0 | 1405 |
|
9 | 1406 |
if ( ! current_user_can( $capability ) ) { |
1407 |
$_wp_submenu_nopriv[ $parent_slug ][ $menu_slug ] = true; |
|
0 | 1408 |
return false; |
1409 |
} |
|
1410 |
||
5 | 1411 |
/* |
1412 |
* If the parent doesn't already have a submenu, add a link to the parent |
|
1413 |
* as the first item in the submenu. If the submenu file is the same as the |
|
1414 |
* parent file someone is trying to link back to the parent manually. In |
|
1415 |
* this case, don't automatically add a link back to avoid duplication. |
|
1416 |
*/ |
|
9 | 1417 |
if ( ! isset( $submenu[ $parent_slug ] ) && $menu_slug != $parent_slug ) { |
1418 |
foreach ( (array) $menu as $parent_menu ) { |
|
1419 |
if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) ) { |
|
1420 |
$submenu[ $parent_slug ][] = array_slice( $parent_menu, 0, 4 ); |
|
1421 |
} |
|
0 | 1422 |
} |
1423 |
} |
|
1424 |
||
16 | 1425 |
$new_sub_menu = array( $menu_title, $capability, $menu_slug, $page_title ); |
1426 |
if ( ! is_int( $position ) ) { |
|
1427 |
if ( null !== $position ) { |
|
1428 |
_doing_it_wrong( |
|
1429 |
__FUNCTION__, |
|
1430 |
sprintf( |
|
1431 |
/* translators: %s: add_submenu_page() */ |
|
1432 |
__( 'The seventh parameter passed to %s should be an integer representing menu position.' ), |
|
1433 |
'<code>add_submenu_page()</code>' |
|
1434 |
), |
|
1435 |
'5.3.0' |
|
1436 |
); |
|
1437 |
} |
|
1438 |
||
1439 |
$submenu[ $parent_slug ][] = $new_sub_menu; |
|
1440 |
} else { |
|
1441 |
// Append the submenu if the parent item is not present in the submenu, |
|
1442 |
// or if position is equal or higher than the number of items in the array. |
|
1443 |
if ( ! isset( $submenu[ $parent_slug ] ) || $position >= count( $submenu[ $parent_slug ] ) ) { |
|
1444 |
$submenu[ $parent_slug ][] = $new_sub_menu; |
|
1445 |
} else { |
|
1446 |
// Test for a negative position. |
|
1447 |
$position = max( $position, 0 ); |
|
1448 |
if ( 0 === $position ) { |
|
1449 |
// For negative or `0` positions, prepend the submenu. |
|
1450 |
array_unshift( $submenu[ $parent_slug ], $new_sub_menu ); |
|
1451 |
} else { |
|
1452 |
// Grab all of the items before the insertion point. |
|
1453 |
$before_items = array_slice( $submenu[ $parent_slug ], 0, $position, true ); |
|
1454 |
// Grab all of the items after the insertion point. |
|
1455 |
$after_items = array_slice( $submenu[ $parent_slug ], $position, null, true ); |
|
1456 |
// Add the new item. |
|
1457 |
$before_items[] = $new_sub_menu; |
|
1458 |
// Merge the items. |
|
1459 |
$submenu[ $parent_slug ] = array_merge( $before_items, $after_items ); |
|
1460 |
} |
|
1461 |
} |
|
1462 |
} |
|
1463 |
// Sort the parent array. |
|
1464 |
ksort( $submenu[ $parent_slug ] ); |
|
0 | 1465 |
|
9 | 1466 |
$hookname = get_plugin_page_hookname( $menu_slug, $parent_slug ); |
1467 |
if ( ! empty( $function ) && ! empty( $hookname ) ) { |
|
0 | 1468 |
add_action( $hookname, $function ); |
9 | 1469 |
} |
0 | 1470 |
|
9 | 1471 |
$_registered_pages[ $hookname ] = true; |
5 | 1472 |
|
1473 |
/* |
|
9 | 1474 |
* Backward-compatibility for plugins using add_management_page(). |
1475 |
* See wp-admin/admin.php for redirect from edit.php to tools.php. |
|
5 | 1476 |
*/ |
16 | 1477 |
if ( 'tools.php' === $parent_slug ) { |
9 | 1478 |
$_registered_pages[ get_plugin_page_hookname( $menu_slug, 'edit.php' ) ] = true; |
1479 |
} |
|
0 | 1480 |
|
5 | 1481 |
// No parent as top level. |
9 | 1482 |
$_parent_pages[ $menu_slug ] = $parent_slug; |
0 | 1483 |
|
1484 |
return $hookname; |
|
1485 |
} |
|
1486 |
||
1487 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1488 |
* Add submenu page to the Tools main menu. |
0 | 1489 |
* |
1490 |
* This function takes a capability which will be used to determine whether |
|
1491 |
* or not a page is included in the menu. |
|
1492 |
* |
|
1493 |
* The function which is hooked in to handle the output of the page must check |
|
1494 |
* that the user has the required capability as well. |
|
1495 |
* |
|
9 | 1496 |
* @since 1.5.0 |
16 | 1497 |
* @since 5.3.0 Added the `$position` parameter. |
9 | 1498 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1499 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1500 |
* @param string $menu_title The text to be used for the menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1501 |
* @param string $capability The capability required for this menu to be displayed to the user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1502 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1503 |
* @param callable $function The function to be called to output the content for this page. |
16 | 1504 |
* @param int $position The position in the menu order this item should appear. |
1505 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
0 | 1506 |
*/ |
16 | 1507 |
function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1508 |
return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
0 | 1509 |
} |
1510 |
||
1511 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1512 |
* Add submenu page to the Settings main menu. |
0 | 1513 |
* |
1514 |
* This function takes a capability which will be used to determine whether |
|
1515 |
* or not a page is included in the menu. |
|
1516 |
* |
|
1517 |
* The function which is hooked in to handle the output of the page must check |
|
1518 |
* that the user has the required capability as well. |
|
1519 |
* |
|
9 | 1520 |
* @since 1.5.0 |
16 | 1521 |
* @since 5.3.0 Added the `$position` parameter. |
9 | 1522 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1523 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1524 |
* @param string $menu_title The text to be used for the menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1525 |
* @param string $capability The capability required for this menu to be displayed to the user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1526 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1527 |
* @param callable $function The function to be called to output the content for this page. |
16 | 1528 |
* @param int $position The position in the menu order this item should appear. |
1529 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
0 | 1530 |
*/ |
16 | 1531 |
function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1532 |
return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
0 | 1533 |
} |
1534 |
||
1535 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1536 |
* Add submenu page to the Appearance main menu. |
0 | 1537 |
* |
1538 |
* This function takes a capability which will be used to determine whether |
|
1539 |
* or not a page is included in the menu. |
|
1540 |
* |
|
1541 |
* The function which is hooked in to handle the output of the page must check |
|
1542 |
* that the user has the required capability as well. |
|
1543 |
* |
|
9 | 1544 |
* @since 2.0.0 |
16 | 1545 |
* @since 5.3.0 Added the `$position` parameter. |
9 | 1546 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1547 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1548 |
* @param string $menu_title The text to be used for the menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1549 |
* @param string $capability The capability required for this menu to be displayed to the user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1550 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1551 |
* @param callable $function The function to be called to output the content for this page. |
16 | 1552 |
* @param int $position The position in the menu order this item should appear. |
1553 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
0 | 1554 |
*/ |
16 | 1555 |
function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1556 |
return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
0 | 1557 |
} |
1558 |
||
1559 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1560 |
* Add submenu page to the Plugins main menu. |
0 | 1561 |
* |
1562 |
* This function takes a capability which will be used to determine whether |
|
1563 |
* or not a page is included in the menu. |
|
1564 |
* |
|
1565 |
* The function which is hooked in to handle the output of the page must check |
|
1566 |
* that the user has the required capability as well. |
|
1567 |
* |
|
9 | 1568 |
* @since 3.0.0 |
16 | 1569 |
* @since 5.3.0 Added the `$position` parameter. |
9 | 1570 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1571 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1572 |
* @param string $menu_title The text to be used for the menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1573 |
* @param string $capability The capability required for this menu to be displayed to the user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1574 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1575 |
* @param callable $function The function to be called to output the content for this page. |
16 | 1576 |
* @param int $position The position in the menu order this item should appear. |
1577 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
0 | 1578 |
*/ |
16 | 1579 |
function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1580 |
return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
0 | 1581 |
} |
1582 |
||
1583 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1584 |
* Add submenu page to the Users/Profile main menu. |
0 | 1585 |
* |
1586 |
* This function takes a capability which will be used to determine whether |
|
1587 |
* or not a page is included in the menu. |
|
1588 |
* |
|
1589 |
* The function which is hooked in to handle the output of the page must check |
|
1590 |
* that the user has the required capability as well. |
|
1591 |
* |
|
9 | 1592 |
* @since 2.1.3 |
16 | 1593 |
* @since 5.3.0 Added the `$position` parameter. |
9 | 1594 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1595 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1596 |
* @param string $menu_title The text to be used for the menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1597 |
* @param string $capability The capability required for this menu to be displayed to the user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1598 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1599 |
* @param callable $function The function to be called to output the content for this page. |
16 | 1600 |
* @param int $position The position in the menu order this item should appear. |
1601 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
0 | 1602 |
*/ |
16 | 1603 |
function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
9 | 1604 |
if ( current_user_can( 'edit_users' ) ) { |
0 | 1605 |
$parent = 'users.php'; |
9 | 1606 |
} else { |
0 | 1607 |
$parent = 'profile.php'; |
9 | 1608 |
} |
16 | 1609 |
return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
0 | 1610 |
} |
16 | 1611 |
|
0 | 1612 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1613 |
* Add submenu page to the Dashboard main menu. |
0 | 1614 |
* |
1615 |
* This function takes a capability which will be used to determine whether |
|
1616 |
* or not a page is included in the menu. |
|
1617 |
* |
|
1618 |
* The function which is hooked in to handle the output of the page must check |
|
1619 |
* that the user has the required capability as well. |
|
1620 |
* |
|
9 | 1621 |
* @since 2.7.0 |
16 | 1622 |
* @since 5.3.0 Added the `$position` parameter. |
9 | 1623 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1624 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1625 |
* @param string $menu_title The text to be used for the menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1626 |
* @param string $capability The capability required for this menu to be displayed to the user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1627 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1628 |
* @param callable $function The function to be called to output the content for this page. |
16 | 1629 |
* @param int $position The position in the menu order this item should appear. |
1630 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
0 | 1631 |
*/ |
16 | 1632 |
function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1633 |
return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
0 | 1634 |
} |
1635 |
||
1636 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1637 |
* Add submenu page to the Posts main menu. |
0 | 1638 |
* |
1639 |
* This function takes a capability which will be used to determine whether |
|
1640 |
* or not a page is included in the menu. |
|
1641 |
* |
|
1642 |
* The function which is hooked in to handle the output of the page must check |
|
1643 |
* that the user has the required capability as well. |
|
1644 |
* |
|
9 | 1645 |
* @since 2.7.0 |
16 | 1646 |
* @since 5.3.0 Added the `$position` parameter. |
9 | 1647 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1648 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1649 |
* @param string $menu_title The text to be used for the menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1650 |
* @param string $capability The capability required for this menu to be displayed to the user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1651 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1652 |
* @param callable $function The function to be called to output the content for this page. |
16 | 1653 |
* @param int $position The position in the menu order this item should appear. |
1654 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
0 | 1655 |
*/ |
16 | 1656 |
function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1657 |
return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
0 | 1658 |
} |
1659 |
||
1660 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1661 |
* Add submenu page to the Media main menu. |
0 | 1662 |
* |
1663 |
* This function takes a capability which will be used to determine whether |
|
1664 |
* or not a page is included in the menu. |
|
1665 |
* |
|
1666 |
* The function which is hooked in to handle the output of the page must check |
|
1667 |
* that the user has the required capability as well. |
|
1668 |
* |
|
9 | 1669 |
* @since 2.7.0 |
16 | 1670 |
* @since 5.3.0 Added the `$position` parameter. |
9 | 1671 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1672 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1673 |
* @param string $menu_title The text to be used for the menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1674 |
* @param string $capability The capability required for this menu to be displayed to the user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1675 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1676 |
* @param callable $function The function to be called to output the content for this page. |
16 | 1677 |
* @param int $position The position in the menu order this item should appear. |
1678 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
0 | 1679 |
*/ |
16 | 1680 |
function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1681 |
return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
0 | 1682 |
} |
1683 |
||
1684 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1685 |
* Add submenu page to the Links main menu. |
0 | 1686 |
* |
1687 |
* This function takes a capability which will be used to determine whether |
|
1688 |
* or not a page is included in the menu. |
|
1689 |
* |
|
1690 |
* The function which is hooked in to handle the output of the page must check |
|
1691 |
* that the user has the required capability as well. |
|
1692 |
* |
|
9 | 1693 |
* @since 2.7.0 |
16 | 1694 |
* @since 5.3.0 Added the `$position` parameter. |
9 | 1695 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1696 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1697 |
* @param string $menu_title The text to be used for the menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1698 |
* @param string $capability The capability required for this menu to be displayed to the user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1699 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1700 |
* @param callable $function The function to be called to output the content for this page. |
16 | 1701 |
* @param int $position The position in the menu order this item should appear. |
1702 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
0 | 1703 |
*/ |
16 | 1704 |
function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1705 |
return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
0 | 1706 |
} |
1707 |
||
1708 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1709 |
* Add submenu page to the Pages main menu. |
0 | 1710 |
* |
1711 |
* This function takes a capability which will be used to determine whether |
|
1712 |
* or not a page is included in the menu. |
|
1713 |
* |
|
1714 |
* The function which is hooked in to handle the output of the page must check |
|
1715 |
* that the user has the required capability as well. |
|
1716 |
* |
|
9 | 1717 |
* @since 2.7.0 |
16 | 1718 |
* @since 5.3.0 Added the `$position` parameter. |
9 | 1719 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1720 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1721 |
* @param string $menu_title The text to be used for the menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1722 |
* @param string $capability The capability required for this menu to be displayed to the user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1723 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1724 |
* @param callable $function The function to be called to output the content for this page. |
16 | 1725 |
* @param int $position The position in the menu order this item should appear. |
1726 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1727 |
*/ |
16 | 1728 |
function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1729 |
return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
0 | 1730 |
} |
1731 |
||
1732 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1733 |
* Add submenu page to the Comments main menu. |
0 | 1734 |
* |
1735 |
* This function takes a capability which will be used to determine whether |
|
1736 |
* or not a page is included in the menu. |
|
1737 |
* |
|
1738 |
* The function which is hooked in to handle the output of the page must check |
|
1739 |
* that the user has the required capability as well. |
|
1740 |
* |
|
9 | 1741 |
* @since 2.7.0 |
16 | 1742 |
* @since 5.3.0 Added the `$position` parameter. |
9 | 1743 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1744 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1745 |
* @param string $menu_title The text to be used for the menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1746 |
* @param string $capability The capability required for this menu to be displayed to the user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1747 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1748 |
* @param callable $function The function to be called to output the content for this page. |
16 | 1749 |
* @param int $position The position in the menu order this item should appear. |
1750 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1751 |
*/ |
16 | 1752 |
function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1753 |
return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
0 | 1754 |
} |
1755 |
||
1756 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1757 |
* Remove a top-level admin menu. |
0 | 1758 |
* |
1759 |
* @since 3.1.0 |
|
1760 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1761 |
* @global array $menu |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1762 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1763 |
* @param string $menu_slug The slug of the menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1764 |
* @return array|bool The removed menu on success, false if not found. |
0 | 1765 |
*/ |
1766 |
function remove_menu_page( $menu_slug ) { |
|
1767 |
global $menu; |
|
1768 |
||
1769 |
foreach ( $menu as $i => $item ) { |
|
1770 |
if ( $menu_slug == $item[2] ) { |
|
9 | 1771 |
unset( $menu[ $i ] ); |
0 | 1772 |
return $item; |
1773 |
} |
|
1774 |
} |
|
1775 |
||
1776 |
return false; |
|
1777 |
} |
|
1778 |
||
1779 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1780 |
* Remove an admin submenu. |
0 | 1781 |
* |
1782 |
* @since 3.1.0 |
|
1783 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1784 |
* @global array $submenu |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1785 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1786 |
* @param string $menu_slug The slug for the parent menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1787 |
* @param string $submenu_slug The slug of the submenu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1788 |
* @return array|bool The removed submenu on success, false if not found. |
0 | 1789 |
*/ |
1790 |
function remove_submenu_page( $menu_slug, $submenu_slug ) { |
|
1791 |
global $submenu; |
|
1792 |
||
9 | 1793 |
if ( ! isset( $submenu[ $menu_slug ] ) ) { |
0 | 1794 |
return false; |
9 | 1795 |
} |
0 | 1796 |
|
9 | 1797 |
foreach ( $submenu[ $menu_slug ] as $i => $item ) { |
0 | 1798 |
if ( $submenu_slug == $item[2] ) { |
9 | 1799 |
unset( $submenu[ $menu_slug ][ $i ] ); |
0 | 1800 |
return $item; |
1801 |
} |
|
1802 |
} |
|
1803 |
||
1804 |
return false; |
|
1805 |
} |
|
1806 |
||
1807 |
/** |
|
16 | 1808 |
* Get the URL to access a particular menu page based on the slug it was registered with. |
0 | 1809 |
* |
16 | 1810 |
* If the slug hasn't been registered properly, no URL will be returned. |
0 | 1811 |
* |
5 | 1812 |
* @since 3.0.0 |
0 | 1813 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1814 |
* @global array $_parent_pages |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1815 |
* |
16 | 1816 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
1817 |
* @param bool $echo Whether or not to echo the URL. Default true. |
|
1818 |
* @return string The menu page URL. |
|
0 | 1819 |
*/ |
9 | 1820 |
function menu_page_url( $menu_slug, $echo = true ) { |
0 | 1821 |
global $_parent_pages; |
1822 |
||
9 | 1823 |
if ( isset( $_parent_pages[ $menu_slug ] ) ) { |
1824 |
$parent_slug = $_parent_pages[ $menu_slug ]; |
|
1825 |
if ( $parent_slug && ! isset( $_parent_pages[ $parent_slug ] ) ) { |
|
0 | 1826 |
$url = admin_url( add_query_arg( 'page', $menu_slug, $parent_slug ) ); |
1827 |
} else { |
|
1828 |
$url = admin_url( 'admin.php?page=' . $menu_slug ); |
|
1829 |
} |
|
1830 |
} else { |
|
1831 |
$url = ''; |
|
1832 |
} |
|
1833 |
||
9 | 1834 |
$url = esc_url( $url ); |
0 | 1835 |
|
9 | 1836 |
if ( $echo ) { |
0 | 1837 |
echo $url; |
9 | 1838 |
} |
0 | 1839 |
|
1840 |
return $url; |
|
1841 |
} |
|
1842 |
||
1843 |
// |
|
16 | 1844 |
// Pluggable Menu Support -- Private. |
0 | 1845 |
// |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1846 |
/** |
16 | 1847 |
* Gets the parent file of the current admin page. |
1848 |
* |
|
1849 |
* @since 1.5.0 |
|
1850 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1851 |
* @global string $parent_file |
16 | 1852 |
* @global array $menu |
1853 |
* @global array $submenu |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1854 |
* @global string $pagenow |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1855 |
* @global string $typenow |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1856 |
* @global string $plugin_page |
16 | 1857 |
* @global array $_wp_real_parent_file |
1858 |
* @global array $_wp_menu_nopriv |
|
1859 |
* @global array $_wp_submenu_nopriv |
|
9 | 1860 |
* |
16 | 1861 |
* @param string $parent The slug name for the parent menu (or the file name of a standard |
1862 |
* WordPress admin page). Default empty string. |
|
1863 |
* @return string The parent file of the current admin page. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1864 |
*/ |
0 | 1865 |
function get_admin_page_parent( $parent = '' ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1866 |
global $parent_file, $menu, $submenu, $pagenow, $typenow, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1867 |
$plugin_page, $_wp_real_parent_file, $_wp_menu_nopriv, $_wp_submenu_nopriv; |
0 | 1868 |
|
16 | 1869 |
if ( ! empty( $parent ) && 'admin.php' !== $parent ) { |
9 | 1870 |
if ( isset( $_wp_real_parent_file[ $parent ] ) ) { |
1871 |
$parent = $_wp_real_parent_file[ $parent ]; |
|
1872 |
} |
|
0 | 1873 |
return $parent; |
1874 |
} |
|
1875 |
||
16 | 1876 |
if ( 'admin.php' === $pagenow && isset( $plugin_page ) ) { |
9 | 1877 |
foreach ( (array) $menu as $parent_menu ) { |
0 | 1878 |
if ( $parent_menu[2] == $plugin_page ) { |
1879 |
$parent_file = $plugin_page; |
|
9 | 1880 |
if ( isset( $_wp_real_parent_file[ $parent_file ] ) ) { |
1881 |
$parent_file = $_wp_real_parent_file[ $parent_file ]; |
|
1882 |
} |
|
0 | 1883 |
return $parent_file; |
1884 |
} |
|
1885 |
} |
|
9 | 1886 |
if ( isset( $_wp_menu_nopriv[ $plugin_page ] ) ) { |
0 | 1887 |
$parent_file = $plugin_page; |
9 | 1888 |
if ( isset( $_wp_real_parent_file[ $parent_file ] ) ) { |
1889 |
$parent_file = $_wp_real_parent_file[ $parent_file ]; |
|
1890 |
} |
|
0 | 1891 |
return $parent_file; |
1892 |
} |
|
1893 |
} |
|
1894 |
||
9 | 1895 |
if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $pagenow ][ $plugin_page ] ) ) { |
0 | 1896 |
$parent_file = $pagenow; |
9 | 1897 |
if ( isset( $_wp_real_parent_file[ $parent_file ] ) ) { |
1898 |
$parent_file = $_wp_real_parent_file[ $parent_file ]; |
|
1899 |
} |
|
0 | 1900 |
return $parent_file; |
1901 |
} |
|
1902 |
||
9 | 1903 |
foreach ( array_keys( (array) $submenu ) as $parent ) { |
1904 |
foreach ( $submenu[ $parent ] as $submenu_array ) { |
|
1905 |
if ( isset( $_wp_real_parent_file[ $parent ] ) ) { |
|
1906 |
$parent = $_wp_real_parent_file[ $parent ]; |
|
1907 |
} |
|
16 | 1908 |
if ( ! empty( $typenow ) && ( "$pagenow?post_type=$typenow" === $submenu_array[2] ) ) { |
0 | 1909 |
$parent_file = $parent; |
1910 |
return $parent; |
|
9 | 1911 |
} elseif ( $submenu_array[2] == $pagenow && empty( $typenow ) && ( empty( $parent_file ) || false === strpos( $parent_file, '?' ) ) ) { |
0 | 1912 |
$parent_file = $parent; |
1913 |
return $parent; |
|
9 | 1914 |
} elseif ( isset( $plugin_page ) && ( $plugin_page == $submenu_array[2] ) ) { |
5 | 1915 |
$parent_file = $parent; |
1916 |
return $parent; |
|
1917 |
} |
|
0 | 1918 |
} |
1919 |
} |
|
1920 |
||
9 | 1921 |
if ( empty( $parent_file ) ) { |
0 | 1922 |
$parent_file = ''; |
9 | 1923 |
} |
0 | 1924 |
return ''; |
1925 |
} |
|
1926 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1927 |
/** |
16 | 1928 |
* Gets the title of the current admin page. |
1929 |
* |
|
1930 |
* @since 1.5.0 |
|
1931 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1932 |
* @global string $title |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1933 |
* @global array $menu |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1934 |
* @global array $submenu |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1935 |
* @global string $pagenow |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1936 |
* @global string $plugin_page |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1937 |
* @global string $typenow |
9 | 1938 |
* |
16 | 1939 |
* @return string The title of the current admin page. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1940 |
*/ |
0 | 1941 |
function get_admin_page_title() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1942 |
global $title, $menu, $submenu, $pagenow, $plugin_page, $typenow; |
0 | 1943 |
|
9 | 1944 |
if ( ! empty( $title ) ) { |
0 | 1945 |
return $title; |
9 | 1946 |
} |
0 | 1947 |
|
1948 |
$hook = get_plugin_page_hook( $plugin_page, $pagenow ); |
|
1949 |
||
16 | 1950 |
$parent = get_admin_page_parent(); |
1951 |
$parent1 = $parent; |
|
0 | 1952 |
|
9 | 1953 |
if ( empty( $parent ) ) { |
1954 |
foreach ( (array) $menu as $menu_array ) { |
|
0 | 1955 |
if ( isset( $menu_array[3] ) ) { |
1956 |
if ( $menu_array[2] == $pagenow ) { |
|
1957 |
$title = $menu_array[3]; |
|
1958 |
return $menu_array[3]; |
|
16 | 1959 |
} elseif ( isset( $plugin_page ) && ( $plugin_page == $menu_array[2] ) && ( $hook == $menu_array[5] ) ) { |
5 | 1960 |
$title = $menu_array[3]; |
1961 |
return $menu_array[3]; |
|
1962 |
} |
|
0 | 1963 |
} else { |
1964 |
$title = $menu_array[0]; |
|
1965 |
return $title; |
|
1966 |
} |
|
1967 |
} |
|
1968 |
} else { |
|
1969 |
foreach ( array_keys( $submenu ) as $parent ) { |
|
9 | 1970 |
foreach ( $submenu[ $parent ] as $submenu_array ) { |
0 | 1971 |
if ( isset( $plugin_page ) && |
1972 |
( $plugin_page == $submenu_array[2] ) && |
|
1973 |
( |
|
1974 |
( $parent == $pagenow ) || |
|
1975 |
( $parent == $plugin_page ) || |
|
1976 |
( $plugin_page == $hook ) || |
|
16 | 1977 |
( 'admin.php' === $pagenow && $parent1 != $submenu_array[2] ) || |
9 | 1978 |
( ! empty( $typenow ) && $parent == $pagenow . '?post_type=' . $typenow ) |
0 | 1979 |
) |
1980 |
) { |
|
1981 |
$title = $submenu_array[3]; |
|
1982 |
return $submenu_array[3]; |
|
9 | 1983 |
} |
0 | 1984 |
|
16 | 1985 |
if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) { // Not the current page. |
0 | 1986 |
continue; |
9 | 1987 |
} |
0 | 1988 |
|
1989 |
if ( isset( $submenu_array[3] ) ) { |
|
1990 |
$title = $submenu_array[3]; |
|
1991 |
return $submenu_array[3]; |
|
1992 |
} else { |
|
1993 |
$title = $submenu_array[0]; |
|
1994 |
return $title; |
|
1995 |
} |
|
1996 |
} |
|
1997 |
} |
|
9 | 1998 |
if ( empty( $title ) ) { |
0 | 1999 |
foreach ( $menu as $menu_array ) { |
2000 |
if ( isset( $plugin_page ) && |
|
2001 |
( $plugin_page == $menu_array[2] ) && |
|
16 | 2002 |
( 'admin.php' === $pagenow ) && |
9 | 2003 |
( $parent1 == $menu_array[2] ) ) { |
0 | 2004 |
$title = $menu_array[3]; |
2005 |
return $menu_array[3]; |
|
9 | 2006 |
} |
0 | 2007 |
} |
2008 |
} |
|
2009 |
} |
|
2010 |
||
2011 |
return $title; |
|
2012 |
} |
|
2013 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2014 |
/** |
16 | 2015 |
* Gets the hook attached to the administrative page of a plugin. |
2016 |
* |
|
2017 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2018 |
* |
9 | 2019 |
* @param string $plugin_page The slug name of the plugin page. |
2020 |
* @param string $parent_page The slug name for the parent menu (or the file name of a standard |
|
2021 |
* WordPress admin page). |
|
2022 |
* @return string|null Hook attached to the plugin page, null otherwise. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2023 |
*/ |
0 | 2024 |
function get_plugin_page_hook( $plugin_page, $parent_page ) { |
2025 |
$hook = get_plugin_page_hookname( $plugin_page, $parent_page ); |
|
9 | 2026 |
if ( has_action( $hook ) ) { |
0 | 2027 |
return $hook; |
9 | 2028 |
} else { |
0 | 2029 |
return null; |
9 | 2030 |
} |
0 | 2031 |
} |
2032 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2033 |
/** |
16 | 2034 |
* Gets the hook name for the administrative page of a plugin. |
2035 |
* |
|
2036 |
* @since 1.5.0 |
|
2037 |
* |
|
9 | 2038 |
* @global array $admin_page_hooks |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2039 |
* |
9 | 2040 |
* @param string $plugin_page The slug name of the plugin page. |
2041 |
* @param string $parent_page The slug name for the parent menu (or the file name of a standard |
|
2042 |
* WordPress admin page). |
|
2043 |
* @return string Hook name for the plugin page. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2044 |
*/ |
0 | 2045 |
function get_plugin_page_hookname( $plugin_page, $parent_page ) { |
2046 |
global $admin_page_hooks; |
|
2047 |
||
2048 |
$parent = get_admin_page_parent( $parent_page ); |
|
2049 |
||
2050 |
$page_type = 'admin'; |
|
16 | 2051 |
if ( empty( $parent_page ) || 'admin.php' === $parent_page || isset( $admin_page_hooks[ $plugin_page ] ) ) { |
9 | 2052 |
if ( isset( $admin_page_hooks[ $plugin_page ] ) ) { |
0 | 2053 |
$page_type = 'toplevel'; |
9 | 2054 |
} elseif ( isset( $admin_page_hooks[ $parent ] ) ) { |
2055 |
$page_type = $admin_page_hooks[ $parent ]; |
|
5 | 2056 |
} |
9 | 2057 |
} elseif ( isset( $admin_page_hooks[ $parent ] ) ) { |
2058 |
$page_type = $admin_page_hooks[ $parent ]; |
|
0 | 2059 |
} |
2060 |
||
2061 |
$plugin_name = preg_replace( '!\.php!', '', $plugin_page ); |
|
2062 |
||
2063 |
return $page_type . '_page_' . $plugin_name; |
|
2064 |
} |
|
2065 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2066 |
/** |
16 | 2067 |
* Determines whether the current user can access the current admin page. |
2068 |
* |
|
2069 |
* @since 1.5.0 |
|
2070 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2071 |
* @global string $pagenow |
16 | 2072 |
* @global array $menu |
2073 |
* @global array $submenu |
|
2074 |
* @global array $_wp_menu_nopriv |
|
2075 |
* @global array $_wp_submenu_nopriv |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2076 |
* @global string $plugin_page |
16 | 2077 |
* @global array $_registered_pages |
9 | 2078 |
* |
16 | 2079 |
* @return bool True if the current user can access the admin page, false otherwise. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2080 |
*/ |
0 | 2081 |
function user_can_access_admin_page() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2082 |
global $pagenow, $menu, $submenu, $_wp_menu_nopriv, $_wp_submenu_nopriv, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2083 |
$plugin_page, $_registered_pages; |
0 | 2084 |
|
2085 |
$parent = get_admin_page_parent(); |
|
2086 |
||
9 | 2087 |
if ( ! isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $parent ][ $pagenow ] ) ) { |
0 | 2088 |
return false; |
9 | 2089 |
} |
0 | 2090 |
|
2091 |
if ( isset( $plugin_page ) ) { |
|
9 | 2092 |
if ( isset( $_wp_submenu_nopriv[ $parent ][ $plugin_page ] ) ) { |
0 | 2093 |
return false; |
9 | 2094 |
} |
0 | 2095 |
|
9 | 2096 |
$hookname = get_plugin_page_hookname( $plugin_page, $parent ); |
0 | 2097 |
|
9 | 2098 |
if ( ! isset( $_registered_pages[ $hookname ] ) ) { |
0 | 2099 |
return false; |
9 | 2100 |
} |
0 | 2101 |
} |
2102 |
||
9 | 2103 |
if ( empty( $parent ) ) { |
2104 |
if ( isset( $_wp_menu_nopriv[ $pagenow ] ) ) { |
|
0 | 2105 |
return false; |
9 | 2106 |
} |
2107 |
if ( isset( $_wp_submenu_nopriv[ $pagenow ][ $pagenow ] ) ) { |
|
0 | 2108 |
return false; |
9 | 2109 |
} |
2110 |
if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $pagenow ][ $plugin_page ] ) ) { |
|
0 | 2111 |
return false; |
9 | 2112 |
} |
2113 |
if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[ $plugin_page ] ) ) { |
|
0 | 2114 |
return false; |
9 | 2115 |
} |
2116 |
foreach ( array_keys( $_wp_submenu_nopriv ) as $key ) { |
|
2117 |
if ( isset( $_wp_submenu_nopriv[ $key ][ $pagenow ] ) ) { |
|
0 | 2118 |
return false; |
9 | 2119 |
} |
2120 |
if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $key ][ $plugin_page ] ) ) { |
|
2121 |
return false; |
|
2122 |
} |
|
0 | 2123 |
} |
2124 |
return true; |
|
2125 |
} |
|
2126 |
||
9 | 2127 |
if ( isset( $plugin_page ) && ( $plugin_page == $parent ) && isset( $_wp_menu_nopriv[ $plugin_page ] ) ) { |
0 | 2128 |
return false; |
9 | 2129 |
} |
0 | 2130 |
|
9 | 2131 |
if ( isset( $submenu[ $parent ] ) ) { |
2132 |
foreach ( $submenu[ $parent ] as $submenu_array ) { |
|
0 | 2133 |
if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) { |
9 | 2134 |
if ( current_user_can( $submenu_array[1] ) ) { |
0 | 2135 |
return true; |
9 | 2136 |
} else { |
0 | 2137 |
return false; |
9 | 2138 |
} |
5 | 2139 |
} elseif ( $submenu_array[2] == $pagenow ) { |
9 | 2140 |
if ( current_user_can( $submenu_array[1] ) ) { |
0 | 2141 |
return true; |
9 | 2142 |
} else { |
0 | 2143 |
return false; |
9 | 2144 |
} |
0 | 2145 |
} |
2146 |
} |
|
2147 |
} |
|
2148 |
||
2149 |
foreach ( $menu as $menu_array ) { |
|
9 | 2150 |
if ( $menu_array[2] == $parent ) { |
2151 |
if ( current_user_can( $menu_array[1] ) ) { |
|
0 | 2152 |
return true; |
9 | 2153 |
} else { |
0 | 2154 |
return false; |
9 | 2155 |
} |
0 | 2156 |
} |
2157 |
} |
|
2158 |
||
2159 |
return true; |
|
2160 |
} |
|
2161 |
||
16 | 2162 |
/* Allowed list functions */ |
0 | 2163 |
|
2164 |
/** |
|
16 | 2165 |
* Refreshes the value of the allowed options list available via the 'allowed_options' hook. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2166 |
* |
16 | 2167 |
* See the {@see 'allowed_options'} filter. |
0 | 2168 |
* |
2169 |
* @since 2.7.0 |
|
16 | 2170 |
* @since 5.5.0 `$new_whitelist_options` was renamed to `$new_allowed_options`. |
2171 |
* Please consider writing more inclusive code. |
|
0 | 2172 |
* |
16 | 2173 |
* @global array $new_allowed_options |
0 | 2174 |
* |
5 | 2175 |
* @param array $options |
2176 |
* @return array |
|
0 | 2177 |
*/ |
2178 |
function option_update_filter( $options ) { |
|
16 | 2179 |
global $new_allowed_options; |
0 | 2180 |
|
16 | 2181 |
if ( is_array( $new_allowed_options ) ) { |
2182 |
$options = add_allowed_options( $new_allowed_options, $options ); |
|
9 | 2183 |
} |
0 | 2184 |
|
2185 |
return $options; |
|
2186 |
} |
|
2187 |
||
2188 |
/** |
|
16 | 2189 |
* Adds an array of options to the list of allowed options. |
0 | 2190 |
* |
2191 |
* @since 2.7.0 |
|
2192 |
* |
|
16 | 2193 |
* @global array $allowed_options |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2194 |
* |
5 | 2195 |
* @param array $new_options |
2196 |
* @param string|array $options |
|
2197 |
* @return array |
|
0 | 2198 |
*/ |
16 | 2199 |
function add_allowed_options( $new_options, $options = '' ) { |
2200 |
if ( '' === $options ) { |
|
2201 |
global $allowed_options; |
|
9 | 2202 |
} else { |
16 | 2203 |
$allowed_options = $options; |
9 | 2204 |
} |
0 | 2205 |
|
2206 |
foreach ( $new_options as $page => $keys ) { |
|
2207 |
foreach ( $keys as $key ) { |
|
16 | 2208 |
if ( ! isset( $allowed_options[ $page ] ) || ! is_array( $allowed_options[ $page ] ) ) { |
2209 |
$allowed_options[ $page ] = array(); |
|
2210 |
$allowed_options[ $page ][] = $key; |
|
0 | 2211 |
} else { |
16 | 2212 |
$pos = array_search( $key, $allowed_options[ $page ], true ); |
2213 |
if ( false === $pos ) { |
|
2214 |
$allowed_options[ $page ][] = $key; |
|
9 | 2215 |
} |
0 | 2216 |
} |
2217 |
} |
|
2218 |
} |
|
2219 |
||
16 | 2220 |
return $allowed_options; |
0 | 2221 |
} |
2222 |
||
2223 |
/** |
|
16 | 2224 |
* Removes a list of options from the allowed options list. |
0 | 2225 |
* |
16 | 2226 |
* @since 5.5.0 |
0 | 2227 |
* |
16 | 2228 |
* @global array $allowed_options |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2229 |
* |
5 | 2230 |
* @param array $del_options |
2231 |
* @param string|array $options |
|
2232 |
* @return array |
|
0 | 2233 |
*/ |
16 | 2234 |
function remove_allowed_options( $del_options, $options = '' ) { |
2235 |
if ( '' === $options ) { |
|
2236 |
global $allowed_options; |
|
9 | 2237 |
} else { |
16 | 2238 |
$allowed_options = $options; |
9 | 2239 |
} |
0 | 2240 |
|
2241 |
foreach ( $del_options as $page => $keys ) { |
|
2242 |
foreach ( $keys as $key ) { |
|
16 | 2243 |
if ( isset( $allowed_options[ $page ] ) && is_array( $allowed_options[ $page ] ) ) { |
2244 |
$pos = array_search( $key, $allowed_options[ $page ], true ); |
|
2245 |
if ( false !== $pos ) { |
|
2246 |
unset( $allowed_options[ $page ][ $pos ] ); |
|
9 | 2247 |
} |
0 | 2248 |
} |
2249 |
} |
|
2250 |
} |
|
2251 |
||
16 | 2252 |
return $allowed_options; |
0 | 2253 |
} |
2254 |
||
2255 |
/** |
|
2256 |
* Output nonce, action, and option_page fields for a settings page. |
|
2257 |
* |
|
2258 |
* @since 2.7.0 |
|
2259 |
* |
|
16 | 2260 |
* @param string $option_group A settings group name. This should match the group name |
2261 |
* used in register_setting(). |
|
0 | 2262 |
*/ |
9 | 2263 |
function settings_fields( $option_group ) { |
2264 |
echo "<input type='hidden' name='option_page' value='" . esc_attr( $option_group ) . "' />"; |
|
0 | 2265 |
echo '<input type="hidden" name="action" value="update" />'; |
9 | 2266 |
wp_nonce_field( "$option_group-options" ); |
0 | 2267 |
} |
2268 |
||
2269 |
/** |
|
16 | 2270 |
* Clears the plugins cache used by get_plugins() and by default, the plugin updates cache. |
0 | 2271 |
* |
2272 |
* @since 3.7.0 |
|
2273 |
* |
|
16 | 2274 |
* @param bool $clear_update_cache Whether to clear the plugin updates cache. Default true. |
0 | 2275 |
*/ |
2276 |
function wp_clean_plugins_cache( $clear_update_cache = true ) { |
|
9 | 2277 |
if ( $clear_update_cache ) { |
0 | 2278 |
delete_site_transient( 'update_plugins' ); |
9 | 2279 |
} |
0 | 2280 |
wp_cache_delete( 'plugins', 'plugins' ); |
2281 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2282 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2283 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2284 |
* Load a given plugin attempt to generate errors. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2285 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2286 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2287 |
* @since 4.4.0 Function was moved into the `wp-admin/includes/plugin.php` file. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2288 |
* |
9 | 2289 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2290 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2291 |
function plugin_sandbox_scrape( $plugin ) { |
9 | 2292 |
if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) { |
2293 |
define( 'WP_SANDBOX_SCRAPING', true ); |
|
2294 |
} |
|
16 | 2295 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2296 |
wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin ); |
16 | 2297 |
include WP_PLUGIN_DIR . '/' . $plugin; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2298 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2299 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2300 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2301 |
* Helper function for adding content to the Privacy Policy Guide. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2302 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2303 |
* Plugins and themes should suggest text for inclusion in the site's privacy policy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2304 |
* The suggested text should contain information about any functionality that affects user privacy, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2305 |
* and will be shown on the Privacy Policy Guide screen. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2306 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2307 |
* A plugin or theme can use this function multiple times as long as it will help to better present |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2308 |
* the suggested policy content. For example modular plugins such as WooCommerse or Jetpack |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2309 |
* can add or remove suggested content depending on the modules/extensions that are enabled. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2310 |
* For more information see the Plugin Handbook: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2311 |
* https://developer.wordpress.org/plugins/privacy/suggesting-text-for-the-site-privacy-policy/. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2312 |
* |
16 | 2313 |
* The HTML contents of the `$policy_text` supports use of a specialized `.privacy-policy-tutorial` |
2314 |
* CSS class which can be used to provide supplemental information. Any content contained within |
|
2315 |
* HTML elements that have the `.privacy-policy-tutorial` CSS class applied will be omitted |
|
2316 |
* from the clipboard when the section content is copied. |
|
2317 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2318 |
* Intended for use with the `'admin_init'` action. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2319 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2320 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2321 |
* |
16 | 2322 |
* @param string $plugin_name The name of the plugin or theme that is suggesting content |
2323 |
* for the site's privacy policy. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2324 |
* @param string $policy_text The suggested content for inclusion in the policy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2325 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2326 |
function wp_add_privacy_policy_content( $plugin_name, $policy_text ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2327 |
if ( ! is_admin() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2328 |
_doing_it_wrong( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2329 |
__FUNCTION__, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2330 |
sprintf( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2331 |
/* translators: %s: admin_init */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2332 |
__( 'The suggested privacy policy content should be added only in wp-admin by using the %s (or later) action.' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2333 |
'<code>admin_init</code>' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2334 |
), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2335 |
'4.9.7' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2336 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2337 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2338 |
} elseif ( ! doing_action( 'admin_init' ) && ! did_action( 'admin_init' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2339 |
_doing_it_wrong( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2340 |
__FUNCTION__, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2341 |
sprintf( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2342 |
/* translators: %s: admin_init */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2343 |
__( 'The suggested privacy policy content should be added by using the %s (or later) action. Please see the inline documentation.' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2344 |
'<code>admin_init</code>' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2345 |
), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2346 |
'4.9.7' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2347 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2348 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2349 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2350 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2351 |
if ( ! class_exists( 'WP_Privacy_Policy_Content' ) ) { |
16 | 2352 |
require_once ABSPATH . 'wp-admin/includes/class-wp-privacy-policy-content.php'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2353 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2354 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2355 |
WP_Privacy_Policy_Content::add( $plugin_name, $policy_text ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2356 |
} |
9 | 2357 |
|
2358 |
/** |
|
2359 |
* Determines whether a plugin is technically active but was paused while |
|
2360 |
* loading. |
|
2361 |
* |
|
2362 |
* For more information on this and similar theme functions, check out |
|
2363 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
2364 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
2365 |
* |
|
2366 |
* @since 5.2.0 |
|
2367 |
* |
|
2368 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
|
16 | 2369 |
* @return bool True, if in the list of paused plugins. False, if not in the list. |
9 | 2370 |
*/ |
2371 |
function is_plugin_paused( $plugin ) { |
|
2372 |
if ( ! isset( $GLOBALS['_paused_plugins'] ) ) { |
|
2373 |
return false; |
|
2374 |
} |
|
2375 |
||
2376 |
if ( ! is_plugin_active( $plugin ) ) { |
|
2377 |
return false; |
|
2378 |
} |
|
2379 |
||
2380 |
list( $plugin ) = explode( '/', $plugin ); |
|
2381 |
||
2382 |
return array_key_exists( $plugin, $GLOBALS['_paused_plugins'] ); |
|
2383 |
} |
|
2384 |
||
2385 |
/** |
|
2386 |
* Gets the error that was recorded for a paused plugin. |
|
2387 |
* |
|
2388 |
* @since 5.2.0 |
|
2389 |
* |
|
16 | 2390 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
2391 |
* @return array|false Array of error information as returned by `error_get_last()`, |
|
2392 |
* or false if none was recorded. |
|
9 | 2393 |
*/ |
2394 |
function wp_get_plugin_error( $plugin ) { |
|
2395 |
if ( ! isset( $GLOBALS['_paused_plugins'] ) ) { |
|
2396 |
return false; |
|
2397 |
} |
|
2398 |
||
2399 |
list( $plugin ) = explode( '/', $plugin ); |
|
2400 |
||
2401 |
if ( ! array_key_exists( $plugin, $GLOBALS['_paused_plugins'] ) ) { |
|
2402 |
return false; |
|
2403 |
} |
|
2404 |
||
2405 |
return $GLOBALS['_paused_plugins'][ $plugin ]; |
|
2406 |
} |
|
2407 |
||
2408 |
/** |
|
2409 |
* Tries to resume a single plugin. |
|
2410 |
* |
|
2411 |
* If a redirect was provided, we first ensure the plugin does not throw fatal |
|
2412 |
* errors anymore. |
|
2413 |
* |
|
2414 |
* The way it works is by setting the redirection to the error before trying to |
|
2415 |
* include the plugin file. If the plugin fails, then the redirection will not |
|
2416 |
* be overwritten with the success message and the plugin will not be resumed. |
|
2417 |
* |
|
2418 |
* @since 5.2.0 |
|
2419 |
* |
|
16 | 2420 |
* @param string $plugin Single plugin to resume. |
2421 |
* @param string $redirect Optional. URL to redirect to. Default empty string. |
|
9 | 2422 |
* @return bool|WP_Error True on success, false if `$plugin` was not paused, |
2423 |
* `WP_Error` on failure. |
|
2424 |
*/ |
|
2425 |
function resume_plugin( $plugin, $redirect = '' ) { |
|
2426 |
/* |
|
2427 |
* We'll override this later if the plugin could be resumed without |
|
2428 |
* creating a fatal error. |
|
2429 |
*/ |
|
2430 |
if ( ! empty( $redirect ) ) { |
|
2431 |
wp_redirect( |
|
2432 |
add_query_arg( |
|
2433 |
'_error_nonce', |
|
2434 |
wp_create_nonce( 'plugin-resume-error_' . $plugin ), |
|
2435 |
$redirect |
|
2436 |
) |
|
2437 |
); |
|
2438 |
||
2439 |
// Load the plugin to test whether it throws a fatal error. |
|
2440 |
ob_start(); |
|
2441 |
plugin_sandbox_scrape( $plugin ); |
|
2442 |
ob_clean(); |
|
2443 |
} |
|
2444 |
||
2445 |
list( $extension ) = explode( '/', $plugin ); |
|
2446 |
||
2447 |
$result = wp_paused_plugins()->delete( $extension ); |
|
2448 |
||
2449 |
if ( ! $result ) { |
|
2450 |
return new WP_Error( |
|
2451 |
'could_not_resume_plugin', |
|
2452 |
__( 'Could not resume the plugin.' ) |
|
2453 |
); |
|
2454 |
} |
|
2455 |
||
2456 |
return true; |
|
2457 |
} |
|
2458 |
||
2459 |
/** |
|
2460 |
* Renders an admin notice in case some plugins have been paused due to errors. |
|
2461 |
* |
|
2462 |
* @since 5.2.0 |
|
2463 |
*/ |
|
2464 |
function paused_plugins_notice() { |
|
2465 |
if ( 'plugins.php' === $GLOBALS['pagenow'] ) { |
|
2466 |
return; |
|
2467 |
} |
|
2468 |
||
2469 |
if ( ! current_user_can( 'resume_plugins' ) ) { |
|
2470 |
return; |
|
2471 |
} |
|
2472 |
||
2473 |
if ( ! isset( $GLOBALS['_paused_plugins'] ) || empty( $GLOBALS['_paused_plugins'] ) ) { |
|
2474 |
return; |
|
2475 |
} |
|
2476 |
||
2477 |
printf( |
|
2478 |
'<div class="notice notice-error"><p><strong>%s</strong><br>%s</p><p><a href="%s">%s</a></p></div>', |
|
2479 |
__( 'One or more plugins failed to load properly.' ), |
|
2480 |
__( 'You can find more details and make changes on the Plugins screen.' ), |
|
2481 |
esc_url( admin_url( 'plugins.php?plugin_status=paused' ) ), |
|
2482 |
__( 'Go to the Plugins screen' ) |
|
2483 |
); |
|
2484 |
} |