PageRenderTime 68ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/includes/plugin.php

http://github.com/markjaquith/WordPress
PHP | 2451 lines | 1143 code | 269 blank | 1039 comment | 300 complexity | c13b0168185f621cea7f42b703ed5837 MD5 | raw file
Possible License(s): 0BSD

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * WordPress Plugin Administration API
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Parses the plugin contents to retrieve plugin's metadata.
  10. *
  11. * All plugin headers must be on their own line. Plugin description must not have
  12. * any newlines, otherwise only parts of the description will be displayed.
  13. * The below is formatted for printing.
  14. *
  15. * /*
  16. * Plugin Name: Name of the plugin.
  17. * Plugin URI: The home page of the plugin.
  18. * Description: Plugin description.
  19. * Author: Plugin author's name.
  20. * Author URI: Link to the author's website.
  21. * Version: Plugin version.
  22. * Text Domain: Optional. Unique identifier, should be same as the one used in
  23. * load_plugin_textdomain().
  24. * Domain Path: Optional. Only useful if the translations are located in a
  25. * folder above the plugin's base path. For example, if .mo files are
  26. * located in the locale folder then Domain Path will be "/locale/" and
  27. * must have the first slash. Defaults to the base folder the plugin is
  28. * located in.
  29. * Network: Optional. Specify "Network: true" to require that a plugin is activated
  30. * across all sites in an installation. This will prevent a plugin from being
  31. * activated on a single site when Multisite is enabled.
  32. * Requires at least: Optional. Specify the minimum required WordPress version.
  33. * Requires PHP: Optional. Specify the minimum required PHP version.
  34. * * / # Remove the space to close comment.
  35. *
  36. * The first 8 KB of the file will be pulled in and if the plugin data is not
  37. * within that first 8 KB, then the plugin author should correct their plugin
  38. * and move the plugin data headers to the top.
  39. *
  40. * The plugin file is assumed to have permissions to allow for scripts to read
  41. * the file. This is not checked however and the file is only opened for
  42. * reading.
  43. *
  44. * @since 1.5.0
  45. * @since 5.3.0 Added support for `Requires at least` and `Requires PHP` headers.
  46. *
  47. * @param string $plugin_file Absolute path to the main plugin file.
  48. * @param bool $markup Optional. If the returned data should have HTML markup applied.
  49. * Default true.
  50. * @param bool $translate Optional. If the returned data should be translated. Default true.
  51. * @return array {
  52. * Plugin data. Values will be empty if not supplied by the plugin.
  53. *
  54. * @type string $Name Name of the plugin. Should be unique.
  55. * @type string $Title Title of the plugin and link to the plugin's site (if set).
  56. * @type string $Description Plugin description.
  57. * @type string $Author Author's name.
  58. * @type string $AuthorURI Author's website address (if set).
  59. * @type string $Version Plugin version.
  60. * @type string $TextDomain Plugin textdomain.
  61. * @type string $DomainPath Plugins relative directory path to .mo files.
  62. * @type bool $Network Whether the plugin can only be activated network-wide.
  63. * @type string $RequiresWP Minimum required version of WordPress.
  64. * @type string $RequiresPHP Minimum required version of PHP.
  65. * }
  66. */
  67. function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
  68. $default_headers = array(
  69. 'Name' => 'Plugin Name',
  70. 'PluginURI' => 'Plugin URI',
  71. 'Version' => 'Version',
  72. 'Description' => 'Description',
  73. 'Author' => 'Author',
  74. 'AuthorURI' => 'Author URI',
  75. 'TextDomain' => 'Text Domain',
  76. 'DomainPath' => 'Domain Path',
  77. 'Network' => 'Network',
  78. 'RequiresWP' => 'Requires at least',
  79. 'RequiresPHP' => 'Requires PHP',
  80. // Site Wide Only is deprecated in favor of Network.
  81. '_sitewide' => 'Site Wide Only',
  82. );
  83. $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
  84. // Site Wide Only is the old header for Network.
  85. if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) {
  86. /* translators: 1: Site Wide Only: true, 2: Network: true */
  87. _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>' ) );
  88. $plugin_data['Network'] = $plugin_data['_sitewide'];
  89. }
  90. $plugin_data['Network'] = ( 'true' == strtolower( $plugin_data['Network'] ) );
  91. unset( $plugin_data['_sitewide'] );
  92. // If no text domain is defined fall back to the plugin slug.
  93. if ( ! $plugin_data['TextDomain'] ) {
  94. $plugin_slug = dirname( plugin_basename( $plugin_file ) );
  95. if ( '.' !== $plugin_slug && false === strpos( $plugin_slug, '/' ) ) {
  96. $plugin_data['TextDomain'] = $plugin_slug;
  97. }
  98. }
  99. if ( $markup || $translate ) {
  100. $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
  101. } else {
  102. $plugin_data['Title'] = $plugin_data['Name'];
  103. $plugin_data['AuthorName'] = $plugin_data['Author'];
  104. }
  105. return $plugin_data;
  106. }
  107. /**
  108. * Sanitizes plugin data, optionally adds markup, optionally translates.
  109. *
  110. * @since 2.7.0
  111. *
  112. * @see get_plugin_data()
  113. *
  114. * @access private
  115. *
  116. * @param string $plugin_file Path to the main plugin file.
  117. * @param array $plugin_data An array of plugin data. See `get_plugin_data()`.
  118. * @param bool $markup Optional. If the returned data should have HTML markup applied.
  119. * Default true.
  120. * @param bool $translate Optional. If the returned data should be translated. Default true.
  121. * @return array {
  122. * Plugin data. Values will be empty if not supplied by the plugin.
  123. *
  124. * @type string $Name Name of the plugin. Should be unique.
  125. * @type string $Title Title of the plugin and link to the plugin's site (if set).
  126. * @type string $Description Plugin description.
  127. * @type string $Author Author's name.
  128. * @type string $AuthorURI Author's website address (if set).
  129. * @type string $Version Plugin version.
  130. * @type string $TextDomain Plugin textdomain.
  131. * @type string $DomainPath Plugins relative directory path to .mo files.
  132. * @type bool $Network Whether the plugin can only be activated network-wide.
  133. * }
  134. */
  135. function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) {
  136. // Sanitize the plugin filename to a WP_PLUGIN_DIR relative path.
  137. $plugin_file = plugin_basename( $plugin_file );
  138. // Translate fields.
  139. if ( $translate ) {
  140. $textdomain = $plugin_data['TextDomain'];
  141. if ( $textdomain ) {
  142. if ( ! is_textdomain_loaded( $textdomain ) ) {
  143. if ( $plugin_data['DomainPath'] ) {
  144. load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] );
  145. } else {
  146. load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) );
  147. }
  148. }
  149. } elseif ( 'hello.php' == basename( $plugin_file ) ) {
  150. $textdomain = 'default';
  151. }
  152. if ( $textdomain ) {
  153. foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field ) {
  154. // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
  155. $plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain );
  156. }
  157. }
  158. }
  159. // Sanitize fields.
  160. $allowed_tags_in_links = array(
  161. 'abbr' => array( 'title' => true ),
  162. 'acronym' => array( 'title' => true ),
  163. 'code' => true,
  164. 'em' => true,
  165. 'strong' => true,
  166. );
  167. $allowed_tags = $allowed_tags_in_links;
  168. $allowed_tags['a'] = array(
  169. 'href' => true,
  170. 'title' => true,
  171. );
  172. // Name is marked up inside <a> tags. Don't allow these.
  173. // Author is too, but some plugins have used <a> here (omitting Author URI).
  174. $plugin_data['Name'] = wp_kses( $plugin_data['Name'], $allowed_tags_in_links );
  175. $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $allowed_tags );
  176. $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags );
  177. $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $allowed_tags );
  178. $plugin_data['PluginURI'] = esc_url( $plugin_data['PluginURI'] );
  179. $plugin_data['AuthorURI'] = esc_url( $plugin_data['AuthorURI'] );
  180. $plugin_data['Title'] = $plugin_data['Name'];
  181. $plugin_data['AuthorName'] = $plugin_data['Author'];
  182. // Apply markup.
  183. if ( $markup ) {
  184. if ( $plugin_data['PluginURI'] && $plugin_data['Name'] ) {
  185. $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '">' . $plugin_data['Name'] . '</a>';
  186. }
  187. if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] ) {
  188. $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '">' . $plugin_data['Author'] . '</a>';
  189. }
  190. $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
  191. if ( $plugin_data['Author'] ) {
  192. $plugin_data['Description'] .= sprintf(
  193. /* translators: %s: Plugin author. */
  194. ' <cite>' . __( 'By %s.' ) . '</cite>',
  195. $plugin_data['Author']
  196. );
  197. }
  198. }
  199. return $plugin_data;
  200. }
  201. /**
  202. * Get a list of a plugin's files.
  203. *
  204. * @since 2.8.0
  205. *
  206. * @param string $plugin Path to the plugin file relative to the plugins directory.
  207. * @return string[] Array of file names relative to the plugin root.
  208. */
  209. function get_plugin_files( $plugin ) {
  210. $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
  211. $dir = dirname( $plugin_file );
  212. $plugin_files = array( plugin_basename( $plugin_file ) );
  213. if ( is_dir( $dir ) && WP_PLUGIN_DIR !== $dir ) {
  214. /**
  215. * Filters the array of excluded directories and files while scanning the folder.
  216. *
  217. * @since 4.9.0
  218. *
  219. * @param string[] $exclusions Array of excluded directories and files.
  220. */
  221. $exclusions = (array) apply_filters( 'plugin_files_exclusions', array( 'CVS', 'node_modules', 'vendor', 'bower_components' ) );
  222. $list_files = list_files( $dir, 100, $exclusions );
  223. $list_files = array_map( 'plugin_basename', $list_files );
  224. $plugin_files = array_merge( $plugin_files, $list_files );
  225. $plugin_files = array_values( array_unique( $plugin_files ) );
  226. }
  227. return $plugin_files;
  228. }
  229. /**
  230. * Check the plugins directory and retrieve all plugin files with plugin data.
  231. *
  232. * WordPress only supports plugin files in the base plugins directory
  233. * (wp-content/plugins) and in one directory above the plugins directory
  234. * (wp-content/plugins/my-plugin). The file it looks for has the plugin data
  235. * and must be found in those two locations. It is recommended to keep your
  236. * plugin files in their own directories.
  237. *
  238. * The file with the plugin data is the file that will be included and therefore
  239. * needs to have the main execution for the plugin. This does not mean
  240. * everything must be contained in the file and it is recommended that the file
  241. * be split for maintainability. Keep everything in one file for extreme
  242. * optimization purposes.
  243. *
  244. * @since 1.5.0
  245. *
  246. * @param string $plugin_folder Optional. Relative path to single plugin folder.
  247. * @return array[] Array of arrays of plugin data, keyed by plugin file name. See `get_plugin_data()`.
  248. */
  249. function get_plugins( $plugin_folder = '' ) {
  250. $cache_plugins = wp_cache_get( 'plugins', 'plugins' );
  251. if ( ! $cache_plugins ) {
  252. $cache_plugins = array();
  253. }
  254. if ( isset( $cache_plugins[ $plugin_folder ] ) ) {
  255. return $cache_plugins[ $plugin_folder ];
  256. }
  257. $wp_plugins = array();
  258. $plugin_root = WP_PLUGIN_DIR;
  259. if ( ! empty( $plugin_folder ) ) {
  260. $plugin_root .= $plugin_folder;
  261. }
  262. // Files in wp-content/plugins directory.
  263. $plugins_dir = @ opendir( $plugin_root );
  264. $plugin_files = array();
  265. if ( $plugins_dir ) {
  266. while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
  267. if ( substr( $file, 0, 1 ) == '.' ) {
  268. continue;
  269. }
  270. if ( is_dir( $plugin_root . '/' . $file ) ) {
  271. $plugins_subdir = @ opendir( $plugin_root . '/' . $file );
  272. if ( $plugins_subdir ) {
  273. while ( ( $subfile = readdir( $plugins_subdir ) ) !== false ) {
  274. if ( substr( $subfile, 0, 1 ) == '.' ) {
  275. continue;
  276. }
  277. if ( substr( $subfile, -4 ) == '.php' ) {
  278. $plugin_files[] = "$file/$subfile";
  279. }
  280. }
  281. closedir( $plugins_subdir );
  282. }
  283. } else {
  284. if ( substr( $file, -4 ) == '.php' ) {
  285. $plugin_files[] = $file;
  286. }
  287. }
  288. }
  289. closedir( $plugins_dir );
  290. }
  291. if ( empty( $plugin_files ) ) {
  292. return $wp_plugins;
  293. }
  294. foreach ( $plugin_files as $plugin_file ) {
  295. if ( ! is_readable( "$plugin_root/$plugin_file" ) ) {
  296. continue;
  297. }
  298. // Do not apply markup/translate as it will be cached.
  299. $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false );
  300. if ( empty( $plugin_data['Name'] ) ) {
  301. continue;
  302. }
  303. $wp_plugins[ plugin_basename( $plugin_file ) ] = $plugin_data;
  304. }
  305. uasort( $wp_plugins, '_sort_uname_callback' );
  306. $cache_plugins[ $plugin_folder ] = $wp_plugins;
  307. wp_cache_set( 'plugins', $cache_plugins, 'plugins' );
  308. return $wp_plugins;
  309. }
  310. /**
  311. * Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data.
  312. *
  313. * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins).
  314. *
  315. * @since 3.0.0
  316. * @return array[] Array of arrays of mu-plugin data, keyed by plugin file name. See `get_plugin_data()`.
  317. */
  318. function get_mu_plugins() {
  319. $wp_plugins = array();
  320. $plugin_files = array();
  321. if ( ! is_dir( WPMU_PLUGIN_DIR ) ) {
  322. return $wp_plugins;
  323. }
  324. // Files in wp-content/mu-plugins directory.
  325. $plugins_dir = @opendir( WPMU_PLUGIN_DIR );
  326. if ( $plugins_dir ) {
  327. while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
  328. if ( substr( $file, -4 ) == '.php' ) {
  329. $plugin_files[] = $file;
  330. }
  331. }
  332. } else {
  333. return $wp_plugins;
  334. }
  335. closedir( $plugins_dir );
  336. if ( empty( $plugin_files ) ) {
  337. return $wp_plugins;
  338. }
  339. foreach ( $plugin_files as $plugin_file ) {
  340. if ( ! is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) ) {
  341. continue;
  342. }
  343. // Do not apply markup/translate as it will be cached.
  344. $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false );
  345. if ( empty( $plugin_data['Name'] ) ) {
  346. $plugin_data['Name'] = $plugin_file;
  347. }
  348. $wp_plugins[ $plugin_file ] = $plugin_data;
  349. }
  350. if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php' ) <= 30 ) {
  351. // Silence is golden.
  352. unset( $wp_plugins['index.php'] );
  353. }
  354. uasort( $wp_plugins, '_sort_uname_callback' );
  355. return $wp_plugins;
  356. }
  357. /**
  358. * Callback to sort array by a 'Name' key.
  359. *
  360. * @since 3.1.0
  361. *
  362. * @access private
  363. *
  364. * @param array $a array with 'Name' key.
  365. * @param array $b array with 'Name' key.
  366. * @return int Return 0 or 1 based on two string comparison.
  367. */
  368. function _sort_uname_callback( $a, $b ) {
  369. return strnatcasecmp( $a['Name'], $b['Name'] );
  370. }
  371. /**
  372. * Check the wp-content directory and retrieve all drop-ins with any plugin data.
  373. *
  374. * @since 3.0.0
  375. * @return array[] Array of arrays of dropin plugin data, keyed by plugin file name. See `get_plugin_data()`.
  376. */
  377. function get_dropins() {
  378. $dropins = array();
  379. $plugin_files = array();
  380. $_dropins = _get_dropins();
  381. // Files in wp-content directory.
  382. $plugins_dir = @opendir( WP_CONTENT_DIR );
  383. if ( $plugins_dir ) {
  384. while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
  385. if ( isset( $_dropins[ $file ] ) ) {
  386. $plugin_files[] = $file;
  387. }
  388. }
  389. } else {
  390. return $dropins;
  391. }
  392. closedir( $plugins_dir );
  393. if ( empty( $plugin_files ) ) {
  394. return $dropins;
  395. }
  396. foreach ( $plugin_files as $plugin_file ) {
  397. if ( ! is_readable( WP_CONTENT_DIR . "/$plugin_file" ) ) {
  398. continue;
  399. }
  400. // Do not apply markup/translate as it will be cached.
  401. $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false );
  402. if ( empty( $plugin_data['Name'] ) ) {
  403. $plugin_data['Name'] = $plugin_file;
  404. }
  405. $dropins[ $plugin_file ] = $plugin_data;
  406. }
  407. uksort( $dropins, 'strnatcasecmp' );
  408. return $dropins;
  409. }
  410. /**
  411. * Returns drop-ins that WordPress uses.
  412. *
  413. * Includes Multisite drop-ins only when is_multisite()
  414. *
  415. * @since 3.0.0
  416. * @return array[] Key is file name. The value is an array, with the first value the
  417. * purpose of the drop-in and the second value the name of the constant that must be
  418. * true for the drop-in to be used, or true if no constant is required.
  419. */
  420. function _get_dropins() {
  421. $dropins = array(
  422. 'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE
  423. 'db.php' => array( __( 'Custom database class.' ), true ), // Auto on load.
  424. 'db-error.php' => array( __( 'Custom database error message.' ), true ), // Auto on error.
  425. 'install.php' => array( __( 'Custom installation script.' ), true ), // Auto on installation.
  426. 'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // Auto on maintenance.
  427. 'object-cache.php' => array( __( 'External object cache.' ), true ), // Auto on load.
  428. 'php-error.php' => array( __( 'Custom PHP error message.' ), true ), // Auto on error.
  429. 'fatal-error-handler.php' => array( __( 'Custom PHP fatal error handler.' ), true ), // Auto on error.
  430. );
  431. if ( is_multisite() ) {
  432. $dropins['sunrise.php'] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE
  433. $dropins['blog-deleted.php'] = array( __( 'Custom site deleted message.' ), true ); // Auto on deleted blog.
  434. $dropins['blog-inactive.php'] = array( __( 'Custom site inactive message.' ), true ); // Auto on inactive blog.
  435. $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // Auto on archived or spammed blog.
  436. }
  437. return $dropins;
  438. }
  439. /**
  440. * Determines whether a plugin is active.
  441. *
  442. * Only plugins installed in the plugins/ folder can be active.
  443. *
  444. * Plugins in the mu-plugins/ folder can't be "activated," so this function will
  445. * return false for those plugins.
  446. *
  447. * For more information on this and similar theme functions, check out
  448. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  449. * Conditional Tags} article in the Theme Developer Handbook.
  450. *
  451. * @since 2.5.0
  452. *
  453. * @param string $plugin Path to the plugin file relative to the plugins directory.
  454. * @return bool True, if in the active plugins list. False, not in the list.
  455. */
  456. function is_plugin_active( $plugin ) {
  457. return in_array( $plugin, (array) get_option( 'active_plugins', array() ), true ) || is_plugin_active_for_network( $plugin );
  458. }
  459. /**
  460. * Determines whether the plugin is inactive.
  461. *
  462. * Reverse of is_plugin_active(). Used as a callback.
  463. *
  464. * For more information on this and similar theme functions, check out
  465. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  466. * Conditional Tags} article in the Theme Developer Handbook.
  467. *
  468. * @since 3.1.0
  469. * @see is_plugin_active()
  470. *
  471. * @param string $plugin Path to the plugin file relative to the plugins directory.
  472. * @return bool True if inactive. False if active.
  473. */
  474. function is_plugin_inactive( $plugin ) {
  475. return ! is_plugin_active( $plugin );
  476. }
  477. /**
  478. * Determines whether the plugin is active for the entire network.
  479. *
  480. * Only plugins installed in the plugins/ folder can be active.
  481. *
  482. * Plugins in the mu-plugins/ folder can't be "activated," so this function will
  483. * return false for those plugins.
  484. *
  485. * For more information on this and similar theme functions, check out
  486. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  487. * Conditional Tags} article in the Theme Developer Handbook.
  488. *
  489. * @since 3.0.0
  490. *
  491. * @param string $plugin Path to the plugin file relative to the plugins directory.
  492. * @return bool True if active for the network, otherwise false.
  493. */
  494. function is_plugin_active_for_network( $plugin ) {
  495. if ( ! is_multisite() ) {
  496. return false;
  497. }
  498. $plugins = get_site_option( 'active_sitewide_plugins' );
  499. if ( isset( $plugins[ $plugin ] ) ) {
  500. return true;
  501. }
  502. return false;
  503. }
  504. /**
  505. * Checks for "Network: true" in the plugin header to see if this should
  506. * be activated only as a network wide plugin. The plugin would also work
  507. * when Multisite is not enabled.
  508. *
  509. * Checks for "Site Wide Only: true" for backward compatibility.
  510. *
  511. * @since 3.0.0
  512. *
  513. * @param string $plugin Path to the plugin file relative to the plugins directory.
  514. * @return bool True if plugin is network only, false otherwise.
  515. */
  516. function is_network_only_plugin( $plugin ) {
  517. $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
  518. if ( $plugin_data ) {
  519. return $plugin_data['Network'];
  520. }
  521. return false;
  522. }
  523. /**
  524. * Attempts activation of plugin in a "sandbox" and redirects on success.
  525. *
  526. * A plugin that is already activated will not attempt to be activated again.
  527. *
  528. * The way it works is by setting the redirection to the error before trying to
  529. * include the plugin file. If the plugin fails, then the redirection will not
  530. * be overwritten with the success message. Also, the options will not be
  531. * updated and the activation hook will not be called on plugin error.
  532. *
  533. * It should be noted that in no way the below code will actually prevent errors
  534. * within the file. The code should not be used elsewhere to replicate the
  535. * "sandbox", which uses redirection to work.
  536. * {@source 13 1}
  537. *
  538. * If any errors are found or text is outputted, then it will be captured to
  539. * ensure that the success redirection will update the error redirection.
  540. *
  541. * @since 2.5.0
  542. * @since 5.2.0 Test for WordPress version and PHP version compatibility.
  543. *
  544. * @param string $plugin Path to the plugin file relative to the plugins directory.
  545. * @param string $redirect Optional. URL to redirect to.
  546. * @param bool $network_wide Optional. Whether to enable the plugin for all sites in the network
  547. * or just the current site. Multisite only. Default false.
  548. * @param bool $silent Optional. Whether to prevent calling activation hooks. Default false.
  549. * @return null|WP_Error Null on success, WP_Error on invalid file.
  550. */
  551. function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
  552. $plugin = plugin_basename( trim( $plugin ) );
  553. if ( is_multisite() && ( $network_wide || is_network_only_plugin( $plugin ) ) ) {
  554. $network_wide = true;
  555. $current = get_site_option( 'active_sitewide_plugins', array() );
  556. $_GET['networkwide'] = 1; // Back compat for plugins looking for this value.
  557. } else {
  558. $current = get_option( 'active_plugins', array() );
  559. }
  560. $valid = validate_plugin( $plugin );
  561. if ( is_wp_error( $valid ) ) {
  562. return $valid;
  563. }
  564. $requirements = validate_plugin_requirements( $plugin );
  565. if ( is_wp_error( $requirements ) ) {
  566. return $requirements;
  567. }
  568. if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current, true ) ) ) {
  569. if ( ! empty( $redirect ) ) {
  570. // We'll override this later if the plugin can be included without fatal error.
  571. wp_redirect( add_query_arg( '_error_nonce', wp_create_nonce( 'plugin-activation-error_' . $plugin ), $redirect ) );
  572. }
  573. ob_start();
  574. wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
  575. $_wp_plugin_file = $plugin;
  576. if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) {
  577. define( 'WP_SANDBOX_SCRAPING', true );
  578. }
  579. include_once WP_PLUGIN_DIR . '/' . $plugin;
  580. $plugin = $_wp_plugin_file; // Avoid stomping of the $plugin variable in a plugin.
  581. if ( ! $silent ) {
  582. /**
  583. * Fires before a plugin is activated.
  584. *
  585. * If a plugin is silently activated (such as during an update),
  586. * this hook does not fire.
  587. *
  588. * @since 2.9.0
  589. *
  590. * @param string $plugin Path to the plugin file relative to the plugins directory.
  591. * @param bool $network_wide Whether to enable the plugin for all sites in the network
  592. * or just the current site. Multisite only. Default is false.
  593. */
  594. do_action( 'activate_plugin', $plugin, $network_wide );
  595. /**
  596. * Fires as a specific plugin is being activated.
  597. *
  598. * This hook is the "activation" hook used internally by register_activation_hook().
  599. * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
  600. *
  601. * If a plugin is silently activated (such as during an update), this hook does not fire.
  602. *
  603. * @since 2.0.0
  604. *
  605. * @param bool $network_wide Whether to enable the plugin for all sites in the network
  606. * or just the current site. Multisite only. Default is false.
  607. */
  608. do_action( "activate_{$plugin}", $network_wide );
  609. }
  610. if ( $network_wide ) {
  611. $current = get_site_option( 'active_sitewide_plugins', array() );
  612. $current[ $plugin ] = time();
  613. update_site_option( 'active_sitewide_plugins', $current );
  614. } else {
  615. $current = get_option( 'active_plugins', array() );
  616. $current[] = $plugin;
  617. sort( $current );
  618. update_option( 'active_plugins', $current );
  619. }
  620. if ( ! $silent ) {
  621. /**
  622. * Fires after a plugin has been activated.
  623. *
  624. * If a plugin is silently activated (such as during an update),
  625. * this hook does not fire.
  626. *
  627. * @since 2.9.0
  628. *
  629. * @param string $plugin Path to the plugin file relative to the plugins directory.
  630. * @param bool $network_wide Whether to enable the plugin for all sites in the network
  631. * or just the current site. Multisite only. Default is false.
  632. */
  633. do_action( 'activated_plugin', $plugin, $network_wide );
  634. }
  635. if ( ob_get_length() > 0 ) {
  636. $output = ob_get_clean();
  637. return new WP_Error( 'unexpected_output', __( 'The plugin generated unexpected output.' ), $output );
  638. }
  639. ob_end_clean();
  640. }
  641. return null;
  642. }
  643. /**
  644. * Deactivate a single plugin or multiple plugins.
  645. *
  646. * The deactivation hook is disabled by the plugin upgrader by using the $silent
  647. * parameter.
  648. *
  649. * @since 2.5.0
  650. *
  651. * @param string|string[] $plugins Single plugin or list of plugins to deactivate.
  652. * @param bool $silent Prevent calling deactivation hooks. Default false.
  653. * @param bool|null $network_wide Whether to deactivate the plugin for all sites in the network.
  654. * A value of null will deactivate plugins for both the network
  655. * and the current site. Multisite only. Default null.
  656. */
  657. function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) {
  658. if ( is_multisite() ) {
  659. $network_current = get_site_option( 'active_sitewide_plugins', array() );
  660. }
  661. $current = get_option( 'active_plugins', array() );
  662. $do_blog = false;
  663. $do_network = false;
  664. foreach ( (array) $plugins as $plugin ) {
  665. $plugin = plugin_basename( trim( $plugin ) );
  666. if ( ! is_plugin_active( $plugin ) ) {
  667. continue;
  668. }
  669. $network_deactivating = false !== $network_wide && is_plugin_active_for_network( $plugin );
  670. if ( ! $silent ) {
  671. /**
  672. * Fires before a plugin is deactivated.
  673. *
  674. * If a plugin is silently deactivated (such as during an update),
  675. * this hook does not fire.
  676. *
  677. * @since 2.9.0
  678. *
  679. * @param string $plugin Path to the plugin file relative to the plugins directory.
  680. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
  681. * or just the current site. Multisite only. Default false.
  682. */
  683. do_action( 'deactivate_plugin', $plugin, $network_deactivating );
  684. }
  685. if ( false !== $network_wide ) {
  686. if ( is_plugin_active_for_network( $plugin ) ) {
  687. $do_network = true;
  688. unset( $network_current[ $plugin ] );
  689. } elseif ( $network_wide ) {
  690. continue;
  691. }
  692. }
  693. if ( true !== $network_wide ) {
  694. $key = array_search( $plugin, $current, true );
  695. if ( false !== $key ) {
  696. $do_blog = true;
  697. unset( $current[ $key ] );
  698. }
  699. }
  700. if ( $do_blog && wp_is_recovery_mode() ) {
  701. list( $extension ) = explode( '/', $plugin );
  702. wp_paused_plugins()->delete( $extension );
  703. }
  704. if ( ! $silent ) {
  705. /**
  706. * Fires as a specific plugin is being deactivated.
  707. *
  708. * This hook is the "deactivation" hook used internally by register_deactivation_hook().
  709. * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
  710. *
  711. * If a plugin is silently deactivated (such as during an update), this hook does not fire.
  712. *
  713. * @since 2.0.0
  714. *
  715. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
  716. * or just the current site. Multisite only. Default false.
  717. */
  718. do_action( "deactivate_{$plugin}", $network_deactivating );
  719. /**
  720. * Fires after a plugin is deactivated.
  721. *
  722. * If a plugin is silently deactivated (such as during an update),
  723. * this hook does not fire.
  724. *
  725. * @since 2.9.0
  726. *
  727. * @param string $plugin Path to the plugin file relative to the plugins directory.
  728. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
  729. * or just the current site. Multisite only. Default false.
  730. */
  731. do_action( 'deactivated_plugin', $plugin, $network_deactivating );
  732. }
  733. }
  734. if ( $do_blog ) {
  735. update_option( 'active_plugins', $current );
  736. }
  737. if ( $do_network ) {
  738. update_site_option( 'active_sitewide_plugins', $network_current );
  739. }
  740. }
  741. /**
  742. * Activate multiple plugins.
  743. *
  744. * When WP_Error is returned, it does not mean that one of the plugins had
  745. * errors. It means that one or more of the plugin file paths were invalid.
  746. *
  747. * The execution will be halted as soon as one of the plugins has an error.
  748. *
  749. * @since 2.6.0
  750. *
  751. * @param string|string[] $plugins Single plugin or list of plugins to activate.
  752. * @param string $redirect Redirect to page after successful activation.
  753. * @param bool $network_wide Whether to enable the plugin for all sites in the network.
  754. * Default false.
  755. * @param bool $silent Prevent calling activation hooks. Default false.
  756. * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
  757. */
  758. function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
  759. if ( ! is_array( $plugins ) ) {
  760. $plugins = array( $plugins );
  761. }
  762. $errors = array();
  763. foreach ( $plugins as $plugin ) {
  764. if ( ! empty( $redirect ) ) {
  765. $redirect = add_query_arg( 'plugin', $plugin, $redirect );
  766. }
  767. $result = activate_plugin( $plugin, $redirect, $network_wide, $silent );
  768. if ( is_wp_error( $result ) ) {
  769. $errors[ $plugin ] = $result;
  770. }
  771. }
  772. if ( ! empty( $errors ) ) {
  773. return new WP_Error( 'plugins_invalid', __( 'One of the plugins is invalid.' ), $errors );
  774. }
  775. return true;
  776. }
  777. /**
  778. * Remove directory and files of a plugin for a list of plugins.
  779. *
  780. * @since 2.6.0
  781. *
  782. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  783. *
  784. * @param string[] $plugins List of plugin paths to delete, relative to the plugins directory.
  785. * @param string $deprecated Not used.
  786. * @return bool|null|WP_Error True on success, false if `$plugins` is empty, `WP_Error` on failure.
  787. * `null` if filesystem credentials are required to proceed.
  788. */
  789. function delete_plugins( $plugins, $deprecated = '' ) {
  790. global $wp_filesystem;
  791. if ( empty( $plugins ) ) {
  792. return false;
  793. }
  794. $checked = array();
  795. foreach ( $plugins as $plugin ) {
  796. $checked[] = 'checked[]=' . $plugin;
  797. }
  798. $url = wp_nonce_url( 'plugins.php?action=delete-selected&verify-delete=1&' . implode( '&', $checked ), 'bulk-plugins' );
  799. ob_start();
  800. $credentials = request_filesystem_credentials( $url );
  801. $data = ob_get_clean();
  802. if ( false === $credentials ) {
  803. if ( ! empty( $data ) ) {
  804. require_once ABSPATH . 'wp-admin/admin-header.php';
  805. echo $data;
  806. require_once ABSPATH . 'wp-admin/admin-footer.php';
  807. exit;
  808. }
  809. return;
  810. }
  811. if ( ! WP_Filesystem( $credentials ) ) {
  812. ob_start();
  813. // Failed to connect. Error and request again.
  814. request_filesystem_credentials( $url, '', true );
  815. $data = ob_get_clean();
  816. if ( ! empty( $data ) ) {
  817. require_once ABSPATH . 'wp-admin/admin-header.php';
  818. echo $data;
  819. require_once ABSPATH . 'wp-admin/admin-footer.php';
  820. exit;
  821. }
  822. return;
  823. }
  824. if ( ! is_object( $wp_filesystem ) ) {
  825. return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
  826. }
  827. if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
  828. return new WP_Error( 'fs_error', __( 'Filesystem error.' ), $wp_filesystem->errors );
  829. }
  830. // Get the base plugin folder.
  831. $plugins_dir = $wp_filesystem->wp_plugins_dir();
  832. if ( empty( $plugins_dir ) ) {
  833. return new WP_Error( 'fs_no_plugins_dir', __( 'Unable to locate WordPress plugin directory.' ) );
  834. }
  835. $plugins_dir = trailingslashit( $plugins_dir );
  836. $plugin_translations = wp_get_installed_translations( 'plugins' );
  837. $errors = array();
  838. foreach ( $plugins as $plugin_file ) {
  839. // Run Uninstall hook.
  840. if ( is_uninstallable_plugin( $plugin_file ) ) {
  841. uninstall_plugin( $plugin_file );
  842. }
  843. /**
  844. * Fires immediately before a plugin deletion attempt.
  845. *
  846. * @since 4.4.0
  847. *
  848. * @param string $plugin_file Path to the plugin file relative to the plugins directory.
  849. */
  850. do_action( 'delete_plugin', $plugin_file );
  851. $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin_file ) );
  852. // If plugin is in its own directory, recursively delete the directory.
  853. // Base check on if plugin includes directory separator AND that it's not the root plugin folder.
  854. if ( strpos( $plugin_file, '/' ) && $this_plugin_dir != $plugins_dir ) {
  855. $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
  856. } else {
  857. $deleted = $wp_filesystem->delete( $plugins_dir . $plugin_file );
  858. }
  859. /**
  860. * Fires immediately after a plugin deletion attempt.
  861. *
  862. * @since 4.4.0
  863. *
  864. * @param string $plugin_file Path to the plugin file relative to the plugins directory.
  865. * @param bool $deleted Whether the plugin deletion was successful.
  866. */
  867. do_action( 'deleted_plugin', $plugin_file, $deleted );
  868. if ( ! $deleted ) {
  869. $errors[] = $plugin_file;
  870. continue;
  871. }
  872. // Remove language files, silently.
  873. $plugin_slug = dirname( $plugin_file );
  874. if ( '.' !== $plugin_slug && ! empty( $plugin_translations[ $plugin_slug ] ) ) {
  875. $translations = $plugin_translations[ $plugin_slug ];
  876. foreach ( $translations as $translation => $data ) {
  877. $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.po' );
  878. $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.mo' );
  879. $json_translation_files = glob( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '-*.json' );
  880. if ( $json_translation_files ) {
  881. array_map( array( $wp_filesystem, 'delete' ), $json_translation_files );
  882. }
  883. }
  884. }
  885. }
  886. // Remove deleted plugins from the plugin updates list.
  887. $current = get_site_transient( 'update_plugins' );
  888. if ( $current ) {
  889. // Don't remove the plugins that weren't deleted.
  890. $deleted = array_diff( $plugins, $errors );
  891. foreach ( $deleted as $plugin_file ) {
  892. unset( $current->response[ $plugin_file ] );
  893. }
  894. set_site_transient( 'update_plugins', $current );
  895. }
  896. if ( ! empty( $errors ) ) {
  897. if ( 1 === count( $errors ) ) {
  898. /* translators: %s: Plugin filename. */
  899. $message = __( 'Could not fully remove the plugin %s.' );
  900. } else {
  901. /* translators: %s: Comma-separated list of plugin filenames. */
  902. $message = __( 'Could not fully remove the plugins %s.' );
  903. }
  904. return new WP_Error( 'could_not_remove_plugin', sprintf( $message, implode( ', ', $errors ) ) );
  905. }
  906. return true;
  907. }
  908. /**
  909. * Validate active plugins
  910. *
  911. * Validate all active plugins, deactivates invalid and
  912. * returns an array of deactivated ones.
  913. *
  914. * @since 2.5.0
  915. * @return WP_Error[] Array of plugin errors keyed by plugin file name.
  916. */
  917. function validate_active_plugins() {
  918. $plugins = get_option( 'active_plugins', array() );
  919. // Validate vartype: array.
  920. if ( ! is_array( $plugins ) ) {
  921. update_option( 'active_plugins', array() );
  922. $plugins = array();
  923. }
  924. if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
  925. $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
  926. $plugins = array_merge( $plugins, array_keys( $network_plugins ) );
  927. }
  928. if ( empty( $plugins ) ) {
  929. return array();
  930. }
  931. $invalid = array();
  932. // Invalid plugins get deactivated.
  933. foreach ( $plugins as $plugin ) {
  934. $result = validate_plugin( $plugin );
  935. if ( is_wp_error( $result ) ) {
  936. $invalid[ $plugin ] = $result;
  937. deactivate_plugins( $plugin, true );
  938. }
  939. }
  940. return $invalid;
  941. }
  942. /**
  943. * Validate the plugin path.
  944. *
  945. * Checks that the main plugin file exists and is a valid plugin. See validate_file().
  946. *
  947. * @since 2.5.0
  948. *
  949. * @param string $plugin Path to the plugin file relative to the plugins directory.
  950. * @return int|WP_Error 0 on success, WP_Error on failure.
  951. */
  952. function validate_plugin( $plugin ) {
  953. if ( validate_file( $plugin ) ) {
  954. return new WP_Error( 'plugin_invalid', __( 'Invalid plugin path.' ) );
  955. }
  956. if ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) {
  957. return new WP_Error( 'plugin_not_found', __( 'Plugin file does not exist.' ) );
  958. }
  959. $installed_plugins = get_plugins();
  960. if ( ! isset( $installed_plugins[ $plugin ] ) ) {
  961. return new WP_Error( 'no_plugin_header', __( 'The plugin does not have a valid header.' ) );
  962. }
  963. return 0;
  964. }
  965. /**
  966. * Validates the plugin requirements for WordPress version and PHP version.
  967. *
  968. * Uses the information from `Requires at least` and `Requires PHP` headers
  969. * defined in the plugin's main PHP file.
  970. *
  971. * If the headers are not present in the plugin's main PHP file,
  972. * `readme.txt` is also checked as a fallback.
  973. *
  974. * @since 5.2.0
  975. * @since 5.3.0 Added support for reading the headers from the plugin's
  976. * main PHP file, with `readme.txt` as a fallback.
  977. *
  978. * @param string $plugin Path to the plugin file relative to the plugins directory.
  979. * @return true|WP_Error True if requirements are met, WP_Error on failure.
  980. */
  981. function validate_plugin_requirements( $plugin ) {
  982. $plugin_headers = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
  983. $requirements = array(
  984. 'requires' => ! empty( $plugin_headers['RequiresWP'] ) ? $plugin_headers['RequiresWP'] : '',
  985. 'requires_php' => ! empty( $plugin_headers['RequiresPHP'] ) ? $plugin_headers['RequiresPHP'] : '',
  986. );
  987. $readme_file = WP_PLUGIN_DIR . '/' . dirname( $plugin ) . '/readme.txt';
  988. if ( file_exists( $readme_file ) ) {
  989. $readme_headers = get_file_data(
  990. $readme_file,
  991. array(
  992. 'requires' => 'Requires at least',
  993. 'requires_php' => 'Requires PHP',
  994. ),
  995. 'plugin'
  996. );
  997. $requirements = array_merge( $readme_headers, $requirements );
  998. }
  999. $compatible_wp = is_wp_version_compatible( $requirements['requires'] );
  1000. $compatible_php = is_php_version_compatible( $requirements['requires_php'] );
  1001. if ( ! $compatible_wp && ! $compatible_php ) {
  1002. return new WP_Error(
  1003. 'plugin_wp_php_incompatible',
  1004. sprintf(
  1005. /* translators: %s: Plugin name. */
  1006. _x( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.', 'plugin' ),
  1007. $plugin_headers['Name']
  1008. )
  1009. );
  1010. } elseif ( ! $compatible_php ) {
  1011. return new WP_Error(
  1012. 'plugin_php_incompatible',
  1013. sprintf(
  1014. /* translators: %s: Plugin name. */
  1015. _x( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.', 'plugin' ),
  1016. $plugin_headers['Name']
  1017. )
  1018. );
  1019. } elseif ( ! $compatible_wp ) {
  1020. return new WP_Error(
  1021. 'plugin_wp_incompatible',
  1022. sprintf(
  1023. /* translators: %s: Plugin name. */
  1024. _x( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.', 'plugin' ),
  1025. $plugin_headers['Name']
  1026. )
  1027. );
  1028. }
  1029. return true;
  1030. }
  1031. /**
  1032. * Whether the plugin can be uninstalled.
  1033. *
  1034. * @since 2.7.0
  1035. *
  1036. * @param string $plugin Path to the plugin file relative to the plugins directory.
  1037. * @return bool Whether plugin can be uninstalled.
  1038. */
  1039. function is_uninstallable_plugin( $plugin ) {
  1040. $file = plugin_basename( $plugin );
  1041. $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
  1042. if ( isset( $uninstallable_plugins[ $file ] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname( $file ) . '/uninstall.php' ) ) {
  1043. return true;
  1044. }
  1045. return false;
  1046. }
  1047. /**
  1048. * Uninstall a single plugin.
  1049. *
  1050. * Calls the uninstall hook, if it is available.
  1051. *
  1052. * @since 2.7.0
  1053. *
  1054. * @param string $plugin Path to the plugin file relative to the plugins directory.
  1055. * @return true True if a plugin's uninstall.php file has been found and included.
  1056. */
  1057. function uninstall_plugin( $plugin ) {
  1058. $file = plugin_basename( $plugin );
  1059. $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
  1060. /**
  1061. * Fires in uninstall_plugin() immediately before the plugin is uninstalled.
  1062. *
  1063. * @since 4.5.0
  1064. *
  1065. * @param string $plugin Path to the plugin file relative to the plugins directory.
  1066. * @param array $uninstallable_plugins Uninstallable plugins.
  1067. */
  1068. do_action( 'pre_uninstall_plugin', $plugin, $uninstallable_plugins );
  1069. if ( file_exists( WP_PLUGIN_DIR . '/' . dirname( $file ) . '/uninstall.php' ) ) {
  1070. if ( isset( $uninstallable_plugins[ $file ] ) ) {
  1071. unset( $uninstallable_plugins[ $file ] );
  1072. update_option( 'uninstall_plugins', $uninstallable_plugins );
  1073. }
  1074. unset( $uninstallable_plugins );
  1075. define( 'WP_UNINSTALL_PLUGIN', $file );
  1076. wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
  1077. include WP_PLUGIN_DIR . '/' . dirname( $file ) . '/uninstall.php';
  1078. return true;
  1079. }
  1080. if ( isset( $uninstallable_plugins[ $file ] ) ) {
  1081. $callable = $uninstallable_plugins[ $file ];
  1082. unset( $uninstallable_plugins[ $file ] );
  1083. update_option( 'uninstall_plugins', $uninstallable_plugins );
  1084. unset( $uninstallable_plugins );
  1085. wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
  1086. include WP_PLUGIN_DIR . '/' . $file;
  1087. add_action( "uninstall_{$file}", $callable );
  1088. /**
  1089. * Fires in uninstall_plugin() once the plugin has been uninstalled.
  1090. *
  1091. * The action concatenates the 'uninstall_' prefix with the basename of the
  1092. * plugin passed to uninstall_plugin() to create a dynamically-named action.
  1093. *
  1094. * @since 2.7.0
  1095. */
  1096. do_action( "uninstall_{$file}" );
  1097. }
  1098. }
  1099. //
  1100. // Menu.
  1101. //
  1102. /**
  1103. * Add a top-level menu page.
  1104. *
  1105. * This function takes a capability which will be used to determine whether
  1106. * or not a page is included in the menu.
  1107. *
  1108. * The function which is hooked in to handle the output of the page must check
  1109. * that the user has the required capability as well.
  1110. *
  1111. * @since 1.5.0
  1112. *
  1113. * @global array $menu
  1114. * @global array $admin_page_hooks
  1115. * @global array $_registered_pages
  1116. * @global array $_parent_pages
  1117. *
  1118. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1119. * @param string $menu_title The text to be used for the menu.
  1120. * @param string $capability The capability required for this menu to be displayed to the user.
  1121. * @param string $menu_slug The slug name to refer to this menu by. Should be unique for this menu page and only
  1122. * include lowercase alphanumeric, dashes, and underscores characters to be compatible
  1123. * with sanitize_key().
  1124. * @param callable $function The function to be called to output the content for this page.
  1125. * @param string $icon_url The URL to the icon to be used for this menu.
  1126. * * Pass a base64-encoded SVG using a data URI, which will be colored to match
  1127. * the color scheme. This should begin with 'data:image/svg+xml;base64,'.
  1128. * * Pass the name of a Dashicons helper class to use a font icon,
  1129. * e.g. 'dashicons-chart-pie'.
  1130. * * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.
  1131. * @param int $position The position in the menu order this item should appear.
  1132. * @return string The resulting page's hook_suffix.
  1133. */
  1134. function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) {
  1135. global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
  1136. $menu_slug = plugin_basename( $menu_slug );
  1137. $admin_page_hooks[ $menu_slug ] = sanitize_title( $menu_title );
  1138. $hookname = get_plugin_page_hookname( $menu_slug, '' );
  1139. if ( ! empty( $function ) && ! empty( $hookname ) && current_user_can( $capability ) ) {
  1140. add_action( $hookname, $function );
  1141. }
  1142. if ( empty( $icon_url ) ) {
  1143. $icon_url = 'dashicons-admin-generic';
  1144. $icon_class = 'menu-icon-generic ';
  1145. } else {
  1146. $icon_url = set_url_scheme( $icon_url );
  1147. $icon_class = '';
  1148. }
  1149. $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url );
  1150. if ( null === $position ) {
  1151. $menu[] = $new_menu;
  1152. } elseif ( isset( $menu[ "$position" ] ) ) {
  1153. $position = $position + substr( base_convert( md5( $menu_slug . $menu_title ), 16, 10 ), -5 ) * 0.00001;
  1154. $menu[ "$position" ] = $new_menu;
  1155. } else {
  1156. $menu[ $position ] = $new_menu;
  1157. }
  1158. $_registered_pages[ $hookname ] = true;
  1159. // No parent as top level.
  1160. $_parent_pages[ $menu_slug ] = false;
  1161. return $hookname;
  1162. }
  1163. /**
  1164. * Add a submenu page.
  1165. *
  1166. * This function takes a capability which will be used to determine whether
  1167. * or not a page is included in the menu.
  1168. *
  1169. * The function which is hooked in to handle the output of the page must check
  1170. * that the user has the required capability as well.
  1171. *
  1172. * @since 1.5.0
  1173. * @since 5.3.0 Added the `$position` parameter.
  1174. *
  1175. * @global array $submenu
  1176. * @global array $menu
  1177. * @global array $_wp_real_parent_file
  1178. * @global bool $_wp_submenu_nopriv
  1179. * @global array $_registered_pages
  1180. * @global array $_parent_pages
  1181. *
  1182. * @param string $parent_slug The slug name for the parent menu (or the file name of a standard
  1183. * WordPress admin page).
  1184. * @param string $page_title The text to be displayed in the title tags of the page when the menu
  1185. * is selected.
  1186. * @param string $menu_title The text to be used for the menu.
  1187. * @param string $capability The capability required for this menu to be displayed to the user.
  1188. * @param string $menu_slug The slug name to refer to this menu by. Should be unique for this menu
  1189. * and only include lowercase alphanumeric, dashes, and underscores characters
  1190. * to be compatible with sanitize_key().
  1191. * @param callable $function The function to be called to output the content for this page.
  1192. * @param int $position The position in the menu order this item should appear.
  1193. * @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
  1194. */
  1195. function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
  1196. global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv,
  1197. $_registered_pages, $_parent_pages;
  1198. $menu_slug = plugin_basename( $menu_slug );
  1199. $parent_slug = plugin_basename( $parent_slug );
  1200. if ( isset( $_wp_real_parent_file[ $parent_slug ] ) ) {
  1201. $parent_slug = $_wp_real_parent_file[ $parent_slug ];
  1202. }
  1203. if ( ! current_user_can( $capability ) ) {
  1204. $_wp_submenu_nopriv[ $parent_slug ][ $menu_slug ] = true;
  1205. return false;
  1206. }
  1207. /*
  1208. * If the parent doesn't already have a submenu, add a link to the parent
  1209. * as the first item in the submenu. If the submenu file is the same as the
  1210. * parent file someone is trying to link back to the parent manually. In
  1211. * this case, don't automatically add a link back to avoid duplication.
  1212. */
  1213. if ( ! isset( $submenu[ $parent_slug ] ) && $menu_slug != $parent_slug ) {
  1214. foreach ( (array) $menu as $parent_menu ) {
  1215. if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) ) {
  1216. $submenu[ $parent_slug ][] = array_slice( $parent_menu, 0, 4 );
  1217. }
  1218. }
  1219. }
  1220. $new_sub_menu = array( $menu_title, $capability, $menu_slug, $page_title );
  1221. if ( ! is_int( $position ) ) {
  1222. if ( null !== $position ) {
  1223. _doing_it_wrong(
  1224. __FUNCTION__,
  1225. sprintf(
  1226. /* translators: %s: add_submenu_page() */
  1227. __( 'The seventh parameter passed to %s should be an integer representing menu position.' ),
  1228. '<code>add_submenu_page()</code>'
  1229. ),
  1230. '5.3.0'
  1231. );
  1232. }
  1233. $submenu[ $parent_slug ][] = $new_sub_menu;
  1234. } else {
  1235. // Append the submenu if the parent item is not present in the submenu,
  1236. // or if position is equal or higher than the number of items in the array.
  1237. if ( ! isset( $submenu[ $parent_slug ] ) || $position >= count( $submenu[ $parent_slug ] ) ) {
  1238. $submenu[ $parent_slug ][] = $new_sub_menu;
  1239. } else {
  1240. // Test for a negative position.
  1241. $position = max( $position, 0 );
  1242. if ( 0 === $position ) {
  1243. // For negative or `0` positions, prepend the submenu.
  1244. array_unshift( $submenu[ $parent_slug ], $new_sub_menu );
  1245. } else {
  1246. // Grab all of the items before the insertion point.
  1247. $before_items = array_slice( $submenu[ $parent_slug ], 0, $position, true );
  1248. // Grab all of the items after the insertion point.
  1249. $after_items = array_slice( $submenu[ $parent_slug ], $position, null, true );
  1250. // Add the new item.
  1251. $before_items[] = $new_sub_menu;
  1252. // Merge the items.
  1253. $submenu[ $parent_slug ] = array_merge( $before_items, $after_items );
  1254. }
  1255. }
  1256. }
  1257. // Sort the parent array.
  1258. ksort( $submenu[ $parent_slug ] );
  1259. $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug );
  1260. if ( ! empty( $function ) && ! empty( $hookname ) ) {
  1261. add_action( $hookname, $function );
  1262. }
  1263. $_registered_pages[ $hookname ]

Large files files are truncated, but you can click here to view the full file