25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

1884 satır
64 KiB

  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. * The metadata of the plugin's data searches for the following in the plugin's
  12. * header. All plugin data must be on its own line. For plugin description, it
  13. * must not have any newlines or only parts of the description will be displayed
  14. * and the same goes for the plugin data. The below is formatted for printing.
  15. *
  16. * /*
  17. * Plugin Name: Name of Plugin
  18. * Plugin URI: Link to plugin information
  19. * Description: Plugin Description
  20. * Author: Plugin author's name
  21. * Author URI: Link to the author's web site
  22. * Version: Must be set in the plugin for WordPress 2.3+
  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. * * / # Remove the space to close comment
  34. *
  35. * Some users have issues with opening large files and manipulating the contents
  36. * for want is usually the first 1kiB or 2kiB. This function stops pulling in
  37. * the plugin contents when it has all of the required plugin data.
  38. *
  39. * The first 8kiB of the file will be pulled in and if the plugin data is not
  40. * within that first 8kiB, then the plugin author should correct their plugin
  41. * and move the plugin data headers to the top.
  42. *
  43. * The plugin file is assumed to have permissions to allow for scripts to read
  44. * the file. This is not checked however and the file is only opened for
  45. * reading.
  46. *
  47. * @since 1.5.0
  48. *
  49. * @param string $plugin_file Path to the plugin file
  50. * @param bool $markup Optional. If the returned data should have HTML markup applied.
  51. * Default true.
  52. * @param bool $translate Optional. If the returned data should be translated. Default true.
  53. * @return array {
  54. * Plugin data. Values will be empty if not supplied by the plugin.
  55. *
  56. * @type string $Name Name of the plugin. Should be unique.
  57. * @type string $Title Title of the plugin and link to the plugin's site (if set).
  58. * @type string $Description Plugin description.
  59. * @type string $Author Author's name.
  60. * @type string $AuthorURI Author's website address (if set).
  61. * @type string $Version Plugin version.
  62. * @type string $TextDomain Plugin textdomain.
  63. * @type string $DomainPath Plugins relative directory path to .mo files.
  64. * @type bool $Network Whether the plugin can only be activated network-wide.
  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. // Site Wide Only is deprecated in favor of Network.
  79. '_sitewide' => 'Site Wide Only',
  80. );
  81. $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
  82. // Site Wide Only is the old header for Network
  83. if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) {
  84. /* translators: 1: Site Wide Only: true, 2: Network: true */
  85. _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>' ) );
  86. $plugin_data['Network'] = $plugin_data['_sitewide'];
  87. }
  88. $plugin_data['Network'] = ( 'true' == strtolower( $plugin_data['Network'] ) );
  89. unset( $plugin_data['_sitewide'] );
  90. // If no text domain is defined fall back to the plugin slug.
  91. if ( ! $plugin_data['TextDomain'] ) {
  92. $plugin_slug = dirname( plugin_basename( $plugin_file ) );
  93. if ( '.' !== $plugin_slug && false === strpos( $plugin_slug, '/' ) ) {
  94. $plugin_data['TextDomain'] = $plugin_slug;
  95. }
  96. }
  97. if ( $markup || $translate ) {
  98. $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
  99. } else {
  100. $plugin_data['Title'] = $plugin_data['Name'];
  101. $plugin_data['AuthorName'] = $plugin_data['Author'];
  102. }
  103. return $plugin_data;
  104. }
  105. /**
  106. * Sanitizes plugin data, optionally adds markup, optionally translates.
  107. *
  108. * @since 2.7.0
  109. * @access private
  110. * @see get_plugin_data()
  111. */
  112. function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) {
  113. // Sanitize the plugin filename to a WP_PLUGIN_DIR relative path
  114. $plugin_file = plugin_basename( $plugin_file );
  115. // Translate fields
  116. if ( $translate ) {
  117. if ( $textdomain = $plugin_data['TextDomain'] ) {
  118. if ( ! is_textdomain_loaded( $textdomain ) ) {
  119. if ( $plugin_data['DomainPath'] ) {
  120. load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] );
  121. } else {
  122. load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) );
  123. }
  124. }
  125. } elseif ( 'hello.php' == basename( $plugin_file ) ) {
  126. $textdomain = 'default';
  127. }
  128. if ( $textdomain ) {
  129. foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field )
  130. $plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain );
  131. }
  132. }
  133. // Sanitize fields
  134. $allowed_tags = $allowed_tags_in_links = array(
  135. 'abbr' => array( 'title' => true ),
  136. 'acronym' => array( 'title' => true ),
  137. 'code' => true,
  138. 'em' => true,
  139. 'strong' => true,
  140. );
  141. $allowed_tags['a'] = array( 'href' => true, 'title' => true );
  142. // Name is marked up inside <a> tags. Don't allow these.
  143. // Author is too, but some plugins have used <a> here (omitting Author URI).
  144. $plugin_data['Name'] = wp_kses( $plugin_data['Name'], $allowed_tags_in_links );
  145. $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $allowed_tags );
  146. $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags );
  147. $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $allowed_tags );
  148. $plugin_data['PluginURI'] = esc_url( $plugin_data['PluginURI'] );
  149. $plugin_data['AuthorURI'] = esc_url( $plugin_data['AuthorURI'] );
  150. $plugin_data['Title'] = $plugin_data['Name'];
  151. $plugin_data['AuthorName'] = $plugin_data['Author'];
  152. // Apply markup
  153. if ( $markup ) {
  154. if ( $plugin_data['PluginURI'] && $plugin_data['Name'] )
  155. $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '">' . $plugin_data['Name'] . '</a>';
  156. if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] )
  157. $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '">' . $plugin_data['Author'] . '</a>';
  158. $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
  159. if ( $plugin_data['Author'] )
  160. $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s.'), $plugin_data['Author'] ) . '</cite>';
  161. }
  162. return $plugin_data;
  163. }
  164. /**
  165. * Get a list of a plugin's files.
  166. *
  167. * @since 2.8.0
  168. *
  169. * @param string $plugin Plugin ID
  170. * @return array List of files relative to the plugin root.
  171. */
  172. function get_plugin_files($plugin) {
  173. $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
  174. $dir = dirname($plugin_file);
  175. $plugin_files = array($plugin);
  176. if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) {
  177. $plugins_dir = @ opendir( $dir );
  178. if ( $plugins_dir ) {
  179. while (($file = readdir( $plugins_dir ) ) !== false ) {
  180. if ( substr($file, 0, 1) == '.' )
  181. continue;
  182. if ( is_dir( $dir . '/' . $file ) ) {
  183. $plugins_subdir = @ opendir( $dir . '/' . $file );
  184. if ( $plugins_subdir ) {
  185. while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
  186. if ( substr($subfile, 0, 1) == '.' )
  187. continue;
  188. $plugin_files[] = plugin_basename("$dir/$file/$subfile");
  189. }
  190. @closedir( $plugins_subdir );
  191. }
  192. } else {
  193. if ( plugin_basename("$dir/$file") != $plugin )
  194. $plugin_files[] = plugin_basename("$dir/$file");
  195. }
  196. }
  197. @closedir( $plugins_dir );
  198. }
  199. }
  200. return $plugin_files;
  201. }
  202. /**
  203. * Check the plugins directory and retrieve all plugin files with plugin data.
  204. *
  205. * WordPress only supports plugin files in the base plugins directory
  206. * (wp-content/plugins) and in one directory above the plugins directory
  207. * (wp-content/plugins/my-plugin). The file it looks for has the plugin data
  208. * and must be found in those two locations. It is recommended to keep your
  209. * plugin files in their own directories.
  210. *
  211. * The file with the plugin data is the file that will be included and therefore
  212. * needs to have the main execution for the plugin. This does not mean
  213. * everything must be contained in the file and it is recommended that the file
  214. * be split for maintainability. Keep everything in one file for extreme
  215. * optimization purposes.
  216. *
  217. * @since 1.5.0
  218. *
  219. * @param string $plugin_folder Optional. Relative path to single plugin folder.
  220. * @return array Key is the plugin file path and the value is an array of the plugin data.
  221. */
  222. function get_plugins($plugin_folder = '') {
  223. if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
  224. $cache_plugins = array();
  225. if ( isset($cache_plugins[ $plugin_folder ]) )
  226. return $cache_plugins[ $plugin_folder ];
  227. $wp_plugins = array ();
  228. $plugin_root = WP_PLUGIN_DIR;
  229. if ( !empty($plugin_folder) )
  230. $plugin_root .= $plugin_folder;
  231. // Files in wp-content/plugins directory
  232. $plugins_dir = @ opendir( $plugin_root);
  233. $plugin_files = array();
  234. if ( $plugins_dir ) {
  235. while (($file = readdir( $plugins_dir ) ) !== false ) {
  236. if ( substr($file, 0, 1) == '.' )
  237. continue;
  238. if ( is_dir( $plugin_root.'/'.$file ) ) {
  239. $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
  240. if ( $plugins_subdir ) {
  241. while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
  242. if ( substr($subfile, 0, 1) == '.' )
  243. continue;
  244. if ( substr($subfile, -4) == '.php' )
  245. $plugin_files[] = "$file/$subfile";
  246. }
  247. closedir( $plugins_subdir );
  248. }
  249. } else {
  250. if ( substr($file, -4) == '.php' )
  251. $plugin_files[] = $file;
  252. }
  253. }
  254. closedir( $plugins_dir );
  255. }
  256. if ( empty($plugin_files) )
  257. return $wp_plugins;
  258. foreach ( $plugin_files as $plugin_file ) {
  259. if ( !is_readable( "$plugin_root/$plugin_file" ) )
  260. continue;
  261. $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
  262. if ( empty ( $plugin_data['Name'] ) )
  263. continue;
  264. $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
  265. }
  266. uasort( $wp_plugins, '_sort_uname_callback' );
  267. $cache_plugins[ $plugin_folder ] = $wp_plugins;
  268. wp_cache_set('plugins', $cache_plugins, 'plugins');
  269. return $wp_plugins;
  270. }
  271. /**
  272. * Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data.
  273. *
  274. * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins).
  275. *
  276. * @since 3.0.0
  277. * @return array Key is the mu-plugin file path and the value is an array of the mu-plugin data.
  278. */
  279. function get_mu_plugins() {
  280. $wp_plugins = array();
  281. // Files in wp-content/mu-plugins directory
  282. $plugin_files = array();
  283. if ( ! is_dir( WPMU_PLUGIN_DIR ) )
  284. return $wp_plugins;
  285. if ( $plugins_dir = @ opendir( WPMU_PLUGIN_DIR ) ) {
  286. while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
  287. if ( substr( $file, -4 ) == '.php' )
  288. $plugin_files[] = $file;
  289. }
  290. } else {
  291. return $wp_plugins;
  292. }
  293. @closedir( $plugins_dir );
  294. if ( empty($plugin_files) )
  295. return $wp_plugins;
  296. foreach ( $plugin_files as $plugin_file ) {
  297. if ( !is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) )
  298. continue;
  299. $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
  300. if ( empty ( $plugin_data['Name'] ) )
  301. $plugin_data['Name'] = $plugin_file;
  302. $wp_plugins[ $plugin_file ] = $plugin_data;
  303. }
  304. if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php') <= 30 ) // silence is golden
  305. unset( $wp_plugins['index.php'] );
  306. uasort( $wp_plugins, '_sort_uname_callback' );
  307. return $wp_plugins;
  308. }
  309. /**
  310. * Callback to sort array by a 'Name' key.
  311. *
  312. * @since 3.1.0
  313. * @access private
  314. */
  315. function _sort_uname_callback( $a, $b ) {
  316. return strnatcasecmp( $a['Name'], $b['Name'] );
  317. }
  318. /**
  319. * Check the wp-content directory and retrieve all drop-ins with any plugin data.
  320. *
  321. * @since 3.0.0
  322. * @return array Key is the file path and the value is an array of the plugin data.
  323. */
  324. function get_dropins() {
  325. $dropins = array();
  326. $plugin_files = array();
  327. $_dropins = _get_dropins();
  328. // These exist in the wp-content directory
  329. if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) {
  330. while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
  331. if ( isset( $_dropins[ $file ] ) )
  332. $plugin_files[] = $file;
  333. }
  334. } else {
  335. return $dropins;
  336. }
  337. @closedir( $plugins_dir );
  338. if ( empty($plugin_files) )
  339. return $dropins;
  340. foreach ( $plugin_files as $plugin_file ) {
  341. if ( !is_readable( WP_CONTENT_DIR . "/$plugin_file" ) )
  342. continue;
  343. $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
  344. if ( empty( $plugin_data['Name'] ) )
  345. $plugin_data['Name'] = $plugin_file;
  346. $dropins[ $plugin_file ] = $plugin_data;
  347. }
  348. uksort( $dropins, 'strnatcasecmp' );
  349. return $dropins;
  350. }
  351. /**
  352. * Returns drop-ins that WordPress uses.
  353. *
  354. * Includes Multisite drop-ins only when is_multisite()
  355. *
  356. * @since 3.0.0
  357. * @return array Key is file name. The value is an array, with the first value the
  358. * purpose of the drop-in and the second value the name of the constant that must be
  359. * true for the drop-in to be used, or true if no constant is required.
  360. */
  361. function _get_dropins() {
  362. $dropins = array(
  363. 'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE
  364. 'db.php' => array( __( 'Custom database class.' ), true ), // auto on load
  365. 'db-error.php' => array( __( 'Custom database error message.' ), true ), // auto on error
  366. 'install.php' => array( __( 'Custom install script.' ), true ), // auto on install
  367. 'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // auto on maintenance
  368. 'object-cache.php' => array( __( 'External object cache.' ), true ), // auto on load
  369. );
  370. if ( is_multisite() ) {
  371. $dropins['sunrise.php' ] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE
  372. $dropins['blog-deleted.php' ] = array( __( 'Custom site deleted message.' ), true ); // auto on deleted blog
  373. $dropins['blog-inactive.php' ] = array( __( 'Custom site inactive message.' ), true ); // auto on inactive blog
  374. $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // auto on archived or spammed blog
  375. }
  376. return $dropins;
  377. }
  378. /**
  379. * Check whether a plugin is active.
  380. *
  381. * Only plugins installed in the plugins/ folder can be active.
  382. *
  383. * Plugins in the mu-plugins/ folder can't be "activated," so this function will
  384. * return false for those plugins.
  385. *
  386. * @since 2.5.0
  387. *
  388. * @param string $plugin Base plugin path from plugins directory.
  389. * @return bool True, if in the active plugins list. False, not in the list.
  390. */
  391. function is_plugin_active( $plugin ) {
  392. return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
  393. }
  394. /**
  395. * Check whether the plugin is inactive.
  396. *
  397. * Reverse of is_plugin_active(). Used as a callback.
  398. *
  399. * @since 3.1.0
  400. * @see is_plugin_active()
  401. *
  402. * @param string $plugin Base plugin path from plugins directory.
  403. * @return bool True if inactive. False if active.
  404. */
  405. function is_plugin_inactive( $plugin ) {
  406. return ! is_plugin_active( $plugin );
  407. }
  408. /**
  409. * Check whether the plugin is active for the entire network.
  410. *
  411. * Only plugins installed in the plugins/ folder can be active.
  412. *
  413. * Plugins in the mu-plugins/ folder can't be "activated," so this function will
  414. * return false for those plugins.
  415. *
  416. * @since 3.0.0
  417. *
  418. * @param string $plugin Base plugin path from plugins directory.
  419. * @return bool True, if active for the network, otherwise false.
  420. */
  421. function is_plugin_active_for_network( $plugin ) {
  422. if ( !is_multisite() )
  423. return false;
  424. $plugins = get_site_option( 'active_sitewide_plugins');
  425. if ( isset($plugins[$plugin]) )
  426. return true;
  427. return false;
  428. }
  429. /**
  430. * Checks for "Network: true" in the plugin header to see if this should
  431. * be activated only as a network wide plugin. The plugin would also work
  432. * when Multisite is not enabled.
  433. *
  434. * Checks for "Site Wide Only: true" for backward compatibility.
  435. *
  436. * @since 3.0.0
  437. *
  438. * @param string $plugin Plugin to check
  439. * @return bool True if plugin is network only, false otherwise.
  440. */
  441. function is_network_only_plugin( $plugin ) {
  442. $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
  443. if ( $plugin_data )
  444. return $plugin_data['Network'];
  445. return false;
  446. }
  447. /**
  448. * Attempts activation of plugin in a "sandbox" and redirects on success.
  449. *
  450. * A plugin that is already activated will not attempt to be activated again.
  451. *
  452. * The way it works is by setting the redirection to the error before trying to
  453. * include the plugin file. If the plugin fails, then the redirection will not
  454. * be overwritten with the success message. Also, the options will not be
  455. * updated and the activation hook will not be called on plugin error.
  456. *
  457. * It should be noted that in no way the below code will actually prevent errors
  458. * within the file. The code should not be used elsewhere to replicate the
  459. * "sandbox", which uses redirection to work.
  460. * {@source 13 1}
  461. *
  462. * If any errors are found or text is outputted, then it will be captured to
  463. * ensure that the success redirection will update the error redirection.
  464. *
  465. * @since 2.5.0
  466. *
  467. * @param string $plugin Plugin path to main plugin file with plugin data.
  468. * @param string $redirect Optional. URL to redirect to.
  469. * @param bool $network_wide Optional. Whether to enable the plugin for all sites in the network
  470. * or just the current site. Multisite only. Default false.
  471. * @param bool $silent Optional. Whether to prevent calling activation hooks. Default false.
  472. * @return WP_Error|null WP_Error on invalid file or null on success.
  473. */
  474. function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
  475. $plugin = plugin_basename( trim( $plugin ) );
  476. if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) {
  477. $network_wide = true;
  478. $current = get_site_option( 'active_sitewide_plugins', array() );
  479. $_GET['networkwide'] = 1; // Back compat for plugins looking for this value.
  480. } else {
  481. $current = get_option( 'active_plugins', array() );
  482. }
  483. $valid = validate_plugin($plugin);
  484. if ( is_wp_error($valid) )
  485. return $valid;
  486. if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current ) ) ) {
  487. if ( !empty($redirect) )
  488. wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error
  489. ob_start();
  490. wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
  491. $_wp_plugin_file = $plugin;
  492. include_once( WP_PLUGIN_DIR . '/' . $plugin );
  493. $plugin = $_wp_plugin_file; // Avoid stomping of the $plugin variable in a plugin.
  494. if ( ! $silent ) {
  495. /**
  496. * Fires before a plugin is activated.
  497. *
  498. * If a plugin is silently activated (such as during an update),
  499. * this hook does not fire.
  500. *
  501. * @since 2.9.0
  502. *
  503. * @param string $plugin Plugin path to main plugin file with plugin data.
  504. * @param bool $network_wide Whether to enable the plugin for all sites in the network
  505. * or just the current site. Multisite only. Default is false.
  506. */
  507. do_action( 'activate_plugin', $plugin, $network_wide );
  508. /**
  509. * Fires as a specific plugin is being activated.
  510. *
  511. * This hook is the "activation" hook used internally by register_activation_hook().
  512. * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
  513. *
  514. * If a plugin is silently activated (such as during an update), this hook does not fire.
  515. *
  516. * @since 2.0.0
  517. *
  518. * @param bool $network_wide Whether to enable the plugin for all sites in the network
  519. * or just the current site. Multisite only. Default is false.
  520. */
  521. do_action( "activate_{$plugin}", $network_wide );
  522. }
  523. if ( $network_wide ) {
  524. $current = get_site_option( 'active_sitewide_plugins', array() );
  525. $current[$plugin] = time();
  526. update_site_option( 'active_sitewide_plugins', $current );
  527. } else {
  528. $current = get_option( 'active_plugins', array() );
  529. $current[] = $plugin;
  530. sort($current);
  531. update_option('active_plugins', $current);
  532. }
  533. if ( ! $silent ) {
  534. /**
  535. * Fires after a plugin has been activated.
  536. *
  537. * If a plugin is silently activated (such as during an update),
  538. * this hook does not fire.
  539. *
  540. * @since 2.9.0
  541. *
  542. * @param string $plugin Plugin path to main plugin file with plugin data.
  543. * @param bool $network_wide Whether to enable the plugin for all sites in the network
  544. * or just the current site. Multisite only. Default is false.
  545. */
  546. do_action( 'activated_plugin', $plugin, $network_wide );
  547. }
  548. if ( ob_get_length() > 0 ) {
  549. $output = ob_get_clean();
  550. return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
  551. }
  552. ob_end_clean();
  553. }
  554. return null;
  555. }
  556. /**
  557. * Deactivate a single plugin or multiple plugins.
  558. *
  559. * The deactivation hook is disabled by the plugin upgrader by using the $silent
  560. * parameter.
  561. *
  562. * @since 2.5.0
  563. *
  564. * @param string|array $plugins Single plugin or list of plugins to deactivate.
  565. * @param bool $silent Prevent calling deactivation hooks. Default is false.
  566. * @param mixed $network_wide Whether to deactivate the plugin for all sites in the network.
  567. * A value of null (the default) will deactivate plugins for both the site and the network.
  568. */
  569. function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) {
  570. if ( is_multisite() )
  571. $network_current = get_site_option( 'active_sitewide_plugins', array() );
  572. $current = get_option( 'active_plugins', array() );
  573. $do_blog = $do_network = false;
  574. foreach ( (array) $plugins as $plugin ) {
  575. $plugin = plugin_basename( trim( $plugin ) );
  576. if ( ! is_plugin_active($plugin) )
  577. continue;
  578. $network_deactivating = false !== $network_wide && is_plugin_active_for_network( $plugin );
  579. if ( ! $silent ) {
  580. /**
  581. * Fires before a plugin is deactivated.
  582. *
  583. * If a plugin is silently deactivated (such as during an update),
  584. * this hook does not fire.
  585. *
  586. * @since 2.9.0
  587. *
  588. * @param string $plugin Plugin path to main plugin file with plugin data.
  589. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
  590. * or just the current site. Multisite only. Default is false.
  591. */
  592. do_action( 'deactivate_plugin', $plugin, $network_deactivating );
  593. }
  594. if ( false !== $network_wide ) {
  595. if ( is_plugin_active_for_network( $plugin ) ) {
  596. $do_network = true;
  597. unset( $network_current[ $plugin ] );
  598. } elseif ( $network_wide ) {
  599. continue;
  600. }
  601. }
  602. if ( true !== $network_wide ) {
  603. $key = array_search( $plugin, $current );
  604. if ( false !== $key ) {
  605. $do_blog = true;
  606. unset( $current[ $key ] );
  607. }
  608. }
  609. if ( ! $silent ) {
  610. /**
  611. * Fires as a specific plugin is being deactivated.
  612. *
  613. * This hook is the "deactivation" hook used internally by register_deactivation_hook().
  614. * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
  615. *
  616. * If a plugin is silently deactivated (such as during an update), this hook does not fire.
  617. *
  618. * @since 2.0.0
  619. *
  620. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
  621. * or just the current site. Multisite only. Default is false.
  622. */
  623. do_action( "deactivate_{$plugin}", $network_deactivating );
  624. /**
  625. * Fires after a plugin is deactivated.
  626. *
  627. * If a plugin is silently deactivated (such as during an update),
  628. * this hook does not fire.
  629. *
  630. * @since 2.9.0
  631. *
  632. * @param string $plugin Plugin basename.
  633. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
  634. * or just the current site. Multisite only. Default false.
  635. */
  636. do_action( 'deactivated_plugin', $plugin, $network_deactivating );
  637. }
  638. }
  639. if ( $do_blog )
  640. update_option('active_plugins', $current);
  641. if ( $do_network )
  642. update_site_option( 'active_sitewide_plugins', $network_current );
  643. }
  644. /**
  645. * Activate multiple plugins.
  646. *
  647. * When WP_Error is returned, it does not mean that one of the plugins had
  648. * errors. It means that one or more of the plugins file path was invalid.
  649. *
  650. * The execution will be halted as soon as one of the plugins has an error.
  651. *
  652. * @since 2.6.0
  653. *
  654. * @param string|array $plugins Single plugin or list of plugins to activate.
  655. * @param string $redirect Redirect to page after successful activation.
  656. * @param bool $network_wide Whether to enable the plugin for all sites in the network.
  657. * @param bool $silent Prevent calling activation hooks. Default is false.
  658. * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
  659. */
  660. function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
  661. if ( !is_array($plugins) )
  662. $plugins = array($plugins);
  663. $errors = array();
  664. foreach ( $plugins as $plugin ) {
  665. if ( !empty($redirect) )
  666. $redirect = add_query_arg('plugin', $plugin, $redirect);
  667. $result = activate_plugin($plugin, $redirect, $network_wide, $silent);
  668. if ( is_wp_error($result) )
  669. $errors[$plugin] = $result;
  670. }
  671. if ( !empty($errors) )
  672. return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
  673. return true;
  674. }
  675. /**
  676. * Remove directory and files of a plugin for a list of plugins.
  677. *
  678. * @since 2.6.0
  679. *
  680. * @global WP_Filesystem_Base $wp_filesystem
  681. *
  682. * @param array $plugins List of plugins to delete.
  683. * @param string $deprecated Deprecated.
  684. * @return bool|null|WP_Error True on success, false is $plugins is empty, WP_Error on failure.
  685. * Null if filesystem credentials are required to proceed.
  686. */
  687. function delete_plugins( $plugins, $deprecated = '' ) {
  688. global $wp_filesystem;
  689. if ( empty($plugins) )
  690. return false;
  691. $checked = array();
  692. foreach ( $plugins as $plugin )
  693. $checked[] = 'checked[]=' . $plugin;
  694. $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-plugins');
  695. ob_start();
  696. $credentials = request_filesystem_credentials( $url );
  697. $data = ob_get_clean();
  698. if ( false === $credentials ) {
  699. if ( ! empty($data) ){
  700. include_once( ABSPATH . 'wp-admin/admin-header.php');
  701. echo $data;
  702. include( ABSPATH . 'wp-admin/admin-footer.php');
  703. exit;
  704. }
  705. return;
  706. }
  707. if ( ! WP_Filesystem( $credentials ) ) {
  708. ob_start();
  709. request_filesystem_credentials( $url, '', true ); // Failed to connect, Error and request again.
  710. $data = ob_get_clean();
  711. if ( ! empty($data) ){
  712. include_once( ABSPATH . 'wp-admin/admin-header.php');
  713. echo $data;
  714. include( ABSPATH . 'wp-admin/admin-footer.php');
  715. exit;
  716. }
  717. return;
  718. }
  719. if ( ! is_object($wp_filesystem) )
  720. return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
  721. if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
  722. return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
  723. // Get the base plugin folder.
  724. $plugins_dir = $wp_filesystem->wp_plugins_dir();
  725. if ( empty( $plugins_dir ) ) {
  726. return new WP_Error( 'fs_no_plugins_dir', __( 'Unable to locate WordPress plugin directory.' ) );
  727. }
  728. $plugins_dir = trailingslashit( $plugins_dir );
  729. $plugin_translations = wp_get_installed_translations( 'plugins' );
  730. $errors = array();
  731. foreach ( $plugins as $plugin_file ) {
  732. // Run Uninstall hook.
  733. if ( is_uninstallable_plugin( $plugin_file ) ) {
  734. uninstall_plugin($plugin_file);
  735. }
  736. /**
  737. * Fires immediately before a plugin deletion attempt.
  738. *
  739. * @since 4.4.0
  740. *
  741. * @param string $plugin_file Plugin file name.
  742. */
  743. do_action( 'delete_plugin', $plugin_file );
  744. $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin_file ) );
  745. // If plugin is in its own directory, recursively delete the directory.
  746. if ( strpos( $plugin_file, '/' ) && $this_plugin_dir != $plugins_dir ) { //base check on if plugin includes directory separator AND that it's not the root plugin folder
  747. $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
  748. } else {
  749. $deleted = $wp_filesystem->delete( $plugins_dir . $plugin_file );
  750. }
  751. /**
  752. * Fires immediately after a plugin deletion attempt.
  753. *
  754. * @since 4.4.0
  755. *
  756. * @param string $plugin_file Plugin file name.
  757. * @param bool $deleted Whether the plugin deletion was successful.
  758. */
  759. do_action( 'deleted_plugin', $plugin_file, $deleted );
  760. if ( ! $deleted ) {
  761. $errors[] = $plugin_file;
  762. continue;
  763. }
  764. // Remove language files, silently.
  765. $plugin_slug = dirname( $plugin_file );
  766. if ( '.' !== $plugin_slug && ! empty( $plugin_translations[ $plugin_slug ] ) ) {
  767. $translations = $plugin_translations[ $plugin_slug ];
  768. foreach ( $translations as $translation => $data ) {
  769. $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.po' );
  770. $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.mo' );
  771. }
  772. }
  773. }
  774. // Remove deleted plugins from the plugin updates list.
  775. if ( $current = get_site_transient('update_plugins') ) {
  776. // Don't remove the plugins that weren't deleted.
  777. $deleted = array_diff( $plugins, $errors );
  778. foreach ( $deleted as $plugin_file ) {
  779. unset( $current->response[ $plugin_file ] );
  780. }
  781. set_site_transient( 'update_plugins', $current );
  782. }
  783. if ( ! empty($errors) )
  784. return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s.'), implode(', ', $errors)) );
  785. return true;
  786. }
  787. /**
  788. * Validate active plugins
  789. *
  790. * Validate all active plugins, deactivates invalid and
  791. * returns an array of deactivated ones.
  792. *
  793. * @since 2.5.0
  794. * @return array invalid plugins, plugin as key, error as value
  795. */
  796. function validate_active_plugins() {
  797. $plugins = get_option( 'active_plugins', array() );
  798. // Validate vartype: array.
  799. if ( ! is_array( $plugins ) ) {
  800. update_option( 'active_plugins', array() );
  801. $plugins = array();
  802. }
  803. if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
  804. $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
  805. $plugins = array_merge( $plugins, array_keys( $network_plugins ) );
  806. }
  807. if ( empty( $plugins ) )
  808. return array();
  809. $invalid = array();
  810. // Invalid plugins get deactivated.
  811. foreach ( $plugins as $plugin ) {
  812. $result = validate_plugin( $plugin );
  813. if ( is_wp_error( $result ) ) {
  814. $invalid[$plugin] = $result;
  815. deactivate_plugins( $plugin, true );
  816. }
  817. }
  818. return $invalid;
  819. }
  820. /**
  821. * Validate the plugin path.
  822. *
  823. * Checks that the file exists and is a valid file. See validate_file().
  824. *
  825. * @since 2.5.0
  826. *
  827. * @param string $plugin Plugin Path.
  828. * @return WP_Error|int 0 on success, WP_Error on failure.
  829. */
  830. function validate_plugin($plugin) {
  831. if ( validate_file($plugin) )
  832. return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
  833. if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
  834. return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
  835. $installed_plugins = get_plugins();
  836. if ( ! isset($installed_plugins[$plugin]) )
  837. return new WP_Error('no_plugin_header', __('The plugin does not have a valid header.'));
  838. return 0;
  839. }
  840. /**
  841. * Whether the plugin can be uninstalled.
  842. *
  843. * @since 2.7.0
  844. *
  845. * @param string $plugin Plugin path to check.
  846. * @return bool Whether plugin can be uninstalled.
  847. */
  848. function is_uninstallable_plugin($plugin) {
  849. $file = plugin_basename($plugin);
  850. $uninstallable_plugins = (array) get_option('uninstall_plugins');
  851. if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
  852. return true;
  853. return false;
  854. }
  855. /**
  856. * Uninstall a single plugin.
  857. *
  858. * Calls the uninstall hook, if it is available.
  859. *
  860. * @since 2.7.0
  861. *
  862. * @param string $plugin Relative plugin path from Plugin Directory.
  863. * @return true True if a plugin's uninstall.php file has been found and included.
  864. */
  865. function uninstall_plugin($plugin) {
  866. $file = plugin_basename($plugin);
  867. $uninstallable_plugins = (array) get_option('uninstall_plugins');
  868. /**
  869. * Fires in uninstall_plugin() immediately before the plugin is uninstalled.
  870. *
  871. * @since 4.5.0
  872. *
  873. * @param string $plugin Relative plugin path from plugin directory.
  874. * @param array $uninstallable_plugins Uninstallable plugins.
  875. */
  876. do_action( 'pre_uninstall_plugin', $plugin, $uninstallable_plugins );
  877. if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
  878. if ( isset( $uninstallable_plugins[$file] ) ) {
  879. unset($uninstallable_plugins[$file]);
  880. update_option('uninstall_plugins', $uninstallable_plugins);
  881. }
  882. unset($uninstallable_plugins);
  883. define('WP_UNINSTALL_PLUGIN', $file);
  884. wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
  885. include( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' );
  886. return true;
  887. }
  888. if ( isset( $uninstallable_plugins[$file] ) ) {
  889. $callable = $uninstallable_plugins[$file];
  890. unset($uninstallable_plugins[$file]);
  891. update_option('uninstall_plugins', $uninstallable_plugins);
  892. unset($uninstallable_plugins);
  893. wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
  894. include( WP_PLUGIN_DIR . '/' . $file );
  895. add_action( 'uninstall_' . $file, $callable );
  896. /**
  897. * Fires in uninstall_plugin() once the plugin has been uninstalled.
  898. *
  899. * The action concatenates the 'uninstall_' prefix with the basename of the
  900. * plugin passed to uninstall_plugin() to create a dynamically-named action.
  901. *
  902. * @since 2.7.0
  903. */
  904. do_action( 'uninstall_' . $file );
  905. }
  906. }
  907. //
  908. // Menu
  909. //
  910. /**
  911. * Add a top-level menu page.
  912. *
  913. * This function takes a capability which will be used to determine whether
  914. * or not a page is included in the menu.
  915. *
  916. * The function which is hooked in to handle the output of the page must check
  917. * that the user has the required capability as well.
  918. *
  919. * @global array $menu
  920. * @global array $admin_page_hooks
  921. * @global array $_registered_pages
  922. * @global array $_parent_pages
  923. *
  924. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  925. * @param string $menu_title The text to be used for the menu.
  926. * @param string $capability The capability required for this menu to be displayed to the user.
  927. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  928. * @param callable $function The function to be called to output the content for this page.
  929. * @param string $icon_url The URL to the icon to be used for this menu.
  930. * * Pass a base64-encoded SVG using a data URI, which will be colored to match
  931. * the color scheme. This should begin with 'data:image/svg+xml;base64,'.
  932. * * Pass the name of a Dashicons helper class to use a font icon,
  933. * e.g. 'dashicons-chart-pie'.
  934. * * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.
  935. * @param int $position The position in the menu order this one should appear.
  936. * @return string The resulting page's hook_suffix.
  937. */
  938. function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) {
  939. global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
  940. $menu_slug = plugin_basename( $menu_slug );
  941. $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title );
  942. $hookname = get_plugin_page_hookname( $menu_slug, '' );
  943. if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) )
  944. add_action( $hookname, $function );
  945. if ( empty($icon_url) ) {
  946. $icon_url = 'dashicons-admin-generic';
  947. $icon_class = 'menu-icon-generic ';
  948. } else {
  949. $icon_url = set_url_scheme( $icon_url );
  950. $icon_class = '';
  951. }
  952. $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url );
  953. if ( null === $position ) {
  954. $menu[] = $new_menu;
  955. } elseif ( isset( $menu[ "$position" ] ) ) {
  956. $position = $position + substr( base_convert( md5( $menu_slug . $menu_title ), 16, 10 ) , -5 ) * 0.00001;
  957. $menu[ "$position" ] = $new_menu;
  958. } else {
  959. $menu[ $position ] = $new_menu;
  960. }
  961. $_registered_pages[$hookname] = true;
  962. // No parent as top level
  963. $_parent_pages[$menu_slug] = false;
  964. return $hookname;
  965. }
  966. /**
  967. * Add a submenu page.
  968. *
  969. * This function takes a capability which will be used to determine whether
  970. * or not a page is included in the menu.
  971. *
  972. * The function which is hooked in to handle the output of the page must check
  973. * that the user has the required capability as well.
  974. *
  975. * @global array $submenu
  976. * @global array $menu
  977. * @global array $_wp_real_parent_file
  978. * @global bool $_wp_submenu_nopriv
  979. * @global array $_registered_pages
  980. * @global array $_parent_pages
  981. *
  982. * @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page).
  983. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  984. * @param string $menu_title The text to be used for the menu.
  985. * @param string $capability The capability required for this menu to be displayed to the user.
  986. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  987. * @param callable $function The function to be called to output the content for this page.
  988. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  989. */
  990. function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  991. global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv,
  992. $_registered_pages, $_parent_pages;
  993. $menu_slug = plugin_basename( $menu_slug );
  994. $parent_slug = plugin_basename( $parent_slug);
  995. if ( isset( $_wp_real_parent_file[$parent_slug] ) )
  996. $parent_slug = $_wp_real_parent_file[$parent_slug];
  997. if ( !current_user_can( $capability ) ) {
  998. $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true;
  999. return false;
  1000. }
  1001. /*
  1002. * If the parent doesn't already have a submenu, add a link to the parent
  1003. * as the first item in the submenu. If the submenu file is the same as the
  1004. * parent file someone is trying to link back to the parent manually. In
  1005. * this case, don't automatically add a link back to avoid duplication.
  1006. */
  1007. if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) {
  1008. foreach ( (array)$menu as $parent_menu ) {
  1009. if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) )
  1010. $submenu[$parent_slug][] = array_slice( $parent_menu, 0, 4 );
  1011. }
  1012. }
  1013. $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title );
  1014. $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);
  1015. if (!empty ( $function ) && !empty ( $hookname ))
  1016. add_action( $hookname, $function );
  1017. $_registered_pages[$hookname] = true;
  1018. /*
  1019. * Backward-compatibility for plugins using add_management page.
  1020. * See wp-admin/admin.php for redirect from edit.php to tools.php
  1021. */
  1022. if ( 'tools.php' == $parent_slug )
  1023. $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true;
  1024. // No parent as top level.
  1025. $_parent_pages[$menu_slug] = $parent_slug;
  1026. return $hookname;
  1027. }
  1028. /**
  1029. * Add submenu page to the Tools main menu.
  1030. *
  1031. * This function takes a capability which will be used to determine whether
  1032. * or not a page is included in the menu.
  1033. *
  1034. * The function which is hooked in to handle the output of the page must check
  1035. * that the user has the required capability as well.
  1036. *
  1037. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1038. * @param string $menu_title The text to be used for the menu.
  1039. * @param string $capability The capability required for this menu to be displayed to the user.
  1040. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1041. * @param callable $function The function to be called to output the content for this page.
  1042. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1043. */
  1044. function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1045. return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1046. }
  1047. /**
  1048. * Add submenu page to the Settings main menu.
  1049. *
  1050. * This function takes a capability which will be used to determine whether
  1051. * or not a page is included in the menu.
  1052. *
  1053. * The function which is hooked in to handle the output of the page must check
  1054. * that the user has the required capability as well.
  1055. *
  1056. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1057. * @param string $menu_title The text to be used for the menu.
  1058. * @param string $capability The capability required for this menu to be displayed to the user.
  1059. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1060. * @param callable $function The function to be called to output the content for this page.
  1061. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1062. */
  1063. function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1064. return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1065. }
  1066. /**
  1067. * Add submenu page to the Appearance main menu.
  1068. *
  1069. * This function takes a capability which will be used to determine whether
  1070. * or not a page is included in the menu.
  1071. *
  1072. * The function which is hooked in to handle the output of the page must check
  1073. * that the user has the required capability as well.
  1074. *
  1075. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1076. * @param string $menu_title The text to be used for the menu.
  1077. * @param string $capability The capability required for this menu to be displayed to the user.
  1078. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1079. * @param callable $function The function to be called to output the content for this page.
  1080. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1081. */
  1082. function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1083. return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1084. }
  1085. /**
  1086. * Add submenu page to the Plugins main menu.
  1087. *
  1088. * This function takes a capability which will be used to determine whether
  1089. * or not a page is included in the menu.
  1090. *
  1091. * The function which is hooked in to handle the output of the page must check
  1092. * that the user has the required capability as well.
  1093. *
  1094. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1095. * @param string $menu_title The text to be used for the menu.
  1096. * @param string $capability The capability required for this menu to be displayed to the user.
  1097. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1098. * @param callable $function The function to be called to output the content for this page.
  1099. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1100. */
  1101. function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1102. return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1103. }
  1104. /**
  1105. * Add submenu page to the Users/Profile main menu.
  1106. *
  1107. * This function takes a capability which will be used to determine whether
  1108. * or not a page is included in the menu.
  1109. *
  1110. * The function which is hooked in to handle the output of the page must check
  1111. * that the user has the required capability as well.
  1112. *
  1113. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1114. * @param string $menu_title The text to be used for the menu.
  1115. * @param string $capability The capability required for this menu to be displayed to the user.
  1116. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1117. * @param callable $function The function to be called to output the content for this page.
  1118. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1119. */
  1120. function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1121. if ( current_user_can('edit_users') )
  1122. $parent = 'users.php';
  1123. else
  1124. $parent = 'profile.php';
  1125. return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
  1126. }
  1127. /**
  1128. * Add submenu page to the Dashboard main menu.
  1129. *
  1130. * This function takes a capability which will be used to determine whether
  1131. * or not a page is included in the menu.
  1132. *
  1133. * The function which is hooked in to handle the output of the page must check
  1134. * that the user has the required capability as well.
  1135. *
  1136. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1137. * @param string $menu_title The text to be used for the menu.
  1138. * @param string $capability The capability required for this menu to be displayed to the user.
  1139. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1140. * @param callable $function The function to be called to output the content for this page.
  1141. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1142. */
  1143. function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1144. return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1145. }
  1146. /**
  1147. * Add submenu page to the Posts main menu.
  1148. *
  1149. * This function takes a capability which will be used to determine whether
  1150. * or not a page is included in the menu.
  1151. *
  1152. * The function which is hooked in to handle the output of the page must check
  1153. * that the user has the required capability as well.
  1154. *
  1155. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1156. * @param string $menu_title The text to be used for the menu.
  1157. * @param string $capability The capability required for this menu to be displayed to the user.
  1158. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1159. * @param callable $function The function to be called to output the content for this page.
  1160. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1161. */
  1162. function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1163. return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1164. }
  1165. /**
  1166. * Add submenu page to the Media main menu.
  1167. *
  1168. * This function takes a capability which will be used to determine whether
  1169. * or not a page is included in the menu.
  1170. *
  1171. * The function which is hooked in to handle the output of the page must check
  1172. * that the user has the required capability as well.
  1173. *
  1174. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1175. * @param string $menu_title The text to be used for the menu.
  1176. * @param string $capability The capability required for this menu to be displayed to the user.
  1177. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1178. * @param callable $function The function to be called to output the content for this page.
  1179. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1180. */
  1181. function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1182. return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1183. }
  1184. /**
  1185. * Add submenu page to the Links main menu.
  1186. *
  1187. * This function takes a capability which will be used to determine whether
  1188. * or not a page is included in the menu.
  1189. *
  1190. * The function which is hooked in to handle the output of the page must check
  1191. * that the user has the required capability as well.
  1192. *
  1193. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1194. * @param string $menu_title The text to be used for the menu.
  1195. * @param string $capability The capability required for this menu to be displayed to the user.
  1196. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1197. * @param callable $function The function to be called to output the content for this page.
  1198. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1199. */
  1200. function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1201. return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1202. }
  1203. /**
  1204. * Add submenu page to the Pages main menu.
  1205. *
  1206. * This function takes a capability which will be used to determine whether
  1207. * or not a page is included in the menu.
  1208. *
  1209. * The function which is hooked in to handle the output of the page must check
  1210. * that the user has the required capability as well.
  1211. *
  1212. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1213. * @param string $menu_title The text to be used for the menu.
  1214. * @param string $capability The capability required for this menu to be displayed to the user.
  1215. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1216. * @param callable $function The function to be called to output the content for this page.
  1217. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1218. */
  1219. function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1220. return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function );
  1221. }
  1222. /**
  1223. * Add submenu page to the Comments main menu.
  1224. *
  1225. * This function takes a capability which will be used to determine whether
  1226. * or not a page is included in the menu.
  1227. *
  1228. * The function which is hooked in to handle the output of the page must check
  1229. * that the user has the required capability as well.
  1230. *
  1231. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1232. * @param string $menu_title The text to be used for the menu.
  1233. * @param string $capability The capability required for this menu to be displayed to the user.
  1234. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1235. * @param callable $function The function to be called to output the content for this page.
  1236. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1237. */
  1238. function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1239. return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1240. }
  1241. /**
  1242. * Remove a top-level admin menu.
  1243. *
  1244. * @since 3.1.0
  1245. *
  1246. * @global array $menu
  1247. *
  1248. * @param string $menu_slug The slug of the menu.
  1249. * @return array|bool The removed menu on success, false if not found.
  1250. */
  1251. function remove_menu_page( $menu_slug ) {
  1252. global $menu;
  1253. foreach ( $menu as $i => $item ) {
  1254. if ( $menu_slug == $item[2] ) {
  1255. unset( $menu[$i] );
  1256. return $item;
  1257. }
  1258. }
  1259. return false;
  1260. }
  1261. /**
  1262. * Remove an admin submenu.
  1263. *
  1264. * @since 3.1.0
  1265. *
  1266. * @global array $submenu
  1267. *
  1268. * @param string $menu_slug The slug for the parent menu.
  1269. * @param string $submenu_slug The slug of the submenu.
  1270. * @return array|bool The removed submenu on success, false if not found.
  1271. */
  1272. function remove_submenu_page( $menu_slug, $submenu_slug ) {
  1273. global $submenu;
  1274. if ( !isset( $submenu[$menu_slug] ) )
  1275. return false;
  1276. foreach ( $submenu[$menu_slug] as $i => $item ) {
  1277. if ( $submenu_slug == $item[2] ) {
  1278. unset( $submenu[$menu_slug][$i] );
  1279. return $item;
  1280. }
  1281. }
  1282. return false;
  1283. }
  1284. /**
  1285. * Get the url to access a particular menu page based on the slug it was registered with.
  1286. *
  1287. * If the slug hasn't been registered properly no url will be returned
  1288. *
  1289. * @since 3.0.0
  1290. *
  1291. * @global array $_parent_pages
  1292. *
  1293. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1294. * @param bool $echo Whether or not to echo the url - default is true
  1295. * @return string the url
  1296. */
  1297. function menu_page_url($menu_slug, $echo = true) {
  1298. global $_parent_pages;
  1299. if ( isset( $_parent_pages[$menu_slug] ) ) {
  1300. $parent_slug = $_parent_pages[$menu_slug];
  1301. if ( $parent_slug && ! isset( $_parent_pages[$parent_slug] ) ) {
  1302. $url = admin_url( add_query_arg( 'page', $menu_slug, $parent_slug ) );
  1303. } else {
  1304. $url = admin_url( 'admin.php?page=' . $menu_slug );
  1305. }
  1306. } else {
  1307. $url = '';
  1308. }
  1309. $url = esc_url($url);
  1310. if ( $echo )
  1311. echo $url;
  1312. return $url;
  1313. }
  1314. //
  1315. // Pluggable Menu Support -- Private
  1316. //
  1317. /**
  1318. *
  1319. * @global string $parent_file
  1320. * @global array $menu
  1321. * @global array $submenu
  1322. * @global string $pagenow
  1323. * @global string $typenow
  1324. * @global string $plugin_page
  1325. * @global array $_wp_real_parent_file
  1326. * @global array $_wp_menu_nopriv
  1327. * @global array $_wp_submenu_nopriv
  1328. */
  1329. function get_admin_page_parent( $parent = '' ) {
  1330. global $parent_file, $menu, $submenu, $pagenow, $typenow,
  1331. $plugin_page, $_wp_real_parent_file, $_wp_menu_nopriv, $_wp_submenu_nopriv;
  1332. if ( !empty ( $parent ) && 'admin.php' != $parent ) {
  1333. if ( isset( $_wp_real_parent_file[$parent] ) )
  1334. $parent = $_wp_real_parent_file[$parent];
  1335. return $parent;
  1336. }
  1337. if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
  1338. foreach ( (array)$menu as $parent_menu ) {
  1339. if ( $parent_menu[2] == $plugin_page ) {
  1340. $parent_file = $plugin_page;
  1341. if ( isset( $_wp_real_parent_file[$parent_file] ) )
  1342. $parent_file = $_wp_real_parent_file[$parent_file];
  1343. return $parent_file;
  1344. }
  1345. }
  1346. if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
  1347. $parent_file = $plugin_page;
  1348. if ( isset( $_wp_real_parent_file[$parent_file] ) )
  1349. $parent_file = $_wp_real_parent_file[$parent_file];
  1350. return $parent_file;
  1351. }
  1352. }
  1353. if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
  1354. $parent_file = $pagenow;
  1355. if ( isset( $_wp_real_parent_file[$parent_file] ) )
  1356. $parent_file = $_wp_real_parent_file[$parent_file];
  1357. return $parent_file;
  1358. }
  1359. foreach (array_keys( (array)$submenu ) as $parent) {
  1360. foreach ( $submenu[$parent] as $submenu_array ) {
  1361. if ( isset( $_wp_real_parent_file[$parent] ) )
  1362. $parent = $_wp_real_parent_file[$parent];
  1363. if ( !empty($typenow) && ($submenu_array[2] == "$pagenow?post_type=$typenow") ) {
  1364. $parent_file = $parent;
  1365. return $parent;
  1366. } elseif ( $submenu_array[2] == $pagenow && empty($typenow) && ( empty($parent_file) || false === strpos($parent_file, '?') ) ) {
  1367. $parent_file = $parent;
  1368. return $parent;
  1369. } elseif ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
  1370. $parent_file = $parent;
  1371. return $parent;
  1372. }
  1373. }
  1374. }
  1375. if ( empty($parent_file) )
  1376. $parent_file = '';
  1377. return '';
  1378. }
  1379. /**
  1380. *
  1381. * @global string $title
  1382. * @global array $menu
  1383. * @global array $submenu
  1384. * @global string $pagenow
  1385. * @global string $plugin_page
  1386. * @global string $typenow
  1387. */
  1388. function get_admin_page_title() {
  1389. global $title, $menu, $submenu, $pagenow, $plugin_page, $typenow;
  1390. if ( ! empty ( $title ) )
  1391. return $title;
  1392. $hook = get_plugin_page_hook( $plugin_page, $pagenow );
  1393. $parent = $parent1 = get_admin_page_parent();
  1394. if ( empty ( $parent) ) {
  1395. foreach ( (array)$menu as $menu_array ) {
  1396. if ( isset( $menu_array[3] ) ) {
  1397. if ( $menu_array[2] == $pagenow ) {
  1398. $title = $menu_array[3];
  1399. return $menu_array[3];
  1400. } elseif ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
  1401. $title = $menu_array[3];
  1402. return $menu_array[3];
  1403. }
  1404. } else {
  1405. $title = $menu_array[0];
  1406. return $title;
  1407. }
  1408. }
  1409. } else {
  1410. foreach ( array_keys( $submenu ) as $parent ) {
  1411. foreach ( $submenu[$parent] as $submenu_array ) {
  1412. if ( isset( $plugin_page ) &&
  1413. ( $plugin_page == $submenu_array[2] ) &&
  1414. (
  1415. ( $parent == $pagenow ) ||
  1416. ( $parent == $plugin_page ) ||
  1417. ( $plugin_page == $hook ) ||
  1418. ( $pagenow == 'admin.php' && $parent1 != $submenu_array[2] ) ||
  1419. ( !empty($typenow) && $parent == $pagenow . '?post_type=' . $typenow)
  1420. )
  1421. ) {
  1422. $title = $submenu_array[3];
  1423. return $submenu_array[3];
  1424. }
  1425. if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
  1426. continue;
  1427. if ( isset( $submenu_array[3] ) ) {
  1428. $title = $submenu_array[3];
  1429. return $submenu_array[3];
  1430. } else {
  1431. $title = $submenu_array[0];
  1432. return $title;
  1433. }
  1434. }
  1435. }
  1436. if ( empty ( $title ) ) {
  1437. foreach ( $menu as $menu_array ) {
  1438. if ( isset( $plugin_page ) &&
  1439. ( $plugin_page == $menu_array[2] ) &&
  1440. ( $pagenow == 'admin.php' ) &&
  1441. ( $parent1 == $menu_array[2] ) )
  1442. {
  1443. $title = $menu_array[3];
  1444. return $menu_array[3];
  1445. }
  1446. }
  1447. }
  1448. }
  1449. return $title;
  1450. }
  1451. /**
  1452. * @since 2.3.0
  1453. *
  1454. * @param string $plugin_page
  1455. * @param string $parent_page
  1456. * @return string|null
  1457. */
  1458. function get_plugin_page_hook( $plugin_page, $parent_page ) {
  1459. $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
  1460. if ( has_action($hook) )
  1461. return $hook;
  1462. else
  1463. return null;
  1464. }
  1465. /**
  1466. *
  1467. * @global array $admin_page_hooks
  1468. * @param string $plugin_page
  1469. * @param string $parent_page
  1470. */
  1471. function get_plugin_page_hookname( $plugin_page, $parent_page ) {
  1472. global $admin_page_hooks;
  1473. $parent = get_admin_page_parent( $parent_page );
  1474. $page_type = 'admin';
  1475. if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
  1476. if ( isset( $admin_page_hooks[$plugin_page] ) ) {
  1477. $page_type = 'toplevel';
  1478. } elseif ( isset( $admin_page_hooks[$parent] )) {
  1479. $page_type = $admin_page_hooks[$parent];
  1480. }
  1481. } elseif ( isset( $admin_page_hooks[$parent] ) ) {
  1482. $page_type = $admin_page_hooks[$parent];
  1483. }
  1484. $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
  1485. return $page_type . '_page_' . $plugin_name;
  1486. }
  1487. /**
  1488. *
  1489. * @global string $pagenow
  1490. * @global array $menu
  1491. * @global array $submenu
  1492. * @global array $_wp_menu_nopriv
  1493. * @global array $_wp_submenu_nopriv
  1494. * @global string $plugin_page
  1495. * @global array $_registered_pages
  1496. */
  1497. function user_can_access_admin_page() {
  1498. global $pagenow, $menu, $submenu, $_wp_menu_nopriv, $_wp_submenu_nopriv,
  1499. $plugin_page, $_registered_pages;
  1500. $parent = get_admin_page_parent();
  1501. if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
  1502. return false;
  1503. if ( isset( $plugin_page ) ) {
  1504. if ( isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
  1505. return false;
  1506. $hookname = get_plugin_page_hookname($plugin_page, $parent);
  1507. if ( !isset($_registered_pages[$hookname]) )
  1508. return false;
  1509. }
  1510. if ( empty( $parent) ) {
  1511. if ( isset( $_wp_menu_nopriv[$pagenow] ) )
  1512. return false;
  1513. if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
  1514. return false;
  1515. if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
  1516. return false;
  1517. if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
  1518. return false;
  1519. foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
  1520. if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
  1521. return false;
  1522. if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
  1523. return false;
  1524. }
  1525. return true;
  1526. }
  1527. if ( isset( $plugin_page ) && ( $plugin_page == $parent ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
  1528. return false;
  1529. if ( isset( $submenu[$parent] ) ) {
  1530. foreach ( $submenu[$parent] as $submenu_array ) {
  1531. if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
  1532. if ( current_user_can( $submenu_array[1] ))
  1533. return true;
  1534. else
  1535. return false;
  1536. } elseif ( $submenu_array[2] == $pagenow ) {
  1537. if ( current_user_can( $submenu_array[1] ))
  1538. return true;
  1539. else
  1540. return false;
  1541. }
  1542. }
  1543. }
  1544. foreach ( $menu as $menu_array ) {
  1545. if ( $menu_array[2] == $parent) {
  1546. if ( current_user_can( $menu_array[1] ))
  1547. return true;
  1548. else
  1549. return false;
  1550. }
  1551. }
  1552. return true;
  1553. }
  1554. /* Whitelist functions */
  1555. /**
  1556. * Refreshes the value of the options whitelist available via the 'whitelist_options' hook.
  1557. *
  1558. * See the {@see 'whitelist_options'} filter.
  1559. *
  1560. * @since 2.7.0
  1561. *
  1562. * @global array $new_whitelist_options
  1563. *
  1564. * @param array $options
  1565. * @return array
  1566. */
  1567. function option_update_filter( $options ) {
  1568. global $new_whitelist_options;
  1569. if ( is_array( $new_whitelist_options ) )
  1570. $options = add_option_whitelist( $new_whitelist_options, $options );
  1571. return $options;
  1572. }
  1573. /**
  1574. * Adds an array of options to the options whitelist.
  1575. *
  1576. * @since 2.7.0
  1577. *
  1578. * @global array $whitelist_options
  1579. *
  1580. * @param array $new_options
  1581. * @param string|array $options
  1582. * @return array
  1583. */
  1584. function add_option_whitelist( $new_options, $options = '' ) {
  1585. if ( $options == '' )
  1586. global $whitelist_options;
  1587. else
  1588. $whitelist_options = $options;
  1589. foreach ( $new_options as $page => $keys ) {
  1590. foreach ( $keys as $key ) {
  1591. if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
  1592. $whitelist_options[ $page ] = array();
  1593. $whitelist_options[ $page ][] = $key;
  1594. } else {
  1595. $pos = array_search( $key, $whitelist_options[ $page ] );
  1596. if ( $pos === false )
  1597. $whitelist_options[ $page ][] = $key;
  1598. }
  1599. }
  1600. }
  1601. return $whitelist_options;
  1602. }
  1603. /**
  1604. * Removes a list of options from the options whitelist.
  1605. *
  1606. * @since 2.7.0
  1607. *
  1608. * @global array $whitelist_options
  1609. *
  1610. * @param array $del_options
  1611. * @param string|array $options
  1612. * @return array
  1613. */
  1614. function remove_option_whitelist( $del_options, $options = '' ) {
  1615. if ( $options == '' )
  1616. global $whitelist_options;
  1617. else
  1618. $whitelist_options = $options;
  1619. foreach ( $del_options as $page => $keys ) {
  1620. foreach ( $keys as $key ) {
  1621. if ( isset($whitelist_options[ $page ]) && is_array($whitelist_options[ $page ]) ) {
  1622. $pos = array_search( $key, $whitelist_options[ $page ] );
  1623. if ( $pos !== false )
  1624. unset( $whitelist_options[ $page ][ $pos ] );
  1625. }
  1626. }
  1627. }
  1628. return $whitelist_options;
  1629. }
  1630. /**
  1631. * Output nonce, action, and option_page fields for a settings page.
  1632. *
  1633. * @since 2.7.0
  1634. *
  1635. * @param string $option_group A settings group name. This should match the group name used in register_setting().
  1636. */
  1637. function settings_fields($option_group) {
  1638. echo "<input type='hidden' name='option_page' value='" . esc_attr($option_group) . "' />";
  1639. echo '<input type="hidden" name="action" value="update" />';
  1640. wp_nonce_field("$option_group-options");
  1641. }
  1642. /**
  1643. * Clears the Plugins cache used by get_plugins() and by default, the Plugin Update cache.
  1644. *
  1645. * @since 3.7.0
  1646. *
  1647. * @param bool $clear_update_cache Whether to clear the Plugin updates cache
  1648. */
  1649. function wp_clean_plugins_cache( $clear_update_cache = true ) {
  1650. if ( $clear_update_cache )
  1651. delete_site_transient( 'update_plugins' );
  1652. wp_cache_delete( 'plugins', 'plugins' );
  1653. }
  1654. /**
  1655. * @param string $plugin
  1656. */
  1657. function plugin_sandbox_scrape( $plugin ) {
  1658. wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
  1659. include( WP_PLUGIN_DIR . '/' . $plugin );
  1660. }