Advertisement
rcain

wp-custom-menu-images my mods

Mar 31st, 2011
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.79 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Custom menu images
  4. Plugin URI: http://8manos.com
  5. Description: This plugin lets you add images as CSS backgrounds to your menu items using the menu editor in wp-admin
  6. Version: 0.5
  7. Author: 8manos
  8. Author URI: http://8manos.com
  9. License: GPLv2
  10. */
  11.  
  12. $prefix = 'menu-item-image';
  13. class CustomMenuImage{
  14.     private $prefix = 'custom-menu-image';
  15.     private $styles = array();
  16.     function __construct(){
  17.         $this->dir = dirname( __FILE__ );
  18.         $this->uri = plugin_dir_url( __FILE__ );
  19.         $this->url = $this->uri;
  20.         $this->plugin_basename = plugin_basename( __FILE__ );
  21.         global $wp_filter;     
  22.        
  23.         if(!get_option($this->prefix)){
  24.             add_option( $this->prefix, array() );
  25.         }
  26.    
  27.         //add_filter( 'wp_get_nav_menu_items', 'addHomeMenuLink', 10, 2 );
  28.         add_filter( 'attachment_fields_to_edit', array(&$this, 'control_add_image_to_menu'), 20,2);
  29.         add_filter("wp_get_nav_menu_items", array(&$this, "get_custom_menu_image_css"), 20, 2);
  30.        
  31.         add_action( 'admin_print_scripts-nav-menus.php', array( &$this, 'add_js' ) );
  32.         add_action( 'admin_print_styles-nav-menus.php', array( &$this, 'add_css' ) );
  33.         add_action('admin_head', array(&$this, 'add_js'));
  34.         add_action('wp_footer', array(&$this, 'print_custom_menu_image_css'));
  35.        
  36.         add_action( 'admin_print_scripts-media-upload-popup', array( &$this, 'media_upload_popup_js' ), 2000 );
  37.        
  38.         add_action('admin_head', array(&$this, 'admin_head'));
  39.        
  40.         add_action('wp_ajax_add_image', array(&$this, "add_image"));
  41.         add_action('wp_ajax_remove_image', array(&$this, "remove_image"));
  42.         register_activation_hook(__FILE__, array(&$this, 'custom_menu_image_activation'));     
  43.         register_uninstall_hook(__FILE__, array(&$this, 'custom_menu_image_uninstall'));
  44.         if(strtolower($_SERVER['REQUEST_METHOD']) == 'post') $this->save_config();
  45.     }  
  46.     function media_upload_popup_js(){
  47.         //wp_enqueue_script('wp-ajax-response');
  48.     }
  49.     function custom_menu_image_activation(){
  50.         $options = get_option($this->prefix);
  51.         if(!$options || !is_array($options))
  52.             add_option($this->prefix, array());
  53.     }
  54.     function custom_menu_image_uninstall(){
  55.         delete_option($this->prefix);
  56.     }
  57.     function save_config(){
  58.         if(count($_POST)){
  59.             $urls       = $_POST[$this->prefix.'-url'];
  60.             $urls_type  = $_POST[$this->prefix.'-url-type'];
  61.             $medias_lib = $_POST[$this->prefix.'-ml'];
  62.  
  63.             if(count($urls)){
  64.                 $data = array();
  65. /* mod jrc 130111 - fix bug*/
  66.                 $data = (get_option($this->prefix));
  67. /*end mod jrc 130111*/
  68.                 foreach($urls as $key => $val){
  69.                     $data[$key] = array(
  70.                         'url_type'  => $urls_type[$key],
  71.                         'url'       => $urls[$key],
  72.                         'media_lib' => $medias_lib[$key]
  73.                     );
  74.                 }
  75.                 update_option($this->prefix, $data);
  76.             }  
  77.         }
  78.     }
  79.     function get_custom_menu_image_css($menuItems, $args){
  80.         //print_r($menuItems);
  81.         $this->styles[] = '<style>';
  82. /*mod jrc 231210*/
  83. /*      $this->styles[] = '
  84.             body{
  85.                 font-weight: bold;
  86.                 color: red;
  87.             }
  88.         ';*/
  89. /*end mod jrc 231210*/
  90.  
  91.         $custom_options = (get_option($this->prefix));
  92.         foreach ($menuItems as $key => $val ) {
  93.             $menuItems[$key]->classes = array('menu_item_'.$val->ID);
  94.             $image_url = ($custom_options[$val->ID]['url_type'] != 'lib' ? $custom_options[$val->ID]['url'] : $custom_options[$val->ID]['media_lib']);
  95. /*mod jrc 050111*/
  96. /*
  97.             $this->styles[] = '
  98.                 #access li.menu_item_'.$val->ID.' > a,
  99.                 #access li.menu_item_'.$val->ID.':hover > a,
  100.                 li.menu-item.menu_item_'.$val->ID.' a,
  101.                 li.menu-item.menu_item_'.$val->ID.':hover a{
  102.                     background-image: url('.$image_url.');
  103.                     background-repeat: no-repeat;
  104.                     background-position: 5px center;
  105.                     padding-left: 23px;
  106.                 }
  107.             ';         
  108. */
  109.  
  110. /*mod jrc 130111 - moved to css*/
  111. /* - works ok on home page menu
  112.             $this->styles[] = '
  113.                 #access li.menu_item_'.$val->ID.' > a,
  114.                 #access li.menu_item_'.$val->ID.':hover > a,
  115.                 li.menu-item.menu_item_'.$val->ID.' a,
  116.                 li.menu-item.menu_item_'.$val->ID.':hover a{
  117.                     background-image: url('.$image_url.');
  118.                     background-repeat: no-repeat;
  119.                     background-position: center center;
  120.                     text-align: right;
  121.                     padding-right: 20px;
  122.                     line-height: 310px;
  123.                     vertical-align: bottom;
  124.                 }
  125.             ';         
  126. */
  127.  
  128.             $this->styles[] = '
  129.                 #access li.menu_item_'.$val->ID.' > a,
  130.                 #access li.menu_item_'.$val->ID.':hover > a,
  131.                 li.menu-item.menu_item_'.$val->ID.' a,
  132.                 li.menu-item.menu_item_'.$val->ID.':hover a{
  133.                     background-image: url('.$image_url.');
  134.                     }
  135.             ';         
  136. /*end mod jrc 130111 */        
  137. /*end mod jrc 050111*/
  138.         }
  139.         $this->styles[] = '</style>';
  140.         return $menuItems;
  141.     }
  142.     function print_custom_menu_image_css(){
  143.         print implode("\n", $this->styles);
  144.     }
  145.     function control_add_image_to_menu( $fields, $post ) {
  146.         if(isset($_GET['cmi_id'])){
  147.             $id = (int) $post->ID;
  148.             $text = __( 'Add Thumbnail Menu' );
  149.             $button = '<a rel="'.$id.'" class="button-primary" href="javascript:void(0);" onclick="add_image(this);">'.$text.'</a>';
  150.             $fields['image-size']['extra_rows']['asdadadadada']['html'] = $button;
  151.         }
  152.         return $fields;
  153.     }
  154.     function add_css(){
  155.         wp_enqueue_style('thickbox');
  156.     }
  157.     function add_js(){
  158.         wp_enqueue_script('jquery');
  159.         wp_enqueue_script('wp-ajax-response');
  160.         wp_enqueue_script('thickbox');
  161.         add_action("admin_head", array(&$this, "xxxx"));
  162.     }
  163.     function xxxx(){
  164.     ?>
  165.     <script>
  166.     var menu_item_options = new Array();
  167.     <?php
  168.     $menu_item_options = get_option($this->prefix);
  169.     if(count($menu_item_options) && is_array($menu_item_options)){
  170.         foreach($menu_item_options as $k=>$v){
  171.             if(is_array($v))
  172.                 echo 'menu_item_options['.$k.'] = '.json_encode($v).';' . "\n";
  173.         }
  174.     }
  175.     ?>
  176.    
  177.     jQuery(document).ready(function(){
  178.         var p = jQuery("div.menu-item-settings");
  179.        
  180.         var url = '<?php echo admin_url( 'media-upload.php' )?>';
  181.        
  182.         for(var i = 0; i < p.length; i++){
  183.             var id = p[i].id.substr(19);       
  184.             var sibling = jQuery("#edit-menu-item-attr-title-"+id).parent().parent();
  185.             var checked = (menu_item_options[id] ? (menu_item_options[id].url_type == 'lib' ? 'lib' : 'url') : 'url');
  186.             var str_e = '\
  187.                     <'+'fieldset style="border:1px solid #CCCCCC;padding: 3px;width:380px;">\
  188.                     <'+'legend style="margin-left:5px;">Navigation Image</'+'legend>\
  189.                         <'+'p class="description description-thin" style="height:auto;">\
  190.                         <'+'input type="radio" name="<?php echo $this->prefix;?>-url-type['+id+']" value="url" '+(checked=='url' ? 'checked="checked"' : '')+' />From URL\
  191.                         <'+'input type="text" value="'+(menu_item_options[id] ? menu_item_options[id].url : "")+'" name="<?php echo $this->prefix;?>-url['+id+']" class="widefat edit-menu-item-title" id="edit-menu-item-title-'+id+'">\
  192.                         <'+'br />\
  193.                         <'+'input type="radio" name="<?php echo $this->prefix;?>-url-type['+id+']" value="lib" '+(checked!='url' ? 'checked="checked"' : '')+'/>From Media Library\
  194.                         <'+'br />\
  195.                         <'+'img src="'+(menu_item_options[id] ? menu_item_options[id].media_lib : "")+'" id="<?php echo $this->prefix;?>-preview-'+id+'" style="max-width:150px;max-height:150px;" />\
  196.                         <'+'input type="hidden" name="<?php echo $this->prefix;?>-ml['+id+']" id="<?php echo $this->prefix;?>-ml-'+id+'" value="'+(menu_item_options[id] ? menu_item_options[id].media_lib : "")+'" />\
  197.                         <'+'br />\
  198.                         <'+'a class="XXX" href="'+url+'?type=image&amp;tab=library&amp;cmi_id='+id+'&amp;TB_iframe=true" onclick="return false;">Add</'+'a>\
  199.                         <'+'a class="YYY" href="javascript:void(0);" onclick="remove_image(this);" rel="'+id+'">Remove</'+'a>\
  200.                         <'+'div style="clear:both;"></'+'div>\
  201.                     <'+'/p>\
  202.                     <'+'/fieldset>';
  203.             var new_e = jQuery(str_e);         
  204.             sibling.after(new_e);
  205.             //jQuery(p[i]).before(new_e);
  206.         }      
  207.         tb_init('a.XXX');//pass where to apply thickbox
  208.     })
  209.     function remove_image(el){
  210.         var data = {
  211.             cmi_id: jQuery(el).attr("rel"),
  212.             action: 'remove_image',
  213.         }
  214.         jQuery.ajax({
  215.             url: ajaxurl,
  216.             type: "POST",
  217.             data: data,
  218.             dataType: 'json',
  219.             cache: false,
  220.             success: function (data, textStatus ) {
  221.                 data = eval( data );
  222.                 var prev_id = '<?php echo $this->prefix;?>-preview-'+data.cmi_id;  
  223.                 jQuery( parent.document.getElementById( prev_id ) )
  224.                 .css({display: 'none'})
  225.                 .attr("src", "")
  226.                
  227.                 var ml_id = '<?php echo $this->prefix;?>-ml-'+data.cmi_id;         
  228.                 /* Refresh the image on the screen below */
  229.                 jQuery( parent.document.getElementById( ml_id ) )
  230.                 .val("")
  231.             }
  232.         });
  233.     }
  234.     </script>
  235.     <?php
  236.     }
  237.    
  238.     function admin_head(){
  239.         ?>
  240.     <!--
  241.     <script type='text/javascript' src='http://localhost/wordpress301/wp-includes/js/thickbox/thickbox.js'></script>
  242.     <script type='text/javascript' src='http://localhost/wordpress301/wp-admin/load-scripts.php?c=1&amp;load=wp-ajax-response,thickbox&amp;ver=ccc374ca1616213ca9ed109e93676b0e'></script>
  243.    
  244.     <link rel="stylesheet" href="http://localhost/wordpress301/wp-includes/js/thickbox/thickbox.css" />-->
  245.    
  246.     <script>
  247.        
  248.     function add_image(el){
  249.         var sizes = jQuery("td.field input");
  250.         var size = '';
  251.         for(var i = 0;i <sizes.length;i++){
  252.             if(sizes[i].name == 'attachments['+jQuery(el).attr('rel')+'][image-size]'){
  253.                 if(sizes[i].checked){
  254.                     size = (sizes[i].value)
  255.                     break;
  256.                 }
  257.             }
  258.         }
  259.         var data = {
  260.             cmi_id: '<?php echo $_GET['cmi_id'];?>',
  261.             action: 'add_image',
  262.             attachment_id : jQuery(el).attr('rel'),
  263.             size: size
  264.         }
  265.         jQuery.ajax({
  266.             url: ajaxurl,
  267.             type: "POST",
  268.             data: data,
  269.             dataType: 'json',
  270.             cache: false,
  271.             success: function (data, textStatus ) {
  272.                
  273.                 /* Vars */
  274.                 data = eval( data );
  275.  
  276.                 var prev_id = '<?php echo $this->prefix;?>-preview-'+data.cmi_id;  
  277.                 jQuery( parent.document.getElementById( prev_id ) )
  278.                 .css({width: '100px', height: '100px', display: 'block'})
  279.                 .attr("src", data.thumb)
  280.                
  281.                 var ml_id = '<?php echo $this->prefix;?>-ml-'+data.cmi_id;         
  282.                 /* Refresh the image on the screen below */
  283.                 jQuery( parent.document.getElementById( ml_id ) )
  284.                 .val(data.thumb)
  285.                
  286.                 /* Close Thickbox */
  287.                 self.parent.tb_remove();
  288.             }
  289.         });
  290.     }
  291.    
  292.     </script>
  293.     <?php
  294.     }  
  295.     public function get_thumb( $id, $size = 'full' ) {
  296.         global $wp_version;            
  297.         /* Get the originally uploaded size path. */
  298.         list( $img_url, $img_path ) = get_attachment_icon_src( $id, true );
  299.         if($size == 'full') return $img_url;
  300.         /* Attepmt to get custom intermediate size. */
  301.         $img = image_get_intermediate_size( $id, $size ); //detail
  302.        
  303.         /* If custom intermediate size cannot be found, attempt to create it. */
  304.         if( !$img ) {
  305.            
  306.             /* Need to check to see if fullsize path can be found - sometimes this disappears during import/export. */
  307.             if( !is_file( $img_path ) ) {
  308.                 $wp_upload_dir = wp_upload_dir();
  309.                 $img_path = $wp_upload_dir['path'] . get_post_meta( $id, '_wp_attached_file', true );
  310.             }
  311.            
  312.             if( is_file( $img_path ) ) {
  313.                 $new = image_resize( $img_path, $this->detail_size[0], $this->detail_size[1], $this->detail_size[2] );
  314.                
  315.                 if( !is_wp_error( $new ) ) {
  316.                     $meta = wp_generate_attachment_metadata( $id, $img_path );
  317.                     wp_update_attachment_metadata( $id, $meta );
  318.                     $img = image_get_intermediate_size( $id, $size );
  319.                 }
  320.             }
  321.         }
  322.        
  323.         /* Custom intermediate size cannot be created, try for thumbnail. */
  324.         if( !$img ) {
  325.             $img = image_get_intermediate_size( $id, 'thumbnail' );
  326.             //echo "3:";print_r($img);
  327.         }
  328.        
  329.         /* Thumbnail cannot be found, try fullsize. */
  330.         if( !$img ) {
  331.             $img['url'] = wp_get_attachment_url( $id );
  332.         }
  333.        
  334.         /* Administration */
  335.         if( isset( $img['url'] ) && !empty( $img['url'] ) ) {
  336.             return $img['url'];
  337.         }
  338.         else if( is_admin() ) {
  339.             return $this->url . 'deleted-image.png';
  340.         }
  341.         return false;
  342.     }
  343.     function add_image(){
  344.         die(json_encode(array(
  345.             'cmi_id'    => $_POST['cmi_id'],
  346.             'thumb' => $this->get_thumb($_POST['attachment_id'], $_POST['size'])
  347.         )));
  348.     }
  349.     function remove_image(){
  350.         die(json_encode(array(
  351.             'cmi_id'    => $_POST['cmi_id']
  352.         )));
  353.     }
  354. }
  355. $custom_menu_image = new CustomMenuImage();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement