Created
April 1, 2016 07:09
-
-
Save sohan5005/b560d20a3bfbfb82e64ea02f8694b7c9 to your computer and use it in GitHub Desktop.
Using Codestar Framework Fields in taxonomy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: