2017-09-05 43 views
-1

当在自定义WordPress页面上放置短代码时,输​​出始终显示在页面内容的顶部。WP短代码输出始终在页面顶部

我发现,这个问题可以通过使用回报,而不是回声是固定的,或者by using output buffering(ob_start()/ob_get_contents())

不幸的是我的编码技能不是我想他们是..我不知道确切地在哪里实施这些修复。

有人可以帮我吗?插件开发人员没有回复我的邮件,我需要让这个工作a.s.a.p.

我认为这需要在故障插件的函数文件中实现,所以我在下面添加了它。

<?php 
 
/** 
 
* Woocommerce Category Accordion Functions 
 
* 
 
* @author \t \t TechieResource 
 
* @category \t Shortcode 
 
* @package \t woocommerce-category-accordion/inc 
 
* @version \t 1.2.1 
 
*/ 
 
/** 
 
    
 
/* Clean variables 
 
* 
 
* @param string $var 
 
* @return string 
 
*/ 
 
if (! defined('ABSPATH')) { 
 
\t exit; 
 
} // Exit if accessed directly 
 

 
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) { 
 
\t 
 
class trwca_wc_category_accordion{ 
 
\t 
 
\t \t /** 
 
\t \t * Constructor 
 
\t \t */ 
 
\t  private $shortcode_tag = "WC-Category-Accordion"; 
 
\t \t 
 
\t \t public function __construct() { 
 
\t \t 
 
\t \t add_action('woocommerce_category_accordion', array($this, 'woocommerce_category_accordion_func'), 10, 2); 
 
\t \t add_shortcode($this->shortcode_tag, array($this, 'wc_category_accordion_sc')); 
 
\t \t 
 
\t \t \t if (is_admin()){ 
 
\t \t \t \t add_action('admin_head', array($this, 'admin_head')); 
 
\t \t \t \t add_action('admin_enqueue_scripts', array($this , 'admin_enqueue_scripts')); 
 
\t \t \t } 
 
\t \t } 
 

 

 
\t \t /** 
 
\t \t * Display the Woocommerce Category Accordion. 
 
\t \t * @since 1.2.1 
 
\t \t * @param array $instance Arguments. 
 
\t \t * @param bool $echo Whether to display or return the output. 
 
\t \t * @return string 
 
\t \t */ 
 
\t \t public function woocommerce_category_accordion_func($instance, $echo = true) { 
 

 
\t \t \t extract($instance, EXTR_SKIP); 
 

 
\t \t \t global $wp_query; \t \t 
 
\t \t 
 
\t \t \t global $post, $product; \t 
 
\t 
 
\t \t \t $exclude_tree = esc_attr($exclude_tree); \t \t 
 
\t \t 
 
\t \t \t $hide_empty  = esc_attr($hide_empty); 
 
\t \t \t 
 
\t \t \t $show_count  = esc_attr($show_count); 
 
\t \t \t 
 
\t \t \t $open_cat  = esc_attr($open_cat); \t \t 
 
\t \t \t 
 
\t \t \t $ac_speed  = esc_attr($ac_speed); 
 
\t \t \t 
 
\t \t \t $ac_type  = esc_attr($ac_type); 
 
\t \t \t 
 
\t \t \t $event_type  = esc_attr($event_type); 
 
\t \t \t 
 
\t \t \t $ac_icon  = esc_attr($ac_icon); 
 
\t \t \t 
 
\t \t \t $sortby   = esc_attr($sortby); 
 
\t \t \t 
 
\t \t \t $ac_theme  = esc_attr($ac_theme); 
 
\t \t \t 
 
\t \t \t $order   = esc_attr($order); 
 
\t \t \t 
 
\t \t \t $level   = esc_attr($level); 
 
\t \t \t 
 
\t \t \t $cats_id  = esc_attr($ac_opencat); 
 
\t \t \t 
 
\t \t \t $disable_parent = esc_attr($disable_parent); 
 
\t \t \t 
 
\t \t \t $disable_aclink = esc_attr($disable_aclink); 
 
\t \t \t \t \t 
 
\t \t \t if(!empty($instance['id'])){ 
 
\t \t \t \t 
 
\t \t \t \t $widgetid= $instance['id']; 
 
\t \t \t } 
 
\t \t \t else{ 
 
\t \t \t \t if($sh_id!=""){ 
 
\t \t \t \t \t $widgetid= "wc_category_accordion-".$sh_id; \t 
 
\t \t \t \t } 
 
\t \t \t \t else{ 
 
\t \t \t \t \t $widgetid= "wc_category_accordion-".$this->trwca_generate_random_code(3); 
 
\t \t \t \t } 
 

 
\t \t \t } 
 
\t \t 
 
\t \t \t $instance_categories = get_terms('product_cat', '&parent=0&exclude='.$exclude_tree.''); \t \t 
 
\t \t \t \t 
 
\t \t \t if (is_array($instance_categories)) { 
 
\t 
 
\t \t \t \t foreach($instance_categories as $categories) { 
 
\t 
 
\t \t \t \t \t $term_id[] = $categories->term_id; 
 
\t 
 
\t \t \t \t \t $term_name = $categories->name; 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t 
 
\t \t \t if(!empty($post->ID)){ 
 
\t \t \t \t 
 
\t \t \t \t $terms =get_the_terms($post->ID, 'product_cat'); 
 
\t \t \t } 
 
\t \t \t else { 
 
\t \t \t 
 
\t \t \t \t $terms=""; 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t if (is_array($terms)) { 
 
\t 
 
\t \t \t \t foreach ($terms as $term) { 
 
\t 
 
\t \t \t \t \t $_cat=$term->term_id; 
 
\t 
 
\t \t \t \t \t break; 
 
\t 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t 
 
     \t \t /* For current category highlight */ 
 
\t \t \t if(is_product()){ 
 
\t \t \t 
 
\t \t \t $current_cat= array(); 
 
\t \t \t 
 
\t \t \t $cat = $wp_query->get_queried_object(); \t \t 
 
\t \t \t \t 
 
\t \t \t if (!empty($cat->term_id)) 
 
\t 
 
\t \t \t { 
 
\t \t \t \t $current_cat = $cat->term_id; 
 
\t 
 
\t \t \t } 
 
\t \t \t else{     
 
\t \t \t \t $_cat_id="1"; 
 
\t 
 
\t \t \t \t if (isset($term->term_id)) 
 
\t 
 
\t \t \t \t \t { 
 
\t \t \t \t \t $_cat=$term->term_id; 
 
\t 
 
\t \t \t \t \t $_cat_id = !empty($_cat) ? $_cat_id=$_cat : $_cat_id=1 ; 
 
\t \t \t \t \t 
 
\t \t \t \t \t } 
 
\t \t \t \t \t if (is_shop()) 
 
\t 
 
\t \t \t \t \t { 
 
\t \t \t \t \t \t $_cat_id="1"; 
 
\t \t \t \t \t } 
 
\t \t \t \t \t if (!is_shop()){ 
 
\t \t \t \t \t \t 
 
\t \t \t \t \t \t if (is_array($terms)) { \t \t \t \t \t 
 
\t \t \t \t \t 
 
\t \t \t \t \t \t \t foreach($terms as $term){ 
 
\t \t \t \t \t \t \t \t $myterms[]= $term->term_id; 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t $cats_id=end($myterms); 
 
?> 
 
\t <script type="text/javascript"> 
 
\t 
 
\t var cats_id= <?php return end($myterms); ?>; 
 
\t 
 
\t </script> 
 
\t <style type="text/css"> 
 
<?php foreach((get_the_terms($post->ID, 'product_cat')) as $term) { 
 
\t \t \t $myterms= $term->term_id; 
 
\t \t \t return 'ul.'.$widgetid.' li.cat-item-'.$myterms.' > a{font-weight:bold;}'; 
 
\t \t } 
 
\t \t \t \t \t } 
 
\t \t \t } 
 
?> 
 
\t </style> 
 
<?php    
 
\t } 
 
\t \t \t } 
 
\t $cat = $wp_query->get_queried_object(); 
 
\t \t \t \t \t 
 
\t \t if (!empty($cat->term_id) && !is_product()){ 
 
\t \t \t 
 
\t \t \t $cats_id = $cat->term_id; 
 
\t \t \t return '<script type="text/javascript"> 
 
\t \t \t 
 
\t \t \t var cats_id= '.$cats_id.'; 
 
\t \t \t 
 
\t \t \t </script>'; 
 
\t \t } 
 
\t \t else if(!is_product_category() && !is_product()){ 
 
\t 
 
\t  $cats_id=$ac_opencat; 
 
\t \t } 
 
\t $ac_type = $ac_type=="toggle" ? $ac_type= "true" : $ac_type= "false"; 
 
\t 
 
\t $open_cat = $open_cat== true || $open_cat =='on' ? $open_cat=true : $open_cat=false; 
 
\t \t \t \t \t 
 
\t \t \t 
 

 
/* Icon Selection */ 
 

 
\t switch ($ac_icon) { 
 
\t \t \t case 'angle' : \t \t \t \t \t 
 
\t \t \t \t $open_icon="angle-down"; \t \t \t \t 
 
\t \t \t \t $close_icon="angle-right"; 
 
\t \t \t \t break; 
 
\t \t \t \t 
 
\t \t \t case 'doubleangle' : 
 
\t \t \t \t $open_icon="angle-double-down"; 
 
\t \t \t \t $close_icon="angle-double-right"; 
 
\t \t \t \t break; 
 
\t \t \t \t 
 
\t \t \t case 'arrow-circle1' : 
 
\t \t \t \t $open_icon="arrow-circle-down"; 
 
\t \t \t \t $close_icon="arrow-circle-right"; 
 
\t \t \t \t break; 
 
\t \t \t \t \t 
 
\t \t \t case 'arrow-circle2' : \t \t \t \t \t 
 
\t \t \t \t $open_icon="arrow-circle-o-down"; \t \t \t \t 
 
\t \t \t \t $close_icon="arrow-circle-o-right"; 
 
\t \t \t \t break; 
 
\t \t \t \t 
 
\t \t \t case 'arrow-right' : 
 
\t \t \t \t $open_icon="arrow-down"; 
 
\t \t \t \t $close_icon="arrow-right"; 
 
\t \t \t \t break; 
 
\t \t \t \t 
 
\t \t \t case 'caret' : 
 
\t \t \t \t $open_icon="caret-down"; 
 
\t \t \t \t $close_icon="caret-right"; 
 
\t \t \t \t break; 
 
\t \t \t 
 
\t \t \t case 'caret-square' : \t \t \t \t \t 
 
\t \t \t \t $open_icon="caret-square-o-down"; \t \t \t \t 
 
\t \t \t \t $close_icon="caret-square-o-right"; 
 
\t \t \t \t break; 
 
\t \t \t \t 
 
\t \t \t case 'chevron' : 
 
\t \t \t \t $open_icon="chevron-down"; 
 
\t \t \t \t $close_icon="chevron-right"; 
 
\t \t \t \t break; 
 
\t \t \t \t 
 
\t \t \t case 'chevron-circle' : 
 
\t \t \t \t $open_icon="chevron-circle-down"; 
 
\t \t \t \t $close_icon="chevron-circle-right"; 
 
\t \t \t \t break; 
 
\t \t \t \t 
 
\t \t \t case 'hand' : 
 
\t \t \t \t $open_icon="hand-o-down"; 
 
\t \t \t \t $close_icon="hand-o-right"; 
 
\t \t \t \t break; 
 
\t \t \t \t 
 
\t \t \t case 'plus' : 
 
\t \t \t \t $open_icon="minus"; 
 
\t \t \t \t $close_icon="plus"; 
 
\t \t \t \t break; 
 
\t \t \t \t 
 
\t \t \t case 'plus-circle' : 
 
\t \t \t \t $open_icon="minus-circle"; 
 
\t \t \t \t $close_icon="plus-circle"; 
 
\t \t \t \t break; 
 
\t \t \t 
 
\t \t \t case 'plus-square1' : 
 
\t \t \t \t $open_icon="minus-square"; 
 
\t \t \t \t $close_icon="plus-square"; 
 
\t \t \t \t break; 
 
\t \t \t \t 
 
\t \t \t case 'plus-square2' : 
 
\t \t \t \t $open_icon="minus-square-o"; 
 
\t \t \t \t $close_icon="plus-square-o"; 
 
\t \t \t \t break; \t \t \t \t \t \t 
 
\t } 
 
\t 
 
\t 
 
\t if($disable_aclink==true){ 
 
\t \t 
 
\t \t $disable_aclink='true'; 
 
\t } 
 
\t else if($disable_aclink==""){ 
 
\t 
 
\t \t $disable_aclink= 'false'; 
 
\t } 
 
\t if($disable_parent==true){ 
 
\t \t 
 
\t \t $disable_parent='true'; 
 
\t } 
 
\t else if($disable_parent==""){ 
 
\t 
 
\t \t $disable_parent='false'; 
 
\t 
 
\t } 
 
\t $cats_id= empty($cats_id) ? 1 : $cats_id; 
 
?> 
 
<script type="text/javascript"> 
 
var $=jQuery.noConflict(); 
 
$(document).ready(function($){ \t 
 
$('.<?php echo $widgetid; ?>').trwcAccordion({ 
 
\t \t \t classParent \t : 'trwca-parent', 
 
\t \t \t classActive \t : 'active', 
 
\t \t \t classArrow \t : 'trwca-icon', 
 
\t \t \t classCount \t : 'trwca-count', 
 
\t \t \t classExpand \t : 'trwca-current-parent', 
 
\t \t \t eventType \t : '<?php echo $event_type; ?>', 
 
\t \t \t hoverDelay \t : 100, 
 
\t \t \t menuClose  : true, 
 
\t \t \t cats_id: <?php echo $cats_id; ?>, 
 
\t \t \t ac_type : <?php echo $ac_type; ?>, 
 
\t \t \t autoExpand \t : true, 
 
\t \t \t speed  : '<?php echo $ac_speed ?>', 
 
\t \t \t saveState \t : '<?php echo $open_cat; ?>', 
 
\t \t \t disableLink \t : <?php echo $disable_aclink; ?>, 
 
\t \t \t disableparentLink : <?php echo $disable_parent; ?>, 
 
\t \t \t auto_open: 1, 
 
\t \t \t showCount : true, 
 
\t \t \t widget_id : "<?php echo $widgetid; ?>", 
 
\t \t \t openIcon \t : '<?php echo $open_icon; ?>', 
 
\t \t \t closeIcon \t : '<?php echo $close_icon; ?>', 
 
}); 
 
}); 
 
</script> 
 
<div class="block-content trwca-actheme <?php echo $ac_theme; ?>"> 
 
<div class="trwca-loader"></div> 
 
<ul class="<?php echo $widgetid; ?> accordion" id="outer_ul"> 
 
<?php 
 

 
$subcat_args = array(
 

 
\t \t \t \t 'taxonomy' => 'product_cat', 
 
\t \t \t \t 'title_li' => '', 
 
\t \t \t \t 'orderby' => $sortby, 
 
\t \t \t \t 'order' => $order, 
 
\t \t \t \t 'depth' => $level, 
 
\t \t \t \t 'show_count' => $show_count, 
 
\t \t \t \t 'hide_empty' => $hide_empty, 
 
\t \t \t \t 'use_desc_for_title' => 1, 
 
\t \t \t \t 'echo' => false, 
 
\t \t \t \t 'exclude' => $exclude_tree, 
 
\t \t \t \t 'hierarchical' => true , 
 
\t \t \t \t 'show_option_none' => __('No Categories Found','trwca'), 
 
\t \t \t \t 'link_after' => '', 
 
\t \t \t \t 'walker'=> new trwca_walker 
 
\t \t \t ); 
 
\t \t \t \t \t \t 
 
?> 
 
<?php $subcategories = wp_list_categories($subcat_args); 
 

 
\t $subcategories=preg_replace_callback(
 
      '/<\/a> \(([0-9]+)\)/', 
 
      function ($matches) { 
 
       return '&nbsp;<span class="count">('.($matches[1]).')</span></a>'; 
 
      },$subcategories 
 
     ); 
 
\t \t 
 
?> 
 
<?php if ($subcategories) { 
 

 
\t \t \t echo $subcategories; 
 

 
      } 
 
?> 
 
</ul> 
 
</div> 
 
<?php 
 

 
\t \t } 
 
\t \t public function trwca_generate_random_code($length=3) { 
 

 
    \t \t $string = ''; 
 
    \t \t 
 
\t \t $characters = "123456789"; 
 

 
    \t \t for ($p = 0; $p < $length; $p++) { 
 
     \t \t $string .= $characters[mt_rand(0, strlen($characters)-1)]; 
 
    \t \t \t } 
 

 
    \t \t return $string; 
 

 
\t \t } 
 
\t \t /** 
 
\t \t * admin_head 
 
\t \t * calls your functions into the correct filters 
 
\t \t * @return void 
 
\t \t */ 
 
\t \t function admin_head() { 
 
\t \t \t // check user permissions 
 
\t \t \t if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) { 
 
\t \t \t \t return; 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t // check if WYSIWYG is enabled 
 
\t \t \t if ('true' == get_user_option('rich_editing')) { 
 
\t \t \t \t add_filter('mce_external_plugins', array($this ,'mce_external_plugins')); 
 
\t \t \t \t add_filter('mce_buttons', array($this, 'mce_buttons')); 
 
\t \t \t } 
 
\t \t } 
 
\t 
 
\t \t /** 
 
\t \t * mce_external_plugins 
 
\t \t * Adds our tinymce plugin 
 
\t \t * @param array $plugin_array 
 
\t \t * @return array 
 
\t \t */ 
 
\t \t function mce_external_plugins($plugin_array) { 
 
\t \t \t 
 
\t \t \t $plugin_array['WC_Category_Accordion'] = plugins_url('admin/js/mce-button.js' , __FILE__); 
 
\t \t \t return $plugin_array; 
 
\t \t } 
 
\t 
 
\t \t /** 
 
\t \t * mce_buttons 
 
\t \t * Adds our tinymce button 
 
\t \t * @param array $buttons 
 
\t \t * @return array 
 
\t \t */ 
 
\t \t function mce_buttons($buttons) { 
 
\t \t \t array_push($buttons, 'WC_Category_Accordion'); 
 
\t \t \t return $buttons; 
 
\t \t } 
 
\t 
 
\t \t /** 
 
\t \t * admin_enqueue_scripts 
 
\t \t * Used to enqueue custom styles 
 
\t \t * @return void 
 
\t \t */ 
 
\t \t function admin_enqueue_scripts(){ 
 
\t \t \t wp_enqueue_style('WC-Category-Accordion-sh', plugins_url('admin/css/mce-button.css' , __FILE__)); 
 
\t \t } 
 
\t \t public function wc_category_accordion_sc($atts, $content = null) { 
 
\t \t \t \t 
 
\t \t \t \t $defaults = array( 
 
\t \t \t \t \t \t \t 'show_count' => 0, 
 
\t \t \t \t \t \t \t 'ac_speed' => 'fast', 
 
\t \t \t \t \t \t \t 'exclude_tree' =>'', 
 
\t \t \t \t \t \t \t 'hide_empty' => 0, 
 
\t \t \t \t \t \t \t 'sortby' =>'name', 
 
\t \t \t \t \t \t \t 'order' =>'ASC', 
 
\t \t \t \t \t \t \t 'level' => 0, 
 
\t \t \t \t \t \t \t 'event_type' => 'click', 
 
\t \t \t \t \t \t \t 'ac_type' => 'toggle', 
 
\t \t \t \t \t \t \t 'open_cat' => 0, 
 
\t \t \t \t \t \t \t 'ac_opencat' => 1, 
 
\t \t \t \t \t \t \t 'ac_icon' =>'plus', 
 
\t \t \t \t \t \t \t 'disable_parent' => 0, 
 
\t \t \t \t \t \t \t 'disable_aclink' => 0, 
 
\t \t \t \t \t \t \t 'ac_theme' => '', 
 
\t \t \t \t \t \t \t 'sh_id'=> ''); 
 
\t \t \t \t $settings = shortcode_atts($defaults, $atts); 
 
\t \t \t \t return $this->woocommerce_category_accordion_func($settings, false); 
 
\t \t } 
 
} 
 
new trwca_wc_category_accordion(); 
 
    
 
\t function trwca_clean($var) { 
 
\t \t return sanitize_text_field($var); 
 
\t } 
 
} 
 
?>

谢谢你们!

+0

我的答案是否解决了问题Affi? – admcfajn

回答

0

你会想要在短代码函数中使用ob_start()并返回ob_get_clean()。 Likeso:

<?php 
function my_shortcode(){ // The function that is our shortode output. 
ob_start();?> 

<div class="my-shortcode-output"> 
    <em>I'm <strong>the best</strong></em> 
</div> 

<?php 
return $ob_get_clean(); 
} 
add_shortcode('my-shortcode', 'my_shortcode'); 

我还没有考虑从PHP呼应的js,使用wp_enque_script和wp_localize_script是好得多。

+0

问题出在我购买的插件中。我不知道输出缓冲在脚本中开始或结束的位置。 –

+0

你能告诉我哪里把这个在我的代码?这个问题花费了我更多的时间。对于知道某人的人应该是一个简单的解决方法。 –

0
function your_shortcode_function(){ 
ob_start(); ?> 
<div> 
// all your stuffs here 
</div> 
<?php 
$contents=ob_get_contents(); 
ob_end_clean(); 
return $contents; 
} 
add_shortcode('your_shortcode', 'your_shortcode_function'); 

你需要ob_start和ob_end_clean()的说,上述OUTPUT BUFFERING

+0

问题出在我买的一个插件中。我不知道输出缓冲在脚本中开始或结束的位置。 –

0

这应该工作。

<?php 
/** 
* Woocommerce Category Accordion Functions 
* 
* @author  TechieResource 
* @category Shortcode 
* @package  woocommerce-category-accordion/inc 
* @version  1.2.1 
*/ 
/** 

/* Clean variables 
* 
* @param string $var 
* @return string 
*/ 
if (!defined('ABSPATH')) { 
    exit; 
} // Exit if accessed directly 

if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) { 

    class trwca_wc_category_accordion 
    { 

     /** 
     * Constructor 
     */ 
     private $shortcode_tag = "WC-Category-Accordion"; 

     public function __construct() 
     { 

      add_action('woocommerce_category_accordion', array(
       $this, 
       'woocommerce_category_accordion_func' 
      ), 10, 2); 
      add_shortcode($this->shortcode_tag, array(
       $this, 
       'wc_category_accordion_sc' 
      )); 

      if (is_admin()) { 
       add_action('admin_head', array(
        $this, 
        'admin_head' 
       )); 
       add_action('admin_enqueue_scripts', array(
        $this, 
        'admin_enqueue_scripts' 
       )); 
      } 
     } 


     /** 
     * Display the Woocommerce Category Accordion. 
     * @since 1.2.1 
     * @param array $instance Arguments. 
     * @param bool $echo Whether to display or return the output. 
     * @return string 
     */ 
     public function woocommerce_category_accordion_func($instance, $echo = true) 
     { 
      ob_start(); 

      extract($instance, EXTR_SKIP); 

      global $wp_query; 

      global $post, $product; 

      $exclude_tree = esc_attr($exclude_tree); 

      $hide_empty = esc_attr($hide_empty); 

      $show_count = esc_attr($show_count); 

      $open_cat = esc_attr($open_cat); 

      $ac_speed = esc_attr($ac_speed); 

      $ac_type = esc_attr($ac_type); 

      $event_type = esc_attr($event_type); 

      $ac_icon = esc_attr($ac_icon); 

      $sortby = esc_attr($sortby); 

      $ac_theme = esc_attr($ac_theme); 

      $order = esc_attr($order); 

      $level = esc_attr($level); 

      $cats_id = esc_attr($ac_opencat); 

      $disable_parent = esc_attr($disable_parent); 

      $disable_aclink = esc_attr($disable_aclink); 

      if (!empty($instance['id'])) { 

       $widgetid = $instance['id']; 
      } else { 
       if ($sh_id != "") { 
        $widgetid = "wc_category_accordion-" . $sh_id; 
       } else { 
        $widgetid = "wc_category_accordion-" . $this->trwca_generate_random_code(3); 
       } 

      } 

      $instance_categories = get_terms('product_cat', '&parent=0&exclude=' . $exclude_tree . ''); 

      if (is_array($instance_categories)) { 

       foreach ($instance_categories as $categories) { 

        $term_id[] = $categories->term_id; 

        $term_name = $categories->name; 
       } 
      } 

      if (!empty($post->ID)) { 

       $terms = get_the_terms($post->ID, 'product_cat'); 
      } else { 

       $terms = ""; 
      } 

      if (is_array($terms)) { 

       foreach ($terms as $term) { 

        $_cat = $term->term_id; 

        break; 

       } 
      } 

      /* For current category highlight */ 
      if (is_product()) { 

       $current_cat = array(); 

       $cat = $wp_query->get_queried_object(); 

       if (!empty($cat->term_id)) { 
        $current_cat = $cat->term_id; 

       } else { 
        $_cat_id = "1"; 

        if (isset($term->term_id)) { 
         $_cat = $term->term_id; 

         $_cat_id = !empty($_cat) ? $_cat_id = $_cat : $_cat_id = 1; 

        } 
        if (is_shop()) { 
         $_cat_id = "1"; 
        } 
        if (!is_shop()) { 

         if (is_array($terms)) { 

          foreach ($terms as $term) { 
           $myterms[] = $term->term_id; 
          } 
          $cats_id = end($myterms); 
          ?> 
           <script type="text/javascript"> 

           var cats_id= <?php 
          return end($myterms); 
          ?>; 

           </script> 
           <style type="text/css"> 
          <?php 
          foreach ((get_the_terms($post->ID, 'product_cat')) as $term) { 
           $myterms = $term->term_id; 
           return 'ul.' . $widgetid . ' li.cat-item-' . $myterms . ' > a{font-weight:bold;}'; 
          } 
         } 
        } 
          ?> 
           </style> 
          <?php 
       } 
      } 
      $cat = $wp_query->get_queried_object(); 

      if (!empty($cat->term_id) && !is_product()) { 

       $cats_id = $cat->term_id; 
       return '<script type="text/javascript"> 

      var cats_id= ' . $cats_id . '; 

      </script>'; 
      } else if (!is_product_category() && !is_product()) { 

       $cats_id = $ac_opencat; 
      } 
      $ac_type = $ac_type == "toggle" ? $ac_type = "true" : $ac_type = "false"; 

      $open_cat = $open_cat == true || $open_cat == 'on' ? $open_cat = true : $open_cat = false; 



      /* Icon Selection */ 

      switch ($ac_icon) { 
       case 'angle': 
        $open_icon = "angle-down"; 
        $close_icon = "angle-right"; 
        break; 

       case 'doubleangle': 
        $open_icon = "angle-double-down"; 
        $close_icon = "angle-double-right"; 
        break; 

       case 'arrow-circle1': 
        $open_icon = "arrow-circle-down"; 
        $close_icon = "arrow-circle-right"; 
        break; 

       case 'arrow-circle2': 
        $open_icon = "arrow-circle-o-down"; 
        $close_icon = "arrow-circle-o-right"; 
        break; 

       case 'arrow-right': 
        $open_icon = "arrow-down"; 
        $close_icon = "arrow-right"; 
        break; 

       case 'caret': 
        $open_icon = "caret-down"; 
        $close_icon = "caret-right"; 
        break; 

       case 'caret-square': 
        $open_icon = "caret-square-o-down"; 
        $close_icon = "caret-square-o-right"; 
        break; 

       case 'chevron': 
        $open_icon = "chevron-down"; 
        $close_icon = "chevron-right"; 
        break; 

       case 'chevron-circle': 
        $open_icon = "chevron-circle-down"; 
        $close_icon = "chevron-circle-right"; 
        break; 

       case 'hand': 
        $open_icon = "hand-o-down"; 
        $close_icon = "hand-o-right"; 
        break; 

       case 'plus': 
        $open_icon = "minus"; 
        $close_icon = "plus"; 
        break; 

       case 'plus-circle': 
        $open_icon = "minus-circle"; 
        $close_icon = "plus-circle"; 
        break; 

       case 'plus-square1': 
        $open_icon = "minus-square"; 
        $close_icon = "plus-square"; 
        break; 

       case 'plus-square2': 
        $open_icon = "minus-square-o"; 
        $close_icon = "plus-square-o"; 
        break; 
      } 


      if ($disable_aclink == true) { 

       $disable_aclink = 'true'; 
      } else if ($disable_aclink == "") { 

       $disable_aclink = 'false'; 
      } 
      if ($disable_parent == true) { 

       $disable_parent = 'true'; 
      } else if ($disable_parent == "") { 

       $disable_parent = 'false'; 

      } 
      $cats_id = empty($cats_id) ? 1 : $cats_id; 
      ?> 
      <script type="text/javascript"> 
      var $=jQuery.noConflict(); 
      $(document).ready(function($){ 
      $('.<?php 
         echo $widgetid; 
      ?>').trwcAccordion({ 
         classParent : 'trwca-parent', 
         classActive : 'active', 
         classArrow : 'trwca-icon', 
         classCount : 'trwca-count', 
         classExpand : 'trwca-current-parent', 
         eventType : '<?php 
         echo $event_type; 
      ?>', 
         hoverDelay : 100, 
         menuClose  : true, 
         cats_id: <?php 
         echo $cats_id; 
      ?>, 
         ac_type : <?php 
         echo $ac_type; 
      ?>, 
         autoExpand : true, 
         speed  : '<?php 
         echo $ac_speed; 
      ?>', 
         saveState : '<?php 
         echo $open_cat; 
      ?>', 
         disableLink : <?php 
         echo $disable_aclink; 
      ?>, 
         disableparentLink : <?php 
         echo $disable_parent; 
      ?>, 
         auto_open: 1, 
         showCount : true, 
         widget_id : "<?php 
         echo $widgetid; 
      ?>", 
         openIcon : '<?php 
         echo $open_icon; 
      ?>', 
         closeIcon : '<?php 
         echo $close_icon; 
      ?>', 
      }); 
      }); 
      </script> 
      <div class="block-content trwca-actheme <?php 
         echo $ac_theme; 
      ?>"> 
      <div class="trwca-loader"></div> 
      <ul class="<?php 
         echo $widgetid; 
      ?> accordion" id="outer_ul"> 
      <?php 

      $subcat_args = array(

       'taxonomy' => 'product_cat', 
       'title_li' => '', 
       'orderby' => $sortby, 
       'order' => $order, 
       'depth' => $level, 
       'show_count' => $show_count, 
       'hide_empty' => $hide_empty, 
       'use_desc_for_title' => 1, 
       'echo' => false, 
       'exclude' => $exclude_tree, 
       'hierarchical' => true, 
       'show_option_none' => __('No Categories Found', 'trwca'), 
       'link_after' => '', 
       'walker' => new trwca_walker 
      ); 


      $subcategories = wp_list_categories($subcat_args); 

      $subcategories = preg_replace_callback('/<\/a> \(([0-9]+)\)/', function($matches) 
      { 
       return '&nbsp;<span class="count">(' . ($matches[1]) . ')</span></a>'; 
      }, $subcategories); 

      if ($subcategories) { 

       echo $subcategories; 

      } 
      ?> 
      </ul> 
      </div> 
      <?php 
      $contents=ob_get_contents(); 
      ob_end_clean(); 
      return $contents;     
     } 

     public function trwca_generate_random_code($length = 3) 
     { 

      $string = ''; 

      $characters = "123456789"; 

      for ($p = 0; $p < $length; $p++) { 
       $string .= $characters[mt_rand(0, strlen($characters) - 1)]; 
      } 

      return $string; 

     } 
     /** 
     * admin_head 
     * calls your functions into the correct filters 
     * @return void 
     */ 
     function admin_head() 
     { 
      // check user permissions 
      if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) { 
       return; 
      } 

      // check if WYSIWYG is enabled 
      if ('true' == get_user_option('rich_editing')) { 
       add_filter('mce_external_plugins', array(
        $this, 
        'mce_external_plugins' 
       )); 
       add_filter('mce_buttons', array(
        $this, 
        'mce_buttons' 
       )); 
      } 
     } 

     /** 
     * mce_external_plugins 
     * Adds our tinymce plugin 
     * @param array $plugin_array 
     * @return array 
     */ 
     function mce_external_plugins($plugin_array) 
     { 

      $plugin_array['WC_Category_Accordion'] = plugins_url('admin/js/mce-button.js', __FILE__); 
      return $plugin_array; 
     } 

     /** 
     * mce_buttons 
     * Adds our tinymce button 
     * @param array $buttons 
     * @return array 
     */ 
     function mce_buttons($buttons) 
     { 
      array_push($buttons, 'WC_Category_Accordion'); 
      return $buttons; 
     } 

     /** 
     * admin_enqueue_scripts 
     * Used to enqueue custom styles 
     * @return void 
     */ 
     function admin_enqueue_scripts() 
     { 
      wp_enqueue_style('WC-Category-Accordion-sh', plugins_url('admin/css/mce-button.css', __FILE__)); 
     } 
     public function wc_category_accordion_sc($atts, $content = null) 
     { 

      $defaults = array(
       'show_count' => 0, 
       'ac_speed' => 'fast', 
       'exclude_tree' => '', 
       'hide_empty' => 0, 
       'sortby' => 'name', 
       'order' => 'ASC', 
       'level' => 0, 
       'event_type' => 'click', 
       'ac_type' => 'toggle', 
       'open_cat' => 0, 
       'ac_opencat' => 1, 
       'ac_icon' => 'plus', 
       'disable_parent' => 0, 
       'disable_aclink' => 0, 
       'ac_theme' => '', 
       'sh_id' => '' 
      ); 
      $settings = shortcode_atts($defaults, $atts); 
      return $this->woocommerce_category_accordion_func($settings, false); 
     } 
    } 
    new trwca_wc_category_accordion(); 

    function trwca_clean($var) 
    { 
     return sanitize_text_field($var); 
    } 
} 
?>