Permalink
Browse files

Adding support for the (as of WP2.5) new colorpicker.

  • Loading branch information...
1 parent 6a5d1af commit f4459a1d92f45ca1d2bb77ec6564f08d9a3e2db7 @zamoose committed Mar 6, 2013
Showing with 27 additions and 12 deletions.
  1. +13 −2 init.php
  2. +14 −10 js/cmb.js
View
@@ -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' );
}
}
View
@@ -45,16 +45,20 @@ jQuery(document).ready(function ($) {
/**
* Initialize color picker
*/
- $('input:text.cmb_colorpicker').each(function (i) {
- $(this).after('<div id="picker-' + i + '" style="z-index: 1000; background: #EEE; border: 1px solid #CCC; position: absolute; display: block;"></div>');
- $('#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('<div id="picker-' + i + '" style="z-index: 1000; background: #EEE; border: 1px solid #CCC; position: absolute; display: block;"></div>');
+ $('#picker-' + i).hide().farbtastic($(this));
+ })
+ .focus(function() {
+ $(this).next().show();
+ })
+ .blur(function() {
+ $(this).next().hide();
+ });
+ }
/**
* File and image upload handling

3 comments on commit f4459a1

@zamoose
Owner
zamoose commented on f4459a1 Mar 6, 2013

Whoops, as of WP 3.5, not 2.4. Fat-fingered.

@jtsternberg

Looks like a nice improvement. Did you test it on a pre-3.5 install?

@zamoose
Owner
zamoose commented on f4459a1 Mar 8, 2013

Yup, appears to fall back correctly to Farbtastic on a clean 3.4.2 install.

Please sign in to comment.