PageRenderTime 48ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/wp-admin/includes/plugin.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 1726 lines | 863 code | 216 blank | 647 comment | 276 complexity | ec909feb5dc03f74b5f245850ae2b376 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0

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

  1. <?php
  2. /**
  3. * WordPress Plugin Administration API
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * 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. * <code>
  17. * /*
  18. * Plugin Name: Name of Plugin
  19. * Plugin URI: Link to plugin information
  20. * Description: Plugin Description
  21. * Author: Plugin author's name
  22. * Author URI: Link to the author's web site
  23. * Version: Must be set in the plugin for WordPress 2.3+
  24. * Text Domain: Optional. Unique identifier, should be same as the one used in
  25. * plugin_text_domain()
  26. * Domain Path: Optional. Only useful if the translations are located in a
  27. * folder above the plugin's base path. For example, if .mo files are
  28. * located in the locale folder then Domain Path will be "/locale/" and
  29. * must have the first slash. Defaults to the base folder the plugin is
  30. * located in.
  31. * Network: Optional. Specify "Network: true" to require that a plugin is activated
  32. * across all sites in an installation. This will prevent a plugin from being
  33. * activated on a single site when Multisite is enabled.
  34. * * / # Remove the space to close comment
  35. * </code>
  36. *
  37. * Plugin data returned array contains the following:
  38. * 'Name' - Name of the plugin, must be unique.
  39. * 'Title' - Title of the plugin and the link to the plugin's web site.
  40. * 'Description' - Description of what the plugin does and/or notes
  41. * from the author.
  42. * 'Author' - The author's name
  43. * 'AuthorURI' - The authors web site address.
  44. * 'Version' - The plugin version number.
  45. * 'PluginURI' - Plugin web site address.
  46. * 'TextDomain' - Plugin's text domain for localization.
  47. * 'DomainPath' - Plugin's relative directory path to .mo files.
  48. * 'Network' - Boolean. Whether the plugin can only be activated network wide.
  49. *
  50. * Some users have issues with opening large files and manipulating the contents
  51. * for want is usually the first 1kiB or 2kiB. This function stops pulling in
  52. * the plugin contents when it has all of the required plugin data.
  53. *
  54. * The first 8kiB of the file will be pulled in and if the plugin data is not
  55. * within that first 8kiB, then the plugin author should correct their plugin
  56. * and move the plugin data headers to the top.
  57. *
  58. * The plugin file is assumed to have permissions to allow for scripts to read
  59. * the file. This is not checked however and the file is only opened for
  60. * reading.
  61. *
  62. * @link http://trac.wordpress.org/ticket/5651 Previous Optimizations.
  63. * @link http://trac.wordpress.org/ticket/7372 Further and better Optimizations.
  64. * @since 1.5.0
  65. *
  66. * @param string $plugin_file Path to the plugin file
  67. * @param bool $markup If the returned data should have HTML markup applied
  68. * @param bool $translate If the returned data should be translated
  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 ( empty( $plugin_data['Network'] ) && ! empty( $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. //For backward compatibility by default Title is the same as Name.
  94. $plugin_data['Title'] = $plugin_data['Name'];
  95. if ( $markup || $translate )
  96. $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
  97. else
  98. $plugin_data['AuthorName'] = $plugin_data['Author'];
  99. return $plugin_data;
  100. }
  101. function _get_plugin_data_markup_translate($plugin_file, $plugin_data, $markup = true, $translate = true) {
  102. //Translate fields
  103. if ( $translate && ! empty($plugin_data['TextDomain']) ) {
  104. if ( ! empty( $plugin_data['DomainPath'] ) )
  105. load_plugin_textdomain($plugin_data['TextDomain'], false, dirname($plugin_file). $plugin_data['DomainPath']);
  106. else
  107. load_plugin_textdomain($plugin_data['TextDomain'], false, dirname($plugin_file));
  108. foreach ( array('Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version') as $field )
  109. $plugin_data[ $field ] = translate($plugin_data[ $field ], $plugin_data['TextDomain']);
  110. }
  111. $plugins_allowedtags = array(
  112. 'a' => array( 'href' => array(), 'title' => array() ),
  113. 'abbr' => array( 'title' => array() ),
  114. 'acronym' => array( 'title' => array() ),
  115. 'code' => array(),
  116. 'em' => array(),
  117. 'strong' => array(),
  118. );
  119. $plugin_data['AuthorName'] = $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $plugins_allowedtags );
  120. //Apply Markup
  121. if ( $markup ) {
  122. if ( ! empty($plugin_data['PluginURI']) && ! empty($plugin_data['Name']) )
  123. $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . esc_attr__( 'Visit plugin homepage' ) . '">' . $plugin_data['Name'] . '</a>';
  124. else
  125. $plugin_data['Title'] = $plugin_data['Name'];
  126. if ( ! empty($plugin_data['AuthorURI']) && ! empty($plugin_data['Author']) )
  127. $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . esc_attr__( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>';
  128. $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
  129. if ( ! empty($plugin_data['Author']) )
  130. $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s'), $plugin_data['Author'] ) . '.</cite>';
  131. }
  132. // Sanitize all displayed data. Author and AuthorName sanitized above.
  133. $plugin_data['Title'] = wp_kses( $plugin_data['Title'], $plugins_allowedtags );
  134. $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $plugins_allowedtags );
  135. $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $plugins_allowedtags );
  136. $plugin_data['Name'] = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
  137. return $plugin_data;
  138. }
  139. /**
  140. * Get a list of a plugin's files.
  141. *
  142. * @since 2.8.0
  143. *
  144. * @param string $plugin Plugin ID
  145. * @return array List of files relative to the plugin root.
  146. */
  147. function get_plugin_files($plugin) {
  148. $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
  149. $dir = dirname($plugin_file);
  150. $plugin_files = array($plugin);
  151. if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) {
  152. $plugins_dir = @ opendir( $dir );
  153. if ( $plugins_dir ) {
  154. while (($file = readdir( $plugins_dir ) ) !== false ) {
  155. if ( substr($file, 0, 1) == '.' )
  156. continue;
  157. if ( is_dir( $dir . '/' . $file ) ) {
  158. $plugins_subdir = @ opendir( $dir . '/' . $file );
  159. if ( $plugins_subdir ) {
  160. while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
  161. if ( substr($subfile, 0, 1) == '.' )
  162. continue;
  163. $plugin_files[] = plugin_basename("$dir/$file/$subfile");
  164. }
  165. @closedir( $plugins_subdir );
  166. }
  167. } else {
  168. if ( plugin_basename("$dir/$file") != $plugin )
  169. $plugin_files[] = plugin_basename("$dir/$file");
  170. }
  171. }
  172. @closedir( $plugins_dir );
  173. }
  174. }
  175. return $plugin_files;
  176. }
  177. /**
  178. * Check the plugins directory and retrieve all plugin files with plugin data.
  179. *
  180. * WordPress only supports plugin files in the base plugins directory
  181. * (wp-content/plugins) and in one directory above the plugins directory
  182. * (wp-content/plugins/my-plugin). The file it looks for has the plugin data and
  183. * must be found in those two locations. It is recommended that do keep your
  184. * plugin files in directories.
  185. *
  186. * The file with the plugin data is the file that will be included and therefore
  187. * needs to have the main execution for the plugin. This does not mean
  188. * everything must be contained in the file and it is recommended that the file
  189. * be split for maintainability. Keep everything in one file for extreme
  190. * optimization purposes.
  191. *
  192. * @since 1.5.0
  193. *
  194. * @param string $plugin_folder Optional. Relative path to single plugin folder.
  195. * @return array Key is the plugin file path and the value is an array of the plugin data.
  196. */
  197. function get_plugins($plugin_folder = '') {
  198. if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
  199. $cache_plugins = array();
  200. if ( isset($cache_plugins[ $plugin_folder ]) )
  201. return $cache_plugins[ $plugin_folder ];
  202. $wp_plugins = array ();
  203. $plugin_root = WP_PLUGIN_DIR;
  204. if ( !empty($plugin_folder) )
  205. $plugin_root .= $plugin_folder;
  206. // Files in wp-content/plugins directory
  207. $plugins_dir = @ opendir( $plugin_root);
  208. $plugin_files = array();
  209. if ( $plugins_dir ) {
  210. while (($file = readdir( $plugins_dir ) ) !== false ) {
  211. if ( substr($file, 0, 1) == '.' )
  212. continue;
  213. if ( is_dir( $plugin_root.'/'.$file ) ) {
  214. $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
  215. if ( $plugins_subdir ) {
  216. while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
  217. if ( substr($subfile, 0, 1) == '.' )
  218. continue;
  219. if ( substr($subfile, -4) == '.php' )
  220. $plugin_files[] = "$file/$subfile";
  221. }
  222. closedir( $plugins_subdir );
  223. }
  224. } else {
  225. if ( substr($file, -4) == '.php' )
  226. $plugin_files[] = $file;
  227. }
  228. }
  229. closedir( $plugins_dir );
  230. }
  231. if ( empty($plugin_files) )
  232. return $wp_plugins;
  233. foreach ( $plugin_files as $plugin_file ) {
  234. if ( !is_readable( "$plugin_root/$plugin_file" ) )
  235. continue;
  236. $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
  237. if ( empty ( $plugin_data['Name'] ) )
  238. continue;
  239. $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
  240. }
  241. uasort( $wp_plugins, '_sort_uname_callback' );
  242. $cache_plugins[ $plugin_folder ] = $wp_plugins;
  243. wp_cache_set('plugins', $cache_plugins, 'plugins');
  244. return $wp_plugins;
  245. }
  246. /**
  247. * Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data.
  248. *
  249. * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins).
  250. *
  251. * @since 3.0.0
  252. * @return array Key is the mu-plugin file path and the value is an array of the mu-plugin data.
  253. */
  254. function get_mu_plugins() {
  255. $wp_plugins = array();
  256. // Files in wp-content/mu-plugins directory
  257. $plugin_files = array();
  258. if ( ! is_dir( WPMU_PLUGIN_DIR ) )
  259. return $wp_plugins;
  260. if ( $plugins_dir = @ opendir( WPMU_PLUGIN_DIR ) ) {
  261. while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
  262. if ( substr( $file, -4 ) == '.php' )
  263. $plugin_files[] = $file;
  264. }
  265. } else {
  266. return $wp_plugins;
  267. }
  268. @closedir( $plugins_dir );
  269. if ( empty($plugin_files) )
  270. return $wp_plugins;
  271. foreach ( $plugin_files as $plugin_file ) {
  272. if ( !is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) )
  273. continue;
  274. $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
  275. if ( empty ( $plugin_data['Name'] ) )
  276. $plugin_data['Name'] = $plugin_file;
  277. $wp_plugins[ $plugin_file ] = $plugin_data;
  278. }
  279. if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php') <= 30 ) // silence is golden
  280. unset( $wp_plugins['index.php'] );
  281. uasort( $wp_plugins, '_sort_uname_callback' );
  282. return $wp_plugins;
  283. }
  284. /**
  285. * Callback to sort array by a 'Name' key.
  286. *
  287. * @since 3.1.0
  288. * @access private
  289. */
  290. function _sort_uname_callback( $a, $b ) {
  291. return strnatcasecmp( $a['Name'], $b['Name'] );
  292. }
  293. /**
  294. * Check the wp-content directory and retrieve all drop-ins with any plugin data.
  295. *
  296. * @since 3.0.0
  297. * @return array Key is the file path and the value is an array of the plugin data.
  298. */
  299. function get_dropins() {
  300. $dropins = array();
  301. $plugin_files = array();
  302. $_dropins = _get_dropins();
  303. // These exist in the wp-content directory
  304. if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) {
  305. while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
  306. if ( isset( $_dropins[ $file ] ) )
  307. $plugin_files[] = $file;
  308. }
  309. } else {
  310. return $dropins;
  311. }
  312. @closedir( $plugins_dir );
  313. if ( empty($plugin_files) )
  314. return $dropins;
  315. foreach ( $plugin_files as $plugin_file ) {
  316. if ( !is_readable( WP_CONTENT_DIR . "/$plugin_file" ) )
  317. continue;
  318. $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
  319. if ( empty( $plugin_data['Name'] ) )
  320. $plugin_data['Name'] = $plugin_file;
  321. $dropins[ $plugin_file ] = $plugin_data;
  322. }
  323. uksort( $dropins, 'strnatcasecmp' );
  324. return $dropins;
  325. }
  326. /**
  327. * Returns drop-ins that WordPress uses.
  328. *
  329. * Includes Multisite drop-ins only when is_multisite()
  330. *
  331. * @since 3.0.0
  332. * @return array Key is file name. The value is an array, with the first value the
  333. * purpose of the drop-in and the second value the name of the constant that must be
  334. * true for the drop-in to be used, or true if no constant is required.
  335. */
  336. function _get_dropins() {
  337. $dropins = array(
  338. 'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE
  339. 'db.php' => array( __( 'Custom database class.' ), true ), // auto on load
  340. 'db-error.php' => array( __( 'Custom database error message.' ), true ), // auto on error
  341. 'install.php' => array( __( 'Custom install script.' ), true ), // auto on install
  342. 'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // auto on maintenance
  343. 'object-cache.php' => array( __( 'External object cache.' ), true ), // auto on load
  344. );
  345. if ( is_multisite() ) {
  346. $dropins['sunrise.php' ] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE
  347. $dropins['blog-deleted.php' ] = array( __( 'Custom site deleted message.' ), true ); // auto on deleted blog
  348. $dropins['blog-inactive.php' ] = array( __( 'Custom site inactive message.' ), true ); // auto on inactive blog
  349. $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // auto on archived or spammed blog
  350. }
  351. return $dropins;
  352. }
  353. /**
  354. * Check whether the plugin is active by checking the active_plugins list.
  355. *
  356. * @since 2.5.0
  357. *
  358. * @param string $plugin Base plugin path from plugins directory.
  359. * @return bool True, if in the active plugins list. False, not in the list.
  360. */
  361. function is_plugin_active( $plugin ) {
  362. return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
  363. }
  364. /**
  365. * Check whether the plugin is inactive.
  366. *
  367. * Reverse of is_plugin_active(). Used as a callback.
  368. *
  369. * @since 3.1.0
  370. * @see is_plugin_active()
  371. *
  372. * @param string $plugin Base plugin path from plugins directory.
  373. * @return bool True if inactive. False if active.
  374. */
  375. function is_plugin_inactive( $plugin ) {
  376. return ! is_plugin_active( $plugin );
  377. }
  378. /**
  379. * Check whether the plugin is active for the entire network.
  380. *
  381. * @since 3.0.0
  382. *
  383. * @param string $plugin Base plugin path from plugins directory.
  384. * @return bool True, if active for the network, otherwise false.
  385. */
  386. function is_plugin_active_for_network( $plugin ) {
  387. if ( !is_multisite() )
  388. return false;
  389. $plugins = get_site_option( 'active_sitewide_plugins');
  390. if ( isset($plugins[$plugin]) )
  391. return true;
  392. return false;
  393. }
  394. /**
  395. * Checks for "Network: true" in the plugin header to see if this should
  396. * be activated only as a network wide plugin. The plugin would also work
  397. * when Multisite is not enabled.
  398. *
  399. * Checks for "Site Wide Only: true" for backwards compatibility.
  400. *
  401. * @since 3.0.0
  402. *
  403. * @param string $plugin Plugin to check
  404. * @return bool True if plugin is network only, false otherwise.
  405. */
  406. function is_network_only_plugin( $plugin ) {
  407. $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
  408. if ( $plugin_data )
  409. return $plugin_data['Network'];
  410. return false;
  411. }
  412. /**
  413. * Attempts activation of plugin in a "sandbox" and redirects on success.
  414. *
  415. * A plugin that is already activated will not attempt to be activated again.
  416. *
  417. * The way it works is by setting the redirection to the error before trying to
  418. * include the plugin file. If the plugin fails, then the redirection will not
  419. * be overwritten with the success message. Also, the options will not be
  420. * updated and the activation hook will not be called on plugin error.
  421. *
  422. * It should be noted that in no way the below code will actually prevent errors
  423. * within the file. The code should not be used elsewhere to replicate the
  424. * "sandbox", which uses redirection to work.
  425. * {@source 13 1}
  426. *
  427. * If any errors are found or text is outputted, then it will be captured to
  428. * ensure that the success redirection will update the error redirection.
  429. *
  430. * @since 2.5.0
  431. *
  432. * @param string $plugin Plugin path to main plugin file with plugin data.
  433. * @param string $redirect Optional. URL to redirect to.
  434. * @param bool $network_wide Whether to enable the plugin for all sites in the
  435. * network or just the current site. Multisite only. Default is false.
  436. * @param bool $silent Prevent calling activation hooks. Optional, default is false.
  437. * @return WP_Error|null WP_Error on invalid file or null on success.
  438. */
  439. function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
  440. $plugin = plugin_basename( trim( $plugin ) );
  441. if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) {
  442. $network_wide = true;
  443. $current = get_site_option( 'active_sitewide_plugins', array() );
  444. } else {
  445. $current = get_option( 'active_plugins', array() );
  446. }
  447. $valid = validate_plugin($plugin);
  448. if ( is_wp_error($valid) )
  449. return $valid;
  450. if ( !in_array($plugin, $current) ) {
  451. if ( !empty($redirect) )
  452. 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
  453. ob_start();
  454. include_once(WP_PLUGIN_DIR . '/' . $plugin);
  455. if ( ! $silent ) {
  456. do_action( 'activate_plugin', $plugin, $network_wide );
  457. do_action( 'activate_' . $plugin, $network_wide );
  458. }
  459. if ( $network_wide ) {
  460. $current[$plugin] = time();
  461. update_site_option( 'active_sitewide_plugins', $current );
  462. } else {
  463. $current[] = $plugin;
  464. sort($current);
  465. update_option('active_plugins', $current);
  466. }
  467. if ( ! $silent ) {
  468. do_action( 'activated_plugin', $plugin, $network_wide );
  469. }
  470. if ( ob_get_length() > 0 ) {
  471. $output = ob_get_clean();
  472. return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
  473. }
  474. ob_end_clean();
  475. }
  476. return null;
  477. }
  478. /**
  479. * Deactivate a single plugin or multiple plugins.
  480. *
  481. * The deactivation hook is disabled by the plugin upgrader by using the $silent
  482. * parameter.
  483. *
  484. * @since 2.5.0
  485. *
  486. * @param string|array $plugins Single plugin or list of plugins to deactivate.
  487. * @param bool $silent Prevent calling deactivation hooks. Default is false.
  488. */
  489. function deactivate_plugins( $plugins, $silent = false ) {
  490. if ( is_multisite() )
  491. $network_current = get_site_option( 'active_sitewide_plugins', array() );
  492. $current = get_option( 'active_plugins', array() );
  493. $do_blog = $do_network = false;
  494. foreach ( (array) $plugins as $plugin ) {
  495. $plugin = plugin_basename( trim( $plugin ) );
  496. if ( ! is_plugin_active($plugin) )
  497. continue;
  498. $network_wide = is_plugin_active_for_network( $plugin );
  499. if ( ! $silent )
  500. do_action( 'deactivate_plugin', $plugin, $network_wide );
  501. if ( $network_wide ) {
  502. $do_network = true;
  503. unset( $network_current[ $plugin ] );
  504. } else {
  505. $key = array_search( $plugin, $current );
  506. if ( false !== $key ) {
  507. $do_blog = true;
  508. array_splice( $current, $key, 1 );
  509. }
  510. }
  511. if ( ! $silent ) {
  512. do_action( 'deactivate_' . $plugin, $network_wide );
  513. do_action( 'deactivated_plugin', $plugin, $network_wide );
  514. }
  515. }
  516. if ( $do_blog )
  517. update_option('active_plugins', $current);
  518. if ( $do_network )
  519. update_site_option( 'active_sitewide_plugins', $network_current );
  520. }
  521. /**
  522. * Activate multiple plugins.
  523. *
  524. * When WP_Error is returned, it does not mean that one of the plugins had
  525. * errors. It means that one or more of the plugins file path was invalid.
  526. *
  527. * The execution will be halted as soon as one of the plugins has an error.
  528. *
  529. * @since 2.6.0
  530. *
  531. * @param string|array $plugins
  532. * @param string $redirect Redirect to page after successful activation.
  533. * @param bool $network_wide Whether to enable the plugin for all sites in the network.
  534. * @param bool $silent Prevent calling activation hooks. Default is false.
  535. * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
  536. */
  537. function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
  538. if ( !is_array($plugins) )
  539. $plugins = array($plugins);
  540. $errors = array();
  541. foreach ( $plugins as $plugin ) {
  542. if ( !empty($redirect) )
  543. $redirect = add_query_arg('plugin', $plugin, $redirect);
  544. $result = activate_plugin($plugin, $redirect, $network_wide, $silent);
  545. if ( is_wp_error($result) )
  546. $errors[$plugin] = $result;
  547. }
  548. if ( !empty($errors) )
  549. return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
  550. return true;
  551. }
  552. /**
  553. * Remove directory and files of a plugin for a single or list of plugin(s).
  554. *
  555. * If the plugins parameter list is empty, false will be returned. True when
  556. * completed.
  557. *
  558. * @since 2.6.0
  559. *
  560. * @param array $plugins List of plugin
  561. * @param string $redirect Redirect to page when complete.
  562. * @return mixed
  563. */
  564. function delete_plugins($plugins, $redirect = '' ) {
  565. global $wp_filesystem;
  566. if ( empty($plugins) )
  567. return false;
  568. $checked = array();
  569. foreach( $plugins as $plugin )
  570. $checked[] = 'checked[]=' . $plugin;
  571. ob_start();
  572. $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-plugins');
  573. if ( false === ($credentials = request_filesystem_credentials($url)) ) {
  574. $data = ob_get_contents();
  575. ob_end_clean();
  576. if ( ! empty($data) ){
  577. include_once( ABSPATH . 'wp-admin/admin-header.php');
  578. echo $data;
  579. include( ABSPATH . 'wp-admin/admin-footer.php');
  580. exit;
  581. }
  582. return;
  583. }
  584. if ( ! WP_Filesystem($credentials) ) {
  585. request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
  586. $data = ob_get_contents();
  587. ob_end_clean();
  588. if ( ! empty($data) ){
  589. include_once( ABSPATH . 'wp-admin/admin-header.php');
  590. echo $data;
  591. include( ABSPATH . 'wp-admin/admin-footer.php');
  592. exit;
  593. }
  594. return;
  595. }
  596. if ( ! is_object($wp_filesystem) )
  597. return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
  598. if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
  599. return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
  600. //Get the base plugin folder
  601. $plugins_dir = $wp_filesystem->wp_plugins_dir();
  602. if ( empty($plugins_dir) )
  603. return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
  604. $plugins_dir = trailingslashit( $plugins_dir );
  605. $errors = array();
  606. foreach( $plugins as $plugin_file ) {
  607. // Run Uninstall hook
  608. if ( is_uninstallable_plugin( $plugin_file ) )
  609. uninstall_plugin($plugin_file);
  610. $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
  611. // If plugin is in its own directory, recursively delete the directory.
  612. if ( strpos($plugin_file, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory separator AND that its not the root plugin folder
  613. $deleted = $wp_filesystem->delete($this_plugin_dir, true);
  614. else
  615. $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
  616. if ( ! $deleted )
  617. $errors[] = $plugin_file;
  618. }
  619. if ( ! empty($errors) )
  620. return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s.'), implode(', ', $errors)) );
  621. // Force refresh of plugin update information
  622. if ( $current = get_site_transient('update_plugins') ) {
  623. unset( $current->response[ $plugin_file ] );
  624. set_site_transient('update_plugins', $current);
  625. }
  626. return true;
  627. }
  628. /**
  629. * Validate active plugins
  630. *
  631. * Validate all active plugins, deactivates invalid and
  632. * returns an array of deactivated ones.
  633. *
  634. * @since 2.5.0
  635. * @return array invalid plugins, plugin as key, error as value
  636. */
  637. function validate_active_plugins() {
  638. $plugins = get_option( 'active_plugins', array() );
  639. // validate vartype: array
  640. if ( ! is_array( $plugins ) ) {
  641. update_option( 'active_plugins', array() );
  642. $plugins = array();
  643. }
  644. if ( is_multisite() && is_super_admin() ) {
  645. $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
  646. $plugins = array_merge( $plugins, array_keys( $network_plugins ) );
  647. }
  648. if ( empty( $plugins ) )
  649. return;
  650. $invalid = array();
  651. // invalid plugins get deactivated
  652. foreach ( $plugins as $plugin ) {
  653. $result = validate_plugin( $plugin );
  654. if ( is_wp_error( $result ) ) {
  655. $invalid[$plugin] = $result;
  656. deactivate_plugins( $plugin, true );
  657. }
  658. }
  659. return $invalid;
  660. }
  661. /**
  662. * Validate the plugin path.
  663. *
  664. * Checks that the file exists and {@link validate_file() is valid file}.
  665. *
  666. * @since 2.5.0
  667. *
  668. * @param string $plugin Plugin Path
  669. * @return WP_Error|int 0 on success, WP_Error on failure.
  670. */
  671. function validate_plugin($plugin) {
  672. if ( validate_file($plugin) )
  673. return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
  674. if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
  675. return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
  676. $installed_plugins = get_plugins();
  677. if ( ! isset($installed_plugins[$plugin]) )
  678. return new WP_Error('no_plugin_header', __('The plugin does not have a valid header.'));
  679. return 0;
  680. }
  681. /**
  682. * Whether the plugin can be uninstalled.
  683. *
  684. * @since 2.7.0
  685. *
  686. * @param string $plugin Plugin path to check.
  687. * @return bool Whether plugin can be uninstalled.
  688. */
  689. function is_uninstallable_plugin($plugin) {
  690. $file = plugin_basename($plugin);
  691. $uninstallable_plugins = (array) get_option('uninstall_plugins');
  692. if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
  693. return true;
  694. return false;
  695. }
  696. /**
  697. * Uninstall a single plugin.
  698. *
  699. * Calls the uninstall hook, if it is available.
  700. *
  701. * @since 2.7.0
  702. *
  703. * @param string $plugin Relative plugin path from Plugin Directory.
  704. */
  705. function uninstall_plugin($plugin) {
  706. $file = plugin_basename($plugin);
  707. $uninstallable_plugins = (array) get_option('uninstall_plugins');
  708. if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
  709. if ( isset( $uninstallable_plugins[$file] ) ) {
  710. unset($uninstallable_plugins[$file]);
  711. update_option('uninstall_plugins', $uninstallable_plugins);
  712. }
  713. unset($uninstallable_plugins);
  714. define('WP_UNINSTALL_PLUGIN', $file);
  715. include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
  716. return true;
  717. }
  718. if ( isset( $uninstallable_plugins[$file] ) ) {
  719. $callable = $uninstallable_plugins[$file];
  720. unset($uninstallable_plugins[$file]);
  721. update_option('uninstall_plugins', $uninstallable_plugins);
  722. unset($uninstallable_plugins);
  723. include WP_PLUGIN_DIR . '/' . $file;
  724. add_action( 'uninstall_' . $file, $callable );
  725. do_action( 'uninstall_' . $file );
  726. }
  727. }
  728. //
  729. // Menu
  730. //
  731. /**
  732. * Add a top level menu page
  733. *
  734. * This function takes a capability which will be used to determine whether
  735. * or not a page is included in the menu.
  736. *
  737. * The function which is hooked in to handle the output of the page must check
  738. * that the user has the required capability as well.
  739. *
  740. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  741. * @param string $menu_title The text to be used for the menu
  742. * @param string $capability The capability required for this menu to be displayed to the user.
  743. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  744. * @param callback $function The function to be called to output the content for this page.
  745. * @param string $icon_url The url to the icon to be used for this menu
  746. * @param int $position The position in the menu order this one should appear
  747. *
  748. * @return string The resulting page's hook_suffix
  749. */
  750. function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = NULL ) {
  751. global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
  752. $menu_slug = plugin_basename( $menu_slug );
  753. $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title );
  754. $hookname = get_plugin_page_hookname( $menu_slug, '' );
  755. if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) )
  756. add_action( $hookname, $function );
  757. if ( empty($icon_url) )
  758. $icon_url = esc_url( admin_url( 'images/generic.png' ) );
  759. elseif ( is_ssl() && 0 === strpos($icon_url, 'http://') )
  760. $icon_url = 'https://' . substr($icon_url, 7);
  761. $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
  762. if ( null === $position )
  763. $menu[] = $new_menu;
  764. else
  765. $menu[$position] = $new_menu;
  766. $_registered_pages[$hookname] = true;
  767. // No parent as top level
  768. $_parent_pages[$menu_slug] = false;
  769. return $hookname;
  770. }
  771. /**
  772. * Add a top level menu page in the 'objects' section
  773. *
  774. * This function takes a capability which will be used to determine whether
  775. * or not a page is included in the menu.
  776. *
  777. * The function which is hooked in to handle the output of the page must check
  778. * that the user has the required capability as well.
  779. *
  780. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  781. * @param string $menu_title The text to be used for the menu
  782. * @param string $capability The capability required for this menu to be displayed to the user.
  783. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  784. * @param callback $function The function to be called to output the content for this page.
  785. * @param string $icon_url The url to the icon to be used for this menu
  786. *
  787. * @return string The resulting page's hook_suffix
  788. */
  789. function add_object_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
  790. global $_wp_last_object_menu;
  791. $_wp_last_object_menu++;
  792. return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_object_menu);
  793. }
  794. /**
  795. * Add a top level menu page in the 'utility' section
  796. *
  797. * This function takes a capability which will be used to determine whether
  798. * or not a page is included in the menu.
  799. *
  800. * The function which is hooked in to handle the output of the page must check
  801. * that the user has the required capability as well.
  802. *
  803. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  804. * @param string $menu_title The text to be used for the menu
  805. * @param string $capability The capability required for this menu to be displayed to the user.
  806. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  807. * @param callback $function The function to be called to output the content for this page.
  808. * @param string $icon_url The url to the icon to be used for this menu
  809. *
  810. * @return string The resulting page's hook_suffix
  811. */
  812. function add_utility_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
  813. global $_wp_last_utility_menu;
  814. $_wp_last_utility_menu++;
  815. return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_utility_menu);
  816. }
  817. /**
  818. * Add a sub menu page
  819. *
  820. * This function takes a capability which will be used to determine whether
  821. * or not a page is included in the menu.
  822. *
  823. * The function which is hooked in to handle the output of the page must check
  824. * that the user has the required capability as well.
  825. *
  826. * @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page)
  827. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  828. * @param string $menu_title The text to be used for the menu
  829. * @param string $capability The capability required for this menu to be displayed to the user.
  830. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  831. * @param callback $function The function to be called to output the content for this page.
  832. *
  833. * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
  834. */
  835. function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  836. global $submenu;
  837. global $menu;
  838. global $_wp_real_parent_file;
  839. global $_wp_submenu_nopriv;
  840. global $_registered_pages;
  841. global $_parent_pages;
  842. $menu_slug = plugin_basename( $menu_slug );
  843. $parent_slug = plugin_basename( $parent_slug);
  844. if ( isset( $_wp_real_parent_file[$parent_slug] ) )
  845. $parent_slug = $_wp_real_parent_file[$parent_slug];
  846. if ( !current_user_can( $capability ) ) {
  847. $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true;
  848. return false;
  849. }
  850. // If the parent doesn't already have a submenu, add a link to the parent
  851. // as the first item in the submenu. If the submenu file is the same as the
  852. // parent file someone is trying to link back to the parent manually. In
  853. // this case, don't automatically add a link back to avoid duplication.
  854. if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) {
  855. foreach ( (array)$menu as $parent_menu ) {
  856. if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) )
  857. $submenu[$parent_slug][] = $parent_menu;
  858. }
  859. }
  860. $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title );
  861. $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);
  862. if (!empty ( $function ) && !empty ( $hookname ))
  863. add_action( $hookname, $function );
  864. $_registered_pages[$hookname] = true;
  865. // backwards-compatibility for plugins using add_management page. See wp-admin/admin.php for redirect from edit.php to tools.php
  866. if ( 'tools.php' == $parent_slug )
  867. $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true;
  868. // No parent as top level
  869. $_parent_pages[$menu_slug] = $parent_slug;
  870. return $hookname;
  871. }
  872. /**
  873. * Add sub menu page to the tools main menu.
  874. *
  875. * This function takes a capability which will be used to determine whether
  876. * or not a page is included in the menu.
  877. *
  878. * The function which is hooked in to handle the output of the page must check
  879. * that the user has the required capability as well.
  880. *
  881. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  882. * @param string $menu_title The text to be used for the menu
  883. * @param string $capability The capability required for this menu to be displayed to the user.
  884. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  885. * @param callback $function The function to be called to output the content for this page.
  886. *
  887. * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
  888. */
  889. function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  890. return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  891. }
  892. /**
  893. * Add sub menu page to the options main menu.
  894. *
  895. * This function takes a capability which will be used to determine whether
  896. * or not a page is included in the menu.
  897. *
  898. * The function which is hooked in to handle the output of the page must check
  899. * that the user has the required capability as well.
  900. *
  901. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  902. * @param string $menu_title The text to be used for the menu
  903. * @param string $capability The capability required for this menu to be displayed to the user.
  904. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  905. * @param callback $function The function to be called to output the content for this page.
  906. *
  907. * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
  908. */
  909. function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  910. return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  911. }
  912. /**
  913. * Add sub menu page to the themes main menu.
  914. *
  915. * This function takes a capability which will be used to determine whether
  916. * or not a page is included in the menu.
  917. *
  918. * The function which is hooked in to handle the output of the page must check
  919. * that the user has the required capability as well.
  920. *
  921. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  922. * @param string $menu_title The text to be used for the menu
  923. * @param string $capability The capability required for this menu to be displayed to the user.
  924. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  925. * @param callback $function The function to be called to output the content for this page.
  926. *
  927. * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
  928. */
  929. function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  930. return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  931. }
  932. /**
  933. * Add sub menu page to the plugins main menu.
  934. *
  935. * This function takes a capability which will be used to determine whether
  936. * or not a page is included in the menu.
  937. *
  938. * The function which is hooked in to handle the output of the page must check
  939. * that the user has the required capability as well.
  940. *
  941. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  942. * @param string $menu_title The text to be used for the menu
  943. * @param string $capability The capability required for this menu to be displayed to the user.
  944. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  945. * @param callback $function The function to be called to output the content for this page.
  946. *
  947. * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
  948. */
  949. function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  950. return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  951. }
  952. /**
  953. * Add sub menu page to the Users/Profile main menu.
  954. *
  955. * This function takes a capability which will be used to determine whether
  956. * or not a page is included in the menu.
  957. *
  958. * The function which is hooked in to handle the output of the page must check
  959. * that the user has the required capability as well.
  960. *
  961. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  962. * @param string $menu_title The text to be used for the menu
  963. * @param string $capability The capability required for this menu to be displayed to the user.
  964. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  965. * @param callback $function The function to be called to output the content for this page.
  966. *
  967. * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
  968. */
  969. function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  970. if ( current_user_can('edit_users') )
  971. $parent = 'users.php';
  972. else
  973. $parent = 'profile.php';
  974. return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
  975. }
  976. /**
  977. * Add sub menu page to the Dashboard main menu.
  978. *
  979. * This function takes a capability which will be used to determine whether
  980. * or not a page is included in the menu.
  981. *
  982. * The function which is hooked in to handle the output of the page must check
  983. * that the user has the required capability as well.
  984. *
  985. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  986. * @param string $menu_title The text to be used for the menu
  987. * @param string $capability The capability required for this menu to be displayed to the user.
  988. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  989. * @param callback $function The function to be called to output the content for this page.
  990. *
  991. * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
  992. */
  993. function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  994. return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  995. }
  996. /**
  997. * Add sub menu page to the posts main menu.
  998. *
  999. * This function takes a capability which will be used to determine whether
  1000. * or not a page is included in the menu.
  1001. *
  1002. * The function which is hooked in to handle the output of the page must check
  1003. * that the user has the required capability as well.
  1004. *
  1005. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  1006. * @param string $menu_title The text to be used for the menu
  1007. * @param string $capability The capability required for this menu to be displayed to the user.
  1008. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1009. * @param callback $function The function to be called to output the content for this page.
  1010. *
  1011. * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
  1012. */
  1013. function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1014. return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1015. }
  1016. /**
  1017. * Add sub menu page to the media main menu.
  1018. *
  1019. * This function takes a capability which will be used to determine whether
  1020. * or not a page is included in the menu.
  1021. *
  1022. * The function which is hooked in to handle the output of the page must check
  1023. * that the user has the required capability as well.
  1024. *
  1025. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  1026. * @param string $menu_title The text to be used for the menu
  1027. * @param string $capability The capability required for this menu to be displayed to the user.
  1028. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1029. * @param callback $function The function to be called to output the content for this page.
  1030. *
  1031. * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
  1032. */
  1033. function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1034. return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1035. }
  1036. /**
  1037. * Add sub menu page to the links main menu.
  1038. *
  1039. * This function takes a capability which will be used to determine whether
  1040. * or not a page is included in the menu.
  1041. *
  1042. * The function which is hooked in to handle the output of the page must check
  1043. * that the user has the required capability as well.
  1044. *
  1045. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  1046. * @param string $menu_title The text to be used for the menu
  1047. * @param string $capability The capability required for this menu to be displayed to the user.
  1048. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1049. * @param callback $function The function to be called to output the content for this page.
  1050. *
  1051. * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
  1052. */
  1053. function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1054. return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1055. }
  1056. /**
  1057. * Add sub menu page to the pages main menu.
  1058. *
  1059. * This function takes a capability which will be used to determine whether
  1060. * or not a page is included in the menu.
  1061. *
  1062. * The function which is hooked in to handle the output of the page must check
  1063. * that the user has the required capability as well.
  1064. *
  1065. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  1066. * @param string $menu_title The text to be used for the menu
  1067. * @param string $capability The capability required for this menu to be displayed to the user.
  1068. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1069. * @param callback $function The function to be called to output the content for this page.
  1070. *
  1071. * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
  1072. */
  1073. function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1074. return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function );
  1075. }
  1076. /**
  1077. * Add sub menu page to the comments main menu.
  1078. *
  1079. * This function takes a capability which will be used to determine whether
  1080. * or not a page is included in the menu.
  1081. *
  1082. * The function which is hooked in to handle the output of the page must check
  1083. * that the user has the required capability as well.
  1084. *
  1085. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  1086. * @param string $menu_title The text to be used for the menu
  1087. * @param string $capability The capability required for this menu to be displayed to the user.
  1088. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1089. * @param callback $function The function to be called to output the content for this page.
  1090. *
  1091. * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
  1092. */
  1093. function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1094. return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1095. }
  1096. /**
  1097. * Remove a top level admin menu
  1098. *
  1099. * @since 3.1.0
  1100. *
  1101. * @param string $menu_slug The slug of the menu
  1102. * @return array|bool The removed menu on success, False if not found
  1103. */
  1104. function remove_menu_page( $menu_slug ) {
  1105. global $menu;
  1106. foreach ( $menu as $i => $item ) {
  1107. if ( $menu_slug == $item[2] ) {
  1108. unset( $menu[$i] );
  1109. return $item;
  1110. }
  1111. }
  1112. return false;
  1113. }
  1114. /**
  1115. * Remove an admin submenu
  1116. *
  1117. * @since 3.1.0
  1118. *
  1119. * @param string $menu_slug The slug for the parent menu
  1120. * @param string $submenu_slug The slug of the submenu
  1121. * @return array|bool The removed submenu on success, False if not found
  1122. */
  1123. function remove_submenu_page( $menu_slug, $submenu_slug ) {
  1124. global $submenu;
  1125. if ( !isset( $submenu[$menu_slug] ) )
  1126. return false;
  1127. foreach ( $submenu[$menu_slug] as $i => $item ) {
  1128. if ( $submenu_slug == $item[2] ) {
  1129. unset( $submenu[$menu_slug][$i] );
  1130. return $item;
  1131. }
  1132. }
  1133. return false;
  1134. }
  1135. /**
  1136. * Get the url to access a particular menu page based on the slug it was registered with.
  1137. *
  1138. * If the slug hasn't been registered properly no url will be returned
  1139. *
  1140. * @since 3.0
  1141. *
  1142. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1143. * @param bool $echo Whether or not to echo the url - default is true
  1144. * @return string the url
  1145. */
  1146. function menu_page_url($menu_slug, $echo = true) {

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