Skip to content

Instantly share code, notes, and snippets.

@Magnacarter
Created December 1, 2017 22:28
Show Gist options
  • Save Magnacarter/ad8e2fa66fd2018017165ffe573a9067 to your computer and use it in GitHub Desktop.
Save Magnacarter/ad8e2fa66fd2018017165ffe573a9067 to your computer and use it in GitHub Desktop.
Custom Field Library
<?php
/**
* Custom Field
*
* Parent class defines custom fields and then builds
* arrays to assign custom fields
*
* @since 1.0.0
* @author Stellar Blue Technologies
* @licence GNU-2.0+
*/
/**
* Class Custom_Field
*/
class Custom_Field {
/**
* Render text field
*
* @since 1.0.0
* @param array $field
* @param string /int $meta
* @return void
*/
public function render_text_field( $field, $meta ) {
printf( '<span class="description">%s</span></br><input type="text" name="%s" id="%2$s" value="%s" size="30" />',
esc_html( $field['desc'] ), esc_attr( $field['id'] ), stripslashes_deep( esc_attr( $meta ) ) );
}
/**
* Render textarea field
*
* @since 1.0.0
* @param array $field
* @param string /int $meta
* @return void
*/
public function render_textarea_field( $field, $meta ) {
?>
<span class="description"><?php echo esc_html( $field['desc'] ); ?></span><br />
<textarea name="<?php echo esc_attr( $field['id'] ); ?>" id="<?php echo esc_attr( $field['id'] ); ?>" cols="60" rows="4"><?php echo esc_html( $meta ); ?></textarea>
<?php
}
/**
* Render wysiwyg field
*
* since 1.0.0
* @param array $field
* @param string $meta
* @return void
*/
public function render_wysiwyg_field( $field, $meta ) {
// Form fields
?>
<span class="description"><?php echo esc_html( $field['desc'] ); ?></span><br />
<?php
$settings = array(
'editor_height' => 200,
'wpautop' => false,
'media_buttons' => false,
'quicktags' => array( 'buttons' => 'strong,em,del,ul,ol,li,close' )
);
$list_field = wp_editor( stripslashes( $meta ), $field['id'], $settings );
}
/**
* Render checkbox field
*
* @since 1.0.0
* @param array $field
* @param string /int $meta
* @return void
*/
public function render_checkbox_field( $field, $meta ) {
echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '" ', $meta ? ' checked="checked"' : '', '/>
<label for="' . $field['id'] . '">' . $field['desc'] . '</label>';
}
/**
* Render select field
*
* @since 1.0.0
* @param array $field
* @param string /int $meta
* @return void
*/
public function render_select_field( $field, $meta ) {
printf( '<span class="description">%s</span><br />', esc_html( $field['desc'] ) );
printf( '<select name="%s" id="%1$s">', esc_attr( $field['id'] ) );
foreach ( $field['options'] as $option ) {
echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="' . esc_attr( $option['value'] ) . '">' . esc_html( $option['value'] ) . '</option>';
}
print( '</select>' );
}
/**
* Render image field
*
* @since 1.0.0
* @param array $field
* @param string $meta
* @param int $post_id
* @return void
*/
public function render_image_field( $field, $meta, $post_id ) {
$upload_link = esc_url( get_upload_iframe_src( 'image', $post_id ) );
$post_type = get_post_type( $post_id );
if ( $post_type == 'region' ) {
$img_id = get_post_meta( $post_id, $field['id'], true );
} else {
$img_id = get_the_author_meta( $field['id'], $post_id );
}
$img_src = wp_get_attachment_image_src( $img_id, 'full' );
$have_img = is_array( $img_src );
?>
<div class="custom-img-container">
<span class="description"><?php echo esc_html( $field['desc'] ) ?></span><br/><br/>
<?php if ( $have_img ) : ?>
<img src="<?php echo $img_src[0] ?>" alt="" style="max-width:150px; max-height: 150px;"/>
<?php endif; ?>
</div>
<p class="hide-if-no-js">
<a class="upload-custom-img <?php if ( $have_img ) {
echo 'hidden';
} ?>"
href="<?php echo $upload_link ?>">
<?php _e( 'Set custom image' ) ?>
</a>
<a class="delete-custom-img <?php if ( ! $have_img ) {
echo 'hidden';
} ?>"
href="#">
<?php _e( 'Remove this image' ) ?>
</a>
</p>
<input class="custom-img-id" name="<?php echo esc_attr( $field['id'] ); ?>" type="hidden" value="<?php echo esc_attr( $img_id ); ?>"/>
<?php
}
/**
* Render repeater field
*
* @since 1.0.0
* @param array $field
* @param string /int $meta
* @return void
*/
public function render_repeater_field( $field, $meta ) {
$post_id = sb_global_id();
printf( '<span class="description">%s</span><br/>', $field['desc'] );
print( '<a class="repeatable-add button" href="#">+</a>' );
printf( '<ul data-post-id="%s" id="%s-repeatable" class="custom_repeatable">', esc_attr( $post_id ), $field['id'] );
$i = 0;
if ( $meta ) {
foreach ( $meta as $row ) {
printf( '<input type="text" name="%s" id="%s" value="%s" size="30" />', $field['id'][$i], $field['id'], $row );
print( '<a class="repeatable-remove button" href="#">-</a></li>' );
$i ++;
}
} else {
printf( '<input type="text" name="%s" id="%s" value="" size="30" />', $field['id'][$i], $field['id'] );
print( '<a class="repeatable-remove button" href="#">-</a></li>' );
}
print( '</ul>' );
}
/**
* Render custom fields
*
* Loop through all of the custom fields in an array and build
* a table of custom fields for the admin page.
*
* @since 1.0.0
* @param array $custom_fields
* @param string $post_type
* @param int $id
* @return void
*/
public function render_custom_fields( $custom_fields, $post_type, $id ) {
?>
<table class="form-table">
<?php
foreach ( $custom_fields as $field ) {
if ( $post_type == 'user' ) {
$meta = get_the_author_meta( $field['id'], $id );
} else {
$meta = get_post_meta( $id, $field['id'], true );
}
?>
<tr><th><label for="<?php echo esc_attr( $field['id'] ); ?>"><?php echo esc_html( $field['label'] ); ?></label></th><td>
<?php
switch( $field['type'] ) {
case 'text':
$this->render_text_field( $field, $meta );
break;
case 'textarea':
$this->render_textarea_field( $field, $meta );
break;
case 'wysiwyg':
$this->render_wysiwyg_field( $field, $meta );
break;
case 'checkbox':
$this->render_checkbox_field( $field, $meta );
break;
case 'select':
$this->render_select_field( $field, $meta );
break;
case 'image':
$this->render_image_field( $field, $meta, $id );
break;
case 'repeatable':
$this->render_repeater_field( $field, $meta );
break;
}
?>
</td></tr>
<?php
}
?>
</table>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment