Advertisement
samuelaguilera

core.php

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