| 1 |
<?php |
| 2 |
namespace EverCompare\Frontend; |
| 3 |
/** |
| 4 |
* Shortcode handler class |
| 5 |
*/ |
| 6 |
class Shortcode { |
| 7 |
|
| 8 |
/** |
| 9 |
* [$_instance] |
| 10 |
* @var null |
| 11 |
*/ |
| 12 |
private static $_instance = null; |
| 13 |
|
| 14 |
/** |
| 15 |
* [instance] Initializes a singleton instance |
| 16 |
* @return [Base] |
| 17 |
*/ |
| 18 |
public static function instance() { |
| 19 |
if ( is_null( self::$_instance ) ) { |
| 20 |
self::$_instance = new self(); |
| 21 |
} |
| 22 |
return self::$_instance; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Initializes the class |
| 27 |
*/ |
| 28 |
function __construct() { |
| 29 |
add_shortcode( 'evercompare_button', [ $this, 'button_shortcode' ] ); |
| 30 |
add_shortcode( 'evercompare_table', [ $this, 'table_shortcode' ] ); |
| 31 |
add_shortcode( 'evercompare_counter', [ $this, 'counter_shortcode' ] ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* [button_shortcode] Compare Button Shortcode callable function |
| 36 |
* @param [type] $atts |
| 37 |
* @param string $content |
| 38 |
* @return [HTML] |
| 39 |
*/ |
| 40 |
public function button_shortcode( $atts, $content = '' ){ |
| 41 |
|
| 42 |
wp_enqueue_style( 'evercompare-frontend' ); |
| 43 |
wp_enqueue_script( 'evercompare-frontend' ); |
| 44 |
|
| 45 |
global $product; |
| 46 |
|
| 47 |
// Fetch option data |
| 48 |
$button_text = ever_compare_get_option( 'button_text','ever_compare_settings_tabs', 'Compare' ); |
| 49 |
$button_added_text = ever_compare_get_option( 'added_button_text','ever_compare_settings_tabs', 'Added' ); |
| 50 |
|
| 51 |
$shop_page_btn_position = ever_compare_get_option( 'shop_btn_position', 'ever_compare_settings_tabs', 'after_cart_btn' ); |
| 52 |
$product_page_btn_position = ever_compare_get_option( 'product_btn_position', 'ever_compare_settings_tabs', 'after_cart_btn' ); |
| 53 |
$button_style = ever_compare_get_option( 'button_style', 'ever_compare_style_tabs', 'theme' ); |
| 54 |
|
| 55 |
$button_class = array( |
| 56 |
'htcompare-btn', |
| 57 |
'htcompare-btn-style-'.$button_style, |
| 58 |
'htcompare-shop-'.$shop_page_btn_position, |
| 59 |
'htcompare-product-'.$product_page_btn_position, |
| 60 |
); |
| 61 |
|
| 62 |
if( $button_style === 'theme' ){ |
| 63 |
$button_class[] = 'button'; |
| 64 |
} |
| 65 |
|
| 66 |
$button_icon = $this->get_icon(); |
| 67 |
$added_button_icon = $this->get_icon('added_'); |
| 68 |
|
| 69 |
if( !empty( $button_text ) ){ |
| 70 |
$button_text = '<span class="evercompare-btn-text">'.$button_text.'</span>'; |
| 71 |
} |
| 72 |
|
| 73 |
if( !empty( $button_added_text ) ){ |
| 74 |
$button_added_text = '<span class="evercompare-btn-text">'.$button_added_text.'</span>'; |
| 75 |
} |
| 76 |
|
| 77 |
if(\EverCompare\Frontend\Manage_Compare::instance()->is_product_in_compare( $product->get_id() )) { |
| 78 |
$button_class[] = 'added'; |
| 79 |
} |
| 80 |
|
| 81 |
// Shortcode atts |
| 82 |
$default_atts = array( |
| 83 |
'product_id' => $product->get_id(), |
| 84 |
'button_url' => Manage_Compare::instance()->get_compare_page_url(), |
| 85 |
'button_class' => implode(' ', $button_class ), |
| 86 |
'button_text' => $button_icon.$button_text, |
| 87 |
'button_added_text' => $added_button_icon.$button_added_text, |
| 88 |
'template_name' => 'add', |
| 89 |
); |
| 90 |
|
| 91 |
$atts = shortcode_atts( $default_atts, $atts, $content ); |
| 92 |
return Manage_Compare::instance()->button_html( $atts ); |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* [table_shortcode] Compare table shortcode callable function |
| 98 |
* @param [type] $atts |
| 99 |
* @param string $content |
| 100 |
* @return [HTML] |
| 101 |
*/ |
| 102 |
public function table_shortcode( $atts, $content = '' ){ |
| 103 |
|
| 104 |
wp_enqueue_style( 'evercompare-frontend' ); |
| 105 |
wp_enqueue_script( 'evercompare-frontend' ); |
| 106 |
|
| 107 |
/* Fetch From option data */ |
| 108 |
$empty_compare_text = ever_compare_get_option('empty_table_text','ever_compare_table_settings_tabs'); |
| 109 |
$return_shop_button = ever_compare_get_option('shop_button_text','ever_compare_table_settings_tabs','Return to shop'); |
| 110 |
|
| 111 |
/* Product and Field */ |
| 112 |
$products = Manage_Compare::instance()->get_compared_products_data(); |
| 113 |
$fields = Manage_Compare::instance()->get_compare_fields(); |
| 114 |
|
| 115 |
$custom_heading = !empty( ever_compare_get_option( 'table_heading', 'ever_compare_table_settings_tabs' ) ) ? ever_compare_get_option( 'table_heading', 'ever_compare_table_settings_tabs' ) : array(); |
| 116 |
|
| 117 |
$default_atts = array( |
| 118 |
'evercompare' => Manage_Compare::instance(), |
| 119 |
'products' => $products, |
| 120 |
'fields' => $fields, |
| 121 |
'return_shop_button'=> $return_shop_button, |
| 122 |
'heading_txt' => $custom_heading, |
| 123 |
'empty_compare_text'=> $empty_compare_text, |
| 124 |
); |
| 125 |
|
| 126 |
$atts = shortcode_atts( $default_atts, $atts, $content ); |
| 127 |
return Manage_Compare::instance()->table_html( $atts ); |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Evercompare Counter Shortcode |
| 133 |
* |
| 134 |
* @param [array] $atts |
| 135 |
* @param string $content |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
public function counter_shortcode( $atts, $content = '' ){ |
| 139 |
|
| 140 |
wp_enqueue_style( 'evercompare-frontend' ); |
| 141 |
wp_enqueue_script( 'evercompare-frontend' ); |
| 142 |
|
| 143 |
$default_atts = array( |
| 144 |
'count' => count(Manage_Compare::instance()->get_compared_products()), |
| 145 |
'page_url' => Manage_Compare::instance()->get_compare_page_url(), |
| 146 |
); |
| 147 |
|
| 148 |
$atts = shortcode_atts( $default_atts, $atts, $content ); |
| 149 |
return Manage_Compare::instance()->count_html( $atts ); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* [get_icon] |
| 155 |
* @param string $type |
| 156 |
* @return [HTML] |
| 157 |
*/ |
| 158 |
public function get_icon( $type = '' ){ |
| 159 |
|
| 160 |
$default_icon = ever_compare_icon_list('default'); |
| 161 |
$default_loader = '<span class="ever-compare-loader">'.ever_compare_icon_list('loading').'</span>'; |
| 162 |
|
| 163 |
$button_text = ( $type === 'added' ) ? ever_compare_get_option( 'added_button_text','ever_compare_settings_tabs', 'Added' ) : ever_compare_get_option( 'button_text','ever_compare_settings_tabs', 'Compare' ); |
| 164 |
|
| 165 |
$button_icon_type = ever_compare_get_option( $type.'button_icon_type', 'ever_compare_settings_tabs', 'default' ); |
| 166 |
|
| 167 |
if( $button_icon_type === 'custom' ){ |
| 168 |
$button_icon = ever_compare_get_option( $type.'button_custom_icon','ever_compare_settings_tabs', '' ); |
| 169 |
}elseif( $button_icon_type === 'default' ){ |
| 170 |
return $default_icon; |
| 171 |
}else{ |
| 172 |
$button_icon = ''; |
| 173 |
} |
| 174 |
|
| 175 |
if( !empty( $button_icon ) ){ |
| 176 |
$button_icon = '<span class="ever-compare-btn-image"><img src="'.esc_url( $button_icon ).'" alt="'.esc_attr( $button_text ).'"></span>'; |
| 177 |
} |
| 178 |
|
| 179 |
return $default_loader.$button_icon; |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
} |
| 185 |
|