assets
5 years ago
admin-product.php
5 years ago
attribute-hooks.php
5 years ago
frontend.php
5 years ago
helper.php
5 years ago
swatches.php
5 years ago
attribute-hooks.php
187 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Modules\Swatches; |
| 4 | |
| 5 | use ShopEngine\Traits\Singleton; |
| 6 | |
| 7 | defined('ABSPATH') || exit; |
| 8 | |
| 9 | class Attribute_Hooks |
| 10 | { |
| 11 | |
| 12 | use Singleton; |
| 13 | |
| 14 | public function init() { |
| 15 | |
| 16 | $attribute_taxonomies = wc_get_attribute_taxonomies(); |
| 17 | |
| 18 | if(empty($attribute_taxonomies)) { |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | foreach($attribute_taxonomies as $tax) { |
| 23 | |
| 24 | add_action('pa_' . $tax->attribute_name . '_add_form_fields', [$this, 'add_attr_field']); |
| 25 | add_action('pa_' . $tax->attribute_name . '_edit_form_fields', [$this, 'edit_attr_field'], 10, 2); |
| 26 | |
| 27 | add_filter('manage_edit-pa_' . $tax->attribute_name . '_columns', [$this, 'add_attr_column']); |
| 28 | add_filter('manage_pa_' . $tax->attribute_name . '_custom_column', [$this, 'add_attr_column_content'], 10, 3); |
| 29 | } |
| 30 | |
| 31 | add_action('created_term', [$this, 'persist_term_meta'], 10, 2); |
| 32 | add_action('edit_term', [$this, 'persist_term_meta'], 10, 2); |
| 33 | |
| 34 | |
| 35 | add_action('shopengine_attribute_field_chain', [$this, 'attribute_fields'], 10, 3); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | public function add_attr_field($taxonomy) { |
| 40 | |
| 41 | $attr = Helper::get_tax_attribute($taxonomy); |
| 42 | |
| 43 | do_action('shopengine_attribute_field_chain', $attr->attribute_type, '', 'add'); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | public function edit_attr_field($term, $taxonomy) { |
| 48 | |
| 49 | $attr = Helper::get_tax_attribute($taxonomy); |
| 50 | |
| 51 | $value = get_term_meta($term->term_id, $attr->attribute_type, true); |
| 52 | |
| 53 | do_action('shopengine_attribute_field_chain', $attr->attribute_type, $value, 'edit'); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * Add another td in the table |
| 59 | * |
| 60 | * @param $columns |
| 61 | * @return mixed |
| 62 | */ |
| 63 | public function add_attr_column($columns) { |
| 64 | $new_columns = []; |
| 65 | $new_columns['cb'] = $columns['cb']; |
| 66 | $new_columns['pa_preview'] = 'Thumbnail'; |
| 67 | unset($columns['cb']); |
| 68 | |
| 69 | return $new_columns + $columns; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | public function add_attr_column_content($columns, $column, $term_id) { |
| 74 | |
| 75 | if('pa_preview' !== $column) { |
| 76 | return $columns; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | $attr = Helper::get_tax_attribute(sanitize_key($_REQUEST['taxonomy'])); |
| 81 | |
| 82 | $value = get_term_meta($term_id, $attr->attribute_type, true); |
| 83 | |
| 84 | switch($attr->attribute_type) { |
| 85 | case Swatches::PA_COLOR: |
| 86 | printf('<div class="swatches_thumb swatch_color" style="background-color:%s;"></div>', esc_attr($value)); |
| 87 | break; |
| 88 | |
| 89 | case Swatches::PA_IMAGE: |
| 90 | $image = $value ? wp_get_attachment_image_src($value) : ''; |
| 91 | $image = $image ? $image[0] : Helper::get_dummy(); |
| 92 | printf('<img class="swatches_thumb swatch_image" src="%s" width="44px" height="44px">', esc_url($image)); |
| 93 | break; |
| 94 | |
| 95 | case Swatches::PA_LABEL: |
| 96 | printf('<div class="swatches_thumb swatch_label">%s</div>', esc_html($value)); |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | return $columns; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | public function persist_term_meta($term_id, $tt_id) { |
| 105 | |
| 106 | $types = Swatches::instance()->get_available_types(); |
| 107 | |
| 108 | foreach($types as $type => $label) { |
| 109 | if(isset($_POST[$type])) { |
| 110 | |
| 111 | if($type == Swatches::PA_COLOR) { |
| 112 | |
| 113 | update_term_meta($term_id, $type, sanitize_hex_color($_POST[$type])); |
| 114 | |
| 115 | }else { |
| 116 | |
| 117 | update_term_meta($term_id, $type, sanitize_text_field($_POST[$type])); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | public function attribute_fields($type, $value, $form) { |
| 125 | |
| 126 | if(in_array($type, ['select', 'text'])) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | $types = Swatches::instance()->get_available_types(); |
| 131 | |
| 132 | printf( |
| 133 | '<%s class="form-field">%s<label for="term-%s">%s</label>%s', |
| 134 | 'edit' == $form ? 'tr' : 'div', |
| 135 | 'edit' == $form ? '<th>' : '', |
| 136 | esc_attr($type), |
| 137 | $types[$type], |
| 138 | 'edit' == $form ? '</th><td>' : '' |
| 139 | ); |
| 140 | |
| 141 | switch($type) { |
| 142 | case Swatches::PA_IMAGE: |
| 143 | $image = $value ? wp_get_attachment_image_src($value) : ''; |
| 144 | $image = $image ? $image[0] : Helper::get_dummy(); |
| 145 | ?> |
| 146 | |
| 147 | <div class="shopengine_term_img_thumbnail" style="float:left;margin-right:10px;"> |
| 148 | <img alt="" src="<?php echo esc_url($image) ?>" width="70px" height="70px"/> |
| 149 | </div> |
| 150 | |
| 151 | <div style="line-height:60px;"> |
| 152 | <input type="hidden" class="shopengine_term_img" name="image" |
| 153 | value="<?php echo esc_attr($value) ?>"/> |
| 154 | |
| 155 | <button type="button" class="shopengine_upload_img_button button"> |
| 156 | <?php esc_html_e('Upload image', 'shopengine'); ?> |
| 157 | </button> |
| 158 | |
| 159 | <button type="button" class="shopengine_remove_img_btn button <?php echo esc_attr( $value ) ? '' : 'hidden' ?>"> |
| 160 | <?php esc_html_e('Remove image', 'shopengine'); ?> |
| 161 | </button> |
| 162 | </div> <?php |
| 163 | |
| 164 | break; |
| 165 | |
| 166 | case Swatches::PA_COLOR : ?> |
| 167 | |
| 168 | <input type="color" |
| 169 | id="term-<?php echo esc_attr($type) ?>" |
| 170 | name="<?php echo esc_attr($type) ?>" |
| 171 | value="<?php echo esc_attr($value) ?>"/> <?php |
| 172 | break; |
| 173 | |
| 174 | default: ?> |
| 175 | |
| 176 | <input type="text" |
| 177 | id="term-<?php echo esc_attr($type) ?>" |
| 178 | name="<?php echo esc_attr($type) ?>" |
| 179 | value="<?php echo esc_attr($value) ?>"/> <?php |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | echo 'edit' == $form ? '</td></tr>' : '</div>'; |
| 185 | } |
| 186 | } |
| 187 |