PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/plugin.php

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