Aucune description

class-wc-admin-attributes.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 "Filter Products by Attribute" widget.
  6. *
  7. * @package WooCommerce\Admin
  8. * @version 2.3.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * WC_Admin_Attributes Class.
  13. */
  14. class WC_Admin_Attributes {
  15. /**
  16. * Edited attribute ID.
  17. *
  18. * @var int
  19. */
  20. private static $edited_attribute_id;
  21. /**
  22. * Handles output of the attributes page in admin.
  23. *
  24. * Shows the created attributes and lets you add new ones or edit existing ones.
  25. * The added attributes are stored in the database and can be used for layered navigation.
  26. */
  27. public static function output() {
  28. $result = '';
  29. $action = '';
  30. // Action to perform: add, edit, delete or none.
  31. if ( ! empty( $_POST['add_new_attribute'] ) ) { // WPCS: CSRF ok.
  32. $action = 'add';
  33. } elseif ( ! empty( $_POST['save_attribute'] ) && ! empty( $_GET['edit'] ) ) { // WPCS: CSRF ok.
  34. $action = 'edit';
  35. } elseif ( ! empty( $_GET['delete'] ) ) {
  36. $action = 'delete';
  37. }
  38. switch ( $action ) {
  39. case 'add':
  40. $result = self::process_add_attribute();
  41. break;
  42. case 'edit':
  43. $result = self::process_edit_attribute();
  44. break;
  45. case 'delete':
  46. $result = self::process_delete_attribute();
  47. break;
  48. }
  49. if ( is_wp_error( $result ) ) {
  50. echo '<div id="woocommerce_errors" class="error"><p>' . wp_kses_post( $result->get_error_message() ) . '</p></div>';
  51. }
  52. // Show admin interface.
  53. if ( ! empty( $_GET['edit'] ) ) {
  54. self::edit_attribute();
  55. } else {
  56. self::add_attribute();
  57. }
  58. }
  59. /**
  60. * Get and sanitize posted attribute data.
  61. *
  62. * @return array
  63. */
  64. private static function get_posted_attribute() {
  65. $attribute = array(
  66. 'attribute_label' => isset( $_POST['attribute_label'] ) ? wc_clean( wp_unslash( $_POST['attribute_label'] ) ) : '', // WPCS: input var ok, CSRF ok.
  67. 'attribute_name' => isset( $_POST['attribute_name'] ) ? wc_sanitize_taxonomy_name( wp_unslash( $_POST['attribute_name'] ) ) : '', // WPCS: input var ok, CSRF ok, sanitization ok.
  68. 'attribute_type' => isset( $_POST['attribute_type'] ) ? wc_clean( wp_unslash( $_POST['attribute_type'] ) ) : 'select', // WPCS: input var ok, CSRF ok.
  69. 'attribute_orderby' => isset( $_POST['attribute_orderby'] ) ? wc_clean( wp_unslash( $_POST['attribute_orderby'] ) ) : '', // WPCS: input var ok, CSRF ok.
  70. 'attribute_public' => isset( $_POST['attribute_public'] ) ? 1 : 0, // WPCS: input var ok, CSRF ok.
  71. );
  72. if ( empty( $attribute['attribute_type'] ) ) {
  73. $attribute['attribute_type'] = 'select';
  74. }
  75. if ( empty( $attribute['attribute_label'] ) ) {
  76. $attribute['attribute_label'] = ucfirst( $attribute['attribute_name'] );
  77. }
  78. if ( empty( $attribute['attribute_name'] ) ) {
  79. $attribute['attribute_name'] = wc_sanitize_taxonomy_name( $attribute['attribute_label'] );
  80. }
  81. return $attribute;
  82. }
  83. /**
  84. * Add an attribute.
  85. *
  86. * @return bool|WP_Error
  87. */
  88. private static function process_add_attribute() {
  89. check_admin_referer( 'woocommerce-add-new_attribute' );
  90. $attribute = self::get_posted_attribute();
  91. $args = array(
  92. 'name' => $attribute['attribute_label'],
  93. 'slug' => $attribute['attribute_name'],
  94. 'type' => $attribute['attribute_type'],
  95. 'order_by' => $attribute['attribute_orderby'],
  96. 'has_archives' => $attribute['attribute_public'],
  97. );
  98. $id = wc_create_attribute( $args );
  99. if ( is_wp_error( $id ) ) {
  100. return $id;
  101. }
  102. return true;
  103. }
  104. /**
  105. * Edit an attribute.
  106. *
  107. * @return bool|WP_Error
  108. */
  109. private static function process_edit_attribute() {
  110. $attribute_id = isset( $_GET['edit'] ) ? absint( $_GET['edit'] ) : 0;
  111. check_admin_referer( 'woocommerce-save-attribute_' . $attribute_id );
  112. $attribute = self::get_posted_attribute();
  113. $args = array(
  114. 'name' => $attribute['attribute_label'],
  115. 'slug' => $attribute['attribute_name'],
  116. 'type' => $attribute['attribute_type'],
  117. 'order_by' => $attribute['attribute_orderby'],
  118. 'has_archives' => $attribute['attribute_public'],
  119. );
  120. $id = wc_update_attribute( $attribute_id, $args );
  121. if ( is_wp_error( $id ) ) {
  122. return $id;
  123. }
  124. self::$edited_attribute_id = $id;
  125. return true;
  126. }
  127. /**
  128. * Delete an attribute.
  129. *
  130. * @return bool
  131. */
  132. private static function process_delete_attribute() {
  133. $attribute_id = isset( $_GET['delete'] ) ? absint( $_GET['delete'] ) : 0;
  134. check_admin_referer( 'woocommerce-delete-attribute_' . $attribute_id );
  135. return wc_delete_attribute( $attribute_id );
  136. }
  137. /**
  138. * Edit Attribute admin panel.
  139. *
  140. * Shows the interface for changing an attributes type between select and text.
  141. */
  142. public static function edit_attribute() {
  143. global $wpdb;
  144. $edit = isset( $_GET['edit'] ) ? absint( $_GET['edit'] ) : 0;
  145. $attribute_to_edit = $wpdb->get_row(
  146. $wpdb->prepare(
  147. "
  148. SELECT attribute_type, attribute_label, attribute_name, attribute_orderby, attribute_public
  149. FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d
  150. ",
  151. $edit
  152. )
  153. );
  154. ?>
  155. <div class="wrap woocommerce">
  156. <h1><?php esc_html_e( 'Edit attribute', 'woocommerce' ); ?></h1>
  157. <?php
  158. if ( ! $attribute_to_edit ) {
  159. echo '<div id="woocommerce_errors" class="error"><p>' . esc_html__( 'Error: non-existing attribute ID.', 'woocommerce' ) . '</p></div>';
  160. } else {
  161. if ( self::$edited_attribute_id > 0 ) {
  162. echo '<div id="message" class="updated"><p>' . esc_html__( 'Attribute updated successfully', 'woocommerce' ) . '</p><p><a href="' . esc_url( admin_url( 'edit.php?post_type=product&amp;page=product_attributes' ) ) . '">' . esc_html__( 'Back to Attributes', 'woocommerce' ) . '</a></p></div>';
  163. self::$edited_attribute_id = null;
  164. }
  165. $att_type = $attribute_to_edit->attribute_type;
  166. $att_label = format_to_edit( $attribute_to_edit->attribute_label );
  167. $att_name = $attribute_to_edit->attribute_name;
  168. $att_orderby = $attribute_to_edit->attribute_orderby;
  169. $att_public = $attribute_to_edit->attribute_public;
  170. ?>
  171. <form action="edit.php?post_type=product&amp;page=product_attributes&amp;edit=<?php echo absint( $edit ); ?>" method="post">
  172. <table class="form-table">
  173. <tbody>
  174. <?php do_action( 'woocommerce_before_edit_attribute_fields' ); ?>
  175. <tr class="form-field form-required">
  176. <th scope="row" valign="top">
  177. <label for="attribute_label"><?php esc_html_e( 'Name', 'woocommerce' ); ?></label>
  178. </th>
  179. <td>
  180. <input name="attribute_label" id="attribute_label" type="text" value="<?php echo esc_attr( $att_label ); ?>" />
  181. <p class="description"><?php esc_html_e( 'Name for the attribute (shown on the front-end).', 'woocommerce' ); ?></p>
  182. </td>
  183. </tr>
  184. <tr class="form-field form-required">
  185. <th scope="row" valign="top">
  186. <label for="attribute_name"><?php esc_html_e( 'Slug', 'woocommerce' ); ?></label>
  187. </th>
  188. <td>
  189. <input name="attribute_name" id="attribute_name" type="text" value="<?php echo esc_attr( $att_name ); ?>" maxlength="28" />
  190. <p class="description"><?php esc_html_e( 'Unique slug/reference for the attribute; must be no more than 28 characters.', 'woocommerce' ); ?></p>
  191. </td>
  192. </tr>
  193. <tr class="form-field form-required">
  194. <th scope="row" valign="top">
  195. <label for="attribute_public"><?php esc_html_e( 'Enable archives?', 'woocommerce' ); ?></label>
  196. </th>
  197. <td>
  198. <input name="attribute_public" id="attribute_public" type="checkbox" value="1" <?php checked( $att_public, 1 ); ?> />
  199. <p class="description"><?php esc_html_e( 'Enable this if you want this attribute to have product archives in your store.', 'woocommerce' ); ?></p>
  200. </td>
  201. </tr>
  202. <?php
  203. /**
  204. * Attribute types can change the way attributes are displayed on the frontend and admin.
  205. *
  206. * By Default WooCommerce only includes the `select` type. Others can be added with the
  207. * `product_attributes_type_selector` filter. If there is only the default type registered,
  208. * this setting will be hidden.
  209. */
  210. if ( wc_has_custom_attribute_types() ) {
  211. ?>
  212. <tr class="form-field form-required">
  213. <th scope="row" valign="top">
  214. <label for="attribute_type"><?php esc_html_e( 'Type', 'woocommerce' ); ?></label>
  215. </th>
  216. <td>
  217. <select name="attribute_type" id="attribute_type">
  218. <?php foreach ( wc_get_attribute_types() as $key => $value ) : ?>
  219. <option value="<?php echo esc_attr( $key ); ?>" <?php selected( $att_type, $key ); ?>><?php echo esc_html( $value ); ?></option>
  220. <?php endforeach; ?>
  221. <?php
  222. /**
  223. * Deprecated action in favor of product_attributes_type_selector filter.
  224. *
  225. * @todo Remove in 4.0.0
  226. * @deprecated 2.4.0
  227. */
  228. do_action( 'woocommerce_admin_attribute_types' );
  229. ?>
  230. </select>
  231. <p class="description"><?php esc_html_e( "Determines how this attribute's values are displayed.", 'woocommerce' ); ?></p>
  232. </td>
  233. </tr>
  234. <?php
  235. }
  236. ?>
  237. <tr class="form-field form-required">
  238. <th scope="row" valign="top">
  239. <label for="attribute_orderby"><?php esc_html_e( 'Default sort order', 'woocommerce' ); ?></label>
  240. </th>
  241. <td>
  242. <select name="attribute_orderby" id="attribute_orderby">
  243. <option value="menu_order" <?php selected( $att_orderby, 'menu_order' ); ?>><?php esc_html_e( 'Custom ordering', 'woocommerce' ); ?></option>
  244. <option value="name" <?php selected( $att_orderby, 'name' ); ?>><?php esc_html_e( 'Name', 'woocommerce' ); ?></option>
  245. <option value="name_num" <?php selected( $att_orderby, 'name_num' ); ?>><?php esc_html_e( 'Name (numeric)', 'woocommerce' ); ?></option>
  246. <option value="id" <?php selected( $att_orderby, 'id' ); ?>><?php esc_html_e( 'Term ID', 'woocommerce' ); ?></option>
  247. </select>
  248. <p class="description"><?php esc_html_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>
  249. </td>
  250. </tr>
  251. <?php do_action( 'woocommerce_after_edit_attribute_fields' ); ?>
  252. </tbody>
  253. </table>
  254. <p class="submit"><button type="submit" name="save_attribute" id="submit" class="button-primary" value="<?php esc_attr_e( 'Update', 'woocommerce' ); ?>"><?php esc_html_e( 'Update', 'woocommerce' ); ?></button></p>
  255. <?php wp_nonce_field( 'woocommerce-save-attribute_' . $edit ); ?>
  256. </form>
  257. <?php } ?>
  258. </div>
  259. <?php
  260. }
  261. /**
  262. * Add Attribute admin panel.
  263. *
  264. * Shows the interface for adding new attributes.
  265. */
  266. public static function add_attribute() {
  267. ?>
  268. <div class="wrap woocommerce">
  269. <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
  270. <br class="clear" />
  271. <div id="col-container">
  272. <div id="col-right">
  273. <div class="col-wrap">
  274. <table class="widefat attributes-table wp-list-table ui-sortable" style="width:100%">
  275. <thead>
  276. <tr>
  277. <th scope="col"><?php esc_html_e( 'Name', 'woocommerce' ); ?></th>
  278. <th scope="col"><?php esc_html_e( 'Slug', 'woocommerce' ); ?></th>
  279. <?php if ( wc_has_custom_attribute_types() ) : ?>
  280. <th scope="col"><?php esc_html_e( 'Type', 'woocommerce' ); ?></th>
  281. <?php endif; ?>
  282. <th scope="col"><?php esc_html_e( 'Order by', 'woocommerce' ); ?></th>
  283. <th scope="col"><?php esc_html_e( 'Terms', 'woocommerce' ); ?></th>
  284. </tr>
  285. </thead>
  286. <tbody>
  287. <?php
  288. $attribute_taxonomies = wc_get_attribute_taxonomies();
  289. if ( $attribute_taxonomies ) :
  290. foreach ( $attribute_taxonomies as $tax ) :
  291. ?>
  292. <tr>
  293. <td>
  294. <strong><a href="edit-tags.php?taxonomy=<?php echo esc_attr( wc_attribute_taxonomy_name( $tax->attribute_name ) ); ?>&amp;post_type=product"><?php echo esc_html( $tax->attribute_label ); ?></a></strong>
  295. <div class="row-actions"><span class="edit"><a href="<?php echo esc_url( add_query_arg( 'edit', $tax->attribute_id, 'edit.php?post_type=product&amp;page=product_attributes' ) ); ?>"><?php esc_html_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, 'edit.php?post_type=product&amp;page=product_attributes' ), 'woocommerce-delete-attribute_' . $tax->attribute_id ) ); ?>"><?php esc_html_e( 'Delete', 'woocommerce' ); ?></a></span></div>
  296. </td>
  297. <td><?php echo esc_html( $tax->attribute_name ); ?></td>
  298. <?php if ( wc_has_custom_attribute_types() ) : ?>
  299. <td><?php echo esc_html( wc_get_attribute_type_label( $tax->attribute_type ) ); ?> <?php echo $tax->attribute_public ? esc_html__( '(Public)', 'woocommerce' ) : ''; ?></td>
  300. <?php endif; ?>
  301. <td>
  302. <?php
  303. switch ( $tax->attribute_orderby ) {
  304. case 'name':
  305. esc_html_e( 'Name', 'woocommerce' );
  306. break;
  307. case 'name_num':
  308. esc_html_e( 'Name (numeric)', 'woocommerce' );
  309. break;
  310. case 'id':
  311. esc_html_e( 'Term ID', 'woocommerce' );
  312. break;
  313. default:
  314. esc_html_e( 'Custom ordering', 'woocommerce' );
  315. break;
  316. }
  317. ?>
  318. </td>
  319. <td class="attribute-terms">
  320. <?php
  321. $taxonomy = wc_attribute_taxonomy_name( $tax->attribute_name );
  322. if ( taxonomy_exists( $taxonomy ) ) {
  323. $terms = get_terms( $taxonomy, 'hide_empty=0' );
  324. $terms_string = implode( ', ', wp_list_pluck( $terms, 'name' ) );
  325. if ( $terms_string ) {
  326. echo esc_html( $terms_string );
  327. } else {
  328. echo '<span class="na">&ndash;</span>';
  329. }
  330. } else {
  331. echo '<span class="na">&ndash;</span>';
  332. }
  333. ?>
  334. <br /><a href="edit-tags.php?taxonomy=<?php echo esc_attr( wc_attribute_taxonomy_name( $tax->attribute_name ) ); ?>&amp;post_type=product" class="configure-terms"><?php esc_html_e( 'Configure terms', 'woocommerce' ); ?></a>
  335. </td>
  336. </tr>
  337. <?php
  338. endforeach;
  339. else :
  340. ?>
  341. <tr>
  342. <td colspan="6"><?php esc_html_e( 'No attributes currently exist.', 'woocommerce' ); ?></td>
  343. </tr>
  344. <?php
  345. endif;
  346. ?>
  347. </tbody>
  348. </table>
  349. </div>
  350. </div>
  351. <div id="col-left">
  352. <div class="col-wrap">
  353. <div class="form-wrap">
  354. <h2><?php esc_html_e( 'Add new attribute', 'woocommerce' ); ?></h2>
  355. <p><?php esc_html_e( 'Attributes let you define extra product data, such as size or color. You can use these attributes in the shop sidebar using the "layered nav" widgets.', 'woocommerce' ); ?></p>
  356. <form action="edit.php?post_type=product&amp;page=product_attributes" method="post">
  357. <?php do_action( 'woocommerce_before_add_attribute_fields' ); ?>
  358. <div class="form-field">
  359. <label for="attribute_label"><?php esc_html_e( 'Name', 'woocommerce' ); ?></label>
  360. <input name="attribute_label" id="attribute_label" type="text" value="" />
  361. <p class="description"><?php esc_html_e( 'Name for the attribute (shown on the front-end).', 'woocommerce' ); ?></p>
  362. </div>
  363. <div class="form-field">
  364. <label for="attribute_name"><?php esc_html_e( 'Slug', 'woocommerce' ); ?></label>
  365. <input name="attribute_name" id="attribute_name" type="text" value="" maxlength="28" />
  366. <p class="description"><?php esc_html_e( 'Unique slug/reference for the attribute; must be no more than 28 characters.', 'woocommerce' ); ?></p>
  367. </div>
  368. <div class="form-field">
  369. <label for="attribute_public"><input name="attribute_public" id="attribute_public" type="checkbox" value="1" /> <?php esc_html_e( 'Enable Archives?', 'woocommerce' ); ?></label>
  370. <p class="description"><?php esc_html_e( 'Enable this if you want this attribute to have product archives in your store.', 'woocommerce' ); ?></p>
  371. </div>
  372. <?php
  373. /**
  374. * Attribute types can change the way attributes are displayed on the frontend and admin.
  375. *
  376. * By Default WooCommerce only includes the `select` type. Others can be added with the
  377. * `product_attributes_type_selector` filter. If there is only the default type registered,
  378. * this setting will be hidden.
  379. */
  380. if ( wc_has_custom_attribute_types() ) {
  381. ?>
  382. <div class="form-field">
  383. <label for="attribute_type"><?php esc_html_e( 'Type', 'woocommerce' ); ?></label>
  384. <select name="attribute_type" id="attribute_type">
  385. <?php foreach ( wc_get_attribute_types() as $key => $value ) : ?>
  386. <option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value ); ?></option>
  387. <?php endforeach; ?>
  388. <?php
  389. /**
  390. * Deprecated action in favor of product_attributes_type_selector filter.
  391. *
  392. * @todo Remove in 4.0.0
  393. * @deprecated 2.4.0
  394. */
  395. do_action( 'woocommerce_admin_attribute_types' );
  396. ?>
  397. </select>
  398. <p class="description"><?php esc_html_e( "Determines how this attribute's values are displayed.", 'woocommerce' ); ?></p>
  399. </div>
  400. <?php
  401. }
  402. ?>
  403. <div class="form-field">
  404. <label for="attribute_orderby"><?php esc_html_e( 'Default sort order', 'woocommerce' ); ?></label>
  405. <select name="attribute_orderby" id="attribute_orderby">
  406. <option value="menu_order"><?php esc_html_e( 'Custom ordering', 'woocommerce' ); ?></option>
  407. <option value="name"><?php esc_html_e( 'Name', 'woocommerce' ); ?></option>
  408. <option value="name_num"><?php esc_html_e( 'Name (numeric)', 'woocommerce' ); ?></option>
  409. <option value="id"><?php esc_html_e( 'Term ID', 'woocommerce' ); ?></option>
  410. </select>
  411. <p class="description"><?php esc_html_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>
  412. </div>
  413. <?php do_action( 'woocommerce_after_add_attribute_fields' ); ?>
  414. <p class="submit"><button type="submit" name="add_new_attribute" id="submit" class="button button-primary" value="<?php esc_attr_e( 'Add attribute', 'woocommerce' ); ?>"><?php esc_html_e( 'Add attribute', 'woocommerce' ); ?></button></p>
  415. <?php wp_nonce_field( 'woocommerce-add-new_attribute' ); ?>
  416. </form>
  417. </div>
  418. </div>
  419. </div>
  420. </div>
  421. <script type="text/javascript">
  422. /* <![CDATA[ */
  423. jQuery( 'a.delete' ).on( 'click', function() {
  424. if ( window.confirm( '<?php esc_html_e( 'Are you sure you want to delete this attribute?', 'woocommerce' ); ?>' ) ) {
  425. return true;
  426. }
  427. return false;
  428. });
  429. /* ]]> */
  430. </script>
  431. </div>
  432. <?php
  433. }
  434. }