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