1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
<?php /** * Attributes Page * * The attributes section lets users add custom attributes to assign to products - they can also be used in the layered nav widget. * * @author WooThemes * @category Admin * @package WooCommerce/Admin * @version 2.1.0 */
if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly }
/** * WC_Admin_Attributes Class */ class WC_Admin_Attributes {
/** * Handles output of the attributes page in admin. * * Shows the created attributes and lets you add new ones or edit existing ones. * The added attributes are stored in the database and can be used for layered navigation. */ public static function output() { global $wpdb;
// Action to perform: add, edit, delete or none $action = ''; if ( ! empty( $_POST['add_new_attribute'] ) ) { $action = 'add'; } elseif ( ! empty( $_POST['save_attribute'] ) && ! empty( $_GET['edit'] ) ) { $action = 'edit'; } elseif ( ! empty( $_GET['delete'] ) ) { $action = 'delete'; }
// Add or edit an attribute if ( 'add' === $action || 'edit' === $action ) {
// Security check if ( 'add' === $action ) { check_admin_referer( 'woocommerce-add-new_attribute' ); } if ( 'edit' === $action ) { $attribute_id = absint( $_GET['edit'] ); check_admin_referer( 'woocommerce-save-attribute_' . $attribute_id ); }
// Grab the submitted data $attribute_label = ( isset( $_POST['attribute_label'] ) ) ? (string) stripslashes( $_POST['attribute_label'] ) : ''; $attribute_name = ( isset( $_POST['attribute_name'] ) ) ? wc_sanitize_taxonomy_name( stripslashes( (string) $_POST['attribute_name'] ) ) : ''; $attribute_type = ( isset( $_POST['attribute_type'] ) ) ? (string) stripslashes( $_POST['attribute_type'] ) : ''; $attribute_orderby = ( isset( $_POST['attribute_orderby'] ) ) ? (string) stripslashes( $_POST['attribute_orderby'] ) : '';
// Auto-generate the label or slug if only one of both was provided if ( ! $attribute_label ) { $attribute_label = ucfirst( $attribute_name ); } if ( ! $attribute_name ) { $attribute_name = wc_sanitize_taxonomy_name( stripslashes( $attribute_label ) ); }
// Forbidden attribute names // http://codex.wordpress.org/Function_Reference/register_taxonomy#Reserved_Terms $reserved_terms = array( 'attachment', 'attachment_id', 'author', 'author_name', 'calendar', 'cat', 'category', 'category__and', 'category__in', 'category__not_in', 'category_name', 'comments_per_page', 'comments_popup', 'cpage', 'day', 'debug', 'error', 'exact', 'feed', 'hour', 'link_category', 'm', 'minute', 'monthnum', 'more', 'name', 'nav_menu', 'nopaging', 'offset', 'order', 'orderby', 'p', 'page', 'page_id', 'paged', 'pagename', 'pb', 'perm', 'post', 'post__in', 'post__not_in', 'post_format', 'post_mime_type', 'post_status', 'post_tag', 'post_type', 'posts', 'posts_per_archive_page', 'posts_per_page', 'preview', 'robots', 's', 'search', 'second', 'sentence', 'showposts', 'static', 'subpost', 'subpost_id', 'tag', 'tag__and', 'tag__in', 'tag__not_in', 'tag_id', 'tag_slug__and', 'tag_slug__in', 'taxonomy', 'tb', 'term', 'type', 'w', 'withcomments', 'withoutcomments', 'year', );
// Error checking if ( ! $attribute_name || ! $attribute_label || ! $attribute_type ) { $error = __( 'Please, provide an attribute name, slug and type.', 'woocommerce' ); } elseif ( strlen( $attribute_name ) >= 28 ) { $error = sprintf( __( 'Slug “%s” is too long (28 characters max). Shorten it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) ); } elseif ( in_array( $attribute_name, $reserved_terms ) ) { $error = sprintf( __( 'Slug “%s” is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) ); } else { $taxonomy_exists = taxonomy_exists( wc_attribute_taxonomy_name( $attribute_name ) );
if ( 'add' === $action && $taxonomy_exists ) { $error = sprintf( __( 'Slug “%s” is already in use. Change it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) ); } if ( 'edit' === $action ) { $old_attribute_name = $wpdb->get_var( "SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = $attribute_id" ); if ( $old_attribute_name != $attribute_name && wc_sanitize_taxonomy_name( $old_attribute_name ) != $attribute_name && $taxonomy_exists ) { $error = sprintf( __( 'Slug “%s” is already in use. Change it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) ); } } }
// Show the error message if any if ( ! empty( $error ) ) { echo '<div id="woocommerce_errors" class="error fade"><p>' . $error . '</p></div>'; } else {
// Add new attribute if ( 'add' === $action ) {
$attribute = array( 'attribute_label' => $attribute_label, 'attribute_name' => $attribute_name, 'attribute_type' => $attribute_type, 'attribute_orderby' => $attribute_orderby, );
$wpdb->insert( $wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute );
do_action( 'woocommerce_attribute_added', $wpdb->insert_id, $attribute );
$action_completed = true; }
// Edit existing attribute if ( 'edit' === $action ) {
$attribute = array( 'attribute_label' => $attribute_label, 'attribute_name' => $attribute_name, 'attribute_type' => $attribute_type, 'attribute_orderby' => $attribute_orderby, );
$wpdb->update( $wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute, array( 'attribute_id' => $attribute_id ) );
do_action( 'woocommerce_attribute_updated', $attribute_id, $attribute, $old_attribute_name );
if ( $old_attribute_name != $attribute_name && ! empty( $old_attribute_name ) ) { // Update taxonomies in the wp term taxonomy table $wpdb->update( $wpdb->term_taxonomy, array( 'taxonomy' => wc_attribute_taxonomy_name( $attribute_name ) ), array( 'taxonomy' => 'pa_' . $old_attribute_name ) );
// Update taxonomy ordering term meta $wpdb->update( $wpdb->prefix . 'woocommerce_termmeta', array( 'meta_key' => 'order_pa_' . sanitize_title( $attribute_name ) ), array( 'meta_key' => 'order_pa_' . sanitize_title( $old_attribute_name ) ) );
// Update product attributes which use this taxonomy $old_attribute_name_length = strlen( $old_attribute_name ) + 3; $attribute_name_length = strlen( $attribute_name ) + 3;
$wpdb->query( " UPDATE {$wpdb->postmeta} SET meta_value = REPLACE( meta_value, 's:{$old_attribute_name_length}:\"pa_{$old_attribute_name}\"', 's:{$attribute_name_length}:\"pa_{$attribute_name}\"' ) WHERE meta_key = '_product_attributes'" );
// Update variations which use this taxonomy $wpdb->update( $wpdb->postmeta, array( 'meta_key' => 'attribute_pa_' . sanitize_title( $attribute_name ) ), array( 'meta_key' => 'attribute_pa_' . sanitize_title( $old_attribute_name ) ) ); }
$action_completed = true; }
flush_rewrite_rules(); } }
// Delete an attribute if ( 'delete' === $action ) { // Security check $attribute_id = absint( $_GET['delete'] ); check_admin_referer( 'woocommerce-delete-attribute_' . $attribute_id );
$attribute_name = $wpdb->get_var( "SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = $attribute_id" );
if ( $attribute_name && $wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = $attribute_id" ) ) {
$taxonomy = wc_attribute_taxonomy_name( $attribute_name );
if ( taxonomy_exists( $taxonomy ) ) { $terms = get_terms( $taxonomy, 'orderby=name&hide_empty=0' ); foreach ( $terms as $term ) { wp_delete_term( $term->term_id, $taxonomy ); } }
do_action( 'woocommerce_attribute_deleted', $attribute_id, $attribute_name, $taxonomy );
$action_completed = true; } }
// If an attribute was added, edited or deleted: clear cache if ( ! empty( $action_completed ) ) { delete_transient( 'wc_attribute_taxonomies' ); }
// Show admin interface if ( ! empty( $_GET['edit'] ) ) self::edit_attribute(); else self::add_attribute(); }
/** * Edit Attribute admin panel * * Shows the interface for changing an attributes type between select and text */ public static function edit_attribute() { global $wpdb;
$edit = absint( $_GET['edit'] );
$attribute_to_edit = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_id = '$edit'");
$att_type = $attribute_to_edit->attribute_type; $att_label = $attribute_to_edit->attribute_label; $att_name = $attribute_to_edit->attribute_name; $att_orderby = $attribute_to_edit->attribute_orderby; ?> <div class="wrap woocommerce"> <div class="icon32 icon32-attributes" id="icon-woocommerce"><br/></div> <h2><?php _e( 'Edit Attribute', 'woocommerce' ) ?></h2> <form action="admin.php?page=product_attributes&edit=<?php echo absint( $edit ); ?>" method="post"> <table class="form-table"> <tbody> <tr class="form-field form-required"> <th scope="row" valign="top"> <label for="attribute_label"><?php _e( 'Name', 'woocommerce' ); ?></label> </th> <td> <input name="attribute_label" id="attribute_label" type="text" value="<?php echo esc_attr( $att_label ); ?>" /> <p class="description"><?php _e( 'Name for the attribute (shown on the front-end).', 'woocommerce' ); ?></p> </td> </tr> <tr class="form-field form-required"> <th scope="row" valign="top"> <label for="attribute_name"><?php _e( 'Slug', 'woocommerce' ); ?></label> </th> <td> <input name="attribute_name" id="attribute_name" type="text" value="<?php echo esc_attr( $att_name ); ?>" maxlength="28" /> <p class="description"><?php _e( 'Unique slug/reference for the attribute; must be shorter than 28 characters.', 'woocommerce' ); ?></p> </td> </tr> <tr class="form-field form-required"> <th scope="row" valign="top"> <label for="attribute_type"><?php _e( 'Type', 'woocommerce' ); ?></label> </th> <td> <select name="attribute_type" id="attribute_type"> <option value="select" <?php selected( $att_type, 'select' ); ?>><?php _e( 'Select', 'woocommerce' ) ?></option> <option value="text" <?php selected( $att_type, 'text' ); ?>><?php _e( 'Text', 'woocommerce' ) ?></option> <?php do_action('woocommerce_admin_attribute_types'); ?> </select> <p class="description"><?php _e( 'Determines how you select attributes for products. Under admin panel -> products -> product data -> attributes -> values, <strong>Text</strong> allows manual entry whereas <strong>select</strong> allows pre-configured terms in a drop-down list.', 'woocommerce' ); ?></p> </td> </tr> <tr class="form-field form-required"> <th scope="row" valign="top"> <label for="attribute_orderby"><?php _e( 'Default sort order', 'woocommerce' ); ?></label> </th> <td> <select name="attribute_orderby" id="attribute_orderby"> <option value="menu_order" <?php selected( $att_orderby, 'menu_order' ); ?>><?php _e( 'Custom ordering', 'woocommerce' ) ?></option> <option value="name" <?php selected( $att_orderby, 'name' ); ?>><?php _e( 'Name', 'woocommerce' ) ?></option> <option value="id" <?php selected( $att_orderby, 'id' ); ?>><?php _e( 'Term ID', 'woocommerce' ) ?></option> </select> <p class="description"><?php _e( 'Determines the sort order of the terms on the frontend shop product pages. If using custom ordering, you can drag and drop the terms in this attribute.', 'woocommerce' ); ?></p> </td> </tr> </tbody> </table> <p class="submit"><input type="submit" name="save_attribute" id="submit" class="button-primary" value="<?php _e( 'Update', 'woocommerce' ); ?>"></p> <?php wp_nonce_field( 'woocommerce-save-attribute_' . $edit ); ?> </form> </div> <?php }
/** * Add Attribute admin panel * * Shows the interface for adding new attributes */ public static function add_attribute() { ?> <div class="wrap woocommerce"> <div class="icon32 icon32-attributes" id="icon-woocommerce"><br/></div> <h2><?php _e( 'Attributes', 'woocommerce' ) ?></h2> <br class="clear" /> <div id="col-container"> <div id="col-right"> <div class="col-wrap"> <table class="widefat attributes-table wp-list-table ui-sortable" style="width:100%"> <thead> <tr> <th scope="col"><?php _e( 'Name', 'woocommerce' ) ?></th> <th scope="col"><?php _e( 'Slug', 'woocommerce' ) ?></th> <th scope="col"><?php _e( 'Type', 'woocommerce' ) ?></th> <th scope="col"><?php _e( 'Order by', 'woocommerce' ) ?></th> <th scope="col" colspan="2"><?php _e( 'Terms', 'woocommerce' ) ?></th> </tr> </thead> <tbody> <?php $attribute_taxonomies = wc_get_attribute_taxonomies(); if ( $attribute_taxonomies ) : foreach ($attribute_taxonomies as $tax) : ?><tr>
<td><a href="edit-tags.php?taxonomy=<?php echo esc_html(wc_attribute_taxonomy_name($tax->attribute_name)); ?>&post_type=product"><?php echo esc_html( $tax->attribute_label ); ?></a>
<div class="row-actions"><span class="edit"><a href="<?php echo esc_url( add_query_arg('edit', $tax->attribute_id, 'admin.php?page=product_attributes') ); ?>"><?php _e( 'Edit', 'woocommerce' ); ?></a> | </span><span class="delete"><a class="delete" href="<?php echo esc_url( wp_nonce_url( add_query_arg('delete', $tax->attribute_id, 'admin.php?page=product_attributes'), 'woocommerce-delete-attribute_' . $tax->attribute_id ) ); ?>"><?php _e( 'Delete', 'woocommerce' ); ?></a></span></div> </td> <td><?php echo esc_html( $tax->attribute_name ); ?></td> <td><?php echo esc_html( ucfirst( $tax->attribute_type ) ); ?></td> <td><?php switch ( $tax->attribute_orderby ) { case 'name' : _e( 'Name', 'woocommerce' ); break; case 'id' : _e( 'Term ID', 'woocommerce' ); break; default: _e( 'Custom ordering', 'woocommerce' ); break; } ?></td> <td class="attribute-terms"><?php if (taxonomy_exists(wc_attribute_taxonomy_name($tax->attribute_name))) : $terms_array = array(); $terms = get_terms( wc_attribute_taxonomy_name($tax->attribute_name), 'orderby=name&hide_empty=0' ); if ($terms) : foreach ($terms as $term) : $terms_array[] = $term->name; endforeach; echo implode(', ', $terms_array); else : echo '<span class="na">–</span>'; endif; else : echo '<span class="na">–</span>'; endif; ?></td> <td class="attribute-actions"><a href="edit-tags.php?taxonomy=<?php echo esc_html(wc_attribute_taxonomy_name($tax->attribute_name)); ?>&post_type=product" class="button alignright tips configure-terms" data-tip="<?php _e( 'Configure terms', 'woocommerce' ); ?>"><?php _e( 'Configure terms', 'woocommerce' ); ?></a></td> </tr><?php endforeach; else : ?><tr><td colspan="6"><?php _e( 'No attributes currently exist.', 'woocommerce' ) ?></td></tr><?php endif; ?> </tbody> </table> </div> </div> <div id="col-left"> <div class="col-wrap"> <div class="form-wrap"> <h3><?php _e( 'Add New Attribute', 'woocommerce' ) ?></h3> <p><?php _e( 'Attributes let you define extra product data, such as size or colour. You can use these attributes in the shop sidebar using the "layered nav" widgets. Please note: you cannot rename an attribute later on.', 'woocommerce' ) ?></p> <form action="admin.php?page=product_attributes" method="post"> <div class="form-field"> <label for="attribute_label"><?php _e( 'Name', 'woocommerce' ); ?></label> <input name="attribute_label" id="attribute_label" type="text" value="" /> <p class="description"><?php _e( 'Name for the attribute (shown on the front-end).', 'woocommerce' ); ?></p> </div>
<div class="form-field"> <label for="attribute_name"><?php _e( 'Slug', 'woocommerce' ); ?></label> <input name="attribute_name" id="attribute_name" type="text" value="" maxlength="28" /> <p class="description"><?php _e( 'Unique slug/reference for the attribute; must be shorter than 28 characters.', 'woocommerce' ); ?></p> </div>
<div class="form-field"> <label for="attribute_type"><?php _e( 'Type', 'woocommerce' ); ?></label> <select name="attribute_type" id="attribute_type"> <option value="select"><?php _e( 'Select', 'woocommerce' ) ?></option> <option value="text"><?php _e( 'Text', 'woocommerce' ) ?></option> <?php do_action('woocommerce_admin_attribute_types'); ?> </select> <p class="description"><?php _e( 'Determines how you select attributes for products. Under admin panel -> products -> product data -> attributes -> values, <strong>Text</strong> allows manual entry whereas <strong>select</strong> allows pre-configured terms in a drop-down list.', 'woocommerce' ); ?></p> </div>
<div class="form-field"> <label for="attribute_orderby"><?php _e( 'Default sort order', 'woocommerce' ); ?></label> <select name="attribute_orderby" id="attribute_orderby"> <option value="menu_order"><?php _e( 'Custom ordering', 'woocommerce' ) ?></option> <option value="name"><?php _e( 'Name', 'woocommerce' ) ?></option> <option value="id"><?php _e( 'Term ID', 'woocommerce' ) ?></option> </select> <p class="description"><?php _e( 'Determines the sort order of the terms on the frontend shop product pages. If using custom ordering, you can drag and drop the terms in this attribute.', 'woocommerce' ); ?></p> </div>
<p class="submit"><input type="submit" name="add_new_attribute" id="submit" class="button" value="<?php _e( 'Add Attribute', 'woocommerce' ); ?>"></p> <?php wp_nonce_field( 'woocommerce-add-new_attribute' ); ?> </form> </div> </div> </div> </div> <script type="text/javascript"> /* <![CDATA[ */
jQuery('a.delete').click(function(){ var answer = confirm ("<?php _e( 'Are you sure you want to delete this attribute?', 'woocommerce' ); ?>"); if (answer) return true; return false; });
/* ]]> */ </script> </div> <?php } }
|