Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Custom menu images
- Plugin URI: http://8manos.com
- Description: This plugin lets you add images as CSS backgrounds to your menu items using the menu editor in wp-admin
- Version: 0.5
- Author: 8manos
- Author URI: http://8manos.com
- License: GPLv2
- */
- $prefix = 'menu-item-image';
- class CustomMenuImage{
- private $prefix = 'custom-menu-image';
- private $styles = array();
- function __construct(){
- $this->dir = dirname( __FILE__ );
- $this->uri = plugin_dir_url( __FILE__ );
- $this->url = $this->uri;
- $this->plugin_basename = plugin_basename( __FILE__ );
- global $wp_filter;
- if(!get_option($this->prefix)){
- add_option( $this->prefix, array() );
- }
- //add_filter( 'wp_get_nav_menu_items', 'addHomeMenuLink', 10, 2 );
- add_filter( 'attachment_fields_to_edit', array(&$this, 'control_add_image_to_menu'), 20,2);
- add_filter("wp_get_nav_menu_items", array(&$this, "get_custom_menu_image_css"), 20, 2);
- add_action( 'admin_print_scripts-nav-menus.php', array( &$this, 'add_js' ) );
- add_action( 'admin_print_styles-nav-menus.php', array( &$this, 'add_css' ) );
- add_action('admin_head', array(&$this, 'add_js'));
- add_action('wp_footer', array(&$this, 'print_custom_menu_image_css'));
- add_action( 'admin_print_scripts-media-upload-popup', array( &$this, 'media_upload_popup_js' ), 2000 );
- add_action('admin_head', array(&$this, 'admin_head'));
- add_action('wp_ajax_add_image', array(&$this, "add_image"));
- add_action('wp_ajax_remove_image', array(&$this, "remove_image"));
- register_activation_hook(__FILE__, array(&$this, 'custom_menu_image_activation'));
- register_uninstall_hook(__FILE__, array(&$this, 'custom_menu_image_uninstall'));
- if(strtolower($_SERVER['REQUEST_METHOD']) == 'post') $this->save_config();
- }
- function media_upload_popup_js(){
- //wp_enqueue_script('wp-ajax-response');
- }
- function custom_menu_image_activation(){
- $options = get_option($this->prefix);
- if(!$options || !is_array($options))
- add_option($this->prefix, array());
- }
- function custom_menu_image_uninstall(){
- delete_option($this->prefix);
- }
- function save_config(){
- if(count($_POST)){
- $urls = $_POST[$this->prefix.'-url'];
- $urls_type = $_POST[$this->prefix.'-url-type'];
- $medias_lib = $_POST[$this->prefix.'-ml'];
- if(count($urls)){
- $data = array();
- /* mod jrc 130111 - fix bug*/
- $data = (get_option($this->prefix));
- /*end mod jrc 130111*/
- foreach($urls as $key => $val){
- $data[$key] = array(
- 'url_type' => $urls_type[$key],
- 'url' => $urls[$key],
- 'media_lib' => $medias_lib[$key]
- );
- }
- update_option($this->prefix, $data);
- }
- }
- }
- function get_custom_menu_image_css($menuItems, $args){
- //print_r($menuItems);
- $this->styles[] = '<style>';
- /*mod jrc 231210*/
- /* $this->styles[] = '
- body{
- font-weight: bold;
- color: red;
- }
- ';*/
- /*end mod jrc 231210*/
- $custom_options = (get_option($this->prefix));
- foreach ($menuItems as $key => $val ) {
- $menuItems[$key]->classes = array('menu_item_'.$val->ID);
- $image_url = ($custom_options[$val->ID]['url_type'] != 'lib' ? $custom_options[$val->ID]['url'] : $custom_options[$val->ID]['media_lib']);
- /*mod jrc 050111*/
- /*
- $this->styles[] = '
- #access li.menu_item_'.$val->ID.' > a,
- #access li.menu_item_'.$val->ID.':hover > a,
- li.menu-item.menu_item_'.$val->ID.' a,
- li.menu-item.menu_item_'.$val->ID.':hover a{
- background-image: url('.$image_url.');
- background-repeat: no-repeat;
- background-position: 5px center;
- padding-left: 23px;
- }
- ';
- */
- /*mod jrc 130111 - moved to css*/
- /* - works ok on home page menu
- $this->styles[] = '
- #access li.menu_item_'.$val->ID.' > a,
- #access li.menu_item_'.$val->ID.':hover > a,
- li.menu-item.menu_item_'.$val->ID.' a,
- li.menu-item.menu_item_'.$val->ID.':hover a{
- background-image: url('.$image_url.');
- background-repeat: no-repeat;
- background-position: center center;
- text-align: right;
- padding-right: 20px;
- line-height: 310px;
- vertical-align: bottom;
- }
- ';
- */
- $this->styles[] = '
- #access li.menu_item_'.$val->ID.' > a,
- #access li.menu_item_'.$val->ID.':hover > a,
- li.menu-item.menu_item_'.$val->ID.' a,
- li.menu-item.menu_item_'.$val->ID.':hover a{
- background-image: url('.$image_url.');
- }
- ';
- /*end mod jrc 130111 */
- /*end mod jrc 050111*/
- }
- $this->styles[] = '</style>';
- return $menuItems;
- }
- function print_custom_menu_image_css(){
- print implode("\n", $this->styles);
- }
- function control_add_image_to_menu( $fields, $post ) {
- if(isset($_GET['cmi_id'])){
- $id = (int) $post->ID;
- $text = __( 'Add Thumbnail Menu' );
- $button = '<a rel="'.$id.'" class="button-primary" href="javascript:void(0);" onclick="add_image(this);">'.$text.'</a>';
- $fields['image-size']['extra_rows']['asdadadadada']['html'] = $button;
- }
- return $fields;
- }
- function add_css(){
- wp_enqueue_style('thickbox');
- }
- function add_js(){
- wp_enqueue_script('jquery');
- wp_enqueue_script('wp-ajax-response');
- wp_enqueue_script('thickbox');
- add_action("admin_head", array(&$this, "xxxx"));
- }
- function xxxx(){
- ?>
- <script>
- var menu_item_options = new Array();
- <?php
- $menu_item_options = get_option($this->prefix);
- if(count($menu_item_options) && is_array($menu_item_options)){
- foreach($menu_item_options as $k=>$v){
- if(is_array($v))
- echo 'menu_item_options['.$k.'] = '.json_encode($v).';' . "\n";
- }
- }
- ?>
- jQuery(document).ready(function(){
- var p = jQuery("div.menu-item-settings");
- var url = '<?php echo admin_url( 'media-upload.php' )?>';
- for(var i = 0; i < p.length; i++){
- var id = p[i].id.substr(19);
- var sibling = jQuery("#edit-menu-item-attr-title-"+id).parent().parent();
- var checked = (menu_item_options[id] ? (menu_item_options[id].url_type == 'lib' ? 'lib' : 'url') : 'url');
- var str_e = '\
- <'+'fieldset style="border:1px solid #CCCCCC;padding: 3px;width:380px;">\
- <'+'legend style="margin-left:5px;">Navigation Image</'+'legend>\
- <'+'p class="description description-thin" style="height:auto;">\
- <'+'input type="radio" name="<?php echo $this->prefix;?>-url-type['+id+']" value="url" '+(checked=='url' ? 'checked="checked"' : '')+' />From URL\
- <'+'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+'">\
- <'+'br />\
- <'+'input type="radio" name="<?php echo $this->prefix;?>-url-type['+id+']" value="lib" '+(checked!='url' ? 'checked="checked"' : '')+'/>From Media Library\
- <'+'br />\
- <'+'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;" />\
- <'+'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 : "")+'" />\
- <'+'br />\
- <'+'a class="XXX" href="'+url+'?type=image&tab=library&cmi_id='+id+'&TB_iframe=true" onclick="return false;">Add</'+'a>\
- <'+'a class="YYY" href="javascript:void(0);" onclick="remove_image(this);" rel="'+id+'">Remove</'+'a>\
- <'+'div style="clear:both;"></'+'div>\
- <'+'/p>\
- <'+'/fieldset>';
- var new_e = jQuery(str_e);
- sibling.after(new_e);
- //jQuery(p[i]).before(new_e);
- }
- tb_init('a.XXX');//pass where to apply thickbox
- })
- function remove_image(el){
- var data = {
- cmi_id: jQuery(el).attr("rel"),
- action: 'remove_image',
- }
- jQuery.ajax({
- url: ajaxurl,
- type: "POST",
- data: data,
- dataType: 'json',
- cache: false,
- success: function (data, textStatus ) {
- data = eval( data );
- var prev_id = '<?php echo $this->prefix;?>-preview-'+data.cmi_id;
- jQuery( parent.document.getElementById( prev_id ) )
- .css({display: 'none'})
- .attr("src", "")
- var ml_id = '<?php echo $this->prefix;?>-ml-'+data.cmi_id;
- /* Refresh the image on the screen below */
- jQuery( parent.document.getElementById( ml_id ) )
- .val("")
- }
- });
- }
- </script>
- <?php
- }
- function admin_head(){
- ?>
- <!--
- <script type='text/javascript' src='http://localhost/wordpress301/wp-includes/js/thickbox/thickbox.js'></script>
- <script type='text/javascript' src='http://localhost/wordpress301/wp-admin/load-scripts.php?c=1&load=wp-ajax-response,thickbox&ver=ccc374ca1616213ca9ed109e93676b0e'></script>
- <link rel="stylesheet" href="http://localhost/wordpress301/wp-includes/js/thickbox/thickbox.css" />-->
- <script>
- function add_image(el){
- var sizes = jQuery("td.field input");
- var size = '';
- for(var i = 0;i <sizes.length;i++){
- if(sizes[i].name == 'attachments['+jQuery(el).attr('rel')+'][image-size]'){
- if(sizes[i].checked){
- size = (sizes[i].value)
- break;
- }
- }
- }
- var data = {
- cmi_id: '<?php echo $_GET['cmi_id'];?>',
- action: 'add_image',
- attachment_id : jQuery(el).attr('rel'),
- size: size
- }
- jQuery.ajax({
- url: ajaxurl,
- type: "POST",
- data: data,
- dataType: 'json',
- cache: false,
- success: function (data, textStatus ) {
- /* Vars */
- data = eval( data );
- var prev_id = '<?php echo $this->prefix;?>-preview-'+data.cmi_id;
- jQuery( parent.document.getElementById( prev_id ) )
- .css({width: '100px', height: '100px', display: 'block'})
- .attr("src", data.thumb)
- var ml_id = '<?php echo $this->prefix;?>-ml-'+data.cmi_id;
- /* Refresh the image on the screen below */
- jQuery( parent.document.getElementById( ml_id ) )
- .val(data.thumb)
- /* Close Thickbox */
- self.parent.tb_remove();
- }
- });
- }
- </script>
- <?php
- }
- public function get_thumb( $id, $size = 'full' ) {
- global $wp_version;
- /* Get the originally uploaded size path. */
- list( $img_url, $img_path ) = get_attachment_icon_src( $id, true );
- if($size == 'full') return $img_url;
- /* Attepmt to get custom intermediate size. */
- $img = image_get_intermediate_size( $id, $size ); //detail
- /* If custom intermediate size cannot be found, attempt to create it. */
- if( !$img ) {
- /* Need to check to see if fullsize path can be found - sometimes this disappears during import/export. */
- if( !is_file( $img_path ) ) {
- $wp_upload_dir = wp_upload_dir();
- $img_path = $wp_upload_dir['path'] . get_post_meta( $id, '_wp_attached_file', true );
- }
- if( is_file( $img_path ) ) {
- $new = image_resize( $img_path, $this->detail_size[0], $this->detail_size[1], $this->detail_size[2] );
- if( !is_wp_error( $new ) ) {
- $meta = wp_generate_attachment_metadata( $id, $img_path );
- wp_update_attachment_metadata( $id, $meta );
- $img = image_get_intermediate_size( $id, $size );
- }
- }
- }
- /* Custom intermediate size cannot be created, try for thumbnail. */
- if( !$img ) {
- $img = image_get_intermediate_size( $id, 'thumbnail' );
- //echo "3:";print_r($img);
- }
- /* Thumbnail cannot be found, try fullsize. */
- if( !$img ) {
- $img['url'] = wp_get_attachment_url( $id );
- }
- /* Administration */
- if( isset( $img['url'] ) && !empty( $img['url'] ) ) {
- return $img['url'];
- }
- else if( is_admin() ) {
- return $this->url . 'deleted-image.png';
- }
- return false;
- }
- function add_image(){
- die(json_encode(array(
- 'cmi_id' => $_POST['cmi_id'],
- 'thumb' => $this->get_thumb($_POST['attachment_id'], $_POST['size'])
- )));
- }
- function remove_image(){
- die(json_encode(array(
- 'cmi_id' => $_POST['cmi_id']
- )));
- }
- }
- $custom_menu_image = new CustomMenuImage();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement