/**
  * Adds meta box to any post type
  *
  * @uses      add_meta_box()
  *
  * @return    void
  *
  * @access    public
  * @since     1.0
  */
 function add_meta_boxes($term)
 {
     rj_ot_admin_scriptss();
     rj_ot_admin_styless();
     foreach ((array) $this->meta_box['id'] as $page) {
         $this->build_taxonomy_meta_box($term, $this->meta_box['fields'], $this->meta_box['id'], $this->meta_box['title']);
     }
 }
function rj_taxonomy_ot_save_settings()
{
    if (isset($_REQUEST['page']) && $_REQUEST['page'] == 'rj-ot-taxonomy_metabox') {
        rj_ot_admin_scriptss();
        rj_ot_admin_styless();
        wp_enqueue_script('rj_ot_taxonomy_script', plugin_dir_url(dirname(__FILE__)) . 'js/taxonomy-ot-metabox-ui-settings.js');
    }
    /* check and verify import settings nonce */
    if (isset($_POST['option_tree_settings_nonce']) && wp_verify_nonce($_POST['option_tree_settings_nonce'], 'rj_option_tree_settings_form') && isset($_GET['page']) && $_GET['page'] == 'rj-ot-taxonomy_metabox') {
        /* settings value */
        $settings = isset($_POST[ot_settings_id()]) ? $_POST[ot_settings_id()] : '';
        /* validate sections */
        if (isset($settings['sections'])) {
            /* fix numeric keys since drag & drop will change them */
            $settings['sections'] = array_values($settings['sections']);
            /* loop through sections */
            foreach ($settings['sections'] as $k => $section) {
                /* remove from array if missing values */
                if (!isset($section['title']) && !isset($section['id']) || '' == $section['title'] && '' == $section['id']) {
                    unset($settings['sections'][$k]);
                } else {
                    /* validate label */
                    if ('' != $section['title']) {
                        $settings['sections'][$k]['title'] = wp_kses_post($section['title']);
                    }
                    /* missing title set to unfiltered ID */
                    if (!isset($section['title']) || '' == $section['title']) {
                        $settings['sections'][$k]['title'] = wp_kses_post($section['id']);
                        /* missing ID set to title */
                    } else {
                        if (!isset($section['id']) || '' == $section['id']) {
                            $section['id'] = wp_kses_post($section['title']);
                        }
                    }
                    /* sanitize ID once everything has been checked first */
                    $settings['sections'][$k]['id'] = ot_sanitize_option_id(wp_kses_post($section['id']));
                }
            }
            $settings['sections'] = ot_stripslashes($settings['sections']);
        }
        /* validate settings by looping over array as many times as it takes */
        if (isset($settings['settings'])) {
            $settings['settings'] = ot_validate_settings_array($settings['settings']);
        }
        /* validate contextual_help */
        if (isset($settings['contextual_help']['content'])) {
            /* fix numeric keys since drag & drop will change them */
            $settings['contextual_help']['content'] = array_values($settings['contextual_help']['content']);
            /* loop through content */
            foreach ($settings['contextual_help']['content'] as $k => $content) {
                /* remove from array if missing values */
                if (!isset($content['title']) && !isset($content['id']) || '' == $content['title'] && '' == $content['id']) {
                    unset($settings['contextual_help']['content'][$k]);
                } else {
                    /* validate label */
                    if ('' != $content['title']) {
                        $settings['contextual_help']['content'][$k]['title'] = wp_kses_post($content['title']);
                    }
                    /* missing title set to unfiltered ID */
                    if (!isset($content['title']) || '' == $content['title']) {
                        $settings['contextual_help']['content'][$k]['title'] = wp_kses_post($content['id']);
                        /* missing ID set to title */
                    } else {
                        if (!isset($content['id']) || '' == $content['id']) {
                            $content['id'] = wp_kses_post($content['title']);
                        }
                    }
                    /* sanitize ID once everything has been checked first */
                    $settings['contextual_help']['content'][$k]['id'] = ot_sanitize_option_id(wp_kses_post($content['id']));
                }
                /* validate textarea description */
                if (isset($content['content'])) {
                    $settings['contextual_help']['content'][$k]['content'] = wp_kses_post($content['content']);
                }
            }
        }
        /* validate contextual_help sidebar */
        if (isset($settings['contextual_help']['sidebar'])) {
            $settings['contextual_help']['sidebar'] = wp_kses_post($settings['contextual_help']['sidebar']);
        }
        $settings['contextual_help'] = ot_stripslashes($settings['contextual_help']);
        /* default message */
        $message = 'failed';
        /* is array: save & show success message */
        if (is_array($settings)) {
            /* WPML unregister ID's that have been removed */
            if (function_exists('icl_unregister_string')) {
                $current = get_option(ot_settings_id());
                $options = get_option(ot_options_id());
                if (isset($current['settings'])) {
                    /* Empty ID array */
                    $new_ids = array();
                    /* Build the WPML IDs array */
                    foreach ($settings['settings'] as $setting) {
                        if ($setting['id']) {
                            $new_ids[] = $setting['id'];
                        }
                    }
                    /* Remove missing IDs from WPML */
                    foreach ($current['settings'] as $current_setting) {
                        if (!in_array($current_setting['id'], $new_ids)) {
                            if (!empty($options[$current_setting['id']]) && in_array($current_setting['type'], array('list-item', 'slider'))) {
                                foreach ($options[$current_setting['id']] as $key => $value) {
                                    foreach ($value as $ckey => $cvalue) {
                                        ot_wpml_unregister_string($current_setting['id'] . '_' . $ckey . '_' . $key);
                                    }
                                }
                            } else {
                                if (!empty($options[$current_setting['id']]) && $current_setting['type'] == 'social-icons') {
                                    foreach ($options[$current_setting['id']] as $key => $value) {
                                        foreach ($value as $ckey => $cvalue) {
                                            ot_wpml_unregister_string($current_setting['id'] . '_' . $ckey . '_' . $key);
                                        }
                                    }
                                } else {
                                    ot_wpml_unregister_string($current_setting['id']);
                                }
                            }
                        }
                    }
                }
            }
            update_option('rj_taxonomy_' . ot_settings_id(), $settings);
            $message = 'success';
        }
        /* redirect */
        wp_redirect(add_query_arg(array('action' => 'save-settings', 'message' => $message), $_POST['_wp_http_referer']));
        exit;
    }
    return false;
}
        /**
         * Adds meta box to any post type
         *
         * @uses      add_meta_box()
         *
         * @return    void
         *
         * @access    public
         * @since     1.0
         */
        function add_widgets($widget_data)
        {
            global $page_now;
            if (is_admin()) {
                rj_ot_admin_scriptss();
                rj_ot_admin_styless();
            }
            $widget_class_name = 'Dynamic_Widget_' . $widget_data['id'];
            $widget_datas = var_export($widget_data, true);
            $dynamic_class = '
       if(!class_exists("' . $widget_class_name . '")){
            class ' . $widget_class_name . ' extends WP_Widget {
            
               public $widget_datas =  ' . $widget_datas . ';
                function ' . $widget_class_name . '() {
                
                    $widget_ops = array("classname" => "' . $widget_class_name . '"
                                        , "description" => "' . $widget_data["description"] . '" );
                    $this->WP_Widget("' . $widget_class_name . '", "' . $widget_data["title"] . '", $widget_ops);           
                }

                function form($instance) {
                
               
               $dynamic_instances = array("title"=>"");
               $fields = $this->widget_datas["fields"];
               if(!empty($fields)){
                   foreach( $fields as  $field ){
                   $dynamic_instances[$field["id"]]=$field["std"];
                   }
               }
               
               
                    $instance = wp_parse_args( (array) $instance, $dynamic_instances );
                    
                   $field_data = array();
                   $field_keys = array();
                   foreach($dynamic_instances as $key=>$val){
                  $field_data[$this->get_field_name($key)] = $instance[$key];
                  $field_keys[$key] = $this->get_field_name($key);
                   }
                    
                    $title = $instance["title"];
                    echo "<p><label for=\\"" . $this->get_field_id("title") . "\\">Title: <input class=\\"widefat\\" id=\\"";
                    echo $this->get_field_id("title") . "\\" name=\\"" . $this->get_field_name("title") . "\\" type=\\"text\\" value=\\"" . attribute_escape($title) . "\\" /></label></p>";
                    RJ_OT_Widget_Meta_Box::build_widget_meta_box("",$this->widget_datas,$instance,$field_data,$field_keys);
                    
                    
                }

                function update($new_instance, $old_instance) {
                $instance = $old_instance;
                 $dynamic_instances[0] = array("id"=>"title");
                $fields = $this->widget_datas["fields"];
                 $fields = array_merge( $fields,$dynamic_instances);
                
               if(!empty($fields)){
                   foreach( $fields as  $field ){
                    $fid = $field["id"];
                    
                    $instance[$fid] = $new_instance[$fid];
                   }
               }
               
                
                    return $instance;
                }

                function widget($args, $instance) {
                $data = array_merge($args,$instance);
                $widget_template = $this->widget_datas["widget_template"];
                $engine = new TemplateEngine();
                if(has_action("wp_widget_design_' . $widget_data['id'] . '")) {
                 do_action("wp_widget_design_' . $widget_data['id'] . '",$args,$instance);
                }else{ 
                    echo do_shortcode($engine->process($widget_template, $data));
                
                }
                  
                }
                
               
            };
            }
        ';
            //echo $dynamic_class;
            eval($dynamic_class);
            register_widget($widget_class_name);
        }