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