/wp-admin/includes/plugin.php

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