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