PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/ozh-admin-drop-down-menu/inc/core.php

https://bitbucket.org/beneberle/recipes.beneberle.com
PHP | 440 lines | 303 code | 94 blank | 43 comment | 94 complexity | c1539cecc50c7a69f4bf92af0e528050 MD5 | raw file
  1. <?php
  2. /*
  3. Part of Plugin: Ozh' Admin Drop Down Menu
  4. http://planetozh.com/blog/my-projects/wordpress-admin-menu-drop-down-css/
  5. */
  6. // The main function that hacks the original menu and display ours instead. Triggered in the _init function by the admin_notice hook.
  7. function wp_ozh_adminmenu () {
  8. global $menu, $submenu, $self, $parent_file, $submenu_file, $plugin_page, $pagenow, $wp_ozh_adminmenu;
  9. // echo "<pre>";print_r($menu);print_r($submenu);echo "</pre>";
  10. // Plugins: hack $menu & $submenu before I butcher them
  11. $menu = apply_filters( 'pre_ozh_adminmenu_menu', $menu );
  12. $submenu = apply_filters( 'pre_ozh_adminmenu_submenu', $submenu );
  13. $ozh_menu = '<div id="ozhmenu_wrap"><ul id="ozhmenu">';
  14. // Plugins: hack $ozh_menu before I start adding stuff to it
  15. $ozh_menu = apply_filters( 'pre_ozh_adminmenu_ozh_menu', $ozh_menu );
  16. if ($wp_ozh_adminmenu['minimode'])
  17. $ozh_menu .= '<li id="oam_bloglink" class="ozhmenu_toplevel">'.wp_ozh_adminmenu_blogtitle().'</li>';
  18. $first = true;
  19. // 0 = name, 1 = capability, 2 = file, 3 = class, 4 = id, 5 = icon src
  20. foreach ( $_menu = $menu as $key => $item ) {
  21. // Top level menu
  22. if (strpos($item[4],'wp-menu-separator') !== false)
  23. continue;
  24. $admin_is_parent = false;
  25. $class = array();
  26. if ( $first ) {
  27. $class[] = 'wp-first-item';
  28. $first = false;
  29. }
  30. if ( !empty($submenu[$item[2]]) )
  31. $class[] = 'wp-has-submenu';
  32. if ( ( $parent_file && $item[2] == $parent_file ) || strcmp($self, $item[2]) == 0 ) {
  33. if ( !empty($submenu[$item[2]]) )
  34. $class[] = 'wp-has-current-submenu current wp-menu-open';
  35. else
  36. $class[] = 'current';
  37. }
  38. if ( isset($item[4]) && ! empty($item[4]) )
  39. $class[] = $item[4];
  40. $class = $class ? ' class="' . join( ' ', $class ) . '"' : '';
  41. $id = isset($item[5]) && ! empty($item[5]) ? 'oam_' . $item[5] : '';
  42. $anchor = $item[0];
  43. if ($wp_ozh_adminmenu['compact']) {
  44. $compactstyle = 'inline';
  45. $fullstyle = 'none';
  46. } else {
  47. $compactstyle = 'none';
  48. $fullstyle = 'inline';
  49. }
  50. if ( isset( $submenu_as_parent ) && !empty( $submenu[$item[2]] ) ) {
  51. $submenu[$item[2]] = array_values($submenu[$item[2]]); // Re-index.
  52. $menu_hook = get_plugin_page_hook($submenu[$item[2]][0][2], $item[2]);
  53. if ( ( ('index.php' != $submenu[$item[2]][0][2]) && file_exists(WP_PLUGIN_DIR . "/{$submenu[$item[2]][0][2]}") ) || !empty($menu_hook)) {
  54. $admin_is_parent = true;
  55. $href = "admin.php?page={$submenu[$item[2]][0][2]}";
  56. } else {
  57. $href = $submenu[$item[2]][0][2];
  58. }
  59. } else if ( current_user_can($item[1]) ) {
  60. $menu_hook = get_plugin_page_hook($item[2], 'admin.php');
  61. if ( ('index.php' != $item[2]) && file_exists(WP_PLUGIN_DIR . "/{$item[2]}") || !empty($menu_hook) ) {
  62. $admin_is_parent = true;
  63. $href = "admin.php?page={$item[2]}";
  64. } else {
  65. $href = $item[2];
  66. }
  67. }
  68. $imgstyle = ($wp_ozh_adminmenu['wpicons']) ? '' : 'style="display:none"';
  69. $img = '';
  70. if ( isset($item[6]) && ! empty($item[6]) ) {
  71. if ( 'div' === $item[6] )
  72. $img = '<div '.$imgstyle.' class="wp-menu-image"><br /></div>';
  73. else
  74. $img = '<img '.$imgstyle.' class="wp-menu-image" src="' . $item[6] . '" alt="" />';
  75. }
  76. if ($wp_ozh_adminmenu['toplinks']) {
  77. $href = "href='$href'";
  78. } else {
  79. $href = ( !empty($submenu[$item[2]]) )? '' : "href='$href'" ;
  80. }
  81. $ozh_menu .= "\t<li class='ozhmenu_toplevel' id='$id'><a $href $class>{$img}<span class='compact' style='display:$compactstyle'>&nbsp;</span><span class='full' style='display:$fullstyle'>$anchor</span></a>";
  82. // Sub level menus
  83. if ( !empty($submenu[$item[2]]) ) {
  84. if( !isset( $ulclass ) )
  85. $ulclass = '';
  86. $ozh_menu .= "\n\t\t<ul$ulclass><li class='toplevel_label'>$anchor</li>\n";
  87. $first = true;
  88. foreach ( $submenu[$item[2]] as $sub_key => $sub_item ) {
  89. if ( !current_user_can($sub_item[1]) )
  90. continue;
  91. $class = array();
  92. if ( $first ) {
  93. $class[] = 'wp-first-item';
  94. $first = false;
  95. }
  96. if ( isset($submenu_file) ) {
  97. if ( $submenu_file == $sub_item[2] )
  98. $class[] = 'current';
  99. // If plugin_page is set the parent must either match the current page or not physically exist.
  100. // This allows plugin pages with the same hook to exist under different parents.
  101. } else if ( (isset($plugin_page) && $plugin_page == $sub_item[2] && (!file_exists($item[2]) || ($item[2] == $self))) || (!isset($plugin_page) && $self == $sub_item[2]) ) {
  102. $class[] = 'current';
  103. }
  104. $subclass = $class ? ' class="' . join( ' ', $class ) . '"' : '';
  105. $menu_hook = get_plugin_page_hook($sub_item[2], $item[2]);
  106. if ( ( ('index.php' != $sub_item[2]) && file_exists(WP_PLUGIN_DIR . "/{$sub_item[2]}") ) || ! empty($menu_hook) ) {
  107. // If admin.php is the current page or if the parent exists as a file in the plugins or admin dir
  108. $parent_exists = (!$admin_is_parent && file_exists(WP_PLUGIN_DIR . "/{$item[2]}") && !is_dir(WP_PLUGIN_DIR . "/{$item[2]}") ) || file_exists($item[2]);
  109. if ( $parent_exists )
  110. $suburl = "{$item[2]}?page={$sub_item[2]}";
  111. elseif ( 'admin.php' == $pagenow || !$parent_exists )
  112. $suburl = "admin.php?page={$sub_item[2]}";
  113. else
  114. $suburl = "{$item[2]}?page={$sub_item[2]}";
  115. // Get icons?
  116. if ($wp_ozh_adminmenu['icons']) {
  117. $plugin_icon = apply_filters('ozh_adminmenu_icon', $sub_item[2]);
  118. $plugin_icon = apply_filters('ozh_adminmenu_icon_'.$sub_item[2], $sub_item[2]);
  119. if ($plugin_icon != $sub_item[2]) {
  120. // we have an icon: no default plugin class & we store the icon location
  121. $plugin_icons[wp_ozh_adminmenu_sanitize_id($sub_item[2])] = $plugin_icon;
  122. $icon = '';
  123. } else {
  124. // no icon: default plugin class
  125. $icon = 'oam_plugin';
  126. }
  127. }
  128. } else {
  129. $suburl = $sub_item[2];
  130. }
  131. // Custom logout menu?
  132. if ($sub_item[2] == 'ozh_admin_menu_logout')
  133. $suburl = wp_logout_url();
  134. $subid = 'oamsub_'.wp_ozh_adminmenu_sanitize_id($sub_item[2]);
  135. $subanchor = strip_tags($sub_item[0]);
  136. if( !isset( $icon ) )
  137. $icon = '';
  138. $ozh_menu .= "\t\t\t<li class='ozhmenu_sublevel $icon' id='$subid'><a href='$suburl'$subclass>$subanchor</a></li>\n";
  139. }
  140. $ozh_menu .= "</ul>";
  141. }
  142. $ozh_menu .= "</li>";
  143. }
  144. $ozh_menu .= "</ul></div>";
  145. // Plugins: hack $ozh_menu now it's complete
  146. $ozh_menu = apply_filters( 'post_ozh_adminmenu_ozh_menu', $ozh_menu );
  147. if ( isset( $plugin_icons ) ) {
  148. global $text_direction;
  149. $align = ($text_direction == 'rtl' ? 'right' : 'left');
  150. echo "\n".'<style type="text/css">'."\n";
  151. foreach( $plugin_icons as $hook=>$icon ) {
  152. $hook = plugin_basename($hook);
  153. //echo "#oamsub_$hook a {background-image:url($icon);}\n";
  154. echo "#oamsub_$hook a {background:url($icon) center $align no-repeat;}\n";
  155. }
  156. echo "</style>\n";
  157. }
  158. echo $ozh_menu;
  159. }
  160. function wp_ozh_adminmenu_blogtitle() {
  161. $blogname = get_bloginfo('name', 'display');
  162. if ( '' == $blogname )
  163. $blogname = '&nbsp;';
  164. $title_class = '';
  165. if ( function_exists('mb_strlen') ) {
  166. if ( mb_strlen($blogname, 'UTF-8') > 30 )
  167. $title_class = 'class="long-title"';
  168. } else {
  169. if ( strlen($blogname) > 30 )
  170. $title_class = 'class="long-title"';
  171. }
  172. $url = trailingslashit( get_bloginfo('url') );
  173. return "<a $title_class href='$url' title='".__('Visit site')."'>$blogname &raquo;</a>";
  174. }
  175. function wp_ozh_adminmenu_sanitize_id($url) {
  176. $url = preg_replace('/(&|&amp;|&#038;)?_wpnonce=([^&]+)/', '', $url);
  177. return str_replace(array('.php','.','/','?','='),array('','_','_','_','_'),$url);
  178. }
  179. function wp_ozh_adminmenu_js() {
  180. global $wp_ozh_adminmenu;
  181. $toomanyplugins = $wp_ozh_adminmenu['too_many_plugins'];
  182. if( empty($toomanyplugins) ) {
  183. $defaults = wp_ozh_adminmenu_defaults();
  184. $toomanyplugins = $defaults['too_many_plugins'];
  185. unset( $defaults );
  186. }
  187. $plugin_url = wp_ozh_adminmenu_pluginurl();
  188. $insert_main_js = '<script src="'.$plugin_url.'inc/js/adminmenu.js?v='. OZH_MENU_VER .'" type="text/javascript"></script>';
  189. echo <<<JS
  190. <script type="text/javascript"><!--//--><![CDATA[//><!--
  191. var oam_toomanypluygins = $toomanyplugins;
  192. var oam_adminmenu = false;
  193. jQuery(document).ready(function() {
  194. // Do we need to init everything ?
  195. var ozhmenu_uselesslinks = jQuery('#user_info p').html();
  196. if (ozhmenu_uselesslinks) {
  197. oam_adminmenu = true;
  198. }
  199. })
  200. //--><!]]></script>
  201. $insert_main_js
  202. JS;
  203. }
  204. function wp_ozh_adminmenu_css() {
  205. global $wp_ozh_adminmenu, $pagenow, $text_direction;
  206. // $submenu = ($wp_ozh_adminmenu['display_submenu'] or ($pagenow == "media-upload.php") ) ? 1 : 0;
  207. // Making links relative so they're more readable and shorter in the query string (also made relative in the .css.php)
  208. $plugin = wp_ozh_adminmenu_pluginurl().'inc/';
  209. // query vars
  210. $query = array(
  211. 'v' => OZH_MENU_VER,
  212. 'p' => wp_make_link_relative( $plugin ),
  213. 'a' => wp_make_link_relative( trailingslashit( get_admin_url() ) ),
  214. 'i' => $wp_ozh_adminmenu['icons'],
  215. 'w' => $wp_ozh_adminmenu['wpicons'],
  216. 'm' => $wp_ozh_adminmenu['minimode'],
  217. 'c' => $wp_ozh_adminmenu['compact'],
  218. 'h' => $wp_ozh_adminmenu['hidebubble'],
  219. 'f' => $wp_ozh_adminmenu['displayfav'],
  220. 'g' => $wp_ozh_adminmenu['grad'], // menu color
  221. 'n' => $wp_ozh_adminmenu['nograd'], // disable gradient bg
  222. 'd' => ($text_direction == 'rtl' ? 'right' : 'left'), // right-to-left locale?
  223. );
  224. $query = http_build_query($query);
  225. echo "<link rel='stylesheet' href='{$plugin}adminmenu.css.php?$query' type='text/css' media='all' />\n";
  226. }
  227. function wp_ozh_adminmenu_head() {
  228. wp_ozh_adminmenu_css();
  229. wp_ozh_adminmenu_js();
  230. }
  231. // Set defaults
  232. function wp_ozh_adminmenu_defaults() {
  233. return array(
  234. 'grad' => '#676768',
  235. 'nograd' => 0,
  236. 'compact' => 0,
  237. 'minimode' => 0,
  238. 'hidebubble' => 0,
  239. 'too_many_plugins' => 30,
  240. 'toplinks' => 1,
  241. 'icons' => 1,
  242. 'wpicons' => 1,
  243. );
  244. }
  245. // Read plugin options or set default values
  246. function wp_ozh_adminmenu_init() {
  247. global $wp_ozh_adminmenu, $plugin_page;
  248. if ($plugin_page == 'ozh_admin_menu')
  249. wp_ozh_adminmenu_load_page();
  250. if (isset($_POST['ozh_adminmenu']) && ($_POST['ozh_adminmenu'] == 1) )
  251. wp_ozh_adminmenu_processform();
  252. $defaults = wp_ozh_adminmenu_defaults();
  253. if (!count($wp_ozh_adminmenu)) {
  254. $wp_ozh_adminmenu = (array)get_option('ozh_adminmenu');
  255. unset($wp_ozh_adminmenu[0]);
  256. }
  257. // Allow plugins to modify the config
  258. $wp_ozh_adminmenu = apply_filters( 'ozh_adminmenu_init_config', array_merge( $defaults, $wp_ozh_adminmenu ) );
  259. // Cannot have wpicons == 0 && compact == 1
  260. if ($wp_ozh_adminmenu['compact'] == 1)
  261. $wp_ozh_adminmenu['wpicons'] = 1;
  262. // upon Fluency activation+deactivation, too_many_plugins can be 0, let's fix this
  263. if (!$wp_ozh_adminmenu['too_many_plugins']) $wp_ozh_adminmenu['too_many_plugins'] = 30;
  264. // On minimode, add a Logout link to the Users menu
  265. if ($wp_ozh_adminmenu['minimode'])
  266. add_users_page(__('Log Out'), __('Log Out'), 'read', 'ozh_admin_menu_logout');
  267. }
  268. // Stuff to do when loading the admin plugin page
  269. function wp_ozh_adminmenu_load_page() {
  270. wp_ozh_adminmenu_load_text_domain();
  271. }
  272. // Hooked into 'ozh_adminmenu_icon', this function give this plugin its own icon
  273. function wp_ozh_adminmenu_customicon($in) {
  274. return wp_ozh_adminmenu_pluginurl().'inc/images/ozh.png';
  275. }
  276. // Add option page, hook Farbtastic in
  277. function wp_ozh_adminmenu_add_page() {
  278. $page = add_options_page('Admin Drop Down Menu', 'Admin Menu', 'manage_options', 'ozh_admin_menu', 'wp_ozh_adminmenu_options_page_includes');
  279. add_action('admin_print_scripts-' . $page, 'wp_ozh_adminmenu_add_farbtastic');
  280. add_action('admin_print_styles-' . $page, 'wp_ozh_adminmenu_add_farbtastic');
  281. }
  282. // Actually add Farbtastic
  283. function wp_ozh_adminmenu_add_farbtastic() {
  284. wp_enqueue_script('farbtastic');
  285. wp_enqueue_style('farbtastic');
  286. }
  287. function wp_ozh_adminmenu_options_page_includes() {
  288. require_once(dirname(__FILE__).'/options.php');
  289. wp_ozh_adminmenu_options_page();
  290. }
  291. // Return plugin URL (SSL pref compliant) (trailing slash)
  292. function wp_ozh_adminmenu_pluginurl() {
  293. return plugin_dir_url( dirname(__FILE__) );
  294. }
  295. // Add the 'Settings' link to the plugin page
  296. function wp_ozh_adminmenu_plugin_actions($links) {
  297. $links[] = "<a href='options-general.php?page=ozh_admin_menu'><b>Settings</b></a>";
  298. return $links;
  299. }
  300. // Translation wrapper
  301. function wp_ozh_adminmenu__($string) {
  302. // return "<span style='color:red;background:yellow'>$string</span>"; // The debugging stuff so I'm sure I didnt miss any of the translatable string
  303. return __($string, 'adminmenu');
  304. }
  305. // Load translation file if any
  306. function wp_ozh_adminmenu_load_text_domain() {
  307. $locale = get_locale();
  308. $mofile = WP_PLUGIN_DIR.'/'.plugin_basename(dirname(__FILE__)).'/translations/adminmenu' . '-' . $locale . '.mo';
  309. load_textdomain('adminmenu', $mofile);
  310. }
  311. function wp_ozh_adminmenu_footer() {
  312. echo <<<HTML
  313. <p id="footer-ozh-oam">Thank you for using <a href="http://planetozh.com/blog/my-projects/wordpress-admin-menu-drop-down-css/">Admin Drop Down Menu</a>, a wonderful plugin by <a href="http://planetozh.com/blog/">Ozh</a></p>
  314. HTML;
  315. }
  316. // Process $_POST
  317. function wp_ozh_adminmenu_processform() {
  318. global $wp_ozh_adminmenu;
  319. check_admin_referer('ozh-adminmenu');
  320. // Debug:
  321. // echo "<pre>";echo htmlentities(print_r($_POST,true));echo "</pre>";
  322. switch ($_POST['action']) {
  323. case 'update_options':
  324. $defaults = wp_ozh_adminmenu_defaults();
  325. foreach ($_POST as $k=>$v) {
  326. $k = str_replace('oam_','',$k);
  327. if (array_key_exists($k, $defaults)) {
  328. $options[$k] = esc_attr( $v );
  329. }
  330. }
  331. if (!update_option('ozh_adminmenu', $options))
  332. add_option('ozh_adminmenu', $options);
  333. $wp_ozh_adminmenu = array_merge( (array)$wp_ozh_adminmenu, $options );
  334. $msg = wp_ozh_adminmenu__("updated");
  335. break;
  336. case 'reset_options':
  337. delete_option('ozh_adminmenu');
  338. $msg = wp_ozh_adminmenu__("deleted");
  339. break;
  340. }
  341. $message = '<div id="message" class="updated fade">';
  342. $message .= '<p>'.sprintf(wp_ozh_adminmenu__('Admin Drop Down Menu settings <strong>%s</strong>'), $msg)."</p>\n";
  343. $message .= "</div>\n";
  344. add_action('admin_notices', create_function( '', "echo '$message';" ) );
  345. }
  346. ?>