PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 4.8.5
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v4.8.5
4.9.1 4.9.0 2.0.0 2.1.0 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.1 3.0.0 3.1.0 3.1.1 4.0.0 4.0.1 4.1.0 4.1.1 4.2.0 4.2.1 4.3.0 4.3.1 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.6.8 4.6.9 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.8.9 trunk 0.1.2-beta 0.1.3-beta 0.1.4-beta 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.5.0 1.5.1 1.6.0 1.6.1 1.7.0 1.8.0 1.8.1 1.9.0
shopengine / modules / swatches / admin-product.php
shopengine / modules / swatches Last commit date
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