PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/plugin.php

https://github.com/schr/wordpress
PHP | 1111 lines | 646 code | 160 blank | 305 comment | 206 complexity | 19c695ebc5c1fff194835e2f6c4b9f53 MD5 | raw 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. * * / # Remove the space to close comment
  32. * </code>
  33. *
  34. * Plugin data returned array contains the following:
  35. * 'Name' - Name of the plugin, must be unique.
  36. * 'Title' - Title of the plugin and the link to the plugin's web site.
  37. * 'Description' - Description of what the plugin does and/or notes
  38. * from the author.
  39. * 'Author' - The author's name
  40. * 'AuthorURI' - The authors web site address.
  41. * 'Version' - The plugin version number.
  42. * 'PluginURI' - Plugin web site address.
  43. * 'TextDomain' - Plugin's text domain for localization.
  44. * 'DomainPath' - Plugin's relative directory path to .mo files.
  45. *
  46. * Some users have issues with opening large files and manipulating the contents
  47. * for want is usually the first 1kiB or 2kiB. This function stops pulling in
  48. * the plugin contents when it has all of the required plugin data.
  49. *
  50. * The first 8kiB of the file will be pulled in and if the plugin data is not
  51. * within that first 8kiB, then the plugin author should correct their plugin
  52. * and move the plugin data headers to the top.
  53. *
  54. * The plugin file is assumed to have permissions to allow for scripts to read
  55. * the file. This is not checked however and the file is only opened for
  56. * reading.
  57. *
  58. * @link http://trac.wordpress.org/ticket/5651 Previous Optimizations.
  59. * @link http://trac.wordpress.org/ticket/7372 Further and better Optimizations.
  60. * @since 1.5.0
  61. *
  62. * @param string $plugin_file Path to the plugin file
  63. * @param bool $markup If the returned data should have HTML markup applied
  64. * @param bool $translate If the returned data should be translated
  65. * @return array See above for description.
  66. */
  67. function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
  68. // We don't need to write to the file, so just open for reading.
  69. $fp = fopen($plugin_file, 'r');
  70. // Pull only the first 8kiB of the file in.
  71. $plugin_data = fread( $fp, 8192 );
  72. // PHP will close file handle, but we are good citizens.
  73. fclose($fp);
  74. preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $name );
  75. preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $uri );
  76. preg_match( '|Version:(.*)|i', $plugin_data, $version );
  77. preg_match( '|Description:(.*)$|mi', $plugin_data, $description );
  78. preg_match( '|Author:(.*)$|mi', $plugin_data, $author_name );
  79. preg_match( '|Author URI:(.*)$|mi', $plugin_data, $author_uri );
  80. preg_match( '|Text Domain:(.*)$|mi', $plugin_data, $text_domain );
  81. preg_match( '|Domain Path:(.*)$|mi', $plugin_data, $domain_path );
  82. foreach ( array( 'name', 'uri', 'version', 'description', 'author_name', 'author_uri', 'text_domain', 'domain_path' ) as $field ) {
  83. if ( !empty( ${$field} ) )
  84. ${$field} = trim(${$field}[1]);
  85. else
  86. ${$field} = '';
  87. }
  88. $plugin_data = array(
  89. 'Name' => $name, 'Title' => $name, 'PluginURI' => $uri, 'Description' => $description,
  90. 'Author' => $author_name, 'AuthorURI' => $author_uri, 'Version' => $version,
  91. 'TextDomain' => $text_domain, 'DomainPath' => $domain_path
  92. );
  93. if ( $markup || $translate )
  94. $plugin_data = _get_plugin_data_markup_translate($plugin_data, $markup, $translate);
  95. return $plugin_data;
  96. }
  97. function _get_plugin_data_markup_translate($plugin_data, $markup = true, $translate = true) {
  98. //Translate fields
  99. if( $translate && ! empty($plugin_data['TextDomain']) ) {
  100. if( ! empty( $plugin_data['DomainPath'] ) )
  101. load_plugin_textdomain($plugin_data['TextDomain'], dirname($plugin_file). $plugin_data['DomainPath']);
  102. else
  103. load_plugin_textdomain($plugin_data['TextDomain'], dirname($plugin_file));
  104. foreach ( array('Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version') as $field )
  105. $plugin_data[ $field ] = translate($plugin_data[ $field ], $plugin_data['TextDomain']);
  106. }
  107. //Apply Markup
  108. if ( $markup ) {
  109. if ( ! empty($plugin_data['PluginURI']) && ! empty($plugin_data['Name']) )
  110. $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . __( 'Visit plugin homepage' ) . '">' . $plugin_data['Name'] . '</a>';
  111. else
  112. $plugin_data['Title'] = $plugin_data['Name'];
  113. if ( ! empty($plugin_data['AuthorURI']) && ! empty($plugin_data['Author']) )
  114. $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . __( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>';
  115. $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
  116. if( ! empty($plugin_data['Author']) )
  117. $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s'), $plugin_data['Author'] ) . '.</cite>';
  118. }
  119. $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
  120. // Sanitize all displayed data
  121. $plugin_data['Title'] = wp_kses($plugin_data['Title'], $plugins_allowedtags);
  122. $plugin_data['Version'] = wp_kses($plugin_data['Version'], $plugins_allowedtags);
  123. $plugin_data['Description'] = wp_kses($plugin_data['Description'], $plugins_allowedtags);
  124. $plugin_data['Author'] = wp_kses($plugin_data['Author'], $plugins_allowedtags);
  125. return $plugin_data;
  126. }
  127. /**
  128. * Get a list of a plugin's files.
  129. *
  130. * @since 2.8.0
  131. *
  132. * @param string $plugin Plugin ID
  133. * @return array List of files relative to the plugin root.
  134. */
  135. function get_plugin_files($plugin) {
  136. $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
  137. $dir = dirname($plugin_file);
  138. $plugin_files = array($plugin);
  139. if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) {
  140. $plugins_dir = @ opendir( $dir );
  141. if ( $plugins_dir ) {
  142. while (($file = readdir( $plugins_dir ) ) !== false ) {
  143. if ( substr($file, 0, 1) == '.' )
  144. continue;
  145. if ( is_dir( $dir . '/' . $file ) ) {
  146. $plugins_subdir = @ opendir( $dir . '/' . $file );
  147. if ( $plugins_subdir ) {
  148. while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
  149. if ( substr($subfile, 0, 1) == '.' )
  150. continue;
  151. $plugin_files[] = plugin_basename("$dir/$file/$subfile");
  152. }
  153. @closedir( $plugins_subdir );
  154. }
  155. } else {
  156. if ( plugin_basename("$dir/$file") != $plugin )
  157. $plugin_files[] = plugin_basename("$dir/$file");
  158. }
  159. }
  160. @closedir( $plugins_dir );
  161. }
  162. }
  163. return $plugin_files;
  164. }
  165. /**
  166. * Check the plugins directory and retrieve all plugin files with plugin data.
  167. *
  168. * WordPress only supports plugin files in the base plugins directory
  169. * (wp-content/plugins) and in one directory above the plugins directory
  170. * (wp-content/plugins/my-plugin). The file it looks for has the plugin data and
  171. * must be found in those two locations. It is recommended that do keep your
  172. * plugin files in directories.
  173. *
  174. * The file with the plugin data is the file that will be included and therefore
  175. * needs to have the main execution for the plugin. This does not mean
  176. * everything must be contained in the file and it is recommended that the file
  177. * be split for maintainability. Keep everything in one file for extreme
  178. * optimization purposes.
  179. *
  180. * @since unknown
  181. *
  182. * @param string $plugin_folder Optional. Relative path to single plugin folder.
  183. * @return array Key is the plugin file path and the value is an array of the plugin data.
  184. */
  185. function get_plugins($plugin_folder = '') {
  186. if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
  187. $cache_plugins = array();
  188. if ( isset($cache_plugins[ $plugin_folder ]) )
  189. return $cache_plugins[ $plugin_folder ];
  190. $wp_plugins = array ();
  191. $plugin_root = WP_PLUGIN_DIR;
  192. if( !empty($plugin_folder) )
  193. $plugin_root .= $plugin_folder;
  194. // Files in wp-content/plugins directory
  195. $plugins_dir = @ opendir( $plugin_root);
  196. if ( $plugins_dir ) {
  197. while (($file = readdir( $plugins_dir ) ) !== false ) {
  198. if ( substr($file, 0, 1) == '.' )
  199. continue;
  200. if ( is_dir( $plugin_root.'/'.$file ) ) {
  201. $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
  202. if ( $plugins_subdir ) {
  203. while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
  204. if ( substr($subfile, 0, 1) == '.' )
  205. continue;
  206. if ( substr($subfile, -4) == '.php' )
  207. $plugin_files[] = "$file/$subfile";
  208. }
  209. }
  210. } else {
  211. if ( substr($file, -4) == '.php' )
  212. $plugin_files[] = $file;
  213. }
  214. }
  215. }
  216. @closedir( $plugins_dir );
  217. @closedir( $plugins_subdir );
  218. if ( !$plugins_dir || !$plugin_files )
  219. return $wp_plugins;
  220. foreach ( $plugin_files as $plugin_file ) {
  221. if ( !is_readable( "$plugin_root/$plugin_file" ) )
  222. continue;
  223. $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
  224. if ( empty ( $plugin_data['Name'] ) )
  225. continue;
  226. $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
  227. }
  228. uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
  229. $cache_plugins[ $plugin_folder ] = $wp_plugins;
  230. wp_cache_set('plugins', $cache_plugins, 'plugins');
  231. return $wp_plugins;
  232. }
  233. /**
  234. * Check whether the plugin is active by checking the active_plugins list.
  235. *
  236. * @since 2.5.0
  237. *
  238. * @param string $plugin Base plugin path from plugins directory.
  239. * @return bool True, if in the active plugins list. False, not in the list.
  240. */
  241. function is_plugin_active($plugin) {
  242. return in_array($plugin, get_option('active_plugins'));
  243. }
  244. /**
  245. * Attempts activation of plugin in a "sandbox" and redirects on success.
  246. *
  247. * A plugin that is already activated will not attempt to be activated again.
  248. *
  249. * The way it works is by setting the redirection to the error before trying to
  250. * include the plugin file. If the plugin fails, then the redirection will not
  251. * be overwritten with the success message. Also, the options will not be
  252. * updated and the activation hook will not be called on plugin error.
  253. *
  254. * It should be noted that in no way the below code will actually prevent errors
  255. * within the file. The code should not be used elsewhere to replicate the
  256. * "sandbox", which uses redirection to work.
  257. * {@source 13 1}
  258. *
  259. * If any errors are found or text is outputted, then it will be captured to
  260. * ensure that the success redirection will update the error redirection.
  261. *
  262. * @since unknown
  263. *
  264. * @param string $plugin Plugin path to main plugin file with plugin data.
  265. * @param string $redirect Optional. URL to redirect to.
  266. * @return WP_Error|null WP_Error on invalid file or null on success.
  267. */
  268. function activate_plugin($plugin, $redirect = '') {
  269. $current = get_option('active_plugins');
  270. $plugin = plugin_basename(trim($plugin));
  271. $valid = validate_plugin($plugin);
  272. if ( is_wp_error($valid) )
  273. return $valid;
  274. if ( !in_array($plugin, $current) ) {
  275. if ( !empty($redirect) )
  276. 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
  277. ob_start();
  278. @include(WP_PLUGIN_DIR . '/' . $plugin);
  279. $current[] = $plugin;
  280. sort($current);
  281. update_option('active_plugins', $current);
  282. do_action('activate_' . $plugin);
  283. ob_end_clean();
  284. }
  285. return null;
  286. }
  287. /**
  288. * Deactivate a single plugin or multiple plugins.
  289. *
  290. * The deactivation hook is disabled by the plugin upgrader by using the $silent
  291. * parameter.
  292. *
  293. * @since unknown
  294. *
  295. * @param string|array $plugins Single plugin or list of plugins to deactivate.
  296. * @param bool $silent Optional, default is false. Prevent calling deactivate hook.
  297. */
  298. function deactivate_plugins($plugins, $silent= false) {
  299. $current = get_option('active_plugins');
  300. if ( !is_array($plugins) )
  301. $plugins = array($plugins);
  302. foreach ( $plugins as $plugin ) {
  303. $plugin = plugin_basename($plugin);
  304. if( ! is_plugin_active($plugin) )
  305. continue;
  306. array_splice($current, array_search( $plugin, $current), 1 ); // Fixed Array-fu!
  307. if ( ! $silent ) //Used by Plugin updater to internally deactivate plugin, however, not to notify plugins of the fact to prevent plugin output.
  308. do_action('deactivate_' . trim( $plugin ));
  309. }
  310. update_option('active_plugins', $current);
  311. }
  312. /**
  313. * Activate multiple plugins.
  314. *
  315. * When WP_Error is returned, it does not mean that one of the plugins had
  316. * errors. It means that one or more of the plugins file path was invalid.
  317. *
  318. * The execution will be halted as soon as one of the plugins has an error.
  319. *
  320. * @since unknown
  321. *
  322. * @param string|array $plugins
  323. * @param string $redirect Redirect to page after successful activation.
  324. * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
  325. */
  326. function activate_plugins($plugins, $redirect = '') {
  327. if ( !is_array($plugins) )
  328. $plugins = array($plugins);
  329. $errors = array();
  330. foreach ( (array) $plugins as $plugin ) {
  331. if ( !empty($redirect) )
  332. $redirect = add_query_arg('plugin', $plugin, $redirect);
  333. $result = activate_plugin($plugin, $redirect);
  334. if ( is_wp_error($result) )
  335. $errors[$plugin] = $result;
  336. }
  337. if ( !empty($errors) )
  338. return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
  339. return true;
  340. }
  341. /**
  342. * Remove directory and files of a plugin for a single or list of plugin(s).
  343. *
  344. * If the plugins parameter list is empty, false will be returned. True when
  345. * completed.
  346. *
  347. * @since unknown
  348. *
  349. * @param array $plugins List of plugin
  350. * @param string $redirect Redirect to page when complete.
  351. * @return mixed
  352. */
  353. function delete_plugins($plugins, $redirect = '' ) {
  354. global $wp_filesystem;
  355. if( empty($plugins) )
  356. return false;
  357. $checked = array();
  358. foreach( $plugins as $plugin )
  359. $checked[] = 'checked[]=' . $plugin;
  360. ob_start();
  361. $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-manage-plugins');
  362. if ( false === ($credentials = request_filesystem_credentials($url)) ) {
  363. $data = ob_get_contents();
  364. ob_end_clean();
  365. if( ! empty($data) ){
  366. include_once( ABSPATH . 'wp-admin/admin-header.php');
  367. echo $data;
  368. include( ABSPATH . 'wp-admin/admin-footer.php');
  369. exit;
  370. }
  371. return;
  372. }
  373. if ( ! WP_Filesystem($credentials) ) {
  374. request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
  375. $data = ob_get_contents();
  376. ob_end_clean();
  377. if( ! empty($data) ){
  378. include_once( ABSPATH . 'wp-admin/admin-header.php');
  379. echo $data;
  380. include( ABSPATH . 'wp-admin/admin-footer.php');
  381. exit;
  382. }
  383. return;
  384. }
  385. if ( $wp_filesystem->errors->get_error_code() ) {
  386. return $wp_filesystem->errors;
  387. }
  388. if ( ! is_object($wp_filesystem) )
  389. return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
  390. if ( $wp_filesystem->errors->get_error_code() )
  391. return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
  392. //Get the base plugin folder
  393. $plugins_dir = $wp_filesystem->wp_plugins_dir();
  394. if ( empty($plugins_dir) )
  395. return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
  396. $plugins_dir = trailingslashit( $plugins_dir );
  397. $errors = array();
  398. foreach( $plugins as $plugin_file ) {
  399. // Run Uninstall hook
  400. if ( is_uninstallable_plugin( $plugin_file ) )
  401. uninstall_plugin($plugin_file);
  402. $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
  403. // If plugin is in its own directory, recursively delete the directory.
  404. 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
  405. $deleted = $wp_filesystem->delete($this_plugin_dir, true);
  406. else
  407. $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
  408. if ( ! $deleted )
  409. $errors[] = $plugin_file;
  410. }
  411. if ( ! empty($errors) )
  412. return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s'), implode(', ', $errors)) );
  413. // Force refresh of plugin update information
  414. delete_transient('update_plugins');
  415. return true;
  416. }
  417. function validate_active_plugins() {
  418. $check_plugins = get_option('active_plugins');
  419. // Sanity check. If the active plugin list is not an array, make it an
  420. // empty array.
  421. if ( !is_array($check_plugins) ) {
  422. update_option('active_plugins', array());
  423. return;
  424. }
  425. //Invalid is any plugin that is deactivated due to error.
  426. $invalid = array();
  427. // If a plugin file does not exist, remove it from the list of active
  428. // plugins.
  429. foreach ( $check_plugins as $check_plugin ) {
  430. $result = validate_plugin($check_plugin);
  431. if ( is_wp_error( $result ) ) {
  432. $invalid[$check_plugin] = $result;
  433. deactivate_plugins( $check_plugin, true);
  434. }
  435. }
  436. return $invalid;
  437. }
  438. /**
  439. * Validate the plugin path.
  440. *
  441. * Checks that the file exists and {@link validate_file() is valid file}.
  442. *
  443. * @since unknown
  444. *
  445. * @param string $plugin Plugin Path
  446. * @return WP_Error|int 0 on success, WP_Error on failure.
  447. */
  448. function validate_plugin($plugin) {
  449. if ( validate_file($plugin) )
  450. return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
  451. if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
  452. return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
  453. $installed_plugins = get_plugins();
  454. if ( ! isset($installed_plugins[$plugin]) )
  455. return new WP_Error('no_plugin_header', __('The plugin does not have a valid header.'));
  456. return 0;
  457. }
  458. /**
  459. * Whether the plugin can be uninstalled.
  460. *
  461. * @since 2.7.0
  462. *
  463. * @param string $plugin Plugin path to check.
  464. * @return bool Whether plugin can be uninstalled.
  465. */
  466. function is_uninstallable_plugin($plugin) {
  467. $file = plugin_basename($plugin);
  468. $uninstallable_plugins = (array) get_option('uninstall_plugins');
  469. if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
  470. return true;
  471. return false;
  472. }
  473. /**
  474. * Uninstall a single plugin.
  475. *
  476. * Calls the uninstall hook, if it is available.
  477. *
  478. * @since 2.7.0
  479. *
  480. * @param string $plugin Relative plugin path from Plugin Directory.
  481. */
  482. function uninstall_plugin($plugin) {
  483. $file = plugin_basename($plugin);
  484. $uninstallable_plugins = (array) get_option('uninstall_plugins');
  485. if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
  486. if ( isset( $uninstallable_plugins[$file] ) ) {
  487. unset($uninstallable_plugins[$file]);
  488. update_option('uninstall_plugins', $uninstallable_plugins);
  489. }
  490. unset($uninstallable_plugins);
  491. define('WP_UNINSTALL_PLUGIN', $file);
  492. include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
  493. return true;
  494. }
  495. if ( isset( $uninstallable_plugins[$file] ) ) {
  496. $callable = $uninstallable_plugins[$file];
  497. unset($uninstallable_plugins[$file]);
  498. update_option('uninstall_plugins', $uninstallable_plugins);
  499. unset($uninstallable_plugins);
  500. include WP_PLUGIN_DIR . '/' . $file;
  501. add_action( 'uninstall_' . $file, $callable );
  502. do_action( 'uninstall_' . $file );
  503. }
  504. }
  505. //
  506. // Menu
  507. //
  508. function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '' ) {
  509. global $menu, $admin_page_hooks;
  510. $file = plugin_basename( $file );
  511. $admin_page_hooks[$file] = sanitize_title( $menu_title );
  512. $hookname = get_plugin_page_hookname( $file, '' );
  513. if (!empty ( $function ) && !empty ( $hookname ))
  514. add_action( $hookname, $function );
  515. if ( empty($icon_url) )
  516. $icon_url = 'images/generic.png';
  517. $menu[] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
  518. return $hookname;
  519. }
  520. function add_object_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
  521. global $menu, $admin_page_hooks, $_wp_last_object_menu;
  522. $file = plugin_basename( $file );
  523. $admin_page_hooks[$file] = sanitize_title( $menu_title );
  524. $hookname = get_plugin_page_hookname( $file, '' );
  525. if (!empty ( $function ) && !empty ( $hookname ))
  526. add_action( $hookname, $function );
  527. if ( empty($icon_url) )
  528. $icon_url = 'images/generic.png';
  529. $_wp_last_object_menu++;
  530. $menu[$_wp_last_object_menu] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
  531. return $hookname;
  532. }
  533. function add_utility_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
  534. global $menu, $admin_page_hooks, $_wp_last_utility_menu;
  535. $file = plugin_basename( $file );
  536. $admin_page_hooks[$file] = sanitize_title( $menu_title );
  537. $hookname = get_plugin_page_hookname( $file, '' );
  538. if (!empty ( $function ) && !empty ( $hookname ))
  539. add_action( $hookname, $function );
  540. if ( empty($icon_url) )
  541. $icon_url = 'images/generic.png';
  542. $_wp_last_utility_menu++;
  543. $menu[$_wp_last_utility_menu] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
  544. return $hookname;
  545. }
  546. function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
  547. global $submenu;
  548. global $menu;
  549. global $_wp_real_parent_file;
  550. global $_wp_submenu_nopriv;
  551. $file = plugin_basename( $file );
  552. $parent = plugin_basename( $parent);
  553. if ( isset( $_wp_real_parent_file[$parent] ) )
  554. $parent = $_wp_real_parent_file[$parent];
  555. if ( !current_user_can( $access_level ) ) {
  556. $_wp_submenu_nopriv[$parent][$file] = true;
  557. return false;
  558. }
  559. // If the parent doesn't already have a submenu, add a link to the parent
  560. // as the first item in the submenu. If the submenu file is the same as the
  561. // parent file someone is trying to link back to the parent manually. In
  562. // this case, don't automatically add a link back to avoid duplication.
  563. if (!isset( $submenu[$parent] ) && $file != $parent ) {
  564. foreach ( (array)$menu as $parent_menu ) {
  565. if ( $parent_menu[2] == $parent && current_user_can( $parent_menu[1] ) )
  566. $submenu[$parent][] = $parent_menu;
  567. }
  568. }
  569. $submenu[$parent][] = array ( $menu_title, $access_level, $file, $page_title );
  570. $hookname = get_plugin_page_hookname( $file, $parent);
  571. if (!empty ( $function ) && !empty ( $hookname ))
  572. add_action( $hookname, $function );
  573. return $hookname;
  574. }
  575. /**
  576. * Add sub menu page to the tools main menu.
  577. *
  578. * @param string $page_title
  579. * @param unknown_type $menu_title
  580. * @param unknown_type $access_level
  581. * @param unknown_type $file
  582. * @param unknown_type $function
  583. * @return unknown
  584. */
  585. function add_management_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  586. return add_submenu_page( 'tools.php', $page_title, $menu_title, $access_level, $file, $function );
  587. }
  588. function add_options_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  589. return add_submenu_page( 'options-general.php', $page_title, $menu_title, $access_level, $file, $function );
  590. }
  591. function add_theme_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  592. return add_submenu_page( 'themes.php', $page_title, $menu_title, $access_level, $file, $function );
  593. }
  594. function add_users_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  595. if ( current_user_can('edit_users') )
  596. $parent = 'users.php';
  597. else
  598. $parent = 'profile.php';
  599. return add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function );
  600. }
  601. function add_dashboard_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  602. return add_submenu_page( 'index.php', $page_title, $menu_title, $access_level, $file, $function );
  603. }
  604. function add_posts_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  605. return add_submenu_page( 'edit.php', $page_title, $menu_title, $access_level, $file, $function );
  606. }
  607. function add_media_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  608. return add_submenu_page( 'upload.php', $page_title, $menu_title, $access_level, $file, $function );
  609. }
  610. function add_links_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  611. return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $access_level, $file, $function );
  612. }
  613. function add_pages_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  614. return add_submenu_page( 'edit-pages.php', $page_title, $menu_title, $access_level, $file, $function );
  615. }
  616. function add_comments_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  617. return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $access_level, $file, $function );
  618. }
  619. //
  620. // Pluggable Menu Support -- Private
  621. //
  622. function get_admin_page_parent( $parent = '' ) {
  623. global $parent_file;
  624. global $menu;
  625. global $submenu;
  626. global $pagenow;
  627. global $plugin_page;
  628. global $_wp_real_parent_file;
  629. global $_wp_menu_nopriv;
  630. global $_wp_submenu_nopriv;
  631. if ( !empty ( $parent ) && 'admin.php' != $parent ) {
  632. if ( isset( $_wp_real_parent_file[$parent] ) )
  633. $parent = $_wp_real_parent_file[$parent];
  634. return $parent;
  635. }
  636. /*
  637. if ( !empty ( $parent_file ) ) {
  638. if ( isset( $_wp_real_parent_file[$parent_file] ) )
  639. $parent_file = $_wp_real_parent_file[$parent_file];
  640. return $parent_file;
  641. }
  642. */
  643. if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
  644. foreach ( (array)$menu as $parent_menu ) {
  645. if ( $parent_menu[2] == $plugin_page ) {
  646. $parent_file = $plugin_page;
  647. if ( isset( $_wp_real_parent_file[$parent_file] ) )
  648. $parent_file = $_wp_real_parent_file[$parent_file];
  649. return $parent_file;
  650. }
  651. }
  652. if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
  653. $parent_file = $plugin_page;
  654. if ( isset( $_wp_real_parent_file[$parent_file] ) )
  655. $parent_file = $_wp_real_parent_file[$parent_file];
  656. return $parent_file;
  657. }
  658. }
  659. if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
  660. $parent_file = $pagenow;
  661. if ( isset( $_wp_real_parent_file[$parent_file] ) )
  662. $parent_file = $_wp_real_parent_file[$parent_file];
  663. return $parent_file;
  664. }
  665. foreach (array_keys( (array)$submenu ) as $parent) {
  666. foreach ( $submenu[$parent] as $submenu_array ) {
  667. if ( isset( $_wp_real_parent_file[$parent] ) )
  668. $parent = $_wp_real_parent_file[$parent];
  669. if ( $submenu_array[2] == $pagenow ) {
  670. $parent_file = $parent;
  671. return $parent;
  672. } else
  673. if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
  674. $parent_file = $parent;
  675. return $parent;
  676. }
  677. }
  678. }
  679. if ( empty($parent_file) )
  680. $parent_file = '';
  681. return '';
  682. }
  683. function get_admin_page_title() {
  684. global $title;
  685. global $menu;
  686. global $submenu;
  687. global $pagenow;
  688. global $plugin_page;
  689. if ( isset( $title ) && !empty ( $title ) ) {
  690. return $title;
  691. }
  692. $hook = get_plugin_page_hook( $plugin_page, $pagenow );
  693. $parent = $parent1 = get_admin_page_parent();
  694. if ( empty ( $parent) ) {
  695. foreach ( (array)$menu as $menu_array ) {
  696. if ( isset( $menu_array[3] ) ) {
  697. if ( $menu_array[2] == $pagenow ) {
  698. $title = $menu_array[3];
  699. return $menu_array[3];
  700. } else
  701. if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
  702. $title = $menu_array[3];
  703. return $menu_array[3];
  704. }
  705. } else {
  706. $title = $menu_array[0];
  707. return $title;
  708. }
  709. }
  710. } else {
  711. foreach (array_keys( $submenu ) as $parent) {
  712. foreach ( $submenu[$parent] as $submenu_array ) {
  713. if ( isset( $plugin_page ) &&
  714. ($plugin_page == $submenu_array[2] ) &&
  715. (($parent == $pagenow ) || ($parent == $plugin_page ) || ($plugin_page == $hook ) || (($pagenow == 'admin.php' ) && ($parent1 != $submenu_array[2] ) ) )
  716. ) {
  717. $title = $submenu_array[3];
  718. return $submenu_array[3];
  719. }
  720. if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
  721. continue;
  722. if ( isset( $submenu_array[3] ) ) {
  723. $title = $submenu_array[3];
  724. return $submenu_array[3];
  725. } else {
  726. $title = $submenu_array[0];
  727. return $title;
  728. }
  729. }
  730. }
  731. }
  732. return $title;
  733. }
  734. function get_plugin_page_hook( $plugin_page, $parent_page ) {
  735. $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
  736. if ( has_action($hook) )
  737. return $hook;
  738. else
  739. return null;
  740. }
  741. function get_plugin_page_hookname( $plugin_page, $parent_page ) {
  742. global $admin_page_hooks;
  743. $parent = get_admin_page_parent( $parent_page );
  744. $page_type = 'admin';
  745. if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
  746. if ( isset( $admin_page_hooks[$plugin_page] ) )
  747. $page_type = 'toplevel';
  748. else
  749. if ( isset( $admin_page_hooks[$parent] ))
  750. $page_type = $admin_page_hooks[$parent];
  751. } else if ( isset( $admin_page_hooks[$parent] ) ) {
  752. $page_type = $admin_page_hooks[$parent];
  753. }
  754. $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
  755. return $page_type.'_page_'.$plugin_name;
  756. }
  757. function user_can_access_admin_page() {
  758. global $pagenow;
  759. global $menu;
  760. global $submenu;
  761. global $_wp_menu_nopriv;
  762. global $_wp_submenu_nopriv;
  763. global $plugin_page;
  764. $parent = get_admin_page_parent();
  765. if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
  766. return false;
  767. if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
  768. return false;
  769. if ( empty( $parent) ) {
  770. if ( isset( $_wp_menu_nopriv[$pagenow] ) )
  771. return false;
  772. if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
  773. return false;
  774. if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
  775. return false;
  776. foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
  777. if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
  778. return false;
  779. if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
  780. return false;
  781. }
  782. return true;
  783. }
  784. if ( isset( $submenu[$parent] ) ) {
  785. foreach ( $submenu[$parent] as $submenu_array ) {
  786. if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
  787. if ( current_user_can( $submenu_array[1] ))
  788. return true;
  789. else
  790. return false;
  791. } else if ( $submenu_array[2] == $pagenow ) {
  792. if ( current_user_can( $submenu_array[1] ))
  793. return true;
  794. else
  795. return false;
  796. }
  797. }
  798. }
  799. foreach ( $menu as $menu_array ) {
  800. if ( $menu_array[2] == $parent) {
  801. if ( current_user_can( $menu_array[1] ))
  802. return true;
  803. else
  804. return false;
  805. }
  806. }
  807. return true;
  808. }
  809. /* Whitelist functions */
  810. /**
  811. * Register a setting and its sanitization callback
  812. *
  813. * @since 2.7.0
  814. *
  815. * @param string $option_group A settings group name. Can be anything.
  816. * @param string $option_name The name of an option to sanitize and save.
  817. * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
  818. * @return unknown
  819. */
  820. function register_setting($option_group, $option_name, $sanitize_callback = '') {
  821. return add_option_update_handler($option_group, $option_name, $sanitize_callback);
  822. }
  823. /**
  824. * Unregister a setting
  825. *
  826. * @since 2.7.0
  827. *
  828. * @param unknown_type $option_group
  829. * @param unknown_type $option_name
  830. * @param unknown_type $sanitize_callback
  831. * @return unknown
  832. */
  833. function unregister_setting($option_group, $option_name, $sanitize_callback = '') {
  834. return remove_option_update_handler($option_group, $option_name, $sanitize_callback);
  835. }
  836. /**
  837. * {@internal Missing Short Description}}
  838. *
  839. * @since unknown
  840. *
  841. * @param unknown_type $option_group
  842. * @param unknown_type $option_name
  843. * @param unknown_type $sanitize_callback
  844. */
  845. function add_option_update_handler($option_group, $option_name, $sanitize_callback = '') {
  846. global $new_whitelist_options;
  847. $new_whitelist_options[ $option_group ][] = $option_name;
  848. if ( $sanitize_callback != '' )
  849. add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
  850. }
  851. /**
  852. * {@internal Missing Short Description}}
  853. *
  854. * @since unknown
  855. *
  856. * @param unknown_type $option_group
  857. * @param unknown_type $option_name
  858. * @param unknown_type $sanitize_callback
  859. */
  860. function remove_option_update_handler($option_group, $option_name, $sanitize_callback = '') {
  861. global $new_whitelist_options;
  862. $pos = array_search( $option_name, $new_whitelist_options );
  863. if ( $pos !== false )
  864. unset( $new_whitelist_options[ $option_group ][ $pos ] );
  865. if ( $sanitize_callback != '' )
  866. remove_filter( "sanitize_option_{$option_name}", $sanitize_callback );
  867. }
  868. /**
  869. * {@internal Missing Short Description}}
  870. *
  871. * @since unknown
  872. *
  873. * @param unknown_type $options
  874. * @return unknown
  875. */
  876. function option_update_filter( $options ) {
  877. global $new_whitelist_options;
  878. if ( is_array( $new_whitelist_options ) )
  879. $options = add_option_whitelist( $new_whitelist_options, $options );
  880. return $options;
  881. }
  882. add_filter( 'whitelist_options', 'option_update_filter' );
  883. /**
  884. * {@internal Missing Short Description}}
  885. *
  886. * @since unknown
  887. *
  888. * @param unknown_type $new_options
  889. * @param unknown_type $options
  890. * @return unknown
  891. */
  892. function add_option_whitelist( $new_options, $options = '' ) {
  893. if( $options == '' ) {
  894. global $whitelist_options;
  895. } else {
  896. $whitelist_options = $options;
  897. }
  898. foreach( $new_options as $page => $keys ) {
  899. foreach( $keys as $key ) {
  900. if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
  901. $whitelist_options[ $page ] = array();
  902. $whitelist_options[ $page ][] = $key;
  903. } else {
  904. $pos = array_search( $key, $whitelist_options[ $page ] );
  905. if ( $pos === false )
  906. $whitelist_options[ $page ][] = $key;
  907. }
  908. }
  909. }
  910. return $whitelist_options;
  911. }
  912. /**
  913. * {@internal Missing Short Description}}
  914. *
  915. * @since unknown
  916. *
  917. * @param unknown_type $del_options
  918. * @param unknown_type $options
  919. * @return unknown
  920. */
  921. function remove_option_whitelist( $del_options, $options = '' ) {
  922. if( $options == '' ) {
  923. global $whitelist_options;
  924. } else {
  925. $whitelist_options = $options;
  926. }
  927. foreach( $del_options as $page => $keys ) {
  928. foreach( $keys as $key ) {
  929. if ( isset($whitelist_options[ $page ]) && is_array($whitelist_options[ $page ]) ) {
  930. $pos = array_search( $key, $whitelist_options[ $page ] );
  931. if( $pos !== false )
  932. unset( $whitelist_options[ $page ][ $pos ] );
  933. }
  934. }
  935. }
  936. return $whitelist_options;
  937. }
  938. /**
  939. * Output nonce, action, and option_page fields for a settings page.
  940. *
  941. * @since 2.7.0
  942. *
  943. * @param string $option_group A settings group name. This should match the group name used in register_setting().
  944. */
  945. function settings_fields($option_group) {
  946. echo "<input type='hidden' name='option_page' value='$option_group' />";
  947. echo '<input type="hidden" name="action" value="update" />';
  948. wp_nonce_field("$option_group-options");
  949. }
  950. ?>