assets
4 years ago
admin-product.php
5 years ago
attribute-hooks.php
4 years ago
frontend.php
5 years ago
helper.php
5 years ago
swatches.php
5 years ago
admin-product.php
204 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Modules\Swatches; |
| 4 | |
| 5 | use ShopEngine\Traits\Singleton; |
| 6 | |
| 7 | defined('ABSPATH') || exit; |
| 8 | |
| 9 | |
| 10 | class Admin_Product |
| 11 | { |
| 12 | |
| 13 | use Singleton; |
| 14 | |
| 15 | |
| 16 | public function init() { |
| 17 | |
| 18 | add_action('woocommerce_product_option_terms', [$this, 'get_option_terms_product'], 10, 2); |
| 19 | |
| 20 | add_action('wp_ajax_shopengine_add_new_attribute', [$this, 'add_attribute_by_ajax']); |
| 21 | |
| 22 | add_action('admin_footer', [$this, 'shopengine_term_template']); |
| 23 | } |
| 24 | |
| 25 | |
| 26 | public function get_option_terms_product($taxonomy, $index) { |
| 27 | |
| 28 | $types = Swatches::instance()->get_available_types(); |
| 29 | |
| 30 | if(!array_key_exists($taxonomy->attribute_type, $types)) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | $taxonomy_name = wc_attribute_taxonomy_name($taxonomy->attribute_name); |
| 35 | |
| 36 | global $thepostid; |
| 37 | |
| 38 | $product_id = isset($_POST['post_id']) ? absint($_POST['post_id']) : $thepostid; ?> |
| 39 | |
| 40 | <select multiple="multiple" data-placeholder="<?php esc_attr_e('Select terms', 'shopengine'); ?>" |
| 41 | class="multiselect attribute_values wc-enhanced-select" |
| 42 | name="attribute_values[<?php echo esc_attr( $index ); ?>][]"> |
| 43 | <?php |
| 44 | |
| 45 | $all_terms = get_terms($taxonomy_name, apply_filters('woocommerce_product_attribute_terms', ['orderby' => 'name', 'hide_empty' => false])); |
| 46 | |
| 47 | if($all_terms) { |
| 48 | foreach($all_terms as $term) { |
| 49 | echo '<option value="' . esc_attr($term->term_id) . '" ' . selected(has_term(absint($term->term_id), $taxonomy_name, $product_id), true, false) . '>' . esc_attr(apply_filters('woocommerce_product_attribute_term_name', $term->name, $term)) . '</option>'; |
| 50 | } |
| 51 | } |
| 52 | ?> |
| 53 | </select> |
| 54 | <button class="button plus select_all_attributes"><?php esc_html_e('Select all', 'shopengine'); ?></button> |
| 55 | <button class="button minus select_no_attributes"><?php esc_html_e('Select none', 'shopengine'); ?></button> |
| 56 | <button class="button fr plus shopengine_assign_new_attribute" data-type="<?php echo esc_attr( $taxonomy->attribute_type ) ?>"> |
| 57 | <?php esc_html_e('Add new', 'shopengine'); ?> |
| 58 | </button> |
| 59 | |
| 60 | <?php |
| 61 | } |
| 62 | |
| 63 | |
| 64 | public function add_attribute_by_ajax() { |
| 65 | |
| 66 | $nonce = isset($_POST['nonce']) ? sanitize_key($_POST['nonce']) : ''; |
| 67 | $tax = isset($_POST['taxonomy']) ? sanitize_key($_POST['taxonomy']) : ''; |
| 68 | $type = isset($_POST['type']) ? sanitize_key($_POST['type']) : ''; |
| 69 | $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : ''; |
| 70 | $slug = isset($_POST['slug']) ? sanitize_key($_POST['slug']) : ''; |
| 71 | $swatch = isset($_POST['swatch']) ? sanitize_text_field($_POST['swatch']) : ''; |
| 72 | |
| 73 | if(!wp_verify_nonce($nonce, 'shopengine_nonce_add_attribute')) { |
| 74 | wp_send_json_error(esc_html__('Request denied', 'shopengine')); |
| 75 | } |
| 76 | |
| 77 | if(empty($name) || empty($swatch) || empty($tax) || empty($type)) { |
| 78 | wp_send_json_error(esc_html__('Insufficient data', 'shopengine')); |
| 79 | } |
| 80 | |
| 81 | if(!taxonomy_exists($tax)) { |
| 82 | wp_send_json_error(esc_html__('Taxonomy is not exists', 'shopengine')); |
| 83 | } |
| 84 | |
| 85 | if(term_exists($name, $tax)) { |
| 86 | wp_send_json_error(esc_html__('Term already exists', 'shopengine')); |
| 87 | } |
| 88 | |
| 89 | $term = wp_insert_term($name, $tax, ['slug' => $slug]); |
| 90 | |
| 91 | if(is_wp_error($term)) { |
| 92 | |
| 93 | wp_send_json_error($term->get_error_message()); |
| 94 | |
| 95 | } else { |
| 96 | $term = get_term_by('id', $term['term_id'], $tax); |
| 97 | update_term_meta($term->term_id, $type, $swatch); |
| 98 | } |
| 99 | |
| 100 | wp_send_json_success( |
| 101 | [ |
| 102 | 'msg' => esc_html__('Successfully added', 'shopengine'), |
| 103 | 'id' => $term->term_id, |
| 104 | 'slug' => $term->slug, |
| 105 | 'name' => $term->name, |
| 106 | ] |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | public function shopengine_term_template() { |
| 112 | |
| 113 | global $pagenow, $post; |
| 114 | |
| 115 | if($pagenow != 'post.php' || (isset($post) && get_post_type($post->ID) != 'product')) { |
| 116 | return; |
| 117 | } |
| 118 | ?> |
| 119 | |
| 120 | <div id="shopengine_tpl_modal" class="shopengine_modal_container"> |
| 121 | <div class="shopengine_modal"> |
| 122 | <button type="button" class="button-link media-modal-close shopengine_modal__close"> |
| 123 | <span class="media-modal-icon"></span></button> |
| 124 | <div class="shopengine_modal__header"><h2><?php esc_html_e('Add new term', 'shopengine') ?></h2></div> |
| 125 | <div class="shopengine_modal__content"> |
| 126 | <p class="shopengine_term__name"> |
| 127 | <label> |
| 128 | <?php esc_html_e('Name', 'shopengine') ?> |
| 129 | <input type="text" class="shopengine__input" name="name"> |
| 130 | </label> |
| 131 | </p> |
| 132 | <p class="shopengine_term__slug"> |
| 133 | <label> |
| 134 | <?php esc_html_e('Slug', 'shopengine') ?> |
| 135 | <input type="text" class="shopengine__input" name="slug"> |
| 136 | </label> |
| 137 | </p> |
| 138 | <div class="shopengine_term__swatch"> |
| 139 | |
| 140 | </div> |
| 141 | <div class="hidden shopengine_term__tax"></div> |
| 142 | |
| 143 | <input type="hidden" class="shopengine__input" name="nonce" |
| 144 | value="<?php echo wp_create_nonce('shopengine_nonce_add_attribute') ?>"> |
| 145 | </div> |
| 146 | <div class="shopengine_modal__footer"> |
| 147 | <button class="button button-secondary shopengine_modal__close"><?php esc_html_e('Cancel', 'shopengine') ?></button> |
| 148 | <button class="button button-primary shopengine_add_attribute_submit"><?php esc_html_e('Add New', 'shopengine') ?></button> |
| 149 | <span class="message"></span> |
| 150 | <span class="spinner"></span> |
| 151 | </div> |
| 152 | </div> |
| 153 | <div class="shopengine_modal__backdrop media-modal-backdrop"></div> |
| 154 | </div> |
| 155 | |
| 156 | <script type="text/template" id="tmpl-shopengine__tpl_input__color"> |
| 157 | |
| 158 | <label><?php esc_html_e('Color', 'shopengine') ?></label><br> |
| 159 | <input type="text" class="shopengine__input shopengine_input__color" name="swatch"> |
| 160 | |
| 161 | </script> |
| 162 | |
| 163 | <script type="text/template" id="tmpl-shopengine__tpl_input__image"> |
| 164 | |
| 165 | <label><?php esc_html_e('Image', 'shopengine') ?></label><br> |
| 166 | |
| 167 | <div class="shopengine_term_img_thumbnail" style="float:left;margin-right:10px;"> |
| 168 | <img src="<?php echo esc_url(Helper::get_dummy()) ?>" width="60px" height="60px"/> |
| 169 | </div> |
| 170 | |
| 171 | <div style="line-height:60px;"> |
| 172 | <input type="hidden" class="shopengine__input shopengine_input__image shopengine_term_img" name="swatch" value=""/> |
| 173 | |
| 174 | <button type="button" class="shopengine_upload_img_button button"> |
| 175 | <?php esc_html_e('Upload/Add image', 'shopengine'); ?> |
| 176 | </button> |
| 177 | |
| 178 | <button type="button" class="shopengine_remove_img_btn button hidden"> |
| 179 | <?php esc_html_e('Remove image', 'shopengine'); ?> |
| 180 | </button> |
| 181 | </div> |
| 182 | |
| 183 | </script> |
| 184 | |
| 185 | <script type="text/template" id="tmpl-shopengine__tpl_input__label"> |
| 186 | |
| 187 | <label> |
| 188 | <?php esc_html_e('Label', 'shopengine') ?> |
| 189 | <input type="text" class="shopengine__input shopengine_input__label" name="swatch"> |
| 190 | </label> |
| 191 | |
| 192 | </script> |
| 193 | |
| 194 | <script type="text/template" id="tmpl-shopengine__tpl_input__tax"> |
| 195 | |
| 196 | <input type="hidden" class="shopengine__input" name="taxonomy" value="{{data.tax}}"> |
| 197 | <input type="hidden" class="shopengine__input" name="type" value="{{data.type}}"> |
| 198 | |
| 199 | </script> |
| 200 | <?php |
| 201 | } |
| 202 | } |
| 203 | |
| 204 |