assets
2 years ago
comparison-cookie.php
2 years ago
comparison-field-value.php
1 year ago
comparison-helper.php
3 years ago
comparison-share.php
3 years ago
comparison.php
3 years ago
group-meta.php
3 years ago
route.php
3 years ago
comparison-helper.php
178 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace ShopEngine\Modules\Comparison; |
| 5 | |
| 6 | |
| 7 | use ShopEngine\Core\Register\Module_List; |
| 8 | use ShopEngine\Utils\Helper; |
| 9 | |
| 10 | class Comparison_Helper { |
| 11 | |
| 12 | public static function get_html($content = [], $comparison_page = false) { |
| 13 | |
| 14 | if(empty($content)) { |
| 15 | return '<h1 class="shopengine-no-comparison-product">'.esc_html__('No product is added for comparison, please add some product to compare', 'shopengine').'</h1>'; |
| 16 | } |
| 17 | |
| 18 | $settings = Module_List::instance()->get_active_settings( 'comparison' ); |
| 19 | $attributes_fields = $settings['attribute_fields']['value'] ?? [] ; |
| 20 | |
| 21 | $default_fields = [ 'url', 'image', 'price' ]; |
| 22 | if( class_exists('ShopEngine_Pro') ) { |
| 23 | $default_fields['attributes'] = $attributes_fields; |
| 24 | } |
| 25 | |
| 26 | $fields = array_merge($default_fields, $settings['shop_field_in_table']['value'] ?? []); |
| 27 | |
| 28 | $fields['attributes'] = $attributes_fields; |
| 29 | |
| 30 | $share_buttons = $settings['share_button']['value'] ?? []; |
| 31 | |
| 32 | $field_value_manager = new Comparison_Field_Value(); |
| 33 | $field_value_manager->product_ids = $content; |
| 34 | |
| 35 | $displayable_fields = []; |
| 36 | |
| 37 | foreach ( $content as $pid ) { |
| 38 | if ( empty( $pid ) ) { |
| 39 | continue; |
| 40 | } |
| 41 | |
| 42 | $product = wc_get_product( $pid ); |
| 43 | if(!$product || $product->get_status() !== 'publish' ) { |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | if ( empty( $product ) ) { |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | foreach ( $fields as $key => $field ) { |
| 53 | |
| 54 | if(gettype($field) != 'string'){ |
| 55 | $slug = $key; |
| 56 | }else{ |
| 57 | $slug = $field ; |
| 58 | } |
| 59 | |
| 60 | $displayable_fields[ $slug ][ $pid ] = $field_value_manager->get_value( $product, |
| 61 | $slug, $field ); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | $format_attributes = []; |
| 66 | if(isset($displayable_fields['attributes'])){ |
| 67 | foreach ($displayable_fields['attributes'] as $product_id => $attribute_data){ |
| 68 | |
| 69 | foreach ($attribute_data[$product_id] as $key => $attributes){ |
| 70 | $format_attributes[$key][$product_id] = $attributes; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | |
| 76 | $displayable_fields['attributes'] = $format_attributes; |
| 77 | |
| 78 | // group first tr values |
| 79 | $first_tr = []; |
| 80 | $first_tr['first_tr']['url'] = $displayable_fields['url'] ?? []; |
| 81 | $first_tr['first_tr']['image'] = $displayable_fields['image'] ?? []; |
| 82 | $first_tr['first_tr']['title'] = $displayable_fields['title'] ?? []; |
| 83 | $first_tr['first_tr']['price'] = $displayable_fields['price'] ?? []; |
| 84 | |
| 85 | $displayable_fields = $first_tr + $displayable_fields ; |
| 86 | if( class_exists('ShopEngine_Pro') ) { |
| 87 | $displayable_fields['custom_meta'] = $settings['custom_meta_fields']['value'] ?? []; |
| 88 | } |
| 89 | |
| 90 | unset($displayable_fields['url'], $displayable_fields['image'], $displayable_fields['title'], $displayable_fields['price']); |
| 91 | |
| 92 | ?> |
| 93 | <div class="shopengine-modal-wrap"> |
| 94 | <div class="shopengine-comparison-container"> |
| 95 | <div class="shopengine-comparison"> |
| 96 | <h2><?php echo esc_html__('Product comparison', 'shopengine') ?> </h2> |
| 97 | |
| 98 | <?php if( class_exists('ShopEngine_Pro') && $share_buttons ) : ?> |
| 99 | <div class="social-share"> |
| 100 | <?php |
| 101 | $share_url = home_url( '?comparison=yes&product_ids=' . implode( ',', $content ) ); |
| 102 | $share = esc_html__("Share On Facebook","shopengine"); |
| 103 | $twitter = esc_html__("Share On Twitter","shopengine"); |
| 104 | $copy = esc_html__("Copy To Clipboard","shopengine"); |
| 105 | |
| 106 | foreach ( $share_buttons as $share_button ) { |
| 107 | $button = ''; |
| 108 | switch ( $share_button ) { |
| 109 | case 'facebook': |
| 110 | $button = sprintf( |
| 111 | '<a title="' . $share . '" href="%1$s" target="_blank" rel="noopener noreferrer nofollow "><i class="eicon-facebook"></i> <span>%2$s</span></a>', |
| 112 | 'https://www.facebook.com/sharer.php?u=' . $share_url, |
| 113 | esc_html__('Share on Facebook', 'shopengine') |
| 114 | ); |
| 115 | break; |
| 116 | case 'twitter': |
| 117 | $button = sprintf( |
| 118 | '<a title="' . $twitter . '" href="%1$s" target="_blank" rel="noopener noreferrer nofollow "><i class="eicon-twitter"></i> <span>%2$s</span></a>', |
| 119 | 'https://twitter.com/intent/tweet?url=' . $share_url, |
| 120 | esc_html__('Share on Twitter', 'shopengine') |
| 121 | ); |
| 122 | break; |
| 123 | case 'copy_url': |
| 124 | $button = sprintf( |
| 125 | |
| 126 | '<button title="' . $copy . '" class="copy-comparison-share-url" data-share-url="%1$s" data-message="%2$s"> <i class="eicon-copy"></i> <span>%3$s</span> </button>', |
| 127 | $share_url, |
| 128 | esc_html__('Copied', 'shopengine'), |
| 129 | esc_html__('Copy', 'shopengine') |
| 130 | ); |
| 131 | break; |
| 132 | } |
| 133 | echo wp_kses($button, Helper::get_kses_array()); |
| 134 | } |
| 135 | ?> |
| 136 | </div> |
| 137 | <?php endif;?> |
| 138 | |
| 139 | <div class="comparison-table-wrap"> |
| 140 | <table class="table table-bordered <?php echo $comparison_page ? 'comparison-page' : '' ?>"> |
| 141 | <tbody> <?php |
| 142 | |
| 143 | foreach ($displayable_fields as $slug => $data){ |
| 144 | $field_value_manager->get_html($slug, $data); |
| 145 | } |
| 146 | ?> |
| 147 | </tbody> |
| 148 | </table> |
| 149 | </div> |
| 150 | </div> |
| 151 | </div> |
| 152 | </div> |
| 153 | <?php |
| 154 | } |
| 155 | |
| 156 | public static function generate_meta_keys_array( $keys ) { |
| 157 | /*$cache = get_transient( 'shopengine-all-products_meta_keys_array' ); |
| 158 | if($cache){ |
| 159 | return $cache; |
| 160 | }*/ |
| 161 | |
| 162 | $keys_array = []; |
| 163 | foreach ( $keys as $key ) { |
| 164 | $title = ucwords( preg_replace( '~([^a-z0-9\-])~i', ' ', $key ) ); |
| 165 | $keys_array[ $key ] = $title; |
| 166 | } |
| 167 | |
| 168 | // set_transient( 'shopengine-all-products_meta_keys_array', $keys_array, 60 * 60 * 0.01 ); |
| 169 | |
| 170 | return $keys_array; |
| 171 | } |
| 172 | |
| 173 | public static function get_products_meta_keys() { |
| 174 | return static::generate_meta_keys_array(Helper::get_products_meta_keys()); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | } |