assets
3 years ago
loop-product-support
3 years ago
admin-product.php
3 years ago
attribute-hooks.php
3 years ago
frontend.php
3 years ago
helper.php
3 years ago
swatches.php
3 years ago
attribute-hooks.php
218 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 | use Singleton; |
| 12 | |
| 13 | public function init() { |
| 14 | |
| 15 | $attribute_taxonomies = wc_get_attribute_taxonomies(); |
| 16 | $types = Swatches::instance()->get_available_types(); |
| 17 | |
| 18 | if(empty($attribute_taxonomies)) { |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | foreach($attribute_taxonomies as $tax) { |
| 23 | |
| 24 | if(isset($types[$tax->attribute_type])) { |
| 25 | |
| 26 | if(Swatches::PA_LABEL === $tax->attribute_type) { |
| 27 | $col = 'label_attribute_column'; |
| 28 | |
| 29 | } elseif(Swatches::PA_COLOR === $tax->attribute_type) { |
| 30 | $col = 'color_attribute_column'; |
| 31 | |
| 32 | } else { |
| 33 | $col = 'image_attribute_column'; |
| 34 | } |
| 35 | |
| 36 | add_filter('manage_edit-pa_' . $tax->attribute_name . '_columns', [$this, $col]); |
| 37 | add_filter('manage_pa_' . $tax->attribute_name . '_custom_column', [$this, 'add_attr_column_content'], 10, 3); |
| 38 | |
| 39 | add_action('pa_' . $tax->attribute_name . '_add_form_fields', [$this, 'add_attr_field']); |
| 40 | add_action('pa_' . $tax->attribute_name . '_edit_form_fields', [$this, 'edit_attr_field'], 10, 2); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | add_action('created_term', [$this, 'persist_term_meta'], 10, 2); |
| 45 | add_action('edit_term', [$this, 'persist_term_meta'], 10, 2); |
| 46 | add_action('shopengine_attribute_field_chain', [$this, 'attribute_fields'], 10, 3); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | public function add_attr_field($taxonomy) { |
| 51 | |
| 52 | $attr = Helper::get_tax_attribute($taxonomy); |
| 53 | |
| 54 | do_action('shopengine_attribute_field_chain', $attr->attribute_type, '', 'add'); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | public function edit_attr_field($term, $taxonomy) { |
| 59 | |
| 60 | $attr = Helper::get_tax_attribute($taxonomy); |
| 61 | |
| 62 | $value = get_term_meta($term->term_id, $attr->attribute_type, true); |
| 63 | |
| 64 | do_action('shopengine_attribute_field_chain', $attr->attribute_type, $value, 'edit'); |
| 65 | } |
| 66 | |
| 67 | public function label_attribute_column($columns) { |
| 68 | $th = esc_html__('Label', 'shopengine'); |
| 69 | return $this->add_attr_column($columns, $th); |
| 70 | } |
| 71 | |
| 72 | public function image_attribute_column($columns) { |
| 73 | $th = esc_html__('Thumbnail', 'shopengine'); |
| 74 | return $this->add_attr_column($columns, $th); |
| 75 | } |
| 76 | |
| 77 | public function color_attribute_column($columns) { |
| 78 | $th = esc_html__('Color', 'shopengine'); |
| 79 | return $this->add_attr_column($columns, $th); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Add another td in the table |
| 84 | * |
| 85 | * @param $columns |
| 86 | * @return mixed |
| 87 | */ |
| 88 | public function add_attr_column($columns, $th = '') { |
| 89 | $new_columns = []; |
| 90 | |
| 91 | if(isset($columns['cb'])) { |
| 92 | $new_columns['cb'] = $columns['cb']; |
| 93 | } |
| 94 | |
| 95 | $new_columns['pa_preview'] = $th; |
| 96 | unset($columns['cb']); |
| 97 | |
| 98 | return $new_columns + $columns; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | public function add_attr_column_content($columns, $column, $term_id) { |
| 103 | |
| 104 | //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This hook can access only admin and not possible nonce here |
| 105 | if('pa_preview' !== $column && empty($_REQUEST['taxonomy'])) { |
| 106 | return $columns; |
| 107 | } |
| 108 | |
| 109 | //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This hook can access only admin and not possible nonce here |
| 110 | $attr = Helper::get_tax_attribute(sanitize_key($_REQUEST['taxonomy'])); |
| 111 | |
| 112 | $value = get_term_meta($term_id, $attr->attribute_type, true); |
| 113 | |
| 114 | switch($attr->attribute_type) { |
| 115 | case Swatches::PA_COLOR: |
| 116 | printf('<div class="swatches_thumb swatch_color" style="background-color:%s;"></div>', esc_attr($value)); |
| 117 | break; |
| 118 | |
| 119 | case Swatches::PA_IMAGE: |
| 120 | $image = $value ? wp_get_attachment_image_src($value) : ''; |
| 121 | $alt = $image ? trim( strip_tags( get_post_meta( $value, '_wp_attachment_image_alt', true ) ) ) : "Placeholder"; |
| 122 | $image = $image ? $image[0] : Helper::get_dummy(); |
| 123 | printf('<img class="swatches_thumb swatch_image" src="%s" width="44px" height="44px" alt="%s">', esc_url($image),esc_attr__($alt,'shopengine')); |
| 124 | break; |
| 125 | |
| 126 | case Swatches::PA_LABEL: |
| 127 | printf('<div class="swatch_label">%s</div>', esc_html($value)); |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | return $columns; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | public function persist_term_meta($term_id, $tt_id) { |
| 136 | |
| 137 | if( |
| 138 | (!empty($_POST['_wpnonce_add-tag']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce_add-tag'])) ,'add-tag')) || |
| 139 | (!empty($_POST['tag_ID']) && !empty($_POST['_wpnonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])) ,'update-tag_' . sanitize_text_field(wp_unslash($_POST['tag_ID'])))) |
| 140 | ) { |
| 141 | $types = Swatches::instance()->get_available_types(); |
| 142 | |
| 143 | foreach($types as $type => $label) { |
| 144 | if(isset($_POST[$type])) { |
| 145 | if($type == Swatches::PA_COLOR) { |
| 146 | update_term_meta($term_id, $type, sanitize_hex_color(wp_unslash($_POST[$type]))); |
| 147 | } else { |
| 148 | update_term_meta($term_id, $type, sanitize_text_field(wp_unslash($_POST[$type]))); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | public function attribute_fields($type, $value, $form) { |
| 156 | |
| 157 | $types = Swatches::instance()->get_available_types(); |
| 158 | |
| 159 | /** |
| 160 | * Rejecting all types from other plugin/module |
| 161 | * |
| 162 | */ |
| 163 | if(!isset($types[$type])) { |
| 164 | |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | shopengine_content_render(sprintf('<%s class="form-field">%s<label for="term-%s">%s</label>%s', 'edit' == $form ? 'tr' : 'div', 'edit' == $form ? '<th>' : '', esc_attr($type), $types[$type], 'edit' == $form ? '</th><td>' : '')); |
| 169 | |
| 170 | switch($type) { |
| 171 | case Swatches::PA_IMAGE: |
| 172 | $image = $value ? wp_get_attachment_image_src($value) : ''; |
| 173 | $image = $image ? $image[0] : Helper::get_dummy(); |
| 174 | $alt = $value ? trim( strip_tags( get_post_meta( $value, '_wp_attachment_image_alt', true ) ) ): "Upload Image"; |
| 175 | ?> |
| 176 | |
| 177 | <div class="shopengine_term_img_thumbnail" style="float:left;margin-right:10px;"> |
| 178 | <img alt="<?php esc_attr_e($alt,'shopengine'); ?>" src="<?php echo esc_url($image) ?>" width="70px" height="70px"/> |
| 179 | </div> |
| 180 | |
| 181 | <div style="line-height:60px;"> |
| 182 | <input type="hidden" class="shopengine_term_img" name="<?php echo esc_attr($type) ?>" |
| 183 | value="<?php echo esc_attr($value) ?>"/> |
| 184 | |
| 185 | <button type="button" class="shopengine_upload_img_button button"> |
| 186 | <?php esc_html_e('Upload image', 'shopengine'); ?> |
| 187 | </button> |
| 188 | |
| 189 | <button type="button" |
| 190 | class="shopengine_remove_img_btn button <?php echo esc_attr($value) ? '' : 'hidden' ?>"> |
| 191 | <?php esc_html_e('Remove image', 'shopengine'); ?> |
| 192 | </button> |
| 193 | </div> <?php |
| 194 | |
| 195 | break; |
| 196 | |
| 197 | case Swatches::PA_COLOR : ?> |
| 198 | |
| 199 | <input type="color" |
| 200 | id="term-<?php echo esc_attr($type) ?>" |
| 201 | name="<?php echo esc_attr($type) ?>" |
| 202 | value="<?php echo esc_attr($value) ?>"/> <?php |
| 203 | break; |
| 204 | |
| 205 | default: ?> |
| 206 | |
| 207 | <input type="text" |
| 208 | id="term-<?php echo esc_attr($type) ?>" |
| 209 | name="<?php echo esc_attr($type) ?>" |
| 210 | value="<?php echo esc_attr($value) ?>"/> <?php |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | echo 'edit' == $form ? '</td></tr>' : '</div>'; |
| 216 | } |
| 217 | } |
| 218 |