Skip to content

Commit

Permalink
JS CS sweep
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeselander committed Nov 13, 2016
1 parent f9a0296 commit fb45336
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 141 deletions.
15 changes: 10 additions & 5 deletions class.cmb-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ public function init_fields( $post_id = 0 ) {
public function init_fields_for_post() {

global $post;
$post_id = null;

// Get the current ID
if ( isset( $_GET['post'] ) ) {
$post_id = $_GET['post'];
$post_id = wp_unslash( $_GET['post'] );
} elseif ( isset( $_POST['post_ID'] ) ) {
$post_id = $_POST['post_ID'];
$post_id = wp_unslash( $_POST['post_ID'] );
} elseif ( ! empty( $post->ID ) ) {
$post_id = $post->ID;
}
Expand All @@ -70,6 +71,10 @@ public function init_fields_for_post() {
return false;
}

if ( ! is_numeric( $post_id ) || $post_id != floor( $post_id ) ) {
return false;
}

$this->init_fields( (int) $post_id );

}
Expand Down Expand Up @@ -392,13 +397,13 @@ function save_for_post( $post_id ) {

function get_post_id() {

$post_id = isset( $_GET['post'] ) ? $_GET['post'] : null;
$post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : null;

if ( ! $post_id && isset( $_POST['post_id'] ) ) {
$post_id = $_POST['post_id'];
$post_id = absint( $_POST['post_id'] );
}

return $post_id;
return (int) $post_id;

}
}
8 changes: 4 additions & 4 deletions classes.fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ static function request_image_ajax_callback() {
return;
}

$id = intval( $_POST['id'] );
$id = absint( $_POST['id'] );

$size = array(
intval( $_POST['width'] ),
Expand All @@ -652,7 +652,7 @@ static function request_image_ajax_callback() {
);

$image = wp_get_attachment_image_src( $id, $size );
echo reset( $image );
echo esc_url( reset( $image ) );

die(); // this is required to return a proper result
}
Expand Down Expand Up @@ -1019,7 +1019,7 @@ public function html() {
<script>
if ( 'undefined' === typeof( cmb_wysiwyg_editors ) )
var cmb_wysiwyg_editors = {};
cmb_wysiwyg_editors.<?php echo $field_id; ?> = '<?php echo $editor; ?>';
cmb_wysiwyg_editors.<?php echo esc_js( $field_id ); ?> = '<?php echo esc_js( $editor ); ?>';
</script>

<?php
Expand Down Expand Up @@ -1434,7 +1434,7 @@ public function output_script() {
// TODO this should be in inside the class
function cmb_ajax_post_select() {

$post_id = ! empty( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : false;
$post_id = ! empty( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : false;
$nonce = ! empty( $_POST['nonce'] ) ? $_POST['nonce'] : false;
$args = ! empty( $_POST['query'] ) ? $_POST['query'] : array();

Expand Down
141 changes: 81 additions & 60 deletions js/cmb.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,37 @@ var CMB = {
init : function() {

jQuery( '.field.repeatable' ).each( function() {
CMB.isMaxFields( jQuery(this) );
CMB.isMaxFields( jQuery( this ) );
} );

// Unbind & Re-bind all CMB events to prevent duplicates.
jQuery(document).unbind( 'click.CMB' );
jQuery(document).on( 'click.CMB', '.cmb-delete-field', CMB.deleteField );
jQuery(document).on( 'click.CMB', '.repeat-field', CMB.repeatField );
jQuery( document ).unbind( 'click.CMB' );
jQuery( document ).on( 'click.CMB', '.cmb-delete-field', CMB.deleteField );
jQuery( document ).on( 'click.CMB', '.repeat-field', CMB.repeatField );

// When toggling the display of the meta box container - reinitialize
jQuery(document).on( 'click.CMB', '.postbox h3, .postbox .handlediv', CMB.init );
jQuery( document ).on( 'click.CMB', '.postbox h3, .postbox .handlediv', CMB.init );

CMB.doneInit();

jQuery('.field.cmb-sortable' ).each( function() {
CMB.sortableInit( jQuery(this) );
jQuery( '.field.cmb-sortable' ).each( function() {
CMB.sortableInit( jQuery( this ) );
} );


},

repeatField : function( e ) {

var templateField, newT, field, index, attr;

field = jQuery( this ).closest('.field' );
field = jQuery( this ).closest( '.field' );

e.preventDefault();
jQuery(this).blur();
jQuery( this ).blur();

if ( CMB.isMaxFields( field, 1 ) )
if ( CMB.isMaxFields( field, 1 ) ) {
return;
}

templateField = field.children( '.field-item.hidden' );

Expand All @@ -59,7 +59,7 @@ var CMB = {
var excludeInputTypes = '[type=submit],[type=button],[type=checkbox],[type=radio],[readonly]';
newT.find( 'input' ).not( excludeInputTypes ).val( '' );

newT.find( '.cmb_upload_status' ).html('');
newT.find( '.cmb_upload_status' ).html( '' );

newT.insertBefore( templateField );

Expand All @@ -72,11 +72,13 @@ var CMB = {
var search = field.hasClass( 'CMB_Group_Field' ) ? /cmb-group-(\d|x)*/g : /cmb-field-(\d|x)*/g;
var replace = field.hasClass( 'CMB_Group_Field' ) ? 'cmb-group-' + index : 'cmb-field-' + index;

jQuery(this).find( '[' + attr.join('],[') + ']' ).each( function() {
jQuery( this ).find( '[' + attr.join( '],[' ) + ']' ).each( function() {

for ( var i = 0; i < attr.length; i++ )
if ( typeof( jQuery(this).attr( attr[i] ) ) !== 'undefined' )
jQuery(this).attr( attr[i], jQuery(this).attr( attr[i] ).replace( search, replace ) );
for ( var i = 0; i < attr.length; i++ ) {
if ( typeof( jQuery( this ).attr( attr[i] ) ) !== 'undefined' ) {
jQuery( this ).attr( attr[i], jQuery( this ).attr( attr[i] ).replace( search, replace ) );
}
}

} );

Expand All @@ -86,9 +88,9 @@ var CMB = {

CMB.clonedField( newT );

if ( field.hasClass( 'cmb-sortable' ) )
if ( field.hasClass( 'cmb-sortable' ) ) {
CMB.sortableInit( field );

}

},

Expand All @@ -97,13 +99,13 @@ var CMB = {
var fieldItem, field;

e.preventDefault();
jQuery(this).blur();
jQuery( this ).blur();

if ( ! confirm( CMBData.strings.confirmDeleteField ) ) {
return;
}

fieldItem = jQuery( this ).closest('.field-item' );
fieldItem = jQuery( this ).closest( '.field-item' );
field = fieldItem.closest( '.field' );

CMB.isMaxFields( field, -1 );
Expand All @@ -129,23 +131,26 @@ var CMB = {
modifier = (modifier) ? parseInt( modifier, 10 ) : 0;

addBtn = field.children( '.repeat-field' );
count = field.children('.field-item').not('.hidden').length + modifier; // Count after anticipated action (modifier)
count = field.children( '.field-item' ).not( '.hidden' ).length + modifier; // Count after anticipated action (modifier)
max = field.attr( 'data-rep-max' );

// Show all the remove field buttons.
field.find( '> .field-item > .cmb-delete-field, > .field-item > .group > .cmb-delete-field' ).show();

if ( typeof( max ) === 'undefined' )
if ( typeof( max ) === 'undefined' ) {
return false;
}

// Disable the add new field button?
if ( count >= parseInt( max, 10 ) )
if ( count >= parseInt( max, 10 ) ) {
addBtn.attr( 'disabled', 'disabled' );
else
} else {
addBtn.removeAttr( 'disabled' );
}

if ( count > parseInt( max, 10 ) )
if ( count > parseInt( max, 10 ) ) {
return true;
}

},

Expand Down Expand Up @@ -174,9 +179,11 @@ var CMB = {

addCallbackForClonedField: function( fieldName, callback ) {

if ( jQuery.isArray( fieldName ) )
for ( var i = 0; i < fieldName.length; i++ )
if ( jQuery.isArray( fieldName ) ) {
for ( var i = 0; i < fieldName.length; i++ ) {
CMB.addCallbackForClonedField( fieldName[i], callback );
}
}

this._clonedFieldCallbacks[fieldName] = this._clonedFieldCallbacks[fieldName] ? this._clonedFieldCallbacks[fieldName] : []
this._clonedFieldCallbacks[fieldName].push( callback )
Expand All @@ -192,21 +199,25 @@ var CMB = {
// also check child elements
el.add( el.find( 'div[data-class]' ) ).each( function( i, el ) {

el = jQuery( el )
var callbacks = CMB._clonedFieldCallbacks[el.attr( 'data-class') ]
el = jQuery( el );
var callbacks = CMB._clonedFieldCallbacks[ el.attr( 'data-class' ) ];

if ( callbacks )
for ( var a = 0; a < callbacks.length; a++ )
if ( callbacks ) {
for ( var a = 0; a < callbacks.length; a++ ) {
callbacks[a]( el );
}
}

})
},

addCallbackForDeletedField: function( fieldName, callback ) {

if ( jQuery.isArray( fieldName ) )
for ( var i = 0; i < fieldName.length; i++ )
if ( jQuery.isArray( fieldName ) ) {
for ( var i = 0; i < fieldName.length; i++ ) {
CMB._deletedFieldCallbacks( fieldName[i], callback );
}
}

this._deletedFieldCallbacks[fieldName] = this._deletedFieldCallbacks[fieldName] ? this._deletedFieldCallbacks[fieldName] : []
this._deletedFieldCallbacks[fieldName].push( callback )
Expand All @@ -220,26 +231,28 @@ var CMB = {
deletedField: function( el ) {

// also check child elements
el.add( el.find( 'div[data-class]' ) ).each( function(i, el) {
el.add( el.find( 'div[data-class]' ) ).each( function( i, el ) {

el = jQuery( el )
var callbacks = CMB._deletedFieldCallbacks[el.attr( 'data-class') ]
el = jQuery( el );
var callbacks = CMB._deletedFieldCallbacks[ el.attr( 'data-class' ) ];

if ( callbacks )
for ( var a = 0; a < callbacks.length; a++ )
if ( callbacks ) {
for ( var a = 0; a < callbacks.length; a++ ) {
callbacks[a]( el )
}
}

})
},

sortableInit : function( field ) {

var items = field.find(' > .field-item').not('.hidden');
var items = field.find( ' > .field-item' ).not( '.hidden' );

field.find( '> .field-item > .cmb-handle' ).remove();

items.each( function() {
jQuery(this).append( '<div class="cmb-handle"></div>' );
jQuery( this ).append( '<div class="cmb-handle"></div>' );
} );

field.sortable( {
Expand All @@ -255,24 +268,28 @@ var CMB = {
sortStart : function ( el ) {

// also check child elements
el.add( el.find( 'div[data-class]' ) ).each( function(i, el) {
el.add( el.find( 'div[data-class]' ) ).each( function( i, el ) {

el = jQuery( el )
var callbacks = CMB._sortStartCallbacks[el.attr( 'data-class') ]
el = jQuery( el );
var callbacks = CMB._sortStartCallbacks[ el.attr( 'data-class' ) ];

if ( callbacks )
for ( var a = 0; a < callbacks.length; a++ )
callbacks[a]( el )
if ( callbacks ) {
for ( var a = 0; a < callbacks.length; a++ ) {
callbacks[a]( el );
}
}

})
});

},

addCallbackForSortStart: function( fieldName, callback ) {

if ( jQuery.isArray( fieldName ) )
for ( var i = 0; i < fieldName.length; i++ )
if ( jQuery.isArray( fieldName ) ) {
for ( var i = 0; i < fieldName.length; i++ ) {
CMB.addCallbackForSortStart( fieldName[i], callback );
}
}

this._sortStartCallbacks[fieldName] = this._sortStartCallbacks[fieldName] ? this._sortStartCallbacks[fieldName] : []
this._sortStartCallbacks[fieldName].push( callback )
Expand All @@ -284,31 +301,35 @@ var CMB = {
// also check child elements
el.add( el.find( 'div[data-class]' ) ).each( function(i, el) {

el = jQuery( el )
var callbacks = CMB._sortEndCallbacks[el.attr( 'data-class') ]
el = jQuery( el );
var callbacks = CMB._sortEndCallbacks[ el.attr( 'data-class' ) ];

if ( callbacks )
for ( var a = 0; a < callbacks.length; a++ )
callbacks[a]( el )
if ( callbacks ) {
for ( var a = 0; a < callbacks.length; a++ ) {
callbacks[a]( el );
}
}

})

},

addCallbackForSortEnd: function( fieldName, callback ) {

if ( jQuery.isArray( fieldName ) )
for ( var i = 0; i < fieldName.length; i++ )
if ( jQuery.isArray( fieldName ) ) {
for ( var i = 0; i < fieldName.length; i++ ) {
CMB.addCallbackForSortEnd( fieldName[i], callback );
}
}

this._sortEndCallbacks[fieldName] = this._sortEndCallbacks[fieldName] ? this._sortEndCallbacks[fieldName] : []
this._sortEndCallbacks[fieldName].push( callback )
this._sortEndCallbacks[fieldName] = this._sortEndCallbacks[fieldName] ? this._sortEndCallbacks[fieldName] : [];
this._sortEndCallbacks[fieldName].push( callback );

}

}
};

jQuery(document).ready( function() {
jQuery( document ).ready( function() {

CMB.init();

Expand Down
Loading

0 comments on commit fb45336

Please sign in to comment.