| 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 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* [button_shortcode] Compare Button Shortcode callable function |
| 35 |
* @param [type] $atts |
| 36 |
* @param string $content |
| 37 |
* @return [HTML] |
| 38 |
*/ |
| 39 |
public function button_shortcode( $atts, $content = '' ){ |
| 40 |
|
| 41 |
wp_enqueue_style( 'evercompare-frontend' ); |
| 42 |
wp_enqueue_script( 'evercompare-frontend' ); |
| 43 |
|
| 44 |
global $product; |
| 45 |
|
| 46 |
// Fetch option data |
| 47 |
$button_text = ever_compare_get_option( 'button_text','ever_compare_settings_tabs', 'Compare' ); |
| 48 |
$button_added_text = ever_compare_get_option( 'added_button_text','ever_compare_settings_tabs', 'Added' ); |
| 49 |
|
| 50 |
$shop_page_btn_position = ever_compare_get_option( 'shop_btn_position', 'ever_compare_settings_tabs', 'after_cart_btn' ); |
| 51 |
$product_page_btn_position = ever_compare_get_option( 'product_btn_position', 'ever_compare_settings_tabs', 'after_cart_btn' ); |
| 52 |
$button_style = ever_compare_get_option( 'button_style', 'ever_compare_style_tabs', 'theme' ); |
| 53 |
|
| 54 |
$button_class = array( |
| 55 |
'htcompare-btn', |
| 56 |
'htcompare-btn-style-'.$button_style, |
| 57 |
'htcompare-shop-'.$shop_page_btn_position, |
| 58 |
'htcompare-product-'.$product_page_btn_position, |
| 59 |
); |
| 60 |
|
| 61 |
if( $button_style === 'theme' ){ |
| 62 |
$button_class[] = 'button'; |
| 63 |
} |
| 64 |
|
| 65 |
$button_icon = $this->get_icon(); |
| 66 |
$added_button_icon = $this->get_icon('added_'); |
| 67 |
|
| 68 |
if( !empty( $button_text ) ){ |
| 69 |
$button_text = '<span class="evercompare-btn-text">'.$button_text.'</span>'; |
| 70 |
} |
| 71 |
|
| 72 |
if( !empty( $button_added_text ) ){ |
| 73 |
$button_added_text = '<span class="evercompare-btn-text">'.$button_added_text.'</span>'; |
| 74 |
} |
| 75 |
|
| 76 |
// Shortcode atts |
| 77 |
$default_atts = array( |
| 78 |
'product_id' => $product->get_id(), |
| 79 |
'button_url' => Manage_Compare::instance()->get_compare_page_url(), |
| 80 |
'button_class' => implode(' ', $button_class ), |
| 81 |
'button_text' => $button_icon.$button_text, |
| 82 |
'button_added_text' => $added_button_icon.$button_added_text, |
| 83 |
'template_name' => 'add', |
| 84 |
); |
| 85 |
|
| 86 |
$atts = shortcode_atts( $default_atts, $atts, $content ); |
| 87 |
return Manage_Compare::instance()->button_html( $atts ); |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* [table_shortcode] Compare table shortcode callable function |
| 93 |
* @param [type] $atts |
| 94 |
* @param string $content |
| 95 |
* @return [HTML] |
| 96 |
*/ |
| 97 |
public function table_shortcode( $atts, $content = '' ){ |
| 98 |
|
| 99 |
wp_enqueue_style( 'evercompare-frontend' ); |
| 100 |
wp_enqueue_script( 'evercompare-frontend' ); |
| 101 |
|
| 102 |
/* Fetch From option data */ |
| 103 |
$empty_compare_text = ever_compare_get_option('empty_table_text','ever_compare_table_settings_tabs'); |
| 104 |
$return_shop_button = ever_compare_get_option('shop_button_text','ever_compare_table_settings_tabs','Return to shop'); |
| 105 |
|
| 106 |
/* Product and Field */ |
| 107 |
$products = Manage_Compare::instance()->get_compared_products_data(); |
| 108 |
$fields = Manage_Compare::instance()->get_compare_fields(); |
| 109 |
|
| 110 |
$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(); |
| 111 |
|
| 112 |
$default_atts = array( |
| 113 |
'evercompare' => Manage_Compare::instance(), |
| 114 |
'products' => $products, |
| 115 |
'fields' => $fields, |
| 116 |
'return_shop_button'=> $return_shop_button, |
| 117 |
'heading_txt' => $custom_heading, |
| 118 |
'empty_compare_text'=> $empty_compare_text, |
| 119 |
); |
| 120 |
|
| 121 |
$atts = shortcode_atts( $default_atts, $atts, $content ); |
| 122 |
return Manage_Compare::instance()->table_html( $atts ); |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* [get_icon] |
| 128 |
* @param string $type |
| 129 |
* @return [HTML] |
| 130 |
*/ |
| 131 |
public function get_icon( $type = '' ){ |
| 132 |
|
| 133 |
$default_icon = ever_compare_icon_list('default'); |
| 134 |
$default_loader = '<span class="ever-compare-loader">'.ever_compare_icon_list('loading').'</span>'; |
| 135 |
|
| 136 |
$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' ); |
| 137 |
|
| 138 |
$button_icon_type = ever_compare_get_option( $type.'button_icon_type', 'ever_compare_settings_tabs', 'default' ); |
| 139 |
|
| 140 |
if( $button_icon_type === 'custom' ){ |
| 141 |
$button_icon = ever_compare_get_option( $type.'button_custom_icon','ever_compare_settings_tabs', '' ); |
| 142 |
}elseif( $button_icon_type === 'default' ){ |
| 143 |
return $default_icon; |
| 144 |
}else{ |
| 145 |
$button_icon = ''; |
| 146 |
} |
| 147 |
|
| 148 |
if( !empty( $button_icon ) ){ |
| 149 |
$button_icon = '<span class="ever-compare-btn-image"><img src="'.esc_url( $button_icon ).'" alt="'.esc_attr( $button_text ).'"></span>'; |
| 150 |
} |
| 151 |
|
| 152 |
return $default_loader.$button_icon; |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
} |
| 158 |
|