Skip to content

Instantly share code, notes, and snippets.

@sohan5005
Created April 1, 2016 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sohan5005/b560d20a3bfbfb82e64ea02f8694b7c9 to your computer and use it in GitHub Desktop.
Save sohan5005/b560d20a3bfbfb82e64ea02f8694b7c9 to your computer and use it in GitHub Desktop.
Using Codestar Framework Fields in taxonomy.
<?php
/**
* Taxonomy term options for Codestar Framework: https://github.com/Codestar/codestar-framework
*
* Contributed by http://themestones.net
*/
if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access pages directly.
if( ! class_exists( 'CSFramework_Taxonomy' ) ) :
class CSFramework_Taxonomy {
/**
* CSFramework_Taxonomy::args
*
* Stores arguments for every metabox group
*
* @var type array
*/
public $args = array();
/**
* CSFramework_Taxonomy:__construct()
* Main constructor
*/
public function __construct( $args ) {
if( is_array( $args ) ) {
foreach ( $args as $arg ) {
$this->execute( $arg );
}
} else {
return new WP_Error( __( 'No valid options given', 'lumen' ) );
}
}
/**
* CSFramework_Taxonomy:execute()
* Executes each option
*/
public function execute( $args ) {
if( !isset( $args['taxonomy'] ) ) {
return; // don't execute if no taxonomy found
}
foreach( (array) $args['taxonomy'] as $tax ) {
$this->args[$tax] = $args;
add_action( $tax . '_edit_form', array( $this, 'render' ), 9999, 2 );
add_action( 'edited_' . $tax, array( $this, 'save' ), 9999, 2);
add_action( $tax . '_add_form_fields', array( $this, 'render' ), 9999, 2 );
add_action( 'create_' . $tax, array( $this, 'save' ), 9999, 2 );
}
}
/**
* CSFramework_Taxonomy:render()
* Renders each option
*/
public function render( $args, $tax = null ) {
global $cs_errors;
$tax = $tax == null ? $args : $tax;
$callback = array(
'args' => $this->args[$tax]
);
$term_id = is_object( $args ) && $args->term_id ? $args->term_id : '' ;
wp_nonce_field( 'cs-framework-metabox', 'cs-framework-metabox-nonce' );
$unique = $callback['args']['id'];
$sections = $callback['args']['sections'];
$meta_value = get_term_meta( $term_id, $unique, true );
$transient = get_transient( 'cs-metabox-transient' );
$cs_errors = isset( $transient['errors'] ) ? $transient['errors'] : $cs_errors;
$has_nav = ( count( $sections ) >= 2 ) ? true : false;
$show_all = ( ! $has_nav ) ? ' cs-show-all' : '';
$section_name = ( ! empty( $sections[0]['fields'] ) ) ? $sections[0]['name'] : $sections[1]['name'];
$section_id = ( ! empty( $transient['ids'][$unique] ) ) ? $transient['ids'][$unique] : $section_name;
$section_id = ( ! empty( $_GET['cs-section'] ) ) ? esc_attr( $_GET['cs-section'] ) : $section_id;
echo '<div class="cs-framework cs-metabox-framework" style="margin: 1.5em 0.1em; width: 95%;">';
echo '<input type="hidden" name="cs_section_id['. $unique .']" class="cs-reset" value="'. $section_id .'">';
echo '<div class="cs-body'. $show_all .'">';
if( $has_nav ) {
echo '<div class="cs-nav">';
echo '<ul>';
foreach( $sections as $value ) {
$tab_icon = ( ! empty( $value['icon'] ) ) ? '<i class="cs-icon '. $value['icon'] .'"></i>' : '';
if( isset( $value['fields'] ) ) {
$active_section = ( $section_id == $value['name'] ) ? ' class="cs-section-active"' : '';
echo '<li><a href="#"'. $active_section .' data-section="'. $value['name'] .'">'. $tab_icon . $value['title'] .'</a></li>';
} else {
echo '<li><div class="cs-seperator">'. $tab_icon . $value['title'] .'</div></li>';
}
}
echo '</ul>';
echo '</div>';
}
echo '<div class="cs-content">';
echo '<div class="cs-sections">';
foreach( $sections as $val ) {
if( isset( $val['fields'] ) ) {
$active_content = ( $section_id == $val['name'] ) ? ' style="display: block;"' : '';
echo '<div id="cs-tab-'. $val['name'] .'" class="cs-section"'. $active_content .'>';
echo ( isset( $val['title'] ) ) ? '<div class="cs-section-title"><h3>'. $val['title'] .'</h3></div>' : '';
foreach ( $val['fields'] as $field_key => $field ) {
$default = ( isset( $field['default'] ) ) ? $field['default'] : '';
$elem_id = ( isset( $field['id'] ) ) ? $field['id'] : '';
$elem_value = ( is_array( $meta_value ) && isset( $meta_value[$elem_id] ) ) ? $meta_value[$elem_id] : $default;
echo cs_add_element( $field, $elem_value, $unique );
}
echo '</div>';
}
}
echo '</div>';
echo '<div class="clear"></div>';
echo '</div>';
echo ( $has_nav ) ? '<div class="cs-nav-background"></div>' : '';
echo '<div class="clear"></div>';
echo '</div>';
echo '</div>';
}
/**
* CSFramework_Taxonomy:save()
* Saves each option
*/
public function save( $term_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; }
$nonce = ( isset( $_POST['cs-framework-metabox-nonce'] ) ) ? $_POST['cs-framework-metabox-nonce'] : '';
if ( ! wp_verify_nonce( $nonce, 'cs-framework-metabox' ) ) { return; }
$errors = array();
$taxonomy = ( isset( $_POST['taxonomy'] ) ) ? $_POST['taxonomy'] : '';
foreach ( $this->args as $tax => $request_value ) {
if( in_array( $taxonomy, (array) $request_value['taxonomy'] ) ) {
$request_key = $request_value['id'];
$meta_value = get_term_meta( $term_id, $request_key, true );
$request = ( isset( $_POST[$request_key] ) ) ? $_POST[$request_key] : array();
// ignore _nonce
if( isset( $request['_nonce'] ) ) {
unset( $request['_nonce'] );
}
foreach( $request_value['sections'] as $key => $section ) {
if( isset( $section['fields'] ) ) {
foreach( $section['fields'] as $field ) {
if( isset( $field['type'] ) && isset( $field['id'] ) ) {
$field_value = ( isset( $_POST[$request_key][$field['id']] ) ) ? $_POST[$request_key][$field['id']] : '';
// sanitize options
if( isset( $field['sanitize'] ) && $field['sanitize'] !== false ) {
$sanitize_type = $field['sanitize'];
} else if ( ! isset( $field['sanitize'] ) ) {
$sanitize_type = $field['type'];
}
if( has_filter( 'cs_sanitize_'. $sanitize_type ) ) {
$request[$field['id']] = apply_filters( 'cs_sanitize_' . $sanitize_type, $field_value, $field, $section['fields'] );
}
// validate options
if ( isset( $field['validate'] ) && has_filter( 'cs_validate_'. $field['validate'] ) ) {
$validate = apply_filters( 'cs_validate_' . $field['validate'], $field_value, $field, $section['fields'] );
if( ! empty( $validate ) ) {
$errors[$field['id']] = array( 'code' => $field['id'], 'message' => $validate, 'type' => 'error' );
$default_value = isset( $field['default'] ) ? $field['default'] : '';
$request[$field['id']] = ( isset( $meta_value[$field['id']] ) ) ? $meta_value[$field['id']] : $default_value;
}
}
}
}
}
}
$request = apply_filters( 'cs_save_term', $request, $request_key, $meta_value, $this );
if( empty( $request ) ) {
delete_term_meta( $term_id, $request_key );
} else {
if( get_term_meta( $term_id, $request_key ) ) {
update_term_meta( $term_id, $request_key, $request );
} else {
add_term_meta( $term_id, $request_key, $request );
}
}
$transient['ids'][$request_key] = $_POST['cs_section_id'][$request_key];
$transient['errors'] = $errors;
}
}
set_transient( 'cs-metabox-transient', $transient, 10 );
}
}
endif;
@sohan5005
Copy link
Author

