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

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

https://github.com/chrissiebrodigan/USC
PHP | 1557 lines | 795 code | 192 blank | 570 comment | 263 complexity | ddc067e4c5233073d7a206b13ed27979 MD5 | raw file

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

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