<!-- ******************this category list******************************************* -->
<?php
if (!defined( 'ABSPATH' )) exit;
if ( isset( $_POST['wp2appir_primery_category'] ) &&
     wp_verify_nonce( $_POST['wp2appir_primery_category'], 'wp2appir_category_primery' ) ) {
	if(isset($_POST['post_category']) || isset($_POST["tax_input"])){
		$cats = "";
		if(isset($_POST["tax_input"]) && !is_null($_POST["tax_input"]))
		{
			$product_cat = $_POST["tax_input"];
			foreach($product_cat["product_cat"] as $key){
				$cats .= $key.",";
			}
		}
		if(isset($_POST["post_category"]) &&  !is_null($_POST['post_category']))
		{
			foreach ($_POST['post_category'] as $key) {
				$cats .= $key.",";
			}
		}
		$cats = substr( $cats , 0 , strlen($cats)-1 );
		if ($cats == "0") {
			$cats = "";
		}
		$r = update_option('wp2app_cats' , $cats);
		if($r){
			update_option('hrt_lastmf_cat' , current_time( 'mysql' ));
		}
	}
}
?>
<?php
$type_post = get_option("wp2app_post_types");// read custom post_types
$ar = json_decode($type_post,true);
if($type_post == ""){
	$taxonomy = get_object_taxonomies("post"); //get taxonomy post_type
	$category = get_categories(array("taxonomy" => "category"));
	$cats_post_types[] = "category";
}else{
	foreach ($ar as $key ) {
		foreach ($key as $value) {
			foreach ($value as $k) {
				$cats_post_types[] = $k;
			}
		}
	}
}
?>
<div class="div_mainpage" >
    <div>
        <h4 class="background-color-e5e5e5 padding-7px">
			<?php _e('Categories', 'woo2app'); ?>
        </h4>
        <form method="post">
			<?php wp_nonce_field('wp2appir_category_primery' , 'wp2appir_primery_category'); ?>
            <table class="form-table width-600px">
                <tbody>
                <tr>
                    <td>
                        <p>
							<?php _e('Note: By default, all categories are selected.', 'woo2app'); ?>
                        </p>
                        <p style="color:red;"><?php _e('Note: To select the sub categories, the parent category must be selected.', 'woo2app'); ?></p>
                    </td>
                </tr>
                <tr>
                    <td id="category-0" class="popular-category">
                        <input type="hidden" id="txt_cat_custom" name="txt_cat_custom">
                        <input value="0" name="post_category[]" id="in-category-0" type="checkbox">
						<?php  _e('All Categories' , 'woo2app');?>
                    </td>
                </tr>
				<?php
				foreach ($cats_post_types as $key ) {
					$my_tax = get_taxonomy($key);
					?>
                    <tr >
                        <td >
                            <hr>
                            <div class="clearfix"></div>
							<?php
							echo post_categories_meta_box($key);
							?>
                        </td>
                    </tr>
					<?php
				}
				?>
                <tr >
                    <td >
                        <button  type="submit" class="button button-primary">
							<?php _e('Save Changes', 'woo2app'); ?>
                            <label class="fa fa-2x fa-save"></label>
                        </button>
                    </td>
                </tr>
                </tbody>
            </table>
        </form>
    </div>
</div>
<!-- ******************this category list******************************************* -->
<?php
$cat_selected = get_option('wp2app_cats');
?>
<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery('#category-all').addClass("koodak");
        jQuery('#product_cat-all').addClass("koodak");
        jQuery('#category-all').css({'font-weight':'100'});
        jQuery('#product_cat-all').css({'font-weight':'100'});
        var cat = "<?php echo $cat_selected; ?>";
        if(cat.length > 0)
        {
            var res = cat.split(",");
            jQuery.each( res, function( key, v ) {
                jQuery('#in-category-' + v).attr('checked','checked');
            });
            jQuery.each( res, function( key, v ) {
                jQuery('#in-product_cat-' + v).attr('checked','checked');
            });
        }else{
            jQuery('#in-category-0').attr('checked','checked');
            jQuery('#in-product_cat-0').attr('checked','checked');
        }
        jQuery('.selectit input:not(#in-category-0)').click(function(){
            jQuery('#in-category-0').attr('checked',false);
        });
        jQuery('.selectit_p input:not(#in-product_cat-0)').click(function(){
            alert(150);
            jQuery('#in-product_cat-0').attr('checked',false);
        });
        jQuery('#in-category-0').click(function(){
            jQuery('.selectit input:not(#in-category-0)').attr('checked','checked');
        });
        jQuery('#in-product_cat-0').click(function(){
            alert(200);
            jQuery('.selectit_p input:not(#in-product_cat-0)').attr('checked','checked');
        });
    });
</script>
<?php
function post_categories_meta_box($tax) {
	$defaults = array( 'taxonomy' => $tax );
	$args = array();
	$r = wp_parse_args( $args, $defaults );
	$tax_name = esc_attr( $r['taxonomy'] );
	$taxonomy = get_taxonomy( $r['taxonomy'] );
	?>
    <div id="taxonomy-<?php echo $tax_name; ?>" class="categorydiv">
        <div id="<?php echo $tax_name; ?>-all" class="">
            <ul id="<?php echo $tax_name; ?>checklist" data-wp-lists="list:<?php echo $tax_name; ?>" class="categorychecklist form-no-clear">
				<?php wp_terms_checklist( 0, array( 'taxonomy' => $tax_name, 'popular_cats' => false ) ); ?>
            </ul>
        </div>
    </div>
	<?php
}