PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/plugin.php

https://gitlab.com/geyson/geyson
PHP | 1950 lines | 892 code | 229 blank | 829 comment | 284 complexity | dfa1b141111487e6ed0cdf28c4f9b50d MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.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. * /*
  17. * Plugin Name: Name of Plugin
  18. * Plugin URI: Link to plugin information
  19. * Description: Plugin Description
  20. * Author: Plugin author's name
  21. * Author URI: Link to the author's web site
  22. * Version: Must be set in the plugin for WordPress 2.3+
  23. * Text Domain: Optional. Unique identifier, should be same as the one used in
  24. * load_plugin_textdomain()
  25. * Domain Path: Optional. Only useful if the translations are located in a
  26. * folder above the plugin's base path. For example, if .mo files are
  27. * located in the locale folder then Domain Path will be "/locale/" and
  28. * must have the first slash. Defaults to the base folder the plugin is
  29. * located in.
  30. * Network: Optional. Specify "Network: true" to require that a plugin is activated
  31. * across all sites in an installation. This will prevent a plugin from being
  32. * activated on a single site when Multisite is enabled.
  33. * * / # Remove the space to close comment
  34. *
  35. * 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
  201. * and must be found in those two locations. It is recommended to keep your
  202. * plugin files in their own 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 = get_site_option( 'active_sitewide_plugins', array() );
  510. $current[$plugin] = time();
  511. update_site_option( 'active_sitewide_plugins', $current );
  512. } else {
  513. $current = get_option( 'active_plugins', array() );
  514. $current[] = $plugin;
  515. sort($current);
  516. update_option('active_plugins', $current);
  517. }
  518. if ( ! $silent ) {
  519. /**
  520. * Fires after a plugin has been activated.
  521. *
  522. * If a plugin is silently activated (such as during an update),
  523. * this hook does not fire.
  524. *
  525. * @since 2.9.0
  526. *
  527. * @param string $plugin Plugin path to main plugin file with plugin data.
  528. * @param bool $network_wide Whether to enable the plugin for all sites in the network
  529. * or just the current site. Multisite only. Default is false.
  530. */
  531. do_action( 'activated_plugin', $plugin, $network_wide );
  532. }
  533. if ( ob_get_length() > 0 ) {
  534. $output = ob_get_clean();
  535. return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
  536. }
  537. ob_end_clean();
  538. }
  539. return null;
  540. }
  541. /**
  542. * Deactivate a single plugin or multiple plugins.
  543. *
  544. * The deactivation hook is disabled by the plugin upgrader by using the $silent
  545. * parameter.
  546. *
  547. * @since 2.5.0
  548. *
  549. * @param string|array $plugins Single plugin or list of plugins to deactivate.
  550. * @param bool $silent Prevent calling deactivation hooks. Default is false.
  551. * @param mixed $network_wide Whether to deactivate the plugin for all sites in the network.
  552. * A value of null (the default) will deactivate plugins for both the site and the network.
  553. */
  554. function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) {
  555. if ( is_multisite() )
  556. $network_current = get_site_option( 'active_sitewide_plugins', array() );
  557. $current = get_option( 'active_plugins', array() );
  558. $do_blog = $do_network = false;
  559. foreach ( (array) $plugins as $plugin ) {
  560. $plugin = plugin_basename( trim( $plugin ) );
  561. if ( ! is_plugin_active($plugin) )
  562. continue;
  563. $network_deactivating = false !== $network_wide && is_plugin_active_for_network( $plugin );
  564. if ( ! $silent ) {
  565. /**
  566. * Fires before a plugin is deactivated.
  567. *
  568. * If a plugin is silently deactivated (such as during an update),
  569. * this hook does not fire.
  570. *
  571. * @since 2.9.0
  572. *
  573. * @param string $plugin Plugin path to main plugin file with plugin data.
  574. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
  575. * or just the current site. Multisite only. Default is false.
  576. */
  577. do_action( 'deactivate_plugin', $plugin, $network_deactivating );
  578. }
  579. if ( false !== $network_wide ) {
  580. if ( is_plugin_active_for_network( $plugin ) ) {
  581. $do_network = true;
  582. unset( $network_current[ $plugin ] );
  583. } elseif ( $network_wide ) {
  584. continue;
  585. }
  586. }
  587. if ( true !== $network_wide ) {
  588. $key = array_search( $plugin, $current );
  589. if ( false !== $key ) {
  590. $do_blog = true;
  591. unset( $current[ $key ] );
  592. }
  593. }
  594. if ( ! $silent ) {
  595. /**
  596. * Fires as a specific plugin is being deactivated.
  597. *
  598. * This hook is the "deactivation" hook used internally by
  599. * {@see register_deactivation_hook()}. The dynamic portion of the
  600. * hook name, `$plugin`, refers to the plugin basename.
  601. *
  602. * If a plugin is silently deactivated (such as during an update),
  603. * this hook does not fire.
  604. *
  605. * @since 2.0.0
  606. *
  607. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
  608. * or just the current site. Multisite only. Default is false.
  609. */
  610. do_action( 'deactivate_' . $plugin, $network_deactivating );
  611. /**
  612. * Fires after a plugin is deactivated.
  613. *
  614. * If a plugin is silently deactivated (such as during an update),
  615. * this hook does not fire.
  616. *
  617. * @since 2.9.0
  618. *
  619. * @param string $plugin Plugin basename.
  620. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
  621. * or just the current site. Multisite only. Default false.
  622. */
  623. do_action( 'deactivated_plugin', $plugin, $network_deactivating );
  624. }
  625. }
  626. if ( $do_blog )
  627. update_option('active_plugins', $current);
  628. if ( $do_network )
  629. update_site_option( 'active_sitewide_plugins', $network_current );
  630. }
  631. /**
  632. * Activate multiple plugins.
  633. *
  634. * When WP_Error is returned, it does not mean that one of the plugins had
  635. * errors. It means that one or more of the plugins file path was invalid.
  636. *
  637. * The execution will be halted as soon as one of the plugins has an error.
  638. *
  639. * @since 2.6.0
  640. *
  641. * @param string|array $plugins Single plugin or list of plugins to activate.
  642. * @param string $redirect Redirect to page after successful activation.
  643. * @param bool $network_wide Whether to enable the plugin for all sites in the network.
  644. * @param bool $silent Prevent calling activation hooks. Default is false.
  645. * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
  646. */
  647. function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
  648. if ( !is_array($plugins) )
  649. $plugins = array($plugins);
  650. $errors = array();
  651. foreach ( $plugins as $plugin ) {
  652. if ( !empty($redirect) )
  653. $redirect = add_query_arg('plugin', $plugin, $redirect);
  654. $result = activate_plugin($plugin, $redirect, $network_wide, $silent);
  655. if ( is_wp_error($result) )
  656. $errors[$plugin] = $result;
  657. }
  658. if ( !empty($errors) )
  659. return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
  660. return true;
  661. }
  662. /**
  663. * Remove directory and files of a plugin for a list of plugins.
  664. *
  665. * @since 2.6.0
  666. *
  667. * @global WP_Filesystem_Base $wp_filesystem
  668. *
  669. * @param array $plugins List of plugins to delete.
  670. * @param string $deprecated Deprecated.
  671. * @return bool|null|WP_Error True on success, false is $plugins is empty, WP_Error on failure.
  672. * Null if filesystem credentials are required to proceed.
  673. */
  674. function delete_plugins( $plugins, $deprecated = '' ) {
  675. global $wp_filesystem;
  676. if ( empty($plugins) )
  677. return false;
  678. $checked = array();
  679. foreach( $plugins as $plugin )
  680. $checked[] = 'checked[]=' . $plugin;
  681. ob_start();
  682. $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-plugins');
  683. if ( false === ($credentials = request_filesystem_credentials($url)) ) {
  684. $data = ob_get_clean();
  685. if ( ! empty($data) ){
  686. include_once( ABSPATH . 'wp-admin/admin-header.php');
  687. echo $data;
  688. include( ABSPATH . 'wp-admin/admin-footer.php');
  689. exit;
  690. }
  691. return;
  692. }
  693. if ( ! WP_Filesystem($credentials) ) {
  694. request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
  695. $data = ob_get_clean();
  696. if ( ! empty($data) ){
  697. include_once( ABSPATH . 'wp-admin/admin-header.php');
  698. echo $data;
  699. include( ABSPATH . 'wp-admin/admin-footer.php');
  700. exit;
  701. }
  702. return;
  703. }
  704. if ( ! is_object($wp_filesystem) )
  705. return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
  706. if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
  707. return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
  708. // Get the base plugin folder.
  709. $plugins_dir = $wp_filesystem->wp_plugins_dir();
  710. if ( empty( $plugins_dir ) ) {
  711. return new WP_Error( 'fs_no_plugins_dir', __( 'Unable to locate WordPress Plugin directory.' ) );
  712. }
  713. $plugins_dir = trailingslashit( $plugins_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 array();
  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. * @return true True if a plugin's uninstall.php file has been found and included.
  832. */
  833. function uninstall_plugin($plugin) {
  834. $file = plugin_basename($plugin);
  835. $uninstallable_plugins = (array) get_option('uninstall_plugins');
  836. if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
  837. if ( isset( $uninstallable_plugins[$file] ) ) {
  838. unset($uninstallable_plugins[$file]);
  839. update_option('uninstall_plugins', $uninstallable_plugins);
  840. }
  841. unset($uninstallable_plugins);
  842. define('WP_UNINSTALL_PLUGIN', $file);
  843. wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . dirname( $file ) );
  844. include( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' );
  845. return true;
  846. }
  847. if ( isset( $uninstallable_plugins[$file] ) ) {
  848. $callable = $uninstallable_plugins[$file];
  849. unset($uninstallable_plugins[$file]);
  850. update_option('uninstall_plugins', $uninstallable_plugins);
  851. unset($uninstallable_plugins);
  852. wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
  853. include( WP_PLUGIN_DIR . '/' . $file );
  854. add_action( 'uninstall_' . $file, $callable );
  855. /**
  856. * Fires in uninstall_plugin() once the plugin has been uninstalled.
  857. *
  858. * The action concatenates the 'uninstall_' prefix with the basename of the
  859. * plugin passed to {@see uninstall_plugin()} to create a dynamically-named action.
  860. *
  861. * @since 2.7.0
  862. */
  863. do_action( 'uninstall_' . $file );
  864. }
  865. }
  866. //
  867. // Menu
  868. //
  869. /**
  870. * Add a top level menu page
  871. *
  872. * This function takes a capability which will be used to determine whether
  873. * or not a page is included in the menu.
  874. *
  875. * The function which is hooked in to handle the output of the page must check
  876. * that the user has the required capability as well.
  877. *
  878. * @global array $menu
  879. * @global array $admin_page_hooks
  880. * @global array $_registered_pages
  881. * @global array $_parent_pages
  882. *
  883. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  884. * @param string $menu_title The text to be used for the menu
  885. * @param string $capability The capability required for this menu to be displayed to the user.
  886. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  887. * @param callback $function The function to be called to output the content for this page.
  888. * @param string $icon_url The url to the icon to be used for this menu.
  889. * * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme.
  890. * This should begin with 'data:image/svg+xml;base64,'.
  891. * * Pass the name of a Dashicons helper class to use a font icon, e.g. 'dashicons-chart-pie'.
  892. * * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.
  893. * @param int $position The position in the menu order this one should appear
  894. *
  895. * @return string The resulting page's hook_suffix
  896. */
  897. function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) {
  898. global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
  899. $menu_slug = plugin_basename( $menu_slug );
  900. $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title );
  901. $hookname = get_plugin_page_hookname( $menu_slug, '' );
  902. if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) )
  903. add_action( $hookname, $function );
  904. if ( empty($icon_url) ) {
  905. $icon_url = 'dashicons-admin-generic';
  906. $icon_class = 'menu-icon-generic ';
  907. } else {
  908. $icon_url = set_url_scheme( $icon_url );
  909. $icon_class = '';
  910. }
  911. $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url );
  912. if ( null === $position )
  913. $menu[] = $new_menu;
  914. else
  915. $menu[$position] = $new_menu;
  916. $_registered_pages[$hookname] = true;
  917. // No parent as top level
  918. $_parent_pages[$menu_slug] = false;
  919. return $hookname;
  920. }
  921. /**
  922. * Add a top level menu page in the 'objects' section
  923. *
  924. * This function takes a capability which will be used to determine whether
  925. * or not a page is included in the menu.
  926. *
  927. * The function which is hooked in to handle the output of the page must check
  928. * that the user has the required capability as well.
  929. *
  930. * @global int $_wp_last_object_menu
  931. *
  932. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  933. * @param string $menu_title The text to be used for the menu
  934. * @param string $capability The capability required for this menu to be displayed to the user.
  935. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  936. * @param callback $function The function to be called to output the content for this page.
  937. * @param string $icon_url The url to the icon to be used for this menu
  938. *
  939. * @return string The resulting page's hook_suffix
  940. */
  941. function add_object_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
  942. global $_wp_last_object_menu;
  943. $_wp_last_object_menu++;
  944. return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_object_menu);
  945. }
  946. /**
  947. * Add a top level menu page in the 'utility' section
  948. *
  949. * This function takes a capability which will be used to determine whether
  950. * or not a page is included in the menu.
  951. *
  952. * The function which is hooked in to handle the output of the page must check
  953. * that the user has the required capability as well.
  954. *
  955. * @global int $_wp_last_utility_menu
  956. *
  957. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  958. * @param string $menu_title The text to be used for the menu
  959. * @param string $capability The capability required for this menu to be displayed to the user.
  960. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  961. * @param callback $function The function to be called to output the content for this page.
  962. * @param string $icon_url The url to the icon to be used for this menu
  963. *
  964. * @return string The resulting page's hook_suffix
  965. */
  966. function add_utility_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
  967. global $_wp_last_utility_menu;
  968. $_wp_last_utility_menu++;
  969. return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_utility_menu);
  970. }
  971. /**
  972. * Add a sub menu page
  973. *
  974. * This function takes a capability which will be used to determine whether
  975. * or not a page is included in the menu.
  976. *
  977. * The function which is hooked in to handle the output of the page must check
  978. * that the user has the required capability as well.
  979. *
  980. * @global array $submenu
  981. * @global array $menu
  982. * @global type $_wp_real_parent_file
  983. * @global bool $_wp_submenu_nopriv
  984. * @global array $_registered_pages
  985. * @global array $_parent_pages
  986. *
  987. * @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page)
  988. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  989. * @param string $menu_title The text to be used for the menu
  990. * @param string $capability The capability required for this menu to be displayed to the user.
  991. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  992. * @param callback $function The function to be called to output the content for this page.
  993. *
  994. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  995. */
  996. function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  997. global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv,
  998. $_registered_pages, $_parent_pages;
  999. $menu_slug = plugin_basename( $menu_slug );
  1000. $parent_slug = plugin_basename( $parent_slug);
  1001. if ( isset( $_wp_real_parent_file[$parent_slug] ) )
  1002. $parent_slug = $_wp_real_parent_file[$parent_slug];
  1003. if ( !current_user_can( $capability ) ) {
  1004. $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true;
  1005. return false;
  1006. }
  1007. /*
  1008. * If the parent doesn't already have a submenu, add a link to the parent
  1009. * as the first item in the submenu. If the submenu file is the same as the
  1010. * parent file someone is trying to link back to the parent manually. In
  1011. * this case, don't automatically add a link back to avoid duplication.
  1012. */
  1013. if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) {
  1014. foreach ( (array)$menu as $parent_menu ) {
  1015. if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) )
  1016. $submenu[$parent_slug][] = array_slice( $parent_menu, 0, 4 );
  1017. }
  1018. }
  1019. $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title );
  1020. $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);
  1021. if (!empty ( $function ) && !empty ( $hookname ))
  1022. add_action( $hookname, $function );
  1023. $_registered_pages[$hookname] = true;
  1024. /*
  1025. * Backward-compatibility for plugins using add_management page.
  1026. * See wp-admin/admin.php for redirect from edit.php to tools.php
  1027. */
  1028. if ( 'tools.php' == $parent_slug )
  1029. $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true;
  1030. // No parent as top level.
  1031. $_parent_pages[$menu_slug] = $parent_slug;
  1032. return $hookname;
  1033. }
  1034. /**
  1035. * Add sub menu page to the tools main menu.
  1036. *
  1037. * This function takes a capability which will be used to determine whether
  1038. * or not a page is included in the menu.
  1039. *
  1040. * The function which is hooked in to handle the output of the page must check
  1041. * that the user has the required capability as well.
  1042. *
  1043. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  1044. * @param string $menu_title The text to be used for the menu
  1045. * @param string $capability The capability required for this menu to be displayed to the user.
  1046. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1047. * @param callback $function The function to be called to output the content for this page.
  1048. *
  1049. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1050. */
  1051. function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1052. return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1053. }
  1054. /**
  1055. * Add sub menu page to the options main menu.
  1056. *
  1057. * This function takes a capability which will be used to determine whether
  1058. * or not a page is included in the menu.
  1059. *
  1060. * The function which is hooked in to handle the output of the page must check
  1061. * that the user has the required capability as well.
  1062. *
  1063. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  1064. * @param string $menu_title The text to be used for the menu
  1065. * @param string $capability The capability required for this menu to be displayed to the user.
  1066. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1067. * @param callback $function The function to be called to output the content for this page.
  1068. *
  1069. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1070. */
  1071. function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1072. return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1073. }
  1074. /**
  1075. * Add sub menu page to the themes main menu.
  1076. *
  1077. * This function takes a capability which will be used to determine whether
  1078. * or not a page is included in the menu.
  1079. *
  1080. * The function which is hooked in to handle the output of the page must check
  1081. * that the user has the required capability as well.
  1082. *
  1083. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  1084. * @param string $menu_title The text to be used for the menu
  1085. * @param string $capability The capability required for this menu to be displayed to the user.
  1086. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1087. * @param callback $function The function to be called to output the content for this page.
  1088. *
  1089. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1090. */
  1091. function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1092. return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1093. }
  1094. /**
  1095. * Add sub menu page to the plugins main menu.
  1096. *
  1097. * This function takes a capability which will be used to determine whether
  1098. * or not a page is included in the menu.
  1099. *
  1100. * The function which is hooked in to handle the output of the page must check
  1101. * that the user has the required capability as well.
  1102. *
  1103. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  1104. * @param string $menu_title The text to be used for the menu
  1105. * @param string $capability The capability required for this menu to be displayed to the user.
  1106. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1107. * @param callback $function The function to be called to output the content for this page.
  1108. *
  1109. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1110. */
  1111. function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1112. return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1113. }
  1114. /**
  1115. * Add sub menu page to the Users/Profile main menu.
  1116. *
  1117. * This function takes a capability which will be used to determine whether
  1118. * or not a page is included in the menu.
  1119. *
  1120. * The function which is hooked in to handle the output of the page must check
  1121. * that the user has the required capability as well.
  1122. *
  1123. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  1124. * @param string $menu_title The text to be used for the menu
  1125. * @param string $capability The capability required for this menu to be displayed to the user.
  1126. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1127. * @param callback $function The function to be called to output the content for this page.
  1128. *
  1129. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1130. */
  1131. function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1132. if ( current_user_can('edit_users') )
  1133. $parent = 'users.php';
  1134. else
  1135. $parent = 'profile.php';
  1136. return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
  1137. }
  1138. /**
  1139. * Add sub menu page to the Dashboard main menu.
  1140. *
  1141. * This function takes a capability which will be used to determine whether
  1142. * or not a page is included in the menu.
  1143. *
  1144. * The function which is hooked in to handle the output of the page must check
  1145. * that the user has the required capability as well.
  1146. *
  1147. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  1148. * @param string $menu_title The text to be used for the menu
  1149. * @param string $capability The capability required for this menu to be displayed to the user.
  1150. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1151. * @param callback $function The function to be called to output the content for this page.
  1152. *
  1153. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1154. */
  1155. function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1156. return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1157. }
  1158. /**
  1159. * Add sub menu page to the posts main menu.
  1160. *
  1161. * This function takes a capability which will be used to determine whether
  1162. * or not a page is included in the menu.
  1163. *
  1164. * The function which is hooked in to handle the output of the page must check
  1165. * that the user has the required capability as well.
  1166. *
  1167. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  1168. * @param string $menu_title The text to be used for the menu
  1169. * @param string $capability The capability required for this menu to be displayed to the user.
  1170. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1171. * @param callback $function The function to be called to output the content for this page.
  1172. *
  1173. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1174. */
  1175. function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1176. return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1177. }
  1178. /**
  1179. * Add sub menu page to the media main menu.
  1180. *
  1181. * This function takes a capability which will be used to determine whether
  1182. * or not a page is included in the menu.
  1183. *
  1184. * The function which is hooked in to handle the output of the page must check
  1185. * that the user has the required capability as well.
  1186. *
  1187. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
  1188. * @param string $menu_title The text to be used for the menu

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