Skip to content

Commit

Permalink
Use a CMB2_Types arg to enqueue JS dependencies rather than calling C…
Browse files Browse the repository at this point in the history
…MB2_JS::add_dependencies directly from each method
  • Loading branch information
jtsternberg committed Oct 2, 2015
1 parent 631a588 commit 9fc34f9
Showing 1 changed file with 58 additions and 53 deletions.
111 changes: 58 additions & 53 deletions includes/CMB2_Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,20 @@ public function _id( $suffix = '' ) {
*/
public function input( $args = array() ) {
$a = $this->parse_args( $args, 'input', array(
'type' => 'text',
'class' => 'regular-text',
'name' => $this->_name(),
'id' => $this->_id(),
'value' => $this->field->escaped_value(),
'desc' => $this->_desc( true ),
'type' => 'text',
'class' => 'regular-text',
'name' => $this->_name(),
'id' => $this->_id(),
'value' => $this->field->escaped_value(),
'desc' => $this->_desc( true ),
'js_dependencies' => array(),
) );

return sprintf( '<input%s/>%s', $this->concat_attrs( $a, array( 'desc' ) ), $a['desc'] );
if ( ! empty( $a['js_dependencies'] ) ) {
CMB2_JS::add_dependencies( $a['js_dependencies'] );
}

return sprintf( '<input%s/>%s', $this->concat_attrs( $a, array( 'desc', 'js_dependencies' ) ), $a['desc'] );
}

/**
Expand Down Expand Up @@ -501,13 +506,12 @@ public function wysiwyg( $args = array() ) {

public function text_date( $args = array() ) {
$args = wp_parse_args( $args, array(
'class' => 'cmb2-text-small cmb2-datepicker',
'value' => $this->field->get_timestamp_format(),
'desc' => $this->_desc(),
'class' => 'cmb2-text-small cmb2-datepicker',
'value' => $this->field->get_timestamp_format(),
'desc' => $this->_desc(),
'js_dependencies' => array( 'jquery-ui-core', 'jquery-ui-datepicker' ),
) );

CMB2_JS::add_dependencies( array( 'jquery-ui-core', 'jquery-ui-datepicker' ) );

return $this->input( $args );
}

Expand All @@ -518,14 +522,12 @@ public function text_date_timestamp( $args = array() ) {

public function text_time( $args = array() ) {
$args = wp_parse_args( $args, array(
'class' => 'cmb2-timepicker text-time',
'value' => $this->field->get_timestamp_format( 'time_format' ),
'desc' => $this->_desc(),
'class' => 'cmb2-timepicker text-time',
'value' => $this->field->get_timestamp_format( 'time_format' ),
'js_dependencies' => array( 'jquery-ui-core', 'jquery-ui-datepicker', 'jquery-ui-datetimepicker' ),
) );

CMB2_JS::add_dependencies( array( 'jquery-ui-core', 'jquery-ui-datepicker', 'jquery-ui-datetimepicker' ) );

return $this->input( $args );
return $this->text_date( $args );
}

public function text_datetime_timestamp( $args = array() ) {
Expand Down Expand Up @@ -556,15 +558,14 @@ public function text_datetime_timestamp( $args = array() ) {
) );

$time_args = wp_parse_args( $args['timepicker'], array(
'class' => 'cmb2-timepicker text-time',
'name' => $this->_name( '[time]' ),
'id' => $this->_id( '_time' ),
'value' => $has_good_value ? $this->field->get_timestamp_format( 'time_format', $args['value'] ) : '',
'desc' => $args['desc'],
'class' => 'cmb2-timepicker text-time',
'name' => $this->_name( '[time]' ),
'id' => $this->_id( '_time' ),
'value' => $has_good_value ? $this->field->get_timestamp_format( 'time_format', $args['value'] ) : '',
'desc' => $args['desc'],
'js_dependencies' => array( 'jquery-ui-core', 'jquery-ui-datepicker', 'jquery-ui-datetimepicker' ),
) );

CMB2_JS::add_dependencies( array( 'jquery-ui-core', 'jquery-ui-datepicker', 'jquery-ui-datetimepicker' ) );

return $this->input( $date_args ) . "\n" . $this->input( $time_args );
}

Expand Down Expand Up @@ -618,8 +619,9 @@ public function select_timezone() {
) );
}

public function colorpicker() {
$meta_value = $this->field->escaped_value();
public function colorpicker( $args = array(), $meta_value = '' ) {
$meta_value = $meta_value ? $meta_value : $this->field->escaped_value();

$hex_color = '(([a-fA-F0-9]){3}){1,2}$';
if ( preg_match( '/^' . $hex_color . '/i', $meta_value ) ) {
// Value is just 123abc, so prepend #
Expand All @@ -630,9 +632,14 @@ public function colorpicker() {
}

wp_enqueue_style( 'wp-color-picker' );
CMB2_JS::add_dependencies( array( 'wp-color-picker' ) );

return $this->input( array( 'class' => 'cmb2-colorpicker cmb2-text-small', 'value' => $meta_value ) );
$args = wp_parse_args( $args, array(
'class' => 'cmb2-colorpicker cmb2-text-small',
'value' => $meta_value,
'js_dependencies' => 'wp-color-picker',
) );

return $this->input( $args );
}

public function title( $args = array() ) {
Expand Down Expand Up @@ -857,6 +864,7 @@ public function file_list() {
'size' => 45, 'desc' => '', 'value' => '',
'data-previewsize' => is_array( $img_size ) ? sprintf( '[%s]', implode( ',', $img_size ) ) : 50,
'data-queryargs' => ! empty( $query_args ) ? json_encode( $query_args ) : '',
'js_dependencies' => 'media-editor',
) ),
$this->input( array(
'type' => 'button',
Expand Down Expand Up @@ -901,8 +909,6 @@ public function file_list() {
}

echo '</ul>';

CMB2_JS::add_dependencies( 'media-editor' );
}

public function file() {
Expand All @@ -921,6 +927,7 @@ public function file() {
'desc' => '',
'data-previewsize' => is_array( $img_size ) ? '[' . implode( ',', $img_size ) . ']' : 350,
'data-queryargs' => ! empty( $query_args ) ? json_encode( $query_args ) : '',
'js_dependencies' => 'media-editor',
) );

printf( '<input class="cmb2-upload-button button" type="button" value="%s" />', esc_attr( $this->_text( 'add_upload_file_text', __( 'Add or Upload File', 'cmb2' ) ) ) );
Expand Down Expand Up @@ -958,36 +965,34 @@ public function file() {
'desc' => '',
) ),
'<div id="', $this->_id( '-status' ), '" class="cmb2-media-status">';
if ( ! empty( $meta_value ) ) {
if ( ! empty( $meta_value ) ) {

if ( $this->is_valid_img_ext( $meta_value ) ) {
if ( $this->is_valid_img_ext( $meta_value ) ) {

if ( $_id_value ) {
$image = wp_get_attachment_image( $_id_value, $img_size, null, array( 'class' => 'cmb-file-field-image' ) );
} else {
$size = is_array( $img_size ) ? $img_size[0] : 350;
$image = '<img style="max-width: ' . absint( $size ) . 'px; width: 100%; height: auto;" src="' . $meta_value . '" alt="" />';
}
if ( $_id_value ) {
$image = wp_get_attachment_image( $_id_value, $img_size, null, array( 'class' => 'cmb-file-field-image' ) );
} else {
$size = is_array( $img_size ) ? $img_size[0] : 350;
$image = '<img style="max-width: ' . absint( $size ) . 'px; width: 100%; height: auto;" src="' . $meta_value . '" alt="" />';
}

$this->img_status_output( array(
'image' => $image,
'tag' => 'div',
'cached_id' => $cached_id,
) );
$this->img_status_output( array(
'image' => $image,
'tag' => 'div',
'cached_id' => $cached_id,
) );

} else {
} else {

$this->file_status_output( array(
'value' => $meta_value,
'tag' => 'div',
'cached_id' => $cached_id,
) );
$this->file_status_output( array(
'value' => $meta_value,
'tag' => 'div',
'cached_id' => $cached_id,
) );

}
}
}
echo '</div>';

CMB2_JS::add_dependencies( 'media-editor' );
}

/**
Expand Down

0 comments on commit 9fc34f9

Please sign in to comment.