<?php
/*
    woocommerce shortcode extensions
*/
if( ! defined( 'ABSPATH' ) ) exit;

new woo_shortcode();
class woo_shortcode {
    
    /* 
        Constructor 
    */
    function __construct() {
        // custom shortcodes
        add_shortcode( 'woo_content', array($this, 'woo_content_sc') );
        // add custom button support to shortcodes
        add_filter( 'do_shortcode_tag', array($this, 'edit_sc'), 10,3);
    }

    /*
        Shortcode
    */
    public function woo_content_sc($atts=[], $content='') {
        return "WOO_CONTENT";
    }
    
    public function edit_sc($output, $tag, $attr){
        if ($tag == 'add_to_cart' ||
            $tag == 'product' ||
            $tag == 'product_page') { //make sure it is the right shortcode
            if (isset($attr['id']) &&
                isset($attr['btn_style'])) {
                // $output = '<pre> make custom button styling </pre>'.$output;
                // debug($attr);
                // debug(__( 'Add to cart', 'core' ));
                // debug(__( 'Read more', 'core' ));
                
                // get button
                $btn_style = isset($attr['btn_style']) ? esc_attr($attr['btn_style']) : '_button';
                $btn_style = str_replace("_", "", $btn_style);
                if ($btn_style == 'custom') {
                    $btn_style = \vc\helper::make_custom_element($attr, 'btn', "");
                    $btn_style = str_replace("\"", "[xxx]", $btn_style);
                    $btn_style = str_replace("'", "\"", $btn_style);
                    $btn_style = str_replace("[xxx]", "'", $btn_style);
                
                    if ($tag == 'product_page')
                        $output = str_replace("button alt", "custom alt\" style=\"".$btn_style, $output);
                    else
                        $output = str_replace("href", "style=\"".$btn_style."\" href", $output);
                }
                else {

                    if ($tag == 'product_page')
                        $output = str_replace("button alt", $btn_style." alt", $output);
                    else
                        $output = str_replace("class=\"button", "class=\"".$btn_style, $output);
                }
                // make link content & icon
                $btn_text = __( 'Add to cart', 'core' );
                if (strpos($output, $btn_text) === false) 
                    $btn_text = __( 'Read more', 'core' );
                $btn_content = \vc\helper::make_custom_icon($attr, 'btn_', $btn_text);
                $output = str_replace($btn_text, $btn_content, $output);
            }
        }
        return $output;
      }
}