From f4459a1d92f45ca1d2bb77ec6564f08d9a3e2db7 Mon Sep 17 00:00:00 2001 From: Doug Stewart Date: Wed, 6 Mar 2013 01:19:26 -0500 Subject: [PATCH] Adding support for the (as of WP2.5) new colorpicker. --- init.php | 15 +++++++++++++-- js/cmb.js | 24 ++++++++++++++---------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/init.php b/init.php index ef8879a..2ef3929 100644 --- a/init.php +++ b/init.php @@ -487,12 +487,23 @@ function save( $post_id) { * Adding scripts and styles */ function cmb_scripts( $hook ) { + global $wp_version; if ( $hook == 'post.php' || $hook == 'post-new.php' || $hook == 'page-new.php' || $hook == 'page.php' ) { + $cmb_script_array = array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'media-upload', 'thickbox' ); + $cmb_style_array = array( 'thickbox' ); + if( 3.5 <= $wp_version ){ + $cmb_script_array[] = 'wp-color-picker'; + $cmb_style_array[] = 'wp-color-picker'; + }else{ + $cmb_script_array[] = 'farbtastic'; + $cmb_style_array[] = 'farbtastic'; + } wp_register_script( 'cmb-timepicker', CMB_META_BOX_URL . 'js/jquery.timePicker.min.js' ); - wp_register_script( 'cmb-scripts', CMB_META_BOX_URL . 'js/cmb.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'media-upload', 'thickbox', 'farbtastic' ) ); + wp_register_script( 'cmb-scripts', CMB_META_BOX_URL . 'js/cmb.js', $cmb_script_array, '0.9.1' ); + wp_localize_script( 'cmb-scripts', 'cmb_ajax_data', array( 'ajax_nonce' => wp_create_nonce( 'ajax_nonce' ), 'post_id' => get_the_ID() ) ); wp_enqueue_script( 'cmb-timepicker' ); wp_enqueue_script( 'cmb-scripts' ); - wp_register_style( 'cmb-styles', CMB_META_BOX_URL . 'style.css', array( 'thickbox', 'farbtastic' ) ); + wp_register_style( 'cmb-styles', CMB_META_BOX_URL . 'style.css', $cmb_style_array ); wp_enqueue_style( 'cmb-styles' ); } } diff --git a/js/cmb.js b/js/cmb.js index 4c48dc6..5b94fd9 100644 --- a/js/cmb.js +++ b/js/cmb.js @@ -45,16 +45,20 @@ jQuery(document).ready(function ($) { /** * Initialize color picker */ - $('input:text.cmb_colorpicker').each(function (i) { - $(this).after('
'); - $('#picker-' + i).hide().farbtastic($(this)); - }) - .focus(function() { - $(this).next().show(); - }) - .blur(function() { - $(this).next().hide(); - }); + if( typeof jQuery.wp === 'object' && typeof jQuery.wp.wpColorPicker === 'function' ){ + $('input:text.cmb_colorpicker').wpColorPicker(); + }else{ + $('input:text.cmb_colorpicker').each(function (i) { + $(this).after('
'); + $('#picker-' + i).hide().farbtastic($(this)); + }) + .focus(function() { + $(this).next().show(); + }) + .blur(function() { + $(this).next().hide(); + }); + } /** * File and image upload handling