Usage:

$options      = array();

$options[]    = array(
  'id'        => '_custom_term_options',
  'title'     => 'Custom Page Options',
  'taxonomy' => 'category',
  'sections'  => array(

    // begin: a section
    array(
      'name'  => 'section_1',
      'title' => 'Section 1',
      'icon'  => 'fa fa-cog',

      // begin: fields
      'fields' => array(

        // begin: a field
        array(
          'id'    => 'section_1_text',
          'type'  => 'text',
          'title' => 'Text Field',
        ),
        // end: a field

        array(
          'id'    => 'section_1_textarea',
          'type'  => 'textarea',
          'title' => 'Textarea Field',
        ),

        array(
          'id'    => 'section_1_upload',
          'type'  => 'upload',
          'title' => 'Upload Field',
        ),

        array(
          'id'    => 'section_1_switcher',
          'type'  => 'switcher',
          'title' => 'Switcher Field',
          'label' => 'Yes, Please do it.',
        ),

      ), // end: fields
    ), // end: a section

    // begin: a section
    array(
      'name'  => 'section_2',
      'title' => 'Section 2',
      'icon'  => 'fa fa-tint',
      'fields' => array(

        array(
          'id'      => 'section_2_color_picker_1',
          'type'    => 'color_picker',
          'title'   => 'Color Picker 1',
          'default' => '#2ecc71',
        ),

        array(
          'id'      => 'section_2_color_picker_2',
          'type'    => 'color_picker',
          'title'   => 'Color Picker 2',
          'default' => '#3498db',
        ),

        array(
          'id'      => 'section_2_color_picker_3',
          'type'    => 'color_picker',
          'title'   => 'Color Picker 3',
          'default' => '#9b59b6',
        ),

        array(
          'id'      => 'section_2_color_picker_4',
          'type'    => 'color_picker',
          'title'   => 'Color Picker 4',
          'default' => '#34495e',
        ),

        array(
          'id'      => 'section_2_color_picker_5',
          'type'    => 'color_picker',
          'title'   => 'Color Picker 5',
          'default' => '#e67e22',
        ),

      ),
    ),
    // end: a section

  ),
);

new CSFramework_Taxonomy($options);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment