WordPressを読む 93-1 /blog/wp-admin/includes/plugin.php 1
/blog/wp-admin/includes/plugin.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | <?php /** * WordPress Plugin Administration API * * @package WordPress * @subpackage Administration */ /** * Parse the plugin contents to retrieve plugin's metadata. * * The metadata of the plugin's data searches for the following in the plugin's * header. All plugin data must be on its own line. For plugin description, it * must not have any newlines or only parts of the description will be displayed * and the same goes for the plugin data. The below is formatted for printing. * * <code> * /* * Plugin Name: Name of Plugin * Plugin URI: Link to plugin information * Description: Plugin Description * Author: Plugin author's name * Author URI: Link to the author's web site * Version: Must be set in the plugin for WordPress 2.3+ * Text Domain: Optional. Unique identifier, should be same as the one used in * plugin_text_domain() * Domain Path: Optional. Only useful if the translations are located in a * folder above the plugin's base path. For example, if .mo files are * located in the locale folder then Domain Path will be "/locale/" and * must have the first slash. Defaults to the base folder the plugin is * located in. * Network: Optional. Specify "Network: true" to require that a plugin is activated * across all sites in an installation. This will prevent a plugin from being * activated on a single site when Multisite is enabled. * * / # Remove the space to close comment * * * Plugin data returned array contains the following: * 'Name' - Name of the plugin, must be unique. * 'Title' - Title of the plugin and the link to the plugin's web site. * 'Description' - Description of what the plugin does and/or notes * from the author. * 'Author' - The author's name * 'AuthorURI' - The authors web site address. * 'Version' - The plugin version number. * 'PluginURI' - Plugin web site address. * 'TextDomain' - Plugin's text domain for localization. * 'DomainPath' - Plugin's relative directory path to .mo files. * 'Network' - Boolean. Whether the plugin can only be activated network wide. * * Some users have issues with opening large files and manipulating the contents * for want is usually the first 1kiB or 2kiB. This function stops pulling in * the plugin contents when it has all of the required plugin data. * * The first 8kiB of the file will be pulled in and if the plugin data is not * within that first 8kiB, then the plugin author should correct their plugin * and move the plugin data headers to the top. * * The plugin file is assumed to have permissions to allow for scripts to read * the file. This is not checked however and the file is only opened for * reading. * * @link http://trac.wordpress.org/ticket/5651 Previous Optimizations. * @link http://trac.wordpress.org/ticket/7372 Further and better Optimizations. * @since 1.5.0 * * @param string $plugin_file Path to the plugin file * @param bool $markup Optional. If the returned data should have HTML markup applied. Defaults to true. * @param bool $translate Optional. If the returned data should be translated. Defaults to true. * @return array See above for description. */ function get_plugin_data( $plugin_file, $markup = true, $translate = true ) { $default_headers = array( 'Name' => 'Plugin Name', 'PluginURI' => 'Plugin URI', 'Version' => 'Version', 'Description' => 'Description', 'Author' => 'Author', 'AuthorURI' => 'Author URI', 'TextDomain' => 'Text Domain', 'DomainPath' => 'Domain Path', 'Network' => 'Network', // Site Wide Only is deprecated in favor of Network. '_sitewide' => 'Site Wide Only', ); $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' ); // Site Wide Only is the old header for Network if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) { _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The <code>%1$s</code> plugin header is deprecated. Use <code>%2$s</code> instead.' ), 'Site Wide Only: true', 'Network: true' ) ); $plugin_data['Network'] = $plugin_data['_sitewide']; } $plugin_data['Network'] = ( 'true' == strtolower( $plugin_data['Network'] ) ); unset( $plugin_data['_sitewide'] ); if ( $markup || $translate ) { $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate ); } else { $plugin_data['Title'] = $plugin_data['Name']; $plugin_data['AuthorName'] = $plugin_data['Author']; } return $plugin_data; } /** * Sanitizes plugin data, optionally adds markup, optionally translates. * * @since 2.7.0 * @access private * @see get_plugin_data() */ function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) { // Sanitize the plugin filename to a WP_PLUGIN_DIR relative path $plugin_file = plugin_basename( $plugin_file ); // Translate fields if ( $translate ) { if ( $textdomain = $plugin_data['TextDomain'] ) { if ( $plugin_data['DomainPath'] ) load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] ); else load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) ); } elseif ( in_array( basename( $plugin_file ), array( 'hello.php', 'akismet.php' ) ) ) { $textdomain = 'default'; } if ( $textdomain ) { foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field ) $plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain ); } } // Sanitize fields $allowed_tags = $allowed_tags_in_links = array( 'abbr' => array( 'title' => true ), 'acronym' => array( 'title' => true ), 'code' => true, 'em' => true, 'strong' => true, ); $allowed_tags['a'] = array( 'href' => true, 'title' => true ); // Name is marked up inside <a> tags. Don't allow these. // Author is too, but some plugins have used <a> here (omitting Author URI). $plugin_data['Name'] = wp_kses( $plugin_data['Name'], $allowed_tags_in_links ); $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $allowed_tags ); $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags ); $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $allowed_tags ); $plugin_data['PluginURI'] = esc_url( $plugin_data['PluginURI'] ); $plugin_data['AuthorURI'] = esc_url( $plugin_data['AuthorURI'] ); $plugin_data['Title'] = $plugin_data['Name']; $plugin_data['AuthorName'] = $plugin_data['Author']; // Apply markup if ( $markup ) { if ( $plugin_data['PluginURI'] && $plugin_data['Name'] ) $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '">' . $plugin_data['Name'] . '</a>'; if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] ) $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '">' . $plugin_data['Author'] . '</a>'; $plugin_data['Description'] = wptexturize( $plugin_data['Description'] ); if ( $plugin_data['Author'] ) $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s.'), $plugin_data['Author'] ) . '</cite>'; } return $plugin_data; } /** * Get a list of a plugin's files. * * @since 2.8.0 * * @param string $plugin Plugin ID * @return array List of files relative to the plugin root. */ function get_plugin_files($plugin) { $plugin_file = WP_PLUGIN_DIR . '/' . $plugin; $dir = dirname($plugin_file); $plugin_files = array($plugin); if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) { $plugins_dir = @ opendir( $dir ); if ( $plugins_dir ) { while (($file = readdir( $plugins_dir ) ) !== false ) { if ( substr($file, 0, 1) == '.' ) continue; if ( is_dir( $dir . '/' . $file ) ) { $plugins_subdir = @ opendir( $dir . '/' . $file ); if ( $plugins_subdir ) { while (($subfile = readdir( $plugins_subdir ) ) !== false ) { if ( substr($subfile, 0, 1) == '.' ) continue; $plugin_files[] = plugin_basename("$dir/$file/$subfile"); } @closedir( $plugins_subdir ); } } else { if ( plugin_basename("$dir/$file") != $plugin ) $plugin_files[] = plugin_basename("$dir/$file"); } } @closedir( $plugins_dir ); } } return $plugin_files; } /** * Check the plugins directory and retrieve all plugin files with plugin data. * * WordPress only supports plugin files in the base plugins directory * (wp-content/plugins) and in one directory above the plugins directory * (wp-content/plugins/my-plugin). The file it looks for has the plugin data and * must be found in those two locations. It is recommended that do keep your * plugin files in directories. * * The file with the plugin data is the file that will be included and therefore * needs to have the main execution for the plugin. This does not mean * everything must be contained in the file and it is recommended that the file * be split for maintainability. Keep everything in one file for extreme * optimization purposes. * * @since 1.5.0 * * @param string $plugin_folder Optional. Relative path to single plugin folder. * @return array Key is the plugin file path and the value is an array of the plugin data. */ function get_plugins($plugin_folder = '') { if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') ) $cache_plugins = array(); if ( isset($cache_plugins[ $plugin_folder ]) ) return $cache_plugins[ $plugin_folder ]; $wp_plugins = array (); $plugin_root = WP_PLUGIN_DIR; if ( !empty($plugin_folder) ) $plugin_root .= $plugin_folder; // Files in wp-content/plugins directory $plugins_dir = @ opendir( $plugin_root); $plugin_files = array(); if ( $plugins_dir ) { while (($file = readdir( $plugins_dir ) ) !== false ) { if ( substr($file, 0, 1) == '.' ) continue; if ( is_dir( $plugin_root.'/'.$file ) ) { $plugins_subdir = @ opendir( $plugin_root.'/'.$file ); if ( $plugins_subdir ) { while (($subfile = readdir( $plugins_subdir ) ) !== false ) { if ( substr($subfile, 0, 1) == '.' ) continue; if ( substr($subfile, -4) == '.php' ) $plugin_files[] = "$file/$subfile"; } closedir( $plugins_subdir ); } } else { if ( substr($file, -4) == '.php' ) $plugin_files[] = $file; } } closedir( $plugins_dir ); } if ( empty($plugin_files) ) return $wp_plugins; foreach ( $plugin_files as $plugin_file ) { if ( !is_readable( "$plugin_root/$plugin_file" ) ) continue; $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached. if ( empty ( $plugin_data['Name'] ) ) continue; $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data; } uasort( $wp_plugins, '_sort_uname_callback' ); $cache_plugins[ $plugin_folder ] = $wp_plugins; wp_cache_set('plugins', $cache_plugins, 'plugins'); return $wp_plugins; } /** * Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data. * * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins). * * @since 3.0.0 * @return array Key is the mu-plugin file path and the value is an array of the mu-plugin data. */ function get_mu_plugins() { $wp_plugins = array(); // Files in wp-content/mu-plugins directory $plugin_files = array(); if ( ! is_dir( WPMU_PLUGIN_DIR ) ) return $wp_plugins; if ( $plugins_dir = @ opendir( WPMU_PLUGIN_DIR ) ) { while ( ( $file = readdir( $plugins_dir ) ) !== false ) { if ( substr( $file, -4 ) == '.php' ) $plugin_files[] = $file; } } else { return $wp_plugins; } @closedir( $plugins_dir ); if ( empty($plugin_files) ) return $wp_plugins; foreach ( $plugin_files as $plugin_file ) { if ( !is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) ) continue; $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached. if ( empty ( $plugin_data['Name'] ) ) $plugin_data['Name'] = $plugin_file; $wp_plugins[ $plugin_file ] = $plugin_data; } if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php') <= 30 ) // silence is golden unset( $wp_plugins['index.php'] ); uasort( $wp_plugins, '_sort_uname_callback' ); return $wp_plugins; } /** * Callback to sort array by a 'Name' key. * * @since 3.1.0 * @access private */ function _sort_uname_callback( $a, $b ) { return strnatcasecmp( $a['Name'], $b['Name'] ); } /** * Check the wp-content directory and retrieve all drop-ins with any plugin data. * * @since 3.0.0 * @return array Key is the file path and the value is an array of the plugin data. */ function get_dropins() { $dropins = array(); $plugin_files = array(); $_dropins = _get_dropins(); // These exist in the wp-content directory if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) { while ( ( $file = readdir( $plugins_dir ) ) !== false ) { if ( isset( $_dropins[ $file ] ) ) $plugin_files[] = $file; } } else { return $dropins; } @closedir( $plugins_dir ); if ( empty($plugin_files) ) return $dropins; foreach ( $plugin_files as $plugin_file ) { if ( !is_readable( WP_CONTENT_DIR . "/$plugin_file" ) ) continue; $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached. if ( empty( $plugin_data['Name'] ) ) $plugin_data['Name'] = $plugin_file; $dropins[ $plugin_file ] = $plugin_data; } uksort( $dropins, 'strnatcasecmp' ); return $dropins; } /** * Returns drop-ins that WordPress uses. * * Includes Multisite drop-ins only when is_multisite() * * @since 3.0.0 * @return array Key is file name. The value is an array, with the first value the * purpose of the drop-in and the second value the name of the constant that must be * true for the drop-in to be used, or true if no constant is required. */ function _get_dropins() { $dropins = array( 'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE 'db.php' => array( __( 'Custom database class.' ), true ), // auto on load 'db-error.php' => array( __( 'Custom database error message.' ), true ), // auto on error 'install.php' => array( __( 'Custom install script.' ), true ), // auto on install 'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // auto on maintenance 'object-cache.php' => array( __( 'External object cache.' ), true ), // auto on load ); if ( is_multisite() ) { $dropins['sunrise.php' ] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE $dropins['blog-deleted.php' ] = array( __( 'Custom site deleted message.' ), true ); // auto on deleted blog $dropins['blog-inactive.php' ] = array( __( 'Custom site inactive message.' ), true ); // auto on inactive blog $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // auto on archived or spammed blog } return $dropins; } /** * Check whether the plugin is active by checking the active_plugins list. * * @since 2.5.0 * * @param string $plugin Base plugin path from plugins directory. * @return bool True, if in the active plugins list. False, not in the list. */ function is_plugin_active( $plugin ) { return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin ); } /** * Check whether the plugin is inactive. * * Reverse of is_plugin_active(). Used as a callback. * * @since 3.1.0 * @see is_plugin_active() * * @param string $plugin Base plugin path from plugins directory. * @return bool True if inactive. False if active. */ function is_plugin_inactive( $plugin ) { return ! is_plugin_active( $plugin ); } /** * Check whether the plugin is active for the entire network. * * @since 3.0.0 * * @param string $plugin Base plugin path from plugins directory. * @return bool True, if active for the network, otherwise false. */ function is_plugin_active_for_network( $plugin ) { if ( !is_multisite() ) return false; $plugins = get_site_option( 'active_sitewide_plugins'); if ( isset($plugins[$plugin]) ) return true; return false; } /** * Checks for "Network: true" in the plugin header to see if this should * be activated only as a network wide plugin. The plugin would also work * when Multisite is not enabled. * * Checks for "Site Wide Only: true" for backwards compatibility. * * @since 3.0.0 * * @param string $plugin Plugin to check * @return bool True if plugin is network only, false otherwise. */ function is_network_only_plugin( $plugin ) { $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); if ( $plugin_data ) return $plugin_data['Network']; return false; } |