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