<?php
//Allow add script to Wp codes if webmanager
add_filter( 'wp_kses_allowed_html', function ( $tags, $context ) {
	if(current_user_can("webmanager") || current_user_can("administrator")){
		$tags['script'] = array(
			'src'          => true,
			'async'       => true,
		);		
	}

    return $tags;
}, 10, 2 );


//Stopping WordPress from Automatically Emptying Trash
if( ds1_custom_is_bool_tester('stop_empty_trash') ) {
	add_action( 'init', function () {
		remove_action( 'wp_scheduled_delete', 'wp_scheduled_delete' );
	} );
}



//Allow HTML in term (category, tag) descriptions
if(ds1_custom_is_bool_tester('allow_html_in_terms')) :

    foreach ( array( 'pre_term_description' ) as $filter ) {
        remove_filter( $filter, 'wp_filter_kses' );
        remove_filter('term_description','wpautop');
    }
    foreach ( array( 'term_description' ) as $filter ) {
        remove_filter( $filter, 'wp_kses_data' );
        remove_filter('term_description','wpautop'); 
    }

	add_filter('category_edit_form_fields', 'edit_cat_description');
	add_filter('typecat_edit_form_fields', 'edit_cat_description');
	add_filter('locations_edit_form_fields', 'edit_cat_description');
    add_filter('post_tag_edit_form_fields', 'edit_cat_description');
    
	function edit_cat_description($category) {
		// $tag_extra_fields = get_option(description1);?>
		<table class="form-table">
		<tr class="form-field">
		<th scope="row" valign="top"><label for="description"><?php _ex('Description', 'Taxonomy Description'); ?></label></th>
		<td>
		<?php     $settings = array('wpautop' => true, 'media_buttons' => true, 'quicktags' => true, 'textarea_rows' => '15', 'textarea_name' => 'description' );
		wp_editor(html_entity_decode($category->description , ENT_QUOTES, 'UTF-8'), 'description1', $settings); ?>
		<br />
		<span class="description"><?php _e('The description is not prominent by default, however some themes may show it.'); ?></span>
		</td>
		</tr>
		</table>
	<?php
	}
	 
	add_action( 'admin_print_styles', 'category_tinymce_css' );
	function category_tinymce_css() { ?>    <style type="text/css">
		.quicktags-toolbar input{float:left !important; width:auto !important;}
		</style>
	<?php 
	}
	 
	add_action('category_add_form_fields','add_cat_description');
	add_action('post_tag_add_form_fields','add_cat_description');
	add_action('typecat_add_form_fields','add_cat_description');
	add_action('locations_add_form_fields','add_cat_description');

	function add_cat_description($tag) {
		// $tag_extra_fields = get_option(description1);?>
		<table class="form-table">
		<tr class="form-field">
		<th scope="row" valign="top"><label for="description"><?php _e('Description'); ?></label></th></tr><tr class="form-field">
		<td>
		<?php     
		    $settings = array('wpautop' => true, 'media_buttons' => true, 'quicktags' => true, 'textarea_rows' => '15', 'textarea_name' => 'description' );
		    $info =($tag && ! is_string($tag)) ? $tag->description : '';

    		   
    		wp_editor(html_entity_decode( $info, ENT_QUOTES, 'UTF-8'), 'description1', $settings); 
    		    
    
		?>    <br />
		<span class="description"><?php _e('The description is not prominent by default, however some themes may show it.'); ?></span>
		</td>
		</tr>
		</table>
	<?php 
	} 
	 
	add_action('admin_head', 'taxonomy_tinycme_hide_description');
	function taxonomy_tinycme_hide_description() {
	global $pagenow; 
	//echo $pagenow;exit;
	 
	//only hide on detail not yet on the overview page.
		if( ($pagenow == 'edit-tags.php' || isset($_GET['taxonomy']) )) :    ?>    <script type="text/javascript">
		jQuery(function($) {
			$('#description, textarea#tag-description').closest('.form-field').hide();
		});
		</script><?php endif;
	}

endif;

