Skip to content

Commit

Permalink
Add ability to specify custom field callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Daryl Lozupone committed Oct 24, 2016
1 parent 6209f96 commit c023925
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions includes/class-meta-box-view.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use \ClubDeuce\WPLib\Components\Fields;

/**
* Class ClubDeuce_Meta_Box_View_Base
*/
Expand All @@ -25,17 +23,32 @@ function the_meta_box( $post, $args = array() ) {
'placeholder' => '',
'value' => $item->$meta_key(),
'class' => 'widefat',
'callback' => [ __CLASS__, '_render_field' ]
) );

$method_name = "_render_{$params['type']}_field";

if ( ! method_exists( $this, $method_name ) ) {
$method_name = '_render_text_field';
if ( is_callable( $params['callback'] ) ) {
call_user_func( $params['callback'], $meta_key, $params );
} else {
WPLib::trigger_error( sprintf( __( 'The specified callback for the field %1$s is not a callable function.' ), $meta_key ) );
}
}

}

call_user_func( array( $this, $method_name ), $meta_key, $params );
/**
* @param string $id
* @param array $params
*/
public function _render_field( $id, $params ) {

$method_name = "_render_{$params['type']}_field";

if ( ! method_exists( $this, $method_name ) ) {
$method_name = '_render_text_field';
}

call_user_func( array( $this, $method_name ), $id, $params );

}

private function _render_text_field($id, $params ) {
Expand Down

0 comments on commit c023925

Please sign in to comment.