PageRenderTime 58ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/Doduo_1.1/WebKitSite/blog/wp-admin/includes/plugin.php

https://github.com/weissms/owb-mirror
PHP | 382 lines | 304 code | 67 blank | 11 comment | 119 complexity | 14def1545ceeeed1ed577eb19824eb2a MD5 | raw file
  1. <?php
  2. function get_plugin_data( $plugin_file ) {
  3. $plugin_data = implode( '', file( $plugin_file ));
  4. preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $plugin_name );
  5. preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $plugin_uri );
  6. preg_match( '|Description:(.*)$|mi', $plugin_data, $description );
  7. preg_match( '|Author:(.*)$|mi', $plugin_data, $author_name );
  8. preg_match( '|Author URI:(.*)$|mi', $plugin_data, $author_uri );
  9. if ( preg_match( "|Version:(.*)|i", $plugin_data, $version ))
  10. $version = trim( $version[1] );
  11. else
  12. $version = '';
  13. $description = wptexturize( trim( $description[1] ));
  14. $name = $plugin_name[1];
  15. $name = trim( $name );
  16. $plugin = $name;
  17. if ('' != trim($plugin_uri[1]) && '' != $name ) {
  18. $plugin = '<a href="' . trim( $plugin_uri[1] ) . '" title="'.__( 'Visit plugin homepage' ).'">'.$plugin.'</a>';
  19. }
  20. if ('' == $author_uri[1] ) {
  21. $author = trim( $author_name[1] );
  22. } else {
  23. $author = '<a href="' . trim( $author_uri[1] ) . '" title="'.__( 'Visit author homepage' ).'">' . trim( $author_name[1] ) . '</a>';
  24. }
  25. return array('Name' => $name, 'Title' => $plugin, 'Description' => $description, 'Author' => $author, 'Version' => $version);
  26. }
  27. function get_plugins() {
  28. global $wp_plugins;
  29. if ( isset( $wp_plugins ) ) {
  30. return $wp_plugins;
  31. }
  32. $wp_plugins = array ();
  33. $plugin_root = ABSPATH . PLUGINDIR;
  34. // Files in wp-content/plugins directory
  35. $plugins_dir = @ opendir( $plugin_root);
  36. if ( $plugins_dir ) {
  37. while (($file = readdir( $plugins_dir ) ) !== false ) {
  38. if ( substr($file, 0, 1) == '.' )
  39. continue;
  40. if ( is_dir( $plugin_root.'/'.$file ) ) {
  41. $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
  42. if ( $plugins_subdir ) {
  43. while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
  44. if ( substr($subfile, 0, 1) == '.' )
  45. continue;
  46. if ( substr($subfile, -4) == '.php' )
  47. $plugin_files[] = "$file/$subfile";
  48. }
  49. }
  50. } else {
  51. if ( substr($file, -4) == '.php' )
  52. $plugin_files[] = $file;
  53. }
  54. }
  55. }
  56. @closedir( $plugins_dir );
  57. @closedir( $plugins_subdir );
  58. if ( !$plugins_dir || !$plugin_files )
  59. return $wp_plugins;
  60. foreach ( $plugin_files as $plugin_file ) {
  61. if ( !is_readable( "$plugin_root/$plugin_file" ) )
  62. continue;
  63. $plugin_data = get_plugin_data( "$plugin_root/$plugin_file" );
  64. if ( empty ( $plugin_data['Name'] ) )
  65. continue;
  66. $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
  67. }
  68. uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
  69. return $wp_plugins;
  70. }
  71. //
  72. // Menu
  73. //
  74. function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  75. global $menu, $admin_page_hooks;
  76. $file = plugin_basename( $file );
  77. $menu[] = array ( $menu_title, $access_level, $file, $page_title );
  78. $admin_page_hooks[$file] = sanitize_title( $menu_title );
  79. $hookname = get_plugin_page_hookname( $file, '' );
  80. if (!empty ( $function ) && !empty ( $hookname ))
  81. add_action( $hookname, $function );
  82. return $hookname;
  83. }
  84. function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
  85. global $submenu;
  86. global $menu;
  87. global $_wp_real_parent_file;
  88. global $_wp_submenu_nopriv;
  89. global $_wp_menu_nopriv;
  90. $file = plugin_basename( $file );
  91. $parent = plugin_basename( $parent);
  92. if ( isset( $_wp_real_parent_file[$parent] ) )
  93. $parent = $_wp_real_parent_file[$parent];
  94. if ( !current_user_can( $access_level ) ) {
  95. $_wp_submenu_nopriv[$parent][$file] = true;
  96. return false;
  97. }
  98. // If the parent doesn't already have a submenu, add a link to the parent
  99. // as the first item in the submenu. If the submenu file is the same as the
  100. // parent file someone is trying to link back to the parent manually. In
  101. // this case, don't automatically add a link back to avoid duplication.
  102. if (!isset( $submenu[$parent] ) && $file != $parent ) {
  103. foreach ( $menu as $parent_menu ) {
  104. if ( $parent_menu[2] == $parent && current_user_can( $parent_menu[1] ) )
  105. $submenu[$parent][] = $parent_menu;
  106. }
  107. }
  108. $submenu[$parent][] = array ( $menu_title, $access_level, $file, $page_title );
  109. $hookname = get_plugin_page_hookname( $file, $parent);
  110. if (!empty ( $function ) && !empty ( $hookname ))
  111. add_action( $hookname, $function );
  112. return $hookname;
  113. }
  114. function add_management_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  115. return add_submenu_page( 'edit.php', $page_title, $menu_title, $access_level, $file, $function );
  116. }
  117. function add_options_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  118. return add_submenu_page( 'options-general.php', $page_title, $menu_title, $access_level, $file, $function );
  119. }
  120. function add_theme_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  121. return add_submenu_page( 'themes.php', $page_title, $menu_title, $access_level, $file, $function );
  122. }
  123. function add_users_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  124. if ( current_user_can('edit_users') )
  125. $parent = 'users.php';
  126. else
  127. $parent = 'profile.php';
  128. return add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function );
  129. }
  130. //
  131. // Pluggable Menu Support -- Private
  132. //
  133. function get_admin_page_parent() {
  134. global $parent_file;
  135. global $menu;
  136. global $submenu;
  137. global $pagenow;
  138. global $plugin_page;
  139. global $_wp_real_parent_file;
  140. global $_wp_menu_nopriv;
  141. global $_wp_submenu_nopriv;
  142. if ( !empty ( $parent_file ) ) {
  143. if ( isset( $_wp_real_parent_file[$parent_file] ) )
  144. $parent_file = $_wp_real_parent_file[$parent_file];
  145. return $parent_file;
  146. }
  147. if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
  148. foreach ( $menu as $parent_menu ) {
  149. if ( $parent_menu[2] == $plugin_page ) {
  150. $parent_file = $plugin_page;
  151. if ( isset( $_wp_real_parent_file[$parent_file] ) )
  152. $parent_file = $_wp_real_parent_file[$parent_file];
  153. return $parent_file;
  154. }
  155. }
  156. if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
  157. $parent_file = $plugin_page;
  158. if ( isset( $_wp_real_parent_file[$parent_file] ) )
  159. $parent_file = $_wp_real_parent_file[$parent_file];
  160. return $parent_file;
  161. }
  162. }
  163. if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
  164. $parent_file = $pagenow;
  165. if ( isset( $_wp_real_parent_file[$parent_file] ) )
  166. $parent_file = $_wp_real_parent_file[$parent_file];
  167. return $parent_file;
  168. }
  169. foreach (array_keys( $submenu ) as $parent) {
  170. foreach ( $submenu[$parent] as $submenu_array ) {
  171. if ( isset( $_wp_real_parent_file[$parent] ) )
  172. $parent = $_wp_real_parent_file[$parent];
  173. if ( $submenu_array[2] == $pagenow ) {
  174. $parent_file = $parent;
  175. return $parent;
  176. } else
  177. if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
  178. $parent_file = $parent;
  179. return $parent;
  180. }
  181. }
  182. }
  183. $parent_file = '';
  184. return '';
  185. }
  186. function get_admin_page_title() {
  187. global $title;
  188. global $menu;
  189. global $submenu;
  190. global $pagenow;
  191. global $plugin_page;
  192. if ( isset( $title ) && !empty ( $title ) ) {
  193. return $title;
  194. }
  195. $hook = get_plugin_page_hook( $plugin_page, $pagenow );
  196. $parent = $parent1 = get_admin_page_parent();
  197. if ( empty ( $parent) ) {
  198. foreach ( $menu as $menu_array ) {
  199. if ( isset( $menu_array[3] ) ) {
  200. if ( $menu_array[2] == $pagenow ) {
  201. $title = $menu_array[3];
  202. return $menu_array[3];
  203. } else
  204. if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
  205. $title = $menu_array[3];
  206. return $menu_array[3];
  207. }
  208. } else {
  209. $title = $menu_array[0];
  210. return $title;
  211. }
  212. }
  213. } else {
  214. foreach (array_keys( $submenu ) as $parent) {
  215. foreach ( $submenu[$parent] as $submenu_array ) {
  216. if ( isset( $plugin_page ) &&
  217. ($plugin_page == $submenu_array[2] ) &&
  218. (($parent == $pagenow ) || ($parent == $plugin_page ) || ($plugin_page == $hook ) || (($pagenow == 'admin.php' ) && ($parent1 != $submenu_array[2] ) ) )
  219. ) {
  220. $title = $submenu_array[3];
  221. return $submenu_array[3];
  222. }
  223. if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
  224. continue;
  225. if ( isset( $submenu_array[3] ) ) {
  226. $title = $submenu_array[3];
  227. return $submenu_array[3];
  228. } else {
  229. $title = $submenu_array[0];
  230. return $title;
  231. }
  232. }
  233. }
  234. }
  235. return $title;
  236. }
  237. function get_plugin_page_hook( $plugin_page, $parent_page ) {
  238. global $wp_filter;
  239. $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
  240. if ( isset( $wp_filter[$hook] ))
  241. return $hook;
  242. else
  243. return null;
  244. }
  245. function get_plugin_page_hookname( $plugin_page, $parent_page ) {
  246. global $admin_page_hooks;
  247. $parent = get_admin_page_parent();
  248. if ( empty ( $parent_page ) || 'admin.php' == $parent_page ) {
  249. if ( isset( $admin_page_hooks[$plugin_page] ))
  250. $page_type = 'toplevel';
  251. else
  252. if ( isset( $admin_page_hooks[$parent] ))
  253. $page_type = $admin_page_hooks[$parent];
  254. } else
  255. if ( isset( $admin_page_hooks[$parent_page] ) ) {
  256. $page_type = $admin_page_hooks[$parent_page];
  257. } else {
  258. $page_type = 'admin';
  259. }
  260. $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
  261. return $page_type.'_page_'.$plugin_name;
  262. }
  263. function user_can_access_admin_page() {
  264. global $pagenow;
  265. global $menu;
  266. global $submenu;
  267. global $_wp_menu_nopriv;
  268. global $_wp_submenu_nopriv;
  269. global $plugin_page;
  270. $parent = get_admin_page_parent();
  271. if ( isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
  272. return false;
  273. if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
  274. return false;
  275. if ( empty( $parent) ) {
  276. if ( isset( $_wp_menu_nopriv[$pagenow] ) )
  277. return false;
  278. if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
  279. return false;
  280. if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
  281. return false;
  282. foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
  283. if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
  284. return false;
  285. if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
  286. return false;
  287. }
  288. return true;
  289. }
  290. if ( isset( $submenu[$parent] ) ) {
  291. foreach ( $submenu[$parent] as $submenu_array ) {
  292. if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
  293. if ( current_user_can( $submenu_array[1] ))
  294. return true;
  295. else
  296. return false;
  297. } else if ( $submenu_array[2] == $pagenow ) {
  298. if ( current_user_can( $submenu_array[1] ))
  299. return true;
  300. else
  301. return false;
  302. }
  303. }
  304. }
  305. foreach ( $menu as $menu_array ) {
  306. if ( $menu_array[2] == $parent) {
  307. if ( current_user_can( $menu_array[1] ))
  308. return true;
  309. else
  310. return false;
  311. }
  312. }
  313. return true;
  314. }
  315. ?>